blob: 097819c51a15b63a961f0adf234523402a9e0852 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 */
28#include <linux/i2c.h>
29#include <linux/delay.h>
30#include "drmP.h"
31#include "drm.h"
32#include "drm_crtc.h"
33#include "intel_drv.h"
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +080034#include "drm_edid.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "i915_drm.h"
36#include "i915_drv.h"
37#include "intel_sdvo_regs.h"
Zhao Yakui6070a4a2010-02-08 21:35:12 +080038#include <linux/dmi.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039
Zhao Yakuice6feab2009-08-24 13:50:26 +080040static char *tv_format_names[] = {
41 "NTSC_M" , "NTSC_J" , "NTSC_443",
42 "PAL_B" , "PAL_D" , "PAL_G" ,
43 "PAL_H" , "PAL_I" , "PAL_M" ,
44 "PAL_N" , "PAL_NC" , "PAL_60" ,
45 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
46 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
47 "SECAM_60"
48};
49
50#define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
51
Jesse Barnes79e53942008-11-07 14:24:08 -080052struct intel_sdvo_priv {
Keith Packardf9c10a92009-05-30 12:16:25 -070053 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080054
55 /* Register for the SDVO device: SDVOB or SDVOC */
Eric Anholtc751ce42010-03-25 11:48:48 -070056 int sdvo_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080057
Jesse Barnese2f0ba92009-02-02 15:11:52 -080058 /* Active outputs controlled by this SDVO output */
59 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080060
Jesse Barnese2f0ba92009-02-02 15:11:52 -080061 /*
62 * Capabilities of the SDVO device returned by
63 * i830_sdvo_get_capabilities()
64 */
Jesse Barnes79e53942008-11-07 14:24:08 -080065 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080066
67 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080068 int pixel_clock_min, pixel_clock_max;
69
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080070 /*
71 * For multiple function SDVO device,
72 * this is for current attached outputs.
73 */
74 uint16_t attached_output;
75
Jesse Barnese2f0ba92009-02-02 15:11:52 -080076 /**
77 * This is set if we're going to treat the device as TV-out.
78 *
79 * While we have these nice friendly flags for output types that ought
80 * to decide this for us, the S-Video output on our HDMI+S-Video card
81 * shows up as RGB1 (VGA).
82 */
83 bool is_tv;
84
Zhao Yakuice6feab2009-08-24 13:50:26 +080085 /* This is for current tv format name */
86 char *tv_format_name;
87
88 /* This contains all current supported TV format */
89 char *tv_format_supported[TV_FORMAT_NUM];
90 int format_supported_num;
91 struct drm_property *tv_format_property;
92 struct drm_property *tv_format_name_property[TV_FORMAT_NUM];
93
Jesse Barnese2f0ba92009-02-02 15:11:52 -080094 /**
95 * This is set if we treat the device as HDMI, instead of DVI.
96 */
97 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +080098
Ma Ling7086c872009-05-13 11:20:06 +080099 /**
100 * This is set if we detect output of sdvo device as LVDS.
101 */
102 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800103
104 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800105 * This is sdvo flags for input timing.
106 */
107 uint8_t sdvo_flags;
108
109 /**
110 * This is sdvo fixed pannel mode pointer
111 */
112 struct drm_display_mode *sdvo_lvds_fixed_mode;
113
114 /**
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800115 * Returned SDTV resolutions allowed for the current format, if the
116 * device reported it.
117 */
118 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
119
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800120 /*
121 * supported encoding mode, used to determine whether HDMI is
122 * supported
123 */
124 struct intel_sdvo_encode encode;
125
Eric Anholtc751ce42010-03-25 11:48:48 -0700126 /* DDC bus used by this SDVO encoder */
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800127 uint8_t ddc_bus;
128
Keith Packard57cdaf92009-09-04 13:07:54 +0800129 /* Mac mini hack -- use the same DDC as the analog connector */
130 struct i2c_adapter *analog_ddc_bus;
131
Jesse Barnes79e53942008-11-07 14:24:08 -0800132 int save_sdvo_mult;
133 u16 save_active_outputs;
134 struct intel_sdvo_dtd save_input_dtd_1, save_input_dtd_2;
135 struct intel_sdvo_dtd save_output_dtd[16];
136 u32 save_SDVOX;
Zhao Yakuib9219c52009-09-10 15:45:46 +0800137 /* add the property for the SDVO-TV */
138 struct drm_property *left_property;
139 struct drm_property *right_property;
140 struct drm_property *top_property;
141 struct drm_property *bottom_property;
142 struct drm_property *hpos_property;
143 struct drm_property *vpos_property;
144
145 /* add the property for the SDVO-TV/LVDS */
146 struct drm_property *brightness_property;
147 struct drm_property *contrast_property;
148 struct drm_property *saturation_property;
149 struct drm_property *hue_property;
150
151 /* Add variable to record current setting for the above property */
152 u32 left_margin, right_margin, top_margin, bottom_margin;
153 /* this is to get the range of margin.*/
154 u32 max_hscan, max_vscan;
155 u32 max_hpos, cur_hpos;
156 u32 max_vpos, cur_vpos;
157 u32 cur_brightness, max_brightness;
158 u32 cur_contrast, max_contrast;
159 u32 cur_saturation, max_saturation;
160 u32 cur_hue, max_hue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800161};
162
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800163static bool
Eric Anholt21d40d32010-03-25 11:11:14 -0700164intel_sdvo_output_setup(struct intel_encoder *intel_encoder, uint16_t flags);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800165
Jesse Barnes79e53942008-11-07 14:24:08 -0800166/**
167 * Writes the SDVOB or SDVOC with the given value, but always writes both
168 * SDVOB and SDVOC to work around apparent hardware issues (according to
169 * comments in the BIOS).
170 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700171static void intel_sdvo_write_sdvox(struct intel_encoder *intel_encoder, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800172{
Eric Anholt21d40d32010-03-25 11:11:14 -0700173 struct drm_device *dev = intel_encoder->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800174 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -0700175 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 u32 bval = val, cval = val;
177 int i;
178
Eric Anholtc751ce42010-03-25 11:48:48 -0700179 if (sdvo_priv->sdvo_reg == SDVOB) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800180 cval = I915_READ(SDVOC);
181 } else {
182 bval = I915_READ(SDVOB);
183 }
184 /*
185 * Write the registers twice for luck. Sometimes,
186 * writing them only once doesn't appear to 'stick'.
187 * The BIOS does this too. Yay, magic
188 */
189 for (i = 0; i < 2; i++)
190 {
191 I915_WRITE(SDVOB, bval);
192 I915_READ(SDVOB);
193 I915_WRITE(SDVOC, cval);
194 I915_READ(SDVOC);
195 }
196}
197
Eric Anholt21d40d32010-03-25 11:11:14 -0700198static bool intel_sdvo_read_byte(struct intel_encoder *intel_encoder, u8 addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800199 u8 *ch)
200{
Eric Anholt21d40d32010-03-25 11:11:14 -0700201 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800202 u8 out_buf[2];
203 u8 buf[2];
204 int ret;
205
206 struct i2c_msg msgs[] = {
207 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700208 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800209 .flags = 0,
210 .len = 1,
211 .buf = out_buf,
212 },
213 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700214 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800215 .flags = I2C_M_RD,
216 .len = 1,
217 .buf = buf,
218 }
219 };
220
221 out_buf[0] = addr;
222 out_buf[1] = 0;
223
Eric Anholt21d40d32010-03-25 11:11:14 -0700224 if ((ret = i2c_transfer(intel_encoder->i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800225 {
226 *ch = buf[0];
227 return true;
228 }
229
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800230 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800231 return false;
232}
233
Eric Anholt21d40d32010-03-25 11:11:14 -0700234static bool intel_sdvo_write_byte(struct intel_encoder *intel_encoder, int addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800235 u8 ch)
236{
Eric Anholt21d40d32010-03-25 11:11:14 -0700237 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800238 u8 out_buf[2];
239 struct i2c_msg msgs[] = {
240 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700241 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800242 .flags = 0,
243 .len = 2,
244 .buf = out_buf,
245 }
246 };
247
248 out_buf[0] = addr;
249 out_buf[1] = ch;
250
Eric Anholt21d40d32010-03-25 11:11:14 -0700251 if (i2c_transfer(intel_encoder->i2c_bus, msgs, 1) == 1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800252 {
253 return true;
254 }
255 return false;
256}
257
258#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
259/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100260static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800261 u8 cmd;
262 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800263} sdvo_cmd_names[] = {
264 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
265 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
266 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
267 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
268 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
269 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
270 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
271 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
272 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
273 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
274 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
275 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
276 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
277 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
278 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
279 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
280 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
281 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
282 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
283 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
289 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
290 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
291 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
292 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
Zhao Yakuib9219c52009-09-10 15:45:46 +0800307 /* Add the op code for SDVO enhancements */
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_POSITION_H),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POSITION_H),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_POSITION_H),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_POSITION_V),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POSITION_V),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_POSITION_V),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800332 /* HDMI op code */
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
342 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800353};
354
Eric Anholtc751ce42010-03-25 11:48:48 -0700355#define SDVO_NAME(dev_priv) ((dev_priv)->sdvo_reg == SDVOB ? "SDVOB" : "SDVOC")
356#define SDVO_PRIV(encoder) ((struct intel_sdvo_priv *) (encoder)->dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -0800357
Eric Anholt21d40d32010-03-25 11:11:14 -0700358static void intel_sdvo_debug_write(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800359 void *args, int args_len)
360{
Eric Anholt21d40d32010-03-25 11:11:14 -0700361 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800362 int i;
363
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800364 DRM_DEBUG_KMS("%s: W: %02X ",
yakui_zhao342dc382009-06-02 14:12:00 +0800365 SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800366 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800367 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800368 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800369 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800370 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
371 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800372 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800373 break;
374 }
375 }
376 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
yakui_zhao342dc382009-06-02 14:12:00 +0800377 DRM_LOG_KMS("(%02X)", cmd);
378 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800379}
Jesse Barnes79e53942008-11-07 14:24:08 -0800380
Eric Anholt21d40d32010-03-25 11:11:14 -0700381static void intel_sdvo_write_cmd(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800382 void *args, int args_len)
383{
384 int i;
385
Eric Anholt21d40d32010-03-25 11:11:14 -0700386 intel_sdvo_debug_write(intel_encoder, cmd, args, args_len);
Jesse Barnes79e53942008-11-07 14:24:08 -0800387
388 for (i = 0; i < args_len; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700389 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_ARG_0 - i,
Jesse Barnes79e53942008-11-07 14:24:08 -0800390 ((u8*)args)[i]);
391 }
392
Eric Anholt21d40d32010-03-25 11:11:14 -0700393 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_OPCODE, cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800394}
395
Jesse Barnes79e53942008-11-07 14:24:08 -0800396static const char *cmd_status_names[] = {
397 "Power on",
398 "Success",
399 "Not supported",
400 "Invalid arg",
401 "Pending",
402 "Target not specified",
403 "Scaling not supported"
404};
405
Eric Anholt21d40d32010-03-25 11:11:14 -0700406static void intel_sdvo_debug_response(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800407 void *response, int response_len,
408 u8 status)
409{
Eric Anholt21d40d32010-03-25 11:11:14 -0700410 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800411 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800412
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800413 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800414 for (i = 0; i < response_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800415 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800416 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800417 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800418 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800419 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800420 else
yakui_zhao342dc382009-06-02 14:12:00 +0800421 DRM_LOG_KMS("(??? %d)", status);
422 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800423}
Jesse Barnes79e53942008-11-07 14:24:08 -0800424
Eric Anholt21d40d32010-03-25 11:11:14 -0700425static u8 intel_sdvo_read_response(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 void *response, int response_len)
427{
428 int i;
429 u8 status;
430 u8 retry = 50;
431
432 while (retry--) {
433 /* Read the command response */
434 for (i = 0; i < response_len; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700435 intel_sdvo_read_byte(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800436 SDVO_I2C_RETURN_0 + i,
437 &((u8 *)response)[i]);
438 }
439
440 /* read the return status */
Eric Anholt21d40d32010-03-25 11:11:14 -0700441 intel_sdvo_read_byte(intel_encoder, SDVO_I2C_CMD_STATUS,
Jesse Barnes79e53942008-11-07 14:24:08 -0800442 &status);
443
Eric Anholt21d40d32010-03-25 11:11:14 -0700444 intel_sdvo_debug_response(intel_encoder, response, response_len,
Jesse Barnes79e53942008-11-07 14:24:08 -0800445 status);
446 if (status != SDVO_CMD_STATUS_PENDING)
447 return status;
448
449 mdelay(50);
450 }
451
452 return status;
453}
454
Hannes Ederb358d0a2008-12-18 21:18:47 +0100455static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800456{
457 if (mode->clock >= 100000)
458 return 1;
459 else if (mode->clock >= 50000)
460 return 2;
461 else
462 return 4;
463}
464
465/**
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800466 * Try to read the response after issuie the DDC switch command. But it
467 * is noted that we must do the action of reading response and issuing DDC
468 * switch command in one I2C transaction. Otherwise when we try to start
469 * another I2C transaction after issuing the DDC bus switch, it will be
470 * switched to the internal SDVO register.
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700472static void intel_sdvo_set_control_bus_switch(struct intel_encoder *intel_encoder,
Hannes Ederb358d0a2008-12-18 21:18:47 +0100473 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800474{
Eric Anholt21d40d32010-03-25 11:11:14 -0700475 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800476 u8 out_buf[2], cmd_buf[2], ret_value[2], ret;
477 struct i2c_msg msgs[] = {
478 {
479 .addr = sdvo_priv->slave_addr >> 1,
480 .flags = 0,
481 .len = 2,
482 .buf = out_buf,
483 },
484 /* the following two are to read the response */
485 {
486 .addr = sdvo_priv->slave_addr >> 1,
487 .flags = 0,
488 .len = 1,
489 .buf = cmd_buf,
490 },
491 {
492 .addr = sdvo_priv->slave_addr >> 1,
493 .flags = I2C_M_RD,
494 .len = 1,
495 .buf = ret_value,
496 },
497 };
498
Eric Anholt21d40d32010-03-25 11:11:14 -0700499 intel_sdvo_debug_write(intel_encoder, SDVO_CMD_SET_CONTROL_BUS_SWITCH,
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800500 &target, 1);
501 /* write the DDC switch command argument */
Eric Anholt21d40d32010-03-25 11:11:14 -0700502 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_ARG_0, target);
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800503
504 out_buf[0] = SDVO_I2C_OPCODE;
505 out_buf[1] = SDVO_CMD_SET_CONTROL_BUS_SWITCH;
506 cmd_buf[0] = SDVO_I2C_CMD_STATUS;
507 cmd_buf[1] = 0;
508 ret_value[0] = 0;
509 ret_value[1] = 0;
510
Eric Anholt21d40d32010-03-25 11:11:14 -0700511 ret = i2c_transfer(intel_encoder->i2c_bus, msgs, 3);
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800512 if (ret != 3) {
513 /* failure in I2C transfer */
514 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
515 return;
516 }
517 if (ret_value[0] != SDVO_CMD_STATUS_SUCCESS) {
518 DRM_DEBUG_KMS("DDC switch command returns response %d\n",
519 ret_value[0]);
520 return;
521 }
522 return;
Jesse Barnes79e53942008-11-07 14:24:08 -0800523}
524
Eric Anholt21d40d32010-03-25 11:11:14 -0700525static bool intel_sdvo_set_target_input(struct intel_encoder *intel_encoder, bool target_0, bool target_1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800526{
527 struct intel_sdvo_set_target_input_args targets = {0};
528 u8 status;
529
530 if (target_0 && target_1)
531 return SDVO_CMD_STATUS_NOTSUPP;
532
533 if (target_1)
534 targets.target_1 = 1;
535
Eric Anholt21d40d32010-03-25 11:11:14 -0700536 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TARGET_INPUT, &targets,
Jesse Barnes79e53942008-11-07 14:24:08 -0800537 sizeof(targets));
538
Eric Anholt21d40d32010-03-25 11:11:14 -0700539 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800540
541 return (status == SDVO_CMD_STATUS_SUCCESS);
542}
543
544/**
545 * Return whether each input is trained.
546 *
547 * This function is making an assumption about the layout of the response,
548 * which should be checked against the docs.
549 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700550static bool intel_sdvo_get_trained_inputs(struct intel_encoder *intel_encoder, bool *input_1, bool *input_2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800551{
552 struct intel_sdvo_get_trained_inputs_response response;
553 u8 status;
554
Eric Anholt21d40d32010-03-25 11:11:14 -0700555 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
556 status = intel_sdvo_read_response(intel_encoder, &response, sizeof(response));
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 if (status != SDVO_CMD_STATUS_SUCCESS)
558 return false;
559
560 *input_1 = response.input0_trained;
561 *input_2 = response.input1_trained;
562 return true;
563}
564
Eric Anholt21d40d32010-03-25 11:11:14 -0700565static bool intel_sdvo_get_active_outputs(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800566 u16 *outputs)
567{
568 u8 status;
569
Eric Anholt21d40d32010-03-25 11:11:14 -0700570 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_ACTIVE_OUTPUTS, NULL, 0);
571 status = intel_sdvo_read_response(intel_encoder, outputs, sizeof(*outputs));
Jesse Barnes79e53942008-11-07 14:24:08 -0800572
573 return (status == SDVO_CMD_STATUS_SUCCESS);
574}
575
Eric Anholt21d40d32010-03-25 11:11:14 -0700576static bool intel_sdvo_set_active_outputs(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800577 u16 outputs)
578{
579 u8 status;
580
Eric Anholt21d40d32010-03-25 11:11:14 -0700581 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 sizeof(outputs));
Eric Anholt21d40d32010-03-25 11:11:14 -0700583 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800584 return (status == SDVO_CMD_STATUS_SUCCESS);
585}
586
Eric Anholt21d40d32010-03-25 11:11:14 -0700587static bool intel_sdvo_set_encoder_power_state(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 int mode)
589{
590 u8 status, state = SDVO_ENCODER_STATE_ON;
591
592 switch (mode) {
593 case DRM_MODE_DPMS_ON:
594 state = SDVO_ENCODER_STATE_ON;
595 break;
596 case DRM_MODE_DPMS_STANDBY:
597 state = SDVO_ENCODER_STATE_STANDBY;
598 break;
599 case DRM_MODE_DPMS_SUSPEND:
600 state = SDVO_ENCODER_STATE_SUSPEND;
601 break;
602 case DRM_MODE_DPMS_OFF:
603 state = SDVO_ENCODER_STATE_OFF;
604 break;
605 }
606
Eric Anholt21d40d32010-03-25 11:11:14 -0700607 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 sizeof(state));
Eric Anholt21d40d32010-03-25 11:11:14 -0700609 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800610
611 return (status == SDVO_CMD_STATUS_SUCCESS);
612}
613
Eric Anholt21d40d32010-03-25 11:11:14 -0700614static bool intel_sdvo_get_input_pixel_clock_range(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800615 int *clock_min,
616 int *clock_max)
617{
618 struct intel_sdvo_pixel_clock_range clocks;
619 u8 status;
620
Eric Anholt21d40d32010-03-25 11:11:14 -0700621 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 NULL, 0);
623
Eric Anholt21d40d32010-03-25 11:11:14 -0700624 status = intel_sdvo_read_response(intel_encoder, &clocks, sizeof(clocks));
Jesse Barnes79e53942008-11-07 14:24:08 -0800625
626 if (status != SDVO_CMD_STATUS_SUCCESS)
627 return false;
628
629 /* Convert the values from units of 10 kHz to kHz. */
630 *clock_min = clocks.min * 10;
631 *clock_max = clocks.max * 10;
632
633 return true;
634}
635
Eric Anholt21d40d32010-03-25 11:11:14 -0700636static bool intel_sdvo_set_target_output(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800637 u16 outputs)
638{
639 u8 status;
640
Eric Anholt21d40d32010-03-25 11:11:14 -0700641 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 sizeof(outputs));
643
Eric Anholt21d40d32010-03-25 11:11:14 -0700644 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800645 return (status == SDVO_CMD_STATUS_SUCCESS);
646}
647
Eric Anholt21d40d32010-03-25 11:11:14 -0700648static bool intel_sdvo_get_timing(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 struct intel_sdvo_dtd *dtd)
650{
651 u8 status;
652
Eric Anholt21d40d32010-03-25 11:11:14 -0700653 intel_sdvo_write_cmd(intel_encoder, cmd, NULL, 0);
654 status = intel_sdvo_read_response(intel_encoder, &dtd->part1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 sizeof(dtd->part1));
656 if (status != SDVO_CMD_STATUS_SUCCESS)
657 return false;
658
Eric Anholt21d40d32010-03-25 11:11:14 -0700659 intel_sdvo_write_cmd(intel_encoder, cmd + 1, NULL, 0);
660 status = intel_sdvo_read_response(intel_encoder, &dtd->part2,
Jesse Barnes79e53942008-11-07 14:24:08 -0800661 sizeof(dtd->part2));
662 if (status != SDVO_CMD_STATUS_SUCCESS)
663 return false;
664
665 return true;
666}
667
Eric Anholt21d40d32010-03-25 11:11:14 -0700668static bool intel_sdvo_get_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 struct intel_sdvo_dtd *dtd)
670{
Eric Anholt21d40d32010-03-25 11:11:14 -0700671 return intel_sdvo_get_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800672 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
673}
674
Eric Anholt21d40d32010-03-25 11:11:14 -0700675static bool intel_sdvo_get_output_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800676 struct intel_sdvo_dtd *dtd)
677{
Eric Anholt21d40d32010-03-25 11:11:14 -0700678 return intel_sdvo_get_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800679 SDVO_CMD_GET_OUTPUT_TIMINGS_PART1, dtd);
680}
681
Eric Anholt21d40d32010-03-25 11:11:14 -0700682static bool intel_sdvo_set_timing(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 struct intel_sdvo_dtd *dtd)
684{
685 u8 status;
686
Eric Anholt21d40d32010-03-25 11:11:14 -0700687 intel_sdvo_write_cmd(intel_encoder, cmd, &dtd->part1, sizeof(dtd->part1));
688 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800689 if (status != SDVO_CMD_STATUS_SUCCESS)
690 return false;
691
Eric Anholt21d40d32010-03-25 11:11:14 -0700692 intel_sdvo_write_cmd(intel_encoder, cmd + 1, &dtd->part2, sizeof(dtd->part2));
693 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800694 if (status != SDVO_CMD_STATUS_SUCCESS)
695 return false;
696
697 return true;
698}
699
Eric Anholt21d40d32010-03-25 11:11:14 -0700700static bool intel_sdvo_set_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800701 struct intel_sdvo_dtd *dtd)
702{
Eric Anholt21d40d32010-03-25 11:11:14 -0700703 return intel_sdvo_set_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800704 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
705}
706
Eric Anholt21d40d32010-03-25 11:11:14 -0700707static bool intel_sdvo_set_output_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800708 struct intel_sdvo_dtd *dtd)
709{
Eric Anholt21d40d32010-03-25 11:11:14 -0700710 return intel_sdvo_set_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800711 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
712}
713
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800714static bool
Eric Anholtc751ce42010-03-25 11:48:48 -0700715intel_sdvo_create_preferred_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800716 uint16_t clock,
717 uint16_t width,
718 uint16_t height)
719{
720 struct intel_sdvo_preferred_input_timing_args args;
Eric Anholtc751ce42010-03-25 11:48:48 -0700721 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800722 uint8_t status;
723
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800724 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800725 args.clock = clock;
726 args.width = width;
727 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800728 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800729
730 if (sdvo_priv->is_lvds &&
731 (sdvo_priv->sdvo_lvds_fixed_mode->hdisplay != width ||
732 sdvo_priv->sdvo_lvds_fixed_mode->vdisplay != height))
733 args.scaled = 1;
734
Eric Anholtc751ce42010-03-25 11:48:48 -0700735 intel_sdvo_write_cmd(intel_encoder,
736 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800737 &args, sizeof(args));
Eric Anholtc751ce42010-03-25 11:48:48 -0700738 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800739 if (status != SDVO_CMD_STATUS_SUCCESS)
740 return false;
741
742 return true;
743}
744
Eric Anholtc751ce42010-03-25 11:48:48 -0700745static bool intel_sdvo_get_preferred_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800746 struct intel_sdvo_dtd *dtd)
747{
748 bool status;
749
Eric Anholtc751ce42010-03-25 11:48:48 -0700750 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800751 NULL, 0);
752
Eric Anholtc751ce42010-03-25 11:48:48 -0700753 status = intel_sdvo_read_response(intel_encoder, &dtd->part1,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800754 sizeof(dtd->part1));
755 if (status != SDVO_CMD_STATUS_SUCCESS)
756 return false;
757
Eric Anholtc751ce42010-03-25 11:48:48 -0700758 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800759 NULL, 0);
760
Eric Anholtc751ce42010-03-25 11:48:48 -0700761 status = intel_sdvo_read_response(intel_encoder, &dtd->part2,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800762 sizeof(dtd->part2));
763 if (status != SDVO_CMD_STATUS_SUCCESS)
764 return false;
765
766 return false;
767}
Jesse Barnes79e53942008-11-07 14:24:08 -0800768
Eric Anholt21d40d32010-03-25 11:11:14 -0700769static int intel_sdvo_get_clock_rate_mult(struct intel_encoder *intel_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -0800770{
771 u8 response, status;
772
Eric Anholt21d40d32010-03-25 11:11:14 -0700773 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_CLOCK_RATE_MULT, NULL, 0);
774 status = intel_sdvo_read_response(intel_encoder, &response, 1);
Jesse Barnes79e53942008-11-07 14:24:08 -0800775
776 if (status != SDVO_CMD_STATUS_SUCCESS) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800777 DRM_DEBUG_KMS("Couldn't get SDVO clock rate multiplier\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800778 return SDVO_CLOCK_RATE_MULT_1X;
779 } else {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800780 DRM_DEBUG_KMS("Current clock rate multiplier: %d\n", response);
Jesse Barnes79e53942008-11-07 14:24:08 -0800781 }
782
783 return response;
784}
785
Eric Anholt21d40d32010-03-25 11:11:14 -0700786static bool intel_sdvo_set_clock_rate_mult(struct intel_encoder *intel_encoder, u8 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800787{
788 u8 status;
789
Eric Anholt21d40d32010-03-25 11:11:14 -0700790 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
791 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800792 if (status != SDVO_CMD_STATUS_SUCCESS)
793 return false;
794
795 return true;
796}
797
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800798static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
799 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800800{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800801 uint16_t width, height;
802 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
803 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800804
805 width = mode->crtc_hdisplay;
806 height = mode->crtc_vdisplay;
807
808 /* do some mode translations */
809 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
810 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
811
812 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
813 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
814
815 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
816 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
817
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800818 dtd->part1.clock = mode->clock / 10;
819 dtd->part1.h_active = width & 0xff;
820 dtd->part1.h_blank = h_blank_len & 0xff;
821 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800822 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800823 dtd->part1.v_active = height & 0xff;
824 dtd->part1.v_blank = v_blank_len & 0xff;
825 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800826 ((v_blank_len >> 8) & 0xf);
827
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800828 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800829 dtd->part2.h_sync_width = h_sync_len & 0xff;
830 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800831 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800832 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800833 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
834 ((v_sync_len & 0x30) >> 4);
835
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800836 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800837 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800838 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800839 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800840 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800841
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800842 dtd->part2.sdvo_flags = 0;
843 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
844 dtd->part2.reserved = 0;
845}
Jesse Barnes79e53942008-11-07 14:24:08 -0800846
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800847static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
848 struct intel_sdvo_dtd *dtd)
849{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800850 mode->hdisplay = dtd->part1.h_active;
851 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
852 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800853 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800854 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
855 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
856 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
857 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
858
859 mode->vdisplay = dtd->part1.v_active;
860 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
861 mode->vsync_start = mode->vdisplay;
862 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800863 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800864 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
865 mode->vsync_end = mode->vsync_start +
866 (dtd->part2.v_sync_off_width & 0xf);
867 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
868 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
869 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
870
871 mode->clock = dtd->part1.clock * 10;
872
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800873 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800874 if (dtd->part2.dtd_flags & 0x2)
875 mode->flags |= DRM_MODE_FLAG_PHSYNC;
876 if (dtd->part2.dtd_flags & 0x4)
877 mode->flags |= DRM_MODE_FLAG_PVSYNC;
878}
879
Eric Anholtc751ce42010-03-25 11:48:48 -0700880static bool intel_sdvo_get_supp_encode(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800881 struct intel_sdvo_encode *encode)
882{
883 uint8_t status;
884
Eric Anholtc751ce42010-03-25 11:48:48 -0700885 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
886 status = intel_sdvo_read_response(intel_encoder, encode, sizeof(*encode));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800887 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
888 memset(encode, 0, sizeof(*encode));
889 return false;
890 }
891
892 return true;
893}
894
Eric Anholtc751ce42010-03-25 11:48:48 -0700895static bool intel_sdvo_set_encode(struct intel_encoder *intel_encoder,
896 uint8_t mode)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800897{
898 uint8_t status;
899
Eric Anholtc751ce42010-03-25 11:48:48 -0700900 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ENCODE, &mode, 1);
901 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800902
903 return (status == SDVO_CMD_STATUS_SUCCESS);
904}
905
Eric Anholtc751ce42010-03-25 11:48:48 -0700906static bool intel_sdvo_set_colorimetry(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800907 uint8_t mode)
908{
909 uint8_t status;
910
Eric Anholtc751ce42010-03-25 11:48:48 -0700911 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
912 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800913
914 return (status == SDVO_CMD_STATUS_SUCCESS);
915}
916
917#if 0
Eric Anholtc751ce42010-03-25 11:48:48 -0700918static void intel_sdvo_dump_hdmi_buf(struct intel_encoder *intel_encoder)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800919{
920 int i, j;
921 uint8_t set_buf_index[2];
922 uint8_t av_split;
923 uint8_t buf_size;
924 uint8_t buf[48];
925 uint8_t *pos;
926
Eric Anholtc751ce42010-03-25 11:48:48 -0700927 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
928 intel_sdvo_read_response(encoder, &av_split, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800929
930 for (i = 0; i <= av_split; i++) {
931 set_buf_index[0] = i; set_buf_index[1] = 0;
Eric Anholtc751ce42010-03-25 11:48:48 -0700932 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800933 set_buf_index, 2);
Eric Anholtc751ce42010-03-25 11:48:48 -0700934 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
935 intel_sdvo_read_response(encoder, &buf_size, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800936
937 pos = buf;
938 for (j = 0; j <= buf_size; j += 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700939 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800940 NULL, 0);
Eric Anholtc751ce42010-03-25 11:48:48 -0700941 intel_sdvo_read_response(encoder, pos, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800942 pos += 8;
943 }
944 }
945}
946#endif
947
Eric Anholtc751ce42010-03-25 11:48:48 -0700948static void intel_sdvo_set_hdmi_buf(struct intel_encoder *intel_encoder,
949 int index,
950 uint8_t *data, int8_t size, uint8_t tx_rate)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800951{
952 uint8_t set_buf_index[2];
953
954 set_buf_index[0] = index;
955 set_buf_index[1] = 0;
956
Eric Anholtc751ce42010-03-25 11:48:48 -0700957 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_INDEX,
958 set_buf_index, 2);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800959
960 for (; size > 0; size -= 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700961 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_DATA, data, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800962 data += 8;
963 }
964
Eric Anholtc751ce42010-03-25 11:48:48 -0700965 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800966}
967
968static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
969{
970 uint8_t csum = 0;
971 int i;
972
973 for (i = 0; i < size; i++)
974 csum += data[i];
975
976 return 0x100 - csum;
977}
978
979#define DIP_TYPE_AVI 0x82
980#define DIP_VERSION_AVI 0x2
981#define DIP_LEN_AVI 13
982
983struct dip_infoframe {
984 uint8_t type;
985 uint8_t version;
986 uint8_t len;
987 uint8_t checksum;
988 union {
989 struct {
990 /* Packet Byte #1 */
991 uint8_t S:2;
992 uint8_t B:2;
993 uint8_t A:1;
994 uint8_t Y:2;
995 uint8_t rsvd1:1;
996 /* Packet Byte #2 */
997 uint8_t R:4;
998 uint8_t M:2;
999 uint8_t C:2;
1000 /* Packet Byte #3 */
1001 uint8_t SC:2;
1002 uint8_t Q:2;
1003 uint8_t EC:3;
1004 uint8_t ITC:1;
1005 /* Packet Byte #4 */
1006 uint8_t VIC:7;
1007 uint8_t rsvd2:1;
1008 /* Packet Byte #5 */
1009 uint8_t PR:4;
1010 uint8_t rsvd3:4;
1011 /* Packet Byte #6~13 */
1012 uint16_t top_bar_end;
1013 uint16_t bottom_bar_start;
1014 uint16_t left_bar_end;
1015 uint16_t right_bar_start;
1016 } avi;
1017 struct {
1018 /* Packet Byte #1 */
1019 uint8_t channel_count:3;
1020 uint8_t rsvd1:1;
1021 uint8_t coding_type:4;
1022 /* Packet Byte #2 */
1023 uint8_t sample_size:2; /* SS0, SS1 */
1024 uint8_t sample_frequency:3;
1025 uint8_t rsvd2:3;
1026 /* Packet Byte #3 */
1027 uint8_t coding_type_private:5;
1028 uint8_t rsvd3:3;
1029 /* Packet Byte #4 */
1030 uint8_t channel_allocation;
1031 /* Packet Byte #5 */
1032 uint8_t rsvd4:3;
1033 uint8_t level_shift:4;
1034 uint8_t downmix_inhibit:1;
1035 } audio;
1036 uint8_t payload[28];
1037 } __attribute__ ((packed)) u;
1038} __attribute__((packed));
1039
Eric Anholtc751ce42010-03-25 11:48:48 -07001040static void intel_sdvo_set_avi_infoframe(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001041 struct drm_display_mode * mode)
1042{
1043 struct dip_infoframe avi_if = {
1044 .type = DIP_TYPE_AVI,
1045 .version = DIP_VERSION_AVI,
1046 .len = DIP_LEN_AVI,
1047 };
1048
1049 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
1050 4 + avi_if.len);
Eric Anholtc751ce42010-03-25 11:48:48 -07001051 intel_sdvo_set_hdmi_buf(intel_encoder, 1, (uint8_t *)&avi_if,
1052 4 + avi_if.len,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001053 SDVO_HBUF_TX_VSYNC);
1054}
1055
Eric Anholtc751ce42010-03-25 11:48:48 -07001056static void intel_sdvo_set_tv_format(struct intel_encoder *intel_encoder)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001057{
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001058
Zhao Yakuice6feab2009-08-24 13:50:26 +08001059 struct intel_sdvo_tv_format format;
Eric Anholtc751ce42010-03-25 11:48:48 -07001060 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001061 uint32_t format_map, i;
1062 uint8_t status;
1063
1064 for (i = 0; i < TV_FORMAT_NUM; i++)
1065 if (tv_format_names[i] == sdvo_priv->tv_format_name)
1066 break;
1067
1068 format_map = 1 << i;
1069 memset(&format, 0, sizeof(format));
1070 memcpy(&format, &format_map, sizeof(format_map) > sizeof(format) ?
1071 sizeof(format) : sizeof(format_map));
1072
Eric Anholtc751ce42010-03-25 11:48:48 -07001073 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TV_FORMAT, &format_map,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001074 sizeof(format));
1075
Eric Anholtc751ce42010-03-25 11:48:48 -07001076 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Zhao Yakuice6feab2009-08-24 13:50:26 +08001077 if (status != SDVO_CMD_STATUS_SUCCESS)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001078 DRM_DEBUG_KMS("%s: Failed to set TV format\n",
Zhao Yakuice6feab2009-08-24 13:50:26 +08001079 SDVO_NAME(sdvo_priv));
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001080}
1081
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001082static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
1083 struct drm_display_mode *mode,
1084 struct drm_display_mode *adjusted_mode)
1085{
Eric Anholtc751ce42010-03-25 11:48:48 -07001086 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1087 struct intel_sdvo_priv *dev_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001088
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001089 if (dev_priv->is_tv) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001090 struct intel_sdvo_dtd output_dtd;
1091 bool success;
1092
1093 /* We need to construct preferred input timings based on our
1094 * output timings. To do that, we have to set the output
1095 * timings, even though this isn't really the right place in
1096 * the sequence to do it. Oh well.
1097 */
1098
1099
1100 /* Set output timings */
1101 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
Eric Anholtc751ce42010-03-25 11:48:48 -07001102 intel_sdvo_set_target_output(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001103 dev_priv->controlled_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001104 intel_sdvo_set_output_timing(intel_encoder, &output_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001105
1106 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001107 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001108
1109
Eric Anholtc751ce42010-03-25 11:48:48 -07001110 success = intel_sdvo_create_preferred_input_timing(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001111 mode->clock / 10,
1112 mode->hdisplay,
1113 mode->vdisplay);
1114 if (success) {
1115 struct intel_sdvo_dtd input_dtd;
1116
Eric Anholtc751ce42010-03-25 11:48:48 -07001117 intel_sdvo_get_preferred_input_timing(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001118 &input_dtd);
1119 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001120 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001121
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001122 drm_mode_set_crtcinfo(adjusted_mode, 0);
1123
1124 mode->clock = adjusted_mode->clock;
1125
1126 adjusted_mode->clock *=
1127 intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001128 } else {
1129 return false;
1130 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001131 } else if (dev_priv->is_lvds) {
1132 struct intel_sdvo_dtd output_dtd;
1133 bool success;
1134
1135 drm_mode_set_crtcinfo(dev_priv->sdvo_lvds_fixed_mode, 0);
1136 /* Set output timings */
1137 intel_sdvo_get_dtd_from_mode(&output_dtd,
1138 dev_priv->sdvo_lvds_fixed_mode);
1139
Eric Anholtc751ce42010-03-25 11:48:48 -07001140 intel_sdvo_set_target_output(intel_encoder,
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001141 dev_priv->controlled_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001142 intel_sdvo_set_output_timing(intel_encoder, &output_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001143
1144 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001145 intel_sdvo_set_target_input(intel_encoder, true, false);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001146
1147
1148 success = intel_sdvo_create_preferred_input_timing(
Eric Anholtc751ce42010-03-25 11:48:48 -07001149 intel_encoder,
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001150 mode->clock / 10,
1151 mode->hdisplay,
1152 mode->vdisplay);
1153
1154 if (success) {
1155 struct intel_sdvo_dtd input_dtd;
1156
Eric Anholtc751ce42010-03-25 11:48:48 -07001157 intel_sdvo_get_preferred_input_timing(intel_encoder,
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001158 &input_dtd);
1159 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1160 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
1161
1162 drm_mode_set_crtcinfo(adjusted_mode, 0);
1163
1164 mode->clock = adjusted_mode->clock;
1165
1166 adjusted_mode->clock *=
1167 intel_sdvo_get_pixel_multiplier(mode);
1168 } else {
1169 return false;
1170 }
1171
1172 } else {
1173 /* Make the CRTC code factor in the SDVO pixel multiplier. The
1174 * SDVO device will be told of the multiplier during mode_set.
1175 */
1176 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001177 }
1178 return true;
1179}
1180
1181static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1182 struct drm_display_mode *mode,
1183 struct drm_display_mode *adjusted_mode)
1184{
1185 struct drm_device *dev = encoder->dev;
1186 struct drm_i915_private *dev_priv = dev->dev_private;
1187 struct drm_crtc *crtc = encoder->crtc;
1188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07001189 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1190 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001191 u32 sdvox = 0;
1192 int sdvo_pixel_multiply;
1193 struct intel_sdvo_in_out_map in_out;
1194 struct intel_sdvo_dtd input_dtd;
1195 u8 status;
1196
1197 if (!mode)
1198 return;
1199
1200 /* First, set the input mapping for the first input to our controlled
1201 * output. This is only correct if we're a single-input device, in
1202 * which case the first input is the output from the appropriate SDVO
1203 * channel on the motherboard. In a two-input device, the first input
1204 * will be SDVOB and the second SDVOC.
1205 */
1206 in_out.in0 = sdvo_priv->controlled_output;
1207 in_out.in1 = 0;
1208
Eric Anholtc751ce42010-03-25 11:48:48 -07001209 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_IN_OUT_MAP,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001210 &in_out, sizeof(in_out));
Eric Anholtc751ce42010-03-25 11:48:48 -07001211 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001212
1213 if (sdvo_priv->is_hdmi) {
Eric Anholtc751ce42010-03-25 11:48:48 -07001214 intel_sdvo_set_avi_infoframe(intel_encoder, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001215 sdvox |= SDVO_AUDIO_ENABLE;
1216 }
1217
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001218 /* We have tried to get input timing in mode_fixup, and filled into
1219 adjusted_mode */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001220 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001221 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001222 input_dtd.part2.sdvo_flags = sdvo_priv->sdvo_flags;
1223 } else
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001224 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001225
1226 /* If it's a TV, we already set the output timing in mode_fixup.
1227 * Otherwise, the output timing is equal to the input timing.
1228 */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001229 if (!sdvo_priv->is_tv && !sdvo_priv->is_lvds) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001230 /* Set the output timing to the screen */
Eric Anholtc751ce42010-03-25 11:48:48 -07001231 intel_sdvo_set_target_output(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001232 sdvo_priv->controlled_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001233 intel_sdvo_set_output_timing(intel_encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001234 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001235
1236 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001237 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001238
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001239 if (sdvo_priv->is_tv)
Eric Anholtc751ce42010-03-25 11:48:48 -07001240 intel_sdvo_set_tv_format(intel_encoder);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001241
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001242 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001243 * provide the device with a timing it can support, if it supports that
1244 * feature. However, presumably we would need to adjust the CRTC to
1245 * output the preferred timing, and we don't support that currently.
1246 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001247#if 0
Eric Anholtc751ce42010-03-25 11:48:48 -07001248 success = intel_sdvo_create_preferred_input_timing(encoder, clock,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001249 width, height);
1250 if (success) {
1251 struct intel_sdvo_dtd *input_dtd;
1252
Eric Anholtc751ce42010-03-25 11:48:48 -07001253 intel_sdvo_get_preferred_input_timing(encoder, &input_dtd);
1254 intel_sdvo_set_input_timing(encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001255 }
1256#else
Eric Anholtc751ce42010-03-25 11:48:48 -07001257 intel_sdvo_set_input_timing(intel_encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001258#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001259
1260 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1261 case 1:
Eric Anholtc751ce42010-03-25 11:48:48 -07001262 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001263 SDVO_CLOCK_RATE_MULT_1X);
1264 break;
1265 case 2:
Eric Anholtc751ce42010-03-25 11:48:48 -07001266 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001267 SDVO_CLOCK_RATE_MULT_2X);
1268 break;
1269 case 4:
Eric Anholtc751ce42010-03-25 11:48:48 -07001270 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001271 SDVO_CLOCK_RATE_MULT_4X);
1272 break;
1273 }
1274
1275 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001276 if (IS_I965G(dev)) {
1277 sdvox |= SDVO_BORDER_ENABLE |
1278 SDVO_VSYNC_ACTIVE_HIGH |
1279 SDVO_HSYNC_ACTIVE_HIGH;
1280 } else {
Eric Anholtc751ce42010-03-25 11:48:48 -07001281 sdvox |= I915_READ(sdvo_priv->sdvo_reg);
1282 switch (sdvo_priv->sdvo_reg) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001283 case SDVOB:
1284 sdvox &= SDVOB_PRESERVE_MASK;
1285 break;
1286 case SDVOC:
1287 sdvox &= SDVOC_PRESERVE_MASK;
1288 break;
1289 }
1290 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1291 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001292 if (intel_crtc->pipe == 1)
1293 sdvox |= SDVO_PIPE_B_SELECT;
1294
1295 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1296 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001297 /* done in crtc_mode_set as the dpll_md reg must be written early */
1298 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1299 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001300 } else {
1301 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1302 }
1303
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001304 if (sdvo_priv->sdvo_flags & SDVO_NEED_TO_STALL)
1305 sdvox |= SDVO_STALL_SELECT;
Eric Anholtc751ce42010-03-25 11:48:48 -07001306 intel_sdvo_write_sdvox(intel_encoder, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001307}
1308
1309static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1310{
1311 struct drm_device *dev = encoder->dev;
1312 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001313 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1314 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001315 u32 temp;
1316
1317 if (mode != DRM_MODE_DPMS_ON) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001318 intel_sdvo_set_active_outputs(intel_encoder, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08001319 if (0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001320 intel_sdvo_set_encoder_power_state(intel_encoder, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08001321
1322 if (mode == DRM_MODE_DPMS_OFF) {
Eric Anholtc751ce42010-03-25 11:48:48 -07001323 temp = I915_READ(sdvo_priv->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001324 if ((temp & SDVO_ENABLE) != 0) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001325 intel_sdvo_write_sdvox(intel_encoder, temp & ~SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001326 }
1327 }
1328 } else {
1329 bool input1, input2;
1330 int i;
1331 u8 status;
1332
Eric Anholtc751ce42010-03-25 11:48:48 -07001333 temp = I915_READ(sdvo_priv->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001334 if ((temp & SDVO_ENABLE) == 0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001335 intel_sdvo_write_sdvox(intel_encoder, temp | SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001336 for (i = 0; i < 2; i++)
1337 intel_wait_for_vblank(dev);
1338
Eric Anholt21d40d32010-03-25 11:11:14 -07001339 status = intel_sdvo_get_trained_inputs(intel_encoder, &input1,
Jesse Barnes79e53942008-11-07 14:24:08 -08001340 &input2);
1341
1342
1343 /* Warn if the device reported failure to sync.
1344 * A lot of SDVO devices fail to notify of sync, but it's
1345 * a given it the status is a success, we succeeded.
1346 */
1347 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001348 DRM_DEBUG_KMS("First %s output reported failure to "
1349 "sync\n", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -08001350 }
1351
1352 if (0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001353 intel_sdvo_set_encoder_power_state(intel_encoder, mode);
1354 intel_sdvo_set_active_outputs(intel_encoder, sdvo_priv->controlled_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001355 }
1356 return;
1357}
1358
1359static void intel_sdvo_save(struct drm_connector *connector)
1360{
1361 struct drm_device *dev = connector->dev;
1362 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001363 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1364 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001365 int o;
1366
Eric Anholt21d40d32010-03-25 11:11:14 -07001367 sdvo_priv->save_sdvo_mult = intel_sdvo_get_clock_rate_mult(intel_encoder);
1368 intel_sdvo_get_active_outputs(intel_encoder, &sdvo_priv->save_active_outputs);
Jesse Barnes79e53942008-11-07 14:24:08 -08001369
1370 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001371 intel_sdvo_set_target_input(intel_encoder, true, false);
1372 intel_sdvo_get_input_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001373 &sdvo_priv->save_input_dtd_1);
1374 }
1375
1376 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001377 intel_sdvo_set_target_input(intel_encoder, false, true);
1378 intel_sdvo_get_input_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001379 &sdvo_priv->save_input_dtd_2);
1380 }
1381
1382 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1383 {
1384 u16 this_output = (1 << o);
1385 if (sdvo_priv->caps.output_flags & this_output)
1386 {
Eric Anholt21d40d32010-03-25 11:11:14 -07001387 intel_sdvo_set_target_output(intel_encoder, this_output);
1388 intel_sdvo_get_output_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001389 &sdvo_priv->save_output_dtd[o]);
1390 }
1391 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001392 if (sdvo_priv->is_tv) {
1393 /* XXX: Save TV format/enhancements. */
1394 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001395
Eric Anholtc751ce42010-03-25 11:48:48 -07001396 sdvo_priv->save_SDVOX = I915_READ(sdvo_priv->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001397}
1398
1399static void intel_sdvo_restore(struct drm_connector *connector)
1400{
1401 struct drm_device *dev = connector->dev;
Eric Anholt21d40d32010-03-25 11:11:14 -07001402 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1403 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001404 int o;
1405 int i;
1406 bool input1, input2;
1407 u8 status;
1408
Eric Anholt21d40d32010-03-25 11:11:14 -07001409 intel_sdvo_set_active_outputs(intel_encoder, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08001410
1411 for (o = SDVO_OUTPUT_FIRST; o <= SDVO_OUTPUT_LAST; o++)
1412 {
1413 u16 this_output = (1 << o);
1414 if (sdvo_priv->caps.output_flags & this_output) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001415 intel_sdvo_set_target_output(intel_encoder, this_output);
1416 intel_sdvo_set_output_timing(intel_encoder, &sdvo_priv->save_output_dtd[o]);
Jesse Barnes79e53942008-11-07 14:24:08 -08001417 }
1418 }
1419
1420 if (sdvo_priv->caps.sdvo_inputs_mask & 0x1) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001421 intel_sdvo_set_target_input(intel_encoder, true, false);
1422 intel_sdvo_set_input_timing(intel_encoder, &sdvo_priv->save_input_dtd_1);
Jesse Barnes79e53942008-11-07 14:24:08 -08001423 }
1424
1425 if (sdvo_priv->caps.sdvo_inputs_mask & 0x2) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001426 intel_sdvo_set_target_input(intel_encoder, false, true);
1427 intel_sdvo_set_input_timing(intel_encoder, &sdvo_priv->save_input_dtd_2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001428 }
1429
Eric Anholt21d40d32010-03-25 11:11:14 -07001430 intel_sdvo_set_clock_rate_mult(intel_encoder, sdvo_priv->save_sdvo_mult);
Jesse Barnes79e53942008-11-07 14:24:08 -08001431
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001432 if (sdvo_priv->is_tv) {
1433 /* XXX: Restore TV format/enhancements. */
1434 }
1435
Eric Anholt21d40d32010-03-25 11:11:14 -07001436 intel_sdvo_write_sdvox(intel_encoder, sdvo_priv->save_SDVOX);
Jesse Barnes79e53942008-11-07 14:24:08 -08001437
1438 if (sdvo_priv->save_SDVOX & SDVO_ENABLE)
1439 {
1440 for (i = 0; i < 2; i++)
1441 intel_wait_for_vblank(dev);
Eric Anholt21d40d32010-03-25 11:11:14 -07001442 status = intel_sdvo_get_trained_inputs(intel_encoder, &input1, &input2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001443 if (status == SDVO_CMD_STATUS_SUCCESS && !input1)
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001444 DRM_DEBUG_KMS("First %s output reported failure to "
1445 "sync\n", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -08001446 }
1447
Eric Anholt21d40d32010-03-25 11:11:14 -07001448 intel_sdvo_set_active_outputs(intel_encoder, sdvo_priv->save_active_outputs);
Jesse Barnes79e53942008-11-07 14:24:08 -08001449}
1450
1451static int intel_sdvo_mode_valid(struct drm_connector *connector,
1452 struct drm_display_mode *mode)
1453{
Eric Anholt21d40d32010-03-25 11:11:14 -07001454 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1455 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001456
1457 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1458 return MODE_NO_DBLESCAN;
1459
1460 if (sdvo_priv->pixel_clock_min > mode->clock)
1461 return MODE_CLOCK_LOW;
1462
1463 if (sdvo_priv->pixel_clock_max < mode->clock)
1464 return MODE_CLOCK_HIGH;
1465
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001466 if (sdvo_priv->is_lvds == true) {
1467 if (sdvo_priv->sdvo_lvds_fixed_mode == NULL)
1468 return MODE_PANEL;
1469
1470 if (mode->hdisplay > sdvo_priv->sdvo_lvds_fixed_mode->hdisplay)
1471 return MODE_PANEL;
1472
1473 if (mode->vdisplay > sdvo_priv->sdvo_lvds_fixed_mode->vdisplay)
1474 return MODE_PANEL;
1475 }
1476
Jesse Barnes79e53942008-11-07 14:24:08 -08001477 return MODE_OK;
1478}
1479
Eric Anholt21d40d32010-03-25 11:11:14 -07001480static bool intel_sdvo_get_capabilities(struct intel_encoder *intel_encoder, struct intel_sdvo_caps *caps)
Jesse Barnes79e53942008-11-07 14:24:08 -08001481{
1482 u8 status;
1483
Eric Anholt21d40d32010-03-25 11:11:14 -07001484 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1485 status = intel_sdvo_read_response(intel_encoder, caps, sizeof(*caps));
Jesse Barnes79e53942008-11-07 14:24:08 -08001486 if (status != SDVO_CMD_STATUS_SUCCESS)
1487 return false;
1488
1489 return true;
1490}
1491
1492struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1493{
1494 struct drm_connector *connector = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07001495 struct intel_encoder *iout = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001496 struct intel_sdvo_priv *sdvo;
1497
1498 /* find the sdvo connector */
1499 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001500 iout = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001501
1502 if (iout->type != INTEL_OUTPUT_SDVO)
1503 continue;
1504
1505 sdvo = iout->dev_priv;
1506
Eric Anholtc751ce42010-03-25 11:48:48 -07001507 if (sdvo->sdvo_reg == SDVOB && sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001508 return connector;
1509
Eric Anholtc751ce42010-03-25 11:48:48 -07001510 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001511 return connector;
1512
1513 }
1514
1515 return NULL;
1516}
1517
1518int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1519{
1520 u8 response[2];
1521 u8 status;
Eric Anholt21d40d32010-03-25 11:11:14 -07001522 struct intel_encoder *intel_encoder;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001523 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08001524
1525 if (!connector)
1526 return 0;
1527
Eric Anholt21d40d32010-03-25 11:11:14 -07001528 intel_encoder = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001529
Eric Anholt21d40d32010-03-25 11:11:14 -07001530 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1531 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001532
1533 if (response[0] !=0)
1534 return 1;
1535
1536 return 0;
1537}
1538
1539void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1540{
1541 u8 response[2];
1542 u8 status;
Eric Anholt21d40d32010-03-25 11:11:14 -07001543 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001544
Eric Anholt21d40d32010-03-25 11:11:14 -07001545 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1546 intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001547
1548 if (on) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001549 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1550 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001551
Eric Anholt21d40d32010-03-25 11:11:14 -07001552 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001553 } else {
1554 response[0] = 0;
1555 response[1] = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -07001556 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001557 }
1558
Eric Anholt21d40d32010-03-25 11:11:14 -07001559 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1560 intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001561}
1562
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001563static bool
Eric Anholt21d40d32010-03-25 11:11:14 -07001564intel_sdvo_multifunc_encoder(struct intel_encoder *intel_encoder)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001565{
Eric Anholt21d40d32010-03-25 11:11:14 -07001566 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001567 int caps = 0;
1568
1569 if (sdvo_priv->caps.output_flags &
1570 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1571 caps++;
1572 if (sdvo_priv->caps.output_flags &
1573 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1574 caps++;
1575 if (sdvo_priv->caps.output_flags &
Roel Kluin19e1f882009-08-09 13:50:53 +02001576 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID1))
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001577 caps++;
1578 if (sdvo_priv->caps.output_flags &
1579 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1580 caps++;
1581 if (sdvo_priv->caps.output_flags &
1582 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1583 caps++;
1584
1585 if (sdvo_priv->caps.output_flags &
1586 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1587 caps++;
1588
1589 if (sdvo_priv->caps.output_flags &
1590 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1591 caps++;
1592
1593 return (caps > 1);
1594}
1595
Keith Packard57cdaf92009-09-04 13:07:54 +08001596static struct drm_connector *
1597intel_find_analog_connector(struct drm_device *dev)
1598{
1599 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -07001600 struct intel_encoder *intel_encoder;
Keith Packard57cdaf92009-09-04 13:07:54 +08001601
1602 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001603 intel_encoder = to_intel_encoder(connector);
1604 if (intel_encoder->type == INTEL_OUTPUT_ANALOG)
Keith Packard57cdaf92009-09-04 13:07:54 +08001605 return connector;
1606 }
1607 return NULL;
1608}
1609
1610static int
1611intel_analog_is_connected(struct drm_device *dev)
1612{
1613 struct drm_connector *analog_connector;
1614 analog_connector = intel_find_analog_connector(dev);
1615
1616 if (!analog_connector)
1617 return false;
1618
1619 if (analog_connector->funcs->detect(analog_connector) ==
1620 connector_status_disconnected)
1621 return false;
1622
1623 return true;
1624}
1625
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001626enum drm_connector_status
1627intel_sdvo_hdmi_sink_detect(struct drm_connector *connector, u16 response)
Ma Ling9dff6af2009-04-02 13:13:26 +08001628{
Eric Anholt21d40d32010-03-25 11:11:14 -07001629 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1630 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001631 enum drm_connector_status status = connector_status_connected;
Ma Ling9dff6af2009-04-02 13:13:26 +08001632 struct edid *edid = NULL;
1633
Eric Anholt21d40d32010-03-25 11:11:14 -07001634 edid = drm_get_edid(&intel_encoder->base,
1635 intel_encoder->ddc_bus);
Keith Packard57cdaf92009-09-04 13:07:54 +08001636
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001637 /* This is only applied to SDVO cards with multiple outputs */
Eric Anholt21d40d32010-03-25 11:11:14 -07001638 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_encoder)) {
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001639 uint8_t saved_ddc, temp_ddc;
1640 saved_ddc = sdvo_priv->ddc_bus;
1641 temp_ddc = sdvo_priv->ddc_bus >> 1;
1642 /*
1643 * Don't use the 1 as the argument of DDC bus switch to get
1644 * the EDID. It is used for SDVO SPD ROM.
1645 */
1646 while(temp_ddc > 1) {
1647 sdvo_priv->ddc_bus = temp_ddc;
Eric Anholt21d40d32010-03-25 11:11:14 -07001648 edid = drm_get_edid(&intel_encoder->base,
1649 intel_encoder->ddc_bus);
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001650 if (edid) {
1651 /*
1652 * When we can get the EDID, maybe it is the
1653 * correct DDC bus. Update it.
1654 */
1655 sdvo_priv->ddc_bus = temp_ddc;
1656 break;
1657 }
1658 temp_ddc >>= 1;
1659 }
1660 if (edid == NULL)
1661 sdvo_priv->ddc_bus = saved_ddc;
1662 }
Keith Packard57cdaf92009-09-04 13:07:54 +08001663 /* when there is no edid and no monitor is connected with VGA
1664 * port, try to use the CRT ddc to read the EDID for DVI-connector
1665 */
1666 if (edid == NULL &&
1667 sdvo_priv->analog_ddc_bus &&
Eric Anholt21d40d32010-03-25 11:11:14 -07001668 !intel_analog_is_connected(intel_encoder->base.dev))
1669 edid = drm_get_edid(&intel_encoder->base,
Keith Packard57cdaf92009-09-04 13:07:54 +08001670 sdvo_priv->analog_ddc_bus);
Ma Ling9dff6af2009-04-02 13:13:26 +08001671 if (edid != NULL) {
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001672 /* Don't report the output as connected if it's a DVI-I
1673 * connector with a non-digital EDID coming out.
1674 */
1675 if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1676 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1677 sdvo_priv->is_hdmi =
1678 drm_detect_hdmi_monitor(edid);
1679 else
1680 status = connector_status_disconnected;
1681 }
1682
Ma Ling9dff6af2009-04-02 13:13:26 +08001683 kfree(edid);
Eric Anholt21d40d32010-03-25 11:11:14 -07001684 intel_encoder->base.display_info.raw_edid = NULL;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001685
1686 } else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1687 status = connector_status_disconnected;
1688
1689 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001690}
1691
Jesse Barnes79e53942008-11-07 14:24:08 -08001692static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1693{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001694 uint16_t response;
Jesse Barnes79e53942008-11-07 14:24:08 -08001695 u8 status;
Eric Anholt21d40d32010-03-25 11:11:14 -07001696 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1697 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001698
Eric Anholt21d40d32010-03-25 11:11:14 -07001699 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001700 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
Zhao Yakuid09c23d2009-11-06 15:39:56 +08001701 if (sdvo_priv->is_tv) {
1702 /* add 30ms delay when the output type is SDVO-TV */
1703 mdelay(30);
1704 }
Eric Anholt21d40d32010-03-25 11:11:14 -07001705 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001706
Dave Airlie51c8b402009-08-20 13:38:04 +10001707 DRM_DEBUG_KMS("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001708
1709 if (status != SDVO_CMD_STATUS_SUCCESS)
1710 return connector_status_unknown;
1711
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001712 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001713 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001714
Eric Anholt21d40d32010-03-25 11:11:14 -07001715 if (intel_sdvo_multifunc_encoder(intel_encoder) &&
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001716 sdvo_priv->attached_output != response) {
1717 if (sdvo_priv->controlled_output != response &&
Eric Anholt21d40d32010-03-25 11:11:14 -07001718 intel_sdvo_output_setup(intel_encoder, response) != true)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001719 return connector_status_unknown;
1720 sdvo_priv->attached_output = response;
1721 }
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001722 return intel_sdvo_hdmi_sink_detect(connector, response);
Jesse Barnes79e53942008-11-07 14:24:08 -08001723}
1724
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001725static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001726{
Eric Anholt21d40d32010-03-25 11:11:14 -07001727 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1728 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Keith Packard57cdaf92009-09-04 13:07:54 +08001729 int num_modes;
Jesse Barnes79e53942008-11-07 14:24:08 -08001730
1731 /* set the bus switch and get the modes */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001732 num_modes = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Jesse Barnes79e53942008-11-07 14:24:08 -08001733
Keith Packard57cdaf92009-09-04 13:07:54 +08001734 /*
1735 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1736 * link between analog and digital outputs. So, if the regular SDVO
1737 * DDC fails, check to see if the analog output is disconnected, in
1738 * which case we'll look there for the digital DDC data.
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001739 */
Keith Packard57cdaf92009-09-04 13:07:54 +08001740 if (num_modes == 0 &&
1741 sdvo_priv->analog_ddc_bus &&
Eric Anholt21d40d32010-03-25 11:11:14 -07001742 !intel_analog_is_connected(intel_encoder->base.dev)) {
Keith Packard57cdaf92009-09-04 13:07:54 +08001743 /* Switch to the analog ddc bus and try that
1744 */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001745 (void) intel_ddc_get_modes(connector, sdvo_priv->analog_ddc_bus);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001746 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001747}
1748
1749/*
1750 * Set of SDVO TV modes.
1751 * Note! This is in reply order (see loop in get_tv_modes).
1752 * XXX: all 60Hz refresh?
1753 */
1754struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001755 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1756 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001757 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001758 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1759 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001760 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001761 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1762 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001763 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001764 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1765 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001766 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001767 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1768 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001769 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001770 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1771 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001772 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001773 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1774 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001775 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001776 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1777 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001778 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001779 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1780 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001781 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001782 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1783 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001784 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001785 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1786 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001787 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001788 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1789 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001790 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001791 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1792 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001793 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001794 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1795 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001796 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001797 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1798 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001799 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001800 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1801 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001802 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001803 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1804 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001805 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001806 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1807 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001808 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001809 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1810 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001811 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1812};
1813
1814static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1815{
Eric Anholt21d40d32010-03-25 11:11:14 -07001816 struct intel_encoder *output = to_intel_encoder(connector);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001817 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1818 struct intel_sdvo_sdtv_resolution_request tv_res;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001819 uint32_t reply = 0, format_map = 0;
1820 int i;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001821 uint8_t status;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001822
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001823
1824 /* Read the list of supported input resolutions for the selected TV
1825 * format.
1826 */
Zhao Yakuice6feab2009-08-24 13:50:26 +08001827 for (i = 0; i < TV_FORMAT_NUM; i++)
1828 if (tv_format_names[i] == sdvo_priv->tv_format_name)
1829 break;
1830
1831 format_map = (1 << i);
1832 memcpy(&tv_res, &format_map,
1833 sizeof(struct intel_sdvo_sdtv_resolution_request) >
1834 sizeof(format_map) ? sizeof(format_map) :
1835 sizeof(struct intel_sdvo_sdtv_resolution_request));
1836
1837 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
1838
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001839 intel_sdvo_write_cmd(output, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001840 &tv_res, sizeof(tv_res));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001841 status = intel_sdvo_read_response(output, &reply, 3);
1842 if (status != SDVO_CMD_STATUS_SUCCESS)
1843 return;
1844
1845 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001846 if (reply & (1 << i)) {
1847 struct drm_display_mode *nmode;
1848 nmode = drm_mode_duplicate(connector->dev,
1849 &sdvo_tv_modes[i]);
1850 if (nmode)
1851 drm_mode_probed_add(connector, nmode);
1852 }
Zhao Yakuice6feab2009-08-24 13:50:26 +08001853
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001854}
1855
Ma Ling7086c872009-05-13 11:20:06 +08001856static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1857{
Eric Anholt21d40d32010-03-25 11:11:14 -07001858 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001859 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001860 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001861 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001862
1863 /*
1864 * Attempt to get the mode list from DDC.
1865 * Assume that the preferred modes are
1866 * arranged in priority order.
1867 */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001868 intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Ma Ling7086c872009-05-13 11:20:06 +08001869 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001870 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001871
1872 /* Fetch modes from VBT */
1873 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001874 newmode = drm_mode_duplicate(connector->dev,
1875 dev_priv->sdvo_lvds_vbt_mode);
1876 if (newmode != NULL) {
1877 /* Guarantee the mode is preferred */
1878 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1879 DRM_MODE_TYPE_DRIVER);
1880 drm_mode_probed_add(connector, newmode);
1881 }
1882 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001883
1884end:
1885 list_for_each_entry(newmode, &connector->probed_modes, head) {
1886 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1887 sdvo_priv->sdvo_lvds_fixed_mode =
1888 drm_mode_duplicate(connector->dev, newmode);
1889 break;
1890 }
1891 }
1892
Ma Ling7086c872009-05-13 11:20:06 +08001893}
1894
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001895static int intel_sdvo_get_modes(struct drm_connector *connector)
1896{
Eric Anholt21d40d32010-03-25 11:11:14 -07001897 struct intel_encoder *output = to_intel_encoder(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001898 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
1899
1900 if (sdvo_priv->is_tv)
1901 intel_sdvo_get_tv_modes(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001902 else if (sdvo_priv->is_lvds == true)
1903 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001904 else
1905 intel_sdvo_get_ddc_modes(connector);
1906
Jesse Barnes79e53942008-11-07 14:24:08 -08001907 if (list_empty(&connector->probed_modes))
1908 return 0;
1909 return 1;
1910}
1911
Zhao Yakuib9219c52009-09-10 15:45:46 +08001912static
1913void intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
1914{
Eric Anholt21d40d32010-03-25 11:11:14 -07001915 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1916 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001917 struct drm_device *dev = connector->dev;
1918
1919 if (sdvo_priv->is_tv) {
1920 if (sdvo_priv->left_property)
1921 drm_property_destroy(dev, sdvo_priv->left_property);
1922 if (sdvo_priv->right_property)
1923 drm_property_destroy(dev, sdvo_priv->right_property);
1924 if (sdvo_priv->top_property)
1925 drm_property_destroy(dev, sdvo_priv->top_property);
1926 if (sdvo_priv->bottom_property)
1927 drm_property_destroy(dev, sdvo_priv->bottom_property);
1928 if (sdvo_priv->hpos_property)
1929 drm_property_destroy(dev, sdvo_priv->hpos_property);
1930 if (sdvo_priv->vpos_property)
1931 drm_property_destroy(dev, sdvo_priv->vpos_property);
1932 }
1933 if (sdvo_priv->is_tv) {
1934 if (sdvo_priv->saturation_property)
1935 drm_property_destroy(dev,
1936 sdvo_priv->saturation_property);
1937 if (sdvo_priv->contrast_property)
1938 drm_property_destroy(dev,
1939 sdvo_priv->contrast_property);
1940 if (sdvo_priv->hue_property)
1941 drm_property_destroy(dev, sdvo_priv->hue_property);
1942 }
Zhao Yakuid0cbde92009-09-10 15:45:47 +08001943 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001944 if (sdvo_priv->brightness_property)
1945 drm_property_destroy(dev,
1946 sdvo_priv->brightness_property);
1947 }
1948 return;
1949}
1950
Jesse Barnes79e53942008-11-07 14:24:08 -08001951static void intel_sdvo_destroy(struct drm_connector *connector)
1952{
Eric Anholt21d40d32010-03-25 11:11:14 -07001953 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1954 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001955
Eric Anholt21d40d32010-03-25 11:11:14 -07001956 if (intel_encoder->i2c_bus)
1957 intel_i2c_destroy(intel_encoder->i2c_bus);
1958 if (intel_encoder->ddc_bus)
1959 intel_i2c_destroy(intel_encoder->ddc_bus);
Keith Packard57cdaf92009-09-04 13:07:54 +08001960 if (sdvo_priv->analog_ddc_bus)
1961 intel_i2c_destroy(sdvo_priv->analog_ddc_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08001962
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001963 if (sdvo_priv->sdvo_lvds_fixed_mode != NULL)
1964 drm_mode_destroy(connector->dev,
1965 sdvo_priv->sdvo_lvds_fixed_mode);
1966
Zhao Yakuice6feab2009-08-24 13:50:26 +08001967 if (sdvo_priv->tv_format_property)
1968 drm_property_destroy(connector->dev,
1969 sdvo_priv->tv_format_property);
1970
Zhao Yakuid0cbde92009-09-10 15:45:47 +08001971 if (sdvo_priv->is_tv || sdvo_priv->is_lvds)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001972 intel_sdvo_destroy_enhance_property(connector);
1973
Jesse Barnes79e53942008-11-07 14:24:08 -08001974 drm_sysfs_connector_remove(connector);
1975 drm_connector_cleanup(connector);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001976
Eric Anholt21d40d32010-03-25 11:11:14 -07001977 kfree(intel_encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08001978}
1979
Zhao Yakuice6feab2009-08-24 13:50:26 +08001980static int
1981intel_sdvo_set_property(struct drm_connector *connector,
1982 struct drm_property *property,
1983 uint64_t val)
1984{
Eric Anholt21d40d32010-03-25 11:11:14 -07001985 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
1986 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
1987 struct drm_encoder *encoder = &intel_encoder->enc;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001988 struct drm_crtc *crtc = encoder->crtc;
1989 int ret = 0;
1990 bool changed = false;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001991 uint8_t cmd, status;
1992 uint16_t temp_value;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001993
1994 ret = drm_connector_property_set_value(connector, property, val);
1995 if (ret < 0)
1996 goto out;
1997
1998 if (property == sdvo_priv->tv_format_property) {
1999 if (val >= TV_FORMAT_NUM) {
2000 ret = -EINVAL;
2001 goto out;
2002 }
2003 if (sdvo_priv->tv_format_name ==
2004 sdvo_priv->tv_format_supported[val])
2005 goto out;
2006
2007 sdvo_priv->tv_format_name = sdvo_priv->tv_format_supported[val];
2008 changed = true;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002009 }
2010
Zhao Yakuid0cbde92009-09-10 15:45:47 +08002011 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002012 cmd = 0;
2013 temp_value = val;
2014 if (sdvo_priv->left_property == property) {
2015 drm_connector_property_set_value(connector,
2016 sdvo_priv->right_property, val);
2017 if (sdvo_priv->left_margin == temp_value)
2018 goto out;
2019
2020 sdvo_priv->left_margin = temp_value;
2021 sdvo_priv->right_margin = temp_value;
2022 temp_value = sdvo_priv->max_hscan -
2023 sdvo_priv->left_margin;
2024 cmd = SDVO_CMD_SET_OVERSCAN_H;
2025 } else if (sdvo_priv->right_property == property) {
2026 drm_connector_property_set_value(connector,
2027 sdvo_priv->left_property, val);
2028 if (sdvo_priv->right_margin == temp_value)
2029 goto out;
2030
2031 sdvo_priv->left_margin = temp_value;
2032 sdvo_priv->right_margin = temp_value;
2033 temp_value = sdvo_priv->max_hscan -
2034 sdvo_priv->left_margin;
2035 cmd = SDVO_CMD_SET_OVERSCAN_H;
2036 } else if (sdvo_priv->top_property == property) {
2037 drm_connector_property_set_value(connector,
2038 sdvo_priv->bottom_property, val);
2039 if (sdvo_priv->top_margin == temp_value)
2040 goto out;
2041
2042 sdvo_priv->top_margin = temp_value;
2043 sdvo_priv->bottom_margin = temp_value;
2044 temp_value = sdvo_priv->max_vscan -
2045 sdvo_priv->top_margin;
2046 cmd = SDVO_CMD_SET_OVERSCAN_V;
2047 } else if (sdvo_priv->bottom_property == property) {
2048 drm_connector_property_set_value(connector,
2049 sdvo_priv->top_property, val);
2050 if (sdvo_priv->bottom_margin == temp_value)
2051 goto out;
2052 sdvo_priv->top_margin = temp_value;
2053 sdvo_priv->bottom_margin = temp_value;
2054 temp_value = sdvo_priv->max_vscan -
2055 sdvo_priv->top_margin;
2056 cmd = SDVO_CMD_SET_OVERSCAN_V;
2057 } else if (sdvo_priv->hpos_property == property) {
2058 if (sdvo_priv->cur_hpos == temp_value)
2059 goto out;
2060
2061 cmd = SDVO_CMD_SET_POSITION_H;
2062 sdvo_priv->cur_hpos = temp_value;
2063 } else if (sdvo_priv->vpos_property == property) {
2064 if (sdvo_priv->cur_vpos == temp_value)
2065 goto out;
2066
2067 cmd = SDVO_CMD_SET_POSITION_V;
2068 sdvo_priv->cur_vpos = temp_value;
2069 } else if (sdvo_priv->saturation_property == property) {
2070 if (sdvo_priv->cur_saturation == temp_value)
2071 goto out;
2072
2073 cmd = SDVO_CMD_SET_SATURATION;
2074 sdvo_priv->cur_saturation = temp_value;
2075 } else if (sdvo_priv->contrast_property == property) {
2076 if (sdvo_priv->cur_contrast == temp_value)
2077 goto out;
2078
2079 cmd = SDVO_CMD_SET_CONTRAST;
2080 sdvo_priv->cur_contrast = temp_value;
2081 } else if (sdvo_priv->hue_property == property) {
2082 if (sdvo_priv->cur_hue == temp_value)
2083 goto out;
2084
2085 cmd = SDVO_CMD_SET_HUE;
2086 sdvo_priv->cur_hue = temp_value;
2087 } else if (sdvo_priv->brightness_property == property) {
2088 if (sdvo_priv->cur_brightness == temp_value)
2089 goto out;
2090
2091 cmd = SDVO_CMD_SET_BRIGHTNESS;
2092 sdvo_priv->cur_brightness = temp_value;
2093 }
2094 if (cmd) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002095 intel_sdvo_write_cmd(intel_encoder, cmd, &temp_value, 2);
2096 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002097 NULL, 0);
2098 if (status != SDVO_CMD_STATUS_SUCCESS) {
2099 DRM_DEBUG_KMS("Incorrect SDVO command \n");
2100 return -EINVAL;
2101 }
2102 changed = true;
2103 }
2104 }
Zhao Yakuice6feab2009-08-24 13:50:26 +08002105 if (changed && crtc)
2106 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
2107 crtc->y, crtc->fb);
2108out:
2109 return ret;
2110}
2111
Jesse Barnes79e53942008-11-07 14:24:08 -08002112static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
2113 .dpms = intel_sdvo_dpms,
2114 .mode_fixup = intel_sdvo_mode_fixup,
2115 .prepare = intel_encoder_prepare,
2116 .mode_set = intel_sdvo_mode_set,
2117 .commit = intel_encoder_commit,
2118};
2119
2120static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07002121 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08002122 .save = intel_sdvo_save,
2123 .restore = intel_sdvo_restore,
2124 .detect = intel_sdvo_detect,
2125 .fill_modes = drm_helper_probe_single_connector_modes,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002126 .set_property = intel_sdvo_set_property,
Jesse Barnes79e53942008-11-07 14:24:08 -08002127 .destroy = intel_sdvo_destroy,
2128};
2129
2130static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2131 .get_modes = intel_sdvo_get_modes,
2132 .mode_valid = intel_sdvo_mode_valid,
2133 .best_encoder = intel_best_encoder,
2134};
2135
Hannes Ederb358d0a2008-12-18 21:18:47 +01002136static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08002137{
2138 drm_encoder_cleanup(encoder);
2139}
2140
2141static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2142 .destroy = intel_sdvo_enc_destroy,
2143};
2144
2145
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002146/**
2147 * Choose the appropriate DDC bus for control bus switch command for this
2148 * SDVO output based on the controlled output.
2149 *
2150 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2151 * outputs, then LVDS outputs.
2152 */
2153static void
2154intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
2155{
2156 uint16_t mask = 0;
2157 unsigned int num_bits;
2158
2159 /* Make a mask of outputs less than or equal to our own priority in the
2160 * list.
2161 */
2162 switch (dev_priv->controlled_output) {
2163 case SDVO_OUTPUT_LVDS1:
2164 mask |= SDVO_OUTPUT_LVDS1;
2165 case SDVO_OUTPUT_LVDS0:
2166 mask |= SDVO_OUTPUT_LVDS0;
2167 case SDVO_OUTPUT_TMDS1:
2168 mask |= SDVO_OUTPUT_TMDS1;
2169 case SDVO_OUTPUT_TMDS0:
2170 mask |= SDVO_OUTPUT_TMDS0;
2171 case SDVO_OUTPUT_RGB1:
2172 mask |= SDVO_OUTPUT_RGB1;
2173 case SDVO_OUTPUT_RGB0:
2174 mask |= SDVO_OUTPUT_RGB0;
2175 break;
2176 }
2177
2178 /* Count bits to find what number we are in the priority list. */
2179 mask &= dev_priv->caps.output_flags;
2180 num_bits = hweight16(mask);
2181 if (num_bits > 3) {
2182 /* if more than 3 outputs, default to DDC bus 3 for now */
2183 num_bits = 3;
2184 }
2185
2186 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2187 dev_priv->ddc_bus = 1 << num_bits;
2188}
2189
2190static bool
Eric Anholt21d40d32010-03-25 11:11:14 -07002191intel_sdvo_get_digital_encoding_mode(struct intel_encoder *output)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002192{
2193 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
2194 uint8_t status;
2195
2196 intel_sdvo_set_target_output(output, sdvo_priv->controlled_output);
2197
2198 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
2199 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
2200 if (status != SDVO_CMD_STATUS_SUCCESS)
2201 return false;
2202 return true;
2203}
2204
Eric Anholt21d40d32010-03-25 11:11:14 -07002205static struct intel_encoder *
2206intel_sdvo_chan_to_intel_encoder(struct intel_i2c_chan *chan)
Ma Ling619ac3b2009-05-18 16:12:46 +08002207{
2208 struct drm_device *dev = chan->drm_dev;
2209 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -07002210 struct intel_encoder *intel_encoder = NULL;
Ma Ling619ac3b2009-05-18 16:12:46 +08002211
2212 list_for_each_entry(connector,
2213 &dev->mode_config.connector_list, head) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002214 if (to_intel_encoder(connector)->ddc_bus == &chan->adapter) {
2215 intel_encoder = to_intel_encoder(connector);
Ma Ling619ac3b2009-05-18 16:12:46 +08002216 break;
2217 }
2218 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002219 return intel_encoder;
Ma Ling619ac3b2009-05-18 16:12:46 +08002220}
2221
2222static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
2223 struct i2c_msg msgs[], int num)
2224{
Eric Anholt21d40d32010-03-25 11:11:14 -07002225 struct intel_encoder *intel_encoder;
Ma Ling619ac3b2009-05-18 16:12:46 +08002226 struct intel_sdvo_priv *sdvo_priv;
2227 struct i2c_algo_bit_data *algo_data;
Keith Packardf9c10a92009-05-30 12:16:25 -07002228 const struct i2c_algorithm *algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002229
2230 algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
Eric Anholt21d40d32010-03-25 11:11:14 -07002231 intel_encoder =
2232 intel_sdvo_chan_to_intel_encoder(
Ma Ling619ac3b2009-05-18 16:12:46 +08002233 (struct intel_i2c_chan *)(algo_data->data));
Eric Anholt21d40d32010-03-25 11:11:14 -07002234 if (intel_encoder == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002235 return -EINVAL;
2236
Eric Anholt21d40d32010-03-25 11:11:14 -07002237 sdvo_priv = intel_encoder->dev_priv;
2238 algo = intel_encoder->i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002239
Eric Anholt21d40d32010-03-25 11:11:14 -07002240 intel_sdvo_set_control_bus_switch(intel_encoder, sdvo_priv->ddc_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08002241 return algo->master_xfer(i2c_adap, msgs, num);
2242}
2243
2244static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
2245 .master_xfer = intel_sdvo_master_xfer,
2246};
2247
yakui_zhao714605e2009-05-31 17:18:07 +08002248static u8
Eric Anholtc751ce42010-03-25 11:48:48 -07002249intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
yakui_zhao714605e2009-05-31 17:18:07 +08002250{
2251 struct drm_i915_private *dev_priv = dev->dev_private;
2252 struct sdvo_device_mapping *my_mapping, *other_mapping;
2253
Eric Anholtc751ce42010-03-25 11:48:48 -07002254 if (sdvo_reg == SDVOB) {
yakui_zhao714605e2009-05-31 17:18:07 +08002255 my_mapping = &dev_priv->sdvo_mappings[0];
2256 other_mapping = &dev_priv->sdvo_mappings[1];
2257 } else {
2258 my_mapping = &dev_priv->sdvo_mappings[1];
2259 other_mapping = &dev_priv->sdvo_mappings[0];
2260 }
2261
2262 /* If the BIOS described our SDVO device, take advantage of it. */
2263 if (my_mapping->slave_addr)
2264 return my_mapping->slave_addr;
2265
2266 /* If the BIOS only described a different SDVO device, use the
2267 * address that it isn't using.
2268 */
2269 if (other_mapping->slave_addr) {
2270 if (other_mapping->slave_addr == 0x70)
2271 return 0x72;
2272 else
2273 return 0x70;
2274 }
2275
2276 /* No SDVO device info is found for another DVO port,
2277 * so use mapping assumption we had before BIOS parsing.
2278 */
Eric Anholtc751ce42010-03-25 11:48:48 -07002279 if (sdvo_reg == SDVOB)
yakui_zhao714605e2009-05-31 17:18:07 +08002280 return 0x70;
2281 else
2282 return 0x72;
2283}
2284
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002285static int intel_sdvo_bad_tv_callback(const struct dmi_system_id *id)
2286{
2287 DRM_DEBUG_KMS("Ignoring bad SDVO TV connector for %s\n", id->ident);
2288 return 1;
2289}
2290
2291static struct dmi_system_id intel_sdvo_bad_tv[] = {
2292 {
2293 .callback = intel_sdvo_bad_tv_callback,
2294 .ident = "IntelG45/ICH10R/DME1737",
2295 .matches = {
2296 DMI_MATCH(DMI_SYS_VENDOR, "IBM CORPORATION"),
2297 DMI_MATCH(DMI_PRODUCT_NAME, "4800784"),
2298 },
2299 },
2300
2301 { } /* terminating entry */
2302};
2303
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002304static bool
Eric Anholt21d40d32010-03-25 11:11:14 -07002305intel_sdvo_output_setup(struct intel_encoder *intel_encoder, uint16_t flags)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002306{
Eric Anholt21d40d32010-03-25 11:11:14 -07002307 struct drm_connector *connector = &intel_encoder->base;
2308 struct drm_encoder *encoder = &intel_encoder->enc;
2309 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002310 bool ret = true, registered = false;
2311
2312 sdvo_priv->is_tv = false;
Eric Anholt21d40d32010-03-25 11:11:14 -07002313 intel_encoder->needs_tv_clock = false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002314 sdvo_priv->is_lvds = false;
2315
2316 if (device_is_registered(&connector->kdev)) {
2317 drm_sysfs_connector_remove(connector);
2318 registered = true;
2319 }
2320
2321 if (flags &
2322 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
2323 if (sdvo_priv->caps.output_flags & SDVO_OUTPUT_TMDS0)
2324 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS0;
2325 else
2326 sdvo_priv->controlled_output = SDVO_OUTPUT_TMDS1;
2327
2328 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2329 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2330
Eric Anholt21d40d32010-03-25 11:11:14 -07002331 if (intel_sdvo_get_supp_encode(intel_encoder,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002332 &sdvo_priv->encode) &&
Eric Anholt21d40d32010-03-25 11:11:14 -07002333 intel_sdvo_get_digital_encoding_mode(intel_encoder) &&
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002334 sdvo_priv->is_hdmi) {
2335 /* enable hdmi encoding mode if supported */
Eric Anholt21d40d32010-03-25 11:11:14 -07002336 intel_sdvo_set_encode(intel_encoder, SDVO_ENCODE_HDMI);
2337 intel_sdvo_set_colorimetry(intel_encoder,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002338 SDVO_COLORIMETRY_RGB256);
2339 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
Eric Anholt21d40d32010-03-25 11:11:14 -07002340 intel_encoder->clone_mask =
Ma Lingf8aed702009-08-24 13:50:24 +08002341 (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2342 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002343 }
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002344 } else if ((flags & SDVO_OUTPUT_SVID0) &&
2345 !dmi_check_system(intel_sdvo_bad_tv)) {
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002346
2347 sdvo_priv->controlled_output = SDVO_OUTPUT_SVID0;
2348 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2349 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2350 sdvo_priv->is_tv = true;
Eric Anholt21d40d32010-03-25 11:11:14 -07002351 intel_encoder->needs_tv_clock = true;
2352 intel_encoder->clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002353 } else if (flags & SDVO_OUTPUT_RGB0) {
2354
2355 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB0;
2356 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2357 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Eric Anholt21d40d32010-03-25 11:11:14 -07002358 intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
Ma Lingf8aed702009-08-24 13:50:24 +08002359 (1 << INTEL_ANALOG_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002360 } else if (flags & SDVO_OUTPUT_RGB1) {
2361
2362 sdvo_priv->controlled_output = SDVO_OUTPUT_RGB1;
2363 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2364 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Eric Anholt21d40d32010-03-25 11:11:14 -07002365 intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
Zhao Yakuie2708462009-09-10 15:45:48 +08002366 (1 << INTEL_ANALOG_CLONE_BIT);
Zhao Yakui2dd87382010-01-27 16:32:46 +08002367 } else if (flags & SDVO_OUTPUT_CVBS0) {
2368
2369 sdvo_priv->controlled_output = SDVO_OUTPUT_CVBS0;
2370 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2371 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2372 sdvo_priv->is_tv = true;
Eric Anholt21d40d32010-03-25 11:11:14 -07002373 intel_encoder->needs_tv_clock = true;
2374 intel_encoder->clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002375 } else if (flags & SDVO_OUTPUT_LVDS0) {
2376
2377 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS0;
2378 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2379 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2380 sdvo_priv->is_lvds = true;
Eric Anholt21d40d32010-03-25 11:11:14 -07002381 intel_encoder->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
Ma Lingf8aed702009-08-24 13:50:24 +08002382 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002383 } else if (flags & SDVO_OUTPUT_LVDS1) {
2384
2385 sdvo_priv->controlled_output = SDVO_OUTPUT_LVDS1;
2386 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2387 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2388 sdvo_priv->is_lvds = true;
Eric Anholt21d40d32010-03-25 11:11:14 -07002389 intel_encoder->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
Ma Lingf8aed702009-08-24 13:50:24 +08002390 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002391 } else {
2392
2393 unsigned char bytes[2];
2394
2395 sdvo_priv->controlled_output = 0;
2396 memcpy(bytes, &sdvo_priv->caps.output_flags, 2);
Dave Airlie51c8b402009-08-20 13:38:04 +10002397 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2398 SDVO_NAME(sdvo_priv),
2399 bytes[0], bytes[1]);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002400 ret = false;
2401 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002402 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002403
2404 if (ret && registered)
2405 ret = drm_sysfs_connector_add(connector) == 0 ? true : false;
2406
2407
2408 return ret;
2409
2410}
2411
Zhao Yakuice6feab2009-08-24 13:50:26 +08002412static void intel_sdvo_tv_create_property(struct drm_connector *connector)
2413{
Eric Anholt21d40d32010-03-25 11:11:14 -07002414 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
2415 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002416 struct intel_sdvo_tv_format format;
2417 uint32_t format_map, i;
2418 uint8_t status;
2419
Eric Anholt21d40d32010-03-25 11:11:14 -07002420 intel_sdvo_set_target_output(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002421 sdvo_priv->controlled_output);
2422
Eric Anholt21d40d32010-03-25 11:11:14 -07002423 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002424 SDVO_CMD_GET_SUPPORTED_TV_FORMATS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002425 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002426 &format, sizeof(format));
2427 if (status != SDVO_CMD_STATUS_SUCCESS)
2428 return;
2429
2430 memcpy(&format_map, &format, sizeof(format) > sizeof(format_map) ?
2431 sizeof(format_map) : sizeof(format));
2432
2433 if (format_map == 0)
2434 return;
2435
2436 sdvo_priv->format_supported_num = 0;
2437 for (i = 0 ; i < TV_FORMAT_NUM; i++)
2438 if (format_map & (1 << i)) {
2439 sdvo_priv->tv_format_supported
2440 [sdvo_priv->format_supported_num++] =
2441 tv_format_names[i];
2442 }
2443
2444
2445 sdvo_priv->tv_format_property =
2446 drm_property_create(
2447 connector->dev, DRM_MODE_PROP_ENUM,
2448 "mode", sdvo_priv->format_supported_num);
2449
2450 for (i = 0; i < sdvo_priv->format_supported_num; i++)
2451 drm_property_add_enum(
2452 sdvo_priv->tv_format_property, i,
2453 i, sdvo_priv->tv_format_supported[i]);
2454
2455 sdvo_priv->tv_format_name = sdvo_priv->tv_format_supported[0];
2456 drm_connector_attach_property(
2457 connector, sdvo_priv->tv_format_property, 0);
2458
2459}
2460
Zhao Yakuib9219c52009-09-10 15:45:46 +08002461static void intel_sdvo_create_enhance_property(struct drm_connector *connector)
2462{
Eric Anholt21d40d32010-03-25 11:11:14 -07002463 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
2464 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakuib9219c52009-09-10 15:45:46 +08002465 struct intel_sdvo_enhancements_reply sdvo_data;
2466 struct drm_device *dev = connector->dev;
2467 uint8_t status;
2468 uint16_t response, data_value[2];
2469
Eric Anholt21d40d32010-03-25 11:11:14 -07002470 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002471 NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002472 status = intel_sdvo_read_response(intel_encoder, &sdvo_data,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002473 sizeof(sdvo_data));
2474 if (status != SDVO_CMD_STATUS_SUCCESS) {
2475 DRM_DEBUG_KMS(" incorrect response is returned\n");
2476 return;
2477 }
2478 response = *((uint16_t *)&sdvo_data);
2479 if (!response) {
2480 DRM_DEBUG_KMS("No enhancement is supported\n");
2481 return;
2482 }
2483 if (sdvo_priv->is_tv) {
2484 /* when horizontal overscan is supported, Add the left/right
2485 * property
2486 */
2487 if (sdvo_data.overscan_h) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002488 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002489 SDVO_CMD_GET_MAX_OVERSCAN_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002490 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002491 &data_value, 4);
2492 if (status != SDVO_CMD_STATUS_SUCCESS) {
2493 DRM_DEBUG_KMS("Incorrect SDVO max "
2494 "h_overscan\n");
2495 return;
2496 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002497 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002498 SDVO_CMD_GET_OVERSCAN_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002499 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002500 &response, 2);
2501 if (status != SDVO_CMD_STATUS_SUCCESS) {
2502 DRM_DEBUG_KMS("Incorrect SDVO h_overscan\n");
2503 return;
2504 }
2505 sdvo_priv->max_hscan = data_value[0];
2506 sdvo_priv->left_margin = data_value[0] - response;
2507 sdvo_priv->right_margin = sdvo_priv->left_margin;
2508 sdvo_priv->left_property =
2509 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2510 "left_margin", 2);
2511 sdvo_priv->left_property->values[0] = 0;
2512 sdvo_priv->left_property->values[1] = data_value[0];
2513 drm_connector_attach_property(connector,
2514 sdvo_priv->left_property,
2515 sdvo_priv->left_margin);
2516 sdvo_priv->right_property =
2517 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2518 "right_margin", 2);
2519 sdvo_priv->right_property->values[0] = 0;
2520 sdvo_priv->right_property->values[1] = data_value[0];
2521 drm_connector_attach_property(connector,
2522 sdvo_priv->right_property,
2523 sdvo_priv->right_margin);
2524 DRM_DEBUG_KMS("h_overscan: max %d, "
2525 "default %d, current %d\n",
2526 data_value[0], data_value[1], response);
2527 }
2528 if (sdvo_data.overscan_v) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002529 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002530 SDVO_CMD_GET_MAX_OVERSCAN_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002531 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002532 &data_value, 4);
2533 if (status != SDVO_CMD_STATUS_SUCCESS) {
2534 DRM_DEBUG_KMS("Incorrect SDVO max "
2535 "v_overscan\n");
2536 return;
2537 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002538 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002539 SDVO_CMD_GET_OVERSCAN_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002540 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002541 &response, 2);
2542 if (status != SDVO_CMD_STATUS_SUCCESS) {
2543 DRM_DEBUG_KMS("Incorrect SDVO v_overscan\n");
2544 return;
2545 }
2546 sdvo_priv->max_vscan = data_value[0];
2547 sdvo_priv->top_margin = data_value[0] - response;
2548 sdvo_priv->bottom_margin = sdvo_priv->top_margin;
2549 sdvo_priv->top_property =
2550 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2551 "top_margin", 2);
2552 sdvo_priv->top_property->values[0] = 0;
2553 sdvo_priv->top_property->values[1] = data_value[0];
2554 drm_connector_attach_property(connector,
2555 sdvo_priv->top_property,
2556 sdvo_priv->top_margin);
2557 sdvo_priv->bottom_property =
2558 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2559 "bottom_margin", 2);
2560 sdvo_priv->bottom_property->values[0] = 0;
2561 sdvo_priv->bottom_property->values[1] = data_value[0];
2562 drm_connector_attach_property(connector,
2563 sdvo_priv->bottom_property,
2564 sdvo_priv->bottom_margin);
2565 DRM_DEBUG_KMS("v_overscan: max %d, "
2566 "default %d, current %d\n",
2567 data_value[0], data_value[1], response);
2568 }
2569 if (sdvo_data.position_h) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002570 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002571 SDVO_CMD_GET_MAX_POSITION_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002572 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002573 &data_value, 4);
2574 if (status != SDVO_CMD_STATUS_SUCCESS) {
2575 DRM_DEBUG_KMS("Incorrect SDVO Max h_pos\n");
2576 return;
2577 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002578 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002579 SDVO_CMD_GET_POSITION_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002580 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002581 &response, 2);
2582 if (status != SDVO_CMD_STATUS_SUCCESS) {
2583 DRM_DEBUG_KMS("Incorrect SDVO get h_postion\n");
2584 return;
2585 }
2586 sdvo_priv->max_hpos = data_value[0];
2587 sdvo_priv->cur_hpos = response;
2588 sdvo_priv->hpos_property =
2589 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2590 "hpos", 2);
2591 sdvo_priv->hpos_property->values[0] = 0;
2592 sdvo_priv->hpos_property->values[1] = data_value[0];
2593 drm_connector_attach_property(connector,
2594 sdvo_priv->hpos_property,
2595 sdvo_priv->cur_hpos);
2596 DRM_DEBUG_KMS("h_position: max %d, "
2597 "default %d, current %d\n",
2598 data_value[0], data_value[1], response);
2599 }
2600 if (sdvo_data.position_v) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002601 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002602 SDVO_CMD_GET_MAX_POSITION_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002603 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002604 &data_value, 4);
2605 if (status != SDVO_CMD_STATUS_SUCCESS) {
2606 DRM_DEBUG_KMS("Incorrect SDVO Max v_pos\n");
2607 return;
2608 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002609 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002610 SDVO_CMD_GET_POSITION_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002611 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002612 &response, 2);
2613 if (status != SDVO_CMD_STATUS_SUCCESS) {
2614 DRM_DEBUG_KMS("Incorrect SDVO get v_postion\n");
2615 return;
2616 }
2617 sdvo_priv->max_vpos = data_value[0];
2618 sdvo_priv->cur_vpos = response;
2619 sdvo_priv->vpos_property =
2620 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2621 "vpos", 2);
2622 sdvo_priv->vpos_property->values[0] = 0;
2623 sdvo_priv->vpos_property->values[1] = data_value[0];
2624 drm_connector_attach_property(connector,
2625 sdvo_priv->vpos_property,
2626 sdvo_priv->cur_vpos);
2627 DRM_DEBUG_KMS("v_position: max %d, "
2628 "default %d, current %d\n",
2629 data_value[0], data_value[1], response);
2630 }
2631 }
2632 if (sdvo_priv->is_tv) {
2633 if (sdvo_data.saturation) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002634 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002635 SDVO_CMD_GET_MAX_SATURATION, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002636 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002637 &data_value, 4);
2638 if (status != SDVO_CMD_STATUS_SUCCESS) {
2639 DRM_DEBUG_KMS("Incorrect SDVO Max sat\n");
2640 return;
2641 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002642 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002643 SDVO_CMD_GET_SATURATION, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002644 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002645 &response, 2);
2646 if (status != SDVO_CMD_STATUS_SUCCESS) {
2647 DRM_DEBUG_KMS("Incorrect SDVO get sat\n");
2648 return;
2649 }
2650 sdvo_priv->max_saturation = data_value[0];
2651 sdvo_priv->cur_saturation = response;
2652 sdvo_priv->saturation_property =
2653 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2654 "saturation", 2);
2655 sdvo_priv->saturation_property->values[0] = 0;
2656 sdvo_priv->saturation_property->values[1] =
2657 data_value[0];
2658 drm_connector_attach_property(connector,
2659 sdvo_priv->saturation_property,
2660 sdvo_priv->cur_saturation);
2661 DRM_DEBUG_KMS("saturation: max %d, "
2662 "default %d, current %d\n",
2663 data_value[0], data_value[1], response);
2664 }
2665 if (sdvo_data.contrast) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002666 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002667 SDVO_CMD_GET_MAX_CONTRAST, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002668 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002669 &data_value, 4);
2670 if (status != SDVO_CMD_STATUS_SUCCESS) {
2671 DRM_DEBUG_KMS("Incorrect SDVO Max contrast\n");
2672 return;
2673 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002674 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002675 SDVO_CMD_GET_CONTRAST, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002676 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002677 &response, 2);
2678 if (status != SDVO_CMD_STATUS_SUCCESS) {
2679 DRM_DEBUG_KMS("Incorrect SDVO get contrast\n");
2680 return;
2681 }
2682 sdvo_priv->max_contrast = data_value[0];
2683 sdvo_priv->cur_contrast = response;
2684 sdvo_priv->contrast_property =
2685 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2686 "contrast", 2);
2687 sdvo_priv->contrast_property->values[0] = 0;
2688 sdvo_priv->contrast_property->values[1] = data_value[0];
2689 drm_connector_attach_property(connector,
2690 sdvo_priv->contrast_property,
2691 sdvo_priv->cur_contrast);
2692 DRM_DEBUG_KMS("contrast: max %d, "
2693 "default %d, current %d\n",
2694 data_value[0], data_value[1], response);
2695 }
2696 if (sdvo_data.hue) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002697 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002698 SDVO_CMD_GET_MAX_HUE, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002699 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002700 &data_value, 4);
2701 if (status != SDVO_CMD_STATUS_SUCCESS) {
2702 DRM_DEBUG_KMS("Incorrect SDVO Max hue\n");
2703 return;
2704 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002705 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002706 SDVO_CMD_GET_HUE, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002707 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002708 &response, 2);
2709 if (status != SDVO_CMD_STATUS_SUCCESS) {
2710 DRM_DEBUG_KMS("Incorrect SDVO get hue\n");
2711 return;
2712 }
2713 sdvo_priv->max_hue = data_value[0];
2714 sdvo_priv->cur_hue = response;
2715 sdvo_priv->hue_property =
2716 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2717 "hue", 2);
2718 sdvo_priv->hue_property->values[0] = 0;
2719 sdvo_priv->hue_property->values[1] =
2720 data_value[0];
2721 drm_connector_attach_property(connector,
2722 sdvo_priv->hue_property,
2723 sdvo_priv->cur_hue);
2724 DRM_DEBUG_KMS("hue: max %d, default %d, current %d\n",
2725 data_value[0], data_value[1], response);
2726 }
2727 }
Zhao Yakuid0cbde92009-09-10 15:45:47 +08002728 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002729 if (sdvo_data.brightness) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002730 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002731 SDVO_CMD_GET_MAX_BRIGHTNESS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002732 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002733 &data_value, 4);
2734 if (status != SDVO_CMD_STATUS_SUCCESS) {
2735 DRM_DEBUG_KMS("Incorrect SDVO Max bright\n");
2736 return;
2737 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002738 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002739 SDVO_CMD_GET_BRIGHTNESS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002740 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002741 &response, 2);
2742 if (status != SDVO_CMD_STATUS_SUCCESS) {
2743 DRM_DEBUG_KMS("Incorrect SDVO get brigh\n");
2744 return;
2745 }
2746 sdvo_priv->max_brightness = data_value[0];
2747 sdvo_priv->cur_brightness = response;
2748 sdvo_priv->brightness_property =
2749 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2750 "brightness", 2);
2751 sdvo_priv->brightness_property->values[0] = 0;
2752 sdvo_priv->brightness_property->values[1] =
2753 data_value[0];
2754 drm_connector_attach_property(connector,
2755 sdvo_priv->brightness_property,
2756 sdvo_priv->cur_brightness);
2757 DRM_DEBUG_KMS("brightness: max %d, "
2758 "default %d, current %d\n",
2759 data_value[0], data_value[1], response);
2760 }
2761 }
2762 return;
2763}
2764
Eric Anholtc751ce42010-03-25 11:48:48 -07002765bool intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
Jesse Barnes79e53942008-11-07 14:24:08 -08002766{
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002767 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002768 struct drm_connector *connector;
Eric Anholt21d40d32010-03-25 11:11:14 -07002769 struct intel_encoder *intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08002770 struct intel_sdvo_priv *sdvo_priv;
Keith Packardf9c10a92009-05-30 12:16:25 -07002771
Jesse Barnes79e53942008-11-07 14:24:08 -08002772 u8 ch[0x40];
2773 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08002774
Eric Anholt21d40d32010-03-25 11:11:14 -07002775 intel_encoder = kcalloc(sizeof(struct intel_encoder)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
2776 if (!intel_encoder) {
Eric Anholt7d573822009-01-02 13:33:00 -08002777 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002778 }
2779
Eric Anholt21d40d32010-03-25 11:11:14 -07002780 sdvo_priv = (struct intel_sdvo_priv *)(intel_encoder + 1);
Eric Anholtc751ce42010-03-25 11:48:48 -07002781 sdvo_priv->sdvo_reg = sdvo_reg;
Keith Packard308cd3a2009-06-14 11:56:18 -07002782
Eric Anholt21d40d32010-03-25 11:11:14 -07002783 intel_encoder->dev_priv = sdvo_priv;
2784 intel_encoder->type = INTEL_OUTPUT_SDVO;
Jesse Barnes79e53942008-11-07 14:24:08 -08002785
Jesse Barnes79e53942008-11-07 14:24:08 -08002786 /* setup the DDC bus. */
Eric Anholtc751ce42010-03-25 11:48:48 -07002787 if (sdvo_reg == SDVOB)
Eric Anholt21d40d32010-03-25 11:11:14 -07002788 intel_encoder->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOB");
Keith Packard308cd3a2009-06-14 11:56:18 -07002789 else
Eric Anholt21d40d32010-03-25 11:11:14 -07002790 intel_encoder->i2c_bus = intel_i2c_create(dev, GPIOE, "SDVOCTRL_E for SDVOC");
Keith Packard308cd3a2009-06-14 11:56:18 -07002791
Eric Anholt21d40d32010-03-25 11:11:14 -07002792 if (!intel_encoder->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002793 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002794
Eric Anholtc751ce42010-03-25 11:48:48 -07002795 sdvo_priv->slave_addr = intel_sdvo_get_slave_addr(dev, sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08002796
Keith Packard308cd3a2009-06-14 11:56:18 -07002797 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
Eric Anholt21d40d32010-03-25 11:11:14 -07002798 intel_sdvo_i2c_bit_algo.functionality = intel_encoder->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002799
Jesse Barnes79e53942008-11-07 14:24:08 -08002800 /* Read the regs to test if we can talk to the device */
2801 for (i = 0; i < 0x40; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002802 if (!intel_sdvo_read_byte(intel_encoder, i, &ch[i])) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002803 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
Eric Anholtc751ce42010-03-25 11:48:48 -07002804 sdvo_reg == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002805 goto err_i2c;
2806 }
2807 }
2808
Ma Ling619ac3b2009-05-18 16:12:46 +08002809 /* setup the DDC bus. */
Eric Anholtc751ce42010-03-25 11:48:48 -07002810 if (sdvo_reg == SDVOB) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002811 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOB DDC BUS");
Keith Packard57cdaf92009-09-04 13:07:54 +08002812 sdvo_priv->analog_ddc_bus = intel_i2c_create(dev, GPIOA,
2813 "SDVOB/VGA DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002814 dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002815 } else {
Eric Anholt21d40d32010-03-25 11:11:14 -07002816 intel_encoder->ddc_bus = intel_i2c_create(dev, GPIOE, "SDVOC DDC BUS");
Keith Packard57cdaf92009-09-04 13:07:54 +08002817 sdvo_priv->analog_ddc_bus = intel_i2c_create(dev, GPIOA,
2818 "SDVOC/VGA DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002819 dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002820 }
Ma Ling619ac3b2009-05-18 16:12:46 +08002821
Eric Anholt21d40d32010-03-25 11:11:14 -07002822 if (intel_encoder->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002823 goto err_i2c;
2824
Keith Packard308cd3a2009-06-14 11:56:18 -07002825 /* Wrap with our custom algo which switches to DDC mode */
Eric Anholt21d40d32010-03-25 11:11:14 -07002826 intel_encoder->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002827
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002828 /* In default case sdvo lvds is false */
Eric Anholt21d40d32010-03-25 11:11:14 -07002829 intel_sdvo_get_capabilities(intel_encoder, &sdvo_priv->caps);
Jesse Barnes79e53942008-11-07 14:24:08 -08002830
Eric Anholt21d40d32010-03-25 11:11:14 -07002831 if (intel_sdvo_output_setup(intel_encoder,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002832 sdvo_priv->caps.output_flags) != true) {
Dave Airlie51c8b402009-08-20 13:38:04 +10002833 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
Eric Anholtc751ce42010-03-25 11:48:48 -07002834 sdvo_reg == SDVOB ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002835 goto err_i2c;
2836 }
2837
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002838
Eric Anholt21d40d32010-03-25 11:11:14 -07002839 connector = &intel_encoder->base;
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002840 drm_connector_init(dev, connector, &intel_sdvo_connector_funcs,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002841 connector->connector_type);
2842
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002843 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
2844 connector->interlace_allowed = 0;
2845 connector->doublescan_allowed = 0;
2846 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2847
Eric Anholt21d40d32010-03-25 11:11:14 -07002848 drm_encoder_init(dev, &intel_encoder->enc,
2849 &intel_sdvo_enc_funcs, intel_encoder->enc.encoder_type);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002850
Eric Anholt21d40d32010-03-25 11:11:14 -07002851 drm_encoder_helper_add(&intel_encoder->enc, &intel_sdvo_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08002852
Eric Anholt21d40d32010-03-25 11:11:14 -07002853 drm_mode_connector_attach_encoder(&intel_encoder->base, &intel_encoder->enc);
Zhao Yakuid0cbde92009-09-10 15:45:47 +08002854 if (sdvo_priv->is_tv)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002855 intel_sdvo_tv_create_property(connector);
Zhao Yakuid0cbde92009-09-10 15:45:47 +08002856
2857 if (sdvo_priv->is_tv || sdvo_priv->is_lvds)
Zhao Yakuib9219c52009-09-10 15:45:46 +08002858 intel_sdvo_create_enhance_property(connector);
Zhao Yakuid0cbde92009-09-10 15:45:47 +08002859
Jesse Barnes79e53942008-11-07 14:24:08 -08002860 drm_sysfs_connector_add(connector);
2861
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002862 intel_sdvo_select_ddc_bus(sdvo_priv);
2863
Jesse Barnes79e53942008-11-07 14:24:08 -08002864 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholt21d40d32010-03-25 11:11:14 -07002865 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08002866
Eric Anholt21d40d32010-03-25 11:11:14 -07002867 intel_sdvo_get_input_pixel_clock_range(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08002868 &sdvo_priv->pixel_clock_min,
2869 &sdvo_priv->pixel_clock_max);
2870
2871
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002872 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
yakui_zhao342dc382009-06-02 14:12:00 +08002873 "clock range %dMHz - %dMHz, "
2874 "input 1: %c, input 2: %c, "
2875 "output 1: %c, output 2: %c\n",
2876 SDVO_NAME(sdvo_priv),
2877 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
2878 sdvo_priv->caps.device_rev_id,
2879 sdvo_priv->pixel_clock_min / 1000,
2880 sdvo_priv->pixel_clock_max / 1000,
2881 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2882 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2883 /* check currently supported outputs */
2884 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002885 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002886 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002887 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2888
Eric Anholt7d573822009-01-02 13:33:00 -08002889 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002890
2891err_i2c:
Keith Packard57cdaf92009-09-04 13:07:54 +08002892 if (sdvo_priv->analog_ddc_bus != NULL)
2893 intel_i2c_destroy(sdvo_priv->analog_ddc_bus);
Eric Anholt21d40d32010-03-25 11:11:14 -07002894 if (intel_encoder->ddc_bus != NULL)
2895 intel_i2c_destroy(intel_encoder->ddc_bus);
2896 if (intel_encoder->i2c_bus != NULL)
2897 intel_i2c_destroy(intel_encoder->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002898err_inteloutput:
Eric Anholt21d40d32010-03-25 11:11:14 -07002899 kfree(intel_encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08002900
Eric Anholt7d573822009-01-02 13:33:00 -08002901 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002902}