i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / gpu / drm / gma500 / psb_intel_sdvo.c
blob88b42971c0fd07356155110581ea5c7e681a8408
1 /*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
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.
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
28 #include <linux/module.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include "drmP.h"
33 #include "drm.h"
34 #include "drm_crtc.h"
35 #include "drm_edid.h"
36 #include "psb_intel_drv.h"
37 #include "gma_drm.h"
38 #include "psb_drv.h"
39 #include "psb_intel_sdvo_regs.h"
40 #include "psb_intel_reg.h"
42 #define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
43 #define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
44 #define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
45 #define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0)
47 #define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
48 SDVO_TV_MASK)
50 #define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
51 #define IS_TMDS(c) (c->output_flag & SDVO_TMDS_MASK)
52 #define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
53 #define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
56 static const char *tv_format_names[] = {
57 "NTSC_M" , "NTSC_J" , "NTSC_443",
58 "PAL_B" , "PAL_D" , "PAL_G" ,
59 "PAL_H" , "PAL_I" , "PAL_M" ,
60 "PAL_N" , "PAL_NC" , "PAL_60" ,
61 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
62 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
63 "SECAM_60"
66 #define TV_FORMAT_NUM (sizeof(tv_format_names) / sizeof(*tv_format_names))
68 struct psb_intel_sdvo {
69 struct psb_intel_encoder base;
71 struct i2c_adapter *i2c;
72 u8 slave_addr;
74 struct i2c_adapter ddc;
76 /* Register for the SDVO device: SDVOB or SDVOC */
77 int sdvo_reg;
79 /* Active outputs controlled by this SDVO output */
80 uint16_t controlled_output;
83 * Capabilities of the SDVO device returned by
84 * i830_sdvo_get_capabilities()
86 struct psb_intel_sdvo_caps caps;
88 /* Pixel clock limitations reported by the SDVO device, in kHz */
89 int pixel_clock_min, pixel_clock_max;
92 * For multiple function SDVO device,
93 * this is for current attached outputs.
95 uint16_t attached_output;
97 /**
98 * This is used to select the color range of RBG outputs in HDMI mode.
99 * It is only valid when using TMDS encoding and 8 bit per color mode.
101 uint32_t color_range;
104 * This is set if we're going to treat the device as TV-out.
106 * While we have these nice friendly flags for output types that ought
107 * to decide this for us, the S-Video output on our HDMI+S-Video card
108 * shows up as RGB1 (VGA).
110 bool is_tv;
112 /* This is for current tv format name */
113 int tv_format_index;
116 * This is set if we treat the device as HDMI, instead of DVI.
118 bool is_hdmi;
119 bool has_hdmi_monitor;
120 bool has_hdmi_audio;
123 * This is set if we detect output of sdvo device as LVDS and
124 * have a valid fixed mode to use with the panel.
126 bool is_lvds;
129 * This is sdvo fixed pannel mode pointer
131 struct drm_display_mode *sdvo_lvds_fixed_mode;
133 /* DDC bus used by this SDVO encoder */
134 uint8_t ddc_bus;
136 /* Input timings for adjusted_mode */
137 struct psb_intel_sdvo_dtd input_dtd;
140 struct psb_intel_sdvo_connector {
141 struct psb_intel_connector base;
143 /* Mark the type of connector */
144 uint16_t output_flag;
146 int force_audio;
148 /* This contains all current supported TV format */
149 u8 tv_format_supported[TV_FORMAT_NUM];
150 int format_supported_num;
151 struct drm_property *tv_format;
153 /* add the property for the SDVO-TV */
154 struct drm_property *left;
155 struct drm_property *right;
156 struct drm_property *top;
157 struct drm_property *bottom;
158 struct drm_property *hpos;
159 struct drm_property *vpos;
160 struct drm_property *contrast;
161 struct drm_property *saturation;
162 struct drm_property *hue;
163 struct drm_property *sharpness;
164 struct drm_property *flicker_filter;
165 struct drm_property *flicker_filter_adaptive;
166 struct drm_property *flicker_filter_2d;
167 struct drm_property *tv_chroma_filter;
168 struct drm_property *tv_luma_filter;
169 struct drm_property *dot_crawl;
171 /* add the property for the SDVO-TV/LVDS */
172 struct drm_property *brightness;
174 /* Add variable to record current setting for the above property */
175 u32 left_margin, right_margin, top_margin, bottom_margin;
177 /* this is to get the range of margin.*/
178 u32 max_hscan, max_vscan;
179 u32 max_hpos, cur_hpos;
180 u32 max_vpos, cur_vpos;
181 u32 cur_brightness, max_brightness;
182 u32 cur_contrast, max_contrast;
183 u32 cur_saturation, max_saturation;
184 u32 cur_hue, max_hue;
185 u32 cur_sharpness, max_sharpness;
186 u32 cur_flicker_filter, max_flicker_filter;
187 u32 cur_flicker_filter_adaptive, max_flicker_filter_adaptive;
188 u32 cur_flicker_filter_2d, max_flicker_filter_2d;
189 u32 cur_tv_chroma_filter, max_tv_chroma_filter;
190 u32 cur_tv_luma_filter, max_tv_luma_filter;
191 u32 cur_dot_crawl, max_dot_crawl;
194 static struct psb_intel_sdvo *to_psb_intel_sdvo(struct drm_encoder *encoder)
196 return container_of(encoder, struct psb_intel_sdvo, base.base);
199 static struct psb_intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
201 return container_of(psb_intel_attached_encoder(connector),
202 struct psb_intel_sdvo, base);
205 static struct psb_intel_sdvo_connector *to_psb_intel_sdvo_connector(struct drm_connector *connector)
207 return container_of(to_psb_intel_connector(connector), struct psb_intel_sdvo_connector, base);
210 static bool
211 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags);
212 static bool
213 psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
214 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
215 int type);
216 static bool
217 psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
218 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector);
221 * Writes the SDVOB or SDVOC with the given value, but always writes both
222 * SDVOB and SDVOC to work around apparent hardware issues (according to
223 * comments in the BIOS).
225 static void psb_intel_sdvo_write_sdvox(struct psb_intel_sdvo *psb_intel_sdvo, u32 val)
227 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
228 u32 bval = val, cval = val;
229 int i;
231 if (psb_intel_sdvo->sdvo_reg == SDVOB) {
232 cval = REG_READ(SDVOC);
233 } else {
234 bval = REG_READ(SDVOB);
237 * Write the registers twice for luck. Sometimes,
238 * writing them only once doesn't appear to 'stick'.
239 * The BIOS does this too. Yay, magic
241 for (i = 0; i < 2; i++)
243 REG_WRITE(SDVOB, bval);
244 REG_READ(SDVOB);
245 REG_WRITE(SDVOC, cval);
246 REG_READ(SDVOC);
250 static bool psb_intel_sdvo_read_byte(struct psb_intel_sdvo *psb_intel_sdvo, u8 addr, u8 *ch)
252 struct i2c_msg msgs[] = {
254 .addr = psb_intel_sdvo->slave_addr,
255 .flags = 0,
256 .len = 1,
257 .buf = &addr,
260 .addr = psb_intel_sdvo->slave_addr,
261 .flags = I2C_M_RD,
262 .len = 1,
263 .buf = ch,
266 int ret;
268 if ((ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, 2)) == 2)
269 return true;
271 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
272 return false;
275 #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
276 /** Mapping of command numbers to names, for debug output */
277 static const struct _sdvo_cmd_name {
278 u8 cmd;
279 const char *name;
280 } sdvo_cmd_names[] = {
281 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
282 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
283 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
289 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
290 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
291 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
292 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
325 /* Add the op code for SDVO enhancements */
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
342 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
356 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
371 /* HDMI op code */
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
373 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
374 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
377 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
378 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
379 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
380 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
381 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
382 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
383 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
384 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
385 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
386 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
387 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
388 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
389 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
390 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
391 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
394 #define IS_SDVOB(reg) (reg == SDVOB)
395 #define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
397 static void psb_intel_sdvo_debug_write(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
398 const void *args, int args_len)
400 int i;
402 DRM_DEBUG_KMS("%s: W: %02X ",
403 SDVO_NAME(psb_intel_sdvo), cmd);
404 for (i = 0; i < args_len; i++)
405 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
406 for (; i < 8; i++)
407 DRM_LOG_KMS(" ");
408 for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
409 if (cmd == sdvo_cmd_names[i].cmd) {
410 DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
411 break;
414 if (i == ARRAY_SIZE(sdvo_cmd_names))
415 DRM_LOG_KMS("(%02X)", cmd);
416 DRM_LOG_KMS("\n");
419 static const char *cmd_status_names[] = {
420 "Power on",
421 "Success",
422 "Not supported",
423 "Invalid arg",
424 "Pending",
425 "Target not specified",
426 "Scaling not supported"
429 static bool psb_intel_sdvo_write_cmd(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
430 const void *args, int args_len)
432 u8 buf[args_len*2 + 2], status;
433 struct i2c_msg msgs[args_len + 3];
434 int i, ret;
436 psb_intel_sdvo_debug_write(psb_intel_sdvo, cmd, args, args_len);
438 for (i = 0; i < args_len; i++) {
439 msgs[i].addr = psb_intel_sdvo->slave_addr;
440 msgs[i].flags = 0;
441 msgs[i].len = 2;
442 msgs[i].buf = buf + 2 *i;
443 buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
444 buf[2*i + 1] = ((u8*)args)[i];
446 msgs[i].addr = psb_intel_sdvo->slave_addr;
447 msgs[i].flags = 0;
448 msgs[i].len = 2;
449 msgs[i].buf = buf + 2*i;
450 buf[2*i + 0] = SDVO_I2C_OPCODE;
451 buf[2*i + 1] = cmd;
453 /* the following two are to read the response */
454 status = SDVO_I2C_CMD_STATUS;
455 msgs[i+1].addr = psb_intel_sdvo->slave_addr;
456 msgs[i+1].flags = 0;
457 msgs[i+1].len = 1;
458 msgs[i+1].buf = &status;
460 msgs[i+2].addr = psb_intel_sdvo->slave_addr;
461 msgs[i+2].flags = I2C_M_RD;
462 msgs[i+2].len = 1;
463 msgs[i+2].buf = &status;
465 ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, i+3);
466 if (ret < 0) {
467 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
468 return false;
470 if (ret != i+3) {
471 /* failure in I2C transfer */
472 DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
473 return false;
476 return true;
479 static bool psb_intel_sdvo_read_response(struct psb_intel_sdvo *psb_intel_sdvo,
480 void *response, int response_len)
482 u8 retry = 5;
483 u8 status;
484 int i;
486 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(psb_intel_sdvo));
489 * The documentation states that all commands will be
490 * processed within 15µs, and that we need only poll
491 * the status byte a maximum of 3 times in order for the
492 * command to be complete.
494 * Check 5 times in case the hardware failed to read the docs.
496 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
497 SDVO_I2C_CMD_STATUS,
498 &status))
499 goto log_fail;
501 while (status == SDVO_CMD_STATUS_PENDING && retry--) {
502 udelay(15);
503 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
504 SDVO_I2C_CMD_STATUS,
505 &status))
506 goto log_fail;
509 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
510 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
511 else
512 DRM_LOG_KMS("(??? %d)", status);
514 if (status != SDVO_CMD_STATUS_SUCCESS)
515 goto log_fail;
517 /* Read the command response */
518 for (i = 0; i < response_len; i++) {
519 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
520 SDVO_I2C_RETURN_0 + i,
521 &((u8 *)response)[i]))
522 goto log_fail;
523 DRM_LOG_KMS(" %02X", ((u8 *)response)[i]);
525 DRM_LOG_KMS("\n");
526 return true;
528 log_fail:
529 DRM_LOG_KMS("... failed\n");
530 return false;
533 static int psb_intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
535 if (mode->clock >= 100000)
536 return 1;
537 else if (mode->clock >= 50000)
538 return 2;
539 else
540 return 4;
543 static bool psb_intel_sdvo_set_control_bus_switch(struct psb_intel_sdvo *psb_intel_sdvo,
544 u8 ddc_bus)
546 /* This must be the immediately preceding write before the i2c xfer */
547 return psb_intel_sdvo_write_cmd(psb_intel_sdvo,
548 SDVO_CMD_SET_CONTROL_BUS_SWITCH,
549 &ddc_bus, 1);
552 static bool psb_intel_sdvo_set_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, const void *data, int len)
554 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, data, len))
555 return false;
557 return psb_intel_sdvo_read_response(psb_intel_sdvo, NULL, 0);
560 static bool
561 psb_intel_sdvo_get_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, void *value, int len)
563 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, NULL, 0))
564 return false;
566 return psb_intel_sdvo_read_response(psb_intel_sdvo, value, len);
569 static bool psb_intel_sdvo_set_target_input(struct psb_intel_sdvo *psb_intel_sdvo)
571 struct psb_intel_sdvo_set_target_input_args targets = {0};
572 return psb_intel_sdvo_set_value(psb_intel_sdvo,
573 SDVO_CMD_SET_TARGET_INPUT,
574 &targets, sizeof(targets));
578 * Return whether each input is trained.
580 * This function is making an assumption about the layout of the response,
581 * which should be checked against the docs.
583 static bool psb_intel_sdvo_get_trained_inputs(struct psb_intel_sdvo *psb_intel_sdvo, bool *input_1, bool *input_2)
585 struct psb_intel_sdvo_get_trained_inputs_response response;
587 BUILD_BUG_ON(sizeof(response) != 1);
588 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
589 &response, sizeof(response)))
590 return false;
592 *input_1 = response.input0_trained;
593 *input_2 = response.input1_trained;
594 return true;
597 static bool psb_intel_sdvo_set_active_outputs(struct psb_intel_sdvo *psb_intel_sdvo,
598 u16 outputs)
600 return psb_intel_sdvo_set_value(psb_intel_sdvo,
601 SDVO_CMD_SET_ACTIVE_OUTPUTS,
602 &outputs, sizeof(outputs));
605 static bool psb_intel_sdvo_set_encoder_power_state(struct psb_intel_sdvo *psb_intel_sdvo,
606 int mode)
608 u8 state = SDVO_ENCODER_STATE_ON;
610 switch (mode) {
611 case DRM_MODE_DPMS_ON:
612 state = SDVO_ENCODER_STATE_ON;
613 break;
614 case DRM_MODE_DPMS_STANDBY:
615 state = SDVO_ENCODER_STATE_STANDBY;
616 break;
617 case DRM_MODE_DPMS_SUSPEND:
618 state = SDVO_ENCODER_STATE_SUSPEND;
619 break;
620 case DRM_MODE_DPMS_OFF:
621 state = SDVO_ENCODER_STATE_OFF;
622 break;
625 return psb_intel_sdvo_set_value(psb_intel_sdvo,
626 SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
629 static bool psb_intel_sdvo_get_input_pixel_clock_range(struct psb_intel_sdvo *psb_intel_sdvo,
630 int *clock_min,
631 int *clock_max)
633 struct psb_intel_sdvo_pixel_clock_range clocks;
635 BUILD_BUG_ON(sizeof(clocks) != 4);
636 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
637 SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
638 &clocks, sizeof(clocks)))
639 return false;
641 /* Convert the values from units of 10 kHz to kHz. */
642 *clock_min = clocks.min * 10;
643 *clock_max = clocks.max * 10;
644 return true;
647 static bool psb_intel_sdvo_set_target_output(struct psb_intel_sdvo *psb_intel_sdvo,
648 u16 outputs)
650 return psb_intel_sdvo_set_value(psb_intel_sdvo,
651 SDVO_CMD_SET_TARGET_OUTPUT,
652 &outputs, sizeof(outputs));
655 static bool psb_intel_sdvo_set_timing(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
656 struct psb_intel_sdvo_dtd *dtd)
658 return psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
659 psb_intel_sdvo_set_value(psb_intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
662 static bool psb_intel_sdvo_set_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
663 struct psb_intel_sdvo_dtd *dtd)
665 return psb_intel_sdvo_set_timing(psb_intel_sdvo,
666 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
669 static bool psb_intel_sdvo_set_output_timing(struct psb_intel_sdvo *psb_intel_sdvo,
670 struct psb_intel_sdvo_dtd *dtd)
672 return psb_intel_sdvo_set_timing(psb_intel_sdvo,
673 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
676 static bool
677 psb_intel_sdvo_create_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
678 uint16_t clock,
679 uint16_t width,
680 uint16_t height)
682 struct psb_intel_sdvo_preferred_input_timing_args args;
684 memset(&args, 0, sizeof(args));
685 args.clock = clock;
686 args.width = width;
687 args.height = height;
688 args.interlace = 0;
690 if (psb_intel_sdvo->is_lvds &&
691 (psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
692 psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
693 args.scaled = 1;
695 return psb_intel_sdvo_set_value(psb_intel_sdvo,
696 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
697 &args, sizeof(args));
700 static bool psb_intel_sdvo_get_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
701 struct psb_intel_sdvo_dtd *dtd)
703 BUILD_BUG_ON(sizeof(dtd->part1) != 8);
704 BUILD_BUG_ON(sizeof(dtd->part2) != 8);
705 return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
706 &dtd->part1, sizeof(dtd->part1)) &&
707 psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
708 &dtd->part2, sizeof(dtd->part2));
711 static bool psb_intel_sdvo_set_clock_rate_mult(struct psb_intel_sdvo *psb_intel_sdvo, u8 val)
713 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
716 static void psb_intel_sdvo_get_dtd_from_mode(struct psb_intel_sdvo_dtd *dtd,
717 const struct drm_display_mode *mode)
719 uint16_t width, height;
720 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
721 uint16_t h_sync_offset, v_sync_offset;
723 width = mode->crtc_hdisplay;
724 height = mode->crtc_vdisplay;
726 /* do some mode translations */
727 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
728 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
730 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
731 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
733 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
734 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
736 dtd->part1.clock = mode->clock / 10;
737 dtd->part1.h_active = width & 0xff;
738 dtd->part1.h_blank = h_blank_len & 0xff;
739 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
740 ((h_blank_len >> 8) & 0xf);
741 dtd->part1.v_active = height & 0xff;
742 dtd->part1.v_blank = v_blank_len & 0xff;
743 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
744 ((v_blank_len >> 8) & 0xf);
746 dtd->part2.h_sync_off = h_sync_offset & 0xff;
747 dtd->part2.h_sync_width = h_sync_len & 0xff;
748 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
749 (v_sync_len & 0xf);
750 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
751 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
752 ((v_sync_len & 0x30) >> 4);
754 dtd->part2.dtd_flags = 0x18;
755 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
756 dtd->part2.dtd_flags |= 0x2;
757 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
758 dtd->part2.dtd_flags |= 0x4;
760 dtd->part2.sdvo_flags = 0;
761 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
762 dtd->part2.reserved = 0;
765 static void psb_intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
766 const struct psb_intel_sdvo_dtd *dtd)
768 mode->hdisplay = dtd->part1.h_active;
769 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
770 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
771 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
772 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
773 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
774 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
775 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
777 mode->vdisplay = dtd->part1.v_active;
778 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
779 mode->vsync_start = mode->vdisplay;
780 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
781 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
782 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
783 mode->vsync_end = mode->vsync_start +
784 (dtd->part2.v_sync_off_width & 0xf);
785 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
786 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
787 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
789 mode->clock = dtd->part1.clock * 10;
791 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
792 if (dtd->part2.dtd_flags & 0x2)
793 mode->flags |= DRM_MODE_FLAG_PHSYNC;
794 if (dtd->part2.dtd_flags & 0x4)
795 mode->flags |= DRM_MODE_FLAG_PVSYNC;
798 static bool psb_intel_sdvo_check_supp_encode(struct psb_intel_sdvo *psb_intel_sdvo)
800 struct psb_intel_sdvo_encode encode;
802 BUILD_BUG_ON(sizeof(encode) != 2);
803 return psb_intel_sdvo_get_value(psb_intel_sdvo,
804 SDVO_CMD_GET_SUPP_ENCODE,
805 &encode, sizeof(encode));
808 static bool psb_intel_sdvo_set_encode(struct psb_intel_sdvo *psb_intel_sdvo,
809 uint8_t mode)
811 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
814 static bool psb_intel_sdvo_set_colorimetry(struct psb_intel_sdvo *psb_intel_sdvo,
815 uint8_t mode)
817 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
820 #if 0
821 static void psb_intel_sdvo_dump_hdmi_buf(struct psb_intel_sdvo *psb_intel_sdvo)
823 int i, j;
824 uint8_t set_buf_index[2];
825 uint8_t av_split;
826 uint8_t buf_size;
827 uint8_t buf[48];
828 uint8_t *pos;
830 psb_intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
832 for (i = 0; i <= av_split; i++) {
833 set_buf_index[0] = i; set_buf_index[1] = 0;
834 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
835 set_buf_index, 2);
836 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
837 psb_intel_sdvo_read_response(encoder, &buf_size, 1);
839 pos = buf;
840 for (j = 0; j <= buf_size; j += 8) {
841 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
842 NULL, 0);
843 psb_intel_sdvo_read_response(encoder, pos, 8);
844 pos += 8;
848 #endif
850 static bool psb_intel_sdvo_set_avi_infoframe(struct psb_intel_sdvo *psb_intel_sdvo)
852 DRM_INFO("HDMI is not supported yet");
854 return false;
855 #if 0
856 struct dip_infoframe avi_if = {
857 .type = DIP_TYPE_AVI,
858 .ver = DIP_VERSION_AVI,
859 .len = DIP_LEN_AVI,
861 uint8_t tx_rate = SDVO_HBUF_TX_VSYNC;
862 uint8_t set_buf_index[2] = { 1, 0 };
863 uint64_t *data = (uint64_t *)&avi_if;
864 unsigned i;
866 intel_dip_infoframe_csum(&avi_if);
868 if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
869 SDVO_CMD_SET_HBUF_INDEX,
870 set_buf_index, 2))
871 return false;
873 for (i = 0; i < sizeof(avi_if); i += 8) {
874 if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
875 SDVO_CMD_SET_HBUF_DATA,
876 data, 8))
877 return false;
878 data++;
881 return psb_intel_sdvo_set_value(psb_intel_sdvo,
882 SDVO_CMD_SET_HBUF_TXRATE,
883 &tx_rate, 1);
884 #endif
887 static bool psb_intel_sdvo_set_tv_format(struct psb_intel_sdvo *psb_intel_sdvo)
889 struct psb_intel_sdvo_tv_format format;
890 uint32_t format_map;
892 format_map = 1 << psb_intel_sdvo->tv_format_index;
893 memset(&format, 0, sizeof(format));
894 memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
896 BUILD_BUG_ON(sizeof(format) != 6);
897 return psb_intel_sdvo_set_value(psb_intel_sdvo,
898 SDVO_CMD_SET_TV_FORMAT,
899 &format, sizeof(format));
902 static bool
903 psb_intel_sdvo_set_output_timings_from_mode(struct psb_intel_sdvo *psb_intel_sdvo,
904 struct drm_display_mode *mode)
906 struct psb_intel_sdvo_dtd output_dtd;
908 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
909 psb_intel_sdvo->attached_output))
910 return false;
912 psb_intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
913 if (!psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &output_dtd))
914 return false;
916 return true;
919 static bool
920 psb_intel_sdvo_set_input_timings_for_mode(struct psb_intel_sdvo *psb_intel_sdvo,
921 struct drm_display_mode *mode,
922 struct drm_display_mode *adjusted_mode)
924 /* Reset the input timing to the screen. Assume always input 0. */
925 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
926 return false;
928 if (!psb_intel_sdvo_create_preferred_input_timing(psb_intel_sdvo,
929 mode->clock / 10,
930 mode->hdisplay,
931 mode->vdisplay))
932 return false;
934 if (!psb_intel_sdvo_get_preferred_input_timing(psb_intel_sdvo,
935 &psb_intel_sdvo->input_dtd))
936 return false;
938 psb_intel_sdvo_get_mode_from_dtd(adjusted_mode, &psb_intel_sdvo->input_dtd);
940 drm_mode_set_crtcinfo(adjusted_mode, 0);
941 return true;
944 static bool psb_intel_sdvo_mode_fixup(struct drm_encoder *encoder,
945 struct drm_display_mode *mode,
946 struct drm_display_mode *adjusted_mode)
948 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
949 int multiplier;
951 /* We need to construct preferred input timings based on our
952 * output timings. To do that, we have to set the output
953 * timings, even though this isn't really the right place in
954 * the sequence to do it. Oh well.
956 if (psb_intel_sdvo->is_tv) {
957 if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo, mode))
958 return false;
960 (void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
961 mode,
962 adjusted_mode);
963 } else if (psb_intel_sdvo->is_lvds) {
964 if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo,
965 psb_intel_sdvo->sdvo_lvds_fixed_mode))
966 return false;
968 (void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
969 mode,
970 adjusted_mode);
973 /* Make the CRTC code factor in the SDVO pixel multiplier. The
974 * SDVO device will factor out the multiplier during mode_set.
976 multiplier = psb_intel_sdvo_get_pixel_multiplier(adjusted_mode);
977 psb_intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
979 return true;
982 static void psb_intel_sdvo_mode_set(struct drm_encoder *encoder,
983 struct drm_display_mode *mode,
984 struct drm_display_mode *adjusted_mode)
986 struct drm_device *dev = encoder->dev;
987 struct drm_crtc *crtc = encoder->crtc;
988 struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
989 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
990 u32 sdvox;
991 struct psb_intel_sdvo_in_out_map in_out;
992 struct psb_intel_sdvo_dtd input_dtd;
993 int pixel_multiplier = psb_intel_mode_get_pixel_multiplier(adjusted_mode);
994 int rate;
996 if (!mode)
997 return;
999 /* First, set the input mapping for the first input to our controlled
1000 * output. This is only correct if we're a single-input device, in
1001 * which case the first input is the output from the appropriate SDVO
1002 * channel on the motherboard. In a two-input device, the first input
1003 * will be SDVOB and the second SDVOC.
1005 in_out.in0 = psb_intel_sdvo->attached_output;
1006 in_out.in1 = 0;
1008 psb_intel_sdvo_set_value(psb_intel_sdvo,
1009 SDVO_CMD_SET_IN_OUT_MAP,
1010 &in_out, sizeof(in_out));
1012 /* Set the output timings to the screen */
1013 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1014 psb_intel_sdvo->attached_output))
1015 return;
1017 /* We have tried to get input timing in mode_fixup, and filled into
1018 * adjusted_mode.
1020 if (psb_intel_sdvo->is_tv || psb_intel_sdvo->is_lvds) {
1021 input_dtd = psb_intel_sdvo->input_dtd;
1022 } else {
1023 /* Set the output timing to the screen */
1024 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1025 psb_intel_sdvo->attached_output))
1026 return;
1028 psb_intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
1029 (void) psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &input_dtd);
1032 /* Set the input timing to the screen. Assume always input 0. */
1033 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
1034 return;
1036 if (psb_intel_sdvo->has_hdmi_monitor) {
1037 psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_HDMI);
1038 psb_intel_sdvo_set_colorimetry(psb_intel_sdvo,
1039 SDVO_COLORIMETRY_RGB256);
1040 psb_intel_sdvo_set_avi_infoframe(psb_intel_sdvo);
1041 } else
1042 psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_DVI);
1044 if (psb_intel_sdvo->is_tv &&
1045 !psb_intel_sdvo_set_tv_format(psb_intel_sdvo))
1046 return;
1048 (void) psb_intel_sdvo_set_input_timing(psb_intel_sdvo, &input_dtd);
1050 switch (pixel_multiplier) {
1051 default:
1052 case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1053 case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1054 case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
1056 if (!psb_intel_sdvo_set_clock_rate_mult(psb_intel_sdvo, rate))
1057 return;
1059 /* Set the SDVO control regs. */
1060 sdvox = REG_READ(psb_intel_sdvo->sdvo_reg);
1061 switch (psb_intel_sdvo->sdvo_reg) {
1062 case SDVOB:
1063 sdvox &= SDVOB_PRESERVE_MASK;
1064 break;
1065 case SDVOC:
1066 sdvox &= SDVOC_PRESERVE_MASK;
1067 break;
1069 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1071 if (psb_intel_crtc->pipe == 1)
1072 sdvox |= SDVO_PIPE_B_SELECT;
1073 if (psb_intel_sdvo->has_hdmi_audio)
1074 sdvox |= SDVO_AUDIO_ENABLE;
1076 /* FIXME: Check if this is needed for PSB
1077 sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1080 if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL)
1081 sdvox |= SDVO_STALL_SELECT;
1082 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, sdvox);
1085 static void psb_intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1087 struct drm_device *dev = encoder->dev;
1088 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1089 u32 temp;
1091 switch (mode) {
1092 case DRM_MODE_DPMS_ON:
1093 DRM_DEBUG("DPMS_ON");
1094 break;
1095 case DRM_MODE_DPMS_OFF:
1096 DRM_DEBUG("DPMS_OFF");
1097 break;
1098 default:
1099 DRM_DEBUG("DPMS: %d", mode);
1102 if (mode != DRM_MODE_DPMS_ON) {
1103 psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, 0);
1104 if (0)
1105 psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1107 if (mode == DRM_MODE_DPMS_OFF) {
1108 temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1109 if ((temp & SDVO_ENABLE) != 0) {
1110 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp & ~SDVO_ENABLE);
1113 } else {
1114 bool input1, input2;
1115 int i;
1116 u8 status;
1118 temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1119 if ((temp & SDVO_ENABLE) == 0)
1120 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp | SDVO_ENABLE);
1121 for (i = 0; i < 2; i++)
1122 psb_intel_wait_for_vblank(dev);
1124 status = psb_intel_sdvo_get_trained_inputs(psb_intel_sdvo, &input1, &input2);
1125 /* Warn if the device reported failure to sync.
1126 * A lot of SDVO devices fail to notify of sync, but it's
1127 * a given it the status is a success, we succeeded.
1129 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1130 DRM_DEBUG_KMS("First %s output reported failure to "
1131 "sync\n", SDVO_NAME(psb_intel_sdvo));
1134 if (0)
1135 psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1136 psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, psb_intel_sdvo->attached_output);
1138 return;
1141 static int psb_intel_sdvo_mode_valid(struct drm_connector *connector,
1142 struct drm_display_mode *mode)
1144 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1145 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1147 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1148 return MODE_NO_DBLESCAN;
1150 if (psb_intel_sdvo->pixel_clock_min > mode->clock)
1151 return MODE_CLOCK_LOW;
1153 if (psb_intel_sdvo->pixel_clock_max < mode->clock)
1154 return MODE_CLOCK_HIGH;
1156 if (psb_intel_sdvo->is_lvds) {
1157 if (mode->hdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
1158 return MODE_PANEL;
1160 if (mode->vdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
1161 return MODE_PANEL;
1164 /* We assume worst case scenario of 32 bpp here, since we don't know */
1165 if ((ALIGN(mode->hdisplay * 4, 64) * mode->vdisplay) >
1166 dev_priv->vram_stolen_size)
1167 return MODE_MEM;
1169 return MODE_OK;
1172 static bool psb_intel_sdvo_get_capabilities(struct psb_intel_sdvo *psb_intel_sdvo, struct psb_intel_sdvo_caps *caps)
1174 BUILD_BUG_ON(sizeof(*caps) != 8);
1175 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
1176 SDVO_CMD_GET_DEVICE_CAPS,
1177 caps, sizeof(*caps)))
1178 return false;
1180 DRM_DEBUG_KMS("SDVO capabilities:\n"
1181 " vendor_id: %d\n"
1182 " device_id: %d\n"
1183 " device_rev_id: %d\n"
1184 " sdvo_version_major: %d\n"
1185 " sdvo_version_minor: %d\n"
1186 " sdvo_inputs_mask: %d\n"
1187 " smooth_scaling: %d\n"
1188 " sharp_scaling: %d\n"
1189 " up_scaling: %d\n"
1190 " down_scaling: %d\n"
1191 " stall_support: %d\n"
1192 " output_flags: %d\n",
1193 caps->vendor_id,
1194 caps->device_id,
1195 caps->device_rev_id,
1196 caps->sdvo_version_major,
1197 caps->sdvo_version_minor,
1198 caps->sdvo_inputs_mask,
1199 caps->smooth_scaling,
1200 caps->sharp_scaling,
1201 caps->up_scaling,
1202 caps->down_scaling,
1203 caps->stall_support,
1204 caps->output_flags);
1206 return true;
1209 /* No use! */
1210 #if 0
1211 struct drm_connector* psb_intel_sdvo_find(struct drm_device *dev, int sdvoB)
1213 struct drm_connector *connector = NULL;
1214 struct psb_intel_sdvo *iout = NULL;
1215 struct psb_intel_sdvo *sdvo;
1217 /* find the sdvo connector */
1218 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1219 iout = to_psb_intel_sdvo(connector);
1221 if (iout->type != INTEL_OUTPUT_SDVO)
1222 continue;
1224 sdvo = iout->dev_priv;
1226 if (sdvo->sdvo_reg == SDVOB && sdvoB)
1227 return connector;
1229 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
1230 return connector;
1234 return NULL;
1237 int psb_intel_sdvo_supports_hotplug(struct drm_connector *connector)
1239 u8 response[2];
1240 u8 status;
1241 struct psb_intel_sdvo *psb_intel_sdvo;
1242 DRM_DEBUG_KMS("\n");
1244 if (!connector)
1245 return 0;
1247 psb_intel_sdvo = to_psb_intel_sdvo(connector);
1249 return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1250 &response, 2) && response[0];
1253 void psb_intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1255 u8 response[2];
1256 u8 status;
1257 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(connector);
1259 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1260 psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1262 if (on) {
1263 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1264 status = psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1266 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1267 } else {
1268 response[0] = 0;
1269 response[1] = 0;
1270 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1273 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1274 psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1276 #endif
1278 static bool
1279 psb_intel_sdvo_multifunc_encoder(struct psb_intel_sdvo *psb_intel_sdvo)
1281 /* Is there more than one type of output? */
1282 int caps = psb_intel_sdvo->caps.output_flags & 0xf;
1283 return caps & -caps;
1286 static struct edid *
1287 psb_intel_sdvo_get_edid(struct drm_connector *connector)
1289 struct psb_intel_sdvo *sdvo = intel_attached_sdvo(connector);
1290 return drm_get_edid(connector, &sdvo->ddc);
1293 /* Mac mini hack -- use the same DDC as the analog connector */
1294 static struct edid *
1295 psb_intel_sdvo_get_analog_edid(struct drm_connector *connector)
1297 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1299 return drm_get_edid(connector,
1300 &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
1301 return NULL;
1304 enum drm_connector_status
1305 psb_intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
1307 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1308 enum drm_connector_status status;
1309 struct edid *edid;
1311 edid = psb_intel_sdvo_get_edid(connector);
1313 if (edid == NULL && psb_intel_sdvo_multifunc_encoder(psb_intel_sdvo)) {
1314 u8 ddc, saved_ddc = psb_intel_sdvo->ddc_bus;
1317 * Don't use the 1 as the argument of DDC bus switch to get
1318 * the EDID. It is used for SDVO SPD ROM.
1320 for (ddc = psb_intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
1321 psb_intel_sdvo->ddc_bus = ddc;
1322 edid = psb_intel_sdvo_get_edid(connector);
1323 if (edid)
1324 break;
1327 * If we found the EDID on the other bus,
1328 * assume that is the correct DDC bus.
1330 if (edid == NULL)
1331 psb_intel_sdvo->ddc_bus = saved_ddc;
1335 * When there is no edid and no monitor is connected with VGA
1336 * port, try to use the CRT ddc to read the EDID for DVI-connector.
1338 if (edid == NULL)
1339 edid = psb_intel_sdvo_get_analog_edid(connector);
1341 status = connector_status_unknown;
1342 if (edid != NULL) {
1343 /* DDC bus is shared, match EDID to connector type */
1344 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1345 status = connector_status_connected;
1346 if (psb_intel_sdvo->is_hdmi) {
1347 psb_intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1348 psb_intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
1350 } else
1351 status = connector_status_disconnected;
1352 connector->display_info.raw_edid = NULL;
1353 kfree(edid);
1356 if (status == connector_status_connected) {
1357 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1358 if (psb_intel_sdvo_connector->force_audio)
1359 psb_intel_sdvo->has_hdmi_audio = psb_intel_sdvo_connector->force_audio > 0;
1362 return status;
1365 static enum drm_connector_status
1366 psb_intel_sdvo_detect(struct drm_connector *connector, bool force)
1368 uint16_t response;
1369 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1370 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1371 enum drm_connector_status ret;
1373 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1374 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
1375 return connector_status_unknown;
1377 /* add 30ms delay when the output type might be TV */
1378 if (psb_intel_sdvo->caps.output_flags &
1379 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_CVBS0))
1380 mdelay(30);
1382 if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2))
1383 return connector_status_unknown;
1385 DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1386 response & 0xff, response >> 8,
1387 psb_intel_sdvo_connector->output_flag);
1389 if (response == 0)
1390 return connector_status_disconnected;
1392 psb_intel_sdvo->attached_output = response;
1394 psb_intel_sdvo->has_hdmi_monitor = false;
1395 psb_intel_sdvo->has_hdmi_audio = false;
1397 if ((psb_intel_sdvo_connector->output_flag & response) == 0)
1398 ret = connector_status_disconnected;
1399 else if (IS_TMDS(psb_intel_sdvo_connector))
1400 ret = psb_intel_sdvo_hdmi_sink_detect(connector);
1401 else {
1402 struct edid *edid;
1404 /* if we have an edid check it matches the connection */
1405 edid = psb_intel_sdvo_get_edid(connector);
1406 if (edid == NULL)
1407 edid = psb_intel_sdvo_get_analog_edid(connector);
1408 if (edid != NULL) {
1409 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1410 ret = connector_status_disconnected;
1411 else
1412 ret = connector_status_connected;
1413 connector->display_info.raw_edid = NULL;
1414 kfree(edid);
1415 } else
1416 ret = connector_status_connected;
1419 /* May update encoder flag for like clock for SDVO TV, etc.*/
1420 if (ret == connector_status_connected) {
1421 psb_intel_sdvo->is_tv = false;
1422 psb_intel_sdvo->is_lvds = false;
1423 psb_intel_sdvo->base.needs_tv_clock = false;
1425 if (response & SDVO_TV_MASK) {
1426 psb_intel_sdvo->is_tv = true;
1427 psb_intel_sdvo->base.needs_tv_clock = true;
1429 if (response & SDVO_LVDS_MASK)
1430 psb_intel_sdvo->is_lvds = psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL;
1433 return ret;
1436 static void psb_intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1438 struct edid *edid;
1440 /* set the bus switch and get the modes */
1441 edid = psb_intel_sdvo_get_edid(connector);
1444 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1445 * link between analog and digital outputs. So, if the regular SDVO
1446 * DDC fails, check to see if the analog output is disconnected, in
1447 * which case we'll look there for the digital DDC data.
1449 if (edid == NULL)
1450 edid = psb_intel_sdvo_get_analog_edid(connector);
1452 if (edid != NULL) {
1453 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1454 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1455 bool connector_is_digital = !!IS_TMDS(psb_intel_sdvo_connector);
1457 if (connector_is_digital == monitor_is_digital) {
1458 drm_mode_connector_update_edid_property(connector, edid);
1459 drm_add_edid_modes(connector, edid);
1462 connector->display_info.raw_edid = NULL;
1463 kfree(edid);
1468 * Set of SDVO TV modes.
1469 * Note! This is in reply order (see loop in get_tv_modes).
1470 * XXX: all 60Hz refresh?
1472 static const struct drm_display_mode sdvo_tv_modes[] = {
1473 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1474 416, 0, 200, 201, 232, 233, 0,
1475 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1476 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1477 416, 0, 240, 241, 272, 273, 0,
1478 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1479 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1480 496, 0, 300, 301, 332, 333, 0,
1481 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1482 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1483 736, 0, 350, 351, 382, 383, 0,
1484 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1485 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1486 736, 0, 400, 401, 432, 433, 0,
1487 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1488 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1489 736, 0, 480, 481, 512, 513, 0,
1490 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1491 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1492 800, 0, 480, 481, 512, 513, 0,
1493 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1494 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1495 800, 0, 576, 577, 608, 609, 0,
1496 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1497 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1498 816, 0, 350, 351, 382, 383, 0,
1499 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1500 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1501 816, 0, 400, 401, 432, 433, 0,
1502 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1503 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1504 816, 0, 480, 481, 512, 513, 0,
1505 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1506 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1507 816, 0, 540, 541, 572, 573, 0,
1508 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1509 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1510 816, 0, 576, 577, 608, 609, 0,
1511 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1512 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1513 864, 0, 576, 577, 608, 609, 0,
1514 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1515 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1516 896, 0, 600, 601, 632, 633, 0,
1517 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1518 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1519 928, 0, 624, 625, 656, 657, 0,
1520 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1521 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1522 1016, 0, 766, 767, 798, 799, 0,
1523 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1524 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1525 1120, 0, 768, 769, 800, 801, 0,
1526 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1527 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1528 1376, 0, 1024, 1025, 1056, 1057, 0,
1529 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1532 static void psb_intel_sdvo_get_tv_modes(struct drm_connector *connector)
1534 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1535 struct psb_intel_sdvo_sdtv_resolution_request tv_res;
1536 uint32_t reply = 0, format_map = 0;
1537 int i;
1539 /* Read the list of supported input resolutions for the selected TV
1540 * format.
1542 format_map = 1 << psb_intel_sdvo->tv_format_index;
1543 memcpy(&tv_res, &format_map,
1544 min(sizeof(format_map), sizeof(struct psb_intel_sdvo_sdtv_resolution_request)));
1546 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, psb_intel_sdvo->attached_output))
1547 return;
1549 BUILD_BUG_ON(sizeof(tv_res) != 3);
1550 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1551 SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1552 &tv_res, sizeof(tv_res)))
1553 return;
1554 if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &reply, 3))
1555 return;
1557 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
1558 if (reply & (1 << i)) {
1559 struct drm_display_mode *nmode;
1560 nmode = drm_mode_duplicate(connector->dev,
1561 &sdvo_tv_modes[i]);
1562 if (nmode)
1563 drm_mode_probed_add(connector, nmode);
1567 static void psb_intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1569 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1570 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1571 struct drm_display_mode *newmode;
1574 * Attempt to get the mode list from DDC.
1575 * Assume that the preferred modes are
1576 * arranged in priority order.
1578 psb_intel_ddc_get_modes(connector, psb_intel_sdvo->i2c);
1579 if (list_empty(&connector->probed_modes) == false)
1580 goto end;
1582 /* Fetch modes from VBT */
1583 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
1584 newmode = drm_mode_duplicate(connector->dev,
1585 dev_priv->sdvo_lvds_vbt_mode);
1586 if (newmode != NULL) {
1587 /* Guarantee the mode is preferred */
1588 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1589 DRM_MODE_TYPE_DRIVER);
1590 drm_mode_probed_add(connector, newmode);
1594 end:
1595 list_for_each_entry(newmode, &connector->probed_modes, head) {
1596 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1597 psb_intel_sdvo->sdvo_lvds_fixed_mode =
1598 drm_mode_duplicate(connector->dev, newmode);
1600 drm_mode_set_crtcinfo(psb_intel_sdvo->sdvo_lvds_fixed_mode,
1603 psb_intel_sdvo->is_lvds = true;
1604 break;
1610 static int psb_intel_sdvo_get_modes(struct drm_connector *connector)
1612 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1614 if (IS_TV(psb_intel_sdvo_connector))
1615 psb_intel_sdvo_get_tv_modes(connector);
1616 else if (IS_LVDS(psb_intel_sdvo_connector))
1617 psb_intel_sdvo_get_lvds_modes(connector);
1618 else
1619 psb_intel_sdvo_get_ddc_modes(connector);
1621 return !list_empty(&connector->probed_modes);
1624 static void
1625 psb_intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
1627 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1628 struct drm_device *dev = connector->dev;
1630 if (psb_intel_sdvo_connector->left)
1631 drm_property_destroy(dev, psb_intel_sdvo_connector->left);
1632 if (psb_intel_sdvo_connector->right)
1633 drm_property_destroy(dev, psb_intel_sdvo_connector->right);
1634 if (psb_intel_sdvo_connector->top)
1635 drm_property_destroy(dev, psb_intel_sdvo_connector->top);
1636 if (psb_intel_sdvo_connector->bottom)
1637 drm_property_destroy(dev, psb_intel_sdvo_connector->bottom);
1638 if (psb_intel_sdvo_connector->hpos)
1639 drm_property_destroy(dev, psb_intel_sdvo_connector->hpos);
1640 if (psb_intel_sdvo_connector->vpos)
1641 drm_property_destroy(dev, psb_intel_sdvo_connector->vpos);
1642 if (psb_intel_sdvo_connector->saturation)
1643 drm_property_destroy(dev, psb_intel_sdvo_connector->saturation);
1644 if (psb_intel_sdvo_connector->contrast)
1645 drm_property_destroy(dev, psb_intel_sdvo_connector->contrast);
1646 if (psb_intel_sdvo_connector->hue)
1647 drm_property_destroy(dev, psb_intel_sdvo_connector->hue);
1648 if (psb_intel_sdvo_connector->sharpness)
1649 drm_property_destroy(dev, psb_intel_sdvo_connector->sharpness);
1650 if (psb_intel_sdvo_connector->flicker_filter)
1651 drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter);
1652 if (psb_intel_sdvo_connector->flicker_filter_2d)
1653 drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter_2d);
1654 if (psb_intel_sdvo_connector->flicker_filter_adaptive)
1655 drm_property_destroy(dev, psb_intel_sdvo_connector->flicker_filter_adaptive);
1656 if (psb_intel_sdvo_connector->tv_luma_filter)
1657 drm_property_destroy(dev, psb_intel_sdvo_connector->tv_luma_filter);
1658 if (psb_intel_sdvo_connector->tv_chroma_filter)
1659 drm_property_destroy(dev, psb_intel_sdvo_connector->tv_chroma_filter);
1660 if (psb_intel_sdvo_connector->dot_crawl)
1661 drm_property_destroy(dev, psb_intel_sdvo_connector->dot_crawl);
1662 if (psb_intel_sdvo_connector->brightness)
1663 drm_property_destroy(dev, psb_intel_sdvo_connector->brightness);
1666 static void psb_intel_sdvo_destroy(struct drm_connector *connector)
1668 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1670 if (psb_intel_sdvo_connector->tv_format)
1671 drm_property_destroy(connector->dev,
1672 psb_intel_sdvo_connector->tv_format);
1674 psb_intel_sdvo_destroy_enhance_property(connector);
1675 drm_sysfs_connector_remove(connector);
1676 drm_connector_cleanup(connector);
1677 kfree(connector);
1680 static bool psb_intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1682 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1683 struct edid *edid;
1684 bool has_audio = false;
1686 if (!psb_intel_sdvo->is_hdmi)
1687 return false;
1689 edid = psb_intel_sdvo_get_edid(connector);
1690 if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
1691 has_audio = drm_detect_monitor_audio(edid);
1693 return has_audio;
1696 static int
1697 psb_intel_sdvo_set_property(struct drm_connector *connector,
1698 struct drm_property *property,
1699 uint64_t val)
1701 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1702 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1703 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1704 uint16_t temp_value;
1705 uint8_t cmd;
1706 int ret;
1708 ret = drm_connector_property_set_value(connector, property, val);
1709 if (ret)
1710 return ret;
1712 if (property == dev_priv->force_audio_property) {
1713 int i = val;
1714 bool has_audio;
1716 if (i == psb_intel_sdvo_connector->force_audio)
1717 return 0;
1719 psb_intel_sdvo_connector->force_audio = i;
1721 if (i == 0)
1722 has_audio = psb_intel_sdvo_detect_hdmi_audio(connector);
1723 else
1724 has_audio = i > 0;
1726 if (has_audio == psb_intel_sdvo->has_hdmi_audio)
1727 return 0;
1729 psb_intel_sdvo->has_hdmi_audio = has_audio;
1730 goto done;
1733 if (property == dev_priv->broadcast_rgb_property) {
1734 if (val == !!psb_intel_sdvo->color_range)
1735 return 0;
1737 psb_intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
1738 goto done;
1741 #define CHECK_PROPERTY(name, NAME) \
1742 if (psb_intel_sdvo_connector->name == property) { \
1743 if (psb_intel_sdvo_connector->cur_##name == temp_value) return 0; \
1744 if (psb_intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1745 cmd = SDVO_CMD_SET_##NAME; \
1746 psb_intel_sdvo_connector->cur_##name = temp_value; \
1747 goto set_value; \
1750 if (property == psb_intel_sdvo_connector->tv_format) {
1751 if (val >= TV_FORMAT_NUM)
1752 return -EINVAL;
1754 if (psb_intel_sdvo->tv_format_index ==
1755 psb_intel_sdvo_connector->tv_format_supported[val])
1756 return 0;
1758 psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[val];
1759 goto done;
1760 } else if (IS_TV_OR_LVDS(psb_intel_sdvo_connector)) {
1761 temp_value = val;
1762 if (psb_intel_sdvo_connector->left == property) {
1763 drm_connector_property_set_value(connector,
1764 psb_intel_sdvo_connector->right, val);
1765 if (psb_intel_sdvo_connector->left_margin == temp_value)
1766 return 0;
1768 psb_intel_sdvo_connector->left_margin = temp_value;
1769 psb_intel_sdvo_connector->right_margin = temp_value;
1770 temp_value = psb_intel_sdvo_connector->max_hscan -
1771 psb_intel_sdvo_connector->left_margin;
1772 cmd = SDVO_CMD_SET_OVERSCAN_H;
1773 goto set_value;
1774 } else if (psb_intel_sdvo_connector->right == property) {
1775 drm_connector_property_set_value(connector,
1776 psb_intel_sdvo_connector->left, val);
1777 if (psb_intel_sdvo_connector->right_margin == temp_value)
1778 return 0;
1780 psb_intel_sdvo_connector->left_margin = temp_value;
1781 psb_intel_sdvo_connector->right_margin = temp_value;
1782 temp_value = psb_intel_sdvo_connector->max_hscan -
1783 psb_intel_sdvo_connector->left_margin;
1784 cmd = SDVO_CMD_SET_OVERSCAN_H;
1785 goto set_value;
1786 } else if (psb_intel_sdvo_connector->top == property) {
1787 drm_connector_property_set_value(connector,
1788 psb_intel_sdvo_connector->bottom, val);
1789 if (psb_intel_sdvo_connector->top_margin == temp_value)
1790 return 0;
1792 psb_intel_sdvo_connector->top_margin = temp_value;
1793 psb_intel_sdvo_connector->bottom_margin = temp_value;
1794 temp_value = psb_intel_sdvo_connector->max_vscan -
1795 psb_intel_sdvo_connector->top_margin;
1796 cmd = SDVO_CMD_SET_OVERSCAN_V;
1797 goto set_value;
1798 } else if (psb_intel_sdvo_connector->bottom == property) {
1799 drm_connector_property_set_value(connector,
1800 psb_intel_sdvo_connector->top, val);
1801 if (psb_intel_sdvo_connector->bottom_margin == temp_value)
1802 return 0;
1804 psb_intel_sdvo_connector->top_margin = temp_value;
1805 psb_intel_sdvo_connector->bottom_margin = temp_value;
1806 temp_value = psb_intel_sdvo_connector->max_vscan -
1807 psb_intel_sdvo_connector->top_margin;
1808 cmd = SDVO_CMD_SET_OVERSCAN_V;
1809 goto set_value;
1811 CHECK_PROPERTY(hpos, HPOS)
1812 CHECK_PROPERTY(vpos, VPOS)
1813 CHECK_PROPERTY(saturation, SATURATION)
1814 CHECK_PROPERTY(contrast, CONTRAST)
1815 CHECK_PROPERTY(hue, HUE)
1816 CHECK_PROPERTY(brightness, BRIGHTNESS)
1817 CHECK_PROPERTY(sharpness, SHARPNESS)
1818 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
1819 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
1820 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
1821 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
1822 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
1823 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
1826 return -EINVAL; /* unknown property */
1828 set_value:
1829 if (!psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &temp_value, 2))
1830 return -EIO;
1833 done:
1834 if (psb_intel_sdvo->base.base.crtc) {
1835 struct drm_crtc *crtc = psb_intel_sdvo->base.base.crtc;
1836 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
1837 crtc->y, crtc->fb);
1840 return 0;
1841 #undef CHECK_PROPERTY
1844 static const struct drm_encoder_helper_funcs psb_intel_sdvo_helper_funcs = {
1845 .dpms = psb_intel_sdvo_dpms,
1846 .mode_fixup = psb_intel_sdvo_mode_fixup,
1847 .prepare = psb_intel_encoder_prepare,
1848 .mode_set = psb_intel_sdvo_mode_set,
1849 .commit = psb_intel_encoder_commit,
1852 static const struct drm_connector_funcs psb_intel_sdvo_connector_funcs = {
1853 .dpms = drm_helper_connector_dpms,
1854 .detect = psb_intel_sdvo_detect,
1855 .fill_modes = drm_helper_probe_single_connector_modes,
1856 .set_property = psb_intel_sdvo_set_property,
1857 .destroy = psb_intel_sdvo_destroy,
1860 static const struct drm_connector_helper_funcs psb_intel_sdvo_connector_helper_funcs = {
1861 .get_modes = psb_intel_sdvo_get_modes,
1862 .mode_valid = psb_intel_sdvo_mode_valid,
1863 .best_encoder = psb_intel_best_encoder,
1866 static void psb_intel_sdvo_enc_destroy(struct drm_encoder *encoder)
1868 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1870 if (psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL)
1871 drm_mode_destroy(encoder->dev,
1872 psb_intel_sdvo->sdvo_lvds_fixed_mode);
1874 i2c_del_adapter(&psb_intel_sdvo->ddc);
1875 psb_intel_encoder_destroy(encoder);
1878 static const struct drm_encoder_funcs psb_intel_sdvo_enc_funcs = {
1879 .destroy = psb_intel_sdvo_enc_destroy,
1882 static void
1883 psb_intel_sdvo_guess_ddc_bus(struct psb_intel_sdvo *sdvo)
1885 /* FIXME: At the moment, ddc_bus = 2 is the only thing that works.
1886 * We need to figure out if this is true for all available poulsbo
1887 * hardware, or if we need to fiddle with the guessing code above.
1888 * The problem might go away if we can parse sdvo mappings from bios */
1889 sdvo->ddc_bus = 2;
1891 #if 0
1892 uint16_t mask = 0;
1893 unsigned int num_bits;
1895 /* Make a mask of outputs less than or equal to our own priority in the
1896 * list.
1898 switch (sdvo->controlled_output) {
1899 case SDVO_OUTPUT_LVDS1:
1900 mask |= SDVO_OUTPUT_LVDS1;
1901 case SDVO_OUTPUT_LVDS0:
1902 mask |= SDVO_OUTPUT_LVDS0;
1903 case SDVO_OUTPUT_TMDS1:
1904 mask |= SDVO_OUTPUT_TMDS1;
1905 case SDVO_OUTPUT_TMDS0:
1906 mask |= SDVO_OUTPUT_TMDS0;
1907 case SDVO_OUTPUT_RGB1:
1908 mask |= SDVO_OUTPUT_RGB1;
1909 case SDVO_OUTPUT_RGB0:
1910 mask |= SDVO_OUTPUT_RGB0;
1911 break;
1914 /* Count bits to find what number we are in the priority list. */
1915 mask &= sdvo->caps.output_flags;
1916 num_bits = hweight16(mask);
1917 /* If more than 3 outputs, default to DDC bus 3 for now. */
1918 if (num_bits > 3)
1919 num_bits = 3;
1921 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1922 sdvo->ddc_bus = 1 << num_bits;
1923 #endif
1927 * Choose the appropriate DDC bus for control bus switch command for this
1928 * SDVO output based on the controlled output.
1930 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1931 * outputs, then LVDS outputs.
1933 static void
1934 psb_intel_sdvo_select_ddc_bus(struct drm_psb_private *dev_priv,
1935 struct psb_intel_sdvo *sdvo, u32 reg)
1937 struct sdvo_device_mapping *mapping;
1939 if (IS_SDVOB(reg))
1940 mapping = &(dev_priv->sdvo_mappings[0]);
1941 else
1942 mapping = &(dev_priv->sdvo_mappings[1]);
1944 if (mapping->initialized)
1945 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
1946 else
1947 psb_intel_sdvo_guess_ddc_bus(sdvo);
1950 static void
1951 psb_intel_sdvo_select_i2c_bus(struct drm_psb_private *dev_priv,
1952 struct psb_intel_sdvo *sdvo, u32 reg)
1954 struct sdvo_device_mapping *mapping;
1955 u8 pin, speed;
1957 if (IS_SDVOB(reg))
1958 mapping = &dev_priv->sdvo_mappings[0];
1959 else
1960 mapping = &dev_priv->sdvo_mappings[1];
1962 pin = GMBUS_PORT_DPB;
1963 speed = GMBUS_RATE_1MHZ >> 8;
1964 if (mapping->initialized) {
1965 pin = mapping->i2c_pin;
1966 speed = mapping->i2c_speed;
1969 if (pin < GMBUS_NUM_PORTS) {
1970 sdvo->i2c = &dev_priv->gmbus[pin].adapter;
1971 gma_intel_gmbus_set_speed(sdvo->i2c, speed);
1972 gma_intel_gmbus_force_bit(sdvo->i2c, true);
1973 } else
1974 sdvo->i2c = &dev_priv->gmbus[GMBUS_PORT_DPB].adapter;
1977 static bool
1978 psb_intel_sdvo_is_hdmi_connector(struct psb_intel_sdvo *psb_intel_sdvo, int device)
1980 return psb_intel_sdvo_check_supp_encode(psb_intel_sdvo);
1983 static u8
1984 psb_intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
1986 struct drm_psb_private *dev_priv = dev->dev_private;
1987 struct sdvo_device_mapping *my_mapping, *other_mapping;
1989 if (IS_SDVOB(sdvo_reg)) {
1990 my_mapping = &dev_priv->sdvo_mappings[0];
1991 other_mapping = &dev_priv->sdvo_mappings[1];
1992 } else {
1993 my_mapping = &dev_priv->sdvo_mappings[1];
1994 other_mapping = &dev_priv->sdvo_mappings[0];
1997 /* If the BIOS described our SDVO device, take advantage of it. */
1998 if (my_mapping->slave_addr)
1999 return my_mapping->slave_addr;
2001 /* If the BIOS only described a different SDVO device, use the
2002 * address that it isn't using.
2004 if (other_mapping->slave_addr) {
2005 if (other_mapping->slave_addr == 0x70)
2006 return 0x72;
2007 else
2008 return 0x70;
2011 /* No SDVO device info is found for another DVO port,
2012 * so use mapping assumption we had before BIOS parsing.
2014 if (IS_SDVOB(sdvo_reg))
2015 return 0x70;
2016 else
2017 return 0x72;
2020 static void
2021 psb_intel_sdvo_connector_init(struct psb_intel_sdvo_connector *connector,
2022 struct psb_intel_sdvo *encoder)
2024 drm_connector_init(encoder->base.base.dev,
2025 &connector->base.base,
2026 &psb_intel_sdvo_connector_funcs,
2027 connector->base.base.connector_type);
2029 drm_connector_helper_add(&connector->base.base,
2030 &psb_intel_sdvo_connector_helper_funcs);
2032 connector->base.base.interlace_allowed = 0;
2033 connector->base.base.doublescan_allowed = 0;
2034 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
2036 psb_intel_connector_attach_encoder(&connector->base, &encoder->base);
2037 drm_sysfs_connector_add(&connector->base.base);
2040 static void
2041 psb_intel_sdvo_add_hdmi_properties(struct psb_intel_sdvo_connector *connector)
2043 /* FIXME: We don't support HDMI at the moment
2044 struct drm_device *dev = connector->base.base.dev;
2046 intel_attach_force_audio_property(&connector->base.base);
2047 if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev))
2048 intel_attach_broadcast_rgb_property(&connector->base.base);
2052 static bool
2053 psb_intel_sdvo_dvi_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2055 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2056 struct drm_connector *connector;
2057 struct psb_intel_connector *intel_connector;
2058 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2060 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2061 if (!psb_intel_sdvo_connector)
2062 return false;
2064 if (device == 0) {
2065 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
2066 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
2067 } else if (device == 1) {
2068 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
2069 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
2072 intel_connector = &psb_intel_sdvo_connector->base;
2073 connector = &intel_connector->base;
2074 // connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
2075 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2076 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2078 if (psb_intel_sdvo_is_hdmi_connector(psb_intel_sdvo, device)) {
2079 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2080 psb_intel_sdvo->is_hdmi = true;
2082 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2083 (1 << INTEL_ANALOG_CLONE_BIT));
2085 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2086 if (psb_intel_sdvo->is_hdmi)
2087 psb_intel_sdvo_add_hdmi_properties(psb_intel_sdvo_connector);
2089 return true;
2092 static bool
2093 psb_intel_sdvo_tv_init(struct psb_intel_sdvo *psb_intel_sdvo, int type)
2095 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2096 struct drm_connector *connector;
2097 struct psb_intel_connector *intel_connector;
2098 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2100 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2101 if (!psb_intel_sdvo_connector)
2102 return false;
2104 intel_connector = &psb_intel_sdvo_connector->base;
2105 connector = &intel_connector->base;
2106 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2107 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2109 psb_intel_sdvo->controlled_output |= type;
2110 psb_intel_sdvo_connector->output_flag = type;
2112 psb_intel_sdvo->is_tv = true;
2113 psb_intel_sdvo->base.needs_tv_clock = true;
2114 psb_intel_sdvo->base.clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
2116 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2118 if (!psb_intel_sdvo_tv_create_property(psb_intel_sdvo, psb_intel_sdvo_connector, type))
2119 goto err;
2121 if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2122 goto err;
2124 return true;
2126 err:
2127 psb_intel_sdvo_destroy(connector);
2128 return false;
2131 static bool
2132 psb_intel_sdvo_analog_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2134 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2135 struct drm_connector *connector;
2136 struct psb_intel_connector *intel_connector;
2137 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2139 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2140 if (!psb_intel_sdvo_connector)
2141 return false;
2143 intel_connector = &psb_intel_sdvo_connector->base;
2144 connector = &intel_connector->base;
2145 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
2146 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2147 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2149 if (device == 0) {
2150 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2151 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2152 } else if (device == 1) {
2153 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2154 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2157 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2158 (1 << INTEL_ANALOG_CLONE_BIT));
2160 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector,
2161 psb_intel_sdvo);
2162 return true;
2165 static bool
2166 psb_intel_sdvo_lvds_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2168 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2169 struct drm_connector *connector;
2170 struct psb_intel_connector *intel_connector;
2171 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2173 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2174 if (!psb_intel_sdvo_connector)
2175 return false;
2177 intel_connector = &psb_intel_sdvo_connector->base;
2178 connector = &intel_connector->base;
2179 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2180 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2182 if (device == 0) {
2183 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2184 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2185 } else if (device == 1) {
2186 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2187 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2190 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
2191 (1 << INTEL_SDVO_LVDS_CLONE_BIT));
2193 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2194 if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2195 goto err;
2197 return true;
2199 err:
2200 psb_intel_sdvo_destroy(connector);
2201 return false;
2204 static bool
2205 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags)
2207 psb_intel_sdvo->is_tv = false;
2208 psb_intel_sdvo->base.needs_tv_clock = false;
2209 psb_intel_sdvo->is_lvds = false;
2211 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
2213 if (flags & SDVO_OUTPUT_TMDS0)
2214 if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 0))
2215 return false;
2217 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2218 if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 1))
2219 return false;
2221 /* TV has no XXX1 function block */
2222 if (flags & SDVO_OUTPUT_SVID0)
2223 if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_SVID0))
2224 return false;
2226 if (flags & SDVO_OUTPUT_CVBS0)
2227 if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_CVBS0))
2228 return false;
2230 if (flags & SDVO_OUTPUT_RGB0)
2231 if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 0))
2232 return false;
2234 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2235 if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 1))
2236 return false;
2238 if (flags & SDVO_OUTPUT_LVDS0)
2239 if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 0))
2240 return false;
2242 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2243 if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 1))
2244 return false;
2246 if ((flags & SDVO_OUTPUT_MASK) == 0) {
2247 unsigned char bytes[2];
2249 psb_intel_sdvo->controlled_output = 0;
2250 memcpy(bytes, &psb_intel_sdvo->caps.output_flags, 2);
2251 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2252 SDVO_NAME(psb_intel_sdvo),
2253 bytes[0], bytes[1]);
2254 return false;
2256 psb_intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
2258 return true;
2261 static bool psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
2262 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2263 int type)
2265 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2266 struct psb_intel_sdvo_tv_format format;
2267 uint32_t format_map, i;
2269 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, type))
2270 return false;
2272 BUILD_BUG_ON(sizeof(format) != 6);
2273 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2274 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2275 &format, sizeof(format)))
2276 return false;
2278 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
2280 if (format_map == 0)
2281 return false;
2283 psb_intel_sdvo_connector->format_supported_num = 0;
2284 for (i = 0 ; i < TV_FORMAT_NUM; i++)
2285 if (format_map & (1 << i))
2286 psb_intel_sdvo_connector->tv_format_supported[psb_intel_sdvo_connector->format_supported_num++] = i;
2289 psb_intel_sdvo_connector->tv_format =
2290 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2291 "mode", psb_intel_sdvo_connector->format_supported_num);
2292 if (!psb_intel_sdvo_connector->tv_format)
2293 return false;
2295 for (i = 0; i < psb_intel_sdvo_connector->format_supported_num; i++)
2296 drm_property_add_enum(
2297 psb_intel_sdvo_connector->tv_format, i,
2298 i, tv_format_names[psb_intel_sdvo_connector->tv_format_supported[i]]);
2300 psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[0];
2301 drm_connector_attach_property(&psb_intel_sdvo_connector->base.base,
2302 psb_intel_sdvo_connector->tv_format, 0);
2303 return true;
2307 #define ENHANCEMENT(name, NAME) do { \
2308 if (enhancements.name) { \
2309 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2310 !psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2311 return false; \
2312 psb_intel_sdvo_connector->max_##name = data_value[0]; \
2313 psb_intel_sdvo_connector->cur_##name = response; \
2314 psb_intel_sdvo_connector->name = \
2315 drm_property_create(dev, DRM_MODE_PROP_RANGE, #name, 2); \
2316 if (!psb_intel_sdvo_connector->name) return false; \
2317 psb_intel_sdvo_connector->name->values[0] = 0; \
2318 psb_intel_sdvo_connector->name->values[1] = data_value[0]; \
2319 drm_connector_attach_property(connector, \
2320 psb_intel_sdvo_connector->name, \
2321 psb_intel_sdvo_connector->cur_##name); \
2322 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2323 data_value[0], data_value[1], response); \
2325 } while(0)
2327 static bool
2328 psb_intel_sdvo_create_enhance_property_tv(struct psb_intel_sdvo *psb_intel_sdvo,
2329 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2330 struct psb_intel_sdvo_enhancements_reply enhancements)
2332 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2333 struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2334 uint16_t response, data_value[2];
2336 /* when horizontal overscan is supported, Add the left/right property */
2337 if (enhancements.overscan_h) {
2338 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2339 SDVO_CMD_GET_MAX_OVERSCAN_H,
2340 &data_value, 4))
2341 return false;
2343 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2344 SDVO_CMD_GET_OVERSCAN_H,
2345 &response, 2))
2346 return false;
2348 psb_intel_sdvo_connector->max_hscan = data_value[0];
2349 psb_intel_sdvo_connector->left_margin = data_value[0] - response;
2350 psb_intel_sdvo_connector->right_margin = psb_intel_sdvo_connector->left_margin;
2351 psb_intel_sdvo_connector->left =
2352 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2353 "left_margin", 2);
2354 if (!psb_intel_sdvo_connector->left)
2355 return false;
2357 psb_intel_sdvo_connector->left->values[0] = 0;
2358 psb_intel_sdvo_connector->left->values[1] = data_value[0];
2359 drm_connector_attach_property(connector,
2360 psb_intel_sdvo_connector->left,
2361 psb_intel_sdvo_connector->left_margin);
2363 psb_intel_sdvo_connector->right =
2364 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2365 "right_margin", 2);
2366 if (!psb_intel_sdvo_connector->right)
2367 return false;
2369 psb_intel_sdvo_connector->right->values[0] = 0;
2370 psb_intel_sdvo_connector->right->values[1] = data_value[0];
2371 drm_connector_attach_property(connector,
2372 psb_intel_sdvo_connector->right,
2373 psb_intel_sdvo_connector->right_margin);
2374 DRM_DEBUG_KMS("h_overscan: max %d, "
2375 "default %d, current %d\n",
2376 data_value[0], data_value[1], response);
2379 if (enhancements.overscan_v) {
2380 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2381 SDVO_CMD_GET_MAX_OVERSCAN_V,
2382 &data_value, 4))
2383 return false;
2385 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2386 SDVO_CMD_GET_OVERSCAN_V,
2387 &response, 2))
2388 return false;
2390 psb_intel_sdvo_connector->max_vscan = data_value[0];
2391 psb_intel_sdvo_connector->top_margin = data_value[0] - response;
2392 psb_intel_sdvo_connector->bottom_margin = psb_intel_sdvo_connector->top_margin;
2393 psb_intel_sdvo_connector->top =
2394 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2395 "top_margin", 2);
2396 if (!psb_intel_sdvo_connector->top)
2397 return false;
2399 psb_intel_sdvo_connector->top->values[0] = 0;
2400 psb_intel_sdvo_connector->top->values[1] = data_value[0];
2401 drm_connector_attach_property(connector,
2402 psb_intel_sdvo_connector->top,
2403 psb_intel_sdvo_connector->top_margin);
2405 psb_intel_sdvo_connector->bottom =
2406 drm_property_create(dev, DRM_MODE_PROP_RANGE,
2407 "bottom_margin", 2);
2408 if (!psb_intel_sdvo_connector->bottom)
2409 return false;
2411 psb_intel_sdvo_connector->bottom->values[0] = 0;
2412 psb_intel_sdvo_connector->bottom->values[1] = data_value[0];
2413 drm_connector_attach_property(connector,
2414 psb_intel_sdvo_connector->bottom,
2415 psb_intel_sdvo_connector->bottom_margin);
2416 DRM_DEBUG_KMS("v_overscan: max %d, "
2417 "default %d, current %d\n",
2418 data_value[0], data_value[1], response);
2421 ENHANCEMENT(hpos, HPOS);
2422 ENHANCEMENT(vpos, VPOS);
2423 ENHANCEMENT(saturation, SATURATION);
2424 ENHANCEMENT(contrast, CONTRAST);
2425 ENHANCEMENT(hue, HUE);
2426 ENHANCEMENT(sharpness, SHARPNESS);
2427 ENHANCEMENT(brightness, BRIGHTNESS);
2428 ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2429 ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2430 ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2431 ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2432 ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
2434 if (enhancements.dot_crawl) {
2435 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2436 return false;
2438 psb_intel_sdvo_connector->max_dot_crawl = 1;
2439 psb_intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2440 psb_intel_sdvo_connector->dot_crawl =
2441 drm_property_create(dev, DRM_MODE_PROP_RANGE, "dot_crawl", 2);
2442 if (!psb_intel_sdvo_connector->dot_crawl)
2443 return false;
2445 psb_intel_sdvo_connector->dot_crawl->values[0] = 0;
2446 psb_intel_sdvo_connector->dot_crawl->values[1] = 1;
2447 drm_connector_attach_property(connector,
2448 psb_intel_sdvo_connector->dot_crawl,
2449 psb_intel_sdvo_connector->cur_dot_crawl);
2450 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2453 return true;
2456 static bool
2457 psb_intel_sdvo_create_enhance_property_lvds(struct psb_intel_sdvo *psb_intel_sdvo,
2458 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2459 struct psb_intel_sdvo_enhancements_reply enhancements)
2461 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2462 struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2463 uint16_t response, data_value[2];
2465 ENHANCEMENT(brightness, BRIGHTNESS);
2467 return true;
2469 #undef ENHANCEMENT
2471 static bool psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
2472 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector)
2474 union {
2475 struct psb_intel_sdvo_enhancements_reply reply;
2476 uint16_t response;
2477 } enhancements;
2479 BUILD_BUG_ON(sizeof(enhancements) != 2);
2481 enhancements.response = 0;
2482 psb_intel_sdvo_get_value(psb_intel_sdvo,
2483 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2484 &enhancements, sizeof(enhancements));
2485 if (enhancements.response == 0) {
2486 DRM_DEBUG_KMS("No enhancement is supported\n");
2487 return true;
2490 if (IS_TV(psb_intel_sdvo_connector))
2491 return psb_intel_sdvo_create_enhance_property_tv(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2492 else if(IS_LVDS(psb_intel_sdvo_connector))
2493 return psb_intel_sdvo_create_enhance_property_lvds(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2494 else
2495 return true;
2498 static int psb_intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2499 struct i2c_msg *msgs,
2500 int num)
2502 struct psb_intel_sdvo *sdvo = adapter->algo_data;
2504 if (!psb_intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2505 return -EIO;
2507 return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2510 static u32 psb_intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2512 struct psb_intel_sdvo *sdvo = adapter->algo_data;
2513 return sdvo->i2c->algo->functionality(sdvo->i2c);
2516 static const struct i2c_algorithm psb_intel_sdvo_ddc_proxy = {
2517 .master_xfer = psb_intel_sdvo_ddc_proxy_xfer,
2518 .functionality = psb_intel_sdvo_ddc_proxy_func
2521 static bool
2522 psb_intel_sdvo_init_ddc_proxy(struct psb_intel_sdvo *sdvo,
2523 struct drm_device *dev)
2525 sdvo->ddc.owner = THIS_MODULE;
2526 sdvo->ddc.class = I2C_CLASS_DDC;
2527 snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2528 sdvo->ddc.dev.parent = &dev->pdev->dev;
2529 sdvo->ddc.algo_data = sdvo;
2530 sdvo->ddc.algo = &psb_intel_sdvo_ddc_proxy;
2532 return i2c_add_adapter(&sdvo->ddc) == 0;
2535 bool psb_intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
2537 struct drm_psb_private *dev_priv = dev->dev_private;
2538 struct psb_intel_encoder *psb_intel_encoder;
2539 struct psb_intel_sdvo *psb_intel_sdvo;
2540 int i;
2542 psb_intel_sdvo = kzalloc(sizeof(struct psb_intel_sdvo), GFP_KERNEL);
2543 if (!psb_intel_sdvo)
2544 return false;
2546 psb_intel_sdvo->sdvo_reg = sdvo_reg;
2547 psb_intel_sdvo->slave_addr = psb_intel_sdvo_get_slave_addr(dev, sdvo_reg) >> 1;
2548 psb_intel_sdvo_select_i2c_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2549 if (!psb_intel_sdvo_init_ddc_proxy(psb_intel_sdvo, dev)) {
2550 kfree(psb_intel_sdvo);
2551 return false;
2554 /* encoder type will be decided later */
2555 psb_intel_encoder = &psb_intel_sdvo->base;
2556 psb_intel_encoder->type = INTEL_OUTPUT_SDVO;
2557 drm_encoder_init(dev, &psb_intel_encoder->base, &psb_intel_sdvo_enc_funcs, 0);
2559 /* Read the regs to test if we can talk to the device */
2560 for (i = 0; i < 0x40; i++) {
2561 u8 byte;
2563 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo, i, &byte)) {
2564 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
2565 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2566 goto err;
2570 if (IS_SDVOB(sdvo_reg))
2571 dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
2572 else
2573 dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
2575 drm_encoder_helper_add(&psb_intel_encoder->base, &psb_intel_sdvo_helper_funcs);
2577 /* In default case sdvo lvds is false */
2578 if (!psb_intel_sdvo_get_capabilities(psb_intel_sdvo, &psb_intel_sdvo->caps))
2579 goto err;
2581 if (psb_intel_sdvo_output_setup(psb_intel_sdvo,
2582 psb_intel_sdvo->caps.output_flags) != true) {
2583 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
2584 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2585 goto err;
2588 psb_intel_sdvo_select_ddc_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2590 /* Set the input timing to the screen. Assume always input 0. */
2591 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
2592 goto err;
2594 if (!psb_intel_sdvo_get_input_pixel_clock_range(psb_intel_sdvo,
2595 &psb_intel_sdvo->pixel_clock_min,
2596 &psb_intel_sdvo->pixel_clock_max))
2597 goto err;
2599 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
2600 "clock range %dMHz - %dMHz, "
2601 "input 1: %c, input 2: %c, "
2602 "output 1: %c, output 2: %c\n",
2603 SDVO_NAME(psb_intel_sdvo),
2604 psb_intel_sdvo->caps.vendor_id, psb_intel_sdvo->caps.device_id,
2605 psb_intel_sdvo->caps.device_rev_id,
2606 psb_intel_sdvo->pixel_clock_min / 1000,
2607 psb_intel_sdvo->pixel_clock_max / 1000,
2608 (psb_intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2609 (psb_intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2610 /* check currently supported outputs */
2611 psb_intel_sdvo->caps.output_flags &
2612 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
2613 psb_intel_sdvo->caps.output_flags &
2614 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2615 return true;
2617 err:
2618 drm_encoder_cleanup(&psb_intel_encoder->base);
2619 i2c_del_adapter(&psb_intel_sdvo->ddc);
2620 kfree(psb_intel_sdvo);
2622 return false;