blob: ca372abc36cd4fb0b412ca3844db66cef4986df2 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/delay.h>
31#include "drmP.h"
32#include "drm.h"
33#include "drm_crtc.h"
34#include "intel_drv.h"
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +080035#include "drm_edid.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "i915_drm.h"
37#include "i915_drv.h"
38#include "intel_sdvo_regs.h"
39
Zhenyu Wang14571b42010-03-30 14:06:33 +080040#define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
41#define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
42#define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
43#define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0)
44
45#define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
46 SDVO_TV_MASK)
47
48#define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
49#define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
50
Jesse Barnes79e53942008-11-07 14:24:08 -080051
Zhao Yakuice6feab2009-08-24 13:50:26 +080052static char *tv_format_names[] = {
53 "NTSC_M" , "NTSC_J" , "NTSC_443",
54 "PAL_B" , "PAL_D" , "PAL_G" ,
55 "PAL_H" , "PAL_I" , "PAL_M" ,
56 "PAL_N" , "PAL_NC" , "PAL_60" ,
57 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
58 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
59 "SECAM_60"
60};
61
62#define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
63
Jesse Barnes79e53942008-11-07 14:24:08 -080064struct intel_sdvo_priv {
Keith Packardf9c10a92009-05-30 12:16:25 -070065 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080066
67 /* Register for the SDVO device: SDVOB or SDVOC */
Eric Anholtc751ce42010-03-25 11:48:48 -070068 int sdvo_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080069
Jesse Barnese2f0ba92009-02-02 15:11:52 -080070 /* Active outputs controlled by this SDVO output */
71 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080072
Jesse Barnese2f0ba92009-02-02 15:11:52 -080073 /*
74 * Capabilities of the SDVO device returned by
75 * i830_sdvo_get_capabilities()
76 */
Jesse Barnes79e53942008-11-07 14:24:08 -080077 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080078
79 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080080 int pixel_clock_min, pixel_clock_max;
81
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080082 /*
83 * For multiple function SDVO device,
84 * this is for current attached outputs.
85 */
86 uint16_t attached_output;
87
Jesse Barnese2f0ba92009-02-02 15:11:52 -080088 /**
89 * This is set if we're going to treat the device as TV-out.
90 *
91 * While we have these nice friendly flags for output types that ought
92 * to decide this for us, the S-Video output on our HDMI+S-Video card
93 * shows up as RGB1 (VGA).
94 */
95 bool is_tv;
96
Zhao Yakuice6feab2009-08-24 13:50:26 +080097 /* This is for current tv format name */
98 char *tv_format_name;
99
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800100 /**
101 * This is set if we treat the device as HDMI, instead of DVI.
102 */
103 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800104
Ma Ling7086c872009-05-13 11:20:06 +0800105 /**
106 * This is set if we detect output of sdvo device as LVDS.
107 */
108 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800109
110 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800111 * This is sdvo flags for input timing.
112 */
113 uint8_t sdvo_flags;
114
115 /**
116 * This is sdvo fixed pannel mode pointer
117 */
118 struct drm_display_mode *sdvo_lvds_fixed_mode;
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
Zhenyu Wang14571b42010-03-30 14:06:33 +0800132};
133
134struct intel_sdvo_connector {
135 /* Mark the type of connector */
136 uint16_t output_flag;
137
138 /* This contains all current supported TV format */
139 char *tv_format_supported[TV_FORMAT_NUM];
140 int format_supported_num;
141 struct drm_property *tv_format_property;
142 struct drm_property *tv_format_name_property[TV_FORMAT_NUM];
143
144 /**
145 * Returned SDTV resolutions allowed for the current format, if the
146 * device reported it.
147 */
148 struct intel_sdvo_sdtv_resolution_reply sdtv_resolutions;
149
Zhao Yakuib9219c52009-09-10 15:45:46 +0800150 /* add the property for the SDVO-TV */
151 struct drm_property *left_property;
152 struct drm_property *right_property;
153 struct drm_property *top_property;
154 struct drm_property *bottom_property;
155 struct drm_property *hpos_property;
156 struct drm_property *vpos_property;
157
158 /* add the property for the SDVO-TV/LVDS */
159 struct drm_property *brightness_property;
160 struct drm_property *contrast_property;
161 struct drm_property *saturation_property;
162 struct drm_property *hue_property;
163
164 /* Add variable to record current setting for the above property */
165 u32 left_margin, right_margin, top_margin, bottom_margin;
166 /* this is to get the range of margin.*/
167 u32 max_hscan, max_vscan;
168 u32 max_hpos, cur_hpos;
169 u32 max_vpos, cur_vpos;
170 u32 cur_brightness, max_brightness;
171 u32 cur_contrast, max_contrast;
172 u32 cur_saturation, max_saturation;
173 u32 cur_hue, max_hue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800174};
175
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800176static bool
Zhenyu Wangd2a82a62010-03-29 21:22:55 +0800177intel_sdvo_output_setup(struct intel_encoder *intel_encoder,
Zhenyu Wangd2a82a62010-03-29 21:22:55 +0800178 uint16_t flags);
Zhenyu Wang14571b42010-03-30 14:06:33 +0800179static void
180intel_sdvo_tv_create_property(struct drm_connector *connector, int type);
181static void
182intel_sdvo_create_enhance_property(struct drm_connector *connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800183
Jesse Barnes79e53942008-11-07 14:24:08 -0800184/**
185 * Writes the SDVOB or SDVOC with the given value, but always writes both
186 * SDVOB and SDVOC to work around apparent hardware issues (according to
187 * comments in the BIOS).
188 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700189static void intel_sdvo_write_sdvox(struct intel_encoder *intel_encoder, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800190{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +0800191 struct drm_device *dev = intel_encoder->enc.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800192 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -0700193 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800194 u32 bval = val, cval = val;
195 int i;
196
Zhao Yakui461ed3c2010-03-30 15:11:33 +0800197 if (sdvo_priv->sdvo_reg == PCH_SDVOB) {
198 I915_WRITE(sdvo_priv->sdvo_reg, val);
199 I915_READ(sdvo_priv->sdvo_reg);
200 return;
201 }
202
Eric Anholtc751ce42010-03-25 11:48:48 -0700203 if (sdvo_priv->sdvo_reg == SDVOB) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800204 cval = I915_READ(SDVOC);
205 } else {
206 bval = I915_READ(SDVOB);
207 }
208 /*
209 * Write the registers twice for luck. Sometimes,
210 * writing them only once doesn't appear to 'stick'.
211 * The BIOS does this too. Yay, magic
212 */
213 for (i = 0; i < 2; i++)
214 {
215 I915_WRITE(SDVOB, bval);
216 I915_READ(SDVOB);
217 I915_WRITE(SDVOC, cval);
218 I915_READ(SDVOC);
219 }
220}
221
Eric Anholt21d40d32010-03-25 11:11:14 -0700222static bool intel_sdvo_read_byte(struct intel_encoder *intel_encoder, u8 addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800223 u8 *ch)
224{
Eric Anholt21d40d32010-03-25 11:11:14 -0700225 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 u8 out_buf[2];
227 u8 buf[2];
228 int ret;
229
230 struct i2c_msg msgs[] = {
231 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700232 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800233 .flags = 0,
234 .len = 1,
235 .buf = out_buf,
236 },
237 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700238 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800239 .flags = I2C_M_RD,
240 .len = 1,
241 .buf = buf,
242 }
243 };
244
245 out_buf[0] = addr;
246 out_buf[1] = 0;
247
Eric Anholt21d40d32010-03-25 11:11:14 -0700248 if ((ret = i2c_transfer(intel_encoder->i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800249 {
250 *ch = buf[0];
251 return true;
252 }
253
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800254 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800255 return false;
256}
257
Eric Anholt21d40d32010-03-25 11:11:14 -0700258static bool intel_sdvo_write_byte(struct intel_encoder *intel_encoder, int addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800259 u8 ch)
260{
Eric Anholt21d40d32010-03-25 11:11:14 -0700261 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800262 u8 out_buf[2];
263 struct i2c_msg msgs[] = {
264 {
Keith Packardf9c10a92009-05-30 12:16:25 -0700265 .addr = sdvo_priv->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800266 .flags = 0,
267 .len = 2,
268 .buf = out_buf,
269 }
270 };
271
272 out_buf[0] = addr;
273 out_buf[1] = ch;
274
Eric Anholt21d40d32010-03-25 11:11:14 -0700275 if (i2c_transfer(intel_encoder->i2c_bus, msgs, 1) == 1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800276 {
277 return true;
278 }
279 return false;
280}
281
282#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
283/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100284static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800285 u8 cmd;
286 char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800287} sdvo_cmd_names[] = {
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
289 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
290 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
291 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
292 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
Zhao Yakuib9219c52009-09-10 15:45:46 +0800331 /* Add the op code for SDVO enhancements */
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_POSITION_H),
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POSITION_H),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_POSITION_H),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_POSITION_V),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POSITION_V),
337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_POSITION_V),
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
342 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800356 /* HDMI op code */
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
370 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
371 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
373 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
374 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800377};
378
Zhao Yakui461ed3c2010-03-30 15:11:33 +0800379#define IS_SDVOB(reg) (reg == SDVOB || reg == PCH_SDVOB)
380#define SDVO_NAME(dev_priv) (IS_SDVOB((dev_priv)->sdvo_reg) ? "SDVOB" : "SDVOC")
Eric Anholtc751ce42010-03-25 11:48:48 -0700381#define SDVO_PRIV(encoder) ((struct intel_sdvo_priv *) (encoder)->dev_priv)
Jesse Barnes79e53942008-11-07 14:24:08 -0800382
Eric Anholt21d40d32010-03-25 11:11:14 -0700383static void intel_sdvo_debug_write(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800384 void *args, int args_len)
385{
Eric Anholt21d40d32010-03-25 11:11:14 -0700386 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800387 int i;
388
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800389 DRM_DEBUG_KMS("%s: W: %02X ",
yakui_zhao342dc382009-06-02 14:12:00 +0800390 SDVO_NAME(sdvo_priv), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800391 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800392 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800393 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800394 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800395 for (i = 0; i < sizeof(sdvo_cmd_names) / sizeof(sdvo_cmd_names[0]); i++) {
396 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800397 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800398 break;
399 }
400 }
401 if (i == sizeof(sdvo_cmd_names)/ sizeof(sdvo_cmd_names[0]))
yakui_zhao342dc382009-06-02 14:12:00 +0800402 DRM_LOG_KMS("(%02X)", cmd);
403 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800404}
Jesse Barnes79e53942008-11-07 14:24:08 -0800405
Eric Anholt21d40d32010-03-25 11:11:14 -0700406static void intel_sdvo_write_cmd(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800407 void *args, int args_len)
408{
409 int i;
410
Eric Anholt21d40d32010-03-25 11:11:14 -0700411 intel_sdvo_debug_write(intel_encoder, cmd, args, args_len);
Jesse Barnes79e53942008-11-07 14:24:08 -0800412
413 for (i = 0; i < args_len; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700414 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_ARG_0 - i,
Jesse Barnes79e53942008-11-07 14:24:08 -0800415 ((u8*)args)[i]);
416 }
417
Eric Anholt21d40d32010-03-25 11:11:14 -0700418 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_OPCODE, cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800419}
420
Jesse Barnes79e53942008-11-07 14:24:08 -0800421static const char *cmd_status_names[] = {
422 "Power on",
423 "Success",
424 "Not supported",
425 "Invalid arg",
426 "Pending",
427 "Target not specified",
428 "Scaling not supported"
429};
430
Eric Anholt21d40d32010-03-25 11:11:14 -0700431static void intel_sdvo_debug_response(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800432 void *response, int response_len,
433 u8 status)
434{
Eric Anholt21d40d32010-03-25 11:11:14 -0700435 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800436 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800437
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800438 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -0800439 for (i = 0; i < response_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800440 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800441 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800442 DRM_LOG_KMS(" ");
Jesse Barnes79e53942008-11-07 14:24:08 -0800443 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800444 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800445 else
yakui_zhao342dc382009-06-02 14:12:00 +0800446 DRM_LOG_KMS("(??? %d)", status);
447 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800448}
Jesse Barnes79e53942008-11-07 14:24:08 -0800449
Eric Anholt21d40d32010-03-25 11:11:14 -0700450static u8 intel_sdvo_read_response(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800451 void *response, int response_len)
452{
453 int i;
454 u8 status;
455 u8 retry = 50;
456
457 while (retry--) {
458 /* Read the command response */
459 for (i = 0; i < response_len; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -0700460 intel_sdvo_read_byte(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800461 SDVO_I2C_RETURN_0 + i,
462 &((u8 *)response)[i]);
463 }
464
465 /* read the return status */
Eric Anholt21d40d32010-03-25 11:11:14 -0700466 intel_sdvo_read_byte(intel_encoder, SDVO_I2C_CMD_STATUS,
Jesse Barnes79e53942008-11-07 14:24:08 -0800467 &status);
468
Eric Anholt21d40d32010-03-25 11:11:14 -0700469 intel_sdvo_debug_response(intel_encoder, response, response_len,
Jesse Barnes79e53942008-11-07 14:24:08 -0800470 status);
471 if (status != SDVO_CMD_STATUS_PENDING)
472 return status;
473
474 mdelay(50);
475 }
476
477 return status;
478}
479
Hannes Ederb358d0a2008-12-18 21:18:47 +0100480static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800481{
482 if (mode->clock >= 100000)
483 return 1;
484 else if (mode->clock >= 50000)
485 return 2;
486 else
487 return 4;
488}
489
490/**
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800491 * Try to read the response after issuie the DDC switch command. But it
492 * is noted that we must do the action of reading response and issuing DDC
493 * switch command in one I2C transaction. Otherwise when we try to start
494 * another I2C transaction after issuing the DDC bus switch, it will be
495 * switched to the internal SDVO register.
Jesse Barnes79e53942008-11-07 14:24:08 -0800496 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700497static void intel_sdvo_set_control_bus_switch(struct intel_encoder *intel_encoder,
Hannes Ederb358d0a2008-12-18 21:18:47 +0100498 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800499{
Eric Anholt21d40d32010-03-25 11:11:14 -0700500 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800501 u8 out_buf[2], cmd_buf[2], ret_value[2], ret;
502 struct i2c_msg msgs[] = {
503 {
504 .addr = sdvo_priv->slave_addr >> 1,
505 .flags = 0,
506 .len = 2,
507 .buf = out_buf,
508 },
509 /* the following two are to read the response */
510 {
511 .addr = sdvo_priv->slave_addr >> 1,
512 .flags = 0,
513 .len = 1,
514 .buf = cmd_buf,
515 },
516 {
517 .addr = sdvo_priv->slave_addr >> 1,
518 .flags = I2C_M_RD,
519 .len = 1,
520 .buf = ret_value,
521 },
522 };
523
Eric Anholt21d40d32010-03-25 11:11:14 -0700524 intel_sdvo_debug_write(intel_encoder, SDVO_CMD_SET_CONTROL_BUS_SWITCH,
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800525 &target, 1);
526 /* write the DDC switch command argument */
Eric Anholt21d40d32010-03-25 11:11:14 -0700527 intel_sdvo_write_byte(intel_encoder, SDVO_I2C_ARG_0, target);
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800528
529 out_buf[0] = SDVO_I2C_OPCODE;
530 out_buf[1] = SDVO_CMD_SET_CONTROL_BUS_SWITCH;
531 cmd_buf[0] = SDVO_I2C_CMD_STATUS;
532 cmd_buf[1] = 0;
533 ret_value[0] = 0;
534 ret_value[1] = 0;
535
Eric Anholt21d40d32010-03-25 11:11:14 -0700536 ret = i2c_transfer(intel_encoder->i2c_bus, msgs, 3);
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800537 if (ret != 3) {
538 /* failure in I2C transfer */
539 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
540 return;
541 }
542 if (ret_value[0] != SDVO_CMD_STATUS_SUCCESS) {
543 DRM_DEBUG_KMS("DDC switch command returns response %d\n",
544 ret_value[0]);
545 return;
546 }
547 return;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548}
549
Eric Anholt21d40d32010-03-25 11:11:14 -0700550static bool intel_sdvo_set_target_input(struct intel_encoder *intel_encoder, bool target_0, bool target_1)
Jesse Barnes79e53942008-11-07 14:24:08 -0800551{
552 struct intel_sdvo_set_target_input_args targets = {0};
553 u8 status;
554
555 if (target_0 && target_1)
556 return SDVO_CMD_STATUS_NOTSUPP;
557
558 if (target_1)
559 targets.target_1 = 1;
560
Eric Anholt21d40d32010-03-25 11:11:14 -0700561 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TARGET_INPUT, &targets,
Jesse Barnes79e53942008-11-07 14:24:08 -0800562 sizeof(targets));
563
Eric Anholt21d40d32010-03-25 11:11:14 -0700564 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800565
566 return (status == SDVO_CMD_STATUS_SUCCESS);
567}
568
569/**
570 * Return whether each input is trained.
571 *
572 * This function is making an assumption about the layout of the response,
573 * which should be checked against the docs.
574 */
Eric Anholt21d40d32010-03-25 11:11:14 -0700575static bool intel_sdvo_get_trained_inputs(struct intel_encoder *intel_encoder, bool *input_1, bool *input_2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800576{
577 struct intel_sdvo_get_trained_inputs_response response;
578 u8 status;
579
Eric Anholt21d40d32010-03-25 11:11:14 -0700580 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_TRAINED_INPUTS, NULL, 0);
581 status = intel_sdvo_read_response(intel_encoder, &response, sizeof(response));
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 if (status != SDVO_CMD_STATUS_SUCCESS)
583 return false;
584
585 *input_1 = response.input0_trained;
586 *input_2 = response.input1_trained;
587 return true;
588}
589
Eric Anholt21d40d32010-03-25 11:11:14 -0700590static bool intel_sdvo_set_active_outputs(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800591 u16 outputs)
592{
593 u8 status;
594
Eric Anholt21d40d32010-03-25 11:11:14 -0700595 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_OUTPUTS, &outputs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 sizeof(outputs));
Eric Anholt21d40d32010-03-25 11:11:14 -0700597 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800598 return (status == SDVO_CMD_STATUS_SUCCESS);
599}
600
Eric Anholt21d40d32010-03-25 11:11:14 -0700601static bool intel_sdvo_set_encoder_power_state(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800602 int mode)
603{
604 u8 status, state = SDVO_ENCODER_STATE_ON;
605
606 switch (mode) {
607 case DRM_MODE_DPMS_ON:
608 state = SDVO_ENCODER_STATE_ON;
609 break;
610 case DRM_MODE_DPMS_STANDBY:
611 state = SDVO_ENCODER_STATE_STANDBY;
612 break;
613 case DRM_MODE_DPMS_SUSPEND:
614 state = SDVO_ENCODER_STATE_SUSPEND;
615 break;
616 case DRM_MODE_DPMS_OFF:
617 state = SDVO_ENCODER_STATE_OFF;
618 break;
619 }
620
Eric Anholt21d40d32010-03-25 11:11:14 -0700621 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ENCODER_POWER_STATE, &state,
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 sizeof(state));
Eric Anholt21d40d32010-03-25 11:11:14 -0700623 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800624
625 return (status == SDVO_CMD_STATUS_SUCCESS);
626}
627
Eric Anholt21d40d32010-03-25 11:11:14 -0700628static bool intel_sdvo_get_input_pixel_clock_range(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800629 int *clock_min,
630 int *clock_max)
631{
632 struct intel_sdvo_pixel_clock_range clocks;
633 u8 status;
634
Eric Anholt21d40d32010-03-25 11:11:14 -0700635 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 NULL, 0);
637
Eric Anholt21d40d32010-03-25 11:11:14 -0700638 status = intel_sdvo_read_response(intel_encoder, &clocks, sizeof(clocks));
Jesse Barnes79e53942008-11-07 14:24:08 -0800639
640 if (status != SDVO_CMD_STATUS_SUCCESS)
641 return false;
642
643 /* Convert the values from units of 10 kHz to kHz. */
644 *clock_min = clocks.min * 10;
645 *clock_max = clocks.max * 10;
646
647 return true;
648}
649
Eric Anholt21d40d32010-03-25 11:11:14 -0700650static bool intel_sdvo_set_target_output(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 u16 outputs)
652{
653 u8 status;
654
Eric Anholt21d40d32010-03-25 11:11:14 -0700655 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TARGET_OUTPUT, &outputs,
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 sizeof(outputs));
657
Eric Anholt21d40d32010-03-25 11:11:14 -0700658 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 return (status == SDVO_CMD_STATUS_SUCCESS);
660}
661
Eric Anholt21d40d32010-03-25 11:11:14 -0700662static bool intel_sdvo_set_timing(struct intel_encoder *intel_encoder, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 struct intel_sdvo_dtd *dtd)
664{
665 u8 status;
666
Eric Anholt21d40d32010-03-25 11:11:14 -0700667 intel_sdvo_write_cmd(intel_encoder, cmd, &dtd->part1, sizeof(dtd->part1));
668 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 if (status != SDVO_CMD_STATUS_SUCCESS)
670 return false;
671
Eric Anholt21d40d32010-03-25 11:11:14 -0700672 intel_sdvo_write_cmd(intel_encoder, cmd + 1, &dtd->part2, sizeof(dtd->part2));
673 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800674 if (status != SDVO_CMD_STATUS_SUCCESS)
675 return false;
676
677 return true;
678}
679
Eric Anholt21d40d32010-03-25 11:11:14 -0700680static bool intel_sdvo_set_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 struct intel_sdvo_dtd *dtd)
682{
Eric Anholt21d40d32010-03-25 11:11:14 -0700683 return intel_sdvo_set_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
685}
686
Eric Anholt21d40d32010-03-25 11:11:14 -0700687static bool intel_sdvo_set_output_timing(struct intel_encoder *intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 struct intel_sdvo_dtd *dtd)
689{
Eric Anholt21d40d32010-03-25 11:11:14 -0700690 return intel_sdvo_set_timing(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -0800691 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
692}
693
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800694static bool
Eric Anholtc751ce42010-03-25 11:48:48 -0700695intel_sdvo_create_preferred_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800696 uint16_t clock,
697 uint16_t width,
698 uint16_t height)
699{
700 struct intel_sdvo_preferred_input_timing_args args;
Eric Anholtc751ce42010-03-25 11:48:48 -0700701 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800702 uint8_t status;
703
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800704 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800705 args.clock = clock;
706 args.width = width;
707 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800708 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800709
710 if (sdvo_priv->is_lvds &&
711 (sdvo_priv->sdvo_lvds_fixed_mode->hdisplay != width ||
712 sdvo_priv->sdvo_lvds_fixed_mode->vdisplay != height))
713 args.scaled = 1;
714
Eric Anholtc751ce42010-03-25 11:48:48 -0700715 intel_sdvo_write_cmd(intel_encoder,
716 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800717 &args, sizeof(args));
Eric Anholtc751ce42010-03-25 11:48:48 -0700718 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800719 if (status != SDVO_CMD_STATUS_SUCCESS)
720 return false;
721
722 return true;
723}
724
Eric Anholtc751ce42010-03-25 11:48:48 -0700725static bool intel_sdvo_get_preferred_input_timing(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800726 struct intel_sdvo_dtd *dtd)
727{
728 bool status;
729
Eric Anholtc751ce42010-03-25 11:48:48 -0700730 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800731 NULL, 0);
732
Eric Anholtc751ce42010-03-25 11:48:48 -0700733 status = intel_sdvo_read_response(intel_encoder, &dtd->part1,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800734 sizeof(dtd->part1));
735 if (status != SDVO_CMD_STATUS_SUCCESS)
736 return false;
737
Eric Anholtc751ce42010-03-25 11:48:48 -0700738 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800739 NULL, 0);
740
Eric Anholtc751ce42010-03-25 11:48:48 -0700741 status = intel_sdvo_read_response(intel_encoder, &dtd->part2,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800742 sizeof(dtd->part2));
743 if (status != SDVO_CMD_STATUS_SUCCESS)
744 return false;
745
746 return false;
747}
Jesse Barnes79e53942008-11-07 14:24:08 -0800748
Eric Anholt21d40d32010-03-25 11:11:14 -0700749static bool intel_sdvo_set_clock_rate_mult(struct intel_encoder *intel_encoder, u8 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800750{
751 u8 status;
752
Eric Anholt21d40d32010-03-25 11:11:14 -0700753 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
754 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -0800755 if (status != SDVO_CMD_STATUS_SUCCESS)
756 return false;
757
758 return true;
759}
760
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800761static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
762 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800763{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800764 uint16_t width, height;
765 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
766 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800767
768 width = mode->crtc_hdisplay;
769 height = mode->crtc_vdisplay;
770
771 /* do some mode translations */
772 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
773 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
774
775 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
776 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
777
778 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
779 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
780
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800781 dtd->part1.clock = mode->clock / 10;
782 dtd->part1.h_active = width & 0xff;
783 dtd->part1.h_blank = h_blank_len & 0xff;
784 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800785 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800786 dtd->part1.v_active = height & 0xff;
787 dtd->part1.v_blank = v_blank_len & 0xff;
788 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800789 ((v_blank_len >> 8) & 0xf);
790
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800791 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800792 dtd->part2.h_sync_width = h_sync_len & 0xff;
793 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800794 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800795 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800796 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
797 ((v_sync_len & 0x30) >> 4);
798
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800799 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800800 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800801 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800802 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800803 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800804
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800805 dtd->part2.sdvo_flags = 0;
806 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
807 dtd->part2.reserved = 0;
808}
Jesse Barnes79e53942008-11-07 14:24:08 -0800809
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800810static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
811 struct intel_sdvo_dtd *dtd)
812{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800813 mode->hdisplay = dtd->part1.h_active;
814 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
815 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800816 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800817 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
818 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
819 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
820 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
821
822 mode->vdisplay = dtd->part1.v_active;
823 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
824 mode->vsync_start = mode->vdisplay;
825 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800826 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800827 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
828 mode->vsync_end = mode->vsync_start +
829 (dtd->part2.v_sync_off_width & 0xf);
830 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
831 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
832 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
833
834 mode->clock = dtd->part1.clock * 10;
835
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800836 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800837 if (dtd->part2.dtd_flags & 0x2)
838 mode->flags |= DRM_MODE_FLAG_PHSYNC;
839 if (dtd->part2.dtd_flags & 0x4)
840 mode->flags |= DRM_MODE_FLAG_PVSYNC;
841}
842
Eric Anholtc751ce42010-03-25 11:48:48 -0700843static bool intel_sdvo_get_supp_encode(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800844 struct intel_sdvo_encode *encode)
845{
846 uint8_t status;
847
Eric Anholtc751ce42010-03-25 11:48:48 -0700848 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_SUPP_ENCODE, NULL, 0);
849 status = intel_sdvo_read_response(intel_encoder, encode, sizeof(*encode));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800850 if (status != SDVO_CMD_STATUS_SUCCESS) { /* non-support means DVI */
851 memset(encode, 0, sizeof(*encode));
852 return false;
853 }
854
855 return true;
856}
857
Eric Anholtc751ce42010-03-25 11:48:48 -0700858static bool intel_sdvo_set_encode(struct intel_encoder *intel_encoder,
859 uint8_t mode)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800860{
861 uint8_t status;
862
Eric Anholtc751ce42010-03-25 11:48:48 -0700863 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ENCODE, &mode, 1);
864 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800865
866 return (status == SDVO_CMD_STATUS_SUCCESS);
867}
868
Eric Anholtc751ce42010-03-25 11:48:48 -0700869static bool intel_sdvo_set_colorimetry(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800870 uint8_t mode)
871{
872 uint8_t status;
873
Eric Anholtc751ce42010-03-25 11:48:48 -0700874 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
875 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800876
877 return (status == SDVO_CMD_STATUS_SUCCESS);
878}
879
880#if 0
Eric Anholtc751ce42010-03-25 11:48:48 -0700881static void intel_sdvo_dump_hdmi_buf(struct intel_encoder *intel_encoder)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800882{
883 int i, j;
884 uint8_t set_buf_index[2];
885 uint8_t av_split;
886 uint8_t buf_size;
887 uint8_t buf[48];
888 uint8_t *pos;
889
Eric Anholtc751ce42010-03-25 11:48:48 -0700890 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, NULL, 0);
891 intel_sdvo_read_response(encoder, &av_split, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800892
893 for (i = 0; i <= av_split; i++) {
894 set_buf_index[0] = i; set_buf_index[1] = 0;
Eric Anholtc751ce42010-03-25 11:48:48 -0700895 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800896 set_buf_index, 2);
Eric Anholtc751ce42010-03-25 11:48:48 -0700897 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
898 intel_sdvo_read_response(encoder, &buf_size, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800899
900 pos = buf;
901 for (j = 0; j <= buf_size; j += 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700902 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800903 NULL, 0);
Eric Anholtc751ce42010-03-25 11:48:48 -0700904 intel_sdvo_read_response(encoder, pos, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800905 pos += 8;
906 }
907 }
908}
909#endif
910
Eric Anholtc751ce42010-03-25 11:48:48 -0700911static void intel_sdvo_set_hdmi_buf(struct intel_encoder *intel_encoder,
912 int index,
913 uint8_t *data, int8_t size, uint8_t tx_rate)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800914{
915 uint8_t set_buf_index[2];
916
917 set_buf_index[0] = index;
918 set_buf_index[1] = 0;
919
Eric Anholtc751ce42010-03-25 11:48:48 -0700920 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_INDEX,
921 set_buf_index, 2);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800922
923 for (; size > 0; size -= 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700924 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_DATA, data, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800925 data += 8;
926 }
927
Eric Anholtc751ce42010-03-25 11:48:48 -0700928 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800929}
930
931static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
932{
933 uint8_t csum = 0;
934 int i;
935
936 for (i = 0; i < size; i++)
937 csum += data[i];
938
939 return 0x100 - csum;
940}
941
942#define DIP_TYPE_AVI 0x82
943#define DIP_VERSION_AVI 0x2
944#define DIP_LEN_AVI 13
945
946struct dip_infoframe {
947 uint8_t type;
948 uint8_t version;
949 uint8_t len;
950 uint8_t checksum;
951 union {
952 struct {
953 /* Packet Byte #1 */
954 uint8_t S:2;
955 uint8_t B:2;
956 uint8_t A:1;
957 uint8_t Y:2;
958 uint8_t rsvd1:1;
959 /* Packet Byte #2 */
960 uint8_t R:4;
961 uint8_t M:2;
962 uint8_t C:2;
963 /* Packet Byte #3 */
964 uint8_t SC:2;
965 uint8_t Q:2;
966 uint8_t EC:3;
967 uint8_t ITC:1;
968 /* Packet Byte #4 */
969 uint8_t VIC:7;
970 uint8_t rsvd2:1;
971 /* Packet Byte #5 */
972 uint8_t PR:4;
973 uint8_t rsvd3:4;
974 /* Packet Byte #6~13 */
975 uint16_t top_bar_end;
976 uint16_t bottom_bar_start;
977 uint16_t left_bar_end;
978 uint16_t right_bar_start;
979 } avi;
980 struct {
981 /* Packet Byte #1 */
982 uint8_t channel_count:3;
983 uint8_t rsvd1:1;
984 uint8_t coding_type:4;
985 /* Packet Byte #2 */
986 uint8_t sample_size:2; /* SS0, SS1 */
987 uint8_t sample_frequency:3;
988 uint8_t rsvd2:3;
989 /* Packet Byte #3 */
990 uint8_t coding_type_private:5;
991 uint8_t rsvd3:3;
992 /* Packet Byte #4 */
993 uint8_t channel_allocation;
994 /* Packet Byte #5 */
995 uint8_t rsvd4:3;
996 uint8_t level_shift:4;
997 uint8_t downmix_inhibit:1;
998 } audio;
999 uint8_t payload[28];
1000 } __attribute__ ((packed)) u;
1001} __attribute__((packed));
1002
Eric Anholtc751ce42010-03-25 11:48:48 -07001003static void intel_sdvo_set_avi_infoframe(struct intel_encoder *intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001004 struct drm_display_mode * mode)
1005{
1006 struct dip_infoframe avi_if = {
1007 .type = DIP_TYPE_AVI,
1008 .version = DIP_VERSION_AVI,
1009 .len = DIP_LEN_AVI,
1010 };
1011
1012 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
1013 4 + avi_if.len);
Eric Anholtc751ce42010-03-25 11:48:48 -07001014 intel_sdvo_set_hdmi_buf(intel_encoder, 1, (uint8_t *)&avi_if,
1015 4 + avi_if.len,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001016 SDVO_HBUF_TX_VSYNC);
1017}
1018
Eric Anholtc751ce42010-03-25 11:48:48 -07001019static void intel_sdvo_set_tv_format(struct intel_encoder *intel_encoder)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001020{
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001021
Zhao Yakuice6feab2009-08-24 13:50:26 +08001022 struct intel_sdvo_tv_format format;
Eric Anholtc751ce42010-03-25 11:48:48 -07001023 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001024 uint32_t format_map, i;
1025 uint8_t status;
1026
1027 for (i = 0; i < TV_FORMAT_NUM; i++)
1028 if (tv_format_names[i] == sdvo_priv->tv_format_name)
1029 break;
1030
1031 format_map = 1 << i;
1032 memset(&format, 0, sizeof(format));
1033 memcpy(&format, &format_map, sizeof(format_map) > sizeof(format) ?
1034 sizeof(format) : sizeof(format_map));
1035
Zhao Yakui8a1837c2010-03-30 15:15:02 +08001036 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_TV_FORMAT, &format,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001037 sizeof(format));
1038
Eric Anholtc751ce42010-03-25 11:48:48 -07001039 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Zhao Yakuice6feab2009-08-24 13:50:26 +08001040 if (status != SDVO_CMD_STATUS_SUCCESS)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001041 DRM_DEBUG_KMS("%s: Failed to set TV format\n",
Zhao Yakuice6feab2009-08-24 13:50:26 +08001042 SDVO_NAME(sdvo_priv));
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001043}
1044
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001045static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
1046 struct drm_display_mode *mode,
1047 struct drm_display_mode *adjusted_mode)
1048{
Eric Anholtc751ce42010-03-25 11:48:48 -07001049 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1050 struct intel_sdvo_priv *dev_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001051
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001052 if (dev_priv->is_tv) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001053 struct intel_sdvo_dtd output_dtd;
1054 bool success;
1055
1056 /* We need to construct preferred input timings based on our
1057 * output timings. To do that, we have to set the output
1058 * timings, even though this isn't really the right place in
1059 * the sequence to do it. Oh well.
1060 */
1061
1062
1063 /* Set output timings */
1064 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
Eric Anholtc751ce42010-03-25 11:48:48 -07001065 intel_sdvo_set_target_output(intel_encoder,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001066 dev_priv->attached_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001067 intel_sdvo_set_output_timing(intel_encoder, &output_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001068
1069 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001070 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001071
1072
Eric Anholtc751ce42010-03-25 11:48:48 -07001073 success = intel_sdvo_create_preferred_input_timing(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001074 mode->clock / 10,
1075 mode->hdisplay,
1076 mode->vdisplay);
1077 if (success) {
1078 struct intel_sdvo_dtd input_dtd;
1079
Eric Anholtc751ce42010-03-25 11:48:48 -07001080 intel_sdvo_get_preferred_input_timing(intel_encoder,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001081 &input_dtd);
1082 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001083 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001084
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001085 drm_mode_set_crtcinfo(adjusted_mode, 0);
1086
1087 mode->clock = adjusted_mode->clock;
1088
1089 adjusted_mode->clock *=
1090 intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001091 } else {
1092 return false;
1093 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001094 } else if (dev_priv->is_lvds) {
1095 struct intel_sdvo_dtd output_dtd;
1096 bool success;
1097
1098 drm_mode_set_crtcinfo(dev_priv->sdvo_lvds_fixed_mode, 0);
1099 /* Set output timings */
1100 intel_sdvo_get_dtd_from_mode(&output_dtd,
1101 dev_priv->sdvo_lvds_fixed_mode);
1102
Eric Anholtc751ce42010-03-25 11:48:48 -07001103 intel_sdvo_set_target_output(intel_encoder,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001104 dev_priv->attached_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001105 intel_sdvo_set_output_timing(intel_encoder, &output_dtd);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001106
1107 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001108 intel_sdvo_set_target_input(intel_encoder, true, false);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001109
1110
1111 success = intel_sdvo_create_preferred_input_timing(
Eric Anholtc751ce42010-03-25 11:48:48 -07001112 intel_encoder,
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001113 mode->clock / 10,
1114 mode->hdisplay,
1115 mode->vdisplay);
1116
1117 if (success) {
1118 struct intel_sdvo_dtd input_dtd;
1119
Eric Anholtc751ce42010-03-25 11:48:48 -07001120 intel_sdvo_get_preferred_input_timing(intel_encoder,
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001121 &input_dtd);
1122 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1123 dev_priv->sdvo_flags = input_dtd.part2.sdvo_flags;
1124
1125 drm_mode_set_crtcinfo(adjusted_mode, 0);
1126
1127 mode->clock = adjusted_mode->clock;
1128
1129 adjusted_mode->clock *=
1130 intel_sdvo_get_pixel_multiplier(mode);
1131 } else {
1132 return false;
1133 }
1134
1135 } else {
1136 /* Make the CRTC code factor in the SDVO pixel multiplier. The
1137 * SDVO device will be told of the multiplier during mode_set.
1138 */
1139 adjusted_mode->clock *= intel_sdvo_get_pixel_multiplier(mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001140 }
1141 return true;
1142}
1143
1144static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1145 struct drm_display_mode *mode,
1146 struct drm_display_mode *adjusted_mode)
1147{
1148 struct drm_device *dev = encoder->dev;
1149 struct drm_i915_private *dev_priv = dev->dev_private;
1150 struct drm_crtc *crtc = encoder->crtc;
1151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07001152 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1153 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001154 u32 sdvox = 0;
1155 int sdvo_pixel_multiply;
1156 struct intel_sdvo_in_out_map in_out;
1157 struct intel_sdvo_dtd input_dtd;
1158 u8 status;
1159
1160 if (!mode)
1161 return;
1162
1163 /* First, set the input mapping for the first input to our controlled
1164 * output. This is only correct if we're a single-input device, in
1165 * which case the first input is the output from the appropriate SDVO
1166 * channel on the motherboard. In a two-input device, the first input
1167 * will be SDVOB and the second SDVOC.
1168 */
Zhenyu Wang14571b42010-03-30 14:06:33 +08001169 in_out.in0 = sdvo_priv->attached_output;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001170 in_out.in1 = 0;
1171
Eric Anholtc751ce42010-03-25 11:48:48 -07001172 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_IN_OUT_MAP,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001173 &in_out, sizeof(in_out));
Eric Anholtc751ce42010-03-25 11:48:48 -07001174 status = intel_sdvo_read_response(intel_encoder, NULL, 0);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001175
1176 if (sdvo_priv->is_hdmi) {
Eric Anholtc751ce42010-03-25 11:48:48 -07001177 intel_sdvo_set_avi_infoframe(intel_encoder, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001178 sdvox |= SDVO_AUDIO_ENABLE;
1179 }
1180
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001181 /* We have tried to get input timing in mode_fixup, and filled into
1182 adjusted_mode */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001183 if (sdvo_priv->is_tv || sdvo_priv->is_lvds) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001184 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001185 input_dtd.part2.sdvo_flags = sdvo_priv->sdvo_flags;
1186 } else
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001187 intel_sdvo_get_dtd_from_mode(&input_dtd, mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001188
1189 /* If it's a TV, we already set the output timing in mode_fixup.
1190 * Otherwise, the output timing is equal to the input timing.
1191 */
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001192 if (!sdvo_priv->is_tv && !sdvo_priv->is_lvds) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001193 /* Set the output timing to the screen */
Eric Anholtc751ce42010-03-25 11:48:48 -07001194 intel_sdvo_set_target_output(intel_encoder,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001195 sdvo_priv->attached_output);
Eric Anholtc751ce42010-03-25 11:48:48 -07001196 intel_sdvo_set_output_timing(intel_encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001197 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001198
1199 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholtc751ce42010-03-25 11:48:48 -07001200 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08001201
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001202 if (sdvo_priv->is_tv)
Eric Anholtc751ce42010-03-25 11:48:48 -07001203 intel_sdvo_set_tv_format(intel_encoder);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001204
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001205 /* We would like to use intel_sdvo_create_preferred_input_timing() to
Jesse Barnes79e53942008-11-07 14:24:08 -08001206 * provide the device with a timing it can support, if it supports that
1207 * feature. However, presumably we would need to adjust the CRTC to
1208 * output the preferred timing, and we don't support that currently.
1209 */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001210#if 0
Eric Anholtc751ce42010-03-25 11:48:48 -07001211 success = intel_sdvo_create_preferred_input_timing(encoder, clock,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001212 width, height);
1213 if (success) {
1214 struct intel_sdvo_dtd *input_dtd;
1215
Eric Anholtc751ce42010-03-25 11:48:48 -07001216 intel_sdvo_get_preferred_input_timing(encoder, &input_dtd);
1217 intel_sdvo_set_input_timing(encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001218 }
1219#else
Eric Anholtc751ce42010-03-25 11:48:48 -07001220 intel_sdvo_set_input_timing(intel_encoder, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001221#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001222
1223 switch (intel_sdvo_get_pixel_multiplier(mode)) {
1224 case 1:
Eric Anholtc751ce42010-03-25 11:48:48 -07001225 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001226 SDVO_CLOCK_RATE_MULT_1X);
1227 break;
1228 case 2:
Eric Anholtc751ce42010-03-25 11:48:48 -07001229 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001230 SDVO_CLOCK_RATE_MULT_2X);
1231 break;
1232 case 4:
Eric Anholtc751ce42010-03-25 11:48:48 -07001233 intel_sdvo_set_clock_rate_mult(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001234 SDVO_CLOCK_RATE_MULT_4X);
1235 break;
1236 }
1237
1238 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001239 if (IS_I965G(dev)) {
1240 sdvox |= SDVO_BORDER_ENABLE |
1241 SDVO_VSYNC_ACTIVE_HIGH |
1242 SDVO_HSYNC_ACTIVE_HIGH;
1243 } else {
Eric Anholtc751ce42010-03-25 11:48:48 -07001244 sdvox |= I915_READ(sdvo_priv->sdvo_reg);
1245 switch (sdvo_priv->sdvo_reg) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001246 case SDVOB:
1247 sdvox &= SDVOB_PRESERVE_MASK;
1248 break;
1249 case SDVOC:
1250 sdvox &= SDVOC_PRESERVE_MASK;
1251 break;
1252 }
1253 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1254 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001255 if (intel_crtc->pipe == 1)
1256 sdvox |= SDVO_PIPE_B_SELECT;
1257
1258 sdvo_pixel_multiply = intel_sdvo_get_pixel_multiplier(mode);
1259 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001260 /* done in crtc_mode_set as the dpll_md reg must be written early */
1261 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1262 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001263 } else {
1264 sdvox |= (sdvo_pixel_multiply - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1265 }
1266
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001267 if (sdvo_priv->sdvo_flags & SDVO_NEED_TO_STALL)
1268 sdvox |= SDVO_STALL_SELECT;
Eric Anholtc751ce42010-03-25 11:48:48 -07001269 intel_sdvo_write_sdvox(intel_encoder, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001270}
1271
1272static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1273{
1274 struct drm_device *dev = encoder->dev;
1275 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001276 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1277 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001278 u32 temp;
1279
1280 if (mode != DRM_MODE_DPMS_ON) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001281 intel_sdvo_set_active_outputs(intel_encoder, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08001282 if (0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001283 intel_sdvo_set_encoder_power_state(intel_encoder, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08001284
1285 if (mode == DRM_MODE_DPMS_OFF) {
Eric Anholtc751ce42010-03-25 11:48:48 -07001286 temp = I915_READ(sdvo_priv->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001287 if ((temp & SDVO_ENABLE) != 0) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001288 intel_sdvo_write_sdvox(intel_encoder, temp & ~SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001289 }
1290 }
1291 } else {
1292 bool input1, input2;
1293 int i;
1294 u8 status;
1295
Eric Anholtc751ce42010-03-25 11:48:48 -07001296 temp = I915_READ(sdvo_priv->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001297 if ((temp & SDVO_ENABLE) == 0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001298 intel_sdvo_write_sdvox(intel_encoder, temp | SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001299 for (i = 0; i < 2; i++)
1300 intel_wait_for_vblank(dev);
1301
Eric Anholt21d40d32010-03-25 11:11:14 -07001302 status = intel_sdvo_get_trained_inputs(intel_encoder, &input1,
Jesse Barnes79e53942008-11-07 14:24:08 -08001303 &input2);
1304
1305
1306 /* Warn if the device reported failure to sync.
1307 * A lot of SDVO devices fail to notify of sync, but it's
1308 * a given it the status is a success, we succeeded.
1309 */
1310 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001311 DRM_DEBUG_KMS("First %s output reported failure to "
1312 "sync\n", SDVO_NAME(sdvo_priv));
Jesse Barnes79e53942008-11-07 14:24:08 -08001313 }
1314
1315 if (0)
Eric Anholt21d40d32010-03-25 11:11:14 -07001316 intel_sdvo_set_encoder_power_state(intel_encoder, mode);
Zhenyu Wang14571b42010-03-30 14:06:33 +08001317 intel_sdvo_set_active_outputs(intel_encoder, sdvo_priv->attached_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001318 }
1319 return;
1320}
1321
Jesse Barnes79e53942008-11-07 14:24:08 -08001322static int intel_sdvo_mode_valid(struct drm_connector *connector,
1323 struct drm_display_mode *mode)
1324{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001325 struct drm_encoder *encoder = intel_attached_encoder(connector);
1326 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07001327 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001328
1329 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1330 return MODE_NO_DBLESCAN;
1331
1332 if (sdvo_priv->pixel_clock_min > mode->clock)
1333 return MODE_CLOCK_LOW;
1334
1335 if (sdvo_priv->pixel_clock_max < mode->clock)
1336 return MODE_CLOCK_HIGH;
1337
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001338 if (sdvo_priv->is_lvds == true) {
1339 if (sdvo_priv->sdvo_lvds_fixed_mode == NULL)
1340 return MODE_PANEL;
1341
1342 if (mode->hdisplay > sdvo_priv->sdvo_lvds_fixed_mode->hdisplay)
1343 return MODE_PANEL;
1344
1345 if (mode->vdisplay > sdvo_priv->sdvo_lvds_fixed_mode->vdisplay)
1346 return MODE_PANEL;
1347 }
1348
Jesse Barnes79e53942008-11-07 14:24:08 -08001349 return MODE_OK;
1350}
1351
Eric Anholt21d40d32010-03-25 11:11:14 -07001352static bool intel_sdvo_get_capabilities(struct intel_encoder *intel_encoder, struct intel_sdvo_caps *caps)
Jesse Barnes79e53942008-11-07 14:24:08 -08001353{
1354 u8 status;
1355
Eric Anholt21d40d32010-03-25 11:11:14 -07001356 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_DEVICE_CAPS, NULL, 0);
1357 status = intel_sdvo_read_response(intel_encoder, caps, sizeof(*caps));
Jesse Barnes79e53942008-11-07 14:24:08 -08001358 if (status != SDVO_CMD_STATUS_SUCCESS)
1359 return false;
1360
1361 return true;
1362}
1363
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001364/* No use! */
1365#if 0
Jesse Barnes79e53942008-11-07 14:24:08 -08001366struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1367{
1368 struct drm_connector *connector = NULL;
Eric Anholt21d40d32010-03-25 11:11:14 -07001369 struct intel_encoder *iout = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08001370 struct intel_sdvo_priv *sdvo;
1371
1372 /* find the sdvo connector */
1373 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001374 iout = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001375
1376 if (iout->type != INTEL_OUTPUT_SDVO)
1377 continue;
1378
1379 sdvo = iout->dev_priv;
1380
Eric Anholtc751ce42010-03-25 11:48:48 -07001381 if (sdvo->sdvo_reg == SDVOB && sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001382 return connector;
1383
Eric Anholtc751ce42010-03-25 11:48:48 -07001384 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001385 return connector;
1386
1387 }
1388
1389 return NULL;
1390}
1391
1392int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1393{
1394 u8 response[2];
1395 u8 status;
Eric Anholt21d40d32010-03-25 11:11:14 -07001396 struct intel_encoder *intel_encoder;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001397 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08001398
1399 if (!connector)
1400 return 0;
1401
Eric Anholt21d40d32010-03-25 11:11:14 -07001402 intel_encoder = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001403
Eric Anholt21d40d32010-03-25 11:11:14 -07001404 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1405 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001406
1407 if (response[0] !=0)
1408 return 1;
1409
1410 return 0;
1411}
1412
1413void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1414{
1415 u8 response[2];
1416 u8 status;
Eric Anholt21d40d32010-03-25 11:11:14 -07001417 struct intel_encoder *intel_encoder = to_intel_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001418
Eric Anholt21d40d32010-03-25 11:11:14 -07001419 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1420 intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001421
1422 if (on) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001423 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1424 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001425
Eric Anholt21d40d32010-03-25 11:11:14 -07001426 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001427 } else {
1428 response[0] = 0;
1429 response[1] = 0;
Eric Anholt21d40d32010-03-25 11:11:14 -07001430 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001431 }
1432
Eric Anholt21d40d32010-03-25 11:11:14 -07001433 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1434 intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001435}
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001436#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001437
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001438static bool
Eric Anholt21d40d32010-03-25 11:11:14 -07001439intel_sdvo_multifunc_encoder(struct intel_encoder *intel_encoder)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001440{
Eric Anholt21d40d32010-03-25 11:11:14 -07001441 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001442 int caps = 0;
1443
1444 if (sdvo_priv->caps.output_flags &
1445 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1446 caps++;
1447 if (sdvo_priv->caps.output_flags &
1448 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1449 caps++;
1450 if (sdvo_priv->caps.output_flags &
Roel Kluin19e1f882009-08-09 13:50:53 +02001451 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID1))
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001452 caps++;
1453 if (sdvo_priv->caps.output_flags &
1454 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1455 caps++;
1456 if (sdvo_priv->caps.output_flags &
1457 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1458 caps++;
1459
1460 if (sdvo_priv->caps.output_flags &
1461 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1462 caps++;
1463
1464 if (sdvo_priv->caps.output_flags &
1465 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1466 caps++;
1467
1468 return (caps > 1);
1469}
1470
Keith Packard57cdaf92009-09-04 13:07:54 +08001471static struct drm_connector *
1472intel_find_analog_connector(struct drm_device *dev)
1473{
1474 struct drm_connector *connector;
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001475 struct drm_encoder *encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07001476 struct intel_encoder *intel_encoder;
Keith Packard57cdaf92009-09-04 13:07:54 +08001477
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001478 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1479 intel_encoder = enc_to_intel_encoder(encoder);
1480 if (intel_encoder->type == INTEL_OUTPUT_ANALOG) {
1481 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1482 if (connector && encoder == intel_attached_encoder(connector))
1483 return connector;
1484 }
1485 }
Keith Packard57cdaf92009-09-04 13:07:54 +08001486 }
1487 return NULL;
1488}
1489
1490static int
1491intel_analog_is_connected(struct drm_device *dev)
1492{
1493 struct drm_connector *analog_connector;
1494 analog_connector = intel_find_analog_connector(dev);
1495
1496 if (!analog_connector)
1497 return false;
1498
1499 if (analog_connector->funcs->detect(analog_connector) ==
1500 connector_status_disconnected)
1501 return false;
1502
1503 return true;
1504}
1505
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001506enum drm_connector_status
1507intel_sdvo_hdmi_sink_detect(struct drm_connector *connector, u16 response)
Ma Ling9dff6af2009-04-02 13:13:26 +08001508{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001509 struct drm_encoder *encoder = intel_attached_encoder(connector);
1510 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07001511 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001512 enum drm_connector_status status = connector_status_connected;
Ma Ling9dff6af2009-04-02 13:13:26 +08001513 struct edid *edid = NULL;
1514
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001515 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -07001516 intel_encoder->ddc_bus);
Keith Packard57cdaf92009-09-04 13:07:54 +08001517
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001518 /* This is only applied to SDVO cards with multiple outputs */
Eric Anholt21d40d32010-03-25 11:11:14 -07001519 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_encoder)) {
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001520 uint8_t saved_ddc, temp_ddc;
1521 saved_ddc = sdvo_priv->ddc_bus;
1522 temp_ddc = sdvo_priv->ddc_bus >> 1;
1523 /*
1524 * Don't use the 1 as the argument of DDC bus switch to get
1525 * the EDID. It is used for SDVO SPD ROM.
1526 */
1527 while(temp_ddc > 1) {
1528 sdvo_priv->ddc_bus = temp_ddc;
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001529 edid = drm_get_edid(connector,
Eric Anholt21d40d32010-03-25 11:11:14 -07001530 intel_encoder->ddc_bus);
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001531 if (edid) {
1532 /*
1533 * When we can get the EDID, maybe it is the
1534 * correct DDC bus. Update it.
1535 */
1536 sdvo_priv->ddc_bus = temp_ddc;
1537 break;
1538 }
1539 temp_ddc >>= 1;
1540 }
1541 if (edid == NULL)
1542 sdvo_priv->ddc_bus = saved_ddc;
1543 }
Keith Packard57cdaf92009-09-04 13:07:54 +08001544 /* when there is no edid and no monitor is connected with VGA
1545 * port, try to use the CRT ddc to read the EDID for DVI-connector
1546 */
1547 if (edid == NULL &&
1548 sdvo_priv->analog_ddc_bus &&
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001549 !intel_analog_is_connected(connector->dev))
1550 edid = drm_get_edid(connector,
Keith Packard57cdaf92009-09-04 13:07:54 +08001551 sdvo_priv->analog_ddc_bus);
Ma Ling9dff6af2009-04-02 13:13:26 +08001552 if (edid != NULL) {
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001553 /* Don't report the output as connected if it's a DVI-I
1554 * connector with a non-digital EDID coming out.
1555 */
1556 if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)) {
1557 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1558 sdvo_priv->is_hdmi =
1559 drm_detect_hdmi_monitor(edid);
1560 else
1561 status = connector_status_disconnected;
1562 }
1563
Ma Ling9dff6af2009-04-02 13:13:26 +08001564 kfree(edid);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001565 connector->display_info.raw_edid = NULL;
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001566
1567 } else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1568 status = connector_status_disconnected;
1569
1570 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001571}
1572
Jesse Barnes79e53942008-11-07 14:24:08 -08001573static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1574{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001575 uint16_t response;
Jesse Barnes79e53942008-11-07 14:24:08 -08001576 u8 status;
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001577 struct drm_encoder *encoder = intel_attached_encoder(connector);
1578 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1579 struct intel_connector *intel_connector = to_intel_connector(connector);
Eric Anholt21d40d32010-03-25 11:11:14 -07001580 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001581 struct intel_sdvo_connector *sdvo_connector = intel_connector->dev_priv;
1582 enum drm_connector_status ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001583
Eric Anholt21d40d32010-03-25 11:11:14 -07001584 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001585 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0);
Zhao Yakuid09c23d2009-11-06 15:39:56 +08001586 if (sdvo_priv->is_tv) {
1587 /* add 30ms delay when the output type is SDVO-TV */
1588 mdelay(30);
1589 }
Eric Anholt21d40d32010-03-25 11:11:14 -07001590 status = intel_sdvo_read_response(intel_encoder, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001591
Dave Airlie51c8b402009-08-20 13:38:04 +10001592 DRM_DEBUG_KMS("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001593
1594 if (status != SDVO_CMD_STATUS_SUCCESS)
1595 return connector_status_unknown;
1596
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001597 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001598 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001599
Zhenyu Wang14571b42010-03-30 14:06:33 +08001600 sdvo_priv->attached_output = response;
1601
1602 if ((sdvo_connector->output_flag & response) == 0)
1603 ret = connector_status_disconnected;
1604 else if (response & (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1605 ret = intel_sdvo_hdmi_sink_detect(connector, response);
1606 else
1607 ret = connector_status_connected;
1608
1609 /* May update encoder flag for like clock for SDVO TV, etc.*/
1610 if (ret == connector_status_connected) {
1611 sdvo_priv->is_tv = false;
1612 sdvo_priv->is_lvds = false;
1613 intel_encoder->needs_tv_clock = false;
1614
1615 if (response & SDVO_TV_MASK) {
1616 sdvo_priv->is_tv = true;
1617 intel_encoder->needs_tv_clock = true;
1618 }
1619 if (response & SDVO_LVDS_MASK)
1620 sdvo_priv->is_lvds = true;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001621 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08001622
1623 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001624}
1625
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001626static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001627{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001628 struct drm_encoder *encoder = intel_attached_encoder(connector);
1629 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07001630 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Keith Packard57cdaf92009-09-04 13:07:54 +08001631 int num_modes;
Jesse Barnes79e53942008-11-07 14:24:08 -08001632
1633 /* set the bus switch and get the modes */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001634 num_modes = intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Jesse Barnes79e53942008-11-07 14:24:08 -08001635
Keith Packard57cdaf92009-09-04 13:07:54 +08001636 /*
1637 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1638 * link between analog and digital outputs. So, if the regular SDVO
1639 * DDC fails, check to see if the analog output is disconnected, in
1640 * which case we'll look there for the digital DDC data.
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001641 */
Keith Packard57cdaf92009-09-04 13:07:54 +08001642 if (num_modes == 0 &&
1643 sdvo_priv->analog_ddc_bus &&
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001644 !intel_analog_is_connected(connector->dev)) {
Keith Packard57cdaf92009-09-04 13:07:54 +08001645 /* Switch to the analog ddc bus and try that
1646 */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001647 (void) intel_ddc_get_modes(connector, sdvo_priv->analog_ddc_bus);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001648 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001649}
1650
1651/*
1652 * Set of SDVO TV modes.
1653 * Note! This is in reply order (see loop in get_tv_modes).
1654 * XXX: all 60Hz refresh?
1655 */
1656struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001657 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1658 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001659 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001660 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1661 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001662 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001663 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1664 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001665 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001666 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1667 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001668 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001669 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1670 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001671 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001672 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1673 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001674 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001675 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1676 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001677 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001678 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1679 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001680 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001681 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1682 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001683 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001684 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1685 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001686 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001687 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1688 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001689 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001690 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1691 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001692 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001693 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1694 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001695 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001696 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1697 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001698 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001699 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1700 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001701 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001702 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1703 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001704 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001705 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1706 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001707 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001708 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1709 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001710 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001711 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1712 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001713 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1714};
1715
1716static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1717{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001718 struct drm_encoder *encoder = intel_attached_encoder(connector);
1719 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
1720 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001721 struct intel_sdvo_sdtv_resolution_request tv_res;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001722 uint32_t reply = 0, format_map = 0;
1723 int i;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001724 uint8_t status;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001725
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001726
1727 /* Read the list of supported input resolutions for the selected TV
1728 * format.
1729 */
Zhao Yakuice6feab2009-08-24 13:50:26 +08001730 for (i = 0; i < TV_FORMAT_NUM; i++)
1731 if (tv_format_names[i] == sdvo_priv->tv_format_name)
1732 break;
1733
1734 format_map = (1 << i);
1735 memcpy(&tv_res, &format_map,
1736 sizeof(struct intel_sdvo_sdtv_resolution_request) >
1737 sizeof(format_map) ? sizeof(format_map) :
1738 sizeof(struct intel_sdvo_sdtv_resolution_request));
1739
Zhenyu Wang14571b42010-03-30 14:06:33 +08001740 intel_sdvo_set_target_output(intel_encoder, sdvo_priv->attached_output);
Zhao Yakuice6feab2009-08-24 13:50:26 +08001741
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001742 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001743 &tv_res, sizeof(tv_res));
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001744 status = intel_sdvo_read_response(intel_encoder, &reply, 3);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001745 if (status != SDVO_CMD_STATUS_SUCCESS)
1746 return;
1747
1748 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001749 if (reply & (1 << i)) {
1750 struct drm_display_mode *nmode;
1751 nmode = drm_mode_duplicate(connector->dev,
1752 &sdvo_tv_modes[i]);
1753 if (nmode)
1754 drm_mode_probed_add(connector, nmode);
1755 }
Zhao Yakuice6feab2009-08-24 13:50:26 +08001756
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001757}
1758
Ma Ling7086c872009-05-13 11:20:06 +08001759static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1760{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001761 struct drm_encoder *encoder = intel_attached_encoder(connector);
1762 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Ma Ling7086c872009-05-13 11:20:06 +08001763 struct drm_i915_private *dev_priv = connector->dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07001764 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001765 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001766
1767 /*
1768 * Attempt to get the mode list from DDC.
1769 * Assume that the preferred modes are
1770 * arranged in priority order.
1771 */
Zhenyu Wang335af9a2010-03-30 14:39:31 +08001772 intel_ddc_get_modes(connector, intel_encoder->ddc_bus);
Ma Ling7086c872009-05-13 11:20:06 +08001773 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001774 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001775
1776 /* Fetch modes from VBT */
1777 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001778 newmode = drm_mode_duplicate(connector->dev,
1779 dev_priv->sdvo_lvds_vbt_mode);
1780 if (newmode != NULL) {
1781 /* Guarantee the mode is preferred */
1782 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1783 DRM_MODE_TYPE_DRIVER);
1784 drm_mode_probed_add(connector, newmode);
1785 }
1786 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001787
1788end:
1789 list_for_each_entry(newmode, &connector->probed_modes, head) {
1790 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1791 sdvo_priv->sdvo_lvds_fixed_mode =
1792 drm_mode_duplicate(connector->dev, newmode);
1793 break;
1794 }
1795 }
1796
Ma Ling7086c872009-05-13 11:20:06 +08001797}
1798
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001799static int intel_sdvo_get_modes(struct drm_connector *connector)
1800{
Zhenyu Wang14571b42010-03-30 14:06:33 +08001801 struct intel_connector *intel_connector = to_intel_connector(connector);
1802 struct intel_sdvo_connector *sdvo_connector = intel_connector->dev_priv;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001803
Zhenyu Wang14571b42010-03-30 14:06:33 +08001804 if (IS_TV(sdvo_connector))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001805 intel_sdvo_get_tv_modes(connector);
Zhenyu Wang14571b42010-03-30 14:06:33 +08001806 else if (IS_LVDS(sdvo_connector))
Ma Ling7086c872009-05-13 11:20:06 +08001807 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001808 else
1809 intel_sdvo_get_ddc_modes(connector);
1810
Jesse Barnes79e53942008-11-07 14:24:08 -08001811 if (list_empty(&connector->probed_modes))
1812 return 0;
1813 return 1;
1814}
1815
Zhao Yakuib9219c52009-09-10 15:45:46 +08001816static
1817void intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
1818{
Zhenyu Wang14571b42010-03-30 14:06:33 +08001819 struct intel_connector *intel_connector = to_intel_connector(connector);
1820 struct intel_sdvo_connector *sdvo_priv = intel_connector->dev_priv;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001821 struct drm_device *dev = connector->dev;
1822
Zhenyu Wang14571b42010-03-30 14:06:33 +08001823 if (IS_TV(sdvo_priv)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001824 if (sdvo_priv->left_property)
1825 drm_property_destroy(dev, sdvo_priv->left_property);
1826 if (sdvo_priv->right_property)
1827 drm_property_destroy(dev, sdvo_priv->right_property);
1828 if (sdvo_priv->top_property)
1829 drm_property_destroy(dev, sdvo_priv->top_property);
1830 if (sdvo_priv->bottom_property)
1831 drm_property_destroy(dev, sdvo_priv->bottom_property);
1832 if (sdvo_priv->hpos_property)
1833 drm_property_destroy(dev, sdvo_priv->hpos_property);
1834 if (sdvo_priv->vpos_property)
1835 drm_property_destroy(dev, sdvo_priv->vpos_property);
Zhao Yakuib9219c52009-09-10 15:45:46 +08001836 if (sdvo_priv->saturation_property)
1837 drm_property_destroy(dev,
1838 sdvo_priv->saturation_property);
1839 if (sdvo_priv->contrast_property)
1840 drm_property_destroy(dev,
1841 sdvo_priv->contrast_property);
1842 if (sdvo_priv->hue_property)
1843 drm_property_destroy(dev, sdvo_priv->hue_property);
1844 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08001845 if (IS_TV(sdvo_priv) || IS_LVDS(sdvo_priv)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001846 if (sdvo_priv->brightness_property)
1847 drm_property_destroy(dev,
1848 sdvo_priv->brightness_property);
1849 }
1850 return;
1851}
1852
Jesse Barnes79e53942008-11-07 14:24:08 -08001853static void intel_sdvo_destroy(struct drm_connector *connector)
1854{
Zhenyu Wang14571b42010-03-30 14:06:33 +08001855 struct intel_connector *intel_connector = to_intel_connector(connector);
1856 struct intel_sdvo_connector *sdvo_connector = intel_connector->dev_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08001857
Zhenyu Wang14571b42010-03-30 14:06:33 +08001858 if (sdvo_connector->tv_format_property)
Zhao Yakuice6feab2009-08-24 13:50:26 +08001859 drm_property_destroy(connector->dev,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001860 sdvo_connector->tv_format_property);
Zhao Yakuice6feab2009-08-24 13:50:26 +08001861
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001862 intel_sdvo_destroy_enhance_property(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001863 drm_sysfs_connector_remove(connector);
1864 drm_connector_cleanup(connector);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001865 kfree(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001866}
1867
Zhao Yakuice6feab2009-08-24 13:50:26 +08001868static int
1869intel_sdvo_set_property(struct drm_connector *connector,
1870 struct drm_property *property,
1871 uint64_t val)
1872{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001873 struct drm_encoder *encoder = intel_attached_encoder(connector);
1874 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07001875 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001876 struct intel_connector *intel_connector = to_intel_connector(connector);
1877 struct intel_sdvo_connector *sdvo_connector = intel_connector->dev_priv;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001878 struct drm_crtc *crtc = encoder->crtc;
1879 int ret = 0;
1880 bool changed = false;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001881 uint8_t cmd, status;
1882 uint16_t temp_value;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001883
1884 ret = drm_connector_property_set_value(connector, property, val);
1885 if (ret < 0)
1886 goto out;
1887
Zhenyu Wang14571b42010-03-30 14:06:33 +08001888 if (property == sdvo_connector->tv_format_property) {
Zhao Yakuice6feab2009-08-24 13:50:26 +08001889 if (val >= TV_FORMAT_NUM) {
1890 ret = -EINVAL;
1891 goto out;
1892 }
1893 if (sdvo_priv->tv_format_name ==
Zhenyu Wang14571b42010-03-30 14:06:33 +08001894 sdvo_connector->tv_format_supported[val])
Zhao Yakuice6feab2009-08-24 13:50:26 +08001895 goto out;
1896
Zhenyu Wang14571b42010-03-30 14:06:33 +08001897 sdvo_priv->tv_format_name = sdvo_connector->tv_format_supported[val];
Zhao Yakuice6feab2009-08-24 13:50:26 +08001898 changed = true;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001899 }
1900
Zhenyu Wang14571b42010-03-30 14:06:33 +08001901 if (IS_TV(sdvo_connector) || IS_LVDS(sdvo_connector)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001902 cmd = 0;
1903 temp_value = val;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001904 if (sdvo_connector->left_property == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001905 drm_connector_property_set_value(connector,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001906 sdvo_connector->right_property, val);
1907 if (sdvo_connector->left_margin == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001908 goto out;
1909
Zhenyu Wang14571b42010-03-30 14:06:33 +08001910 sdvo_connector->left_margin = temp_value;
1911 sdvo_connector->right_margin = temp_value;
1912 temp_value = sdvo_connector->max_hscan -
1913 sdvo_connector->left_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001914 cmd = SDVO_CMD_SET_OVERSCAN_H;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001915 } else if (sdvo_connector->right_property == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001916 drm_connector_property_set_value(connector,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001917 sdvo_connector->left_property, val);
1918 if (sdvo_connector->right_margin == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001919 goto out;
1920
Zhenyu Wang14571b42010-03-30 14:06:33 +08001921 sdvo_connector->left_margin = temp_value;
1922 sdvo_connector->right_margin = temp_value;
1923 temp_value = sdvo_connector->max_hscan -
1924 sdvo_connector->left_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001925 cmd = SDVO_CMD_SET_OVERSCAN_H;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001926 } else if (sdvo_connector->top_property == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001927 drm_connector_property_set_value(connector,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001928 sdvo_connector->bottom_property, val);
1929 if (sdvo_connector->top_margin == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001930 goto out;
1931
Zhenyu Wang14571b42010-03-30 14:06:33 +08001932 sdvo_connector->top_margin = temp_value;
1933 sdvo_connector->bottom_margin = temp_value;
1934 temp_value = sdvo_connector->max_vscan -
1935 sdvo_connector->top_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001936 cmd = SDVO_CMD_SET_OVERSCAN_V;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001937 } else if (sdvo_connector->bottom_property == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001938 drm_connector_property_set_value(connector,
Zhenyu Wang14571b42010-03-30 14:06:33 +08001939 sdvo_connector->top_property, val);
1940 if (sdvo_connector->bottom_margin == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001941 goto out;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001942 sdvo_connector->top_margin = temp_value;
1943 sdvo_connector->bottom_margin = temp_value;
1944 temp_value = sdvo_connector->max_vscan -
1945 sdvo_connector->top_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001946 cmd = SDVO_CMD_SET_OVERSCAN_V;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001947 } else if (sdvo_connector->hpos_property == property) {
1948 if (sdvo_connector->cur_hpos == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001949 goto out;
1950
1951 cmd = SDVO_CMD_SET_POSITION_H;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001952 sdvo_connector->cur_hpos = temp_value;
1953 } else if (sdvo_connector->vpos_property == property) {
1954 if (sdvo_connector->cur_vpos == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001955 goto out;
1956
1957 cmd = SDVO_CMD_SET_POSITION_V;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001958 sdvo_connector->cur_vpos = temp_value;
1959 } else if (sdvo_connector->saturation_property == property) {
1960 if (sdvo_connector->cur_saturation == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001961 goto out;
1962
1963 cmd = SDVO_CMD_SET_SATURATION;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001964 sdvo_connector->cur_saturation = temp_value;
1965 } else if (sdvo_connector->contrast_property == property) {
1966 if (sdvo_connector->cur_contrast == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001967 goto out;
1968
1969 cmd = SDVO_CMD_SET_CONTRAST;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001970 sdvo_connector->cur_contrast = temp_value;
1971 } else if (sdvo_connector->hue_property == property) {
1972 if (sdvo_connector->cur_hue == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001973 goto out;
1974
1975 cmd = SDVO_CMD_SET_HUE;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001976 sdvo_connector->cur_hue = temp_value;
1977 } else if (sdvo_connector->brightness_property == property) {
1978 if (sdvo_connector->cur_brightness == temp_value)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001979 goto out;
1980
1981 cmd = SDVO_CMD_SET_BRIGHTNESS;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001982 sdvo_connector->cur_brightness = temp_value;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001983 }
1984 if (cmd) {
Eric Anholt21d40d32010-03-25 11:11:14 -07001985 intel_sdvo_write_cmd(intel_encoder, cmd, &temp_value, 2);
1986 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08001987 NULL, 0);
1988 if (status != SDVO_CMD_STATUS_SUCCESS) {
1989 DRM_DEBUG_KMS("Incorrect SDVO command \n");
1990 return -EINVAL;
1991 }
1992 changed = true;
1993 }
1994 }
Zhao Yakuice6feab2009-08-24 13:50:26 +08001995 if (changed && crtc)
1996 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
1997 crtc->y, crtc->fb);
1998out:
1999 return ret;
2000}
2001
Jesse Barnes79e53942008-11-07 14:24:08 -08002002static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
2003 .dpms = intel_sdvo_dpms,
2004 .mode_fixup = intel_sdvo_mode_fixup,
2005 .prepare = intel_encoder_prepare,
2006 .mode_set = intel_sdvo_mode_set,
2007 .commit = intel_encoder_commit,
2008};
2009
2010static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07002011 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08002012 .detect = intel_sdvo_detect,
2013 .fill_modes = drm_helper_probe_single_connector_modes,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002014 .set_property = intel_sdvo_set_property,
Jesse Barnes79e53942008-11-07 14:24:08 -08002015 .destroy = intel_sdvo_destroy,
2016};
2017
2018static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2019 .get_modes = intel_sdvo_get_modes,
2020 .mode_valid = intel_sdvo_mode_valid,
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002021 .best_encoder = intel_attached_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08002022};
2023
Hannes Ederb358d0a2008-12-18 21:18:47 +01002024static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08002025{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002026 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
2027 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
2028
2029 if (intel_encoder->i2c_bus)
2030 intel_i2c_destroy(intel_encoder->i2c_bus);
2031 if (intel_encoder->ddc_bus)
2032 intel_i2c_destroy(intel_encoder->ddc_bus);
2033 if (sdvo_priv->analog_ddc_bus)
2034 intel_i2c_destroy(sdvo_priv->analog_ddc_bus);
2035
2036 if (sdvo_priv->sdvo_lvds_fixed_mode != NULL)
2037 drm_mode_destroy(encoder->dev,
2038 sdvo_priv->sdvo_lvds_fixed_mode);
2039
Jesse Barnes79e53942008-11-07 14:24:08 -08002040 drm_encoder_cleanup(encoder);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002041 kfree(intel_encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08002042}
2043
2044static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2045 .destroy = intel_sdvo_enc_destroy,
2046};
2047
2048
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002049/**
2050 * Choose the appropriate DDC bus for control bus switch command for this
2051 * SDVO output based on the controlled output.
2052 *
2053 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2054 * outputs, then LVDS outputs.
2055 */
2056static void
2057intel_sdvo_select_ddc_bus(struct intel_sdvo_priv *dev_priv)
2058{
2059 uint16_t mask = 0;
2060 unsigned int num_bits;
2061
2062 /* Make a mask of outputs less than or equal to our own priority in the
2063 * list.
2064 */
2065 switch (dev_priv->controlled_output) {
2066 case SDVO_OUTPUT_LVDS1:
2067 mask |= SDVO_OUTPUT_LVDS1;
2068 case SDVO_OUTPUT_LVDS0:
2069 mask |= SDVO_OUTPUT_LVDS0;
2070 case SDVO_OUTPUT_TMDS1:
2071 mask |= SDVO_OUTPUT_TMDS1;
2072 case SDVO_OUTPUT_TMDS0:
2073 mask |= SDVO_OUTPUT_TMDS0;
2074 case SDVO_OUTPUT_RGB1:
2075 mask |= SDVO_OUTPUT_RGB1;
2076 case SDVO_OUTPUT_RGB0:
2077 mask |= SDVO_OUTPUT_RGB0;
2078 break;
2079 }
2080
2081 /* Count bits to find what number we are in the priority list. */
2082 mask &= dev_priv->caps.output_flags;
2083 num_bits = hweight16(mask);
2084 if (num_bits > 3) {
2085 /* if more than 3 outputs, default to DDC bus 3 for now */
2086 num_bits = 3;
2087 }
2088
2089 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2090 dev_priv->ddc_bus = 1 << num_bits;
2091}
2092
2093static bool
Zhenyu Wang14571b42010-03-30 14:06:33 +08002094intel_sdvo_get_digital_encoding_mode(struct intel_encoder *output, int device)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002095{
2096 struct intel_sdvo_priv *sdvo_priv = output->dev_priv;
2097 uint8_t status;
2098
Zhenyu Wang14571b42010-03-30 14:06:33 +08002099 if (device == 0)
2100 intel_sdvo_set_target_output(output, SDVO_OUTPUT_TMDS0);
2101 else
2102 intel_sdvo_set_target_output(output, SDVO_OUTPUT_TMDS1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002103
2104 intel_sdvo_write_cmd(output, SDVO_CMD_GET_ENCODE, NULL, 0);
2105 status = intel_sdvo_read_response(output, &sdvo_priv->is_hdmi, 1);
2106 if (status != SDVO_CMD_STATUS_SUCCESS)
2107 return false;
2108 return true;
2109}
2110
Eric Anholt21d40d32010-03-25 11:11:14 -07002111static struct intel_encoder *
2112intel_sdvo_chan_to_intel_encoder(struct intel_i2c_chan *chan)
Ma Ling619ac3b2009-05-18 16:12:46 +08002113{
2114 struct drm_device *dev = chan->drm_dev;
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002115 struct drm_encoder *encoder;
Eric Anholt21d40d32010-03-25 11:11:14 -07002116 struct intel_encoder *intel_encoder = NULL;
Ma Ling619ac3b2009-05-18 16:12:46 +08002117
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002118 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
2119 intel_encoder = enc_to_intel_encoder(encoder);
2120 if (intel_encoder->ddc_bus == &chan->adapter)
Ma Ling619ac3b2009-05-18 16:12:46 +08002121 break;
Ma Ling619ac3b2009-05-18 16:12:46 +08002122 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002123 return intel_encoder;
Ma Ling619ac3b2009-05-18 16:12:46 +08002124}
2125
2126static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
2127 struct i2c_msg msgs[], int num)
2128{
Eric Anholt21d40d32010-03-25 11:11:14 -07002129 struct intel_encoder *intel_encoder;
Ma Ling619ac3b2009-05-18 16:12:46 +08002130 struct intel_sdvo_priv *sdvo_priv;
2131 struct i2c_algo_bit_data *algo_data;
Keith Packardf9c10a92009-05-30 12:16:25 -07002132 const struct i2c_algorithm *algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002133
2134 algo_data = (struct i2c_algo_bit_data *)i2c_adap->algo_data;
Eric Anholt21d40d32010-03-25 11:11:14 -07002135 intel_encoder =
2136 intel_sdvo_chan_to_intel_encoder(
Ma Ling619ac3b2009-05-18 16:12:46 +08002137 (struct intel_i2c_chan *)(algo_data->data));
Eric Anholt21d40d32010-03-25 11:11:14 -07002138 if (intel_encoder == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002139 return -EINVAL;
2140
Eric Anholt21d40d32010-03-25 11:11:14 -07002141 sdvo_priv = intel_encoder->dev_priv;
2142 algo = intel_encoder->i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002143
Eric Anholt21d40d32010-03-25 11:11:14 -07002144 intel_sdvo_set_control_bus_switch(intel_encoder, sdvo_priv->ddc_bus);
Ma Ling619ac3b2009-05-18 16:12:46 +08002145 return algo->master_xfer(i2c_adap, msgs, num);
2146}
2147
2148static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
2149 .master_xfer = intel_sdvo_master_xfer,
2150};
2151
yakui_zhao714605e2009-05-31 17:18:07 +08002152static u8
Eric Anholtc751ce42010-03-25 11:48:48 -07002153intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
yakui_zhao714605e2009-05-31 17:18:07 +08002154{
2155 struct drm_i915_private *dev_priv = dev->dev_private;
2156 struct sdvo_device_mapping *my_mapping, *other_mapping;
2157
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002158 if (IS_SDVOB(sdvo_reg)) {
yakui_zhao714605e2009-05-31 17:18:07 +08002159 my_mapping = &dev_priv->sdvo_mappings[0];
2160 other_mapping = &dev_priv->sdvo_mappings[1];
2161 } else {
2162 my_mapping = &dev_priv->sdvo_mappings[1];
2163 other_mapping = &dev_priv->sdvo_mappings[0];
2164 }
2165
2166 /* If the BIOS described our SDVO device, take advantage of it. */
2167 if (my_mapping->slave_addr)
2168 return my_mapping->slave_addr;
2169
2170 /* If the BIOS only described a different SDVO device, use the
2171 * address that it isn't using.
2172 */
2173 if (other_mapping->slave_addr) {
2174 if (other_mapping->slave_addr == 0x70)
2175 return 0x72;
2176 else
2177 return 0x70;
2178 }
2179
2180 /* No SDVO device info is found for another DVO port,
2181 * so use mapping assumption we had before BIOS parsing.
2182 */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002183 if (IS_SDVOB(sdvo_reg))
yakui_zhao714605e2009-05-31 17:18:07 +08002184 return 0x70;
2185 else
2186 return 0x72;
2187}
2188
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002189static bool
Zhenyu Wang14571b42010-03-30 14:06:33 +08002190intel_sdvo_connector_alloc (struct intel_connector **ret)
Jesse Barnes79e53942008-11-07 14:24:08 -08002191{
Zhenyu Wang14571b42010-03-30 14:06:33 +08002192 struct intel_connector *intel_connector;
2193 struct intel_sdvo_connector *sdvo_connector;
2194
2195 *ret = kzalloc(sizeof(*intel_connector) +
2196 sizeof(*sdvo_connector), GFP_KERNEL);
2197 if (!*ret)
2198 return false;
2199
2200 intel_connector = *ret;
2201 sdvo_connector = (struct intel_sdvo_connector *)(intel_connector + 1);
2202 intel_connector->dev_priv = sdvo_connector;
2203
2204 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002205}
2206
Zhenyu Wang14571b42010-03-30 14:06:33 +08002207static void
2208intel_sdvo_connector_create (struct drm_encoder *encoder,
2209 struct drm_connector *connector)
2210{
2211 drm_connector_init(encoder->dev, connector, &intel_sdvo_connector_funcs,
2212 connector->connector_type);
Jesse Barnes79e53942008-11-07 14:24:08 -08002213
Zhenyu Wang14571b42010-03-30 14:06:33 +08002214 drm_connector_helper_add(connector, &intel_sdvo_connector_helper_funcs);
2215
2216 connector->interlace_allowed = 0;
2217 connector->doublescan_allowed = 0;
2218 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2219
2220 drm_mode_connector_attach_encoder(connector, encoder);
2221 drm_sysfs_connector_add(connector);
2222}
2223
2224static bool
2225intel_sdvo_dvi_init(struct intel_encoder *intel_encoder, int device)
2226{
Eric Anholt21d40d32010-03-25 11:11:14 -07002227 struct drm_encoder *encoder = &intel_encoder->enc;
2228 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002229 struct drm_connector *connector;
2230 struct intel_connector *intel_connector;
2231 struct intel_sdvo_connector *sdvo_connector;
2232
2233 if (!intel_sdvo_connector_alloc(&intel_connector))
2234 return false;
2235
2236 sdvo_connector = intel_connector->dev_priv;
2237
2238 if (device == 0) {
2239 sdvo_priv->controlled_output |= SDVO_OUTPUT_TMDS0;
2240 sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
2241 } else if (device == 1) {
2242 sdvo_priv->controlled_output |= SDVO_OUTPUT_TMDS1;
2243 sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
2244 }
2245
2246 connector = &intel_connector->base;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002247 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002248 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2249 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2250
2251 if (intel_sdvo_get_supp_encode(intel_encoder, &sdvo_priv->encode)
2252 && intel_sdvo_get_digital_encoding_mode(intel_encoder, device)
2253 && sdvo_priv->is_hdmi) {
2254 /* enable hdmi encoding mode if supported */
2255 intel_sdvo_set_encode(intel_encoder, SDVO_ENCODE_HDMI);
2256 intel_sdvo_set_colorimetry(intel_encoder,
2257 SDVO_COLORIMETRY_RGB256);
2258 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2259 }
2260 intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2261 (1 << INTEL_ANALOG_CLONE_BIT);
2262
2263 intel_sdvo_connector_create(encoder, connector);
2264
2265 return true;
2266}
2267
2268static bool
2269intel_sdvo_tv_init(struct intel_encoder *intel_encoder, int type)
2270{
2271 struct drm_encoder *encoder = &intel_encoder->enc;
2272 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
2273 struct drm_connector *connector;
2274 struct intel_connector *intel_connector;
2275 struct intel_sdvo_connector *sdvo_connector;
2276
2277 if (!intel_sdvo_connector_alloc(&intel_connector))
2278 return false;
2279
2280 connector = &intel_connector->base;
2281 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2282 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2283 sdvo_connector = intel_connector->dev_priv;
2284
2285 sdvo_priv->controlled_output |= type;
2286 sdvo_connector->output_flag = type;
2287
2288 sdvo_priv->is_tv = true;
2289 intel_encoder->needs_tv_clock = true;
2290 intel_encoder->clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
2291
2292 intel_sdvo_connector_create(encoder, connector);
2293
2294 intel_sdvo_tv_create_property(connector, type);
2295
2296 intel_sdvo_create_enhance_property(connector);
2297
2298 return true;
2299}
2300
2301static bool
2302intel_sdvo_analog_init(struct intel_encoder *intel_encoder, int device)
2303{
2304 struct drm_encoder *encoder = &intel_encoder->enc;
2305 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
2306 struct drm_connector *connector;
2307 struct intel_connector *intel_connector;
2308 struct intel_sdvo_connector *sdvo_connector;
2309
2310 if (!intel_sdvo_connector_alloc(&intel_connector))
2311 return false;
2312
2313 connector = &intel_connector->base;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002314 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002315 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2316 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2317 sdvo_connector = intel_connector->dev_priv;
2318
2319 if (device == 0) {
2320 sdvo_priv->controlled_output |= SDVO_OUTPUT_RGB0;
2321 sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2322 } else if (device == 1) {
2323 sdvo_priv->controlled_output |= SDVO_OUTPUT_RGB1;
2324 sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2325 }
2326
2327 intel_encoder->clone_mask = (1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2328 (1 << INTEL_ANALOG_CLONE_BIT);
2329
2330 intel_sdvo_connector_create(encoder, connector);
2331 return true;
2332}
2333
2334static bool
2335intel_sdvo_lvds_init(struct intel_encoder *intel_encoder, int device)
2336{
2337 struct drm_encoder *encoder = &intel_encoder->enc;
2338 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
2339 struct drm_connector *connector;
2340 struct intel_connector *intel_connector;
2341 struct intel_sdvo_connector *sdvo_connector;
2342
2343 if (!intel_sdvo_connector_alloc(&intel_connector))
2344 return false;
2345
2346 connector = &intel_connector->base;
2347 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2348 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2349 sdvo_connector = intel_connector->dev_priv;
2350
2351 sdvo_priv->is_lvds = true;
2352
2353 if (device == 0) {
2354 sdvo_priv->controlled_output |= SDVO_OUTPUT_LVDS0;
2355 sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2356 } else if (device == 1) {
2357 sdvo_priv->controlled_output |= SDVO_OUTPUT_LVDS1;
2358 sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2359 }
2360
2361 intel_encoder->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT) |
2362 (1 << INTEL_SDVO_LVDS_CLONE_BIT);
2363
2364 intel_sdvo_connector_create(encoder, connector);
2365 intel_sdvo_create_enhance_property(connector);
2366 return true;
2367}
Jesse Barnes79e53942008-11-07 14:24:08 -08002368
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002369static bool
2370intel_sdvo_output_setup(struct intel_encoder *intel_encoder, uint16_t flags)
2371{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002372 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002373
2374 sdvo_priv->is_tv = false;
Eric Anholt21d40d32010-03-25 11:11:14 -07002375 intel_encoder->needs_tv_clock = false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002376 sdvo_priv->is_lvds = false;
2377
Zhenyu Wang14571b42010-03-30 14:06:33 +08002378 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002379
Zhenyu Wang14571b42010-03-30 14:06:33 +08002380 if (flags & SDVO_OUTPUT_TMDS0)
2381 if (!intel_sdvo_dvi_init(intel_encoder, 0))
2382 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002383
Zhenyu Wang14571b42010-03-30 14:06:33 +08002384 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2385 if (!intel_sdvo_dvi_init(intel_encoder, 1))
2386 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002387
Zhenyu Wang14571b42010-03-30 14:06:33 +08002388 /* TV has no XXX1 function block */
Zhenyu Wanga1f4b7ff2010-03-29 23:16:13 +08002389 if (flags & SDVO_OUTPUT_SVID0)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002390 if (!intel_sdvo_tv_init(intel_encoder, SDVO_OUTPUT_SVID0))
2391 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002392
Zhenyu Wang14571b42010-03-30 14:06:33 +08002393 if (flags & SDVO_OUTPUT_CVBS0)
2394 if (!intel_sdvo_tv_init(intel_encoder, SDVO_OUTPUT_CVBS0))
2395 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002396
Zhenyu Wang14571b42010-03-30 14:06:33 +08002397 if (flags & SDVO_OUTPUT_RGB0)
2398 if (!intel_sdvo_analog_init(intel_encoder, 0))
2399 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002400
Zhenyu Wang14571b42010-03-30 14:06:33 +08002401 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2402 if (!intel_sdvo_analog_init(intel_encoder, 1))
2403 return false;
Zhao Yakui2dd87382010-01-27 16:32:46 +08002404
Zhenyu Wang14571b42010-03-30 14:06:33 +08002405 if (flags & SDVO_OUTPUT_LVDS0)
2406 if (!intel_sdvo_lvds_init(intel_encoder, 0))
2407 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002408
Zhenyu Wang14571b42010-03-30 14:06:33 +08002409 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2410 if (!intel_sdvo_lvds_init(intel_encoder, 1))
2411 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002412
Zhenyu Wang14571b42010-03-30 14:06:33 +08002413 if ((flags & SDVO_OUTPUT_MASK) == 0) {
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002414 unsigned char bytes[2];
2415
2416 sdvo_priv->controlled_output = 0;
2417 memcpy(bytes, &sdvo_priv->caps.output_flags, 2);
Dave Airlie51c8b402009-08-20 13:38:04 +10002418 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2419 SDVO_NAME(sdvo_priv),
2420 bytes[0], bytes[1]);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002421 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002422 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002423 intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002424
Zhenyu Wang14571b42010-03-30 14:06:33 +08002425 return true;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002426}
2427
Zhenyu Wang14571b42010-03-30 14:06:33 +08002428static void intel_sdvo_tv_create_property(struct drm_connector *connector, int type)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002429{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002430 struct drm_encoder *encoder = intel_attached_encoder(connector);
2431 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Eric Anholt21d40d32010-03-25 11:11:14 -07002432 struct intel_sdvo_priv *sdvo_priv = intel_encoder->dev_priv;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002433 struct intel_connector *intel_connector = to_intel_connector(connector);
2434 struct intel_sdvo_connector *sdvo_connector = intel_connector->dev_priv;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002435 struct intel_sdvo_tv_format format;
2436 uint32_t format_map, i;
2437 uint8_t status;
2438
Zhenyu Wang14571b42010-03-30 14:06:33 +08002439 intel_sdvo_set_target_output(intel_encoder, type);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002440
Eric Anholt21d40d32010-03-25 11:11:14 -07002441 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002442 SDVO_CMD_GET_SUPPORTED_TV_FORMATS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002443 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuice6feab2009-08-24 13:50:26 +08002444 &format, sizeof(format));
2445 if (status != SDVO_CMD_STATUS_SUCCESS)
2446 return;
2447
2448 memcpy(&format_map, &format, sizeof(format) > sizeof(format_map) ?
2449 sizeof(format_map) : sizeof(format));
2450
2451 if (format_map == 0)
2452 return;
2453
Zhenyu Wang14571b42010-03-30 14:06:33 +08002454 sdvo_connector->format_supported_num = 0;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002455 for (i = 0 ; i < TV_FORMAT_NUM; i++)
2456 if (format_map & (1 << i)) {
Zhenyu Wang14571b42010-03-30 14:06:33 +08002457 sdvo_connector->tv_format_supported
2458 [sdvo_connector->format_supported_num++] =
Zhao Yakuice6feab2009-08-24 13:50:26 +08002459 tv_format_names[i];
2460 }
2461
2462
Zhenyu Wang14571b42010-03-30 14:06:33 +08002463 sdvo_connector->tv_format_property =
Zhao Yakuice6feab2009-08-24 13:50:26 +08002464 drm_property_create(
2465 connector->dev, DRM_MODE_PROP_ENUM,
Zhenyu Wang14571b42010-03-30 14:06:33 +08002466 "mode", sdvo_connector->format_supported_num);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002467
Zhenyu Wang14571b42010-03-30 14:06:33 +08002468 for (i = 0; i < sdvo_connector->format_supported_num; i++)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002469 drm_property_add_enum(
Zhenyu Wang14571b42010-03-30 14:06:33 +08002470 sdvo_connector->tv_format_property, i,
2471 i, sdvo_connector->tv_format_supported[i]);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002472
Zhenyu Wang14571b42010-03-30 14:06:33 +08002473 sdvo_priv->tv_format_name = sdvo_connector->tv_format_supported[0];
Zhao Yakuice6feab2009-08-24 13:50:26 +08002474 drm_connector_attach_property(
Zhenyu Wang14571b42010-03-30 14:06:33 +08002475 connector, sdvo_connector->tv_format_property, 0);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002476
2477}
2478
Zhao Yakuib9219c52009-09-10 15:45:46 +08002479static void intel_sdvo_create_enhance_property(struct drm_connector *connector)
2480{
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002481 struct drm_encoder *encoder = intel_attached_encoder(connector);
2482 struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002483 struct intel_connector *intel_connector = to_intel_connector(connector);
2484 struct intel_sdvo_connector *sdvo_priv = intel_connector->dev_priv;
Zhao Yakuib9219c52009-09-10 15:45:46 +08002485 struct intel_sdvo_enhancements_reply sdvo_data;
2486 struct drm_device *dev = connector->dev;
2487 uint8_t status;
2488 uint16_t response, data_value[2];
2489
Eric Anholt21d40d32010-03-25 11:11:14 -07002490 intel_sdvo_write_cmd(intel_encoder, SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002491 NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002492 status = intel_sdvo_read_response(intel_encoder, &sdvo_data,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002493 sizeof(sdvo_data));
2494 if (status != SDVO_CMD_STATUS_SUCCESS) {
2495 DRM_DEBUG_KMS(" incorrect response is returned\n");
2496 return;
2497 }
2498 response = *((uint16_t *)&sdvo_data);
2499 if (!response) {
2500 DRM_DEBUG_KMS("No enhancement is supported\n");
2501 return;
2502 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002503 if (IS_TV(sdvo_priv)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002504 /* when horizontal overscan is supported, Add the left/right
2505 * property
2506 */
2507 if (sdvo_data.overscan_h) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002508 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002509 SDVO_CMD_GET_MAX_OVERSCAN_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002510 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002511 &data_value, 4);
2512 if (status != SDVO_CMD_STATUS_SUCCESS) {
2513 DRM_DEBUG_KMS("Incorrect SDVO max "
2514 "h_overscan\n");
2515 return;
2516 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002517 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002518 SDVO_CMD_GET_OVERSCAN_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002519 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002520 &response, 2);
2521 if (status != SDVO_CMD_STATUS_SUCCESS) {
2522 DRM_DEBUG_KMS("Incorrect SDVO h_overscan\n");
2523 return;
2524 }
2525 sdvo_priv->max_hscan = data_value[0];
2526 sdvo_priv->left_margin = data_value[0] - response;
2527 sdvo_priv->right_margin = sdvo_priv->left_margin;
2528 sdvo_priv->left_property =
2529 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2530 "left_margin", 2);
2531 sdvo_priv->left_property->values[0] = 0;
2532 sdvo_priv->left_property->values[1] = data_value[0];
2533 drm_connector_attach_property(connector,
2534 sdvo_priv->left_property,
2535 sdvo_priv->left_margin);
2536 sdvo_priv->right_property =
2537 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2538 "right_margin", 2);
2539 sdvo_priv->right_property->values[0] = 0;
2540 sdvo_priv->right_property->values[1] = data_value[0];
2541 drm_connector_attach_property(connector,
2542 sdvo_priv->right_property,
2543 sdvo_priv->right_margin);
2544 DRM_DEBUG_KMS("h_overscan: max %d, "
2545 "default %d, current %d\n",
2546 data_value[0], data_value[1], response);
2547 }
2548 if (sdvo_data.overscan_v) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002549 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002550 SDVO_CMD_GET_MAX_OVERSCAN_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002551 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002552 &data_value, 4);
2553 if (status != SDVO_CMD_STATUS_SUCCESS) {
2554 DRM_DEBUG_KMS("Incorrect SDVO max "
2555 "v_overscan\n");
2556 return;
2557 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002558 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002559 SDVO_CMD_GET_OVERSCAN_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002560 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002561 &response, 2);
2562 if (status != SDVO_CMD_STATUS_SUCCESS) {
2563 DRM_DEBUG_KMS("Incorrect SDVO v_overscan\n");
2564 return;
2565 }
2566 sdvo_priv->max_vscan = data_value[0];
2567 sdvo_priv->top_margin = data_value[0] - response;
2568 sdvo_priv->bottom_margin = sdvo_priv->top_margin;
2569 sdvo_priv->top_property =
2570 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2571 "top_margin", 2);
2572 sdvo_priv->top_property->values[0] = 0;
2573 sdvo_priv->top_property->values[1] = data_value[0];
2574 drm_connector_attach_property(connector,
2575 sdvo_priv->top_property,
2576 sdvo_priv->top_margin);
2577 sdvo_priv->bottom_property =
2578 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2579 "bottom_margin", 2);
2580 sdvo_priv->bottom_property->values[0] = 0;
2581 sdvo_priv->bottom_property->values[1] = data_value[0];
2582 drm_connector_attach_property(connector,
2583 sdvo_priv->bottom_property,
2584 sdvo_priv->bottom_margin);
2585 DRM_DEBUG_KMS("v_overscan: max %d, "
2586 "default %d, current %d\n",
2587 data_value[0], data_value[1], response);
2588 }
2589 if (sdvo_data.position_h) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002590 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002591 SDVO_CMD_GET_MAX_POSITION_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002592 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002593 &data_value, 4);
2594 if (status != SDVO_CMD_STATUS_SUCCESS) {
2595 DRM_DEBUG_KMS("Incorrect SDVO Max h_pos\n");
2596 return;
2597 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002598 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002599 SDVO_CMD_GET_POSITION_H, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002600 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002601 &response, 2);
2602 if (status != SDVO_CMD_STATUS_SUCCESS) {
2603 DRM_DEBUG_KMS("Incorrect SDVO get h_postion\n");
2604 return;
2605 }
2606 sdvo_priv->max_hpos = data_value[0];
2607 sdvo_priv->cur_hpos = response;
2608 sdvo_priv->hpos_property =
2609 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2610 "hpos", 2);
2611 sdvo_priv->hpos_property->values[0] = 0;
2612 sdvo_priv->hpos_property->values[1] = data_value[0];
2613 drm_connector_attach_property(connector,
2614 sdvo_priv->hpos_property,
2615 sdvo_priv->cur_hpos);
2616 DRM_DEBUG_KMS("h_position: max %d, "
2617 "default %d, current %d\n",
2618 data_value[0], data_value[1], response);
2619 }
2620 if (sdvo_data.position_v) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002621 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002622 SDVO_CMD_GET_MAX_POSITION_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002623 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002624 &data_value, 4);
2625 if (status != SDVO_CMD_STATUS_SUCCESS) {
2626 DRM_DEBUG_KMS("Incorrect SDVO Max v_pos\n");
2627 return;
2628 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002629 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002630 SDVO_CMD_GET_POSITION_V, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002631 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002632 &response, 2);
2633 if (status != SDVO_CMD_STATUS_SUCCESS) {
2634 DRM_DEBUG_KMS("Incorrect SDVO get v_postion\n");
2635 return;
2636 }
2637 sdvo_priv->max_vpos = data_value[0];
2638 sdvo_priv->cur_vpos = response;
2639 sdvo_priv->vpos_property =
2640 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2641 "vpos", 2);
2642 sdvo_priv->vpos_property->values[0] = 0;
2643 sdvo_priv->vpos_property->values[1] = data_value[0];
2644 drm_connector_attach_property(connector,
2645 sdvo_priv->vpos_property,
2646 sdvo_priv->cur_vpos);
2647 DRM_DEBUG_KMS("v_position: max %d, "
2648 "default %d, current %d\n",
2649 data_value[0], data_value[1], response);
2650 }
Zhao Yakuib9219c52009-09-10 15:45:46 +08002651 if (sdvo_data.saturation) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002652 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002653 SDVO_CMD_GET_MAX_SATURATION, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002654 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002655 &data_value, 4);
2656 if (status != SDVO_CMD_STATUS_SUCCESS) {
2657 DRM_DEBUG_KMS("Incorrect SDVO Max sat\n");
2658 return;
2659 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002660 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002661 SDVO_CMD_GET_SATURATION, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002662 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002663 &response, 2);
2664 if (status != SDVO_CMD_STATUS_SUCCESS) {
2665 DRM_DEBUG_KMS("Incorrect SDVO get sat\n");
2666 return;
2667 }
2668 sdvo_priv->max_saturation = data_value[0];
2669 sdvo_priv->cur_saturation = response;
2670 sdvo_priv->saturation_property =
2671 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2672 "saturation", 2);
2673 sdvo_priv->saturation_property->values[0] = 0;
2674 sdvo_priv->saturation_property->values[1] =
2675 data_value[0];
2676 drm_connector_attach_property(connector,
2677 sdvo_priv->saturation_property,
2678 sdvo_priv->cur_saturation);
2679 DRM_DEBUG_KMS("saturation: max %d, "
2680 "default %d, current %d\n",
2681 data_value[0], data_value[1], response);
2682 }
2683 if (sdvo_data.contrast) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002684 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002685 SDVO_CMD_GET_MAX_CONTRAST, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002686 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002687 &data_value, 4);
2688 if (status != SDVO_CMD_STATUS_SUCCESS) {
2689 DRM_DEBUG_KMS("Incorrect SDVO Max contrast\n");
2690 return;
2691 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002692 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002693 SDVO_CMD_GET_CONTRAST, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002694 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002695 &response, 2);
2696 if (status != SDVO_CMD_STATUS_SUCCESS) {
2697 DRM_DEBUG_KMS("Incorrect SDVO get contrast\n");
2698 return;
2699 }
2700 sdvo_priv->max_contrast = data_value[0];
2701 sdvo_priv->cur_contrast = response;
2702 sdvo_priv->contrast_property =
2703 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2704 "contrast", 2);
2705 sdvo_priv->contrast_property->values[0] = 0;
2706 sdvo_priv->contrast_property->values[1] = data_value[0];
2707 drm_connector_attach_property(connector,
2708 sdvo_priv->contrast_property,
2709 sdvo_priv->cur_contrast);
2710 DRM_DEBUG_KMS("contrast: max %d, "
2711 "default %d, current %d\n",
2712 data_value[0], data_value[1], response);
2713 }
2714 if (sdvo_data.hue) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002715 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002716 SDVO_CMD_GET_MAX_HUE, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002717 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002718 &data_value, 4);
2719 if (status != SDVO_CMD_STATUS_SUCCESS) {
2720 DRM_DEBUG_KMS("Incorrect SDVO Max hue\n");
2721 return;
2722 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002723 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002724 SDVO_CMD_GET_HUE, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002725 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002726 &response, 2);
2727 if (status != SDVO_CMD_STATUS_SUCCESS) {
2728 DRM_DEBUG_KMS("Incorrect SDVO get hue\n");
2729 return;
2730 }
2731 sdvo_priv->max_hue = data_value[0];
2732 sdvo_priv->cur_hue = response;
2733 sdvo_priv->hue_property =
2734 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2735 "hue", 2);
2736 sdvo_priv->hue_property->values[0] = 0;
2737 sdvo_priv->hue_property->values[1] =
2738 data_value[0];
2739 drm_connector_attach_property(connector,
2740 sdvo_priv->hue_property,
2741 sdvo_priv->cur_hue);
2742 DRM_DEBUG_KMS("hue: max %d, default %d, current %d\n",
2743 data_value[0], data_value[1], response);
2744 }
2745 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002746 if (IS_TV(sdvo_priv) || IS_LVDS(sdvo_priv)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002747 if (sdvo_data.brightness) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002748 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002749 SDVO_CMD_GET_MAX_BRIGHTNESS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002750 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002751 &data_value, 4);
2752 if (status != SDVO_CMD_STATUS_SUCCESS) {
2753 DRM_DEBUG_KMS("Incorrect SDVO Max bright\n");
2754 return;
2755 }
Eric Anholt21d40d32010-03-25 11:11:14 -07002756 intel_sdvo_write_cmd(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002757 SDVO_CMD_GET_BRIGHTNESS, NULL, 0);
Eric Anholt21d40d32010-03-25 11:11:14 -07002758 status = intel_sdvo_read_response(intel_encoder,
Zhao Yakuib9219c52009-09-10 15:45:46 +08002759 &response, 2);
2760 if (status != SDVO_CMD_STATUS_SUCCESS) {
2761 DRM_DEBUG_KMS("Incorrect SDVO get brigh\n");
2762 return;
2763 }
2764 sdvo_priv->max_brightness = data_value[0];
2765 sdvo_priv->cur_brightness = response;
2766 sdvo_priv->brightness_property =
2767 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2768 "brightness", 2);
2769 sdvo_priv->brightness_property->values[0] = 0;
2770 sdvo_priv->brightness_property->values[1] =
2771 data_value[0];
2772 drm_connector_attach_property(connector,
2773 sdvo_priv->brightness_property,
2774 sdvo_priv->cur_brightness);
2775 DRM_DEBUG_KMS("brightness: max %d, "
2776 "default %d, current %d\n",
2777 data_value[0], data_value[1], response);
2778 }
2779 }
2780 return;
2781}
2782
Eric Anholtc751ce42010-03-25 11:48:48 -07002783bool intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
Jesse Barnes79e53942008-11-07 14:24:08 -08002784{
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002785 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07002786 struct intel_encoder *intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08002787 struct intel_sdvo_priv *sdvo_priv;
Jesse Barnes79e53942008-11-07 14:24:08 -08002788 u8 ch[0x40];
2789 int i;
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002790 u32 i2c_reg, ddc_reg, analog_ddc_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -08002791
Eric Anholt21d40d32010-03-25 11:11:14 -07002792 intel_encoder = kcalloc(sizeof(struct intel_encoder)+sizeof(struct intel_sdvo_priv), 1, GFP_KERNEL);
2793 if (!intel_encoder) {
Eric Anholt7d573822009-01-02 13:33:00 -08002794 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002795 }
2796
Eric Anholt21d40d32010-03-25 11:11:14 -07002797 sdvo_priv = (struct intel_sdvo_priv *)(intel_encoder + 1);
Eric Anholtc751ce42010-03-25 11:48:48 -07002798 sdvo_priv->sdvo_reg = sdvo_reg;
Keith Packard308cd3a2009-06-14 11:56:18 -07002799
Eric Anholt21d40d32010-03-25 11:11:14 -07002800 intel_encoder->dev_priv = sdvo_priv;
2801 intel_encoder->type = INTEL_OUTPUT_SDVO;
Jesse Barnes79e53942008-11-07 14:24:08 -08002802
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002803 if (HAS_PCH_SPLIT(dev)) {
2804 i2c_reg = PCH_GPIOE;
2805 ddc_reg = PCH_GPIOE;
2806 analog_ddc_reg = PCH_GPIOA;
2807 } else {
2808 i2c_reg = GPIOE;
2809 ddc_reg = GPIOE;
2810 analog_ddc_reg = GPIOA;
2811 }
2812
Jesse Barnes79e53942008-11-07 14:24:08 -08002813 /* setup the DDC bus. */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002814 if (IS_SDVOB(sdvo_reg))
2815 intel_encoder->i2c_bus = intel_i2c_create(dev, i2c_reg, "SDVOCTRL_E for SDVOB");
Keith Packard308cd3a2009-06-14 11:56:18 -07002816 else
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002817 intel_encoder->i2c_bus = intel_i2c_create(dev, i2c_reg, "SDVOCTRL_E for SDVOC");
Keith Packard308cd3a2009-06-14 11:56:18 -07002818
Eric Anholt21d40d32010-03-25 11:11:14 -07002819 if (!intel_encoder->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002820 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002821
Eric Anholtc751ce42010-03-25 11:48:48 -07002822 sdvo_priv->slave_addr = intel_sdvo_get_slave_addr(dev, sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08002823
Keith Packard308cd3a2009-06-14 11:56:18 -07002824 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
Eric Anholt21d40d32010-03-25 11:11:14 -07002825 intel_sdvo_i2c_bit_algo.functionality = intel_encoder->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002826
Jesse Barnes79e53942008-11-07 14:24:08 -08002827 /* Read the regs to test if we can talk to the device */
2828 for (i = 0; i < 0x40; i++) {
Eric Anholt21d40d32010-03-25 11:11:14 -07002829 if (!intel_sdvo_read_byte(intel_encoder, i, &ch[i])) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002830 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002831 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002832 goto err_i2c;
2833 }
2834 }
2835
Ma Ling619ac3b2009-05-18 16:12:46 +08002836 /* setup the DDC bus. */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002837 if (IS_SDVOB(sdvo_reg)) {
2838 intel_encoder->ddc_bus = intel_i2c_create(dev, ddc_reg, "SDVOB DDC BUS");
2839 sdvo_priv->analog_ddc_bus = intel_i2c_create(dev, analog_ddc_reg,
Keith Packard57cdaf92009-09-04 13:07:54 +08002840 "SDVOB/VGA DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002841 dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002842 } else {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002843 intel_encoder->ddc_bus = intel_i2c_create(dev, ddc_reg, "SDVOC DDC BUS");
2844 sdvo_priv->analog_ddc_bus = intel_i2c_create(dev, analog_ddc_reg,
Keith Packard57cdaf92009-09-04 13:07:54 +08002845 "SDVOC/VGA DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002846 dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002847 }
Ma Ling619ac3b2009-05-18 16:12:46 +08002848
Eric Anholt21d40d32010-03-25 11:11:14 -07002849 if (intel_encoder->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002850 goto err_i2c;
2851
Keith Packard308cd3a2009-06-14 11:56:18 -07002852 /* Wrap with our custom algo which switches to DDC mode */
Eric Anholt21d40d32010-03-25 11:11:14 -07002853 intel_encoder->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002854
Zhenyu Wang14571b42010-03-30 14:06:33 +08002855 /* encoder type will be decided later */
2856 drm_encoder_init(dev, &intel_encoder->enc, &intel_sdvo_enc_funcs, 0);
2857 drm_encoder_helper_add(&intel_encoder->enc, &intel_sdvo_helper_funcs);
2858
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002859 /* In default case sdvo lvds is false */
Eric Anholt21d40d32010-03-25 11:11:14 -07002860 intel_sdvo_get_capabilities(intel_encoder, &sdvo_priv->caps);
Jesse Barnes79e53942008-11-07 14:24:08 -08002861
Eric Anholt21d40d32010-03-25 11:11:14 -07002862 if (intel_sdvo_output_setup(intel_encoder,
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002863 sdvo_priv->caps.output_flags) != true) {
Dave Airlie51c8b402009-08-20 13:38:04 +10002864 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002865 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002866 goto err_i2c;
2867 }
2868
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002869 intel_sdvo_select_ddc_bus(sdvo_priv);
2870
Jesse Barnes79e53942008-11-07 14:24:08 -08002871 /* Set the input timing to the screen. Assume always input 0. */
Eric Anholt21d40d32010-03-25 11:11:14 -07002872 intel_sdvo_set_target_input(intel_encoder, true, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08002873
Eric Anholt21d40d32010-03-25 11:11:14 -07002874 intel_sdvo_get_input_pixel_clock_range(intel_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08002875 &sdvo_priv->pixel_clock_min,
2876 &sdvo_priv->pixel_clock_max);
2877
2878
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002879 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
yakui_zhao342dc382009-06-02 14:12:00 +08002880 "clock range %dMHz - %dMHz, "
2881 "input 1: %c, input 2: %c, "
2882 "output 1: %c, output 2: %c\n",
2883 SDVO_NAME(sdvo_priv),
2884 sdvo_priv->caps.vendor_id, sdvo_priv->caps.device_id,
2885 sdvo_priv->caps.device_rev_id,
2886 sdvo_priv->pixel_clock_min / 1000,
2887 sdvo_priv->pixel_clock_max / 1000,
2888 (sdvo_priv->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2889 (sdvo_priv->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2890 /* check currently supported outputs */
2891 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002892 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002893 sdvo_priv->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002894 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2895
Eric Anholt7d573822009-01-02 13:33:00 -08002896 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002897
2898err_i2c:
Keith Packard57cdaf92009-09-04 13:07:54 +08002899 if (sdvo_priv->analog_ddc_bus != NULL)
2900 intel_i2c_destroy(sdvo_priv->analog_ddc_bus);
Eric Anholt21d40d32010-03-25 11:11:14 -07002901 if (intel_encoder->ddc_bus != NULL)
2902 intel_i2c_destroy(intel_encoder->ddc_bus);
2903 if (intel_encoder->i2c_bus != NULL)
2904 intel_i2c_destroy(intel_encoder->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002905err_inteloutput:
Eric Anholt21d40d32010-03-25 11:11:14 -07002906 kfree(intel_encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08002907
Eric Anholt7d573822009-01-02 13:33:00 -08002908 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002909}