blob: ea311c7241c20ac50dde36330396e7a3e0846a00 [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"
34#include "i915_drm.h"
35#include "i915_drv.h"
36#include "intel_sdvo_regs.h"
37
38#undef SDVO_DEBUG
39
40struct intel_sdvo_priv {
41 struct intel_i2c_chan *i2c_bus;
42 int slaveaddr;
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
Jesse Barnese2f0ba92009-02-02 15:11:52 -080059 /**
60 * This is set if we're going to treat the device as TV-out.
61 *
62 * While we have these nice friendly flags for output types that ought
63 * to decide this for us, the S-Video output on our HDMI+S-Video card
64 * shows up as RGB1 (VGA).
65 */
66 bool is_tv;
67
68 /**
69 * This is set if we treat the device as HDMI, instead of DVI.
70 */
71 bool is_hdmi;
72
73 /**
74 * Returned SDTV resolutions allowed for the current format, if the
75 * device reported it.
76 */
77 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
78
79 /**
80 * Current selected TV format.
81 *
82 * This is stored in the same structure that's passed to the device, for
83 * convenience.
84 */
85 struct intel_sdvo_tv_format tv_format;
86
87 /*
88 * supported encoding mode, used to determine whether HDMI is
89 * supported
90 */
91 struct intel_sdvo_encode encode;
92
93 /* DDC bus used by this SDVO output */
94 uint8_t ddc_bus;
95
Jesse Barnes79e53942008-11-07 14:24:08 -080096 int save_sdvo_mult;
97 u16 save_active_outputs;
98 struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
99 struct intel_sdvo_dtd save_output_dtd[16];
100 u32 save_SDVOX;
101};
102
103/**
104 * Writes the SDVOB or SDVOC with the given value, but always writes both
105 * SDVOB and SDVOC to work around apparent hardware issues (according to
106 * comments in the BIOS).
107 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100108static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800109{
110 struct drm_device *dev = intel_output->base.dev;
111 struct drm_i915_private *dev_priv = dev->dev_private;
112 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
113 u32 bval = val, cval = val;
114 int i;
115
116 if (sdvo_priv->output_device == SDVOB) {
117 cval = I915_READ(SDVOC);
118 } else {
119 bval = I915_READ(SDVOB);
120 }
121 /*
122 * Write the registers twice for luck. Sometimes,
123 * writing them only once doesn't appear to 'stick'.
124 * The BIOS does this too. Yay, magic
125 */
126 for (i = 0; i < 2; i++)
127 {
128 I915_WRITE(SDVOB, bval);
129 I915_READ(SDVOB);
130 I915_WRITE(SDVOC, cval);
131 I915_READ(SDVOC);
132 }
133}
134
135static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
136 u8 *ch)
137{
138 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
139 u8 out_buf[2];
140 u8 buf[2];
141 int ret;
142
143 struct i2c_msg msgs[] = {
144 {
145 .addr = sdvo_priv->i2c_bus->slave_addr,
146 .flags = 0,
147 .len = 1,
148 .buf = out_buf,
149 },
150 {
151 .addr = sdvo_priv->i2c_bus->slave_addr,
152 .flags = I2C_M_RD,
153 .len = 1,
154 .buf = buf,
155 }
156 };
157
158 out_buf[0] = addr;
159 out_buf[1] = 0;
160
161 if ((ret = i2c_transfer(&sdvo_priv->i2c_bus->adapter, msgs, 2)) == 2)
162 {
163 *ch = buf[0];
164 return true;
165 }
166
167 DRM_DEBUG("i2c transfer returned %d\n", ret);
168 return false;
169}
170
171static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
172 u8 ch)
173{
174 u8 out_buf[2];
175 struct i2c_msg msgs[] = {
176 {
177 .addr = intel_output->i2c_bus->slave_addr,
178 .flags = 0,
179 .len = 2,
180 .buf = out_buf,
181 }
182 };
183
184 out_buf[0] = addr;
185 out_buf[1] = ch;
186
187 if (i2c_transfer(&intel_output->i2c_bus->adapter, msgs, 1) == 1)
188 {
189 return true;
190 }
191 return false;
192}
193
194#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
195/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100196static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800197 u8 cmd;
198 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800199} sdvo_cmd_names[] = {
200 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
201 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
202 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
203 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
204 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
205 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
206 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
207 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
208 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
209 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
210 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
211 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
212 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
213 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
214 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
215 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
216 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
217 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
218 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
219 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
220 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
221 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
222 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
223 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
224 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
225 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
226 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
227 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
228 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
229 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
230 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
231 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
232 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
233 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
234 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800235 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
236 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
237 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
238 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800239 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800240 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
241 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
242 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
243 /* HDMI op code */
244 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
245 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
246 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
247 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
248 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
249 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
250 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
251 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
252 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
253 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
254 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
255 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
256 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
257 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
258 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
259 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
260 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
261 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
262 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
263 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800264};
265
266#define SDVO_NAME(dev_priv) ((dev_priv)->output_device == SDVOB ? "SDVOB" : "SDVOC")
267#define SDVO_PRIV(output) ((struct intel_sdvo_priv *) (output)->dev_priv)
268
269#ifdef SDVO_DEBUG
270static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
271 void *args, int args_len)
272{
273 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
274 int i;
275
Zhenyu Wang33b52962009-03-24 14:02:40 +0800276 printk(KERN_DEBUG "%s: W: %02X ", SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800277 for (i = 0; i < args_len; i++)
Zhenyu Wang33b52962009-03-24 14:02:40 +0800278 printk(KERN_DEBUG "%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800279 for (; i < 8; i++)
Zhenyu Wang33b52962009-03-24 14:02:40 +0800280 printk(KERN_DEBUG " ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800281 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
282 if (cmd == sdvo_cmd_names[i].cmd) {
Zhenyu Wang33b52962009-03-24 14:02:40 +0800283 printk(KERN_DEBUG "(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800284 break;
285 }
286 }
287 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
Zhenyu Wang33b52962009-03-24 14:02:40 +0800288 printk(KERN_DEBUG "(%02X)", cmd);
289 printk(KERN_DEBUG "\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800290}
291#else
292#define intel_sdvo_debug_write(o, c, a, l)
293#endif
294
295static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
296 void *args, int args_len)
297{
298 int i;
299
300 intel_sdvo_debug_write(intel_output, cmd, args, args_len);
301
302 for (i = 0; i < args_len; i++) {
303 intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
304 ((u8*)args)[i]);
305 }
306
307 intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
308}
309
310#ifdef SDVO_DEBUG
311static const char *cmd_status_names[] = {
312 "Power on",
313 "Success",
314 "Not supported",
315 "Invalid arg",
316 "Pending",
317 "Target not specified",
318 "Scaling not supported"
319};
320
321static void intel_sdvo_debug_response(struct intel_output *intel_output,
322 void *response, int response_len,
323 u8 status)
324{
325 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800326 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800327
Zhenyu Wang33b52962009-03-24 14:02:40 +0800328 printk(KERN_DEBUG "%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800329 for (i = 0; i < response_len; i++)
Zhenyu Wang33b52962009-03-24 14:02:40 +0800330 printk(KERN_DEBUG "%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800331 for (; i < 8; i++)
Zhenyu Wang33b52962009-03-24 14:02:40 +0800332 printk(KERN_DEBUG " ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800333 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
Zhenyu Wang33b52962009-03-24 14:02:40 +0800334 printk(KERN_DEBUG "(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800335 else
Zhenyu Wang33b52962009-03-24 14:02:40 +0800336 printk(KERN_DEBUG "(??? %d)", status);
337 printk(KERN_DEBUG "\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800338}
339#else
340#define intel_sdvo_debug_response(o, r, l, s)
341#endif
342
343static u8 intel_sdvo_read_response(struct intel_output *intel_output,
344 void *response, int response_len)
345{
346 int i;
347 u8 status;
348 u8 retry = 50;
349
350 while (retry--) {
351 /* Read the command response */
352 for (i = 0; i < response_len; i++) {
353 intel_sdvo_read_byte(intel_output,
354 SDVO_I2C_RETURN_0 + i,
355 &((u8 *)response)[i]);
356 }
357
358 /* read the return status */
359 intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
360 &status);
361
362 intel_sdvo_debug_response(intel_output, response, response_len,
363 status);
364 if (status != SDVO_CMD_STATUS_PENDING)
365 return status;
366
367 mdelay(50);
368 }
369
370 return status;
371}
372
Hannes Ederb358d0a2008-12-18 21:18:47 +0100373static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800374{
375 if (mode->clock >= 100000)
376 return 1;
377 else if (mode->clock >= 50000)
378 return 2;
379 else
380 return 4;
381}
382
383/**
384 * Don't check status code from this as it switches the bus back to the
385 * SDVO chips which defeats the purpose of doing a bus switch in the first
386 * place.
387 */
Hannes Ederb358d0a2008-12-18 21:18:47 +0100388static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
389 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800390{
391 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
392}
393
394static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
395{
396 struct intel_sdvo_set_target_input_args targets = {0};
397 u8 status;
398
399 if (target_0 && target_1)
400 return SDVO_CMD_STATUS_NOTSUPP;
401
402 if (target_1)
403 targets.target_1 = 1;
404
405 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
406 sizeof(targets));
407
408 status = intel_sdvo_read_response(intel_output, NULL, 0);
409
410 return (status == SDVO_CMD_STATUS_SUCCESS);
411}
412
413/**
414 * Return whether each input is trained.
415 *
416 * This function is making an assumption about the layout of the response,
417 * which should be checked against the docs.
418 */
419static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
420{
421 struct intel_sdvo_get_trained_inputs_response response;
422 u8 status;
423
424 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
425 status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
426 if (status != SDVO_CMD_STATUS_SUCCESS)
427 return false;
428
429 *input_1 = response.input0_trained;
430 *input_2 = response.input1_trained;
431 return true;
432}
433
434static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
435 u16 *outputs)
436{
437 u8 status;
438
439 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
440 status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
441
442 return (status == SDVO_CMD_STATUS_SUCCESS);
443}
444
445static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
446 u16 outputs)
447{
448 u8 status;
449
450 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
451 sizeof(outputs));
452 status = intel_sdvo_read_response(intel_output, NULL, 0);
453 return (status == SDVO_CMD_STATUS_SUCCESS);
454}
455
456static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
457 int mode)
458{
459 u8 status, state = SDVO_ENCODER_STATE_ON;
460
461 switch (mode) {
462 case DRM_MODE_DPMS_ON:
463 state = SDVO_ENCODER_STATE_ON;
464 break;
465 case DRM_MODE_DPMS_STANDBY:
466 state = SDVO_ENCODER_STATE_STANDBY;
467 break;
468 case DRM_MODE_DPMS_SUSPEND:
469 state = SDVO_ENCODER_STATE_SUSPEND;
470 break;
471 case DRM_MODE_DPMS_OFF:
472 state = SDVO_ENCODER_STATE_OFF;
473 break;
474 }
475
476 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
477 sizeof(state));
478 status = intel_sdvo_read_response(intel_output, NULL, 0);
479
480 return (status == SDVO_CMD_STATUS_SUCCESS);
481}
482
483static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
484 int *clock_min,
485 int *clock_max)
486{
487 struct intel_sdvo_pixel_clock_range clocks;
488 u8 status;
489
490 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
491 NULL, 0);
492
493 status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
494
495 if (status != SDVO_CMD_STATUS_SUCCESS)
496 return false;
497
498 /* Convert the values from units of 10 kHz to kHz. */
499 *clock_min = clocks.min * 10;
500 *clock_max = clocks.max * 10;
501
502 return true;
503}
504
505static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
506 u16 outputs)
507{
508 u8 status;
509
510 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
511 sizeof(outputs));
512
513 status = intel_sdvo_read_response(intel_output, NULL, 0);
514 return (status == SDVO_CMD_STATUS_SUCCESS);
515}
516
517static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
518 struct intel_sdvo_dtd *dtd)
519{
520 u8 status;
521
522 intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
523 status = intel_sdvo_read_response(intel_output, &dtd->part1,
524 sizeof(dtd->part1));
525 if (status != SDVO_CMD_STATUS_SUCCESS)
526 return false;
527
528 intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
529 status = intel_sdvo_read_response(intel_output, &dtd->part2,
530 sizeof(dtd->part2));
531 if (status != SDVO_CMD_STATUS_SUCCESS)
532 return false;
533
534 return true;
535}
536
537static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
538 struct intel_sdvo_dtd *dtd)
539{
540 return intel_sdvo_get_timing(intel_output,
541 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
542}
543
544static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
545 struct intel_sdvo_dtd *dtd)
546{
547 return intel_sdvo_get_timing(intel_output,
548 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
549}
550
551static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
552 struct intel_sdvo_dtd *dtd)
553{
554 u8 status;
555
556 intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
557 status = intel_sdvo_read_response(intel_output, NULL, 0);
558 if (status != SDVO_CMD_STATUS_SUCCESS)
559 return false;
560
561 intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
562 status = intel_sdvo_read_response(intel_output, NULL, 0);
563 if (status != SDVO_CMD_STATUS_SUCCESS)
564 return false;
565
566 return true;
567}
568
569static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
570 struct intel_sdvo_dtd *dtd)
571{
572 return intel_sdvo_set_timing(intel_output,
573 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
574}
575
576static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
577 struct intel_sdvo_dtd *dtd)
578{
579 return intel_sdvo_set_timing(intel_output,
580 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
581}
582
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800583static bool
584intel_sdvo_create_preferred_input_timing(struct intel_output *output,
585 uint16_t clock,
586 uint16_t width,
587 uint16_t height)
588{
589 struct intel_sdvo_preferred_input_timing_args args;
590 uint8_t status;
591
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800592 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800593 args.clock = clock;
594 args.width = width;
595 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800596 args.interlace = 0;
597 args.scaled = 0;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800598 intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
599 &args, sizeof(args));
600 status = intel_sdvo_read_response(output, NULL, 0);
601 if (status != SDVO_CMD_STATUS_SUCCESS)
602 return false;
603
604 return true;
605}
606
607static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
608 struct intel_sdvo_dtd *dtd)
609{
610 bool status;
611
612 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
613 NULL, 0);
614
615 status = intel_sdvo_read_response(output, &dtd->part1,
616 sizeof(dtd->part1));
617 if (status != SDVO_CMD_STATUS_SUCCESS)
618 return false;
619
620 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
621 NULL, 0);
622
623 status = intel_sdvo_read_response(output, &dtd->part2,
624 sizeof(dtd->part2));
625 if (status != SDVO_CMD_STATUS_SUCCESS)
626 return false;
627
628 return false;
629}
Jesse Barnes79e53942008-11-07 14:24:08 -0800630
631static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
632{
633 u8 response, status;
634
635 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
636 status = intel_sdvo_read_response(intel_output, &response, 1);
637
638 if (status != SDVO_CMD_STATUS_SUCCESS) {
639 DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
640 return SDVO_CLOCK_RATE_MULT_1X;
641 } else {
642 DRM_DEBUG("Current clock rate multiplier: %d\n", response);
643 }
644
645 return response;
646}
647
648static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
649{
650 u8 status;
651
652 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
653 status = intel_sdvo_read_response(intel_output, NULL, 0);
654 if (status != SDVO_CMD_STATUS_SUCCESS)
655 return false;
656
657 return true;
658}
659
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800660static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
661 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800662{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800663 uint16_t width, height;
664 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
665 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800666
667 width = mode->crtc_hdisplay;
668 height = mode->crtc_vdisplay;
669
670 /* do some mode translations */
671 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
672 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
673
674 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
675 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
676
677 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
678 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
679
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800680 dtd->part1.clock = mode->clock / 10;
681 dtd->part1.h_active = width & 0xff;
682 dtd->part1.h_blank = h_blank_len & 0xff;
683 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800685 dtd->part1.v_active = height & 0xff;
686 dtd->part1.v_blank = v_blank_len & 0xff;
687 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 ((v_blank_len >> 8) & 0xf);
689
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800690 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800691 dtd->part2.h_sync_width = h_sync_len & 0xff;
692 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800693 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800694 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
696 ((v_sync_len & 0x30) >> 4);
697
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800698 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800699 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800700 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800702 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800703
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800704 dtd->part2.sdvo_flags = 0;
705 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
706 dtd->part2.reserved = 0;
707}
Jesse Barnes79e53942008-11-07 14:24:08 -0800708
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800709static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
710 struct intel_sdvo_dtd *dtd)
711{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800712 mode->hdisplay = dtd->part1.h_active;
713 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
714 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800715 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800716 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
717 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
718 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
719 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
720
721 mode->vdisplay = dtd->part1.v_active;
722 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
723 mode->vsync_start = mode->vdisplay;
724 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800725 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800726 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
727 mode->vsync_end = mode->vsync_start +
728 (dtd->part2.v_sync_off_width & 0xf);
729 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
730 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
731 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
732
733 mode->clock = dtd->part1.clock * 10;
734
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800735 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800736 if (dtd->part2.dtd_flags & 0x2)
737 mode->flags |= DRM_MODE_FLAG_PHSYNC;
738 if (dtd->part2.dtd_flags & 0x4)
739 mode->flags |= DRM_MODE_FLAG_PVSYNC;
740}
741
742static bool intel_sdvo_get_supp_encode(struct intel_output *output,
743 struct intel_sdvo_encode *encode)
744{
745 uint8_t status;
746
747 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
748 status = intel_sdvo_read_response(output, encode, sizeof(*encode));
749 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
750 memset(encode, 0, sizeof(*encode));
751 return false;
752 }
753
754 return true;
755}
756
757static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
758{
759 uint8_t status;
760
761 intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
762 status = intel_sdvo_read_response(output, NULL, 0);
763
764 return (status == SDVO_CMD_STATUS_SUCCESS);
765}
766
767static bool intel_sdvo_set_colorimetry(struct intel_output *output,
768 uint8_t mode)
769{
770 uint8_t status;
771
772 intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
773 status = intel_sdvo_read_response(output, NULL, 0);
774
775 return (status == SDVO_CMD_STATUS_SUCCESS);
776}
777
778#if 0
779static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
780{
781 int i, j;
782 uint8_t set_buf_index[2];
783 uint8_t av_split;
784 uint8_t buf_size;
785 uint8_t buf[48];
786 uint8_t *pos;
787
788 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
789 intel_sdvo_read_response(output, &av_split, 1);
790
791 for (i = 0; i <= av_split; i++) {
792 set_buf_index[0] = i; set_buf_index[1] = 0;
793 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
794 set_buf_index, 2);
795 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
796 intel_sdvo_read_response(output, &buf_size, 1);
797
798 pos = buf;
799 for (j = 0; j <= buf_size; j += 8) {
800 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
801 NULL, 0);
802 intel_sdvo_read_response(output, pos, 8);
803 pos += 8;
804 }
805 }
806}
807#endif
808
809static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
810 uint8_t *data, int8_t size, uint8_t tx_rate)
811{
812 uint8_t set_buf_index[2];
813
814 set_buf_index[0] = index;
815 set_buf_index[1] = 0;
816
817 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
818
819 for (; size > 0; size -= 8) {
820 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
821 data += 8;
822 }
823
824 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
825}
826
827static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
828{
829 uint8_t csum = 0;
830 int i;
831
832 for (i = 0; i < size; i++)
833 csum += data[i];
834
835 return 0x100 - csum;
836}
837
838#define DIP_TYPE_AVI 0x82
839#define DIP_VERSION_AVI 0x2
840#define DIP_LEN_AVI 13
841
842struct dip_infoframe {
843 uint8_t type;
844 uint8_t version;
845 uint8_t len;
846 uint8_t checksum;
847 union {
848 struct {
849 /* Packet Byte #1 */
850 uint8_t S:2;
851 uint8_t B:2;
852 uint8_t A:1;
853 uint8_t Y:2;
854 uint8_t rsvd1:1;
855 /* Packet Byte #2 */
856 uint8_t R:4;
857 uint8_t M:2;
858 uint8_t C:2;
859 /* Packet Byte #3 */
860 uint8_t SC:2;
861 uint8_t Q:2;
862 uint8_t EC:3;
863 uint8_t ITC:1;
864 /* Packet Byte #4 */
865 uint8_t VIC:7;
866 uint8_t rsvd2:1;
867 /* Packet Byte #5 */
868 uint8_t PR:4;
869 uint8_t rsvd3:4;
870 /* Packet Byte #6~13 */
871 uint16_t top_bar_end;
872 uint16_t bottom_bar_start;
873 uint16_t left_bar_end;
874 uint16_t right_bar_start;
875 } avi;
876 struct {
877 /* Packet Byte #1 */
878 uint8_t channel_count:3;
879 uint8_t rsvd1:1;
880 uint8_t coding_type:4;
881 /* Packet Byte #2 */
882 uint8_t sample_size:2; /* SS0, SS1 */
883 uint8_t sample_frequency:3;
884 uint8_t rsvd2:3;
885 /* Packet Byte #3 */
886 uint8_t coding_type_private:5;
887 uint8_t rsvd3:3;
888 /* Packet Byte #4 */
889 uint8_t channel_allocation;
890 /* Packet Byte #5 */
891 uint8_t rsvd4:3;
892 uint8_t level_shift:4;
893 uint8_t downmix_inhibit:1;
894 } audio;
895 uint8_t payload[28];
896 } __attribute__ ((packed)) u;
897} __attribute__((packed));
898
899static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
900 struct drm_display_mode * mode)
901{
902 struct dip_infoframe avi_if = {
903 .type = DIP_TYPE_AVI,
904 .version = DIP_VERSION_AVI,
905 .len = DIP_LEN_AVI,
906 };
907
908 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
909 4 + avi_if.len);
910 intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
911 SDVO_HBUF_TX_VSYNC);
912}
913
914static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
915 struct drm_display_mode *mode,
916 struct drm_display_mode *adjusted_mode)
917{
918 struct intel_output *output = enc_to_intel_output(encoder);
919 struct intel_sdvo_priv *dev_priv = output->dev_priv;
920
921 if (!dev_priv->is_tv) {
922 /* Make the CRTC code factor in the SDVO pixel multiplier. The
923 * SDVO device will be told of the multiplier during mode_set.
924 */
925 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
926 } else {
927 struct intel_sdvo_dtd output_dtd;
928 bool success;
929
930 /* We need to construct preferred input timings based on our
931 * output timings. To do that, we have to set the output
932 * timings, even though this isn't really the right place in
933 * the sequence to do it. Oh well.
934 */
935
936
937 /* Set output timings */
938 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
939 intel_sdvo_set_target_output(output,
940 dev_priv->controlled_output);
941 intel_sdvo_set_output_timing(output, &output_dtd);
942
943 /* Set the input timing to the screen. Assume always input 0. */
944 intel_sdvo_set_target_input(output, true, false);
945
946
947 success = intel_sdvo_create_preferred_input_timing(output,
948 mode->clock / 10,
949 mode->hdisplay,
950 mode->vdisplay);
951 if (success) {
952 struct intel_sdvo_dtd input_dtd;
953
954 intel_sdvo_get_preferred_input_timing(output,
955 &input_dtd);
956 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
957
958 } else {
959 return false;
960 }
961 }
962 return true;
963}
964
965static void intel_sdvo_mode_set(struct drm_encoder *encoder,
966 struct drm_display_mode *mode,
967 struct drm_display_mode *adjusted_mode)
968{
969 struct drm_device *dev = encoder->dev;
970 struct drm_i915_private *dev_priv = dev->dev_private;
971 struct drm_crtc *crtc = encoder->crtc;
972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
973 struct intel_output *output = enc_to_intel_output(encoder);
974 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
975 u32 sdvox = 0;
976 int sdvo_pixel_multiply;
977 struct intel_sdvo_in_out_map in_out;
978 struct intel_sdvo_dtd input_dtd;
979 u8 status;
980
981 if (!mode)
982 return;
983
984 /* First, set the input mapping for the first input to our controlled
985 * output. This is only correct if we're a single-input device, in
986 * which case the first input is the output from the appropriate SDVO
987 * channel on the motherboard. In a two-input device, the first input
988 * will be SDVOB and the second SDVOC.
989 */
990 in_out.in0 = sdvo_priv->controlled_output;
991 in_out.in1 = 0;
992
993 intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
994 &in_out, sizeof(in_out));
995 status = intel_sdvo_read_response(output, NULL, 0);
996
997 if (sdvo_priv->is_hdmi) {
998 intel_sdvo_set_avi_infoframe(output, mode);
999 sdvox |= SDVO_AUDIO_ENABLE;
1000 }
1001
1002 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
1003
1004 /* If it's a TV, we already set the output timing in mode_fixup.
1005 * Otherwise, the output timing is equal to the input timing.
1006 */
1007 if (!sdvo_priv->is_tv) {
1008 /* Set the output timing to the screen */
1009 intel_sdvo_set_target_output(output,
1010 sdvo_priv->controlled_output);
1011 intel_sdvo_set_output_timing(output, &input_dtd);
1012 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001013
1014 /* Set the input timing to the screen. Assume always input 0. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001015 intel_sdvo_set_target_input(output, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001016
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001017 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001018 * provide the device with a timing it can support, if it supports that
1019 * feature. However, presumably we would need to adjust the CRTC to
1020 * output the preferred timing, and we don't support that currently.
1021 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001022#if 0
1023 success = intel_sdvo_create_preferred_input_timing(output, clock,
1024 width, height);
1025 if (success) {
1026 struct intel_sdvo_dtd *input_dtd;
1027
1028 intel_sdvo_get_preferred_input_timing(output, &input_dtd);
1029 intel_sdvo_set_input_timing(output, &input_dtd);
1030 }
1031#else
1032 intel_sdvo_set_input_timing(output, &input_dtd);
1033#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001034
1035 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1036 case 1:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001037 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001038 SDVO_CLOCK_RATE_MULT_1X);
1039 break;
1040 case 2:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001041 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001042 SDVO_CLOCK_RATE_MULT_2X);
1043 break;
1044 case 4:
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001045 intel_sdvo_set_clock_rate_mult(output,
Jesse Barnes79e53942008-11-07 14:24:08 -08001046 SDVO_CLOCK_RATE_MULT_4X);
1047 break;
1048 }
1049
1050 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001051 if (IS_I965G(dev)) {
1052 sdvox |= SDVO_BORDER_ENABLE |
1053 SDVO_VSYNC_ACTIVE_HIGH |
1054 SDVO_HSYNC_ACTIVE_HIGH;
1055 } else {
1056 sdvox |= I915_READ(sdvo_priv->output_device);
1057 switch (sdvo_priv->output_device) {
1058 case SDVOB:
1059 sdvox &= SDVOB_PRESERVE_MASK;
1060 break;
1061 case SDVOC:
1062 sdvox &= SDVOC_PRESERVE_MASK;
1063 break;
1064 }
1065 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1066 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001067 if (intel_crtc->pipe == 1)
1068 sdvox |= SDVO_PIPE_B_SELECT;
1069
1070 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1071 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001072 /* done in crtc_mode_set as the dpll_md reg must be written early */
1073 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1074 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001075 } else {
1076 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1077 }
1078
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001079 intel_sdvo_write_sdvox(output, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001080}
1081
1082static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1083{
1084 struct drm_device *dev = encoder->dev;
1085 struct drm_i915_private *dev_priv = dev->dev_private;
1086 struct intel_output *intel_output = enc_to_intel_output(encoder);
1087 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1088 u32 temp;
1089
1090 if (mode != DRM_MODE_DPMS_ON) {
1091 intel_sdvo_set_active_outputs(intel_output, 0);
1092 if (0)
1093 intel_sdvo_set_encoder_power_state(intel_output, mode);
1094
1095 if (mode == DRM_MODE_DPMS_OFF) {
1096 temp = I915_READ(sdvo_priv->output_device);
1097 if ((temp & SDVO_ENABLE) != 0) {
1098 intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
1099 }
1100 }
1101 } else {
1102 bool input1, input2;
1103 int i;
1104 u8 status;
1105
1106 temp = I915_READ(sdvo_priv->output_device);
1107 if ((temp & SDVO_ENABLE) == 0)
1108 intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
1109 for (i = 0; i < 2; i++)
1110 intel_wait_for_vblank(dev);
1111
1112 status = intel_sdvo_get_trained_inputs(intel_output, &input1,
1113 &input2);
1114
1115
1116 /* Warn if the device reported failure to sync.
1117 * A lot of SDVO devices fail to notify of sync, but it's
1118 * a given it the status is a success, we succeeded.
1119 */
1120 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1121 DRM_DEBUG("First %s output reported failure to sync\n",
1122 SDVO_NAME(sdvo_priv));
1123 }
1124
1125 if (0)
1126 intel_sdvo_set_encoder_power_state(intel_output, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001127 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001128 }
1129 return;
1130}
1131
1132static void intel_sdvo_save(struct drm_connector *connector)
1133{
1134 struct drm_device *dev = connector->dev;
1135 struct drm_i915_private *dev_priv = dev->dev_private;
1136 struct intel_output *intel_output = to_intel_output(connector);
1137 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1138 int o;
1139
1140 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
1141 intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
1142
1143 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1144 intel_sdvo_set_target_input(intel_output, true, false);
1145 intel_sdvo_get_input_timing(intel_output,
1146 &sdvo_priv->save_input_dtd_1);
1147 }
1148
1149 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1150 intel_sdvo_set_target_input(intel_output, false, true);
1151 intel_sdvo_get_input_timing(intel_output,
1152 &sdvo_priv->save_input_dtd_2);
1153 }
1154
1155 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1156 {
1157 u16 this_output = (1 << o);
1158 if (sdvo_priv->caps.output_flags & this_output)
1159 {
1160 intel_sdvo_set_target_output(intel_output, this_output);
1161 intel_sdvo_get_output_timing(intel_output,
1162 &sdvo_priv->save_output_dtd[o]);
1163 }
1164 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001165 if (sdvo_priv->is_tv) {
1166 /* XXX: Save TV format/enhancements. */
1167 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001168
1169 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
1170}
1171
1172static void intel_sdvo_restore(struct drm_connector *connector)
1173{
1174 struct drm_device *dev = connector->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08001175 struct intel_output *intel_output = to_intel_output(connector);
1176 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1177 int o;
1178 int i;
1179 bool input1, input2;
1180 u8 status;
1181
1182 intel_sdvo_set_active_outputs(intel_output, 0);
1183
1184 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1185 {
1186 u16 this_output = (1 << o);
1187 if (sdvo_priv->caps.output_flags & this_output) {
1188 intel_sdvo_set_target_output(intel_output, this_output);
1189 intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
1190 }
1191 }
1192
1193 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1194 intel_sdvo_set_target_input(intel_output, true, false);
1195 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
1196 }
1197
1198 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1199 intel_sdvo_set_target_input(intel_output, false, true);
1200 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
1201 }
1202
1203 intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
1204
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001205 if (sdvo_priv->is_tv) {
1206 /* XXX: Restore TV format/enhancements. */
1207 }
1208
1209 intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
Jesse Barnes79e53942008-11-07 14:24:08 -08001210
1211 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1212 {
1213 for (i = 0; i < 2; i++)
1214 intel_wait_for_vblank(dev);
1215 status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
1216 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
1217 DRM_DEBUG("First %s output reported failure to sync\n",
1218 SDVO_NAME(sdvo_priv));
1219 }
1220
1221 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
1222}
1223
1224static int intel_sdvo_mode_valid(struct drm_connector *connector,
1225 struct drm_display_mode *mode)
1226{
1227 struct intel_output *intel_output = to_intel_output(connector);
1228 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1229
1230 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1231 return MODE_NO_DBLESCAN;
1232
1233 if (sdvo_priv->pixel_clock_min > mode->clock)
1234 return MODE_CLOCK_LOW;
1235
1236 if (sdvo_priv->pixel_clock_max < mode->clock)
1237 return MODE_CLOCK_HIGH;
1238
1239 return MODE_OK;
1240}
1241
1242static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
1243{
1244 u8 status;
1245
1246 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1247 status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
1248 if (status != SDVO_CMD_STATUS_SUCCESS)
1249 return false;
1250
1251 return true;
1252}
1253
1254struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1255{
1256 struct drm_connector *connector = NULL;
1257 struct intel_output *iout = NULL;
1258 struct intel_sdvo_priv *sdvo;
1259
1260 /* find the sdvo connector */
1261 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1262 iout = to_intel_output(connector);
1263
1264 if (iout->type != INTEL_OUTPUT_SDVO)
1265 continue;
1266
1267 sdvo = iout->dev_priv;
1268
1269 if (sdvo->output_device == SDVOB && sdvoB)
1270 return connector;
1271
1272 if (sdvo->output_device == SDVOC && !sdvoB)
1273 return connector;
1274
1275 }
1276
1277 return NULL;
1278}
1279
1280int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1281{
1282 u8 response[2];
1283 u8 status;
1284 struct intel_output *intel_output;
1285 DRM_DEBUG("\n");
1286
1287 if (!connector)
1288 return 0;
1289
1290 intel_output = to_intel_output(connector);
1291
1292 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1293 status = intel_sdvo_read_response(intel_output, &response, 2);
1294
1295 if (response[0] !=0)
1296 return 1;
1297
1298 return 0;
1299}
1300
1301void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1302{
1303 u8 response[2];
1304 u8 status;
1305 struct intel_output *intel_output = to_intel_output(connector);
1306
1307 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1308 intel_sdvo_read_response(intel_output, &response, 2);
1309
1310 if (on) {
1311 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1312 status = intel_sdvo_read_response(intel_output, &response, 2);
1313
1314 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1315 } else {
1316 response[0] = 0;
1317 response[1] = 0;
1318 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1319 }
1320
1321 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1322 intel_sdvo_read_response(intel_output, &response, 2);
1323}
1324
1325static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1326{
1327 u8 response[2];
1328 u8 status;
1329 struct intel_output *intel_output = to_intel_output(connector);
1330
1331 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
1332 status = intel_sdvo_read_response(intel_output, &response, 2);
1333
1334 DRM_DEBUG("SDVO response %d %d\n", response[0], response[1]);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001335
1336 if (status != SDVO_CMD_STATUS_SUCCESS)
1337 return connector_status_unknown;
1338
Jesse Barnes79e53942008-11-07 14:24:08 -08001339 if ((response[0] != 0) || (response[1] != 0))
1340 return connector_status_connected;
1341 else
1342 return connector_status_disconnected;
1343}
1344
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001345static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001346{
1347 struct intel_output *intel_output = to_intel_output(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001348 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001349
1350 /* set the bus switch and get the modes */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001351 intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
Jesse Barnes79e53942008-11-07 14:24:08 -08001352 intel_ddc_get_modes(intel_output);
1353
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001354#if 0
1355 struct drm_device *dev = encoder->dev;
1356 struct drm_i915_private *dev_priv = dev->dev_private;
1357 /* Mac mini hack. On this device, I get DDC through the analog, which
1358 * load-detects as disconnected. I fail to DDC through the SDVO DDC,
1359 * but it does load-detect as connected. So, just steal the DDC bits
1360 * from analog when we fail at finding it the right way.
1361 */
1362 crt = xf86_config->output[0];
1363 intel_output = crt->driver_private;
1364 if (intel_output->type == I830_OUTPUT_ANALOG &&
1365 crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
1366 I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
1367 edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
1368 xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
1369 }
1370 if (edid_mon) {
1371 xf86OutputSetEDID(output, edid_mon);
1372 modes = xf86OutputGetEDIDModes(output);
1373 }
1374#endif
1375}
1376
1377/**
1378 * This function checks the current TV format, and chooses a default if
1379 * it hasn't been set.
1380 */
1381static void
1382intel_sdvo_check_tv_format(struct intel_output *output)
1383{
1384 struct intel_sdvo_priv *dev_priv = output->dev_priv;
1385 struct intel_sdvo_tv_format format, unset;
1386 uint8_t status;
1387
1388 intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
1389 status = intel_sdvo_read_response(output, &format, sizeof(format));
1390 if (status != SDVO_CMD_STATUS_SUCCESS)
1391 return;
1392
1393 memset(&unset, 0, sizeof(unset));
1394 if (memcmp(&format, &unset, sizeof(format))) {
1395 DRM_DEBUG("%s: Choosing default TV format of NTSC-M\n",
1396 SDVO_NAME(dev_priv));
1397
1398 format.ntsc_m = true;
1399 intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, NULL, 0);
1400 status = intel_sdvo_read_response(output, NULL, 0);
1401 }
1402}
1403
1404/*
1405 * Set of SDVO TV modes.
1406 * Note! This is in reply order (see loop in get_tv_modes).
1407 * XXX: all 60Hz refresh?
1408 */
1409struct drm_display_mode sdvo_tv_modes[] = {
1410 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815680, 321, 384, 416,
1411 200, 0, 232, 201, 233, 4196112, 0,
1412 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1413 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814080, 321, 384, 416,
1414 240, 0, 272, 241, 273, 4196112, 0,
1415 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1416 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910080, 401, 464, 496,
1417 300, 0, 332, 301, 333, 4196112, 0,
1418 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1419 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913280, 641, 704, 736,
1420 350, 0, 382, 351, 383, 4196112, 0,
1421 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1422 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
1423 400, 0, 432, 401, 433, 4196112, 0,
1424 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1425 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
1426 400, 0, 432, 401, 433, 4196112, 0,
1427 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1428 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624000, 705, 768, 800,
1429 480, 0, 512, 481, 513, 4196112, 0,
1430 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1431 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232000, 705, 768, 800,
1432 576, 0, 608, 577, 609, 4196112, 0,
1433 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1434 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751680, 721, 784, 816,
1435 350, 0, 382, 351, 383, 4196112, 0,
1436 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1437 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199680, 721, 784, 816,
1438 400, 0, 432, 401, 433, 4196112, 0,
1439 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1440 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116480, 721, 784, 816,
1441 480, 0, 512, 481, 513, 4196112, 0,
1442 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1443 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054080, 721, 784, 816,
1444 540, 0, 572, 541, 573, 4196112, 0,
1445 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1446 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816640, 721, 784, 816,
1447 576, 0, 608, 577, 609, 4196112, 0,
1448 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1449 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570560, 769, 832, 864,
1450 576, 0, 608, 577, 609, 4196112, 0,
1451 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1452 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030080, 801, 864, 896,
1453 600, 0, 632, 601, 633, 4196112, 0,
1454 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1455 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581760, 833, 896, 928,
1456 624, 0, 656, 625, 657, 4196112, 0,
1457 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1458 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707040, 921, 984, 1016,
1459 766, 0, 798, 767, 799, 4196112, 0,
1460 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1461 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827200, 1025, 1088, 1120,
1462 768, 0, 800, 769, 801, 4196112, 0,
1463 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1464 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265920, 1281, 1344, 1376,
1465 1024, 0, 1056, 1025, 1057, 4196112, 0,
1466 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1467};
1468
1469static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1470{
1471 struct intel_output *output = to_intel_output(connector);
1472 uint32_t reply = 0;
1473 uint8_t status;
1474 int i = 0;
1475
1476 intel_sdvo_check_tv_format(output);
1477
1478 /* Read the list of supported input resolutions for the selected TV
1479 * format.
1480 */
1481 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1482 NULL, 0);
1483 status = intel_sdvo_read_response(output, &reply, 3);
1484 if (status != SDVO_CMD_STATUS_SUCCESS)
1485 return;
1486
1487 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
1488 if (reply & (1 << i))
1489 drm_mode_probed_add(connector, &sdvo_tv_modes[i]);
1490}
1491
1492static int intel_sdvo_get_modes(struct drm_connector *connector)
1493{
1494 struct intel_output *output = to_intel_output(connector);
1495 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1496
1497 if (sdvo_priv->is_tv)
1498 intel_sdvo_get_tv_modes(connector);
1499 else
1500 intel_sdvo_get_ddc_modes(connector);
1501
Jesse Barnes79e53942008-11-07 14:24:08 -08001502 if (list_empty(&connector->probed_modes))
1503 return 0;
1504 return 1;
1505}
1506
1507static void intel_sdvo_destroy(struct drm_connector *connector)
1508{
1509 struct intel_output *intel_output = to_intel_output(connector);
1510
1511 if (intel_output->i2c_bus)
1512 intel_i2c_destroy(intel_output->i2c_bus);
1513 drm_sysfs_connector_remove(connector);
1514 drm_connector_cleanup(connector);
1515 kfree(intel_output);
1516}
1517
1518static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1519 .dpms = intel_sdvo_dpms,
1520 .mode_fixup = intel_sdvo_mode_fixup,
1521 .prepare = intel_encoder_prepare,
1522 .mode_set = intel_sdvo_mode_set,
1523 .commit = intel_encoder_commit,
1524};
1525
1526static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
1527 .save = intel_sdvo_save,
1528 .restore = intel_sdvo_restore,
1529 .detect = intel_sdvo_detect,
1530 .fill_modes = drm_helper_probe_single_connector_modes,
1531 .destroy = intel_sdvo_destroy,
1532};
1533
1534static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1535 .get_modes = intel_sdvo_get_modes,
1536 .mode_valid = intel_sdvo_mode_valid,
1537 .best_encoder = intel_best_encoder,
1538};
1539
Hannes Ederb358d0a2008-12-18 21:18:47 +01001540static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001541{
1542 drm_encoder_cleanup(encoder);
1543}
1544
1545static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1546 .destroy = intel_sdvo_enc_destroy,
1547};
1548
1549
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001550/**
1551 * Choose the appropriate DDC bus for control bus switch command for this
1552 * SDVO output based on the controlled output.
1553 *
1554 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1555 * outputs, then LVDS outputs.
1556 */
1557static void
1558intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
1559{
1560 uint16_t mask = 0;
1561 unsigned int num_bits;
1562
1563 /* Make a mask of outputs less than or equal to our own priority in the
1564 * list.
1565 */
1566 switch (dev_priv->controlled_output) {
1567 case SDVO_OUTPUT_LVDS1:
1568 mask |= SDVO_OUTPUT_LVDS1;
1569 case SDVO_OUTPUT_LVDS0:
1570 mask |= SDVO_OUTPUT_LVDS0;
1571 case SDVO_OUTPUT_TMDS1:
1572 mask |= SDVO_OUTPUT_TMDS1;
1573 case SDVO_OUTPUT_TMDS0:
1574 mask |= SDVO_OUTPUT_TMDS0;
1575 case SDVO_OUTPUT_RGB1:
1576 mask |= SDVO_OUTPUT_RGB1;
1577 case SDVO_OUTPUT_RGB0:
1578 mask |= SDVO_OUTPUT_RGB0;
1579 break;
1580 }
1581
1582 /* Count bits to find what number we are in the priority list. */
1583 mask &= dev_priv->caps.output_flags;
1584 num_bits = hweight16(mask);
1585 if (num_bits > 3) {
1586 /* if more than 3 outputs, default to DDC bus 3 for now */
1587 num_bits = 3;
1588 }
1589
1590 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1591 dev_priv->ddc_bus = 1 << num_bits;
1592}
1593
1594static bool
1595intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
1596{
1597 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1598 uint8_t status;
1599
1600 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1601
1602 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
1603 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
1604 if (status != SDVO_CMD_STATUS_SUCCESS)
1605 return false;
1606 return true;
1607}
1608
Eric Anholt7d573822009-01-02 13:33:00 -08001609bool intel_sdvo_init(struct drm_device *dev, int output_device)
Jesse Barnes79e53942008-11-07 14:24:08 -08001610{
1611 struct drm_connector *connector;
1612 struct intel_output *intel_output;
1613 struct intel_sdvo_priv *sdvo_priv;
1614 struct intel_i2c_chan *i2cbus = NULL;
1615 int connector_type;
1616 u8 ch[0x40];
1617 int i;
1618 int encoder_type, output_id;
1619
1620 intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
1621 if (!intel_output) {
Eric Anholt7d573822009-01-02 13:33:00 -08001622 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08001623 }
1624
1625 connector = &intel_output->base;
1626
1627 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
1628 DRM_MODE_CONNECTOR_Unknown);
1629 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
1630 sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
1631 intel_output->type = INTEL_OUTPUT_SDVO;
1632
1633 connector->interlace_allowed = 0;
1634 connector->doublescan_allowed = 0;
1635
1636 /* setup the DDC bus. */
1637 if (output_device == SDVOB)
1638 i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
1639 else
1640 i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
1641
1642 if (!i2cbus)
1643 goto err_connector;
1644
1645 sdvo_priv->i2c_bus = i2cbus;
1646
1647 if (output_device == SDVOB) {
1648 output_id = 1;
1649 sdvo_priv->i2c_bus->slave_addr = 0x38;
1650 } else {
1651 output_id = 2;
1652 sdvo_priv->i2c_bus->slave_addr = 0x39;
1653 }
1654
1655 sdvo_priv->output_device = output_device;
1656 intel_output->i2c_bus = i2cbus;
1657 intel_output->dev_priv = sdvo_priv;
1658
1659
1660 /* Read the regs to test if we can talk to the device */
1661 for (i = 0; i < 0x40; i++) {
1662 if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
1663 DRM_DEBUG("No SDVO device found on SDVO%c\n",
1664 output_device == SDVOB ? 'B' : 'C');
1665 goto err_i2c;
1666 }
1667 }
1668
1669 intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
1670
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001671 if (sdvo_priv->caps.output_flags &
1672 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1673 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
1674 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
1675 else
1676 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001677
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001678 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1679 encoder_type = DRM_MODE_ENCODER_TMDS;
1680 connector_type = DRM_MODE_CONNECTOR_DVID;
1681
1682 if (intel_sdvo_get_supp_encode(intel_output,
1683 &sdvo_priv->encode) &&
1684 intel_sdvo_get_digital_encoding_mode(intel_output) &&
1685 sdvo_priv->is_hdmi) {
1686 /* enable hdmi encoding mode if supported */
1687 intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
1688 intel_sdvo_set_colorimetry(intel_output,
1689 SDVO_COLORIMETRY_RGB256);
1690 connector_type = DRM_MODE_CONNECTOR_HDMIA;
1691 }
1692 }
1693 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_SVID0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001694 {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001695 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
1696 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1697 encoder_type = DRM_MODE_ENCODER_TVDAC;
1698 connector_type = DRM_MODE_CONNECTOR_SVIDEO;
1699 sdvo_priv->is_tv = true;
1700 intel_output->needs_tv_clock = true;
1701 }
1702 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB0)
1703 {
1704 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001705 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1706 encoder_type = DRM_MODE_ENCODER_DAC;
1707 connector_type = DRM_MODE_CONNECTOR_VGA;
1708 }
1709 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB1)
1710 {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001711 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001712 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1713 encoder_type = DRM_MODE_ENCODER_DAC;
1714 connector_type = DRM_MODE_CONNECTOR_VGA;
1715 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001716 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001717 {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001718 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001719 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001720 encoder_type = DRM_MODE_ENCODER_LVDS;
1721 connector_type = DRM_MODE_CONNECTOR_LVDS;
Jesse Barnes79e53942008-11-07 14:24:08 -08001722 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001723 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS1)
Jesse Barnes79e53942008-11-07 14:24:08 -08001724 {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001725 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
Jesse Barnes79e53942008-11-07 14:24:08 -08001726 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001727 encoder_type = DRM_MODE_ENCODER_LVDS;
1728 connector_type = DRM_MODE_CONNECTOR_LVDS;
Jesse Barnes79e53942008-11-07 14:24:08 -08001729 }
1730 else
1731 {
1732 unsigned char bytes[2];
1733
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001734 sdvo_priv->controlled_output = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08001735 memcpy (bytes, &sdvo_priv->caps.output_flags, 2);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001736 DRM_DEBUG("%s: Unknown SDVO output type (0x%02x%02x)\n",
Jesse Barnes79e53942008-11-07 14:24:08 -08001737 SDVO_NAME(sdvo_priv),
1738 bytes[0], bytes[1]);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001739 encoder_type = DRM_MODE_ENCODER_NONE;
1740 connector_type = DRM_MODE_CONNECTOR_Unknown;
Jesse Barnes79e53942008-11-07 14:24:08 -08001741 goto err_i2c;
1742 }
1743
1744 drm_encoder_init(dev, &intel_output->enc, &intel_sdvo_enc_funcs, encoder_type);
1745 drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
1746 connector->connector_type = connector_type;
1747
1748 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
1749 drm_sysfs_connector_add(connector);
1750
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001751 intel_sdvo_select_ddc_bus(sdvo_priv);
1752
Jesse Barnes79e53942008-11-07 14:24:08 -08001753 /* Set the input timing to the screen. Assume always input 0. */
1754 intel_sdvo_set_target_input(intel_output, true, false);
1755
1756 intel_sdvo_get_input_pixel_clock_range(intel_output,
1757 &sdvo_priv->pixel_clock_min,
1758 &sdvo_priv->pixel_clock_max);
1759
1760
1761 DRM_DEBUG("%s device VID/DID: %02X:%02X.%02X, "
1762 "clock range %dMHz - %dMHz, "
1763 "input 1: %c, input 2: %c, "
1764 "output 1: %c, output 2: %c\n",
1765 SDVO_NAME(sdvo_priv),
1766 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
1767 sdvo_priv->caps.device_rev_id,
1768 sdvo_priv->pixel_clock_min / 1000,
1769 sdvo_priv->pixel_clock_max / 1000,
1770 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
1771 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
1772 /* check currently supported outputs */
1773 sdvo_priv->caps.output_flags &
1774 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
1775 sdvo_priv->caps.output_flags &
1776 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
1777
1778 intel_output->ddc_bus = i2cbus;
1779
Eric Anholt7d573822009-01-02 13:33:00 -08001780 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08001781
1782err_i2c:
1783 intel_i2c_destroy(intel_output->i2c_bus);
1784err_connector:
1785 drm_connector_cleanup(connector);
1786 kfree(intel_output);
1787
Eric Anholt7d573822009-01-02 13:33:00 -08001788 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08001789}