blob: f7030e4810833f3577a1f952c702764cda24b9cb [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"
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +080034#include "drm_edid.h"
Chris Wilsonea5b2132010-08-04 13:50:23 +010035#include "intel_drv.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)
Chris Wilson32aad862010-08-04 13:50:25 +010050#define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
Zhenyu Wang14571b42010-03-30 14:06:33 +080051
Jesse Barnes79e53942008-11-07 14:24:08 -080052
Chris Wilson2e88e402010-08-07 11:01:27 +010053static const char *tv_format_names[] = {
Zhao Yakuice6feab2009-08-24 13:50:26 +080054 "NTSC_M" , "NTSC_J" , "NTSC_443",
55 "PAL_B" , "PAL_D" , "PAL_G" ,
56 "PAL_H" , "PAL_I" , "PAL_M" ,
57 "PAL_N" , "PAL_NC" , "PAL_60" ,
58 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
59 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
60 "SECAM_60"
61};
62
63#define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
64
Chris Wilsonea5b2132010-08-04 13:50:23 +010065struct intel_sdvo {
66 struct intel_encoder base;
67
Keith Packardf9c10a92009-05-30 12:16:25 -070068 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080069
70 /* Register for the SDVO device: SDVOB or SDVOC */
Eric Anholtc751ce42010-03-25 11:48:48 -070071 int sdvo_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080072
Jesse Barnese2f0ba92009-02-02 15:11:52 -080073 /* Active outputs controlled by this SDVO output */
74 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080075
Jesse Barnese2f0ba92009-02-02 15:11:52 -080076 /*
77 * Capabilities of the SDVO device returned by
78 * i830_sdvo_get_capabilities()
79 */
Jesse Barnes79e53942008-11-07 14:24:08 -080080 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080081
82 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080083 int pixel_clock_min, pixel_clock_max;
84
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080085 /*
86 * For multiple function SDVO device,
87 * this is for current attached outputs.
88 */
89 uint16_t attached_output;
90
Jesse Barnese2f0ba92009-02-02 15:11:52 -080091 /**
92 * This is set if we're going to treat the device as TV-out.
93 *
94 * While we have these nice friendly flags for output types that ought
95 * to decide this for us, the S-Video output on our HDMI+S-Video card
96 * shows up as RGB1 (VGA).
97 */
98 bool is_tv;
99
Zhao Yakuice6feab2009-08-24 13:50:26 +0800100 /* This is for current tv format name */
Chris Wilson40039752010-08-04 13:50:26 +0100101 int tv_format_index;
Zhao Yakuice6feab2009-08-24 13:50:26 +0800102
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800103 /**
104 * This is set if we treat the device as HDMI, instead of DVI.
105 */
106 bool is_hdmi;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800107
Ma Ling7086c872009-05-13 11:20:06 +0800108 /**
Chris Wilson6c9547f2010-08-25 10:05:17 +0100109 * This is set if we detect output of sdvo device as LVDS and
110 * have a valid fixed mode to use with the panel.
Ma Ling7086c872009-05-13 11:20:06 +0800111 */
112 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800113
114 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800115 * This is sdvo fixed pannel mode pointer
116 */
117 struct drm_display_mode *sdvo_lvds_fixed_mode;
118
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800119 /*
120 * supported encoding mode, used to determine whether HDMI is
121 * supported
122 */
123 struct intel_sdvo_encode encode;
124
Eric Anholtc751ce42010-03-25 11:48:48 -0700125 /* DDC bus used by this SDVO encoder */
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800126 uint8_t ddc_bus;
127
Chris Wilson6c9547f2010-08-25 10:05:17 +0100128 /* Input timings for adjusted_mode */
129 struct intel_sdvo_dtd input_dtd;
Zhenyu Wang14571b42010-03-30 14:06:33 +0800130};
131
132struct intel_sdvo_connector {
Chris Wilson615fb932010-08-04 13:50:24 +0100133 struct intel_connector base;
134
Zhenyu Wang14571b42010-03-30 14:06:33 +0800135 /* Mark the type of connector */
136 uint16_t output_flag;
137
138 /* This contains all current supported TV format */
Chris Wilson40039752010-08-04 13:50:26 +0100139 u8 tv_format_supported[TV_FORMAT_NUM];
Zhenyu Wang14571b42010-03-30 14:06:33 +0800140 int format_supported_num;
Chris Wilsonc5521702010-08-04 13:50:28 +0100141 struct drm_property *tv_format;
Zhenyu Wang14571b42010-03-30 14:06:33 +0800142
Zhao Yakuib9219c52009-09-10 15:45:46 +0800143 /* add the property for the SDVO-TV */
Chris Wilsonc5521702010-08-04 13:50:28 +0100144 struct drm_property *left;
145 struct drm_property *right;
146 struct drm_property *top;
147 struct drm_property *bottom;
148 struct drm_property *hpos;
149 struct drm_property *vpos;
150 struct drm_property *contrast;
151 struct drm_property *saturation;
152 struct drm_property *hue;
153 struct drm_property *sharpness;
154 struct drm_property *flicker_filter;
155 struct drm_property *flicker_filter_adaptive;
156 struct drm_property *flicker_filter_2d;
157 struct drm_property *tv_chroma_filter;
158 struct drm_property *tv_luma_filter;
Chris Wilsone0442182010-08-04 13:50:29 +0100159 struct drm_property *dot_crawl;
Zhao Yakuib9219c52009-09-10 15:45:46 +0800160
161 /* add the property for the SDVO-TV/LVDS */
Chris Wilsonc5521702010-08-04 13:50:28 +0100162 struct drm_property *brightness;
Zhao Yakuib9219c52009-09-10 15:45:46 +0800163
164 /* Add variable to record current setting for the above property */
165 u32 left_margin, right_margin, top_margin, bottom_margin;
Chris Wilsonc5521702010-08-04 13:50:28 +0100166
Zhao Yakuib9219c52009-09-10 15:45:46 +0800167 /* this is to get the range of margin.*/
168 u32 max_hscan, max_vscan;
169 u32 max_hpos, cur_hpos;
170 u32 max_vpos, cur_vpos;
171 u32 cur_brightness, max_brightness;
172 u32 cur_contrast, max_contrast;
173 u32 cur_saturation, max_saturation;
174 u32 cur_hue, max_hue;
Chris Wilsonc5521702010-08-04 13:50:28 +0100175 u32 cur_sharpness, max_sharpness;
176 u32 cur_flicker_filter, max_flicker_filter;
177 u32 cur_flicker_filter_adaptive, max_flicker_filter_adaptive;
178 u32 cur_flicker_filter_2d, max_flicker_filter_2d;
179 u32 cur_tv_chroma_filter, max_tv_chroma_filter;
180 u32 cur_tv_luma_filter, max_tv_luma_filter;
Chris Wilsone0442182010-08-04 13:50:29 +0100181 u32 cur_dot_crawl, max_dot_crawl;
Jesse Barnes79e53942008-11-07 14:24:08 -0800182};
183
Chris Wilson890f3352010-09-14 16:46:59 +0100184static struct intel_sdvo *to_intel_sdvo(struct drm_encoder *encoder)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100185{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100186 return container_of(encoder, struct intel_sdvo, base.base);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100187}
188
Chris Wilsondf0e9242010-09-09 16:20:55 +0100189static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
190{
191 return container_of(intel_attached_encoder(connector),
192 struct intel_sdvo, base);
193}
194
Chris Wilson615fb932010-08-04 13:50:24 +0100195static struct intel_sdvo_connector *to_intel_sdvo_connector(struct drm_connector *connector)
196{
197 return container_of(to_intel_connector(connector), struct intel_sdvo_connector, base);
198}
199
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800200static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +0100201intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
Chris Wilson32aad862010-08-04 13:50:25 +0100202static bool
203intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
204 struct intel_sdvo_connector *intel_sdvo_connector,
205 int type);
206static bool
207intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
208 struct intel_sdvo_connector *intel_sdvo_connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800209
Jesse Barnes79e53942008-11-07 14:24:08 -0800210/**
211 * Writes the SDVOB or SDVOC with the given value, but always writes both
212 * SDVOB and SDVOC to work around apparent hardware issues (according to
213 * comments in the BIOS).
214 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100215static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800216{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100217 struct drm_device *dev = intel_sdvo->base.base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800218 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -0800219 u32 bval = val, cval = val;
220 int i;
221
Chris Wilsonea5b2132010-08-04 13:50:23 +0100222 if (intel_sdvo->sdvo_reg == PCH_SDVOB) {
223 I915_WRITE(intel_sdvo->sdvo_reg, val);
224 I915_READ(intel_sdvo->sdvo_reg);
Zhao Yakui461ed3c2010-03-30 15:11:33 +0800225 return;
226 }
227
Chris Wilsonea5b2132010-08-04 13:50:23 +0100228 if (intel_sdvo->sdvo_reg == SDVOB) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800229 cval = I915_READ(SDVOC);
230 } else {
231 bval = I915_READ(SDVOB);
232 }
233 /*
234 * Write the registers twice for luck. Sometimes,
235 * writing them only once doesn't appear to 'stick'.
236 * The BIOS does this too. Yay, magic
237 */
238 for (i = 0; i < 2; i++)
239 {
240 I915_WRITE(SDVOB, bval);
241 I915_READ(SDVOB);
242 I915_WRITE(SDVOC, cval);
243 I915_READ(SDVOC);
244 }
245}
246
Chris Wilson32aad862010-08-04 13:50:25 +0100247static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
Jesse Barnes79e53942008-11-07 14:24:08 -0800248{
Chris Wilson32aad862010-08-04 13:50:25 +0100249 u8 out_buf[2] = { addr, 0 };
Jesse Barnes79e53942008-11-07 14:24:08 -0800250 u8 buf[2];
Jesse Barnes79e53942008-11-07 14:24:08 -0800251 struct i2c_msg msgs[] = {
252 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100253 .addr = intel_sdvo->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800254 .flags = 0,
255 .len = 1,
256 .buf = out_buf,
257 },
258 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100259 .addr = intel_sdvo->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800260 .flags = I2C_M_RD,
261 .len = 1,
262 .buf = buf,
263 }
264 };
Chris Wilson32aad862010-08-04 13:50:25 +0100265 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800266
Chris Wilsonea5b2132010-08-04 13:50:23 +0100267 if ((ret = i2c_transfer(intel_sdvo->base.i2c_bus, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800268 {
269 *ch = buf[0];
270 return true;
271 }
272
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800273 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800274 return false;
275}
276
Chris Wilson32aad862010-08-04 13:50:25 +0100277static bool intel_sdvo_write_byte(struct intel_sdvo *intel_sdvo, int addr, u8 ch)
Jesse Barnes79e53942008-11-07 14:24:08 -0800278{
Chris Wilson32aad862010-08-04 13:50:25 +0100279 u8 out_buf[2] = { addr, ch };
Jesse Barnes79e53942008-11-07 14:24:08 -0800280 struct i2c_msg msgs[] = {
281 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100282 .addr = intel_sdvo->slave_addr >> 1,
Jesse Barnes79e53942008-11-07 14:24:08 -0800283 .flags = 0,
284 .len = 2,
285 .buf = out_buf,
286 }
287 };
288
Chris Wilson32aad862010-08-04 13:50:25 +0100289 return i2c_transfer(intel_sdvo->base.i2c_bus, msgs, 1) == 1;
Jesse Barnes79e53942008-11-07 14:24:08 -0800290}
291
292#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
293/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100294static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800295 u8 cmd;
Chris Wilson2e88e402010-08-07 11:01:27 +0100296 const char *name;
Jesse Barnes79e53942008-11-07 14:24:08 -0800297} sdvo_cmd_names[] = {
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
Jesse Barnes79e53942008-11-07 14:24:08 -0800337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
Chris Wilsonc5521702010-08-04 13:50:28 +0100341
Zhao Yakuib9219c52009-09-10 15:45:46 +0800342 /* Add the op code for SDVO enhancements */
Chris Wilsonc5521702010-08-04 13:50:28 +0100343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
Zhao Yakuib9219c52009-09-10 15:45:46 +0800349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
356 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
Chris Wilsonc5521702010-08-04 13:50:28 +0100367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
370 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
371 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
373 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
374 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
377 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
378 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
379 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
380 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
381 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
382 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
383 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
384 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
385 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
386 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
387
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800388 /* HDMI op code */
389 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
390 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
391 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
392 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
393 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
394 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
395 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
396 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
397 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
398 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
399 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
400 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
401 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
402 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
403 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
404 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
405 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
406 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
407 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
408 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800409};
410
Zhao Yakui461ed3c2010-03-30 15:11:33 +0800411#define IS_SDVOB(reg) (reg == SDVOB || reg == PCH_SDVOB)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100412#define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
Jesse Barnes79e53942008-11-07 14:24:08 -0800413
Chris Wilsonea5b2132010-08-04 13:50:23 +0100414static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
Chris Wilson32aad862010-08-04 13:50:25 +0100415 const void *args, int args_len)
Jesse Barnes79e53942008-11-07 14:24:08 -0800416{
Jesse Barnes79e53942008-11-07 14:24:08 -0800417 int i;
418
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800419 DRM_DEBUG_KMS("%s: W: %02X ",
Chris Wilsonea5b2132010-08-04 13:50:23 +0100420 SDVO_NAME(intel_sdvo), cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800421 for (i = 0; i < args_len; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800422 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800423 for (; i < 8; i++)
yakui_zhao342dc382009-06-02 14:12:00 +0800424 DRM_LOG_KMS(" ");
Kulikov Vasiliy04ad3272010-06-28 15:54:56 +0400425 for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 if (cmd == sdvo_cmd_names[i].cmd) {
yakui_zhao342dc382009-06-02 14:12:00 +0800427 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800428 break;
429 }
430 }
Kulikov Vasiliy04ad3272010-06-28 15:54:56 +0400431 if (i == ARRAY_SIZE(sdvo_cmd_names))
yakui_zhao342dc382009-06-02 14:12:00 +0800432 DRM_LOG_KMS("(%02X)", cmd);
433 DRM_LOG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800434}
Jesse Barnes79e53942008-11-07 14:24:08 -0800435
Chris Wilson32aad862010-08-04 13:50:25 +0100436static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
437 const void *args, int args_len)
Jesse Barnes79e53942008-11-07 14:24:08 -0800438{
439 int i;
440
Chris Wilsonea5b2132010-08-04 13:50:23 +0100441 intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
Jesse Barnes79e53942008-11-07 14:24:08 -0800442
443 for (i = 0; i < args_len; i++) {
Chris Wilson32aad862010-08-04 13:50:25 +0100444 if (!intel_sdvo_write_byte(intel_sdvo, SDVO_I2C_ARG_0 - i,
445 ((u8*)args)[i]))
446 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800447 }
448
Chris Wilson32aad862010-08-04 13:50:25 +0100449 return intel_sdvo_write_byte(intel_sdvo, SDVO_I2C_OPCODE, cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -0800450}
451
Jesse Barnes79e53942008-11-07 14:24:08 -0800452static const char *cmd_status_names[] = {
453 "Power on",
454 "Success",
455 "Not supported",
456 "Invalid arg",
457 "Pending",
458 "Target not specified",
459 "Scaling not supported"
460};
461
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100462static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
463 void *response, int response_len)
Jesse Barnes79e53942008-11-07 14:24:08 -0800464{
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100465 u8 retry = 5;
466 u8 status;
Zhenyu Wang33b52962009-03-24 14:02:40 +0800467 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -0800468
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100469 /*
470 * The documentation states that all commands will be
471 * processed within 15µs, and that we need only poll
472 * the status byte a maximum of 3 times in order for the
473 * command to be complete.
474 *
475 * Check 5 times in case the hardware failed to read the docs.
476 */
477 do {
478 if (!intel_sdvo_read_byte(intel_sdvo,
479 SDVO_I2C_CMD_STATUS,
480 &status))
481 return false;
482 } while (status == SDVO_CMD_STATUS_PENDING && --retry);
483
Chris Wilsonea5b2132010-08-04 13:50:23 +0100484 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(intel_sdvo));
Jesse Barnes79e53942008-11-07 14:24:08 -0800485 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
yakui_zhao342dc382009-06-02 14:12:00 +0800486 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800487 else
yakui_zhao342dc382009-06-02 14:12:00 +0800488 DRM_LOG_KMS("(??? %d)", status);
Jesse Barnes79e53942008-11-07 14:24:08 -0800489
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100490 if (status != SDVO_CMD_STATUS_SUCCESS)
491 goto log_fail;
Jesse Barnes79e53942008-11-07 14:24:08 -0800492
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100493 /* Read the command response */
494 for (i = 0; i < response_len; i++) {
495 if (!intel_sdvo_read_byte(intel_sdvo,
496 SDVO_I2C_RETURN_0 + i,
497 &((u8 *)response)[i]))
498 goto log_fail;
499 DRM_LOG_KMS("%02X ", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800500 }
501
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100502 for (; i < 8; i++)
503 DRM_LOG_KMS(" ");
504 DRM_LOG_KMS("\n");
505
506 return true;
507
508log_fail:
509 DRM_LOG_KMS("\n");
510 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800511}
512
Hannes Ederb358d0a2008-12-18 21:18:47 +0100513static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800514{
515 if (mode->clock >= 100000)
516 return 1;
517 else if (mode->clock >= 50000)
518 return 2;
519 else
520 return 4;
521}
522
523/**
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800524 * Try to read the response after issuie the DDC switch command. But it
525 * is noted that we must do the action of reading response and issuing DDC
526 * switch command in one I2C transaction. Otherwise when we try to start
527 * another I2C transaction after issuing the DDC bus switch, it will be
528 * switched to the internal SDVO register.
Jesse Barnes79e53942008-11-07 14:24:08 -0800529 */
Chris Wilson819f3fb2010-09-14 19:11:56 +0100530static int intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
531 u8 target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800532{
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800533 u8 out_buf[2], cmd_buf[2], ret_value[2], ret;
534 struct i2c_msg msgs[] = {
535 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100536 .addr = intel_sdvo->slave_addr >> 1,
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800537 .flags = 0,
538 .len = 2,
539 .buf = out_buf,
540 },
541 /* the following two are to read the response */
542 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100543 .addr = intel_sdvo->slave_addr >> 1,
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800544 .flags = 0,
545 .len = 1,
546 .buf = cmd_buf,
547 },
548 {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100549 .addr = intel_sdvo->slave_addr >> 1,
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800550 .flags = I2C_M_RD,
551 .len = 1,
552 .buf = ret_value,
553 },
554 };
555
Chris Wilsonea5b2132010-08-04 13:50:23 +0100556 intel_sdvo_debug_write(intel_sdvo, SDVO_CMD_SET_CONTROL_BUS_SWITCH,
Chris Wilson819f3fb2010-09-14 19:11:56 +0100557 &target, 1);
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800558 /* write the DDC switch command argument */
Chris Wilson819f3fb2010-09-14 19:11:56 +0100559 if (!intel_sdvo_write_byte(intel_sdvo, SDVO_I2C_ARG_0, target))
560 return -EIO;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800561
562 out_buf[0] = SDVO_I2C_OPCODE;
563 out_buf[1] = SDVO_CMD_SET_CONTROL_BUS_SWITCH;
564 cmd_buf[0] = SDVO_I2C_CMD_STATUS;
565 cmd_buf[1] = 0;
566 ret_value[0] = 0;
567 ret_value[1] = 0;
568
Chris Wilsonea5b2132010-08-04 13:50:23 +0100569 ret = i2c_transfer(intel_sdvo->base.i2c_bus, msgs, 3);
Chris Wilson819f3fb2010-09-14 19:11:56 +0100570 if (ret < 0)
571 return ret;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800572 if (ret != 3) {
573 /* failure in I2C transfer */
574 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
Chris Wilson819f3fb2010-09-14 19:11:56 +0100575 return -EIO;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800576 }
577 if (ret_value[0] != SDVO_CMD_STATUS_SUCCESS) {
578 DRM_DEBUG_KMS("DDC switch command returns response %d\n",
Chris Wilson819f3fb2010-09-14 19:11:56 +0100579 ret_value[0]);
580 return -EIO;
Zhao Yakui6a304ca2010-01-08 10:58:19 +0800581 }
Chris Wilson819f3fb2010-09-14 19:11:56 +0100582
583 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800584}
585
Chris Wilson32aad862010-08-04 13:50:25 +0100586static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
587{
588 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
589 return false;
590
591 return intel_sdvo_read_response(intel_sdvo, NULL, 0);
592}
593
594static bool
595intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
596{
597 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
598 return false;
599
600 return intel_sdvo_read_response(intel_sdvo, value, len);
601}
602
603static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
Jesse Barnes79e53942008-11-07 14:24:08 -0800604{
605 struct intel_sdvo_set_target_input_args targets = {0};
Chris Wilson32aad862010-08-04 13:50:25 +0100606 return intel_sdvo_set_value(intel_sdvo,
607 SDVO_CMD_SET_TARGET_INPUT,
608 &targets, sizeof(targets));
Jesse Barnes79e53942008-11-07 14:24:08 -0800609}
610
611/**
612 * Return whether each input is trained.
613 *
614 * This function is making an assumption about the layout of the response,
615 * which should be checked against the docs.
616 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100617static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800618{
619 struct intel_sdvo_get_trained_inputs_response response;
Jesse Barnes79e53942008-11-07 14:24:08 -0800620
Chris Wilson32aad862010-08-04 13:50:25 +0100621 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
622 &response, sizeof(response)))
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 return false;
624
625 *input_1 = response.input0_trained;
626 *input_2 = response.input1_trained;
627 return true;
628}
629
Chris Wilsonea5b2132010-08-04 13:50:23 +0100630static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 u16 outputs)
632{
Chris Wilson32aad862010-08-04 13:50:25 +0100633 return intel_sdvo_set_value(intel_sdvo,
634 SDVO_CMD_SET_ACTIVE_OUTPUTS,
635 &outputs, sizeof(outputs));
Jesse Barnes79e53942008-11-07 14:24:08 -0800636}
637
Chris Wilsonea5b2132010-08-04 13:50:23 +0100638static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800639 int mode)
640{
Chris Wilson32aad862010-08-04 13:50:25 +0100641 u8 state = SDVO_ENCODER_STATE_ON;
Jesse Barnes79e53942008-11-07 14:24:08 -0800642
643 switch (mode) {
644 case DRM_MODE_DPMS_ON:
645 state = SDVO_ENCODER_STATE_ON;
646 break;
647 case DRM_MODE_DPMS_STANDBY:
648 state = SDVO_ENCODER_STATE_STANDBY;
649 break;
650 case DRM_MODE_DPMS_SUSPEND:
651 state = SDVO_ENCODER_STATE_SUSPEND;
652 break;
653 case DRM_MODE_DPMS_OFF:
654 state = SDVO_ENCODER_STATE_OFF;
655 break;
656 }
657
Chris Wilson32aad862010-08-04 13:50:25 +0100658 return intel_sdvo_set_value(intel_sdvo,
659 SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
Jesse Barnes79e53942008-11-07 14:24:08 -0800660}
661
Chris Wilsonea5b2132010-08-04 13:50:23 +0100662static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 int *clock_min,
664 int *clock_max)
665{
666 struct intel_sdvo_pixel_clock_range clocks;
Jesse Barnes79e53942008-11-07 14:24:08 -0800667
Chris Wilson32aad862010-08-04 13:50:25 +0100668 if (!intel_sdvo_get_value(intel_sdvo,
669 SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
670 &clocks, sizeof(clocks)))
Jesse Barnes79e53942008-11-07 14:24:08 -0800671 return false;
672
673 /* Convert the values from units of 10 kHz to kHz. */
674 *clock_min = clocks.min * 10;
675 *clock_max = clocks.max * 10;
Jesse Barnes79e53942008-11-07 14:24:08 -0800676 return true;
677}
678
Chris Wilsonea5b2132010-08-04 13:50:23 +0100679static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 u16 outputs)
681{
Chris Wilson32aad862010-08-04 13:50:25 +0100682 return intel_sdvo_set_value(intel_sdvo,
683 SDVO_CMD_SET_TARGET_OUTPUT,
684 &outputs, sizeof(outputs));
Jesse Barnes79e53942008-11-07 14:24:08 -0800685}
686
Chris Wilsonea5b2132010-08-04 13:50:23 +0100687static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800688 struct intel_sdvo_dtd *dtd)
689{
Chris Wilson32aad862010-08-04 13:50:25 +0100690 return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
691 intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
Jesse Barnes79e53942008-11-07 14:24:08 -0800692}
693
Chris Wilsonea5b2132010-08-04 13:50:23 +0100694static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800695 struct intel_sdvo_dtd *dtd)
696{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100697 return intel_sdvo_set_timing(intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800698 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
699}
700
Chris Wilsonea5b2132010-08-04 13:50:23 +0100701static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800702 struct intel_sdvo_dtd *dtd)
703{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100704 return intel_sdvo_set_timing(intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800705 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
706}
707
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800708static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +0100709intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800710 uint16_t clock,
711 uint16_t width,
712 uint16_t height)
713{
714 struct intel_sdvo_preferred_input_timing_args args;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800715
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800716 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800717 args.clock = clock;
718 args.width = width;
719 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800720 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800721
Chris Wilsonea5b2132010-08-04 13:50:23 +0100722 if (intel_sdvo->is_lvds &&
723 (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
724 intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800725 args.scaled = 1;
726
Chris Wilson32aad862010-08-04 13:50:25 +0100727 return intel_sdvo_set_value(intel_sdvo,
728 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
729 &args, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800730}
731
Chris Wilsonea5b2132010-08-04 13:50:23 +0100732static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800733 struct intel_sdvo_dtd *dtd)
734{
Chris Wilson32aad862010-08-04 13:50:25 +0100735 return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
736 &dtd->part1, sizeof(dtd->part1)) &&
737 intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
738 &dtd->part2, sizeof(dtd->part2));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800739}
Jesse Barnes79e53942008-11-07 14:24:08 -0800740
Chris Wilsonea5b2132010-08-04 13:50:23 +0100741static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800742{
Chris Wilson32aad862010-08-04 13:50:25 +0100743 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
Jesse Barnes79e53942008-11-07 14:24:08 -0800744}
745
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800746static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
Chris Wilson32aad862010-08-04 13:50:25 +0100747 const struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800748{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800749 uint16_t width, height;
750 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
751 uint16_t h_sync_offset, v_sync_offset;
Jesse Barnes79e53942008-11-07 14:24:08 -0800752
753 width = mode->crtc_hdisplay;
754 height = mode->crtc_vdisplay;
755
756 /* do some mode translations */
757 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
758 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
759
760 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
761 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
762
763 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
764 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
765
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800766 dtd->part1.clock = mode->clock / 10;
767 dtd->part1.h_active = width & 0xff;
768 dtd->part1.h_blank = h_blank_len & 0xff;
769 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800770 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800771 dtd->part1.v_active = height & 0xff;
772 dtd->part1.v_blank = v_blank_len & 0xff;
773 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800774 ((v_blank_len >> 8) & 0xf);
775
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800776 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800777 dtd->part2.h_sync_width = h_sync_len & 0xff;
778 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800779 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800780 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800781 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
782 ((v_sync_len & 0x30) >> 4);
783
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800784 dtd->part2.dtd_flags = 0x18;
Jesse Barnes79e53942008-11-07 14:24:08 -0800785 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800786 dtd->part2.dtd_flags |= 0x2;
Jesse Barnes79e53942008-11-07 14:24:08 -0800787 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800788 dtd->part2.dtd_flags |= 0x4;
Jesse Barnes79e53942008-11-07 14:24:08 -0800789
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800790 dtd->part2.sdvo_flags = 0;
791 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
792 dtd->part2.reserved = 0;
793}
Jesse Barnes79e53942008-11-07 14:24:08 -0800794
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800795static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
Chris Wilson32aad862010-08-04 13:50:25 +0100796 const struct intel_sdvo_dtd *dtd)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800797{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800798 mode->hdisplay = dtd->part1.h_active;
799 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
800 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800801 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800802 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
803 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
804 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
805 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
806
807 mode->vdisplay = dtd->part1.v_active;
808 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
809 mode->vsync_start = mode->vdisplay;
810 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800811 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800812 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
813 mode->vsync_end = mode->vsync_start +
814 (dtd->part2.v_sync_off_width & 0xf);
815 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
816 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
817 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
818
819 mode->clock = dtd->part1.clock * 10;
820
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800821 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800822 if (dtd->part2.dtd_flags & 0x2)
823 mode->flags |= DRM_MODE_FLAG_PHSYNC;
824 if (dtd->part2.dtd_flags & 0x4)
825 mode->flags |= DRM_MODE_FLAG_PVSYNC;
826}
827
Chris Wilsonea5b2132010-08-04 13:50:23 +0100828static bool intel_sdvo_get_supp_encode(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800829 struct intel_sdvo_encode *encode)
830{
Chris Wilson32aad862010-08-04 13:50:25 +0100831 if (intel_sdvo_get_value(intel_sdvo,
832 SDVO_CMD_GET_SUPP_ENCODE,
833 encode, sizeof(*encode)))
834 return true;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800835
Chris Wilson32aad862010-08-04 13:50:25 +0100836 /* non-support means DVI */
837 memset(encode, 0, sizeof(*encode));
838 return false;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800839}
840
Chris Wilsonea5b2132010-08-04 13:50:23 +0100841static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
Eric Anholtc751ce42010-03-25 11:48:48 -0700842 uint8_t mode)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800843{
Chris Wilson32aad862010-08-04 13:50:25 +0100844 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800845}
846
Chris Wilsonea5b2132010-08-04 13:50:23 +0100847static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800848 uint8_t mode)
849{
Chris Wilson32aad862010-08-04 13:50:25 +0100850 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800851}
852
853#if 0
Chris Wilsonea5b2132010-08-04 13:50:23 +0100854static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800855{
856 int i, j;
857 uint8_t set_buf_index[2];
858 uint8_t av_split;
859 uint8_t buf_size;
860 uint8_t buf[48];
861 uint8_t *pos;
862
Chris Wilson32aad862010-08-04 13:50:25 +0100863 intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800864
865 for (i = 0; i <= av_split; i++) {
866 set_buf_index[0] = i; set_buf_index[1] = 0;
Eric Anholtc751ce42010-03-25 11:48:48 -0700867 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800868 set_buf_index, 2);
Eric Anholtc751ce42010-03-25 11:48:48 -0700869 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
870 intel_sdvo_read_response(encoder, &buf_size, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800871
872 pos = buf;
873 for (j = 0; j <= buf_size; j += 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700874 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800875 NULL, 0);
Eric Anholtc751ce42010-03-25 11:48:48 -0700876 intel_sdvo_read_response(encoder, pos, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800877 pos += 8;
878 }
879 }
880}
881#endif
882
Chris Wilson32aad862010-08-04 13:50:25 +0100883static bool intel_sdvo_set_hdmi_buf(struct intel_sdvo *intel_sdvo,
Eric Anholtc751ce42010-03-25 11:48:48 -0700884 int index,
885 uint8_t *data, int8_t size, uint8_t tx_rate)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800886{
887 uint8_t set_buf_index[2];
888
889 set_buf_index[0] = index;
890 set_buf_index[1] = 0;
891
Chris Wilson32aad862010-08-04 13:50:25 +0100892 if (!intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_HBUF_INDEX,
893 set_buf_index, 2))
894 return false;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800895
896 for (; size > 0; size -= 8) {
Chris Wilson32aad862010-08-04 13:50:25 +0100897 if (!intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_HBUF_DATA, data, 8))
898 return false;
899
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800900 data += 8;
901 }
902
Chris Wilson32aad862010-08-04 13:50:25 +0100903 return intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_HBUF_TXRATE, &tx_rate, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800904}
905
906static uint8_t intel_sdvo_calc_hbuf_csum(uint8_t *data, uint8_t size)
907{
908 uint8_t csum = 0;
909 int i;
910
911 for (i = 0; i < size; i++)
912 csum += data[i];
913
914 return 0x100 - csum;
915}
916
917#define DIP_TYPE_AVI 0x82
918#define DIP_VERSION_AVI 0x2
919#define DIP_LEN_AVI 13
920
921struct dip_infoframe {
922 uint8_t type;
923 uint8_t version;
924 uint8_t len;
925 uint8_t checksum;
926 union {
927 struct {
928 /* Packet Byte #1 */
929 uint8_t S:2;
930 uint8_t B:2;
931 uint8_t A:1;
932 uint8_t Y:2;
933 uint8_t rsvd1:1;
934 /* Packet Byte #2 */
935 uint8_t R:4;
936 uint8_t M:2;
937 uint8_t C:2;
938 /* Packet Byte #3 */
939 uint8_t SC:2;
940 uint8_t Q:2;
941 uint8_t EC:3;
942 uint8_t ITC:1;
943 /* Packet Byte #4 */
944 uint8_t VIC:7;
945 uint8_t rsvd2:1;
946 /* Packet Byte #5 */
947 uint8_t PR:4;
948 uint8_t rsvd3:4;
949 /* Packet Byte #6~13 */
950 uint16_t top_bar_end;
951 uint16_t bottom_bar_start;
952 uint16_t left_bar_end;
953 uint16_t right_bar_start;
954 } avi;
955 struct {
956 /* Packet Byte #1 */
957 uint8_t channel_count:3;
958 uint8_t rsvd1:1;
959 uint8_t coding_type:4;
960 /* Packet Byte #2 */
961 uint8_t sample_size:2; /* SS0, SS1 */
962 uint8_t sample_frequency:3;
963 uint8_t rsvd2:3;
964 /* Packet Byte #3 */
965 uint8_t coding_type_private:5;
966 uint8_t rsvd3:3;
967 /* Packet Byte #4 */
968 uint8_t channel_allocation;
969 /* Packet Byte #5 */
970 uint8_t rsvd4:3;
971 uint8_t level_shift:4;
972 uint8_t downmix_inhibit:1;
973 } audio;
974 uint8_t payload[28];
975 } __attribute__ ((packed)) u;
976} __attribute__((packed));
977
Chris Wilson32aad862010-08-04 13:50:25 +0100978static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800979 struct drm_display_mode * mode)
980{
981 struct dip_infoframe avi_if = {
982 .type = DIP_TYPE_AVI,
983 .version = DIP_VERSION_AVI,
984 .len = DIP_LEN_AVI,
985 };
986
987 avi_if.checksum = intel_sdvo_calc_hbuf_csum((uint8_t *)&avi_if,
988 4 + avi_if.len);
Chris Wilson32aad862010-08-04 13:50:25 +0100989 return intel_sdvo_set_hdmi_buf(intel_sdvo, 1, (uint8_t *)&avi_if,
990 4 + avi_if.len,
991 SDVO_HBUF_TX_VSYNC);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800992}
993
Chris Wilson32aad862010-08-04 13:50:25 +0100994static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +0800995{
Zhao Yakuice6feab2009-08-24 13:50:26 +0800996 struct intel_sdvo_tv_format format;
Chris Wilson40039752010-08-04 13:50:26 +0100997 uint32_t format_map;
Zhao Yakuice6feab2009-08-24 13:50:26 +0800998
Chris Wilson40039752010-08-04 13:50:26 +0100999 format_map = 1 << intel_sdvo->tv_format_index;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001000 memset(&format, 0, sizeof(format));
Chris Wilson32aad862010-08-04 13:50:25 +01001001 memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08001002
Chris Wilson32aad862010-08-04 13:50:25 +01001003 BUILD_BUG_ON(sizeof(format) != 6);
1004 return intel_sdvo_set_value(intel_sdvo,
1005 SDVO_CMD_SET_TV_FORMAT,
1006 &format, sizeof(format));
1007}
Zhao Yakuice6feab2009-08-24 13:50:26 +08001008
Chris Wilson32aad862010-08-04 13:50:25 +01001009static bool
1010intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
1011 struct drm_display_mode *mode)
1012{
1013 struct intel_sdvo_dtd output_dtd;
1014
1015 if (!intel_sdvo_set_target_output(intel_sdvo,
1016 intel_sdvo->attached_output))
1017 return false;
1018
1019 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1020 if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1021 return false;
1022
1023 return true;
1024}
1025
1026static bool
1027intel_sdvo_set_input_timings_for_mode(struct intel_sdvo *intel_sdvo,
1028 struct drm_display_mode *mode,
1029 struct drm_display_mode *adjusted_mode)
1030{
Chris Wilson32aad862010-08-04 13:50:25 +01001031 /* Reset the input timing to the screen. Assume always input 0. */
1032 if (!intel_sdvo_set_target_input(intel_sdvo))
1033 return false;
1034
1035 if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
1036 mode->clock / 10,
1037 mode->hdisplay,
1038 mode->vdisplay))
1039 return false;
1040
1041 if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
Chris Wilson6c9547f2010-08-25 10:05:17 +01001042 &intel_sdvo->input_dtd))
Chris Wilson32aad862010-08-04 13:50:25 +01001043 return false;
1044
Chris Wilson6c9547f2010-08-25 10:05:17 +01001045 intel_sdvo_get_mode_from_dtd(adjusted_mode, &intel_sdvo->input_dtd);
Chris Wilson32aad862010-08-04 13:50:25 +01001046
1047 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson32aad862010-08-04 13:50:25 +01001048 return true;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001049}
1050
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001051static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
1052 struct drm_display_mode *mode,
1053 struct drm_display_mode *adjusted_mode)
1054{
Chris Wilson890f3352010-09-14 16:46:59 +01001055 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
Chris Wilson6c9547f2010-08-25 10:05:17 +01001056 int multiplier;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001057
Chris Wilson32aad862010-08-04 13:50:25 +01001058 /* We need to construct preferred input timings based on our
1059 * output timings. To do that, we have to set the output
1060 * timings, even though this isn't really the right place in
1061 * the sequence to do it. Oh well.
1062 */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001063 if (intel_sdvo->is_tv) {
Chris Wilson32aad862010-08-04 13:50:25 +01001064 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001065 return false;
Chris Wilson32aad862010-08-04 13:50:25 +01001066
Pavel Roskinc74696b2010-09-02 14:46:34 -04001067 (void) intel_sdvo_set_input_timings_for_mode(intel_sdvo,
1068 mode,
1069 adjusted_mode);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001070 } else if (intel_sdvo->is_lvds) {
Chris Wilson32aad862010-08-04 13:50:25 +01001071 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
Chris Wilson6c9547f2010-08-25 10:05:17 +01001072 intel_sdvo->sdvo_lvds_fixed_mode))
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001073 return false;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001074
Pavel Roskinc74696b2010-09-02 14:46:34 -04001075 (void) intel_sdvo_set_input_timings_for_mode(intel_sdvo,
1076 mode,
1077 adjusted_mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001078 }
Chris Wilson32aad862010-08-04 13:50:25 +01001079
1080 /* Make the CRTC code factor in the SDVO pixel multiplier. The
Chris Wilson6c9547f2010-08-25 10:05:17 +01001081 * SDVO device will factor out the multiplier during mode_set.
Chris Wilson32aad862010-08-04 13:50:25 +01001082 */
Chris Wilson6c9547f2010-08-25 10:05:17 +01001083 multiplier = intel_sdvo_get_pixel_multiplier(adjusted_mode);
1084 intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
Chris Wilson32aad862010-08-04 13:50:25 +01001085
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001086 return true;
1087}
1088
1089static void intel_sdvo_mode_set(struct drm_encoder *encoder,
1090 struct drm_display_mode *mode,
1091 struct drm_display_mode *adjusted_mode)
1092{
1093 struct drm_device *dev = encoder->dev;
1094 struct drm_i915_private *dev_priv = dev->dev_private;
1095 struct drm_crtc *crtc = encoder->crtc;
1096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson890f3352010-09-14 16:46:59 +01001097 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
Chris Wilson6c9547f2010-08-25 10:05:17 +01001098 u32 sdvox;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001099 struct intel_sdvo_in_out_map in_out;
1100 struct intel_sdvo_dtd input_dtd;
Chris Wilson6c9547f2010-08-25 10:05:17 +01001101 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
1102 int rate;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001103
1104 if (!mode)
1105 return;
1106
1107 /* First, set the input mapping for the first input to our controlled
1108 * output. This is only correct if we're a single-input device, in
1109 * which case the first input is the output from the appropriate SDVO
1110 * channel on the motherboard. In a two-input device, the first input
1111 * will be SDVOB and the second SDVOC.
1112 */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001113 in_out.in0 = intel_sdvo->attached_output;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001114 in_out.in1 = 0;
1115
Pavel Roskinc74696b2010-09-02 14:46:34 -04001116 intel_sdvo_set_value(intel_sdvo,
1117 SDVO_CMD_SET_IN_OUT_MAP,
1118 &in_out, sizeof(in_out));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001119
Chris Wilson6c9547f2010-08-25 10:05:17 +01001120 /* Set the output timings to the screen */
1121 if (!intel_sdvo_set_target_output(intel_sdvo,
1122 intel_sdvo->attached_output))
1123 return;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001124
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001125 /* We have tried to get input timing in mode_fixup, and filled into
Chris Wilson6c9547f2010-08-25 10:05:17 +01001126 * adjusted_mode.
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001127 */
Chris Wilson6c9547f2010-08-25 10:05:17 +01001128 if (intel_sdvo->is_tv || intel_sdvo->is_lvds) {
1129 input_dtd = intel_sdvo->input_dtd;
1130 } else {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001131 /* Set the output timing to the screen */
Chris Wilson32aad862010-08-04 13:50:25 +01001132 if (!intel_sdvo_set_target_output(intel_sdvo,
1133 intel_sdvo->attached_output))
1134 return;
1135
Chris Wilson6c9547f2010-08-25 10:05:17 +01001136 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
Pavel Roskinc74696b2010-09-02 14:46:34 -04001137 (void) intel_sdvo_set_output_timing(intel_sdvo, &input_dtd);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001138 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001139
1140 /* Set the input timing to the screen. Assume always input 0. */
Chris Wilson32aad862010-08-04 13:50:25 +01001141 if (!intel_sdvo_set_target_input(intel_sdvo))
1142 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001143
Chris Wilson6c9547f2010-08-25 10:05:17 +01001144 if (intel_sdvo->is_hdmi &&
1145 !intel_sdvo_set_avi_infoframe(intel_sdvo, mode))
1146 return;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001147
Chris Wilson6c9547f2010-08-25 10:05:17 +01001148 if (intel_sdvo->is_tv &&
1149 !intel_sdvo_set_tv_format(intel_sdvo))
1150 return;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001151
Pavel Roskinc74696b2010-09-02 14:46:34 -04001152 (void) intel_sdvo_set_input_timing(intel_sdvo, &input_dtd);
Jesse Barnes79e53942008-11-07 14:24:08 -08001153
Chris Wilson6c9547f2010-08-25 10:05:17 +01001154 switch (pixel_multiplier) {
1155 default:
Chris Wilson32aad862010-08-04 13:50:25 +01001156 case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1157 case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1158 case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
Jesse Barnes79e53942008-11-07 14:24:08 -08001159 }
Chris Wilson32aad862010-08-04 13:50:25 +01001160 if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
1161 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001162
1163 /* Set the SDVO control regs. */
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001164 if (IS_I965G(dev)) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01001165 sdvox = SDVO_BORDER_ENABLE;
Adam Jackson81a14b42010-07-16 14:46:32 -04001166 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1167 sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
1168 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1169 sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001170 } else {
Chris Wilson6c9547f2010-08-25 10:05:17 +01001171 sdvox = I915_READ(intel_sdvo->sdvo_reg);
Chris Wilsonea5b2132010-08-04 13:50:23 +01001172 switch (intel_sdvo->sdvo_reg) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001173 case SDVOB:
1174 sdvox &= SDVOB_PRESERVE_MASK;
1175 break;
1176 case SDVOC:
1177 sdvox &= SDVOC_PRESERVE_MASK;
1178 break;
1179 }
1180 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1181 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001182 if (intel_crtc->pipe == 1)
1183 sdvox |= SDVO_PIPE_B_SELECT;
Chris Wilson6c9547f2010-08-25 10:05:17 +01001184 if (intel_sdvo->is_hdmi)
1185 sdvox |= SDVO_AUDIO_ENABLE;
Jesse Barnes79e53942008-11-07 14:24:08 -08001186
Jesse Barnes79e53942008-11-07 14:24:08 -08001187 if (IS_I965G(dev)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001188 /* done in crtc_mode_set as the dpll_md reg must be written early */
1189 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
1190 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001191 } else {
Chris Wilson6c9547f2010-08-25 10:05:17 +01001192 sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001193 }
1194
Chris Wilson6c9547f2010-08-25 10:05:17 +01001195 if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001196 sdvox |= SDVO_STALL_SELECT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001197 intel_sdvo_write_sdvox(intel_sdvo, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001198}
1199
1200static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1201{
1202 struct drm_device *dev = encoder->dev;
1203 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson890f3352010-09-14 16:46:59 +01001204 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001205 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08001206 u32 temp;
1207
1208 if (mode != DRM_MODE_DPMS_ON) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001209 intel_sdvo_set_active_outputs(intel_sdvo, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08001210 if (0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001211 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08001212
1213 if (mode == DRM_MODE_DPMS_OFF) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001214 temp = I915_READ(intel_sdvo->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001215 if ((temp & SDVO_ENABLE) != 0) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001216 intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001217 }
1218 }
1219 } else {
1220 bool input1, input2;
1221 int i;
1222 u8 status;
1223
Chris Wilsonea5b2132010-08-04 13:50:23 +01001224 temp = I915_READ(intel_sdvo->sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08001225 if ((temp & SDVO_ENABLE) == 0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001226 intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
Jesse Barnes79e53942008-11-07 14:24:08 -08001227 for (i = 0; i < 2; i++)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001228 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08001229
Chris Wilson32aad862010-08-04 13:50:25 +01001230 status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001231 /* Warn if the device reported failure to sync.
1232 * A lot of SDVO devices fail to notify of sync, but it's
1233 * a given it the status is a success, we succeeded.
1234 */
1235 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001236 DRM_DEBUG_KMS("First %s output reported failure to "
Chris Wilsonea5b2132010-08-04 13:50:23 +01001237 "sync\n", SDVO_NAME(intel_sdvo));
Jesse Barnes79e53942008-11-07 14:24:08 -08001238 }
1239
1240 if (0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001241 intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
1242 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
Jesse Barnes79e53942008-11-07 14:24:08 -08001243 }
1244 return;
1245}
1246
Jesse Barnes79e53942008-11-07 14:24:08 -08001247static int intel_sdvo_mode_valid(struct drm_connector *connector,
1248 struct drm_display_mode *mode)
1249{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001250 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001251
1252 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1253 return MODE_NO_DBLESCAN;
1254
Chris Wilsonea5b2132010-08-04 13:50:23 +01001255 if (intel_sdvo->pixel_clock_min > mode->clock)
Jesse Barnes79e53942008-11-07 14:24:08 -08001256 return MODE_CLOCK_LOW;
1257
Chris Wilsonea5b2132010-08-04 13:50:23 +01001258 if (intel_sdvo->pixel_clock_max < mode->clock)
Jesse Barnes79e53942008-11-07 14:24:08 -08001259 return MODE_CLOCK_HIGH;
1260
Chris Wilson85454232010-08-08 14:28:23 +01001261 if (intel_sdvo->is_lvds) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001262 if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001263 return MODE_PANEL;
1264
Chris Wilsonea5b2132010-08-04 13:50:23 +01001265 if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001266 return MODE_PANEL;
1267 }
1268
Jesse Barnes79e53942008-11-07 14:24:08 -08001269 return MODE_OK;
1270}
1271
Chris Wilsonea5b2132010-08-04 13:50:23 +01001272static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
Jesse Barnes79e53942008-11-07 14:24:08 -08001273{
Chris Wilson32aad862010-08-04 13:50:25 +01001274 return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DEVICE_CAPS, caps, sizeof(*caps));
Jesse Barnes79e53942008-11-07 14:24:08 -08001275}
1276
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001277/* No use! */
1278#if 0
Jesse Barnes79e53942008-11-07 14:24:08 -08001279struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
1280{
1281 struct drm_connector *connector = NULL;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001282 struct intel_sdvo *iout = NULL;
1283 struct intel_sdvo *sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -08001284
1285 /* find the sdvo connector */
1286 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001287 iout = to_intel_sdvo(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001288
1289 if (iout->type != INTEL_OUTPUT_SDVO)
1290 continue;
1291
1292 sdvo = iout->dev_priv;
1293
Eric Anholtc751ce42010-03-25 11:48:48 -07001294 if (sdvo->sdvo_reg == SDVOB && sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001295 return connector;
1296
Eric Anholtc751ce42010-03-25 11:48:48 -07001297 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
Jesse Barnes79e53942008-11-07 14:24:08 -08001298 return connector;
1299
1300 }
1301
1302 return NULL;
1303}
1304
1305int intel_sdvo_supports_hotplug(struct drm_connector *connector)
1306{
1307 u8 response[2];
1308 u8 status;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001309 struct intel_sdvo *intel_sdvo;
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08001310 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08001311
1312 if (!connector)
1313 return 0;
1314
Chris Wilsonea5b2132010-08-04 13:50:23 +01001315 intel_sdvo = to_intel_sdvo(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001316
Chris Wilson32aad862010-08-04 13:50:25 +01001317 return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1318 &response, 2) && response[0];
Jesse Barnes79e53942008-11-07 14:24:08 -08001319}
1320
1321void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1322{
1323 u8 response[2];
1324 u8 status;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001325 struct intel_sdvo *intel_sdvo = to_intel_sdvo(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001326
Chris Wilsonea5b2132010-08-04 13:50:23 +01001327 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1328 intel_sdvo_read_response(intel_sdvo, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001329
1330 if (on) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001331 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1332 status = intel_sdvo_read_response(intel_sdvo, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001333
Chris Wilsonea5b2132010-08-04 13:50:23 +01001334 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001335 } else {
1336 response[0] = 0;
1337 response[1] = 0;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001338 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001339 }
1340
Chris Wilsonea5b2132010-08-04 13:50:23 +01001341 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1342 intel_sdvo_read_response(intel_sdvo, &response, 2);
Jesse Barnes79e53942008-11-07 14:24:08 -08001343}
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001344#endif
Jesse Barnes79e53942008-11-07 14:24:08 -08001345
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001346static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001347intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001348{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001349 int caps = 0;
1350
Chris Wilsonea5b2132010-08-04 13:50:23 +01001351 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001352 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1))
1353 caps++;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001354 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001355 (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1))
1356 caps++;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001357 if (intel_sdvo->caps.output_flags &
Roel Kluin19e1f882009-08-09 13:50:53 +02001358 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_SVID1))
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001359 caps++;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001360 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001361 (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_CVBS1))
1362 caps++;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001363 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001364 (SDVO_OUTPUT_YPRPB0 | SDVO_OUTPUT_YPRPB1))
1365 caps++;
1366
Chris Wilsonea5b2132010-08-04 13:50:23 +01001367 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001368 (SDVO_OUTPUT_SCART0 | SDVO_OUTPUT_SCART1))
1369 caps++;
1370
Chris Wilsonea5b2132010-08-04 13:50:23 +01001371 if (intel_sdvo->caps.output_flags &
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001372 (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1))
1373 caps++;
1374
1375 return (caps > 1);
1376}
1377
Keith Packard57cdaf92009-09-04 13:07:54 +08001378static struct drm_connector *
1379intel_find_analog_connector(struct drm_device *dev)
1380{
1381 struct drm_connector *connector;
Chris Wilsondf0e9242010-09-09 16:20:55 +01001382 struct intel_sdvo *encoder;
Keith Packard57cdaf92009-09-04 13:07:54 +08001383
Chris Wilsondf0e9242010-09-09 16:20:55 +01001384 list_for_each_entry(encoder,
1385 &dev->mode_config.encoder_list,
1386 base.base.head) {
1387 if (encoder->base.type == INTEL_OUTPUT_ANALOG) {
1388 list_for_each_entry(connector,
1389 &dev->mode_config.connector_list,
1390 head) {
1391 if (&encoder->base ==
1392 intel_attached_encoder(connector))
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001393 return connector;
1394 }
1395 }
Keith Packard57cdaf92009-09-04 13:07:54 +08001396 }
Chris Wilsondf0e9242010-09-09 16:20:55 +01001397
Keith Packard57cdaf92009-09-04 13:07:54 +08001398 return NULL;
1399}
1400
1401static int
1402intel_analog_is_connected(struct drm_device *dev)
1403{
1404 struct drm_connector *analog_connector;
Keith Packard57cdaf92009-09-04 13:07:54 +08001405
Chris Wilson32aad862010-08-04 13:50:25 +01001406 analog_connector = intel_find_analog_connector(dev);
Keith Packard57cdaf92009-09-04 13:07:54 +08001407 if (!analog_connector)
1408 return false;
1409
1410 if (analog_connector->funcs->detect(analog_connector) ==
1411 connector_status_disconnected)
1412 return false;
1413
1414 return true;
1415}
1416
Chris Wilsonff482d82010-09-15 10:40:38 +01001417/* Mac mini hack -- use the same DDC as the analog connector */
1418static struct edid *
1419intel_sdvo_get_analog_edid(struct drm_connector *connector)
1420{
1421 struct intel_encoder *encoder = intel_attached_encoder(connector);
1422 struct drm_device *dev = connector->dev;
1423 struct i2c_adapter *ddc;
1424 struct edid *edid;
1425 u32 ddc_reg;
1426
1427 if (!intel_analog_is_connected(dev))
1428 return NULL;
1429
1430 if (HAS_PCH_SPLIT(dev))
1431 ddc_reg = PCH_GPIOA;
1432 else
1433 ddc_reg = GPIOA;
1434
1435 ddc = intel_i2c_create(encoder, ddc_reg, "SDVO/VGA DDC BUS");
1436 if (ddc == NULL)
1437 return NULL;
1438
1439 edid = drm_get_edid(connector, ddc);
1440 intel_i2c_destroy(ddc);
1441
1442 return edid;
1443}
1444
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001445enum drm_connector_status
Adam Jackson149c36a2010-04-29 14:05:18 -04001446intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +08001447{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001448 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilson9d1a9032010-09-14 17:58:19 +01001449 enum drm_connector_status status;
1450 struct edid *edid;
Ma Ling9dff6af2009-04-02 13:13:26 +08001451
Chris Wilsonea5b2132010-08-04 13:50:23 +01001452 edid = drm_get_edid(connector, intel_sdvo->base.ddc_bus);
Keith Packard57cdaf92009-09-04 13:07:54 +08001453
Chris Wilsonea5b2132010-08-04 13:50:23 +01001454 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
Chris Wilson9d1a9032010-09-14 17:58:19 +01001455 u8 saved_ddc = intel_sdvo->ddc_bus, ddc;
1456
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001457 /*
1458 * Don't use the 1 as the argument of DDC bus switch to get
1459 * the EDID. It is used for SDVO SPD ROM.
1460 */
Chris Wilson9d1a9032010-09-14 17:58:19 +01001461 for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
1462 intel_sdvo->ddc_bus = ddc;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001463 edid = drm_get_edid(connector, intel_sdvo->base.ddc_bus);
Chris Wilson9d1a9032010-09-14 17:58:19 +01001464 if (edid)
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001465 break;
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001466 }
Chris Wilson9d1a9032010-09-14 17:58:19 +01001467
1468 /*
1469 * If we found the EDID on the other bus, maybe that is the
1470 * correct DDC bus.
1471 */
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001472 if (edid == NULL)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001473 intel_sdvo->ddc_bus = saved_ddc;
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001474 }
Chris Wilson9d1a9032010-09-14 17:58:19 +01001475
1476 /*
1477 * When there is no edid and no monitor is connected with VGA
1478 * port, try to use the CRT ddc to read the EDID for DVI-connector.
Keith Packard57cdaf92009-09-04 13:07:54 +08001479 */
Chris Wilsonff482d82010-09-15 10:40:38 +01001480 if (edid == NULL)
1481 edid = intel_sdvo_get_analog_edid(connector);
Adam Jackson149c36a2010-04-29 14:05:18 -04001482
Chris Wilson2f551c82010-09-15 10:42:50 +01001483 status = connector_status_unknown;
Ma Ling9dff6af2009-04-02 13:13:26 +08001484 if (edid != NULL) {
Adam Jackson149c36a2010-04-29 14:05:18 -04001485 /* DDC bus is shared, match EDID to connector type */
Chris Wilson9d1a9032010-09-14 17:58:19 +01001486 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1487 status = connector_status_connected;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001488 intel_sdvo->is_hdmi = drm_detect_hdmi_monitor(edid);
Chris Wilson9d1a9032010-09-14 17:58:19 +01001489 }
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001490 connector->display_info.raw_edid = NULL;
Chris Wilson9d1a9032010-09-14 17:58:19 +01001491 kfree(edid);
1492 }
Adam Jackson149c36a2010-04-29 14:05:18 -04001493
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001494 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001495}
1496
Jesse Barnes79e53942008-11-07 14:24:08 -08001497static enum drm_connector_status intel_sdvo_detect(struct drm_connector *connector)
1498{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001499 uint16_t response;
Chris Wilsondf0e9242010-09-09 16:20:55 +01001500 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilson615fb932010-08-04 13:50:24 +01001501 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Zhenyu Wang14571b42010-03-30 14:06:33 +08001502 enum drm_connector_status ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001503
Chris Wilson32aad862010-08-04 13:50:25 +01001504 if (!intel_sdvo_write_cmd(intel_sdvo,
1505 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
1506 return connector_status_unknown;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001507 if (intel_sdvo->is_tv) {
Zhao Yakuid09c23d2009-11-06 15:39:56 +08001508 /* add 30ms delay when the output type is SDVO-TV */
1509 mdelay(30);
1510 }
Chris Wilson32aad862010-08-04 13:50:25 +01001511 if (!intel_sdvo_read_response(intel_sdvo, &response, 2))
1512 return connector_status_unknown;
Jesse Barnes79e53942008-11-07 14:24:08 -08001513
Dave Airlie51c8b402009-08-20 13:38:04 +10001514 DRM_DEBUG_KMS("SDVO response %d %d\n", response & 0xff, response >> 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001515
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001516 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001517 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001518
Chris Wilsonea5b2132010-08-04 13:50:23 +01001519 intel_sdvo->attached_output = response;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001520
Chris Wilson615fb932010-08-04 13:50:24 +01001521 if ((intel_sdvo_connector->output_flag & response) == 0)
Zhenyu Wang14571b42010-03-30 14:06:33 +08001522 ret = connector_status_disconnected;
Adam Jackson149c36a2010-04-29 14:05:18 -04001523 else if (response & SDVO_TMDS_MASK)
1524 ret = intel_sdvo_hdmi_sink_detect(connector);
Zhenyu Wang14571b42010-03-30 14:06:33 +08001525 else
1526 ret = connector_status_connected;
1527
1528 /* May update encoder flag for like clock for SDVO TV, etc.*/
1529 if (ret == connector_status_connected) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001530 intel_sdvo->is_tv = false;
1531 intel_sdvo->is_lvds = false;
1532 intel_sdvo->base.needs_tv_clock = false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001533
1534 if (response & SDVO_TV_MASK) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001535 intel_sdvo->is_tv = true;
1536 intel_sdvo->base.needs_tv_clock = true;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001537 }
1538 if (response & SDVO_LVDS_MASK)
Chris Wilson85454232010-08-08 14:28:23 +01001539 intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001540 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08001541
1542 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001543}
1544
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001545static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001546{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001547 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilsonff482d82010-09-15 10:40:38 +01001548 struct edid *edid;
Keith Packard57cdaf92009-09-04 13:07:54 +08001549 int num_modes;
Jesse Barnes79e53942008-11-07 14:24:08 -08001550
1551 /* set the bus switch and get the modes */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001552 num_modes = intel_ddc_get_modes(connector, intel_sdvo->base.ddc_bus);
Chris Wilsonff482d82010-09-15 10:40:38 +01001553 if (num_modes)
1554 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001555
Keith Packard57cdaf92009-09-04 13:07:54 +08001556 /*
1557 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1558 * link between analog and digital outputs. So, if the regular SDVO
1559 * DDC fails, check to see if the analog output is disconnected, in
1560 * which case we'll look there for the digital DDC data.
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001561 */
Chris Wilsonff482d82010-09-15 10:40:38 +01001562 edid = intel_sdvo_get_analog_edid(connector);
1563 if (edid != NULL) {
1564 drm_mode_connector_update_edid_property(connector, edid);
1565 drm_add_edid_modes(connector, edid);
1566 connector->display_info.raw_edid = NULL;
1567 kfree(edid);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001568 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001569}
1570
1571/*
1572 * Set of SDVO TV modes.
1573 * Note! This is in reply order (see loop in get_tv_modes).
1574 * XXX: all 60Hz refresh?
1575 */
1576struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001577 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1578 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001579 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001580 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1581 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001582 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001583 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1584 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001585 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001586 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1587 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001588 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001589 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1590 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001591 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001592 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1593 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001594 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001595 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1596 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001597 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001598 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1599 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001600 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001601 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1602 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001603 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001604 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1605 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001606 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001607 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1608 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001609 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001610 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1611 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001612 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001613 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1614 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001615 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001616 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1617 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001618 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001619 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1620 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001621 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001622 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1623 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001624 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001625 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1626 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001627 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001628 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1629 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001630 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001631 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1632 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001633 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1634};
1635
1636static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1637{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001638 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001639 struct intel_sdvo_sdtv_resolution_request tv_res;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001640 uint32_t reply = 0, format_map = 0;
1641 int i;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001642
1643 /* Read the list of supported input resolutions for the selected TV
1644 * format.
1645 */
Chris Wilson40039752010-08-04 13:50:26 +01001646 format_map = 1 << intel_sdvo->tv_format_index;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001647 memcpy(&tv_res, &format_map,
Chris Wilson32aad862010-08-04 13:50:25 +01001648 min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08001649
Chris Wilson32aad862010-08-04 13:50:25 +01001650 if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
1651 return;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001652
Chris Wilson32aad862010-08-04 13:50:25 +01001653 BUILD_BUG_ON(sizeof(tv_res) != 3);
1654 if (!intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1655 &tv_res, sizeof(tv_res)))
1656 return;
1657 if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001658 return;
1659
1660 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001661 if (reply & (1 << i)) {
1662 struct drm_display_mode *nmode;
1663 nmode = drm_mode_duplicate(connector->dev,
Chris Wilson32aad862010-08-04 13:50:25 +01001664 &sdvo_tv_modes[i]);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001665 if (nmode)
1666 drm_mode_probed_add(connector, nmode);
1667 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001668}
1669
Ma Ling7086c872009-05-13 11:20:06 +08001670static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1671{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001672 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Ma Ling7086c872009-05-13 11:20:06 +08001673 struct drm_i915_private *dev_priv = connector->dev->dev_private;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001674 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08001675
1676 /*
1677 * Attempt to get the mode list from DDC.
1678 * Assume that the preferred modes are
1679 * arranged in priority order.
1680 */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001681 intel_ddc_get_modes(connector, intel_sdvo->base.ddc_bus);
Ma Ling7086c872009-05-13 11:20:06 +08001682 if (list_empty(&connector->probed_modes) == false)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001683 goto end;
Ma Ling7086c872009-05-13 11:20:06 +08001684
1685 /* Fetch modes from VBT */
1686 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08001687 newmode = drm_mode_duplicate(connector->dev,
1688 dev_priv->sdvo_lvds_vbt_mode);
1689 if (newmode != NULL) {
1690 /* Guarantee the mode is preferred */
1691 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1692 DRM_MODE_TYPE_DRIVER);
1693 drm_mode_probed_add(connector, newmode);
1694 }
1695 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001696
1697end:
1698 list_for_each_entry(newmode, &connector->probed_modes, head) {
1699 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001700 intel_sdvo->sdvo_lvds_fixed_mode =
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001701 drm_mode_duplicate(connector->dev, newmode);
Chris Wilson6c9547f2010-08-25 10:05:17 +01001702
1703 drm_mode_set_crtcinfo(intel_sdvo->sdvo_lvds_fixed_mode,
1704 0);
1705
Chris Wilson85454232010-08-08 14:28:23 +01001706 intel_sdvo->is_lvds = true;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001707 break;
1708 }
1709 }
1710
Ma Ling7086c872009-05-13 11:20:06 +08001711}
1712
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001713static int intel_sdvo_get_modes(struct drm_connector *connector)
1714{
Chris Wilson615fb932010-08-04 13:50:24 +01001715 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001716
Chris Wilson615fb932010-08-04 13:50:24 +01001717 if (IS_TV(intel_sdvo_connector))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001718 intel_sdvo_get_tv_modes(connector);
Chris Wilson615fb932010-08-04 13:50:24 +01001719 else if (IS_LVDS(intel_sdvo_connector))
Ma Ling7086c872009-05-13 11:20:06 +08001720 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001721 else
1722 intel_sdvo_get_ddc_modes(connector);
1723
Chris Wilson32aad862010-08-04 13:50:25 +01001724 return !list_empty(&connector->probed_modes);
Jesse Barnes79e53942008-11-07 14:24:08 -08001725}
1726
Chris Wilsonfcc8d672010-08-04 13:50:27 +01001727static void
1728intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001729{
Chris Wilson615fb932010-08-04 13:50:24 +01001730 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Zhao Yakuib9219c52009-09-10 15:45:46 +08001731 struct drm_device *dev = connector->dev;
1732
Chris Wilsonc5521702010-08-04 13:50:28 +01001733 if (intel_sdvo_connector->left)
1734 drm_property_destroy(dev, intel_sdvo_connector->left);
1735 if (intel_sdvo_connector->right)
1736 drm_property_destroy(dev, intel_sdvo_connector->right);
1737 if (intel_sdvo_connector->top)
1738 drm_property_destroy(dev, intel_sdvo_connector->top);
1739 if (intel_sdvo_connector->bottom)
1740 drm_property_destroy(dev, intel_sdvo_connector->bottom);
1741 if (intel_sdvo_connector->hpos)
1742 drm_property_destroy(dev, intel_sdvo_connector->hpos);
1743 if (intel_sdvo_connector->vpos)
1744 drm_property_destroy(dev, intel_sdvo_connector->vpos);
1745 if (intel_sdvo_connector->saturation)
1746 drm_property_destroy(dev, intel_sdvo_connector->saturation);
1747 if (intel_sdvo_connector->contrast)
1748 drm_property_destroy(dev, intel_sdvo_connector->contrast);
1749 if (intel_sdvo_connector->hue)
1750 drm_property_destroy(dev, intel_sdvo_connector->hue);
1751 if (intel_sdvo_connector->sharpness)
1752 drm_property_destroy(dev, intel_sdvo_connector->sharpness);
1753 if (intel_sdvo_connector->flicker_filter)
1754 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
1755 if (intel_sdvo_connector->flicker_filter_2d)
1756 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
1757 if (intel_sdvo_connector->flicker_filter_adaptive)
1758 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
1759 if (intel_sdvo_connector->tv_luma_filter)
1760 drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
1761 if (intel_sdvo_connector->tv_chroma_filter)
1762 drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
Chris Wilsone0442182010-08-04 13:50:29 +01001763 if (intel_sdvo_connector->dot_crawl)
1764 drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
Chris Wilsonc5521702010-08-04 13:50:28 +01001765 if (intel_sdvo_connector->brightness)
1766 drm_property_destroy(dev, intel_sdvo_connector->brightness);
Zhao Yakuib9219c52009-09-10 15:45:46 +08001767}
1768
Jesse Barnes79e53942008-11-07 14:24:08 -08001769static void intel_sdvo_destroy(struct drm_connector *connector)
1770{
Chris Wilson615fb932010-08-04 13:50:24 +01001771 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001772
Chris Wilsonc5521702010-08-04 13:50:28 +01001773 if (intel_sdvo_connector->tv_format)
Zhao Yakuice6feab2009-08-24 13:50:26 +08001774 drm_property_destroy(connector->dev,
Chris Wilsonc5521702010-08-04 13:50:28 +01001775 intel_sdvo_connector->tv_format);
Zhao Yakuice6feab2009-08-24 13:50:26 +08001776
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001777 intel_sdvo_destroy_enhance_property(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001778 drm_sysfs_connector_remove(connector);
1779 drm_connector_cleanup(connector);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001780 kfree(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001781}
1782
Zhao Yakuice6feab2009-08-24 13:50:26 +08001783static int
1784intel_sdvo_set_property(struct drm_connector *connector,
1785 struct drm_property *property,
1786 uint64_t val)
1787{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001788 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilson615fb932010-08-04 13:50:24 +01001789 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Zhao Yakuib9219c52009-09-10 15:45:46 +08001790 uint16_t temp_value;
Chris Wilson32aad862010-08-04 13:50:25 +01001791 uint8_t cmd;
1792 int ret;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001793
1794 ret = drm_connector_property_set_value(connector, property, val);
Chris Wilson32aad862010-08-04 13:50:25 +01001795 if (ret)
1796 return ret;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001797
Chris Wilsonc5521702010-08-04 13:50:28 +01001798#define CHECK_PROPERTY(name, NAME) \
1799 if (intel_sdvo_connector->name == property) { \
1800 if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
1801 if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1802 cmd = SDVO_CMD_SET_##NAME; \
1803 intel_sdvo_connector->cur_##name = temp_value; \
1804 goto set_value; \
1805 }
1806
1807 if (property == intel_sdvo_connector->tv_format) {
Chris Wilson32aad862010-08-04 13:50:25 +01001808 if (val >= TV_FORMAT_NUM)
1809 return -EINVAL;
1810
Chris Wilson40039752010-08-04 13:50:26 +01001811 if (intel_sdvo->tv_format_index ==
Chris Wilson615fb932010-08-04 13:50:24 +01001812 intel_sdvo_connector->tv_format_supported[val])
Chris Wilson32aad862010-08-04 13:50:25 +01001813 return 0;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001814
Chris Wilson40039752010-08-04 13:50:26 +01001815 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
Chris Wilsonc5521702010-08-04 13:50:28 +01001816 goto done;
Chris Wilson32aad862010-08-04 13:50:25 +01001817 } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001818 temp_value = val;
Chris Wilsonc5521702010-08-04 13:50:28 +01001819 if (intel_sdvo_connector->left == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001820 drm_connector_property_set_value(connector,
Chris Wilsonc5521702010-08-04 13:50:28 +01001821 intel_sdvo_connector->right, val);
Chris Wilson615fb932010-08-04 13:50:24 +01001822 if (intel_sdvo_connector->left_margin == temp_value)
Chris Wilson32aad862010-08-04 13:50:25 +01001823 return 0;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001824
Chris Wilson615fb932010-08-04 13:50:24 +01001825 intel_sdvo_connector->left_margin = temp_value;
1826 intel_sdvo_connector->right_margin = temp_value;
1827 temp_value = intel_sdvo_connector->max_hscan -
Chris Wilsonc5521702010-08-04 13:50:28 +01001828 intel_sdvo_connector->left_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001829 cmd = SDVO_CMD_SET_OVERSCAN_H;
Chris Wilsonc5521702010-08-04 13:50:28 +01001830 goto set_value;
1831 } else if (intel_sdvo_connector->right == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001832 drm_connector_property_set_value(connector,
Chris Wilsonc5521702010-08-04 13:50:28 +01001833 intel_sdvo_connector->left, val);
Chris Wilson615fb932010-08-04 13:50:24 +01001834 if (intel_sdvo_connector->right_margin == temp_value)
Chris Wilson32aad862010-08-04 13:50:25 +01001835 return 0;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001836
Chris Wilson615fb932010-08-04 13:50:24 +01001837 intel_sdvo_connector->left_margin = temp_value;
1838 intel_sdvo_connector->right_margin = temp_value;
1839 temp_value = intel_sdvo_connector->max_hscan -
1840 intel_sdvo_connector->left_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001841 cmd = SDVO_CMD_SET_OVERSCAN_H;
Chris Wilsonc5521702010-08-04 13:50:28 +01001842 goto set_value;
1843 } else if (intel_sdvo_connector->top == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001844 drm_connector_property_set_value(connector,
Chris Wilsonc5521702010-08-04 13:50:28 +01001845 intel_sdvo_connector->bottom, val);
Chris Wilson615fb932010-08-04 13:50:24 +01001846 if (intel_sdvo_connector->top_margin == temp_value)
Chris Wilson32aad862010-08-04 13:50:25 +01001847 return 0;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001848
Chris Wilson615fb932010-08-04 13:50:24 +01001849 intel_sdvo_connector->top_margin = temp_value;
1850 intel_sdvo_connector->bottom_margin = temp_value;
1851 temp_value = intel_sdvo_connector->max_vscan -
Chris Wilsonc5521702010-08-04 13:50:28 +01001852 intel_sdvo_connector->top_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001853 cmd = SDVO_CMD_SET_OVERSCAN_V;
Chris Wilsonc5521702010-08-04 13:50:28 +01001854 goto set_value;
1855 } else if (intel_sdvo_connector->bottom == property) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08001856 drm_connector_property_set_value(connector,
Chris Wilsonc5521702010-08-04 13:50:28 +01001857 intel_sdvo_connector->top, val);
Chris Wilson615fb932010-08-04 13:50:24 +01001858 if (intel_sdvo_connector->bottom_margin == temp_value)
Chris Wilson32aad862010-08-04 13:50:25 +01001859 return 0;
1860
Chris Wilson615fb932010-08-04 13:50:24 +01001861 intel_sdvo_connector->top_margin = temp_value;
1862 intel_sdvo_connector->bottom_margin = temp_value;
1863 temp_value = intel_sdvo_connector->max_vscan -
Chris Wilsonc5521702010-08-04 13:50:28 +01001864 intel_sdvo_connector->top_margin;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001865 cmd = SDVO_CMD_SET_OVERSCAN_V;
Chris Wilsonc5521702010-08-04 13:50:28 +01001866 goto set_value;
Zhao Yakuib9219c52009-09-10 15:45:46 +08001867 }
Chris Wilsonc5521702010-08-04 13:50:28 +01001868 CHECK_PROPERTY(hpos, HPOS)
1869 CHECK_PROPERTY(vpos, VPOS)
1870 CHECK_PROPERTY(saturation, SATURATION)
1871 CHECK_PROPERTY(contrast, CONTRAST)
1872 CHECK_PROPERTY(hue, HUE)
1873 CHECK_PROPERTY(brightness, BRIGHTNESS)
1874 CHECK_PROPERTY(sharpness, SHARPNESS)
1875 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
1876 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
1877 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
1878 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
1879 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
Chris Wilsone0442182010-08-04 13:50:29 +01001880 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
Zhao Yakuib9219c52009-09-10 15:45:46 +08001881 }
Chris Wilsonc5521702010-08-04 13:50:28 +01001882
1883 return -EINVAL; /* unknown property */
1884
1885set_value:
1886 if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
1887 return -EIO;
1888
1889
1890done:
Chris Wilsondf0e9242010-09-09 16:20:55 +01001891 if (intel_sdvo->base.base.crtc) {
1892 struct drm_crtc *crtc = intel_sdvo->base.base.crtc;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001893 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
Chris Wilsonc5521702010-08-04 13:50:28 +01001894 crtc->y, crtc->fb);
1895 }
1896
Chris Wilson32aad862010-08-04 13:50:25 +01001897 return 0;
Chris Wilsonc5521702010-08-04 13:50:28 +01001898#undef CHECK_PROPERTY
Zhao Yakuice6feab2009-08-24 13:50:26 +08001899}
1900
Jesse Barnes79e53942008-11-07 14:24:08 -08001901static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
1902 .dpms = intel_sdvo_dpms,
1903 .mode_fixup = intel_sdvo_mode_fixup,
1904 .prepare = intel_encoder_prepare,
1905 .mode_set = intel_sdvo_mode_set,
1906 .commit = intel_encoder_commit,
1907};
1908
1909static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Keith Packardc9fb15f2009-05-30 20:42:28 -07001910 .dpms = drm_helper_connector_dpms,
Jesse Barnes79e53942008-11-07 14:24:08 -08001911 .detect = intel_sdvo_detect,
1912 .fill_modes = drm_helper_probe_single_connector_modes,
Zhao Yakuice6feab2009-08-24 13:50:26 +08001913 .set_property = intel_sdvo_set_property,
Jesse Barnes79e53942008-11-07 14:24:08 -08001914 .destroy = intel_sdvo_destroy,
1915};
1916
1917static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
1918 .get_modes = intel_sdvo_get_modes,
1919 .mode_valid = intel_sdvo_mode_valid,
Chris Wilsondf0e9242010-09-09 16:20:55 +01001920 .best_encoder = intel_best_encoder,
Jesse Barnes79e53942008-11-07 14:24:08 -08001921};
1922
Hannes Ederb358d0a2008-12-18 21:18:47 +01001923static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001924{
Chris Wilson890f3352010-09-14 16:46:59 +01001925 struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001926
Chris Wilsonea5b2132010-08-04 13:50:23 +01001927 if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001928 drm_mode_destroy(encoder->dev,
Chris Wilsonea5b2132010-08-04 13:50:23 +01001929 intel_sdvo->sdvo_lvds_fixed_mode);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08001930
Chris Wilsonea5b2132010-08-04 13:50:23 +01001931 intel_encoder_destroy(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08001932}
1933
1934static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
1935 .destroy = intel_sdvo_enc_destroy,
1936};
1937
Chris Wilsonb66d8422010-08-12 15:26:41 +01001938static void
1939intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
1940{
1941 uint16_t mask = 0;
1942 unsigned int num_bits;
1943
1944 /* Make a mask of outputs less than or equal to our own priority in the
1945 * list.
1946 */
1947 switch (sdvo->controlled_output) {
1948 case SDVO_OUTPUT_LVDS1:
1949 mask |= SDVO_OUTPUT_LVDS1;
1950 case SDVO_OUTPUT_LVDS0:
1951 mask |= SDVO_OUTPUT_LVDS0;
1952 case SDVO_OUTPUT_TMDS1:
1953 mask |= SDVO_OUTPUT_TMDS1;
1954 case SDVO_OUTPUT_TMDS0:
1955 mask |= SDVO_OUTPUT_TMDS0;
1956 case SDVO_OUTPUT_RGB1:
1957 mask |= SDVO_OUTPUT_RGB1;
1958 case SDVO_OUTPUT_RGB0:
1959 mask |= SDVO_OUTPUT_RGB0;
1960 break;
1961 }
1962
1963 /* Count bits to find what number we are in the priority list. */
1964 mask &= sdvo->caps.output_flags;
1965 num_bits = hweight16(mask);
1966 /* If more than 3 outputs, default to DDC bus 3 for now. */
1967 if (num_bits > 3)
1968 num_bits = 3;
1969
1970 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1971 sdvo->ddc_bus = 1 << num_bits;
1972}
Jesse Barnes79e53942008-11-07 14:24:08 -08001973
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001974/**
1975 * Choose the appropriate DDC bus for control bus switch command for this
1976 * SDVO output based on the controlled output.
1977 *
1978 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1979 * outputs, then LVDS outputs.
1980 */
1981static void
Adam Jacksonb1083332010-04-23 16:07:40 -04001982intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
Chris Wilsonea5b2132010-08-04 13:50:23 +01001983 struct intel_sdvo *sdvo, u32 reg)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001984{
Adam Jacksonb1083332010-04-23 16:07:40 -04001985 struct sdvo_device_mapping *mapping;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001986
Adam Jacksonb1083332010-04-23 16:07:40 -04001987 if (IS_SDVOB(reg))
1988 mapping = &(dev_priv->sdvo_mappings[0]);
1989 else
1990 mapping = &(dev_priv->sdvo_mappings[1]);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001991
Chris Wilsonb66d8422010-08-12 15:26:41 +01001992 if (mapping->initialized)
1993 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
1994 else
1995 intel_sdvo_guess_ddc_bus(sdvo);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001996}
1997
1998static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001999intel_sdvo_get_digital_encoding_mode(struct intel_sdvo *intel_sdvo, int device)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002000{
Chris Wilson32aad862010-08-04 13:50:25 +01002001 return intel_sdvo_set_target_output(intel_sdvo,
2002 device == 0 ? SDVO_OUTPUT_TMDS0 : SDVO_OUTPUT_TMDS1) &&
2003 intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_ENCODE,
2004 &intel_sdvo->is_hdmi, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002005}
2006
Ma Ling619ac3b2009-05-18 16:12:46 +08002007static int intel_sdvo_master_xfer(struct i2c_adapter *i2c_adap,
2008 struct i2c_msg msgs[], int num)
2009{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002010 struct intel_sdvo *intel_sdvo;
Keith Packardf9c10a92009-05-30 12:16:25 -07002011 const struct i2c_algorithm *algo;
Chris Wilson819f3fb2010-09-14 19:11:56 +01002012 int ret;
Ma Ling619ac3b2009-05-18 16:12:46 +08002013
Chris Wilson890f3352010-09-14 16:46:59 +01002014 intel_sdvo = container_of(i2c_adap->algo_data,
2015 struct intel_sdvo,
2016 base);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002017 algo = intel_sdvo->base.i2c_bus->algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002018
Chris Wilson819f3fb2010-09-14 19:11:56 +01002019 ret = intel_sdvo_set_control_bus_switch(intel_sdvo,
2020 intel_sdvo->ddc_bus);
2021 if (ret)
2022 return ret;
2023
Ma Ling619ac3b2009-05-18 16:12:46 +08002024 return algo->master_xfer(i2c_adap, msgs, num);
2025}
2026
2027static struct i2c_algorithm intel_sdvo_i2c_bit_algo = {
2028 .master_xfer = intel_sdvo_master_xfer,
2029};
2030
yakui_zhao714605e2009-05-31 17:18:07 +08002031static u8
Eric Anholtc751ce42010-03-25 11:48:48 -07002032intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
yakui_zhao714605e2009-05-31 17:18:07 +08002033{
2034 struct drm_i915_private *dev_priv = dev->dev_private;
2035 struct sdvo_device_mapping *my_mapping, *other_mapping;
2036
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002037 if (IS_SDVOB(sdvo_reg)) {
yakui_zhao714605e2009-05-31 17:18:07 +08002038 my_mapping = &dev_priv->sdvo_mappings[0];
2039 other_mapping = &dev_priv->sdvo_mappings[1];
2040 } else {
2041 my_mapping = &dev_priv->sdvo_mappings[1];
2042 other_mapping = &dev_priv->sdvo_mappings[0];
2043 }
2044
2045 /* If the BIOS described our SDVO device, take advantage of it. */
2046 if (my_mapping->slave_addr)
2047 return my_mapping->slave_addr;
2048
2049 /* If the BIOS only described a different SDVO device, use the
2050 * address that it isn't using.
2051 */
2052 if (other_mapping->slave_addr) {
2053 if (other_mapping->slave_addr == 0x70)
2054 return 0x72;
2055 else
2056 return 0x70;
2057 }
2058
2059 /* No SDVO device info is found for another DVO port,
2060 * so use mapping assumption we had before BIOS parsing.
2061 */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002062 if (IS_SDVOB(sdvo_reg))
yakui_zhao714605e2009-05-31 17:18:07 +08002063 return 0x70;
2064 else
2065 return 0x72;
2066}
2067
Zhenyu Wang14571b42010-03-30 14:06:33 +08002068static void
Chris Wilsondf0e9242010-09-09 16:20:55 +01002069intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
2070 struct intel_sdvo *encoder)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002071{
Chris Wilsondf0e9242010-09-09 16:20:55 +01002072 drm_connector_init(encoder->base.base.dev,
2073 &connector->base.base,
2074 &intel_sdvo_connector_funcs,
2075 connector->base.base.connector_type);
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002076
Chris Wilsondf0e9242010-09-09 16:20:55 +01002077 drm_connector_helper_add(&connector->base.base,
2078 &intel_sdvo_connector_helper_funcs);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002079
Chris Wilsondf0e9242010-09-09 16:20:55 +01002080 connector->base.base.interlace_allowed = 0;
2081 connector->base.base.doublescan_allowed = 0;
2082 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002083
Chris Wilsondf0e9242010-09-09 16:20:55 +01002084 intel_connector_attach_encoder(&connector->base, &encoder->base);
2085 drm_sysfs_connector_add(&connector->base.base);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002086}
2087
2088static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002089intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002090{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002091 struct drm_encoder *encoder = &intel_sdvo->base.base;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002092 struct drm_connector *connector;
2093 struct intel_connector *intel_connector;
Chris Wilson615fb932010-08-04 13:50:24 +01002094 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002095
Chris Wilson615fb932010-08-04 13:50:24 +01002096 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2097 if (!intel_sdvo_connector)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002098 return false;
2099
Zhenyu Wang14571b42010-03-30 14:06:33 +08002100 if (device == 0) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002101 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
Chris Wilson615fb932010-08-04 13:50:24 +01002102 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002103 } else if (device == 1) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002104 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
Chris Wilson615fb932010-08-04 13:50:24 +01002105 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002106 }
2107
Chris Wilson615fb932010-08-04 13:50:24 +01002108 intel_connector = &intel_sdvo_connector->base;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002109 connector = &intel_connector->base;
Dave Airlieeb1f8e42010-05-07 06:42:51 +00002110 connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002111 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2112 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2113
Chris Wilsonea5b2132010-08-04 13:50:23 +01002114 if (intel_sdvo_get_supp_encode(intel_sdvo, &intel_sdvo->encode)
2115 && intel_sdvo_get_digital_encoding_mode(intel_sdvo, device)
2116 && intel_sdvo->is_hdmi) {
Zhenyu Wang14571b42010-03-30 14:06:33 +08002117 /* enable hdmi encoding mode if supported */
Chris Wilsonea5b2132010-08-04 13:50:23 +01002118 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
2119 intel_sdvo_set_colorimetry(intel_sdvo,
Zhenyu Wang14571b42010-03-30 14:06:33 +08002120 SDVO_COLORIMETRY_RGB256);
2121 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2122 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002123 intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2124 (1 << INTEL_ANALOG_CLONE_BIT));
Zhenyu Wang14571b42010-03-30 14:06:33 +08002125
Chris Wilsondf0e9242010-09-09 16:20:55 +01002126 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002127
2128 return true;
2129}
2130
2131static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002132intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002133{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002134 struct drm_encoder *encoder = &intel_sdvo->base.base;
2135 struct drm_connector *connector;
2136 struct intel_connector *intel_connector;
2137 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002138
Chris Wilson615fb932010-08-04 13:50:24 +01002139 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2140 if (!intel_sdvo_connector)
2141 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002142
Chris Wilson615fb932010-08-04 13:50:24 +01002143 intel_connector = &intel_sdvo_connector->base;
Chris Wilson4ef69c72010-09-09 15:14:28 +01002144 connector = &intel_connector->base;
2145 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2146 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002147
Chris Wilson4ef69c72010-09-09 15:14:28 +01002148 intel_sdvo->controlled_output |= type;
2149 intel_sdvo_connector->output_flag = type;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002150
Chris Wilson4ef69c72010-09-09 15:14:28 +01002151 intel_sdvo->is_tv = true;
2152 intel_sdvo->base.needs_tv_clock = true;
2153 intel_sdvo->base.clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002154
Chris Wilsondf0e9242010-09-09 16:20:55 +01002155 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002156
Chris Wilson4ef69c72010-09-09 15:14:28 +01002157 if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
Chris Wilson32aad862010-08-04 13:50:25 +01002158 goto err;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002159
Chris Wilson4ef69c72010-09-09 15:14:28 +01002160 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
Chris Wilson32aad862010-08-04 13:50:25 +01002161 goto err;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002162
Chris Wilson4ef69c72010-09-09 15:14:28 +01002163 return true;
Chris Wilson32aad862010-08-04 13:50:25 +01002164
2165err:
Chris Wilsonfcc8d672010-08-04 13:50:27 +01002166 intel_sdvo_destroy_enhance_property(connector);
Chris Wilson32aad862010-08-04 13:50:25 +01002167 kfree(intel_sdvo_connector);
2168 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002169}
2170
2171static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002172intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002173{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002174 struct drm_encoder *encoder = &intel_sdvo->base.base;
2175 struct drm_connector *connector;
2176 struct intel_connector *intel_connector;
2177 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002178
Chris Wilson615fb932010-08-04 13:50:24 +01002179 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2180 if (!intel_sdvo_connector)
2181 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002182
Chris Wilson615fb932010-08-04 13:50:24 +01002183 intel_connector = &intel_sdvo_connector->base;
2184 connector = &intel_connector->base;
Chris Wilson4ef69c72010-09-09 15:14:28 +01002185 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
2186 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2187 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002188
Chris Wilson4ef69c72010-09-09 15:14:28 +01002189 if (device == 0) {
2190 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2191 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2192 } else if (device == 1) {
2193 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2194 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2195 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002196
Chris Wilson4ef69c72010-09-09 15:14:28 +01002197 intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2198 (1 << INTEL_ANALOG_CLONE_BIT));
2199
Chris Wilsondf0e9242010-09-09 16:20:55 +01002200 intel_sdvo_connector_init(intel_sdvo_connector,
2201 intel_sdvo);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002202 return true;
2203}
2204
2205static bool
2206intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
2207{
2208 struct drm_encoder *encoder = &intel_sdvo->base.base;
2209 struct drm_connector *connector;
2210 struct intel_connector *intel_connector;
2211 struct intel_sdvo_connector *intel_sdvo_connector;
2212
2213 intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
2214 if (!intel_sdvo_connector)
2215 return false;
2216
2217 intel_connector = &intel_sdvo_connector->base;
2218 connector = &intel_connector->base;
2219 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2220 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2221
2222 if (device == 0) {
2223 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2224 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2225 } else if (device == 1) {
2226 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2227 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2228 }
2229
2230 intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
Chris Wilsonea5b2132010-08-04 13:50:23 +01002231 (1 << INTEL_SDVO_LVDS_CLONE_BIT));
Zhenyu Wang14571b42010-03-30 14:06:33 +08002232
Chris Wilsondf0e9242010-09-09 16:20:55 +01002233 intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
Chris Wilson4ef69c72010-09-09 15:14:28 +01002234 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
Chris Wilson32aad862010-08-04 13:50:25 +01002235 goto err;
2236
2237 return true;
2238
2239err:
Chris Wilsonfcc8d672010-08-04 13:50:27 +01002240 intel_sdvo_destroy_enhance_property(connector);
Chris Wilson32aad862010-08-04 13:50:25 +01002241 kfree(intel_sdvo_connector);
2242 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002243}
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002244
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002245static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002246intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002247{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002248 intel_sdvo->is_tv = false;
2249 intel_sdvo->base.needs_tv_clock = false;
2250 intel_sdvo->is_lvds = false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002251
Zhenyu Wang14571b42010-03-30 14:06:33 +08002252 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002253
Zhenyu Wang14571b42010-03-30 14:06:33 +08002254 if (flags & SDVO_OUTPUT_TMDS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002255 if (!intel_sdvo_dvi_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002256 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002257
Zhenyu Wang14571b42010-03-30 14:06:33 +08002258 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002259 if (!intel_sdvo_dvi_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002260 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002261
Zhenyu Wang14571b42010-03-30 14:06:33 +08002262 /* TV has no XXX1 function block */
Zhenyu Wanga1f4b7ff2010-03-29 23:16:13 +08002263 if (flags & SDVO_OUTPUT_SVID0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002264 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002265 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002266
Zhenyu Wang14571b42010-03-30 14:06:33 +08002267 if (flags & SDVO_OUTPUT_CVBS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002268 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002269 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002270
Zhenyu Wang14571b42010-03-30 14:06:33 +08002271 if (flags & SDVO_OUTPUT_RGB0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002272 if (!intel_sdvo_analog_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002273 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002274
Zhenyu Wang14571b42010-03-30 14:06:33 +08002275 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002276 if (!intel_sdvo_analog_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002277 return false;
Zhao Yakui2dd87382010-01-27 16:32:46 +08002278
Zhenyu Wang14571b42010-03-30 14:06:33 +08002279 if (flags & SDVO_OUTPUT_LVDS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002280 if (!intel_sdvo_lvds_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002281 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002282
Zhenyu Wang14571b42010-03-30 14:06:33 +08002283 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002284 if (!intel_sdvo_lvds_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002285 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002286
Zhenyu Wang14571b42010-03-30 14:06:33 +08002287 if ((flags & SDVO_OUTPUT_MASK) == 0) {
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002288 unsigned char bytes[2];
2289
Chris Wilsonea5b2132010-08-04 13:50:23 +01002290 intel_sdvo->controlled_output = 0;
2291 memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
Dave Airlie51c8b402009-08-20 13:38:04 +10002292 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +01002293 SDVO_NAME(intel_sdvo),
Dave Airlie51c8b402009-08-20 13:38:04 +10002294 bytes[0], bytes[1]);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002295 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002296 }
Chris Wilsonea5b2132010-08-04 13:50:23 +01002297 intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002298
Zhenyu Wang14571b42010-03-30 14:06:33 +08002299 return true;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002300}
2301
Chris Wilson32aad862010-08-04 13:50:25 +01002302static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
2303 struct intel_sdvo_connector *intel_sdvo_connector,
2304 int type)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002305{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002306 struct drm_device *dev = intel_sdvo->base.base.dev;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002307 struct intel_sdvo_tv_format format;
2308 uint32_t format_map, i;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002309
Chris Wilson32aad862010-08-04 13:50:25 +01002310 if (!intel_sdvo_set_target_output(intel_sdvo, type))
2311 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002312
Chris Wilson32aad862010-08-04 13:50:25 +01002313 if (!intel_sdvo_get_value(intel_sdvo,
2314 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2315 &format, sizeof(format)))
2316 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002317
Chris Wilson32aad862010-08-04 13:50:25 +01002318 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08002319
2320 if (format_map == 0)
Chris Wilson32aad862010-08-04 13:50:25 +01002321 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002322
Chris Wilson615fb932010-08-04 13:50:24 +01002323 intel_sdvo_connector->format_supported_num = 0;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002324 for (i = 0 ; i < TV_FORMAT_NUM; i++)
Chris Wilson40039752010-08-04 13:50:26 +01002325 if (format_map & (1 << i))
2326 intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002327
2328
Chris Wilsonc5521702010-08-04 13:50:28 +01002329 intel_sdvo_connector->tv_format =
Chris Wilson32aad862010-08-04 13:50:25 +01002330 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2331 "mode", intel_sdvo_connector->format_supported_num);
Chris Wilsonc5521702010-08-04 13:50:28 +01002332 if (!intel_sdvo_connector->tv_format)
Chris Wilsonfcc8d672010-08-04 13:50:27 +01002333 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002334
Chris Wilson615fb932010-08-04 13:50:24 +01002335 for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002336 drm_property_add_enum(
Chris Wilsonc5521702010-08-04 13:50:28 +01002337 intel_sdvo_connector->tv_format, i,
Chris Wilson40039752010-08-04 13:50:26 +01002338 i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002339
Chris Wilson40039752010-08-04 13:50:26 +01002340 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
Chris Wilson32aad862010-08-04 13:50:25 +01002341 drm_connector_attach_property(&intel_sdvo_connector->base.base,
Chris Wilsonc5521702010-08-04 13:50:28 +01002342 intel_sdvo_connector->tv_format, 0);
Chris Wilson32aad862010-08-04 13:50:25 +01002343 return true;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002344
2345}
2346
Chris Wilsonc5521702010-08-04 13:50:28 +01002347#define ENHANCEMENT(name, NAME) do { \
2348 if (enhancements.name) { \
2349 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2350 !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2351 return false; \
2352 intel_sdvo_connector->max_##name = data_value[0]; \
2353 intel_sdvo_connector->cur_##name = response; \
2354 intel_sdvo_connector->name = \
2355 drm_property_create(dev, DRM_MODE_PROP_RANGE, #name, 2); \
2356 if (!intel_sdvo_connector->name) return false; \
2357 intel_sdvo_connector->name->values[0] = 0; \
2358 intel_sdvo_connector->name->values[1] = data_value[0]; \
2359 drm_connector_attach_property(connector, \
2360 intel_sdvo_connector->name, \
2361 intel_sdvo_connector->cur_##name); \
2362 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2363 data_value[0], data_value[1], response); \
2364 } \
2365} while(0)
2366
2367static bool
2368intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
2369 struct intel_sdvo_connector *intel_sdvo_connector,
2370 struct intel_sdvo_enhancements_reply enhancements)
Zhao Yakuib9219c52009-09-10 15:45:46 +08002371{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002372 struct drm_device *dev = intel_sdvo->base.base.dev;
Chris Wilson32aad862010-08-04 13:50:25 +01002373 struct drm_connector *connector = &intel_sdvo_connector->base.base;
Zhao Yakuib9219c52009-09-10 15:45:46 +08002374 uint16_t response, data_value[2];
2375
Chris Wilsonc5521702010-08-04 13:50:28 +01002376 /* when horizontal overscan is supported, Add the left/right property */
2377 if (enhancements.overscan_h) {
2378 if (!intel_sdvo_get_value(intel_sdvo,
2379 SDVO_CMD_GET_MAX_OVERSCAN_H,
2380 &data_value, 4))
2381 return false;
2382
2383 if (!intel_sdvo_get_value(intel_sdvo,
2384 SDVO_CMD_GET_OVERSCAN_H,
2385 &response, 2))
2386 return false;
2387
2388 intel_sdvo_connector->max_hscan = data_value[0];
2389 intel_sdvo_connector->left_margin = data_value[0] - response;
2390 intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
2391 intel_sdvo_connector->left =
2392 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2393 "left_margin", 2);
2394 if (!intel_sdvo_connector->left)
2395 return false;
2396
2397 intel_sdvo_connector->left->values[0] = 0;
2398 intel_sdvo_connector->left->values[1] = data_value[0];
2399 drm_connector_attach_property(connector,
2400 intel_sdvo_connector->left,
2401 intel_sdvo_connector->left_margin);
2402
2403 intel_sdvo_connector->right =
2404 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2405 "right_margin", 2);
2406 if (!intel_sdvo_connector->right)
2407 return false;
2408
2409 intel_sdvo_connector->right->values[0] = 0;
2410 intel_sdvo_connector->right->values[1] = data_value[0];
2411 drm_connector_attach_property(connector,
2412 intel_sdvo_connector->right,
2413 intel_sdvo_connector->right_margin);
2414 DRM_DEBUG_KMS("h_overscan: max %d, "
2415 "default %d, current %d\n",
2416 data_value[0], data_value[1], response);
2417 }
2418
2419 if (enhancements.overscan_v) {
2420 if (!intel_sdvo_get_value(intel_sdvo,
2421 SDVO_CMD_GET_MAX_OVERSCAN_V,
2422 &data_value, 4))
2423 return false;
2424
2425 if (!intel_sdvo_get_value(intel_sdvo,
2426 SDVO_CMD_GET_OVERSCAN_V,
2427 &response, 2))
2428 return false;
2429
2430 intel_sdvo_connector->max_vscan = data_value[0];
2431 intel_sdvo_connector->top_margin = data_value[0] - response;
2432 intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
2433 intel_sdvo_connector->top =
2434 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2435 "top_margin", 2);
2436 if (!intel_sdvo_connector->top)
2437 return false;
2438
2439 intel_sdvo_connector->top->values[0] = 0;
2440 intel_sdvo_connector->top->values[1] = data_value[0];
2441 drm_connector_attach_property(connector,
2442 intel_sdvo_connector->top,
2443 intel_sdvo_connector->top_margin);
2444
2445 intel_sdvo_connector->bottom =
2446 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2447 "bottom_margin", 2);
2448 if (!intel_sdvo_connector->bottom)
2449 return false;
2450
2451 intel_sdvo_connector->bottom->values[0] = 0;
2452 intel_sdvo_connector->bottom->values[1] = data_value[0];
2453 drm_connector_attach_property(connector,
2454 intel_sdvo_connector->bottom,
2455 intel_sdvo_connector->bottom_margin);
2456 DRM_DEBUG_KMS("v_overscan: max %d, "
2457 "default %d, current %d\n",
2458 data_value[0], data_value[1], response);
2459 }
2460
2461 ENHANCEMENT(hpos, HPOS);
2462 ENHANCEMENT(vpos, VPOS);
2463 ENHANCEMENT(saturation, SATURATION);
2464 ENHANCEMENT(contrast, CONTRAST);
2465 ENHANCEMENT(hue, HUE);
2466 ENHANCEMENT(sharpness, SHARPNESS);
2467 ENHANCEMENT(brightness, BRIGHTNESS);
2468 ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2469 ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2470 ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2471 ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2472 ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
2473
Chris Wilsone0442182010-08-04 13:50:29 +01002474 if (enhancements.dot_crawl) {
2475 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2476 return false;
2477
2478 intel_sdvo_connector->max_dot_crawl = 1;
2479 intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2480 intel_sdvo_connector->dot_crawl =
2481 drm_property_create(dev, DRM_MODE_PROP_RANGE, "dot_crawl", 2);
2482 if (!intel_sdvo_connector->dot_crawl)
2483 return false;
2484
2485 intel_sdvo_connector->dot_crawl->values[0] = 0;
2486 intel_sdvo_connector->dot_crawl->values[1] = 1;
2487 drm_connector_attach_property(connector,
2488 intel_sdvo_connector->dot_crawl,
2489 intel_sdvo_connector->cur_dot_crawl);
2490 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2491 }
2492
Chris Wilsonc5521702010-08-04 13:50:28 +01002493 return true;
2494}
2495
2496static bool
2497intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
2498 struct intel_sdvo_connector *intel_sdvo_connector,
2499 struct intel_sdvo_enhancements_reply enhancements)
2500{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002501 struct drm_device *dev = intel_sdvo->base.base.dev;
Chris Wilsonc5521702010-08-04 13:50:28 +01002502 struct drm_connector *connector = &intel_sdvo_connector->base.base;
2503 uint16_t response, data_value[2];
2504
2505 ENHANCEMENT(brightness, BRIGHTNESS);
2506
2507 return true;
2508}
2509#undef ENHANCEMENT
2510
2511static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
2512 struct intel_sdvo_connector *intel_sdvo_connector)
2513{
2514 union {
2515 struct intel_sdvo_enhancements_reply reply;
2516 uint16_t response;
2517 } enhancements;
2518
Chris Wilson32aad862010-08-04 13:50:25 +01002519 if (!intel_sdvo_get_value(intel_sdvo,
2520 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
Chris Wilsonc5521702010-08-04 13:50:28 +01002521 &enhancements, sizeof(enhancements)))
Chris Wilson32aad862010-08-04 13:50:25 +01002522 return false;
2523
Chris Wilsonc5521702010-08-04 13:50:28 +01002524 if (enhancements.response == 0) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002525 DRM_DEBUG_KMS("No enhancement is supported\n");
Chris Wilson32aad862010-08-04 13:50:25 +01002526 return true;
Zhao Yakuib9219c52009-09-10 15:45:46 +08002527 }
Chris Wilson32aad862010-08-04 13:50:25 +01002528
Chris Wilsonc5521702010-08-04 13:50:28 +01002529 if (IS_TV(intel_sdvo_connector))
2530 return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2531 else if(IS_LVDS(intel_sdvo_connector))
2532 return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2533 else
2534 return true;
Chris Wilson32aad862010-08-04 13:50:25 +01002535
Zhao Yakuib9219c52009-09-10 15:45:46 +08002536}
2537
Eric Anholtc751ce42010-03-25 11:48:48 -07002538bool intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
Jesse Barnes79e53942008-11-07 14:24:08 -08002539{
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002540 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt21d40d32010-03-25 11:11:14 -07002541 struct intel_encoder *intel_encoder;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002542 struct intel_sdvo *intel_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -08002543 u8 ch[0x40];
2544 int i;
Chris Wilsonff482d82010-09-15 10:40:38 +01002545 u32 i2c_reg, ddc_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -08002546
Chris Wilsonea5b2132010-08-04 13:50:23 +01002547 intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
2548 if (!intel_sdvo)
Eric Anholt7d573822009-01-02 13:33:00 -08002549 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002550
Chris Wilsonea5b2132010-08-04 13:50:23 +01002551 intel_sdvo->sdvo_reg = sdvo_reg;
Keith Packard308cd3a2009-06-14 11:56:18 -07002552
Chris Wilsonea5b2132010-08-04 13:50:23 +01002553 intel_encoder = &intel_sdvo->base;
Eric Anholt21d40d32010-03-25 11:11:14 -07002554 intel_encoder->type = INTEL_OUTPUT_SDVO;
Chris Wilson373a3cf2010-09-15 12:03:59 +01002555 /* encoder type will be decided later */
2556 drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
Jesse Barnes79e53942008-11-07 14:24:08 -08002557
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002558 if (HAS_PCH_SPLIT(dev)) {
2559 i2c_reg = PCH_GPIOE;
2560 ddc_reg = PCH_GPIOE;
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002561 } else {
2562 i2c_reg = GPIOE;
2563 ddc_reg = GPIOE;
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002564 }
2565
Jesse Barnes79e53942008-11-07 14:24:08 -08002566 /* setup the DDC bus. */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002567 if (IS_SDVOB(sdvo_reg))
Chris Wilson890f3352010-09-14 16:46:59 +01002568 intel_encoder->i2c_bus =
2569 intel_i2c_create(intel_encoder,
2570 i2c_reg, "SDVOCTRL_E for SDVOB");
Keith Packard308cd3a2009-06-14 11:56:18 -07002571 else
Chris Wilson890f3352010-09-14 16:46:59 +01002572 intel_encoder->i2c_bus =
2573 intel_i2c_create(intel_encoder,
2574 i2c_reg, "SDVOCTRL_E for SDVOC");
Keith Packard308cd3a2009-06-14 11:56:18 -07002575
Eric Anholt21d40d32010-03-25 11:11:14 -07002576 if (!intel_encoder->i2c_bus)
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002577 goto err_inteloutput;
Jesse Barnes79e53942008-11-07 14:24:08 -08002578
Chris Wilsonea5b2132010-08-04 13:50:23 +01002579 intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, sdvo_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -08002580
Keith Packard308cd3a2009-06-14 11:56:18 -07002581 /* Save the bit-banging i2c functionality for use by the DDC wrapper */
Eric Anholt21d40d32010-03-25 11:11:14 -07002582 intel_sdvo_i2c_bit_algo.functionality = intel_encoder->i2c_bus->algo->functionality;
Jesse Barnes79e53942008-11-07 14:24:08 -08002583
Jesse Barnes79e53942008-11-07 14:24:08 -08002584 /* Read the regs to test if we can talk to the device */
2585 for (i = 0; i < 0x40; i++) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002586 if (!intel_sdvo_read_byte(intel_sdvo, i, &ch[i])) {
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002587 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002588 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
Jesse Barnes79e53942008-11-07 14:24:08 -08002589 goto err_i2c;
2590 }
2591 }
2592
Ma Ling619ac3b2009-05-18 16:12:46 +08002593 /* setup the DDC bus. */
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002594 if (IS_SDVOB(sdvo_reg)) {
Chris Wilson890f3352010-09-14 16:46:59 +01002595 intel_encoder->ddc_bus =
2596 intel_i2c_create(intel_encoder,
2597 ddc_reg, "SDVOB DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002598 dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002599 } else {
Chris Wilson890f3352010-09-14 16:46:59 +01002600 intel_encoder->ddc_bus =
2601 intel_i2c_create(intel_encoder,
2602 ddc_reg, "SDVOC DDC BUS");
Jesse Barnesb01f2c32009-12-11 11:07:17 -08002603 dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
Keith Packard57cdaf92009-09-04 13:07:54 +08002604 }
Chris Wilsonff482d82010-09-15 10:40:38 +01002605 if (intel_encoder->ddc_bus == NULL)
Ma Ling619ac3b2009-05-18 16:12:46 +08002606 goto err_i2c;
2607
Keith Packard308cd3a2009-06-14 11:56:18 -07002608 /* Wrap with our custom algo which switches to DDC mode */
Eric Anholt21d40d32010-03-25 11:11:14 -07002609 intel_encoder->ddc_bus->algo = &intel_sdvo_i2c_bit_algo;
Ma Ling619ac3b2009-05-18 16:12:46 +08002610
Chris Wilson4ef69c72010-09-09 15:14:28 +01002611 drm_encoder_helper_add(&intel_encoder->base, &intel_sdvo_helper_funcs);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002612
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002613 /* In default case sdvo lvds is false */
Chris Wilson32aad862010-08-04 13:50:25 +01002614 if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
Chris Wilson373a3cf2010-09-15 12:03:59 +01002615 goto err_i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -08002616
Chris Wilsonea5b2132010-08-04 13:50:23 +01002617 if (intel_sdvo_output_setup(intel_sdvo,
2618 intel_sdvo->caps.output_flags) != true) {
Dave Airlie51c8b402009-08-20 13:38:04 +10002619 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
Zhao Yakui461ed3c2010-03-30 15:11:33 +08002620 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
Chris Wilson373a3cf2010-09-15 12:03:59 +01002621 goto err_i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -08002622 }
2623
Chris Wilsonea5b2132010-08-04 13:50:23 +01002624 intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002625
Jesse Barnes79e53942008-11-07 14:24:08 -08002626 /* Set the input timing to the screen. Assume always input 0. */
Chris Wilson32aad862010-08-04 13:50:25 +01002627 if (!intel_sdvo_set_target_input(intel_sdvo))
Chris Wilson373a3cf2010-09-15 12:03:59 +01002628 goto err_i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -08002629
Chris Wilson32aad862010-08-04 13:50:25 +01002630 if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
2631 &intel_sdvo->pixel_clock_min,
2632 &intel_sdvo->pixel_clock_max))
Chris Wilson373a3cf2010-09-15 12:03:59 +01002633 goto err_i2c;
Jesse Barnes79e53942008-11-07 14:24:08 -08002634
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08002635 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
yakui_zhao342dc382009-06-02 14:12:00 +08002636 "clock range %dMHz - %dMHz, "
2637 "input 1: %c, input 2: %c, "
2638 "output 1: %c, output 2: %c\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +01002639 SDVO_NAME(intel_sdvo),
2640 intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
2641 intel_sdvo->caps.device_rev_id,
2642 intel_sdvo->pixel_clock_min / 1000,
2643 intel_sdvo->pixel_clock_max / 1000,
2644 (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2645 (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08002646 /* check currently supported outputs */
Chris Wilsonea5b2132010-08-04 13:50:23 +01002647 intel_sdvo->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002648 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
Chris Wilsonea5b2132010-08-04 13:50:23 +01002649 intel_sdvo->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08002650 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
Eric Anholt7d573822009-01-02 13:33:00 -08002651 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08002652
2653err_i2c:
Eric Anholt21d40d32010-03-25 11:11:14 -07002654 if (intel_encoder->ddc_bus != NULL)
2655 intel_i2c_destroy(intel_encoder->ddc_bus);
2656 if (intel_encoder->i2c_bus != NULL)
2657 intel_i2c_destroy(intel_encoder->i2c_bus);
Jonas Bonnad5b2a62009-05-15 09:10:41 +02002658err_inteloutput:
Chris Wilson373a3cf2010-09-15 12:03:59 +01002659 drm_encoder_cleanup(&intel_encoder->base);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002660 kfree(intel_sdvo);
Jesse Barnes79e53942008-11-07 14:24:08 -08002661
Eric Anholt7d573822009-01-02 13:33:00 -08002662 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08002663}