2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
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:
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
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.
26 * Eric Anholt <eric@anholt.net>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
33 #include "intel_drv.h"
36 #include "intel_sdvo_regs.h"
40 struct intel_sdvo_priv {
41 struct intel_i2c_chan *i2c_bus;
44 /* Register for the SDVO device: SDVOB or SDVOC */
47 /* Active outputs controlled by this SDVO output */
48 uint16_t controlled_output;
51 * Capabilities of the SDVO device returned by
52 * i830_sdvo_get_capabilities()
54 struct intel_sdvo_caps caps;
56 /* Pixel clock limitations reported by the SDVO device, in kHz */
57 int pixel_clock_min, pixel_clock_max;
60 * This is set if we're going to treat the device as TV-out.
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).
69 * This is set if we treat the device as HDMI, instead of DVI.
74 * Returned SDTV resolutions allowed for the current format, if the
77 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
80 * Current selected TV format.
82 * This is stored in the same structure that's passed to the device, for
85 struct intel_sdvo_tv_format tv_format;
88 * supported encoding mode, used to determine whether HDMI is
91 struct intel_sdvo_encode encode;
93 /* DDC bus used by this SDVO output */
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];
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).
108 static void intel_sdvo_write_sdvox(struct intel_output *intel_output, u32 val)
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;
116 if (sdvo_priv->output_device == SDVOB) {
117 cval = I915_READ(SDVOC);
119 bval = I915_READ(SDVOB);
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
126 for (i = 0; i < 2; i++)
128 I915_WRITE(SDVOB, bval);
130 I915_WRITE(SDVOC, cval);
135 static bool intel_sdvo_read_byte(struct intel_output *intel_output, u8 addr,
138 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
143 struct i2c_msg msgs[] = {
145 .addr = sdvo_priv->i2c_bus->slave_addr,
151 .addr = sdvo_priv->i2c_bus->slave_addr,
161 if ((ret = i2c_transfer(&sdvo_priv->i2c_bus->adapter, msgs, 2)) == 2)
167 DRM_DEBUG("i2c transfer returned %d\n", ret);
171 static bool intel_sdvo_write_byte(struct intel_output *intel_output, int addr,
175 struct i2c_msg msgs[] = {
177 .addr = intel_output->i2c_bus->slave_addr,
187 if (i2c_transfer(&intel_output->i2c_bus->adapter, msgs, 1) == 1)
194 #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
195 /** Mapping of command numbers to names, for debug output */
196 static const struct _sdvo_cmd_name {
199 } 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),
235 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),
239 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
240 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),
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),
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)
270 static void intel_sdvo_debug_write(struct intel_output *intel_output, u8 cmd,
271 void *args, int args_len)
273 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
276 DRM_DEBUG("%s: W: %02X ", SDVO_NAME(sdvo_priv), cmd);
277 for (i = 0; i < args_len; i++)
278 printk("%02X ", ((u8 *)args)[i]);
281 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
282 if (cmd == sdvo_cmd_names[i].cmd) {
283 printk("(%s)", sdvo_cmd_names[i].name);
287 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
288 printk("(%02X)",cmd);
292 #define intel_sdvo_debug_write(o, c, a, l)
295 static void intel_sdvo_write_cmd(struct intel_output *intel_output, u8 cmd,
296 void *args, int args_len)
300 intel_sdvo_debug_write(intel_output, cmd, args, args_len);
302 for (i = 0; i < args_len; i++) {
303 intel_sdvo_write_byte(intel_output, SDVO_I2C_ARG_0 - i,
307 intel_sdvo_write_byte(intel_output, SDVO_I2C_OPCODE, cmd);
311 static const char *cmd_status_names[] = {
317 "Target not specified",
318 "Scaling not supported"
321 static void intel_sdvo_debug_response(struct intel_output *intel_output,
322 void *response, int response_len,
325 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
327 DRM_DEBUG("%s: R: ", SDVO_NAME(sdvo_priv));
328 for (i = 0; i < response_len; i++)
329 printk("%02X ", ((u8 *)response)[i]);
332 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
333 printk("(%s)", cmd_status_names[status]);
335 printk("(??? %d)", status);
339 #define intel_sdvo_debug_response(o, r, l, s)
342 static u8 intel_sdvo_read_response(struct intel_output *intel_output,
343 void *response, int response_len)
350 /* Read the command response */
351 for (i = 0; i < response_len; i++) {
352 intel_sdvo_read_byte(intel_output,
353 SDVO_I2C_RETURN_0 + i,
354 &((u8 *)response)[i]);
357 /* read the return status */
358 intel_sdvo_read_byte(intel_output, SDVO_I2C_CMD_STATUS,
361 intel_sdvo_debug_response(intel_output, response, response_len,
363 if (status != SDVO_CMD_STATUS_PENDING)
372 static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
374 if (mode->clock >= 100000)
376 else if (mode->clock >= 50000)
383 * Don't check status code from this as it switches the bus back to the
384 * SDVO chips which defeats the purpose of doing a bus switch in the first
387 static void intel_sdvo_set_control_bus_switch(struct intel_output *intel_output,
390 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CONTROL_BUS_SWITCH, &target, 1);
393 static bool intel_sdvo_set_target_input(struct intel_output *intel_output, bool target_0, bool target_1)
395 struct intel_sdvo_set_target_input_args targets = {0};
398 if (target_0 && target_1)
399 return SDVO_CMD_STATUS_NOTSUPP;
402 targets.target_1 = 1;
404 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_INPUT, &targets,
407 status = intel_sdvo_read_response(intel_output, NULL, 0);
409 return (status == SDVO_CMD_STATUS_SUCCESS);
413 * Return whether each input is trained.
415 * This function is making an assumption about the layout of the response,
416 * which should be checked against the docs.
418 static bool intel_sdvo_get_trained_inputs(struct intel_output *intel_output, bool *input_1, bool *input_2)
420 struct intel_sdvo_get_trained_inputs_response response;
423 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
424 status = intel_sdvo_read_response(intel_output, &response, sizeof(response));
425 if (status != SDVO_CMD_STATUS_SUCCESS)
428 *input_1 = response.input0_trained;
429 *input_2 = response.input1_trained;
433 static bool intel_sdvo_get_active_outputs(struct intel_output *intel_output,
438 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
439 status = intel_sdvo_read_response(intel_output, outputs, sizeof(*outputs));
441 return (status == SDVO_CMD_STATUS_SUCCESS);
444 static bool intel_sdvo_set_active_outputs(struct intel_output *intel_output,
449 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
451 status = intel_sdvo_read_response(intel_output, NULL, 0);
452 return (status == SDVO_CMD_STATUS_SUCCESS);
455 static bool intel_sdvo_set_encoder_power_state(struct intel_output *intel_output,
458 u8 status, state = SDVO_ENCODER_STATE_ON;
461 case DRM_MODE_DPMS_ON:
462 state = SDVO_ENCODER_STATE_ON;
464 case DRM_MODE_DPMS_STANDBY:
465 state = SDVO_ENCODER_STATE_STANDBY;
467 case DRM_MODE_DPMS_SUSPEND:
468 state = SDVO_ENCODER_STATE_SUSPEND;
470 case DRM_MODE_DPMS_OFF:
471 state = SDVO_ENCODER_STATE_OFF;
475 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
477 status = intel_sdvo_read_response(intel_output, NULL, 0);
479 return (status == SDVO_CMD_STATUS_SUCCESS);
482 static bool intel_sdvo_get_input_pixel_clock_range(struct intel_output *intel_output,
486 struct intel_sdvo_pixel_clock_range clocks;
489 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
492 status = intel_sdvo_read_response(intel_output, &clocks, sizeof(clocks));
494 if (status != SDVO_CMD_STATUS_SUCCESS)
497 /* Convert the values from units of 10 kHz to kHz. */
498 *clock_min = clocks.min * 10;
499 *clock_max = clocks.max * 10;
504 static bool intel_sdvo_set_target_output(struct intel_output *intel_output,
509 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
512 status = intel_sdvo_read_response(intel_output, NULL, 0);
513 return (status == SDVO_CMD_STATUS_SUCCESS);
516 static bool intel_sdvo_get_timing(struct intel_output *intel_output, u8 cmd,
517 struct intel_sdvo_dtd *dtd)
521 intel_sdvo_write_cmd(intel_output, cmd, NULL, 0);
522 status = intel_sdvo_read_response(intel_output, &dtd->part1,
524 if (status != SDVO_CMD_STATUS_SUCCESS)
527 intel_sdvo_write_cmd(intel_output, cmd + 1, NULL, 0);
528 status = intel_sdvo_read_response(intel_output, &dtd->part2,
530 if (status != SDVO_CMD_STATUS_SUCCESS)
536 static bool intel_sdvo_get_input_timing(struct intel_output *intel_output,
537 struct intel_sdvo_dtd *dtd)
539 return intel_sdvo_get_timing(intel_output,
540 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
543 static bool intel_sdvo_get_output_timing(struct intel_output *intel_output,
544 struct intel_sdvo_dtd *dtd)
546 return intel_sdvo_get_timing(intel_output,
547 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
550 static bool intel_sdvo_set_timing(struct intel_output *intel_output, u8 cmd,
551 struct intel_sdvo_dtd *dtd)
555 intel_sdvo_write_cmd(intel_output, cmd, &dtd->part1, sizeof(dtd->part1));
556 status = intel_sdvo_read_response(intel_output, NULL, 0);
557 if (status != SDVO_CMD_STATUS_SUCCESS)
560 intel_sdvo_write_cmd(intel_output, cmd + 1, &dtd->part2, sizeof(dtd->part2));
561 status = intel_sdvo_read_response(intel_output, NULL, 0);
562 if (status != SDVO_CMD_STATUS_SUCCESS)
568 static bool intel_sdvo_set_input_timing(struct intel_output *intel_output,
569 struct intel_sdvo_dtd *dtd)
571 return intel_sdvo_set_timing(intel_output,
572 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
575 static bool intel_sdvo_set_output_timing(struct intel_output *intel_output,
576 struct intel_sdvo_dtd *dtd)
578 return intel_sdvo_set_timing(intel_output,
579 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
583 intel_sdvo_create_preferred_input_timing(struct intel_output *output,
588 struct intel_sdvo_preferred_input_timing_args args;
593 args.height = height;
594 intel_sdvo_write_cmd(output, SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
595 &args, sizeof(args));
596 status = intel_sdvo_read_response(output, NULL, 0);
597 if (status != SDVO_CMD_STATUS_SUCCESS)
603 static bool intel_sdvo_get_preferred_input_timing(struct intel_output *output,
604 struct intel_sdvo_dtd *dtd)
608 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
611 status = intel_sdvo_read_response(output, &dtd->part1,
613 if (status != SDVO_CMD_STATUS_SUCCESS)
616 intel_sdvo_write_cmd(output, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
619 status = intel_sdvo_read_response(output, &dtd->part2,
621 if (status != SDVO_CMD_STATUS_SUCCESS)
627 static int intel_sdvo_get_clock_rate_mult(struct intel_output *intel_output)
631 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
632 status = intel_sdvo_read_response(intel_output, &response, 1);
634 if (status != SDVO_CMD_STATUS_SUCCESS) {
635 DRM_DEBUG("Couldn't get SDVO clock rate multiplier\n");
636 return SDVO_CLOCK_RATE_MULT_1X;
638 DRM_DEBUG("Current clock rate multiplier: %d\n", response);
644 static bool intel_sdvo_set_clock_rate_mult(struct intel_output *intel_output, u8 val)
648 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
649 status = intel_sdvo_read_response(intel_output, NULL, 0);
650 if (status != SDVO_CMD_STATUS_SUCCESS)
656 static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
657 struct drm_display_mode *mode)
659 uint16_t width, height;
660 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
661 uint16_t h_sync_offset, v_sync_offset;
663 width = mode->crtc_hdisplay;
664 height = mode->crtc_vdisplay;
666 /* do some mode translations */
667 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
668 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
670 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
671 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
673 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
674 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
676 dtd->part1.clock = mode->clock / 10;
677 dtd->part1.h_active = width & 0xff;
678 dtd->part1.h_blank = h_blank_len & 0xff;
679 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
680 ((h_blank_len >> 8) & 0xf);
681 dtd->part1.v_active = height & 0xff;
682 dtd->part1.v_blank = v_blank_len & 0xff;
683 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
684 ((v_blank_len >> 8) & 0xf);
686 dtd->part2.h_sync_off = h_sync_offset;
687 dtd->part2.h_sync_width = h_sync_len & 0xff;
688 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
690 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
691 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
692 ((v_sync_len & 0x30) >> 4);
694 dtd->part2.dtd_flags = 0x18;
695 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
696 dtd->part2.dtd_flags |= 0x2;
697 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
698 dtd->part2.dtd_flags |= 0x4;
700 dtd->part2.sdvo_flags = 0;
701 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
702 dtd->part2.reserved = 0;
705 static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
706 struct intel_sdvo_dtd *dtd)
708 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;
712 width = mode->crtc_hdisplay;
713 height = mode->crtc_vdisplay;
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;
719 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
720 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
722 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
723 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
725 mode->hdisplay = dtd->part1.h_active;
726 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
727 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
728 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xa0) << 2;
729 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
730 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
731 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
732 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
734 mode->vdisplay = dtd->part1.v_active;
735 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
736 mode->vsync_start = mode->vdisplay;
737 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
738 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0a) << 2;
739 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
740 mode->vsync_end = mode->vsync_start +
741 (dtd->part2.v_sync_off_width & 0xf);
742 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
743 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
744 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
746 mode->clock = dtd->part1.clock * 10;
748 mode->flags &= (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
749 if (dtd->part2.dtd_flags & 0x2)
750 mode->flags |= DRM_MODE_FLAG_PHSYNC;
751 if (dtd->part2.dtd_flags & 0x4)
752 mode->flags |= DRM_MODE_FLAG_PVSYNC;
755 static bool intel_sdvo_get_supp_encode(struct intel_output *output,
756 struct intel_sdvo_encode *encode)
760 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
761 status = intel_sdvo_read_response(output, encode, sizeof(*encode));
762 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
763 memset(encode, 0, sizeof(*encode));
770 static bool intel_sdvo_set_encode(struct intel_output *output, uint8_t mode)
774 intel_sdvo_write_cmd(output, SDVO_CMD_SET_ENCODE, &mode, 1);
775 status = intel_sdvo_read_response(output, NULL, 0);
777 return (status == SDVO_CMD_STATUS_SUCCESS);
780 static bool intel_sdvo_set_colorimetry(struct intel_output *output,
785 intel_sdvo_write_cmd(output, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
786 status = intel_sdvo_read_response(output, NULL, 0);
788 return (status == SDVO_CMD_STATUS_SUCCESS);
792 static void intel_sdvo_dump_hdmi_buf(struct intel_output *output)
795 uint8_t set_buf_index[2];
801 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
802 intel_sdvo_read_response(output, &av_split, 1);
804 for (i = 0; i <= av_split; i++) {
805 set_buf_index[0] = i; set_buf_index[1] = 0;
806 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX,
808 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
809 intel_sdvo_read_response(output, &buf_size, 1);
812 for (j = 0; j <= buf_size; j += 8) {
813 intel_sdvo_write_cmd(output, SDVO_CMD_GET_HBUF_DATA,
815 intel_sdvo_read_response(output, pos, 8);
822 static void intel_sdvo_set_hdmi_buf(struct intel_output *output, int index,
823 uint8_t *data, int8_t size, uint8_t tx_rate)
825 uint8_t set_buf_index[2];
827 set_buf_index[0] = index;
828 set_buf_index[1] = 0;
830 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_INDEX, set_buf_index, 2);
832 for (; size > 0; size -= 8) {
833 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_DATA, data, 8);
837 intel_sdvo_write_cmd(output, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
840 static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
845 for (i = 0; i < size; i++)
851 #define DIP_TYPE_AVI 0x82
852 #define DIP_VERSION_AVI 0x2
853 #define DIP_LEN_AVI 13
855 struct dip_infoframe {
883 /* Packet Byte #6~13 */
884 uint16_t top_bar_end;
885 uint16_t bottom_bar_start;
886 uint16_t left_bar_end;
887 uint16_t right_bar_start;
891 uint8_t channel_count:3;
893 uint8_t coding_type:4;
895 uint8_t sample_size:2; /* SS0, SS1 */
896 uint8_t sample_frequency:3;
899 uint8_t coding_type_private:5;
902 uint8_t channel_allocation;
905 uint8_t level_shift:4;
906 uint8_t downmix_inhibit:1;
909 } __attribute__ ((packed)) u;
910 } __attribute__((packed));
912 static void intel_sdvo_set_avi_infoframe(struct intel_output *output,
913 struct drm_display_mode * mode)
915 struct dip_infoframe avi_if = {
916 .type = DIP_TYPE_AVI,
917 .version = DIP_VERSION_AVI,
921 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
923 intel_sdvo_set_hdmi_buf(output, 1, (uint8_t *)&avi_if, 4 + avi_if.len,
927 static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
928 struct drm_display_mode *mode,
929 struct drm_display_mode *adjusted_mode)
931 struct intel_output *output = enc_to_intel_output(encoder);
932 struct intel_sdvo_priv *dev_priv = output->dev_priv;
934 if (!dev_priv->is_tv) {
935 /* Make the CRTC code factor in the SDVO pixel multiplier. The
936 * SDVO device will be told of the multiplier during mode_set.
938 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
940 struct intel_sdvo_dtd output_dtd;
943 /* We need to construct preferred input timings based on our
944 * output timings. To do that, we have to set the output
945 * timings, even though this isn't really the right place in
946 * the sequence to do it. Oh well.
950 /* Set output timings */
951 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
952 intel_sdvo_set_target_output(output,
953 dev_priv->controlled_output);
954 intel_sdvo_set_output_timing(output, &output_dtd);
956 /* Set the input timing to the screen. Assume always input 0. */
957 intel_sdvo_set_target_input(output, true, false);
960 success = intel_sdvo_create_preferred_input_timing(output,
965 struct intel_sdvo_dtd input_dtd;
967 intel_sdvo_get_preferred_input_timing(output,
969 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
978 static void intel_sdvo_mode_set(struct drm_encoder *encoder,
979 struct drm_display_mode *mode,
980 struct drm_display_mode *adjusted_mode)
982 struct drm_device *dev = encoder->dev;
983 struct drm_i915_private *dev_priv = dev->dev_private;
984 struct drm_crtc *crtc = encoder->crtc;
985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
986 struct intel_output *output = enc_to_intel_output(encoder);
987 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
989 int sdvo_pixel_multiply;
990 struct intel_sdvo_in_out_map in_out;
991 struct intel_sdvo_dtd input_dtd;
997 /* First, set the input mapping for the first input to our controlled
998 * output. This is only correct if we're a single-input device, in
999 * which case the first input is the output from the appropriate SDVO
1000 * channel on the motherboard. In a two-input device, the first input
1001 * will be SDVOB and the second SDVOC.
1003 in_out.in0 = sdvo_priv->controlled_output;
1006 intel_sdvo_write_cmd(output, SDVO_CMD_SET_IN_OUT_MAP,
1007 &in_out, sizeof(in_out));
1008 status = intel_sdvo_read_response(output, NULL, 0);
1010 if (sdvo_priv->is_hdmi) {
1011 intel_sdvo_set_avi_infoframe(output, mode);
1012 sdvox |= SDVO_AUDIO_ENABLE;
1015 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
1017 /* If it's a TV, we already set the output timing in mode_fixup.
1018 * Otherwise, the output timing is equal to the input timing.
1020 if (!sdvo_priv->is_tv) {
1021 /* Set the output timing to the screen */
1022 intel_sdvo_set_target_output(output,
1023 sdvo_priv->controlled_output);
1024 intel_sdvo_set_output_timing(output, &input_dtd);
1027 /* Set the input timing to the screen. Assume always input 0. */
1028 intel_sdvo_set_target_input(output, true, false);
1030 /* We would like to use intel_sdvo_create_preferred_input_timing() to
1031 * provide the device with a timing it can support, if it supports that
1032 * feature. However, presumably we would need to adjust the CRTC to
1033 * output the preferred timing, and we don't support that currently.
1036 success = intel_sdvo_create_preferred_input_timing(output, clock,
1039 struct intel_sdvo_dtd *input_dtd;
1041 intel_sdvo_get_preferred_input_timing(output, &input_dtd);
1042 intel_sdvo_set_input_timing(output, &input_dtd);
1045 intel_sdvo_set_input_timing(output, &input_dtd);
1048 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1050 intel_sdvo_set_clock_rate_mult(output,
1051 SDVO_CLOCK_RATE_MULT_1X);
1054 intel_sdvo_set_clock_rate_mult(output,
1055 SDVO_CLOCK_RATE_MULT_2X);
1058 intel_sdvo_set_clock_rate_mult(output,
1059 SDVO_CLOCK_RATE_MULT_4X);
1063 /* Set the SDVO control regs. */
1064 if (IS_I965G(dev)) {
1065 sdvox |= SDVO_BORDER_ENABLE |
1066 SDVO_VSYNC_ACTIVE_HIGH |
1067 SDVO_HSYNC_ACTIVE_HIGH;
1069 sdvox |= I915_READ(sdvo_priv->output_device);
1070 switch (sdvo_priv->output_device) {
1072 sdvox &= SDVOB_PRESERVE_MASK;
1075 sdvox &= SDVOC_PRESERVE_MASK;
1078 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1080 if (intel_crtc->pipe == 1)
1081 sdvox |= SDVO_PIPE_B_SELECT;
1083 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1084 if (IS_I965G(dev)) {
1085 /* done in crtc_mode_set as the dpll_md reg must be written early */
1086 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1087 /* done in crtc_mode_set as it lives inside the dpll register */
1089 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1092 intel_sdvo_write_sdvox(output, sdvox);
1095 static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1097 struct drm_device *dev = encoder->dev;
1098 struct drm_i915_private *dev_priv = dev->dev_private;
1099 struct intel_output *intel_output = enc_to_intel_output(encoder);
1100 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1103 if (mode != DRM_MODE_DPMS_ON) {
1104 intel_sdvo_set_active_outputs(intel_output, 0);
1106 intel_sdvo_set_encoder_power_state(intel_output, mode);
1108 if (mode == DRM_MODE_DPMS_OFF) {
1109 temp = I915_READ(sdvo_priv->output_device);
1110 if ((temp & SDVO_ENABLE) != 0) {
1111 intel_sdvo_write_sdvox(intel_output, temp & ~SDVO_ENABLE);
1115 bool input1, input2;
1119 temp = I915_READ(sdvo_priv->output_device);
1120 if ((temp & SDVO_ENABLE) == 0)
1121 intel_sdvo_write_sdvox(intel_output, temp | SDVO_ENABLE);
1122 for (i = 0; i < 2; i++)
1123 intel_wait_for_vblank(dev);
1125 status = intel_sdvo_get_trained_inputs(intel_output, &input1,
1129 /* Warn if the device reported failure to sync.
1130 * A lot of SDVO devices fail to notify of sync, but it's
1131 * a given it the status is a success, we succeeded.
1133 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1134 DRM_DEBUG("First %s output reported failure to sync\n",
1135 SDVO_NAME(sdvo_priv));
1139 intel_sdvo_set_encoder_power_state(intel_output, mode);
1140 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->controlled_output);
1145 static void intel_sdvo_save(struct drm_connector *connector)
1147 struct drm_device *dev = connector->dev;
1148 struct drm_i915_private *dev_priv = dev->dev_private;
1149 struct intel_output *intel_output = to_intel_output(connector);
1150 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1153 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_output);
1154 intel_sdvo_get_active_outputs(intel_output, &sdvo_priv->save_active_outputs);
1156 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1157 intel_sdvo_set_target_input(intel_output, true, false);
1158 intel_sdvo_get_input_timing(intel_output,
1159 &sdvo_priv->save_input_dtd_1);
1162 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1163 intel_sdvo_set_target_input(intel_output, false, true);
1164 intel_sdvo_get_input_timing(intel_output,
1165 &sdvo_priv->save_input_dtd_2);
1168 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1170 u16 this_output = (1 << o);
1171 if (sdvo_priv->caps.output_flags & this_output)
1173 intel_sdvo_set_target_output(intel_output, this_output);
1174 intel_sdvo_get_output_timing(intel_output,
1175 &sdvo_priv->save_output_dtd[o]);
1178 if (sdvo_priv->is_tv) {
1179 /* XXX: Save TV format/enhancements. */
1182 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->output_device);
1185 static void intel_sdvo_restore(struct drm_connector *connector)
1187 struct drm_device *dev = connector->dev;
1188 struct intel_output *intel_output = to_intel_output(connector);
1189 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1192 bool input1, input2;
1195 intel_sdvo_set_active_outputs(intel_output, 0);
1197 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1199 u16 this_output = (1 << o);
1200 if (sdvo_priv->caps.output_flags & this_output) {
1201 intel_sdvo_set_target_output(intel_output, this_output);
1202 intel_sdvo_set_output_timing(intel_output, &sdvo_priv->save_output_dtd[o]);
1206 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
1207 intel_sdvo_set_target_input(intel_output, true, false);
1208 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_1);
1211 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
1212 intel_sdvo_set_target_input(intel_output, false, true);
1213 intel_sdvo_set_input_timing(intel_output, &sdvo_priv->save_input_dtd_2);
1216 intel_sdvo_set_clock_rate_mult(intel_output, sdvo_priv->save_sdvo_mult);
1218 if (sdvo_priv->is_tv) {
1219 /* XXX: Restore TV format/enhancements. */
1222 intel_sdvo_write_sdvox(intel_output, sdvo_priv->save_SDVOX);
1224 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1226 for (i = 0; i < 2; i++)
1227 intel_wait_for_vblank(dev);
1228 status = intel_sdvo_get_trained_inputs(intel_output, &input1, &input2);
1229 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
1230 DRM_DEBUG("First %s output reported failure to sync\n",
1231 SDVO_NAME(sdvo_priv));
1234 intel_sdvo_set_active_outputs(intel_output, sdvo_priv->save_active_outputs);
1237 static int intel_sdvo_mode_valid(struct drm_connector *connector,
1238 struct drm_display_mode *mode)
1240 struct intel_output *intel_output = to_intel_output(connector);
1241 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1243 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1244 return MODE_NO_DBLESCAN;
1246 if (sdvo_priv->pixel_clock_min > mode->clock)
1247 return MODE_CLOCK_LOW;
1249 if (sdvo_priv->pixel_clock_max < mode->clock)
1250 return MODE_CLOCK_HIGH;
1255 static bool intel_sdvo_get_capabilities(struct intel_output *intel_output, struct intel_sdvo_caps *caps)
1259 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1260 status = intel_sdvo_read_response(intel_output, caps, sizeof(*caps));
1261 if (status != SDVO_CMD_STATUS_SUCCESS)
1267 struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1269 struct drm_connector *connector = NULL;
1270 struct intel_output *iout = NULL;
1271 struct intel_sdvo_priv *sdvo;
1273 /* find the sdvo connector */
1274 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1275 iout = to_intel_output(connector);
1277 if (iout->type != INTEL_OUTPUT_SDVO)
1280 sdvo = iout->dev_priv;
1282 if (sdvo->output_device == SDVOB && sdvoB)
1285 if (sdvo->output_device == SDVOC && !sdvoB)
1293 int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1297 struct intel_output *intel_output;
1303 intel_output = to_intel_output(connector);
1305 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1306 status = intel_sdvo_read_response(intel_output, &response, 2);
1308 if (response[0] !=0)
1314 void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1318 struct intel_output *intel_output = to_intel_output(connector);
1320 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1321 intel_sdvo_read_response(intel_output, &response, 2);
1324 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1325 status = intel_sdvo_read_response(intel_output, &response, 2);
1327 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1331 intel_sdvo_write_cmd(intel_output, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1334 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1335 intel_sdvo_read_response(intel_output, &response, 2);
1338 static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1342 struct intel_output *intel_output = to_intel_output(connector);
1344 intel_sdvo_write_cmd(intel_output, SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
1345 status = intel_sdvo_read_response(intel_output, &response, 2);
1347 DRM_DEBUG("SDVO response %d %d\n", response[0], response[1]);
1349 if (status != SDVO_CMD_STATUS_SUCCESS)
1350 return connector_status_unknown;
1352 if ((response[0] != 0) || (response[1] != 0))
1353 return connector_status_connected;
1355 return connector_status_disconnected;
1358 static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1360 struct intel_output *intel_output = to_intel_output(connector);
1361 struct intel_sdvo_priv *sdvo_priv = intel_output->dev_priv;
1363 /* set the bus switch and get the modes */
1364 intel_sdvo_set_control_bus_switch(intel_output, sdvo_priv->ddc_bus);
1365 intel_ddc_get_modes(intel_output);
1368 struct drm_device *dev = encoder->dev;
1369 struct drm_i915_private *dev_priv = dev->dev_private;
1370 /* Mac mini hack. On this device, I get DDC through the analog, which
1371 * load-detects as disconnected. I fail to DDC through the SDVO DDC,
1372 * but it does load-detect as connected. So, just steal the DDC bits
1373 * from analog when we fail at finding it the right way.
1375 crt = xf86_config->output[0];
1376 intel_output = crt->driver_private;
1377 if (intel_output->type == I830_OUTPUT_ANALOG &&
1378 crt->funcs->detect(crt) == XF86OutputStatusDisconnected) {
1379 I830I2CInit(pScrn, &intel_output->pDDCBus, GPIOA, "CRTDDC_A");
1380 edid_mon = xf86OutputGetEDID(crt, intel_output->pDDCBus);
1381 xf86DestroyI2CBusRec(intel_output->pDDCBus, true, true);
1384 xf86OutputSetEDID(output, edid_mon);
1385 modes = xf86OutputGetEDIDModes(output);
1391 * This function checks the current TV format, and chooses a default if
1392 * it hasn't been set.
1395 intel_sdvo_check_tv_format(struct intel_output *output)
1397 struct intel_sdvo_priv *dev_priv = output->dev_priv;
1398 struct intel_sdvo_tv_format format, unset;
1401 intel_sdvo_write_cmd(output, SDVO_CMD_GET_TV_FORMAT, NULL, 0);
1402 status = intel_sdvo_read_response(output, &format, sizeof(format));
1403 if (status != SDVO_CMD_STATUS_SUCCESS)
1406 memset(&unset, 0, sizeof(unset));
1407 if (memcmp(&format, &unset, sizeof(format))) {
1408 DRM_DEBUG("%s: Choosing default TV format of NTSC-M\n",
1409 SDVO_NAME(dev_priv));
1411 format.ntsc_m = true;
1412 intel_sdvo_write_cmd(output, SDVO_CMD_SET_TV_FORMAT, NULL, 0);
1413 status = intel_sdvo_read_response(output, NULL, 0);
1418 * Set of SDVO TV modes.
1419 * Note! This is in reply order (see loop in get_tv_modes).
1420 * XXX: all 60Hz refresh?
1422 struct drm_display_mode sdvo_tv_modes[] = {
1423 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815680, 321, 384, 416,
1424 200, 0, 232, 201, 233, 4196112, 0,
1425 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1426 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814080, 321, 384, 416,
1427 240, 0, 272, 241, 273, 4196112, 0,
1428 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1429 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910080, 401, 464, 496,
1430 300, 0, 332, 301, 333, 4196112, 0,
1431 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1432 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913280, 641, 704, 736,
1433 350, 0, 382, 351, 383, 4196112, 0,
1434 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1435 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
1436 400, 0, 432, 401, 433, 4196112, 0,
1437 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1438 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121280, 641, 704, 736,
1439 400, 0, 432, 401, 433, 4196112, 0,
1440 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1441 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624000, 705, 768, 800,
1442 480, 0, 512, 481, 513, 4196112, 0,
1443 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1444 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232000, 705, 768, 800,
1445 576, 0, 608, 577, 609, 4196112, 0,
1446 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1447 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751680, 721, 784, 816,
1448 350, 0, 382, 351, 383, 4196112, 0,
1449 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1450 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199680, 721, 784, 816,
1451 400, 0, 432, 401, 433, 4196112, 0,
1452 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1453 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116480, 721, 784, 816,
1454 480, 0, 512, 481, 513, 4196112, 0,
1455 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1456 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054080, 721, 784, 816,
1457 540, 0, 572, 541, 573, 4196112, 0,
1458 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1459 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816640, 721, 784, 816,
1460 576, 0, 608, 577, 609, 4196112, 0,
1461 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1462 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570560, 769, 832, 864,
1463 576, 0, 608, 577, 609, 4196112, 0,
1464 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1465 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030080, 801, 864, 896,
1466 600, 0, 632, 601, 633, 4196112, 0,
1467 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1468 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581760, 833, 896, 928,
1469 624, 0, 656, 625, 657, 4196112, 0,
1470 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1471 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707040, 921, 984, 1016,
1472 766, 0, 798, 767, 799, 4196112, 0,
1473 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1474 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827200, 1025, 1088, 1120,
1475 768, 0, 800, 769, 801, 4196112, 0,
1476 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1477 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265920, 1281, 1344, 1376,
1478 1024, 0, 1056, 1025, 1057, 4196112, 0,
1479 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1482 static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1484 struct intel_output *output = to_intel_output(connector);
1489 intel_sdvo_check_tv_format(output);
1491 /* Read the list of supported input resolutions for the selected TV
1494 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1496 status = intel_sdvo_read_response(output, &reply, 3);
1497 if (status != SDVO_CMD_STATUS_SUCCESS)
1500 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
1501 if (reply & (1 << i))
1502 drm_mode_probed_add(connector, &sdvo_tv_modes[i]);
1505 static int intel_sdvo_get_modes(struct drm_connector *connector)
1507 struct intel_output *output = to_intel_output(connector);
1508 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1510 if (sdvo_priv->is_tv)
1511 intel_sdvo_get_tv_modes(connector);
1513 intel_sdvo_get_ddc_modes(connector);
1515 if (list_empty(&connector->probed_modes))
1520 static void intel_sdvo_destroy(struct drm_connector *connector)
1522 struct intel_output *intel_output = to_intel_output(connector);
1524 if (intel_output->i2c_bus)
1525 intel_i2c_destroy(intel_output->i2c_bus);
1526 drm_sysfs_connector_remove(connector);
1527 drm_connector_cleanup(connector);
1528 kfree(intel_output);
1531 static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1532 .dpms = intel_sdvo_dpms,
1533 .mode_fixup = intel_sdvo_mode_fixup,
1534 .prepare = intel_encoder_prepare,
1535 .mode_set = intel_sdvo_mode_set,
1536 .commit = intel_encoder_commit,
1539 static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
1540 .save = intel_sdvo_save,
1541 .restore = intel_sdvo_restore,
1542 .detect = intel_sdvo_detect,
1543 .fill_modes = drm_helper_probe_single_connector_modes,
1544 .destroy = intel_sdvo_destroy,
1547 static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1548 .get_modes = intel_sdvo_get_modes,
1549 .mode_valid = intel_sdvo_mode_valid,
1550 .best_encoder = intel_best_encoder,
1553 static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
1555 drm_encoder_cleanup(encoder);
1558 static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1559 .destroy = intel_sdvo_enc_destroy,
1564 * Choose the appropriate DDC bus for control bus switch command for this
1565 * SDVO output based on the controlled output.
1567 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1568 * outputs, then LVDS outputs.
1571 intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
1574 unsigned int num_bits;
1576 /* Make a mask of outputs less than or equal to our own priority in the
1579 switch (dev_priv->controlled_output) {
1580 case SDVO_OUTPUT_LVDS1:
1581 mask |= SDVO_OUTPUT_LVDS1;
1582 case SDVO_OUTPUT_LVDS0:
1583 mask |= SDVO_OUTPUT_LVDS0;
1584 case SDVO_OUTPUT_TMDS1:
1585 mask |= SDVO_OUTPUT_TMDS1;
1586 case SDVO_OUTPUT_TMDS0:
1587 mask |= SDVO_OUTPUT_TMDS0;
1588 case SDVO_OUTPUT_RGB1:
1589 mask |= SDVO_OUTPUT_RGB1;
1590 case SDVO_OUTPUT_RGB0:
1591 mask |= SDVO_OUTPUT_RGB0;
1595 /* Count bits to find what number we are in the priority list. */
1596 mask &= dev_priv->caps.output_flags;
1597 num_bits = hweight16(mask);
1599 /* if more than 3 outputs, default to DDC bus 3 for now */
1603 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1604 dev_priv->ddc_bus = 1 << num_bits;
1608 intel_sdvo_get_digital_encoding_mode(struct intel_output *output)
1610 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1613 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1615 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
1616 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
1617 if (status != SDVO_CMD_STATUS_SUCCESS)
1622 bool intel_sdvo_init(struct drm_device *dev, int output_device)
1624 struct drm_connector *connector;
1625 struct intel_output *intel_output;
1626 struct intel_sdvo_priv *sdvo_priv;
1627 struct intel_i2c_chan *i2cbus = NULL;
1631 int encoder_type, output_id;
1633 intel_output = kcalloc(sizeof(struct intel_output)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
1634 if (!intel_output) {
1638 connector = &intel_output->base;
1640 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
1641 DRM_MODE_CONNECTOR_Unknown);
1642 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
1643 sdvo_priv = (struct intel_sdvo_priv *)(intel_output + 1);
1644 intel_output->type = INTEL_OUTPUT_SDVO;
1646 connector->interlace_allowed = 0;
1647 connector->doublescan_allowed = 0;
1649 /* setup the DDC bus. */
1650 if (output_device == SDVOB)
1651 i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
1653 i2cbus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
1658 sdvo_priv->i2c_bus = i2cbus;
1660 if (output_device == SDVOB) {
1662 sdvo_priv->i2c_bus->slave_addr = 0x38;
1665 sdvo_priv->i2c_bus->slave_addr = 0x39;
1668 sdvo_priv->output_device = output_device;
1669 intel_output->i2c_bus = i2cbus;
1670 intel_output->dev_priv = sdvo_priv;
1673 /* Read the regs to test if we can talk to the device */
1674 for (i = 0; i < 0x40; i++) {
1675 if (!intel_sdvo_read_byte(intel_output, i, &ch[i])) {
1676 DRM_DEBUG("No SDVO device found on SDVO%c\n",
1677 output_device == SDVOB ? 'B' : 'C');
1682 intel_sdvo_get_capabilities(intel_output, &sdvo_priv->caps);
1684 if (sdvo_priv->caps.output_flags &
1685 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1686 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
1687 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
1689 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
1691 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1692 encoder_type = DRM_MODE_ENCODER_TMDS;
1693 connector_type = DRM_MODE_CONNECTOR_DVID;
1695 if (intel_sdvo_get_supp_encode(intel_output,
1696 &sdvo_priv->encode) &&
1697 intel_sdvo_get_digital_encoding_mode(intel_output) &&
1698 sdvo_priv->is_hdmi) {
1699 /* enable hdmi encoding mode if supported */
1700 intel_sdvo_set_encode(intel_output, SDVO_ENCODE_HDMI);
1701 intel_sdvo_set_colorimetry(intel_output,
1702 SDVO_COLORIMETRY_RGB256);
1703 connector_type = DRM_MODE_CONNECTOR_HDMIA;
1706 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_SVID0)
1708 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
1709 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1710 encoder_type = DRM_MODE_ENCODER_TVDAC;
1711 connector_type = DRM_MODE_CONNECTOR_SVIDEO;
1712 sdvo_priv->is_tv = true;
1713 intel_output->needs_tv_clock = true;
1715 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB0)
1717 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
1718 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1719 encoder_type = DRM_MODE_ENCODER_DAC;
1720 connector_type = DRM_MODE_CONNECTOR_VGA;
1722 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_RGB1)
1724 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
1725 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1726 encoder_type = DRM_MODE_ENCODER_DAC;
1727 connector_type = DRM_MODE_CONNECTOR_VGA;
1729 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS0)
1731 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
1732 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1733 encoder_type = DRM_MODE_ENCODER_LVDS;
1734 connector_type = DRM_MODE_CONNECTOR_LVDS;
1736 else if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_LVDS1)
1738 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
1739 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
1740 encoder_type = DRM_MODE_ENCODER_LVDS;
1741 connector_type = DRM_MODE_CONNECTOR_LVDS;
1745 unsigned char bytes[2];
1747 sdvo_priv->controlled_output = 0;
1748 memcpy (bytes, &sdvo_priv->caps.output_flags, 2);
1749 DRM_DEBUG("%s: Unknown SDVO output type (0x%02x%02x)\n",
1750 SDVO_NAME(sdvo_priv),
1751 bytes[0], bytes[1]);
1752 encoder_type = DRM_MODE_ENCODER_NONE;
1753 connector_type = DRM_MODE_CONNECTOR_Unknown;
1757 drm_encoder_init(dev, &intel_output->enc, &intel_sdvo_enc_funcs, encoder_type);
1758 drm_encoder_helper_add(&intel_output->enc, &intel_sdvo_helper_funcs);
1759 connector->connector_type = connector_type;
1761 drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc);
1762 drm_sysfs_connector_add(connector);
1764 intel_sdvo_select_ddc_bus(sdvo_priv);
1766 /* Set the input timing to the screen. Assume always input 0. */
1767 intel_sdvo_set_target_input(intel_output, true, false);
1769 intel_sdvo_get_input_pixel_clock_range(intel_output,
1770 &sdvo_priv->pixel_clock_min,
1771 &sdvo_priv->pixel_clock_max);
1774 DRM_DEBUG("%s device VID/DID: %02X:%02X.%02X, "
1775 "clock range %dMHz - %dMHz, "
1776 "input 1: %c, input 2: %c, "
1777 "output 1: %c, output 2: %c\n",
1778 SDVO_NAME(sdvo_priv),
1779 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
1780 sdvo_priv->caps.device_rev_id,
1781 sdvo_priv->pixel_clock_min / 1000,
1782 sdvo_priv->pixel_clock_max / 1000,
1783 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
1784 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
1785 /* check currently supported outputs */
1786 sdvo_priv->caps.output_flags &
1787 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
1788 sdvo_priv->caps.output_flags &
1789 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
1791 intel_output->ddc_bus = i2cbus;
1796 intel_i2c_destroy(intel_output->i2c_bus);
1798 drm_connector_cleanup(connector);
1799 kfree(intel_output);