blob: d3b74ba62b4a07bb1702eca5e62f28dcea017ade [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
yakui_zhao342dc382009-06-02 14:12:00 +080040#define I915_SDVO "i915_sdvo"
Jesse Barnes79e53942008-11-07 14:24:08 -080041struct intel_sdvo_priv {
Keith Packardf9c10a92009-05-30 12:16:25 -070042 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080043
44 /* Register for the SDVO device: SDVOB or SDVOC */
Jesse Barnes79e53942008-11-07 14:24:08 -080045 int output_device;
46
Jesse Barnese2f0ba92009-02-02 15:11:52 -080047 /* Active outputs controlled by this SDVO output */
48 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Jesse Barnese2f0ba92009-02-02 15:11:52 -080050 /*
51 * Capabilities of the SDVO device returned by
52 * i830_sdvo_get_capabilities()
53 */
Jesse Barnes79e53942008-11-07 14:24:08 -080054 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080055
56 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080057 int pixel_clock_min, pixel_clock_max;
58
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080059 /*
60 * For multiple function SDVO device,
61 * this is for current attached outputs.
62 */
63 uint16_t attached_output;
64
Jesse Barnese2f0ba92009-02-02 15:11:52 -080065 /**
66 * This is set if we're going to treat the device as TV-out.
67 *
68 * While we have these nice friendly flags for output types that ought
69 * to decide this for us, the S-Video output on our HDMI+S-Video card
70 * shows up as RGB1 (VGA).
71 */
72 bool is_tv;
73
74 /**
75 * This is set if we treat the device as HDMI, instead of DVI.
76 */
77 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +080078
Ma Ling7086c872009-05-13 11:20:06 +080079 /**
80 * This is set if we detect output of sdvo device as LVDS.
81 */
82 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080083
84 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +080085 * This is sdvo flags for input timing.
86 */
87 uint8_t sdvo_flags;
88
89 /**
90 * This is sdvo fixed pannel mode pointer
91 */
92 struct drm_display_mode *sdvo_lvds_fixed_mode;
93
94 /**
Jesse Barnese2f0ba92009-02-02 15:11:52 -080095 * Returned SDTV resolutions allowed for the current format, if the
96 * device reported it.
97 */
98 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
99
100 /**
101 * Current selected TV format.
102 *
103 * This is stored in the same structure that's passed to the device, for
104 * convenience.
105 */
106 struct intel_sdvo_tv_format tv_format;
107
108 /*
109 * supported encoding mode, used to determine whether HDMI is
110 * supported
111 */
112 struct intel_sdvo_encode encode;
113
114 /* DDC bus used by this SDVO output */
115 uint8_t ddc_bus;
116
Jesse Barnes79e53942008-11-07 14:24:08 -0800117 int save_sdvo_mult;
118 u16 save_active_outputs;
119 struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
120 struct intel_sdvo_dtd save_output_dtd[16];
121 u32 save_SDVOX;
122};
123
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800124static bool
125intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags);
126
Jesse Barnes79e53942008-11-07 14:24:08 -0800127/**
128 * Writes the SDVOB or SDVOC with the given value, but always writes both
129 * SDVOB and SDVOC to work around apparent hardware issues (according to
130 * comments in the BIOS).
131 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100132static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800133{
134 struct drm_device *dev = intel_output->base.dev;
135 struct drm_i915_private *dev_priv = dev->dev_private;
136 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
137 u32 bval = val, cval = val;
138 int i;
139
140 if (sdvo_priv->output_device == SDVOB) {
141 cval = I915_READ(SDVOC);
142 } else {
143 bval = I915_READ(SDVOB);
144 }
145 /*
146 * Write the registers twice for luck. Sometimes,
147 * writing them only once doesn't appear to 'stick'.
148 * The BIOS does this too. Yay, magic
149 */
150 for (i = 0; i < 2; i++)
151 {
152 I915_WRITE(SDVOB, bval);
153 I915_READ(SDVOB);
154 I915_WRITE(SDVOC, cval);
155 I915_READ(SDVOC);
156 }
157}
158
159static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
160 u8 *ch)
161{
162 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
163 u8 out_buf[2];
164 u8 buf[2];
165 int ret;
166
167 struct i2c_msg msgs[] = {
168 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700169 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800170 .flags = 0,
171 .len = 1,
172 .buf = out_buf,
173 },
174 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700175 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 .flags = I2C_M_RD,
177 .len = 1,
178 .buf = buf,
179 }
180 };
181
182 out_buf[0] = addr;
183 out_buf[1] = 0;
184
Keith Packard308cd3a2009-06-14 11:56:18 -0700185 if ((ret = i2c_transfer(intel_output->i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800186 {
187 *ch = buf[0];
188 return true;
189 }
190
191 DRM_DEBUG("i2c transfer returned %d\n", ret);
192 return false;
193}
194
195static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
196 u8 ch)
197{
Keith Packardf9c10a92009-05-30 12:16:25 -0700198 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800199 u8 out_buf[2];
200 struct i2c_msg msgs[] = {
201 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700202 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800203 .flags = 0,
204 .len = 2,
205 .buf = out_buf,
206 }
207 };
208
209 out_buf[0] = addr;
210 out_buf[1] = ch;
211
Keith Packardf9c10a92009-05-30 12:16:25 -0700212 if (i2c_transfer(intel_output->i2c_bus, msgs, 1) == 1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800213 {
214 return true;
215 }
216 return false;
217}
218
219#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
220/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100221static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800222 u8 cmd;
223 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800224} sdvo_cmd_names[] = {
225 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
226 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
227 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
228 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
229 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
230 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
231 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
232 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
233 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
234 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
235 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
236 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
237 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
238 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
239 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
240 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
241 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
242 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
243 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
244 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
245 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
246 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
247 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
248 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
249 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
250 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
251 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
252 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
253 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
254 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
255 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
256 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
257 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
258 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
259 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800260 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
261 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
262 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
263 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800264 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800265 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
266 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
267 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
268 /* HDMI op code */
269 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
270 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
271 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
272 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
273 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
274 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
275 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
276 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
277 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
278 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
279 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
280 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
281 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
282 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
283 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800289};
290
291#define SDVO_NAME(dev_priv) ((dev_priv)->output_device == SDVOB ? "SDVOB" : "SDVOC")
292#define SDVO_PRIV(output) ((struct intel_sdvo_priv *) (output)->dev_priv)
293
294#ifdef SDVO_DEBUG
295static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
296 void *args, int args_len)
297{
298 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
299 int i;
300
yakui_zhao342dc382009-06-02 14:12:00 +0800301 DRM_DEBUG_KMS(I915_SDVO, "%s: W: %02X ",
302 SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800303 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800304 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800305 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800306 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800307 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
308 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800309 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800310 break;
311 }
312 }
313 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
yakui_zhao342dc382009-06-02 14:12:00 +0800314 DRM_LOG_KMS("(%02X)", cmd);
315 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800316}
317#else
318#define intel_sdvo_debug_write(o, c, a, l)
319#endif
320
321static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
322 void *args, int args_len)
323{
324 int i;
325
326 intel_sdvo_debug_write(intel_output, cmd, args, args_len);
327
328 for (i = 0; i < args_len; i++) {
329 intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
330 ((u8*)args)[i]);
331 }
332
333 intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
334}
335
336#ifdef SDVO_DEBUG
337static const char *cmd_status_names[] = {
338 "Power on",
339 "Success",
340 "Not supported",
341 "Invalid arg",
342 "Pending",
343 "Target not specified",
344 "Scaling not supported"
345};
346
347static void intel_sdvo_debug_response(struct intel_output *intel_output,
348 void *response, int response_len,
349 u8 status)
350{
351 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800352 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800353
yakui_zhao342dc382009-06-02 14:12:00 +0800354 DRM_DEBUG_KMS(I915_SDVO, "%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800355 for (i = 0; i < response_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800356 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800357 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800358 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800359 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800360 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800361 else
yakui_zhao342dc382009-06-02 14:12:00 +0800362 DRM_LOG_KMS("(??? %d)", status);
363 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800364}
365#else
366#define intel_sdvo_debug_response(o, r, l, s)
367#endif
368
369static u8 intel_sdvo_read_response(struct intel_output *intel_output,
370 void *response, int response_len)
371{
372 int i;
373 u8 status;
374 u8 retry = 50;
375
376 while (retry--) {
377 /* Read the command response */
378 for (i = 0; i < response_len; i++) {
379 intel_sdvo_read_byte(intel_output,
380 SDVO_I2C_RETURN_0 + i,
381 &((u8 *)response)[i]);
382 }
383
384 /* read the return status */
385 intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
386 &status);
387
388 intel_sdvo_debug_response(intel_output, response, response_len,
389 status);
390 if (status != SDVO_CMD_STATUS_PENDING)
391 return status;
392
393 mdelay(50);
394 }
395
396 return status;
397}
398
Hannes Ederb358d0a2008-12-18 21:18:47 +0100399static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800400{
401 if (mode->clock >= 100000)
402 return 1;
403 else if (mode->clock >= 50000)
404 return 2;
405 else
406 return 4;
407}
408
409/**
410 * Don't check status code from this as it switches the bus back to the
411 * SDVO chips which defeats the purpose of doing a bus switch in the first
412 * place.
413 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100414static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
415 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800416{
417 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
418}
419
420static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
421{
422 struct intel_sdvo_set_target_input_args targets = {0};
423 u8 status;
424
425 if (target_0 && target_1)
426 return SDVO_CMD_STATUS_NOTSUPP;
427
428 if (target_1)
429 targets.target_1 = 1;
430
431 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
432 sizeof(targets));
433
434 status = intel_sdvo_read_response(intel_output, NULL, 0);
435
436 return (status == SDVO_CMD_STATUS_SUCCESS);
437}
438
439/**
440 * Return whether each input is trained.
441 *
442 * This function is making an assumption about the layout of the response,
443 * which should be checked against the docs.
444 */
445static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
446{
447 struct intel_sdvo_get_trained_inputs_response response;
448 u8 status;
449
450 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
451 status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
452 if (status != SDVO_CMD_STATUS_SUCCESS)
453 return false;
454
455 *input_1 = response.input0_trained;
456 *input_2 = response.input1_trained;
457 return true;
458}
459
460static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
461 u16 *outputs)
462{
463 u8 status;
464
465 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
466 status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
467
468 return (status == SDVO_CMD_STATUS_SUCCESS);
469}
470
471static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
472 u16 outputs)
473{
474 u8 status;
475
476 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
477 sizeof(outputs));
478 status = intel_sdvo_read_response(intel_output, NULL, 0);
479 return (status == SDVO_CMD_STATUS_SUCCESS);
480}
481
482static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
483 int mode)
484{
485 u8 status, state = SDVO_ENCODER_STATE_ON;
486
487 switch (mode) {
488 case DRM_MODE_DPMS_ON:
489 state = SDVO_ENCODER_STATE_ON;
490 break;
491 case DRM_MODE_DPMS_STANDBY:
492 state = SDVO_ENCODER_STATE_STANDBY;
493 break;
494 case DRM_MODE_DPMS_SUSPEND:
495 state = SDVO_ENCODER_STATE_SUSPEND;
496 break;
497 case DRM_MODE_DPMS_OFF:
498 state = SDVO_ENCODER_STATE_OFF;
499 break;
500 }
501
502 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
503 sizeof(state));
504 status = intel_sdvo_read_response(intel_output, NULL, 0);
505
506 return (status == SDVO_CMD_STATUS_SUCCESS);
507}
508
509static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
510 int *clock_min,
511 int *clock_max)
512{
513 struct intel_sdvo_pixel_clock_range clocks;
514 u8 status;
515
516 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
517 NULL, 0);
518
519 status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
520
521 if (status != SDVO_CMD_STATUS_SUCCESS)
522 return false;
523
524 /* Convert the values from units of 10 kHz to kHz. */
525 *clock_min = clocks.min * 10;
526 *clock_max = clocks.max * 10;
527
528 return true;
529}
530
531static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
532 u16 outputs)
533{
534 u8 status;
535
536 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
537 sizeof(outputs));
538
539 status = intel_sdvo_read_response(intel_output, NULL, 0);
540 return (status == SDVO_CMD_STATUS_SUCCESS);
541}
542
543static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
544 struct intel_sdvo_dtd *dtd)
545{
546 u8 status;
547
548 intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
549 status = intel_sdvo_read_response(intel_output, &dtd->part1,
550 sizeof(dtd->part1));
551 if (status != SDVO_CMD_STATUS_SUCCESS)
552 return false;
553
554 intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
555 status = intel_sdvo_read_response(intel_output, &dtd->part2,
556 sizeof(dtd->part2));
557 if (status != SDVO_CMD_STATUS_SUCCESS)
558 return false;
559
560 return true;
561}
562
563static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
564 struct intel_sdvo_dtd *dtd)
565{
566 return intel_sdvo_get_timing(intel_output,
567 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
568}
569
570static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
571 struct intel_sdvo_dtd *dtd)
572{
573 return intel_sdvo_get_timing(intel_output,
574 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
575}
576
577static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
578 struct intel_sdvo_dtd *dtd)
579{
580 u8 status;
581
582 intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
583 status = intel_sdvo_read_response(intel_output, NULL, 0);
584 if (status != SDVO_CMD_STATUS_SUCCESS)
585 return false;
586
587 intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
588 status = intel_sdvo_read_response(intel_output, NULL, 0);
589 if (status != SDVO_CMD_STATUS_SUCCESS)
590 return false;
591
592 return true;
593}
594
595static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
596 struct intel_sdvo_dtd *dtd)
597{
598 return intel_sdvo_set_timing(intel_output,
599 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
600}
601
602static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
603 struct intel_sdvo_dtd *dtd)
604{
605 return intel_sdvo_set_timing(intel_output,
606 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
607}
608
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800609static bool
610intel_sdvo_create_preferred_input_timing(struct intel_output *output,
611 uint16_t clock,
612 uint16_t width,
613 uint16_t height)
614{
615 struct intel_sdvo_preferred_input_timing_args args;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800616 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800617 uint8_t status;
618
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800619 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800620 args.clock = clock;
621 args.width = width;
622 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800623 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800624
625 if (sdvo_priv->is_lvds &&
626 (sdvo_priv->sdvo_lvds_fixed_mode->hdisplay != width ||
627 sdvo_priv->sdvo_lvds_fixed_mode->vdisplay != height))
628 args.scaled = 1;
629
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800630 intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
631 &args, sizeof(args));
632 status = intel_sdvo_read_response(output, NULL, 0);
633 if (status != SDVO_CMD_STATUS_SUCCESS)
634 return false;
635
636 return true;
637}
638
639static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
640 struct intel_sdvo_dtd *dtd)
641{
642 bool status;
643
644 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
645 NULL, 0);
646
647 status = intel_sdvo_read_response(output, &dtd->part1,
648 sizeof(dtd->part1));
649 if (status != SDVO_CMD_STATUS_SUCCESS)
650 return false;
651
652 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
653 NULL, 0);
654
655 status = intel_sdvo_read_response(output, &dtd->part2,
656 sizeof(dtd->part2));
657 if (status != SDVO_CMD_STATUS_SUCCESS)
658 return false;
659
660 return false;
661}
Jesse Barnes79e53942008-11-07 14:24:08 -0800662
663static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
664{
665 u8 response, status;
666
667 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
668 status = intel_sdvo_read_response(intel_output, &response, 1);
669
670 if (status != SDVO_CMD_STATUS_SUCCESS) {
671 DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
672 return SDVO_CLOCK_RATE_MULT_1X;
673 } else {
674 DRM_DEBUG("Current clock rate multiplier: %d\n", response);
675 }
676
677 return response;
678}
679
680static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
681{
682 u8 status;
683
684 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
685 status = intel_sdvo_read_response(intel_output, NULL, 0);
686 if (status != SDVO_CMD_STATUS_SUCCESS)
687 return false;
688
689 return true;
690}
691
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800692static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
693 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800694{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800695 uint16_t width, height;
696 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
697 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800698
699 width = mode->crtc_hdisplay;
700 height = mode->crtc_vdisplay;
701
702 /* do some mode translations */
703 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
704 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
705
706 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
707 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
708
709 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
710 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
711
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800712 dtd->part1.clock = mode->clock / 10;
713 dtd->part1.h_active = width & 0xff;
714 dtd->part1.h_blank = h_blank_len & 0xff;
715 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800716 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800717 dtd->part1.v_active = height & 0xff;
718 dtd->part1.v_blank = v_blank_len & 0xff;
719 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800720 ((v_blank_len >> 8) & 0xf);
721
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800722 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800723 dtd->part2.h_sync_width = h_sync_len & 0xff;
724 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800725 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800726 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800727 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
728 ((v_sync_len & 0x30) >> 4);
729
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800730 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800731 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800732 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800733 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800734 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800735
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800736 dtd->part2.sdvo_flags = 0;
737 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
738 dtd->part2.reserved = 0;
739}
Jesse Barnes79e53942008-11-07 14:24:08 -0800740
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800741static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
742 struct intel_sdvo_dtd *dtd)
743{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800744 mode->hdisplay = dtd->part1.h_active;
745 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
746 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800747 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800748 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
749 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
750 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
751 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
752
753 mode->vdisplay = dtd->part1.v_active;
754 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
755 mode->vsync_start = mode->vdisplay;
756 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800757 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800758 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
759 mode->vsync_end = mode->vsync_start +
760 (dtd->part2.v_sync_off_width & 0xf);
761 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
762 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
763 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
764
765 mode->clock = dtd->part1.clock * 10;
766
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800767 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800768 if (dtd->part2.dtd_flags & 0x2)
769 mode->flags |= DRM_MODE_FLAG_PHSYNC;
770 if (dtd->part2.dtd_flags & 0x4)
771 mode->flags |= DRM_MODE_FLAG_PVSYNC;
772}
773
774static bool intel_sdvo_get_supp_encode(struct intel_output *output,
775 struct intel_sdvo_encode *encode)
776{
777 uint8_t status;
778
779 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
780 status = intel_sdvo_read_response(output, encode, sizeof(*encode));
781 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
782 memset(encode, 0, sizeof(*encode));
783 return false;
784 }
785
786 return true;
787}
788
789static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
790{
791 uint8_t status;
792
793 intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
794 status = intel_sdvo_read_response(output, NULL, 0);
795
796 return (status == SDVO_CMD_STATUS_SUCCESS);
797}
798
799static bool intel_sdvo_set_colorimetry(struct intel_output *output,
800 uint8_t mode)
801{
802 uint8_t status;
803
804 intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
805 status = intel_sdvo_read_response(output, NULL, 0);
806
807 return (status == SDVO_CMD_STATUS_SUCCESS);
808}
809
810#if 0
811static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
812{
813 int i, j;
814 uint8_t set_buf_index[2];
815 uint8_t av_split;
816 uint8_t buf_size;
817 uint8_t buf[48];
818 uint8_t *pos;
819
820 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
821 intel_sdvo_read_response(output, &av_split, 1);
822
823 for (i = 0; i <= av_split; i++) {
824 set_buf_index[0] = i; set_buf_index[1] = 0;
825 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
826 set_buf_index, 2);
827 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
828 intel_sdvo_read_response(output, &buf_size, 1);
829
830 pos = buf;
831 for (j = 0; j <= buf_size; j += 8) {
832 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
833 NULL, 0);
834 intel_sdvo_read_response(output, pos, 8);
835 pos += 8;
836 }
837 }
838}
839#endif
840
841static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
842 uint8_t *data, int8_t size, uint8_t tx_rate)
843{
844 uint8_t set_buf_index[2];
845
846 set_buf_index[0] = index;
847 set_buf_index[1] = 0;
848
849 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
850
851 for (; size > 0; size -= 8) {
852 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
853 data += 8;
854 }
855
856 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
857}
858
859static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
860{
861 uint8_t csum = 0;
862 int i;
863
864 for (i = 0; i < size; i++)
865 csum += data[i];
866
867 return 0x100 - csum;
868}
869
870#define DIP_TYPE_AVI 0x82
871#define DIP_VERSION_AVI 0x2
872#define DIP_LEN_AVI 13
873
874struct dip_infoframe {
875 uint8_t type;
876 uint8_t version;
877 uint8_t len;
878 uint8_t checksum;
879 union {
880 struct {
881 /* Packet Byte #1 */
882 uint8_t S:2;
883 uint8_t B:2;
884 uint8_t A:1;
885 uint8_t Y:2;
886 uint8_t rsvd1:1;
887 /* Packet Byte #2 */
888 uint8_t R:4;
889 uint8_t M:2;
890 uint8_t C:2;
891 /* Packet Byte #3 */
892 uint8_t SC:2;
893 uint8_t Q:2;
894 uint8_t EC:3;
895 uint8_t ITC:1;
896 /* Packet Byte #4 */
897 uint8_t VIC:7;
898 uint8_t rsvd2:1;
899 /* Packet Byte #5 */
900 uint8_t PR:4;
901 uint8_t rsvd3:4;
902 /* Packet Byte #6~13 */
903 uint16_t top_bar_end;
904 uint16_t bottom_bar_start;
905 uint16_t left_bar_end;
906 uint16_t right_bar_start;
907 } avi;
908 struct {
909 /* Packet Byte #1 */
910 uint8_t channel_count:3;
911 uint8_t rsvd1:1;
912 uint8_t coding_type:4;
913 /* Packet Byte #2 */
914 uint8_t sample_size:2; /* SS0, SS1 */
915 uint8_t sample_frequency:3;
916 uint8_t rsvd2:3;
917 /* Packet Byte #3 */
918 uint8_t coding_type_private:5;
919 uint8_t rsvd3:3;
920 /* Packet Byte #4 */
921 uint8_t channel_allocation;
922 /* Packet Byte #5 */
923 uint8_t rsvd4:3;
924 uint8_t level_shift:4;
925 uint8_t downmix_inhibit:1;
926 } audio;
927 uint8_t payload[28];
928 } __attribute__ ((packed)) u;
929} __attribute__((packed));
930
931static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
932 struct drm_display_mode * mode)
933{
934 struct dip_infoframe avi_if = {
935 .type = DIP_TYPE_AVI,
936 .version = DIP_VERSION_AVI,
937 .len = DIP_LEN_AVI,
938 };
939
940 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
941 4 + avi_if.len);
942 intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
943 SDVO_HBUF_TX_VSYNC);
944}
945
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800946static void intel_sdvo_set_tv_format(struct intel_output *output)
947{
948 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
949 struct intel_sdvo_tv_format *format, unset;
950 u8 status;
951
952 format = &sdvo_priv->tv_format;
953 memset(&unset, 0, sizeof(unset));
954 if (memcmp(format, &unset, sizeof(*format))) {
955 DRM_DEBUG("%s: Choosing default TV format of NTSC-M\n",
956 SDVO_NAME(sdvo_priv));
957 format->ntsc_m = 1;
958 intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, format,
959 sizeof(*format));
960 status = intel_sdvo_read_response(output, NULL, 0);
961 if (status != SDVO_CMD_STATUS_SUCCESS)
962 DRM_DEBUG("%s: Failed to set TV format\n",
963 SDVO_NAME(sdvo_priv));
964 }
965}
966
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800967static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
968 struct drm_display_mode *mode,
969 struct drm_display_mode *adjusted_mode)
970{
971 struct intel_output *output = enc_to_intel_output(encoder);
972 struct intel_sdvo_priv *dev_priv = output->dev_priv;
973
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800974 if (dev_priv->is_tv) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800975 struct intel_sdvo_dtd output_dtd;
976 bool success;
977
978 /* We need to construct preferred input timings based on our
979 * output timings. To do that, we have to set the output
980 * timings, even though this isn't really the right place in
981 * the sequence to do it. Oh well.
982 */
983
984
985 /* Set output timings */
986 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
987 intel_sdvo_set_target_output(output,
988 dev_priv->controlled_output);
989 intel_sdvo_set_output_timing(output, &output_dtd);
990
991 /* Set the input timing to the screen. Assume always input 0. */
992 intel_sdvo_set_target_input(output, true, false);
993
994
995 success = intel_sdvo_create_preferred_input_timing(output,
996 mode->clock / 10,
997 mode->hdisplay,
998 mode->vdisplay);
999 if (success) {
1000 struct intel_sdvo_dtd input_dtd;
1001
1002 intel_sdvo_get_preferred_input_timing(output,
1003 &input_dtd);
1004 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001005 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001006
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001007 drm_mode_set_crtcinfo(adjusted_mode, 0);
1008
1009 mode->clock = adjusted_mode->clock;
1010
1011 adjusted_mode->clock *=
1012 intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001013 } else {
1014 return false;
1015 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001016 } else if (dev_priv->is_lvds) {
1017 struct intel_sdvo_dtd output_dtd;
1018 bool success;
1019
1020 drm_mode_set_crtcinfo(dev_priv->sdvo_lvds_fixed_mode, 0);
1021 /* Set output timings */
1022 intel_sdvo_get_dtd_from_mode(&output_dtd,
1023 dev_priv->sdvo_lvds_fixed_mode);
1024
1025 intel_sdvo_set_target_output(output,
1026 dev_priv->controlled_output);
1027 intel_sdvo_set_output_timing(output, &output_dtd);
1028
1029 /* Set the input timing to the screen. Assume always input 0. */
1030 intel_sdvo_set_target_input(output, true, false);
1031
1032
1033 success = intel_sdvo_create_preferred_input_timing(
1034 output,
1035 mode->clock / 10,
1036 mode->hdisplay,
1037 mode->vdisplay);
1038
1039 if (success) {
1040 struct intel_sdvo_dtd input_dtd;
1041
1042 intel_sdvo_get_preferred_input_timing(output,
1043 &input_dtd);
1044 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1045 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
1046
1047 drm_mode_set_crtcinfo(adjusted_mode, 0);
1048
1049 mode->clock = adjusted_mode->clock;
1050
1051 adjusted_mode->clock *=
1052 intel_sdvo_get_pixel_multiplier(mode);
1053 } else {
1054 return false;
1055 }
1056
1057 } else {
1058 /* Make the CRTC code factor in the SDVO pixel multiplier. The
1059 * SDVO device will be told of the multiplier during mode_set.
1060 */
1061 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001062 }
1063 return true;
1064}
1065
1066static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1067 struct drm_display_mode *mode,
1068 struct drm_display_mode *adjusted_mode)
1069{
1070 struct drm_device *dev = encoder->dev;
1071 struct drm_i915_private *dev_priv = dev->dev_private;
1072 struct drm_crtc *crtc = encoder->crtc;
1073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1074 struct intel_output *output = enc_to_intel_output(encoder);
1075 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1076 u32 sdvox = 0;
1077 int sdvo_pixel_multiply;
1078 struct intel_sdvo_in_out_map in_out;
1079 struct intel_sdvo_dtd input_dtd;
1080 u8 status;
1081
1082 if (!mode)
1083 return;
1084
1085 /* First, set the input mapping for the first input to our controlled
1086 * output. This is only correct if we're a single-input device, in
1087 * which case the first input is the output from the appropriate SDVO
1088 * channel on the motherboard. In a two-input device, the first input
1089 * will be SDVOB and the second SDVOC.
1090 */
1091 in_out.in0 = sdvo_priv->controlled_output;
1092 in_out.in1 = 0;
1093
1094 intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
1095 &in_out, sizeof(in_out));
1096 status = intel_sdvo_read_response(output, NULL, 0);
1097
1098 if (sdvo_priv->is_hdmi) {
1099 intel_sdvo_set_avi_infoframe(output, mode);
1100 sdvox |= SDVO_AUDIO_ENABLE;
1101 }
1102
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001103 /* We have tried to get input timing in mode_fixup, and filled into
1104 adjusted_mode */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001105 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001106 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001107 input_dtd.part2.sdvo_flags = sdvo_priv->sdvo_flags;
1108 } else
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001109 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001110
1111 /* If it's a TV, we already set the output timing in mode_fixup.
1112 * Otherwise, the output timing is equal to the input timing.
1113 */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001114 if (!sdvo_priv->is_tv && !sdvo_priv->is_lvds) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001115 /* Set the output timing to the screen */
1116 intel_sdvo_set_target_output(output,
1117 sdvo_priv->controlled_output);
1118 intel_sdvo_set_output_timing(output, &input_dtd);
1119 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001120
1121 /* Set the input timing to the screen. Assume always input 0. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001122 intel_sdvo_set_target_input(output, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001123
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001124 if (sdvo_priv->is_tv)
1125 intel_sdvo_set_tv_format(output);
1126
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001127 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001128 * provide the device with a timing it can support, if it supports that
1129 * feature. However, presumably we would need to adjust the CRTC to
1130 * output the preferred timing, and we don't support that currently.
1131 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001132#if 0
1133 success = intel_sdvo_create_preferred_input_timing(output, clock,
1134 width, height);
1135 if (success) {
1136 struct intel_sdvo_dtd *input_dtd;
1137
1138 intel_sdvo_get_preferred_input_timing(output, &input_dtd);
1139 intel_sdvo_set_input_timing(output, &input_dtd);
1140 }
1141#else
1142 intel_sdvo_set_input_timing(output, &input_dtd);
1143#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001144
1145 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1146 case 1:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001147 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001148 SDVO_CLOCK_RATE_MULT_1X);
1149 break;
1150 case 2:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001151 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001152 SDVO_CLOCK_RATE_MULT_2X);
1153 break;
1154 case 4:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001155 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001156 SDVO_CLOCK_RATE_MULT_4X);
1157 break;
1158 }
1159
1160 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001161 if (IS_I965G(dev)) {
1162 sdvox |= SDVO_BORDER_ENABLE |
1163 SDVO_VSYNC_ACTIVE_HIGH |
1164 SDVO_HSYNC_ACTIVE_HIGH;
1165 } else {
1166 sdvox |= I915_READ(sdvo_priv->output_device);
1167 switch (sdvo_priv->output_device) {
1168 case SDVOB:
1169 sdvox &= SDVOB_PRESERVE_MASK;
1170 break;
1171 case SDVOC:
1172 sdvox &= SDVOC_PRESERVE_MASK;
1173 break;
1174 }
1175 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1176 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001177 if (intel_crtc->pipe == 1)
1178 sdvox |= SDVO_PIPE_B_SELECT;
1179
1180 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1181 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001182 /* done in crtc_mode_set as the dpll_md reg must be written early */
1183 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1184 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001185 } else {
1186 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1187 }
1188
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001189 if (sdvo_priv->sdvo_flags & SDVO_NEED_TO_STALL)
1190 sdvox |= SDVO_STALL_SELECT;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001191 intel_sdvo_write_sdvox(output, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001192}
1193
1194static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1195{
1196 struct drm_device *dev = encoder->dev;
1197 struct drm_i915_private *dev_priv = dev->dev_private;
1198 struct intel_output *intel_output = enc_to_intel_output(encoder);
1199 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1200 u32 temp;
1201
1202 if (mode != DRM_MODE_DPMS_ON) {
1203 intel_sdvo_set_active_outputs(intel_output, 0);
1204 if (0)
1205 intel_sdvo_set_encoder_power_state(intel_output, mode);
1206
1207 if (mode == DRM_MODE_DPMS_OFF) {
1208 temp = I915_READ(sdvo_priv->output_device);
1209 if ((temp & SDVO_ENABLE) != 0) {
1210 intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
1211 }
1212 }
1213 } else {
1214 bool input1, input2;
1215 int i;
1216 u8 status;
1217
1218 temp = I915_READ(sdvo_priv->output_device);
1219 if ((temp & SDVO_ENABLE) == 0)
1220 intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
1221 for (i = 0; i < 2; i++)
1222 intel_wait_for_vblank(dev);
1223
1224 status = intel_sdvo_get_trained_inputs(intel_output, &input1,
1225 &input2);
1226
1227
1228 /* Warn if the device reported failure to sync.
1229 * A lot of SDVO devices fail to notify of sync, but it's
1230 * a given it the status is a success, we succeeded.
1231 */
1232 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1233 DRM_DEBUG("First %s output reported failure to sync\n",
1234 SDVO_NAME(sdvo_priv));
1235 }
1236
1237 if (0)
1238 intel_sdvo_set_encoder_power_state(intel_output, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001239 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001240 }
1241 return;
1242}
1243
1244static void intel_sdvo_save(struct drm_connector *connector)
1245{
1246 struct drm_device *dev = connector->dev;
1247 struct drm_i915_private *dev_priv = dev->dev_private;
1248 struct intel_output *intel_output = to_intel_output(connector);
1249 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1250 int o;
1251
1252 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
1253 intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
1254
1255 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1256 intel_sdvo_set_target_input(intel_output, true, false);
1257 intel_sdvo_get_input_timing(intel_output,
1258 &sdvo_priv->save_input_dtd_1);
1259 }
1260
1261 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1262 intel_sdvo_set_target_input(intel_output, false, true);
1263 intel_sdvo_get_input_timing(intel_output,
1264 &sdvo_priv->save_input_dtd_2);
1265 }
1266
1267 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1268 {
1269 u16 this_output = (1 << o);
1270 if (sdvo_priv->caps.output_flags & this_output)
1271 {
1272 intel_sdvo_set_target_output(intel_output, this_output);
1273 intel_sdvo_get_output_timing(intel_output,
1274 &sdvo_priv->save_output_dtd[o]);
1275 }
1276 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001277 if (sdvo_priv->is_tv) {
1278 /* XXX: Save TV format/enhancements. */
1279 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001280
1281 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
1282}
1283
1284static void intel_sdvo_restore(struct drm_connector *connector)
1285{
1286 struct drm_device *dev = connector->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001287 struct intel_output *intel_output = to_intel_output(connector);
1288 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1289 int o;
1290 int i;
1291 bool input1, input2;
1292 u8 status;
1293
1294 intel_sdvo_set_active_outputs(intel_output, 0);
1295
1296 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1297 {
1298 u16 this_output = (1 << o);
1299 if (sdvo_priv->caps.output_flags & this_output) {
1300 intel_sdvo_set_target_output(intel_output, this_output);
1301 intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
1302 }
1303 }
1304
1305 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1306 intel_sdvo_set_target_input(intel_output, true, false);
1307 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
1308 }
1309
1310 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1311 intel_sdvo_set_target_input(intel_output, false, true);
1312 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
1313 }
1314
1315 intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
1316
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001317 if (sdvo_priv->is_tv) {
1318 /* XXX: Restore TV format/enhancements. */
1319 }
1320
1321 intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
Jesse Barnes79e53942008-11-07 14:24:08 -08001322
1323 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1324 {
1325 for (i = 0; i < 2; i++)
1326 intel_wait_for_vblank(dev);
1327 status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
1328 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
1329 DRM_DEBUG("First %s output reported failure to sync\n",
1330 SDVO_NAME(sdvo_priv));
1331 }
1332
1333 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
1334}
1335
1336static int intel_sdvo_mode_valid(struct drm_connector *connector,
1337 struct drm_display_mode *mode)
1338{
1339 struct intel_output *intel_output = to_intel_output(connector);
1340 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1341
1342 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1343 return MODE_NO_DBLESCAN;
1344
1345 if (sdvo_priv->pixel_clock_min > mode->clock)
1346 return MODE_CLOCK_LOW;
1347
1348 if (sdvo_priv->pixel_clock_max < mode->clock)
1349 return MODE_CLOCK_HIGH;
1350
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001351 if (sdvo_priv->is_lvds == true) {
1352 if (sdvo_priv->sdvo_lvds_fixed_mode == NULL)
1353 return MODE_PANEL;
1354
1355 if (mode->hdisplay > sdvo_priv->sdvo_lvds_fixed_mode->hdisplay)
1356 return MODE_PANEL;
1357
1358 if (mode->vdisplay > sdvo_priv->sdvo_lvds_fixed_mode->vdisplay)
1359 return MODE_PANEL;
1360 }
1361
Jesse Barnes79e53942008-11-07 14:24:08 -08001362 return MODE_OK;
1363}
1364
1365static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
1366{
1367 u8 status;
1368
1369 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1370 status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
1371 if (status != SDVO_CMD_STATUS_SUCCESS)
1372 return false;
1373
1374 return true;
1375}
1376
1377struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1378{
1379 struct drm_connector *connector = NULL;
1380 struct intel_output *iout = NULL;
1381 struct intel_sdvo_priv *sdvo;
1382
1383 /* find the sdvo connector */
1384 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1385 iout = to_intel_output(connector);
1386
1387 if (iout->type != INTEL_OUTPUT_SDVO)
1388 continue;
1389
1390 sdvo = iout->dev_priv;
1391
1392 if (sdvo->output_device == SDVOB && sdvoB)
1393 return connector;
1394
1395 if (sdvo->output_device == SDVOC && !sdvoB)
1396 return connector;
1397
1398 }
1399
1400 return NULL;
1401}
1402
1403int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1404{
1405 u8 response[2];
1406 u8 status;
1407 struct intel_output *intel_output;
1408 DRM_DEBUG("\n");
1409
1410 if (!connector)
1411 return 0;
1412
1413 intel_output = to_intel_output(connector);
1414
1415 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1416 status = intel_sdvo_read_response(intel_output, &response, 2);
1417
1418 if (response[0] !=0)
1419 return 1;
1420
1421 return 0;
1422}
1423
1424void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1425{
1426 u8 response[2];
1427 u8 status;
1428 struct intel_output *intel_output = to_intel_output(connector);
1429
1430 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1431 intel_sdvo_read_response(intel_output, &response, 2);
1432
1433 if (on) {
1434 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1435 status = intel_sdvo_read_response(intel_output, &response, 2);
1436
1437 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1438 } else {
1439 response[0] = 0;
1440 response[1] = 0;
1441 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1442 }
1443
1444 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1445 intel_sdvo_read_response(intel_output, &response, 2);
1446}
1447
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001448static bool
1449intel_sdvo_multifunc_encoder(struct intel_output *intel_output)
1450{
1451 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1452 int caps = 0;
1453
1454 if (sdvo_priv->caps.output_flags &
1455 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1456 caps++;
1457 if (sdvo_priv->caps.output_flags &
1458 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1459 caps++;
1460 if (sdvo_priv->caps.output_flags &
Roel Kluin19e1f882009-08-09 13:50:53 +02001461 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID1))
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001462 caps++;
1463 if (sdvo_priv->caps.output_flags &
1464 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1465 caps++;
1466 if (sdvo_priv->caps.output_flags &
1467 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1468 caps++;
1469
1470 if (sdvo_priv->caps.output_flags &
1471 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1472 caps++;
1473
1474 if (sdvo_priv->caps.output_flags &
1475 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1476 caps++;
1477
1478 return (caps > 1);
1479}
1480
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001481enum drm_connector_status
1482intel_sdvo_hdmi_sink_detect(struct drm_connector *connector, u16 response)
Ma Ling9dff6af2009-04-02 13:13:26 +08001483{
1484 struct intel_output *intel_output = to_intel_output(connector);
1485 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001486 enum drm_connector_status status = connector_status_connected;
Ma Ling9dff6af2009-04-02 13:13:26 +08001487 struct edid *edid = NULL;
1488
Ma Ling9dff6af2009-04-02 13:13:26 +08001489 edid = drm_get_edid(&intel_output->base,
Keith Packardf9c10a92009-05-30 12:16:25 -07001490 intel_output->ddc_bus);
Ma Ling9dff6af2009-04-02 13:13:26 +08001491 if (edid != NULL) {
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001492 /* Don't report the output as connected if it's a DVI-I
1493 * connector with a non-digital EDID coming out.
1494 */
1495 if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1496 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1497 sdvo_priv->is_hdmi =
1498 drm_detect_hdmi_monitor(edid);
1499 else
1500 status = connector_status_disconnected;
1501 }
1502
Ma Ling9dff6af2009-04-02 13:13:26 +08001503 kfree(edid);
1504 intel_output->base.display_info.raw_edid = NULL;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001505
1506 } else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1507 status = connector_status_disconnected;
1508
1509 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001510}
1511
Jesse Barnes79e53942008-11-07 14:24:08 -08001512static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1513{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001514 uint16_t response;
Jesse Barnes79e53942008-11-07 14:24:08 -08001515 u8 status;
1516 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001517 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001518
1519 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
1520 status = intel_sdvo_read_response(intel_output, &response, 2);
1521
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001522 DRM_DEBUG("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001523
1524 if (status != SDVO_CMD_STATUS_SUCCESS)
1525 return connector_status_unknown;
1526
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001527 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001528 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001529
1530 if (intel_sdvo_multifunc_encoder(intel_output) &&
1531 sdvo_priv->attached_output != response) {
1532 if (sdvo_priv->controlled_output != response &&
1533 intel_sdvo_output_setup(intel_output, response) != true)
1534 return connector_status_unknown;
1535 sdvo_priv->attached_output = response;
1536 }
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001537 return intel_sdvo_hdmi_sink_detect(connector, response);
Jesse Barnes79e53942008-11-07 14:24:08 -08001538}
1539
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001540static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001541{
1542 struct intel_output *intel_output = to_intel_output(connector);
1543
1544 /* set the bus switch and get the modes */
Jesse Barnes79e53942008-11-07 14:24:08 -08001545 intel_ddc_get_modes(intel_output);
1546
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001547#if 0
1548 struct drm_device *dev = encoder->dev;
1549 struct drm_i915_private *dev_priv = dev->dev_private;
1550 /* Mac mini hack. On this device, I get DDC through the analog, which
1551 * load-detects as disconnected. I fail to DDC through the SDVO DDC,
1552 * but it does load-detect as connected. So, just steal the DDC bits
1553 * from analog when we fail at finding it the right way.
1554 */
1555 crt = xf86_config->output[0];
1556 intel_output = crt->driver_private;
1557 if (intel_output->type == I830_OUTPUT_ANALOG &&
1558 crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
1559 I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
1560 edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
1561 xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
1562 }
1563 if (edid_mon) {
1564 xf86OutputSetEDID(output, edid_mon);
1565 modes = xf86OutputGetEDIDModes(output);
1566 }
1567#endif
1568}
1569
1570/**
1571 * This function checks the current TV format, and chooses a default if
1572 * it hasn't been set.
1573 */
1574static void
1575intel_sdvo_check_tv_format(struct intel_output *output)
1576{
1577 struct intel_sdvo_priv *dev_priv = output->dev_priv;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001578 struct intel_sdvo_tv_format format;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001579 uint8_t status;
1580
1581 intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
1582 status = intel_sdvo_read_response(output, &format, sizeof(format));
1583 if (status != SDVO_CMD_STATUS_SUCCESS)
1584 return;
1585
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001586 memcpy(&dev_priv->tv_format, &format, sizeof(format));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001587}
1588
1589/*
1590 * Set of SDVO TV modes.
1591 * Note! This is in reply order (see loop in get_tv_modes).
1592 * XXX: all 60Hz refresh?
1593 */
1594struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001595 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1596 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001597 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001598 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1599 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001600 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001601 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1602 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001603 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001604 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1605 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001606 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001607 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1608 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001609 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001610 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1611 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001612 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001613 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1614 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001615 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001616 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1617 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001618 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001619 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1620 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001621 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001622 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1623 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001624 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001625 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1626 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001627 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001628 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1629 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001630 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001631 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1632 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001633 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001634 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1635 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001636 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001637 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1638 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001639 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001640 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1641 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001642 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001643 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1644 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001645 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001646 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1647 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001648 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001649 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1650 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001651 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1652};
1653
1654static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1655{
1656 struct intel_output *output = to_intel_output(connector);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001657 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1658 struct intel_sdvo_sdtv_resolution_request tv_res;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001659 uint32_t reply = 0;
1660 uint8_t status;
1661 int i = 0;
1662
1663 intel_sdvo_check_tv_format(output);
1664
1665 /* Read the list of supported input resolutions for the selected TV
1666 * format.
1667 */
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001668 memset(&tv_res, 0, sizeof(tv_res));
1669 memcpy(&tv_res, &sdvo_priv->tv_format, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001670 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001671 &tv_res, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001672 status = intel_sdvo_read_response(output, &reply, 3);
1673 if (status != SDVO_CMD_STATUS_SUCCESS)
1674 return;
1675
1676 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001677 if (reply & (1 << i)) {
1678 struct drm_display_mode *nmode;
1679 nmode = drm_mode_duplicate(connector->dev,
1680 &sdvo_tv_modes[i]);
1681 if (nmode)
1682 drm_mode_probed_add(connector, nmode);
1683 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001684}
1685
Ma Ling7086c872009-05-13 11:20:06 +08001686static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1687{
1688 struct intel_output *intel_output = to_intel_output(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001689 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001690 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1691 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001692
1693 /*
1694 * Attempt to get the mode list from DDC.
1695 * Assume that the preferred modes are
1696 * arranged in priority order.
1697 */
Ma Ling7086c872009-05-13 11:20:06 +08001698 intel_ddc_get_modes(intel_output);
1699 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001700 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001701
1702 /* Fetch modes from VBT */
1703 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001704 newmode = drm_mode_duplicate(connector->dev,
1705 dev_priv->sdvo_lvds_vbt_mode);
1706 if (newmode != NULL) {
1707 /* Guarantee the mode is preferred */
1708 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1709 DRM_MODE_TYPE_DRIVER);
1710 drm_mode_probed_add(connector, newmode);
1711 }
1712 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001713
1714end:
1715 list_for_each_entry(newmode, &connector->probed_modes, head) {
1716 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1717 sdvo_priv->sdvo_lvds_fixed_mode =
1718 drm_mode_duplicate(connector->dev, newmode);
1719 break;
1720 }
1721 }
1722
Ma Ling7086c872009-05-13 11:20:06 +08001723}
1724
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001725static int intel_sdvo_get_modes(struct drm_connector *connector)
1726{
1727 struct intel_output *output = to_intel_output(connector);
1728 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1729
1730 if (sdvo_priv->is_tv)
1731 intel_sdvo_get_tv_modes(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001732 else if (sdvo_priv->is_lvds == true)
1733 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001734 else
1735 intel_sdvo_get_ddc_modes(connector);
1736
Jesse Barnes79e53942008-11-07 14:24:08 -08001737 if (list_empty(&connector->probed_modes))
1738 return 0;
1739 return 1;
1740}
1741
1742static void intel_sdvo_destroy(struct drm_connector *connector)
1743{
1744 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001745 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001746
1747 if (intel_output->i2c_bus)
1748 intel_i2c_destroy(intel_output->i2c_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08001749 if (intel_output->ddc_bus)
1750 intel_i2c_destroy(intel_output->ddc_bus);
1751
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001752 if (sdvo_priv->sdvo_lvds_fixed_mode != NULL)
1753 drm_mode_destroy(connector->dev,
1754 sdvo_priv->sdvo_lvds_fixed_mode);
1755
Jesse Barnes79e53942008-11-07 14:24:08 -08001756 drm_sysfs_connector_remove(connector);
1757 drm_connector_cleanup(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001758
Jesse Barnes79e53942008-11-07 14:24:08 -08001759 kfree(intel_output);
1760}
1761
1762static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1763 .dpms = intel_sdvo_dpms,
1764 .mode_fixup = intel_sdvo_mode_fixup,
1765 .prepare = intel_encoder_prepare,
1766 .mode_set = intel_sdvo_mode_set,
1767 .commit = intel_encoder_commit,
1768};
1769
1770static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07001771 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08001772 .save = intel_sdvo_save,
1773 .restore = intel_sdvo_restore,
1774 .detect = intel_sdvo_detect,
1775 .fill_modes = drm_helper_probe_single_connector_modes,
1776 .destroy = intel_sdvo_destroy,
1777};
1778
1779static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1780 .get_modes = intel_sdvo_get_modes,
1781 .mode_valid = intel_sdvo_mode_valid,
1782 .best_encoder = intel_best_encoder,
1783};
1784
Hannes Ederb358d0a2008-12-18 21:18:47 +01001785static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001786{
1787 drm_encoder_cleanup(encoder);
1788}
1789
1790static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1791 .destroy = intel_sdvo_enc_destroy,
1792};
1793
1794
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001795/**
1796 * Choose the appropriate DDC bus for control bus switch command for this
1797 * SDVO output based on the controlled output.
1798 *
1799 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1800 * outputs, then LVDS outputs.
1801 */
1802static void
1803intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
1804{
1805 uint16_t mask = 0;
1806 unsigned int num_bits;
1807
1808 /* Make a mask of outputs less than or equal to our own priority in the
1809 * list.
1810 */
1811 switch (dev_priv->controlled_output) {
1812 case SDVO_OUTPUT_LVDS1:
1813 mask |= SDVO_OUTPUT_LVDS1;
1814 case SDVO_OUTPUT_LVDS0:
1815 mask |= SDVO_OUTPUT_LVDS0;
1816 case SDVO_OUTPUT_TMDS1:
1817 mask |= SDVO_OUTPUT_TMDS1;
1818 case SDVO_OUTPUT_TMDS0:
1819 mask |= SDVO_OUTPUT_TMDS0;
1820 case SDVO_OUTPUT_RGB1:
1821 mask |= SDVO_OUTPUT_RGB1;
1822 case SDVO_OUTPUT_RGB0:
1823 mask |= SDVO_OUTPUT_RGB0;
1824 break;
1825 }
1826
1827 /* Count bits to find what number we are in the priority list. */
1828 mask &= dev_priv->caps.output_flags;
1829 num_bits = hweight16(mask);
1830 if (num_bits > 3) {
1831 /* if more than 3 outputs, default to DDC bus 3 for now */
1832 num_bits = 3;
1833 }
1834
1835 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1836 dev_priv->ddc_bus = 1 << num_bits;
1837}
1838
1839static bool
1840intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
1841{
1842 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1843 uint8_t status;
1844
1845 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1846
1847 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
1848 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
1849 if (status != SDVO_CMD_STATUS_SUCCESS)
1850 return false;
1851 return true;
1852}
1853
Ma Ling619ac3b2009-05-18 16:12:46 +08001854static struct intel_output *
1855intel_sdvo_chan_to_intel_output(struct intel_i2c_chan *chan)
1856{
1857 struct drm_device *dev = chan->drm_dev;
1858 struct drm_connector *connector;
1859 struct intel_output *intel_output = NULL;
1860
1861 list_for_each_entry(connector,
1862 &dev->mode_config.connector_list, head) {
Keith Packardf9c10a92009-05-30 12:16:25 -07001863 if (to_intel_output(connector)->ddc_bus == &chan->adapter) {
Ma Ling619ac3b2009-05-18 16:12:46 +08001864 intel_output = to_intel_output(connector);
1865 break;
1866 }
1867 }
1868 return intel_output;
1869}
1870
1871static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
1872 struct i2c_msg msgs[], int num)
1873{
1874 struct intel_output *intel_output;
1875 struct intel_sdvo_priv *sdvo_priv;
1876 struct i2c_algo_bit_data *algo_data;
Keith Packardf9c10a92009-05-30 12:16:25 -07001877 const struct i2c_algorithm *algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001878
1879 algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
1880 intel_output =
1881 intel_sdvo_chan_to_intel_output(
1882 (struct intel_i2c_chan *)(algo_data->data));
1883 if (intel_output == NULL)
1884 return -EINVAL;
1885
1886 sdvo_priv = intel_output->dev_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07001887 algo = intel_output->i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001888
1889 intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
1890 return algo->master_xfer(i2c_adap, msgs, num);
1891}
1892
1893static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
1894 .master_xfer = intel_sdvo_master_xfer,
1895};
1896
yakui_zhao714605e2009-05-31 17:18:07 +08001897static u8
1898intel_sdvo_get_slave_addr(struct drm_device *dev, int output_device)
1899{
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct sdvo_device_mapping *my_mapping, *other_mapping;
1902
1903 if (output_device == SDVOB) {
1904 my_mapping = &dev_priv->sdvo_mappings[0];
1905 other_mapping = &dev_priv->sdvo_mappings[1];
1906 } else {
1907 my_mapping = &dev_priv->sdvo_mappings[1];
1908 other_mapping = &dev_priv->sdvo_mappings[0];
1909 }
1910
1911 /* If the BIOS described our SDVO device, take advantage of it. */
1912 if (my_mapping->slave_addr)
1913 return my_mapping->slave_addr;
1914
1915 /* If the BIOS only described a different SDVO device, use the
1916 * address that it isn't using.
1917 */
1918 if (other_mapping->slave_addr) {
1919 if (other_mapping->slave_addr == 0x70)
1920 return 0x72;
1921 else
1922 return 0x70;
1923 }
1924
1925 /* No SDVO device info is found for another DVO port,
1926 * so use mapping assumption we had before BIOS parsing.
1927 */
1928 if (output_device == SDVOB)
1929 return 0x70;
1930 else
1931 return 0x72;
1932}
1933
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001934static bool
1935intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags)
1936{
1937 struct drm_connector *connector = &intel_output->base;
1938 struct drm_encoder *encoder = &intel_output->enc;
1939 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1940 bool ret = true, registered = false;
1941
1942 sdvo_priv->is_tv = false;
1943 intel_output->needs_tv_clock = false;
1944 sdvo_priv->is_lvds = false;
1945
1946 if (device_is_registered(&connector->kdev)) {
1947 drm_sysfs_connector_remove(connector);
1948 registered = true;
1949 }
1950
1951 if (flags &
1952 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1953 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
1954 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
1955 else
1956 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
1957
1958 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
1959 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
1960
1961 if (intel_sdvo_get_supp_encode(intel_output,
1962 &sdvo_priv->encode) &&
1963 intel_sdvo_get_digital_encoding_mode(intel_output) &&
1964 sdvo_priv->is_hdmi) {
1965 /* enable hdmi encoding mode if supported */
1966 intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
1967 intel_sdvo_set_colorimetry(intel_output,
1968 SDVO_COLORIMETRY_RGB256);
1969 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
Ma Lingf8aed702009-08-24 13:50:24 +08001970 intel_output->clone_mask =
1971 (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
1972 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001973 }
1974 } else if (flags & SDVO_OUTPUT_SVID0) {
1975
1976 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
1977 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
1978 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
1979 sdvo_priv->is_tv = true;
1980 intel_output->needs_tv_clock = true;
Ma Lingf8aed702009-08-24 13:50:24 +08001981 intel_output->clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001982 } else if (flags & SDVO_OUTPUT_RGB0) {
1983
1984 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
1985 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
1986 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Ma Lingf8aed702009-08-24 13:50:24 +08001987 intel_output->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
1988 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001989 } else if (flags & SDVO_OUTPUT_RGB1) {
1990
1991 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
1992 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
1993 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
1994 } else if (flags & SDVO_OUTPUT_LVDS0) {
1995
1996 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
1997 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
1998 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
1999 sdvo_priv->is_lvds = true;
Ma Lingf8aed702009-08-24 13:50:24 +08002000 intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
2001 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002002 } else if (flags & SDVO_OUTPUT_LVDS1) {
2003
2004 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
2005 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2006 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2007 sdvo_priv->is_lvds = true;
Ma Lingf8aed702009-08-24 13:50:24 +08002008 intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
2009 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002010 } else {
2011
2012 unsigned char bytes[2];
2013
2014 sdvo_priv->controlled_output = 0;
2015 memcpy(bytes, &sdvo_priv->caps.output_flags, 2);
2016 DRM_DEBUG_KMS(I915_SDVO,
2017 "%s: Unknown SDVO output type (0x%02x%02x)\n",
2018 SDVO_NAME(sdvo_priv),
2019 bytes[0], bytes[1]);
2020 ret = false;
2021 }
Ma Lingf8aed702009-08-24 13:50:24 +08002022 intel_output->crtc_mask = (1 << 0) | (1 << 1);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002023
2024 if (ret && registered)
2025 ret = drm_sysfs_connector_add(connector) == 0 ? true : false;
2026
2027
2028 return ret;
2029
2030}
2031
Eric Anholt7d573822009-01-02 13:33:00 -08002032bool intel_sdvo_init(struct drm_device *dev, int output_device)
Jesse Barnes79e53942008-11-07 14:24:08 -08002033{
2034 struct drm_connector *connector;
2035 struct intel_output *intel_output;
2036 struct intel_sdvo_priv *sdvo_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07002037
Jesse Barnes79e53942008-11-07 14:24:08 -08002038 u8 ch[0x40];
2039 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08002040
2041 intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
2042 if (!intel_output) {
Eric Anholt7d573822009-01-02 13:33:00 -08002043 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002044 }
2045
Jesse Barnes79e53942008-11-07 14:24:08 -08002046 sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
Keith Packard308cd3a2009-06-14 11:56:18 -07002047 sdvo_priv->output_device = output_device;
2048
2049 intel_output->dev_priv = sdvo_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08002050 intel_output->type = INTEL_OUTPUT_SDVO;
2051
Jesse Barnes79e53942008-11-07 14:24:08 -08002052 /* setup the DDC bus. */
Keith Packard308cd3a2009-06-14 11:56:18 -07002053 if (output_device == SDVOB)
2054 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
2055 else
2056 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
2057
2058 if (!intel_output->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002059 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002060
Keith Packard308cd3a2009-06-14 11:56:18 -07002061 sdvo_priv->slave_addr = intel_sdvo_get_slave_addr(dev, output_device);
Jesse Barnes79e53942008-11-07 14:24:08 -08002062
Keith Packard308cd3a2009-06-14 11:56:18 -07002063 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
2064 intel_sdvo_i2c_bit_algo.functionality = intel_output->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002065
Jesse Barnes79e53942008-11-07 14:24:08 -08002066 /* Read the regs to test if we can talk to the device */
2067 for (i = 0; i < 0x40; i++) {
2068 if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
yakui_zhao342dc382009-06-02 14:12:00 +08002069 DRM_DEBUG_KMS(I915_SDVO,
2070 "No SDVO device found on SDVO%c\n",
2071 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002072 goto err_i2c;
2073 }
2074 }
2075
Ma Ling619ac3b2009-05-18 16:12:46 +08002076 /* setup the DDC bus. */
2077 if (output_device == SDVOB)
Keith Packard308cd3a2009-06-14 11:56:18 -07002078 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOB DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002079 else
Keith Packard308cd3a2009-06-14 11:56:18 -07002080 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOC DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002081
Keith Packard308cd3a2009-06-14 11:56:18 -07002082 if (intel_output->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002083 goto err_i2c;
2084
Keith Packard308cd3a2009-06-14 11:56:18 -07002085 /* Wrap with our custom algo which switches to DDC mode */
2086 intel_output->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002087
Ma Ling7086c872009-05-13 11:20:06 +08002088 /* In defaut case sdvo lvds is false */
Jesse Barnes79e53942008-11-07 14:24:08 -08002089 intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
2090
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002091 if (intel_sdvo_output_setup(intel_output,
2092 sdvo_priv->caps.output_flags) != true) {
2093 DRM_DEBUG("SDVO output failed to setup on SDVO%c\n",
2094 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002095 goto err_i2c;
2096 }
2097
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002098
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002099 connector = &intel_output->base;
2100 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002101 connector->connector_type);
2102
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002103 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
2104 connector->interlace_allowed = 0;
2105 connector->doublescan_allowed = 0;
2106 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2107
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002108 drm_encoder_init(dev, &intel_output->enc,
2109 &intel_sdvo_enc_funcs, intel_output->enc.encoder_type);
2110
Jesse Barnes79e53942008-11-07 14:24:08 -08002111 drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08002112
2113 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
2114 drm_sysfs_connector_add(connector);
2115
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002116 intel_sdvo_select_ddc_bus(sdvo_priv);
2117
Jesse Barnes79e53942008-11-07 14:24:08 -08002118 /* Set the input timing to the screen. Assume always input 0. */
2119 intel_sdvo_set_target_input(intel_output, true, false);
2120
2121 intel_sdvo_get_input_pixel_clock_range(intel_output,
2122 &sdvo_priv->pixel_clock_min,
2123 &sdvo_priv->pixel_clock_max);
2124
2125
yakui_zhao342dc382009-06-02 14:12:00 +08002126 DRM_DEBUG_KMS(I915_SDVO, "%s device VID/DID: %02X:%02X.%02X, "
2127 "clock range %dMHz - %dMHz, "
2128 "input 1: %c, input 2: %c, "
2129 "output 1: %c, output 2: %c\n",
2130 SDVO_NAME(sdvo_priv),
2131 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
2132 sdvo_priv->caps.device_rev_id,
2133 sdvo_priv->pixel_clock_min / 1000,
2134 sdvo_priv->pixel_clock_max / 1000,
2135 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2136 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2137 /* check currently supported outputs */
2138 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002139 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002140 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002141 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2142
Eric Anholt7d573822009-01-02 13:33:00 -08002143 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002144
2145err_i2c:
Keith Packard308cd3a2009-06-14 11:56:18 -07002146 if (intel_output->ddc_bus != NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002147 intel_i2c_destroy(intel_output->ddc_bus);
Keith Packard308cd3a2009-06-14 11:56:18 -07002148 if (intel_output->i2c_bus != NULL)
2149 intel_i2c_destroy(intel_output->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002150err_inteloutput:
Jesse Barnes79e53942008-11-07 14:24:08 -08002151 kfree(intel_output);
2152
Eric Anholt7d573822009-01-02 13:33:00 -08002153 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002154}