blob: cabe32df7cd56818d354807a4e5f199ba8cfe144 [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"
Zhao Yakuice6feab2009-08-24 13:50:26 +080041static char *tv_format_names[] = {
42 "NTSC_M" , "NTSC_J" , "NTSC_443",
43 "PAL_B" , "PAL_D" , "PAL_G" ,
44 "PAL_H" , "PAL_I" , "PAL_M" ,
45 "PAL_N" , "PAL_NC" , "PAL_60" ,
46 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
47 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
48 "SECAM_60"
49};
50
51#define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053struct intel_sdvo_priv {
Keith Packardf9c10a92009-05-30 12:16:25 -070054 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080055
56 /* Register for the SDVO device: SDVOB or SDVOC */
Jesse Barnes79e53942008-11-07 14:24:08 -080057 int output_device;
58
Jesse Barnese2f0ba92009-02-02 15:11:52 -080059 /* Active outputs controlled by this SDVO output */
60 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080061
Jesse Barnese2f0ba92009-02-02 15:11:52 -080062 /*
63 * Capabilities of the SDVO device returned by
64 * i830_sdvo_get_capabilities()
65 */
Jesse Barnes79e53942008-11-07 14:24:08 -080066 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080067
68 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080069 int pixel_clock_min, pixel_clock_max;
70
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080071 /*
72 * For multiple function SDVO device,
73 * this is for current attached outputs.
74 */
75 uint16_t attached_output;
76
Jesse Barnese2f0ba92009-02-02 15:11:52 -080077 /**
78 * This is set if we're going to treat the device as TV-out.
79 *
80 * While we have these nice friendly flags for output types that ought
81 * to decide this for us, the S-Video output on our HDMI+S-Video card
82 * shows up as RGB1 (VGA).
83 */
84 bool is_tv;
85
Zhao Yakuice6feab2009-08-24 13:50:26 +080086 /* This is for current tv format name */
87 char *tv_format_name;
88
89 /* This contains all current supported TV format */
90 char *tv_format_supported[TV_FORMAT_NUM];
91 int format_supported_num;
92 struct drm_property *tv_format_property;
93 struct drm_property *tv_format_name_property[TV_FORMAT_NUM];
94
Jesse Barnese2f0ba92009-02-02 15:11:52 -080095 /**
96 * This is set if we treat the device as HDMI, instead of DVI.
97 */
98 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +080099
Ma Ling7086c872009-05-13 11:20:06 +0800100 /**
101 * This is set if we detect output of sdvo device as LVDS.
102 */
103 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800104
105 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800106 * This is sdvo flags for input timing.
107 */
108 uint8_t sdvo_flags;
109
110 /**
111 * This is sdvo fixed pannel mode pointer
112 */
113 struct drm_display_mode *sdvo_lvds_fixed_mode;
114
115 /**
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800116 * Returned SDTV resolutions allowed for the current format, if the
117 * device reported it.
118 */
119 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
120
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800121 /*
122 * supported encoding mode, used to determine whether HDMI is
123 * supported
124 */
125 struct intel_sdvo_encode encode;
126
127 /* DDC bus used by this SDVO output */
128 uint8_t ddc_bus;
129
Jesse Barnes79e53942008-11-07 14:24:08 -0800130 int save_sdvo_mult;
131 u16 save_active_outputs;
132 struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
133 struct intel_sdvo_dtd save_output_dtd[16];
134 u32 save_SDVOX;
135};
136
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800137static bool
138intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags);
139
Jesse Barnes79e53942008-11-07 14:24:08 -0800140/**
141 * Writes the SDVOB or SDVOC with the given value, but always writes both
142 * SDVOB and SDVOC to work around apparent hardware issues (according to
143 * comments in the BIOS).
144 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100145static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800146{
147 struct drm_device *dev = intel_output->base.dev;
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
150 u32 bval = val, cval = val;
151 int i;
152
153 if (sdvo_priv->output_device == SDVOB) {
154 cval = I915_READ(SDVOC);
155 } else {
156 bval = I915_READ(SDVOB);
157 }
158 /*
159 * Write the registers twice for luck. Sometimes,
160 * writing them only once doesn't appear to 'stick'.
161 * The BIOS does this too. Yay, magic
162 */
163 for (i = 0; i < 2; i++)
164 {
165 I915_WRITE(SDVOB, bval);
166 I915_READ(SDVOB);
167 I915_WRITE(SDVOC, cval);
168 I915_READ(SDVOC);
169 }
170}
171
172static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
173 u8 *ch)
174{
175 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
176 u8 out_buf[2];
177 u8 buf[2];
178 int ret;
179
180 struct i2c_msg msgs[] = {
181 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700182 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800183 .flags = 0,
184 .len = 1,
185 .buf = out_buf,
186 },
187 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700188 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800189 .flags = I2C_M_RD,
190 .len = 1,
191 .buf = buf,
192 }
193 };
194
195 out_buf[0] = addr;
196 out_buf[1] = 0;
197
Keith Packard308cd3a2009-06-14 11:56:18 -0700198 if ((ret = i2c_transfer(intel_output->i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800199 {
200 *ch = buf[0];
201 return true;
202 }
203
204 DRM_DEBUG("i2c transfer returned %d\n", ret);
205 return false;
206}
207
208static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
209 u8 ch)
210{
Keith Packardf9c10a92009-05-30 12:16:25 -0700211 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800212 u8 out_buf[2];
213 struct i2c_msg msgs[] = {
214 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700215 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800216 .flags = 0,
217 .len = 2,
218 .buf = out_buf,
219 }
220 };
221
222 out_buf[0] = addr;
223 out_buf[1] = ch;
224
Keith Packardf9c10a92009-05-30 12:16:25 -0700225 if (i2c_transfer(intel_output->i2c_bus, msgs, 1) == 1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 {
227 return true;
228 }
229 return false;
230}
231
232#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
233/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100234static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800235 u8 cmd;
236 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800237} sdvo_cmd_names[] = {
238 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
239 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
240 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
241 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
242 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
243 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
244 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
245 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
246 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
247 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
248 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
249 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
250 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
251 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
252 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
253 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
254 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
255 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
256 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
257 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
258 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
259 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
260 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
261 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
262 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
263 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
264 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
265 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
266 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
267 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
268 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
269 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
270 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
271 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
272 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800273 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
274 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
275 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
276 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800277 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800278 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
279 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
280 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
281 /* HDMI op code */
282 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
283 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
289 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
290 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
291 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
292 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800302};
303
304#define SDVO_NAME(dev_priv) ((dev_priv)->output_device == SDVOB ? "SDVOB" : "SDVOC")
305#define SDVO_PRIV(output) ((struct intel_sdvo_priv *) (output)->dev_priv)
306
307#ifdef SDVO_DEBUG
308static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
309 void *args, int args_len)
310{
311 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
312 int i;
313
yakui_zhao342dc382009-06-02 14:12:00 +0800314 DRM_DEBUG_KMS(I915_SDVO, "%s: W: %02X ",
315 SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800316 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800317 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800318 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800319 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800320 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
321 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800322 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800323 break;
324 }
325 }
326 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
yakui_zhao342dc382009-06-02 14:12:00 +0800327 DRM_LOG_KMS("(%02X)", cmd);
328 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800329}
330#else
331#define intel_sdvo_debug_write(o, c, a, l)
332#endif
333
334static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
335 void *args, int args_len)
336{
337 int i;
338
339 intel_sdvo_debug_write(intel_output, cmd, args, args_len);
340
341 for (i = 0; i < args_len; i++) {
342 intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
343 ((u8*)args)[i]);
344 }
345
346 intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
347}
348
349#ifdef SDVO_DEBUG
350static const char *cmd_status_names[] = {
351 "Power on",
352 "Success",
353 "Not supported",
354 "Invalid arg",
355 "Pending",
356 "Target not specified",
357 "Scaling not supported"
358};
359
360static void intel_sdvo_debug_response(struct intel_output *intel_output,
361 void *response, int response_len,
362 u8 status)
363{
364 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800365 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800366
yakui_zhao342dc382009-06-02 14:12:00 +0800367 DRM_DEBUG_KMS(I915_SDVO, "%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800368 for (i = 0; i < response_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800369 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800370 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800371 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800372 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800373 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800374 else
yakui_zhao342dc382009-06-02 14:12:00 +0800375 DRM_LOG_KMS("(??? %d)", status);
376 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800377}
378#else
379#define intel_sdvo_debug_response(o, r, l, s)
380#endif
381
382static u8 intel_sdvo_read_response(struct intel_output *intel_output,
383 void *response, int response_len)
384{
385 int i;
386 u8 status;
387 u8 retry = 50;
388
389 while (retry--) {
390 /* Read the command response */
391 for (i = 0; i < response_len; i++) {
392 intel_sdvo_read_byte(intel_output,
393 SDVO_I2C_RETURN_0 + i,
394 &((u8 *)response)[i]);
395 }
396
397 /* read the return status */
398 intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
399 &status);
400
401 intel_sdvo_debug_response(intel_output, response, response_len,
402 status);
403 if (status != SDVO_CMD_STATUS_PENDING)
404 return status;
405
406 mdelay(50);
407 }
408
409 return status;
410}
411
Hannes Ederb358d0a2008-12-18 21:18:47 +0100412static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800413{
414 if (mode->clock >= 100000)
415 return 1;
416 else if (mode->clock >= 50000)
417 return 2;
418 else
419 return 4;
420}
421
422/**
423 * Don't check status code from this as it switches the bus back to the
424 * SDVO chips which defeats the purpose of doing a bus switch in the first
425 * place.
426 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100427static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
428 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800429{
430 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
431}
432
433static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
434{
435 struct intel_sdvo_set_target_input_args targets = {0};
436 u8 status;
437
438 if (target_0 && target_1)
439 return SDVO_CMD_STATUS_NOTSUPP;
440
441 if (target_1)
442 targets.target_1 = 1;
443
444 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
445 sizeof(targets));
446
447 status = intel_sdvo_read_response(intel_output, NULL, 0);
448
449 return (status == SDVO_CMD_STATUS_SUCCESS);
450}
451
452/**
453 * Return whether each input is trained.
454 *
455 * This function is making an assumption about the layout of the response,
456 * which should be checked against the docs.
457 */
458static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
459{
460 struct intel_sdvo_get_trained_inputs_response response;
461 u8 status;
462
463 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
464 status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
465 if (status != SDVO_CMD_STATUS_SUCCESS)
466 return false;
467
468 *input_1 = response.input0_trained;
469 *input_2 = response.input1_trained;
470 return true;
471}
472
473static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
474 u16 *outputs)
475{
476 u8 status;
477
478 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
479 status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
480
481 return (status == SDVO_CMD_STATUS_SUCCESS);
482}
483
484static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
485 u16 outputs)
486{
487 u8 status;
488
489 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
490 sizeof(outputs));
491 status = intel_sdvo_read_response(intel_output, NULL, 0);
492 return (status == SDVO_CMD_STATUS_SUCCESS);
493}
494
495static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
496 int mode)
497{
498 u8 status, state = SDVO_ENCODER_STATE_ON;
499
500 switch (mode) {
501 case DRM_MODE_DPMS_ON:
502 state = SDVO_ENCODER_STATE_ON;
503 break;
504 case DRM_MODE_DPMS_STANDBY:
505 state = SDVO_ENCODER_STATE_STANDBY;
506 break;
507 case DRM_MODE_DPMS_SUSPEND:
508 state = SDVO_ENCODER_STATE_SUSPEND;
509 break;
510 case DRM_MODE_DPMS_OFF:
511 state = SDVO_ENCODER_STATE_OFF;
512 break;
513 }
514
515 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
516 sizeof(state));
517 status = intel_sdvo_read_response(intel_output, NULL, 0);
518
519 return (status == SDVO_CMD_STATUS_SUCCESS);
520}
521
522static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
523 int *clock_min,
524 int *clock_max)
525{
526 struct intel_sdvo_pixel_clock_range clocks;
527 u8 status;
528
529 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
530 NULL, 0);
531
532 status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
533
534 if (status != SDVO_CMD_STATUS_SUCCESS)
535 return false;
536
537 /* Convert the values from units of 10 kHz to kHz. */
538 *clock_min = clocks.min * 10;
539 *clock_max = clocks.max * 10;
540
541 return true;
542}
543
544static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
545 u16 outputs)
546{
547 u8 status;
548
549 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
550 sizeof(outputs));
551
552 status = intel_sdvo_read_response(intel_output, NULL, 0);
553 return (status == SDVO_CMD_STATUS_SUCCESS);
554}
555
556static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
557 struct intel_sdvo_dtd *dtd)
558{
559 u8 status;
560
561 intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
562 status = intel_sdvo_read_response(intel_output, &dtd->part1,
563 sizeof(dtd->part1));
564 if (status != SDVO_CMD_STATUS_SUCCESS)
565 return false;
566
567 intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
568 status = intel_sdvo_read_response(intel_output, &dtd->part2,
569 sizeof(dtd->part2));
570 if (status != SDVO_CMD_STATUS_SUCCESS)
571 return false;
572
573 return true;
574}
575
576static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
577 struct intel_sdvo_dtd *dtd)
578{
579 return intel_sdvo_get_timing(intel_output,
580 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
581}
582
583static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
584 struct intel_sdvo_dtd *dtd)
585{
586 return intel_sdvo_get_timing(intel_output,
587 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
588}
589
590static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
591 struct intel_sdvo_dtd *dtd)
592{
593 u8 status;
594
595 intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
596 status = intel_sdvo_read_response(intel_output, NULL, 0);
597 if (status != SDVO_CMD_STATUS_SUCCESS)
598 return false;
599
600 intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
601 status = intel_sdvo_read_response(intel_output, NULL, 0);
602 if (status != SDVO_CMD_STATUS_SUCCESS)
603 return false;
604
605 return true;
606}
607
608static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
609 struct intel_sdvo_dtd *dtd)
610{
611 return intel_sdvo_set_timing(intel_output,
612 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
613}
614
615static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
616 struct intel_sdvo_dtd *dtd)
617{
618 return intel_sdvo_set_timing(intel_output,
619 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
620}
621
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800622static bool
623intel_sdvo_create_preferred_input_timing(struct intel_output *output,
624 uint16_t clock,
625 uint16_t width,
626 uint16_t height)
627{
628 struct intel_sdvo_preferred_input_timing_args args;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800629 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800630 uint8_t status;
631
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800632 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800633 args.clock = clock;
634 args.width = width;
635 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800636 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800637
638 if (sdvo_priv->is_lvds &&
639 (sdvo_priv->sdvo_lvds_fixed_mode->hdisplay != width ||
640 sdvo_priv->sdvo_lvds_fixed_mode->vdisplay != height))
641 args.scaled = 1;
642
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800643 intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
644 &args, sizeof(args));
645 status = intel_sdvo_read_response(output, NULL, 0);
646 if (status != SDVO_CMD_STATUS_SUCCESS)
647 return false;
648
649 return true;
650}
651
652static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
653 struct intel_sdvo_dtd *dtd)
654{
655 bool status;
656
657 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
658 NULL, 0);
659
660 status = intel_sdvo_read_response(output, &dtd->part1,
661 sizeof(dtd->part1));
662 if (status != SDVO_CMD_STATUS_SUCCESS)
663 return false;
664
665 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
666 NULL, 0);
667
668 status = intel_sdvo_read_response(output, &dtd->part2,
669 sizeof(dtd->part2));
670 if (status != SDVO_CMD_STATUS_SUCCESS)
671 return false;
672
673 return false;
674}
Jesse Barnes79e53942008-11-07 14:24:08 -0800675
676static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
677{
678 u8 response, status;
679
680 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
681 status = intel_sdvo_read_response(intel_output, &response, 1);
682
683 if (status != SDVO_CMD_STATUS_SUCCESS) {
684 DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
685 return SDVO_CLOCK_RATE_MULT_1X;
686 } else {
687 DRM_DEBUG("Current clock rate multiplier: %d\n", response);
688 }
689
690 return response;
691}
692
693static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
694{
695 u8 status;
696
697 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
698 status = intel_sdvo_read_response(intel_output, NULL, 0);
699 if (status != SDVO_CMD_STATUS_SUCCESS)
700 return false;
701
702 return true;
703}
704
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800705static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
706 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800707{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800708 uint16_t width, height;
709 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
710 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800711
712 width = mode->crtc_hdisplay;
713 height = mode->crtc_vdisplay;
714
715 /* do some mode translations */
716 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
717 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
718
719 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
720 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
721
722 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
723 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
724
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800725 dtd->part1.clock = mode->clock / 10;
726 dtd->part1.h_active = width & 0xff;
727 dtd->part1.h_blank = h_blank_len & 0xff;
728 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800729 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800730 dtd->part1.v_active = height & 0xff;
731 dtd->part1.v_blank = v_blank_len & 0xff;
732 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800733 ((v_blank_len >> 8) & 0xf);
734
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800735 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800736 dtd->part2.h_sync_width = h_sync_len & 0xff;
737 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800738 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800739 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800740 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
741 ((v_sync_len & 0x30) >> 4);
742
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800743 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800744 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800745 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800746 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800747 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800748
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800749 dtd->part2.sdvo_flags = 0;
750 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
751 dtd->part2.reserved = 0;
752}
Jesse Barnes79e53942008-11-07 14:24:08 -0800753
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800754static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
755 struct intel_sdvo_dtd *dtd)
756{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800757 mode->hdisplay = dtd->part1.h_active;
758 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
759 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800760 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800761 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
762 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
763 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
764 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
765
766 mode->vdisplay = dtd->part1.v_active;
767 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
768 mode->vsync_start = mode->vdisplay;
769 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800770 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800771 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
772 mode->vsync_end = mode->vsync_start +
773 (dtd->part2.v_sync_off_width & 0xf);
774 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
775 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
776 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
777
778 mode->clock = dtd->part1.clock * 10;
779
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800780 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800781 if (dtd->part2.dtd_flags & 0x2)
782 mode->flags |= DRM_MODE_FLAG_PHSYNC;
783 if (dtd->part2.dtd_flags & 0x4)
784 mode->flags |= DRM_MODE_FLAG_PVSYNC;
785}
786
787static bool intel_sdvo_get_supp_encode(struct intel_output *output,
788 struct intel_sdvo_encode *encode)
789{
790 uint8_t status;
791
792 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
793 status = intel_sdvo_read_response(output, encode, sizeof(*encode));
794 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
795 memset(encode, 0, sizeof(*encode));
796 return false;
797 }
798
799 return true;
800}
801
802static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
803{
804 uint8_t status;
805
806 intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
807 status = intel_sdvo_read_response(output, NULL, 0);
808
809 return (status == SDVO_CMD_STATUS_SUCCESS);
810}
811
812static bool intel_sdvo_set_colorimetry(struct intel_output *output,
813 uint8_t mode)
814{
815 uint8_t status;
816
817 intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
818 status = intel_sdvo_read_response(output, NULL, 0);
819
820 return (status == SDVO_CMD_STATUS_SUCCESS);
821}
822
823#if 0
824static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
825{
826 int i, j;
827 uint8_t set_buf_index[2];
828 uint8_t av_split;
829 uint8_t buf_size;
830 uint8_t buf[48];
831 uint8_t *pos;
832
833 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
834 intel_sdvo_read_response(output, &av_split, 1);
835
836 for (i = 0; i <= av_split; i++) {
837 set_buf_index[0] = i; set_buf_index[1] = 0;
838 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
839 set_buf_index, 2);
840 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
841 intel_sdvo_read_response(output, &buf_size, 1);
842
843 pos = buf;
844 for (j = 0; j <= buf_size; j += 8) {
845 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
846 NULL, 0);
847 intel_sdvo_read_response(output, pos, 8);
848 pos += 8;
849 }
850 }
851}
852#endif
853
854static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
855 uint8_t *data, int8_t size, uint8_t tx_rate)
856{
857 uint8_t set_buf_index[2];
858
859 set_buf_index[0] = index;
860 set_buf_index[1] = 0;
861
862 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
863
864 for (; size > 0; size -= 8) {
865 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
866 data += 8;
867 }
868
869 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
870}
871
872static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
873{
874 uint8_t csum = 0;
875 int i;
876
877 for (i = 0; i < size; i++)
878 csum += data[i];
879
880 return 0x100 - csum;
881}
882
883#define DIP_TYPE_AVI 0x82
884#define DIP_VERSION_AVI 0x2
885#define DIP_LEN_AVI 13
886
887struct dip_infoframe {
888 uint8_t type;
889 uint8_t version;
890 uint8_t len;
891 uint8_t checksum;
892 union {
893 struct {
894 /* Packet Byte #1 */
895 uint8_t S:2;
896 uint8_t B:2;
897 uint8_t A:1;
898 uint8_t Y:2;
899 uint8_t rsvd1:1;
900 /* Packet Byte #2 */
901 uint8_t R:4;
902 uint8_t M:2;
903 uint8_t C:2;
904 /* Packet Byte #3 */
905 uint8_t SC:2;
906 uint8_t Q:2;
907 uint8_t EC:3;
908 uint8_t ITC:1;
909 /* Packet Byte #4 */
910 uint8_t VIC:7;
911 uint8_t rsvd2:1;
912 /* Packet Byte #5 */
913 uint8_t PR:4;
914 uint8_t rsvd3:4;
915 /* Packet Byte #6~13 */
916 uint16_t top_bar_end;
917 uint16_t bottom_bar_start;
918 uint16_t left_bar_end;
919 uint16_t right_bar_start;
920 } avi;
921 struct {
922 /* Packet Byte #1 */
923 uint8_t channel_count:3;
924 uint8_t rsvd1:1;
925 uint8_t coding_type:4;
926 /* Packet Byte #2 */
927 uint8_t sample_size:2; /* SS0, SS1 */
928 uint8_t sample_frequency:3;
929 uint8_t rsvd2:3;
930 /* Packet Byte #3 */
931 uint8_t coding_type_private:5;
932 uint8_t rsvd3:3;
933 /* Packet Byte #4 */
934 uint8_t channel_allocation;
935 /* Packet Byte #5 */
936 uint8_t rsvd4:3;
937 uint8_t level_shift:4;
938 uint8_t downmix_inhibit:1;
939 } audio;
940 uint8_t payload[28];
941 } __attribute__ ((packed)) u;
942} __attribute__((packed));
943
944static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
945 struct drm_display_mode * mode)
946{
947 struct dip_infoframe avi_if = {
948 .type = DIP_TYPE_AVI,
949 .version = DIP_VERSION_AVI,
950 .len = DIP_LEN_AVI,
951 };
952
953 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
954 4 + avi_if.len);
955 intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
956 SDVO_HBUF_TX_VSYNC);
957}
958
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800959static void intel_sdvo_set_tv_format(struct intel_output *output)
960{
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800961
Zhao Yakuice6feab2009-08-24 13:50:26 +0800962 struct intel_sdvo_tv_format format;
963 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
964 uint32_t format_map, i;
965 uint8_t status;
966
967 for (i = 0; i < TV_FORMAT_NUM; i++)
968 if (tv_format_names[i] == sdvo_priv->tv_format_name)
969 break;
970
971 format_map = 1 << i;
972 memset(&format, 0, sizeof(format));
973 memcpy(&format, &format_map, sizeof(format_map) > sizeof(format) ?
974 sizeof(format) : sizeof(format_map));
975
976 intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, &format_map,
977 sizeof(format));
978
979 status = intel_sdvo_read_response(output, NULL, 0);
980 if (status != SDVO_CMD_STATUS_SUCCESS)
981 DRM_DEBUG("%s: Failed to set TV format\n",
982 SDVO_NAME(sdvo_priv));
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800983}
984
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800985static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
986 struct drm_display_mode *mode,
987 struct drm_display_mode *adjusted_mode)
988{
989 struct intel_output *output = enc_to_intel_output(encoder);
990 struct intel_sdvo_priv *dev_priv = output->dev_priv;
991
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800992 if (dev_priv->is_tv) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800993 struct intel_sdvo_dtd output_dtd;
994 bool success;
995
996 /* We need to construct preferred input timings based on our
997 * output timings. To do that, we have to set the output
998 * timings, even though this isn't really the right place in
999 * the sequence to do it. Oh well.
1000 */
1001
1002
1003 /* Set output timings */
1004 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1005 intel_sdvo_set_target_output(output,
1006 dev_priv->controlled_output);
1007 intel_sdvo_set_output_timing(output, &output_dtd);
1008
1009 /* Set the input timing to the screen. Assume always input 0. */
1010 intel_sdvo_set_target_input(output, true, false);
1011
1012
1013 success = intel_sdvo_create_preferred_input_timing(output,
1014 mode->clock / 10,
1015 mode->hdisplay,
1016 mode->vdisplay);
1017 if (success) {
1018 struct intel_sdvo_dtd input_dtd;
1019
1020 intel_sdvo_get_preferred_input_timing(output,
1021 &input_dtd);
1022 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001023 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001024
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001025 drm_mode_set_crtcinfo(adjusted_mode, 0);
1026
1027 mode->clock = adjusted_mode->clock;
1028
1029 adjusted_mode->clock *=
1030 intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001031 } else {
1032 return false;
1033 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001034 } else if (dev_priv->is_lvds) {
1035 struct intel_sdvo_dtd output_dtd;
1036 bool success;
1037
1038 drm_mode_set_crtcinfo(dev_priv->sdvo_lvds_fixed_mode, 0);
1039 /* Set output timings */
1040 intel_sdvo_get_dtd_from_mode(&output_dtd,
1041 dev_priv->sdvo_lvds_fixed_mode);
1042
1043 intel_sdvo_set_target_output(output,
1044 dev_priv->controlled_output);
1045 intel_sdvo_set_output_timing(output, &output_dtd);
1046
1047 /* Set the input timing to the screen. Assume always input 0. */
1048 intel_sdvo_set_target_input(output, true, false);
1049
1050
1051 success = intel_sdvo_create_preferred_input_timing(
1052 output,
1053 mode->clock / 10,
1054 mode->hdisplay,
1055 mode->vdisplay);
1056
1057 if (success) {
1058 struct intel_sdvo_dtd input_dtd;
1059
1060 intel_sdvo_get_preferred_input_timing(output,
1061 &input_dtd);
1062 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1063 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
1064
1065 drm_mode_set_crtcinfo(adjusted_mode, 0);
1066
1067 mode->clock = adjusted_mode->clock;
1068
1069 adjusted_mode->clock *=
1070 intel_sdvo_get_pixel_multiplier(mode);
1071 } else {
1072 return false;
1073 }
1074
1075 } else {
1076 /* Make the CRTC code factor in the SDVO pixel multiplier. The
1077 * SDVO device will be told of the multiplier during mode_set.
1078 */
1079 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001080 }
1081 return true;
1082}
1083
1084static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1085 struct drm_display_mode *mode,
1086 struct drm_display_mode *adjusted_mode)
1087{
1088 struct drm_device *dev = encoder->dev;
1089 struct drm_i915_private *dev_priv = dev->dev_private;
1090 struct drm_crtc *crtc = encoder->crtc;
1091 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1092 struct intel_output *output = enc_to_intel_output(encoder);
1093 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1094 u32 sdvox = 0;
1095 int sdvo_pixel_multiply;
1096 struct intel_sdvo_in_out_map in_out;
1097 struct intel_sdvo_dtd input_dtd;
1098 u8 status;
1099
1100 if (!mode)
1101 return;
1102
1103 /* First, set the input mapping for the first input to our controlled
1104 * output. This is only correct if we're a single-input device, in
1105 * which case the first input is the output from the appropriate SDVO
1106 * channel on the motherboard. In a two-input device, the first input
1107 * will be SDVOB and the second SDVOC.
1108 */
1109 in_out.in0 = sdvo_priv->controlled_output;
1110 in_out.in1 = 0;
1111
1112 intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
1113 &in_out, sizeof(in_out));
1114 status = intel_sdvo_read_response(output, NULL, 0);
1115
1116 if (sdvo_priv->is_hdmi) {
1117 intel_sdvo_set_avi_infoframe(output, mode);
1118 sdvox |= SDVO_AUDIO_ENABLE;
1119 }
1120
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001121 /* We have tried to get input timing in mode_fixup, and filled into
1122 adjusted_mode */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001123 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001124 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001125 input_dtd.part2.sdvo_flags = sdvo_priv->sdvo_flags;
1126 } else
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001127 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001128
1129 /* If it's a TV, we already set the output timing in mode_fixup.
1130 * Otherwise, the output timing is equal to the input timing.
1131 */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001132 if (!sdvo_priv->is_tv && !sdvo_priv->is_lvds) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001133 /* Set the output timing to the screen */
1134 intel_sdvo_set_target_output(output,
1135 sdvo_priv->controlled_output);
1136 intel_sdvo_set_output_timing(output, &input_dtd);
1137 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001138
1139 /* Set the input timing to the screen. Assume always input 0. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001140 intel_sdvo_set_target_input(output, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001141
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001142 if (sdvo_priv->is_tv)
1143 intel_sdvo_set_tv_format(output);
1144
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001145 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001146 * provide the device with a timing it can support, if it supports that
1147 * feature. However, presumably we would need to adjust the CRTC to
1148 * output the preferred timing, and we don't support that currently.
1149 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001150#if 0
1151 success = intel_sdvo_create_preferred_input_timing(output, clock,
1152 width, height);
1153 if (success) {
1154 struct intel_sdvo_dtd *input_dtd;
1155
1156 intel_sdvo_get_preferred_input_timing(output, &input_dtd);
1157 intel_sdvo_set_input_timing(output, &input_dtd);
1158 }
1159#else
1160 intel_sdvo_set_input_timing(output, &input_dtd);
1161#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001162
1163 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1164 case 1:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001165 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001166 SDVO_CLOCK_RATE_MULT_1X);
1167 break;
1168 case 2:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001169 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001170 SDVO_CLOCK_RATE_MULT_2X);
1171 break;
1172 case 4:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001173 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001174 SDVO_CLOCK_RATE_MULT_4X);
1175 break;
1176 }
1177
1178 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001179 if (IS_I965G(dev)) {
1180 sdvox |= SDVO_BORDER_ENABLE |
1181 SDVO_VSYNC_ACTIVE_HIGH |
1182 SDVO_HSYNC_ACTIVE_HIGH;
1183 } else {
1184 sdvox |= I915_READ(sdvo_priv->output_device);
1185 switch (sdvo_priv->output_device) {
1186 case SDVOB:
1187 sdvox &= SDVOB_PRESERVE_MASK;
1188 break;
1189 case SDVOC:
1190 sdvox &= SDVOC_PRESERVE_MASK;
1191 break;
1192 }
1193 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1194 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001195 if (intel_crtc->pipe == 1)
1196 sdvox |= SDVO_PIPE_B_SELECT;
1197
1198 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1199 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001200 /* done in crtc_mode_set as the dpll_md reg must be written early */
1201 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1202 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001203 } else {
1204 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1205 }
1206
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001207 if (sdvo_priv->sdvo_flags & SDVO_NEED_TO_STALL)
1208 sdvox |= SDVO_STALL_SELECT;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001209 intel_sdvo_write_sdvox(output, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001210}
1211
1212static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1213{
1214 struct drm_device *dev = encoder->dev;
1215 struct drm_i915_private *dev_priv = dev->dev_private;
1216 struct intel_output *intel_output = enc_to_intel_output(encoder);
1217 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1218 u32 temp;
1219
1220 if (mode != DRM_MODE_DPMS_ON) {
1221 intel_sdvo_set_active_outputs(intel_output, 0);
1222 if (0)
1223 intel_sdvo_set_encoder_power_state(intel_output, mode);
1224
1225 if (mode == DRM_MODE_DPMS_OFF) {
1226 temp = I915_READ(sdvo_priv->output_device);
1227 if ((temp & SDVO_ENABLE) != 0) {
1228 intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
1229 }
1230 }
1231 } else {
1232 bool input1, input2;
1233 int i;
1234 u8 status;
1235
1236 temp = I915_READ(sdvo_priv->output_device);
1237 if ((temp & SDVO_ENABLE) == 0)
1238 intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
1239 for (i = 0; i < 2; i++)
1240 intel_wait_for_vblank(dev);
1241
1242 status = intel_sdvo_get_trained_inputs(intel_output, &input1,
1243 &input2);
1244
1245
1246 /* Warn if the device reported failure to sync.
1247 * A lot of SDVO devices fail to notify of sync, but it's
1248 * a given it the status is a success, we succeeded.
1249 */
1250 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1251 DRM_DEBUG("First %s output reported failure to sync\n",
1252 SDVO_NAME(sdvo_priv));
1253 }
1254
1255 if (0)
1256 intel_sdvo_set_encoder_power_state(intel_output, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001257 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001258 }
1259 return;
1260}
1261
1262static void intel_sdvo_save(struct drm_connector *connector)
1263{
1264 struct drm_device *dev = connector->dev;
1265 struct drm_i915_private *dev_priv = dev->dev_private;
1266 struct intel_output *intel_output = to_intel_output(connector);
1267 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1268 int o;
1269
1270 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
1271 intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
1272
1273 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1274 intel_sdvo_set_target_input(intel_output, true, false);
1275 intel_sdvo_get_input_timing(intel_output,
1276 &sdvo_priv->save_input_dtd_1);
1277 }
1278
1279 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1280 intel_sdvo_set_target_input(intel_output, false, true);
1281 intel_sdvo_get_input_timing(intel_output,
1282 &sdvo_priv->save_input_dtd_2);
1283 }
1284
1285 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1286 {
1287 u16 this_output = (1 << o);
1288 if (sdvo_priv->caps.output_flags & this_output)
1289 {
1290 intel_sdvo_set_target_output(intel_output, this_output);
1291 intel_sdvo_get_output_timing(intel_output,
1292 &sdvo_priv->save_output_dtd[o]);
1293 }
1294 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001295 if (sdvo_priv->is_tv) {
1296 /* XXX: Save TV format/enhancements. */
1297 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001298
1299 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
1300}
1301
1302static void intel_sdvo_restore(struct drm_connector *connector)
1303{
1304 struct drm_device *dev = connector->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001305 struct intel_output *intel_output = to_intel_output(connector);
1306 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1307 int o;
1308 int i;
1309 bool input1, input2;
1310 u8 status;
1311
1312 intel_sdvo_set_active_outputs(intel_output, 0);
1313
1314 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1315 {
1316 u16 this_output = (1 << o);
1317 if (sdvo_priv->caps.output_flags & this_output) {
1318 intel_sdvo_set_target_output(intel_output, this_output);
1319 intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
1320 }
1321 }
1322
1323 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1324 intel_sdvo_set_target_input(intel_output, true, false);
1325 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
1326 }
1327
1328 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1329 intel_sdvo_set_target_input(intel_output, false, true);
1330 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
1331 }
1332
1333 intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
1334
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001335 if (sdvo_priv->is_tv) {
1336 /* XXX: Restore TV format/enhancements. */
1337 }
1338
1339 intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
Jesse Barnes79e53942008-11-07 14:24:08 -08001340
1341 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1342 {
1343 for (i = 0; i < 2; i++)
1344 intel_wait_for_vblank(dev);
1345 status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
1346 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
1347 DRM_DEBUG("First %s output reported failure to sync\n",
1348 SDVO_NAME(sdvo_priv));
1349 }
1350
1351 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
1352}
1353
1354static int intel_sdvo_mode_valid(struct drm_connector *connector,
1355 struct drm_display_mode *mode)
1356{
1357 struct intel_output *intel_output = to_intel_output(connector);
1358 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1359
1360 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1361 return MODE_NO_DBLESCAN;
1362
1363 if (sdvo_priv->pixel_clock_min > mode->clock)
1364 return MODE_CLOCK_LOW;
1365
1366 if (sdvo_priv->pixel_clock_max < mode->clock)
1367 return MODE_CLOCK_HIGH;
1368
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001369 if (sdvo_priv->is_lvds == true) {
1370 if (sdvo_priv->sdvo_lvds_fixed_mode == NULL)
1371 return MODE_PANEL;
1372
1373 if (mode->hdisplay > sdvo_priv->sdvo_lvds_fixed_mode->hdisplay)
1374 return MODE_PANEL;
1375
1376 if (mode->vdisplay > sdvo_priv->sdvo_lvds_fixed_mode->vdisplay)
1377 return MODE_PANEL;
1378 }
1379
Jesse Barnes79e53942008-11-07 14:24:08 -08001380 return MODE_OK;
1381}
1382
1383static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
1384{
1385 u8 status;
1386
1387 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1388 status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
1389 if (status != SDVO_CMD_STATUS_SUCCESS)
1390 return false;
1391
1392 return true;
1393}
1394
1395struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1396{
1397 struct drm_connector *connector = NULL;
1398 struct intel_output *iout = NULL;
1399 struct intel_sdvo_priv *sdvo;
1400
1401 /* find the sdvo connector */
1402 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1403 iout = to_intel_output(connector);
1404
1405 if (iout->type != INTEL_OUTPUT_SDVO)
1406 continue;
1407
1408 sdvo = iout->dev_priv;
1409
1410 if (sdvo->output_device == SDVOB && sdvoB)
1411 return connector;
1412
1413 if (sdvo->output_device == SDVOC && !sdvoB)
1414 return connector;
1415
1416 }
1417
1418 return NULL;
1419}
1420
1421int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1422{
1423 u8 response[2];
1424 u8 status;
1425 struct intel_output *intel_output;
1426 DRM_DEBUG("\n");
1427
1428 if (!connector)
1429 return 0;
1430
1431 intel_output = to_intel_output(connector);
1432
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 if (response[0] !=0)
1437 return 1;
1438
1439 return 0;
1440}
1441
1442void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1443{
1444 u8 response[2];
1445 u8 status;
1446 struct intel_output *intel_output = to_intel_output(connector);
1447
1448 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1449 intel_sdvo_read_response(intel_output, &response, 2);
1450
1451 if (on) {
1452 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1453 status = intel_sdvo_read_response(intel_output, &response, 2);
1454
1455 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1456 } else {
1457 response[0] = 0;
1458 response[1] = 0;
1459 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1460 }
1461
1462 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1463 intel_sdvo_read_response(intel_output, &response, 2);
1464}
1465
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001466static bool
1467intel_sdvo_multifunc_encoder(struct intel_output *intel_output)
1468{
1469 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1470 int caps = 0;
1471
1472 if (sdvo_priv->caps.output_flags &
1473 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1474 caps++;
1475 if (sdvo_priv->caps.output_flags &
1476 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1477 caps++;
1478 if (sdvo_priv->caps.output_flags &
Roel Kluin19e1f882009-08-09 13:50:53 +02001479 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID1))
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001480 caps++;
1481 if (sdvo_priv->caps.output_flags &
1482 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1483 caps++;
1484 if (sdvo_priv->caps.output_flags &
1485 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1486 caps++;
1487
1488 if (sdvo_priv->caps.output_flags &
1489 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1490 caps++;
1491
1492 if (sdvo_priv->caps.output_flags &
1493 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1494 caps++;
1495
1496 return (caps > 1);
1497}
1498
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001499enum drm_connector_status
1500intel_sdvo_hdmi_sink_detect(struct drm_connector *connector, u16 response)
Ma Ling9dff6af2009-04-02 13:13:26 +08001501{
1502 struct intel_output *intel_output = to_intel_output(connector);
1503 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001504 enum drm_connector_status status = connector_status_connected;
Ma Ling9dff6af2009-04-02 13:13:26 +08001505 struct edid *edid = NULL;
1506
Ma Ling9dff6af2009-04-02 13:13:26 +08001507 edid = drm_get_edid(&intel_output->base,
Keith Packardf9c10a92009-05-30 12:16:25 -07001508 intel_output->ddc_bus);
Ma Ling9dff6af2009-04-02 13:13:26 +08001509 if (edid != NULL) {
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001510 /* Don't report the output as connected if it's a DVI-I
1511 * connector with a non-digital EDID coming out.
1512 */
1513 if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1514 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1515 sdvo_priv->is_hdmi =
1516 drm_detect_hdmi_monitor(edid);
1517 else
1518 status = connector_status_disconnected;
1519 }
1520
Ma Ling9dff6af2009-04-02 13:13:26 +08001521 kfree(edid);
1522 intel_output->base.display_info.raw_edid = NULL;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001523
1524 } else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1525 status = connector_status_disconnected;
1526
1527 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001528}
1529
Jesse Barnes79e53942008-11-07 14:24:08 -08001530static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1531{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001532 uint16_t response;
Jesse Barnes79e53942008-11-07 14:24:08 -08001533 u8 status;
1534 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001535 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001536
Zhao Yakuice6feab2009-08-24 13:50:26 +08001537 intel_sdvo_write_cmd(intel_output,
1538 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08001539 status = intel_sdvo_read_response(intel_output, &response, 2);
1540
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001541 DRM_DEBUG("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001542
1543 if (status != SDVO_CMD_STATUS_SUCCESS)
1544 return connector_status_unknown;
1545
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001546 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001547 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001548
1549 if (intel_sdvo_multifunc_encoder(intel_output) &&
1550 sdvo_priv->attached_output != response) {
1551 if (sdvo_priv->controlled_output != response &&
1552 intel_sdvo_output_setup(intel_output, response) != true)
1553 return connector_status_unknown;
1554 sdvo_priv->attached_output = response;
1555 }
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001556 return intel_sdvo_hdmi_sink_detect(connector, response);
Jesse Barnes79e53942008-11-07 14:24:08 -08001557}
1558
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001559static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001560{
1561 struct intel_output *intel_output = to_intel_output(connector);
1562
1563 /* set the bus switch and get the modes */
Jesse Barnes79e53942008-11-07 14:24:08 -08001564 intel_ddc_get_modes(intel_output);
1565
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001566#if 0
1567 struct drm_device *dev = encoder->dev;
1568 struct drm_i915_private *dev_priv = dev->dev_private;
1569 /* Mac mini hack. On this device, I get DDC through the analog, which
1570 * load-detects as disconnected. I fail to DDC through the SDVO DDC,
1571 * but it does load-detect as connected. So, just steal the DDC bits
1572 * from analog when we fail at finding it the right way.
1573 */
1574 crt = xf86_config->output[0];
1575 intel_output = crt->driver_private;
1576 if (intel_output->type == I830_OUTPUT_ANALOG &&
1577 crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
1578 I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
1579 edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
1580 xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
1581 }
1582 if (edid_mon) {
1583 xf86OutputSetEDID(output, edid_mon);
1584 modes = xf86OutputGetEDIDModes(output);
1585 }
1586#endif
1587}
1588
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001589/*
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;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001659 uint32_t reply = 0, format_map = 0;
1660 int i;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001661 uint8_t status;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001662
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001663
1664 /* Read the list of supported input resolutions for the selected TV
1665 * format.
1666 */
Zhao Yakuice6feab2009-08-24 13:50:26 +08001667 for (i = 0; i < TV_FORMAT_NUM; i++)
1668 if (tv_format_names[i] == sdvo_priv->tv_format_name)
1669 break;
1670
1671 format_map = (1 << i);
1672 memcpy(&tv_res, &format_map,
1673 sizeof(struct intel_sdvo_sdtv_resolution_request) >
1674 sizeof(format_map) ? sizeof(format_map) :
1675 sizeof(struct intel_sdvo_sdtv_resolution_request));
1676
1677 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1678
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001679 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001680 &tv_res, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001681 status = intel_sdvo_read_response(output, &reply, 3);
1682 if (status != SDVO_CMD_STATUS_SUCCESS)
1683 return;
1684
1685 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001686 if (reply & (1 << i)) {
1687 struct drm_display_mode *nmode;
1688 nmode = drm_mode_duplicate(connector->dev,
1689 &sdvo_tv_modes[i]);
1690 if (nmode)
1691 drm_mode_probed_add(connector, nmode);
1692 }
Zhao Yakuice6feab2009-08-24 13:50:26 +08001693
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001694}
1695
Ma Ling7086c872009-05-13 11:20:06 +08001696static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1697{
1698 struct intel_output *intel_output = to_intel_output(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001699 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001700 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1701 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001702
1703 /*
1704 * Attempt to get the mode list from DDC.
1705 * Assume that the preferred modes are
1706 * arranged in priority order.
1707 */
Ma Ling7086c872009-05-13 11:20:06 +08001708 intel_ddc_get_modes(intel_output);
1709 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001710 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001711
1712 /* Fetch modes from VBT */
1713 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001714 newmode = drm_mode_duplicate(connector->dev,
1715 dev_priv->sdvo_lvds_vbt_mode);
1716 if (newmode != NULL) {
1717 /* Guarantee the mode is preferred */
1718 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1719 DRM_MODE_TYPE_DRIVER);
1720 drm_mode_probed_add(connector, newmode);
1721 }
1722 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001723
1724end:
1725 list_for_each_entry(newmode, &connector->probed_modes, head) {
1726 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1727 sdvo_priv->sdvo_lvds_fixed_mode =
1728 drm_mode_duplicate(connector->dev, newmode);
1729 break;
1730 }
1731 }
1732
Ma Ling7086c872009-05-13 11:20:06 +08001733}
1734
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001735static int intel_sdvo_get_modes(struct drm_connector *connector)
1736{
1737 struct intel_output *output = to_intel_output(connector);
1738 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1739
1740 if (sdvo_priv->is_tv)
1741 intel_sdvo_get_tv_modes(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001742 else if (sdvo_priv->is_lvds == true)
1743 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001744 else
1745 intel_sdvo_get_ddc_modes(connector);
1746
Jesse Barnes79e53942008-11-07 14:24:08 -08001747 if (list_empty(&connector->probed_modes))
1748 return 0;
1749 return 1;
1750}
1751
1752static void intel_sdvo_destroy(struct drm_connector *connector)
1753{
1754 struct intel_output *intel_output = to_intel_output(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001755 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001756
1757 if (intel_output->i2c_bus)
1758 intel_i2c_destroy(intel_output->i2c_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08001759 if (intel_output->ddc_bus)
1760 intel_i2c_destroy(intel_output->ddc_bus);
1761
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001762 if (sdvo_priv->sdvo_lvds_fixed_mode != NULL)
1763 drm_mode_destroy(connector->dev,
1764 sdvo_priv->sdvo_lvds_fixed_mode);
1765
Zhao Yakuice6feab2009-08-24 13:50:26 +08001766 if (sdvo_priv->tv_format_property)
1767 drm_property_destroy(connector->dev,
1768 sdvo_priv->tv_format_property);
1769
Jesse Barnes79e53942008-11-07 14:24:08 -08001770 drm_sysfs_connector_remove(connector);
1771 drm_connector_cleanup(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001772
Jesse Barnes79e53942008-11-07 14:24:08 -08001773 kfree(intel_output);
1774}
1775
Zhao Yakuice6feab2009-08-24 13:50:26 +08001776static int
1777intel_sdvo_set_property(struct drm_connector *connector,
1778 struct drm_property *property,
1779 uint64_t val)
1780{
1781 struct intel_output *intel_output = to_intel_output(connector);
1782 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1783 struct drm_encoder *encoder = &intel_output->enc;
1784 struct drm_crtc *crtc = encoder->crtc;
1785 int ret = 0;
1786 bool changed = false;
1787
1788 ret = drm_connector_property_set_value(connector, property, val);
1789 if (ret < 0)
1790 goto out;
1791
1792 if (property == sdvo_priv->tv_format_property) {
1793 if (val >= TV_FORMAT_NUM) {
1794 ret = -EINVAL;
1795 goto out;
1796 }
1797 if (sdvo_priv->tv_format_name ==
1798 sdvo_priv->tv_format_supported[val])
1799 goto out;
1800
1801 sdvo_priv->tv_format_name = sdvo_priv->tv_format_supported[val];
1802 changed = true;
1803 } else {
1804 ret = -EINVAL;
1805 goto out;
1806 }
1807
1808 if (changed && crtc)
1809 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
1810 crtc->y, crtc->fb);
1811out:
1812 return ret;
1813}
1814
Jesse Barnes79e53942008-11-07 14:24:08 -08001815static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1816 .dpms = intel_sdvo_dpms,
1817 .mode_fixup = intel_sdvo_mode_fixup,
1818 .prepare = intel_encoder_prepare,
1819 .mode_set = intel_sdvo_mode_set,
1820 .commit = intel_encoder_commit,
1821};
1822
1823static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07001824 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08001825 .save = intel_sdvo_save,
1826 .restore = intel_sdvo_restore,
1827 .detect = intel_sdvo_detect,
1828 .fill_modes = drm_helper_probe_single_connector_modes,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001829 .set_property = intel_sdvo_set_property,
Jesse Barnes79e53942008-11-07 14:24:08 -08001830 .destroy = intel_sdvo_destroy,
1831};
1832
1833static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1834 .get_modes = intel_sdvo_get_modes,
1835 .mode_valid = intel_sdvo_mode_valid,
1836 .best_encoder = intel_best_encoder,
1837};
1838
Hannes Ederb358d0a2008-12-18 21:18:47 +01001839static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001840{
1841 drm_encoder_cleanup(encoder);
1842}
1843
1844static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1845 .destroy = intel_sdvo_enc_destroy,
1846};
1847
1848
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001849/**
1850 * Choose the appropriate DDC bus for control bus switch command for this
1851 * SDVO output based on the controlled output.
1852 *
1853 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1854 * outputs, then LVDS outputs.
1855 */
1856static void
1857intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
1858{
1859 uint16_t mask = 0;
1860 unsigned int num_bits;
1861
1862 /* Make a mask of outputs less than or equal to our own priority in the
1863 * list.
1864 */
1865 switch (dev_priv->controlled_output) {
1866 case SDVO_OUTPUT_LVDS1:
1867 mask |= SDVO_OUTPUT_LVDS1;
1868 case SDVO_OUTPUT_LVDS0:
1869 mask |= SDVO_OUTPUT_LVDS0;
1870 case SDVO_OUTPUT_TMDS1:
1871 mask |= SDVO_OUTPUT_TMDS1;
1872 case SDVO_OUTPUT_TMDS0:
1873 mask |= SDVO_OUTPUT_TMDS0;
1874 case SDVO_OUTPUT_RGB1:
1875 mask |= SDVO_OUTPUT_RGB1;
1876 case SDVO_OUTPUT_RGB0:
1877 mask |= SDVO_OUTPUT_RGB0;
1878 break;
1879 }
1880
1881 /* Count bits to find what number we are in the priority list. */
1882 mask &= dev_priv->caps.output_flags;
1883 num_bits = hweight16(mask);
1884 if (num_bits > 3) {
1885 /* if more than 3 outputs, default to DDC bus 3 for now */
1886 num_bits = 3;
1887 }
1888
1889 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1890 dev_priv->ddc_bus = 1 << num_bits;
1891}
1892
1893static bool
1894intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
1895{
1896 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1897 uint8_t status;
1898
1899 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1900
1901 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
1902 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
1903 if (status != SDVO_CMD_STATUS_SUCCESS)
1904 return false;
1905 return true;
1906}
1907
Ma Ling619ac3b2009-05-18 16:12:46 +08001908static struct intel_output *
1909intel_sdvo_chan_to_intel_output(struct intel_i2c_chan *chan)
1910{
1911 struct drm_device *dev = chan->drm_dev;
1912 struct drm_connector *connector;
1913 struct intel_output *intel_output = NULL;
1914
1915 list_for_each_entry(connector,
1916 &dev->mode_config.connector_list, head) {
Keith Packardf9c10a92009-05-30 12:16:25 -07001917 if (to_intel_output(connector)->ddc_bus == &chan->adapter) {
Ma Ling619ac3b2009-05-18 16:12:46 +08001918 intel_output = to_intel_output(connector);
1919 break;
1920 }
1921 }
1922 return intel_output;
1923}
1924
1925static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
1926 struct i2c_msg msgs[], int num)
1927{
1928 struct intel_output *intel_output;
1929 struct intel_sdvo_priv *sdvo_priv;
1930 struct i2c_algo_bit_data *algo_data;
Keith Packardf9c10a92009-05-30 12:16:25 -07001931 const struct i2c_algorithm *algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001932
1933 algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
1934 intel_output =
1935 intel_sdvo_chan_to_intel_output(
1936 (struct intel_i2c_chan *)(algo_data->data));
1937 if (intel_output == NULL)
1938 return -EINVAL;
1939
1940 sdvo_priv = intel_output->dev_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07001941 algo = intel_output->i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08001942
1943 intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
1944 return algo->master_xfer(i2c_adap, msgs, num);
1945}
1946
1947static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
1948 .master_xfer = intel_sdvo_master_xfer,
1949};
1950
yakui_zhao714605e2009-05-31 17:18:07 +08001951static u8
1952intel_sdvo_get_slave_addr(struct drm_device *dev, int output_device)
1953{
1954 struct drm_i915_private *dev_priv = dev->dev_private;
1955 struct sdvo_device_mapping *my_mapping, *other_mapping;
1956
1957 if (output_device == SDVOB) {
1958 my_mapping = &dev_priv->sdvo_mappings[0];
1959 other_mapping = &dev_priv->sdvo_mappings[1];
1960 } else {
1961 my_mapping = &dev_priv->sdvo_mappings[1];
1962 other_mapping = &dev_priv->sdvo_mappings[0];
1963 }
1964
1965 /* If the BIOS described our SDVO device, take advantage of it. */
1966 if (my_mapping->slave_addr)
1967 return my_mapping->slave_addr;
1968
1969 /* If the BIOS only described a different SDVO device, use the
1970 * address that it isn't using.
1971 */
1972 if (other_mapping->slave_addr) {
1973 if (other_mapping->slave_addr == 0x70)
1974 return 0x72;
1975 else
1976 return 0x70;
1977 }
1978
1979 /* No SDVO device info is found for another DVO port,
1980 * so use mapping assumption we had before BIOS parsing.
1981 */
1982 if (output_device == SDVOB)
1983 return 0x70;
1984 else
1985 return 0x72;
1986}
1987
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001988static bool
1989intel_sdvo_output_setup(struct intel_output *intel_output, uint16_t flags)
1990{
1991 struct drm_connector *connector = &intel_output->base;
1992 struct drm_encoder *encoder = &intel_output->enc;
1993 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1994 bool ret = true, registered = false;
1995
1996 sdvo_priv->is_tv = false;
1997 intel_output->needs_tv_clock = false;
1998 sdvo_priv->is_lvds = false;
1999
2000 if (device_is_registered(&connector->kdev)) {
2001 drm_sysfs_connector_remove(connector);
2002 registered = true;
2003 }
2004
2005 if (flags &
2006 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
2007 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
2008 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
2009 else
2010 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
2011
2012 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2013 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2014
2015 if (intel_sdvo_get_supp_encode(intel_output,
2016 &sdvo_priv->encode) &&
2017 intel_sdvo_get_digital_encoding_mode(intel_output) &&
2018 sdvo_priv->is_hdmi) {
2019 /* enable hdmi encoding mode if supported */
2020 intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
2021 intel_sdvo_set_colorimetry(intel_output,
2022 SDVO_COLORIMETRY_RGB256);
2023 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
Ma Lingf8aed702009-08-24 13:50:24 +08002024 intel_output->clone_mask =
2025 (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2026 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002027 }
2028 } else if (flags & SDVO_OUTPUT_SVID0) {
2029
2030 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
2031 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2032 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2033 sdvo_priv->is_tv = true;
2034 intel_output->needs_tv_clock = true;
Ma Lingf8aed702009-08-24 13:50:24 +08002035 intel_output->clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002036 } else if (flags & SDVO_OUTPUT_RGB0) {
2037
2038 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
2039 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2040 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Ma Lingf8aed702009-08-24 13:50:24 +08002041 intel_output->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2042 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002043 } else if (flags & SDVO_OUTPUT_RGB1) {
2044
2045 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
2046 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2047 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2048 } else if (flags & SDVO_OUTPUT_LVDS0) {
2049
2050 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
2051 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2052 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2053 sdvo_priv->is_lvds = true;
Ma Lingf8aed702009-08-24 13:50:24 +08002054 intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
2055 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002056 } else if (flags & SDVO_OUTPUT_LVDS1) {
2057
2058 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
2059 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2060 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2061 sdvo_priv->is_lvds = true;
Ma Lingf8aed702009-08-24 13:50:24 +08002062 intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
2063 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002064 } else {
2065
2066 unsigned char bytes[2];
2067
2068 sdvo_priv->controlled_output = 0;
2069 memcpy(bytes, &sdvo_priv->caps.output_flags, 2);
2070 DRM_DEBUG_KMS(I915_SDVO,
2071 "%s: Unknown SDVO output type (0x%02x%02x)\n",
2072 SDVO_NAME(sdvo_priv),
2073 bytes[0], bytes[1]);
2074 ret = false;
2075 }
Ma Lingf8aed702009-08-24 13:50:24 +08002076 intel_output->crtc_mask = (1 << 0) | (1 << 1);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002077
2078 if (ret && registered)
2079 ret = drm_sysfs_connector_add(connector) == 0 ? true : false;
2080
2081
2082 return ret;
2083
2084}
2085
Zhao Yakuice6feab2009-08-24 13:50:26 +08002086static void intel_sdvo_tv_create_property(struct drm_connector *connector)
2087{
2088 struct intel_output *intel_output = to_intel_output(connector);
2089 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
2090 struct intel_sdvo_tv_format format;
2091 uint32_t format_map, i;
2092 uint8_t status;
2093
2094 intel_sdvo_set_target_output(intel_output,
2095 sdvo_priv->controlled_output);
2096
2097 intel_sdvo_write_cmd(intel_output,
2098 SDVO_CMD_GET_SUPPORTED_TV_FORMATS, NULL, 0);
2099 status = intel_sdvo_read_response(intel_output,
2100 &format, sizeof(format));
2101 if (status != SDVO_CMD_STATUS_SUCCESS)
2102 return;
2103
2104 memcpy(&format_map, &format, sizeof(format) > sizeof(format_map) ?
2105 sizeof(format_map) : sizeof(format));
2106
2107 if (format_map == 0)
2108 return;
2109
2110 sdvo_priv->format_supported_num = 0;
2111 for (i = 0 ; i < TV_FORMAT_NUM; i++)
2112 if (format_map & (1 << i)) {
2113 sdvo_priv->tv_format_supported
2114 [sdvo_priv->format_supported_num++] =
2115 tv_format_names[i];
2116 }
2117
2118
2119 sdvo_priv->tv_format_property =
2120 drm_property_create(
2121 connector->dev, DRM_MODE_PROP_ENUM,
2122 "mode", sdvo_priv->format_supported_num);
2123
2124 for (i = 0; i < sdvo_priv->format_supported_num; i++)
2125 drm_property_add_enum(
2126 sdvo_priv->tv_format_property, i,
2127 i, sdvo_priv->tv_format_supported[i]);
2128
2129 sdvo_priv->tv_format_name = sdvo_priv->tv_format_supported[0];
2130 drm_connector_attach_property(
2131 connector, sdvo_priv->tv_format_property, 0);
2132
2133}
2134
Eric Anholt7d573822009-01-02 13:33:00 -08002135bool intel_sdvo_init(struct drm_device *dev, int output_device)
Jesse Barnes79e53942008-11-07 14:24:08 -08002136{
2137 struct drm_connector *connector;
2138 struct intel_output *intel_output;
2139 struct intel_sdvo_priv *sdvo_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07002140
Jesse Barnes79e53942008-11-07 14:24:08 -08002141 u8 ch[0x40];
2142 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08002143
2144 intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
2145 if (!intel_output) {
Eric Anholt7d573822009-01-02 13:33:00 -08002146 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002147 }
2148
Jesse Barnes79e53942008-11-07 14:24:08 -08002149 sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
Keith Packard308cd3a2009-06-14 11:56:18 -07002150 sdvo_priv->output_device = output_device;
2151
2152 intel_output->dev_priv = sdvo_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08002153 intel_output->type = INTEL_OUTPUT_SDVO;
2154
Jesse Barnes79e53942008-11-07 14:24:08 -08002155 /* setup the DDC bus. */
Keith Packard308cd3a2009-06-14 11:56:18 -07002156 if (output_device == SDVOB)
2157 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
2158 else
2159 intel_output->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
2160
2161 if (!intel_output->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002162 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002163
Keith Packard308cd3a2009-06-14 11:56:18 -07002164 sdvo_priv->slave_addr = intel_sdvo_get_slave_addr(dev, output_device);
Jesse Barnes79e53942008-11-07 14:24:08 -08002165
Keith Packard308cd3a2009-06-14 11:56:18 -07002166 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
2167 intel_sdvo_i2c_bit_algo.functionality = intel_output->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002168
Jesse Barnes79e53942008-11-07 14:24:08 -08002169 /* Read the regs to test if we can talk to the device */
2170 for (i = 0; i < 0x40; i++) {
2171 if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
yakui_zhao342dc382009-06-02 14:12:00 +08002172 DRM_DEBUG_KMS(I915_SDVO,
2173 "No SDVO device found on SDVO%c\n",
2174 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002175 goto err_i2c;
2176 }
2177 }
2178
Ma Ling619ac3b2009-05-18 16:12:46 +08002179 /* setup the DDC bus. */
2180 if (output_device == SDVOB)
Keith Packard308cd3a2009-06-14 11:56:18 -07002181 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOB DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002182 else
Keith Packard308cd3a2009-06-14 11:56:18 -07002183 intel_output->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOC DDC BUS");
Ma Ling619ac3b2009-05-18 16:12:46 +08002184
Keith Packard308cd3a2009-06-14 11:56:18 -07002185 if (intel_output->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002186 goto err_i2c;
2187
Keith Packard308cd3a2009-06-14 11:56:18 -07002188 /* Wrap with our custom algo which switches to DDC mode */
2189 intel_output->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002190
Ma Ling7086c872009-05-13 11:20:06 +08002191 /* In defaut case sdvo lvds is false */
Jesse Barnes79e53942008-11-07 14:24:08 -08002192 intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
2193
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002194 if (intel_sdvo_output_setup(intel_output,
2195 sdvo_priv->caps.output_flags) != true) {
2196 DRM_DEBUG("SDVO output failed to setup on SDVO%c\n",
2197 output_device == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002198 goto err_i2c;
2199 }
2200
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002201
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002202 connector = &intel_output->base;
2203 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002204 connector->connector_type);
2205
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002206 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
2207 connector->interlace_allowed = 0;
2208 connector->doublescan_allowed = 0;
2209 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2210
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002211 drm_encoder_init(dev, &intel_output->enc,
2212 &intel_sdvo_enc_funcs, intel_output->enc.encoder_type);
2213
Jesse Barnes79e53942008-11-07 14:24:08 -08002214 drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08002215
2216 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002217 if (sdvo_priv->is_tv)
2218 intel_sdvo_tv_create_property(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08002219 drm_sysfs_connector_add(connector);
2220
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002221 intel_sdvo_select_ddc_bus(sdvo_priv);
2222
Jesse Barnes79e53942008-11-07 14:24:08 -08002223 /* Set the input timing to the screen. Assume always input 0. */
2224 intel_sdvo_set_target_input(intel_output, true, false);
2225
2226 intel_sdvo_get_input_pixel_clock_range(intel_output,
2227 &sdvo_priv->pixel_clock_min,
2228 &sdvo_priv->pixel_clock_max);
2229
2230
yakui_zhao342dc382009-06-02 14:12:00 +08002231 DRM_DEBUG_KMS(I915_SDVO, "%s device VID/DID: %02X:%02X.%02X, "
2232 "clock range %dMHz - %dMHz, "
2233 "input 1: %c, input 2: %c, "
2234 "output 1: %c, output 2: %c\n",
2235 SDVO_NAME(sdvo_priv),
2236 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
2237 sdvo_priv->caps.device_rev_id,
2238 sdvo_priv->pixel_clock_min / 1000,
2239 sdvo_priv->pixel_clock_max / 1000,
2240 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2241 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2242 /* check currently supported outputs */
2243 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002244 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002245 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002246 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2247
Eric Anholt7d573822009-01-02 13:33:00 -08002248 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002249
2250err_i2c:
Keith Packard308cd3a2009-06-14 11:56:18 -07002251 if (intel_output->ddc_bus != NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002252 intel_i2c_destroy(intel_output->ddc_bus);
Keith Packard308cd3a2009-06-14 11:56:18 -07002253 if (intel_output->i2c_bus != NULL)
2254 intel_i2c_destroy(intel_output->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002255err_inteloutput:
Jesse Barnes79e53942008-11-07 14:24:08 -08002256 kfree(intel_output);
2257
Eric Anholt7d573822009-01-02 13:33:00 -08002258 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002259}