Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / gma500 / psb_intel_sdvo.c
blob84507912be841b2709c1dc3652db190a6320e896
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 <drm/drmP.h>
33 #include <drm/drm_crtc.h>
34 #include <drm/drm_edid.h>
35 #include "psb_intel_drv.h"
36 #include <drm/gma_drm.h>
37 #include "psb_drv.h"
38 #include "psb_intel_sdvo_regs.h"
39 #include "psb_intel_reg.h"
40 #include <linux/kernel.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 struct psb_intel_sdvo {
67 struct gma_encoder base;
69 struct i2c_adapter *i2c;
70 u8 slave_addr;
72 struct i2c_adapter ddc;
74 /* Register for the SDVO device: SDVOB or SDVOC */
75 int sdvo_reg;
77 /* Active outputs controlled by this SDVO output */
78 uint16_t controlled_output;
81 * Capabilities of the SDVO device returned by
82 * i830_sdvo_get_capabilities()
84 struct psb_intel_sdvo_caps caps;
86 /* Pixel clock limitations reported by the SDVO device, in kHz */
87 int pixel_clock_min, pixel_clock_max;
90 * For multiple function SDVO device,
91 * this is for current attached outputs.
93 uint16_t attached_output;
95 /**
96 * This is used to select the color range of RBG outputs in HDMI mode.
97 * It is only valid when using TMDS encoding and 8 bit per color mode.
99 uint32_t color_range;
102 * This is set if we're going to treat the device as TV-out.
104 * While we have these nice friendly flags for output types that ought
105 * to decide this for us, the S-Video output on our HDMI+S-Video card
106 * shows up as RGB1 (VGA).
108 bool is_tv;
110 /* This is for current tv format name */
111 int tv_format_index;
114 * This is set if we treat the device as HDMI, instead of DVI.
116 bool is_hdmi;
117 bool has_hdmi_monitor;
118 bool has_hdmi_audio;
121 * This is set if we detect output of sdvo device as LVDS and
122 * have a valid fixed mode to use with the panel.
124 bool is_lvds;
127 * This is sdvo fixed pannel mode pointer
129 struct drm_display_mode *sdvo_lvds_fixed_mode;
131 /* DDC bus used by this SDVO encoder */
132 uint8_t ddc_bus;
134 /* Input timings for adjusted_mode */
135 struct psb_intel_sdvo_dtd input_dtd;
137 /* Saved SDVO output states */
138 uint32_t saveSDVO; /* Can be SDVOB or SDVOC depending on sdvo_reg */
141 struct psb_intel_sdvo_connector {
142 struct gma_connector base;
144 /* Mark the type of connector */
145 uint16_t output_flag;
147 int force_audio;
149 /* This contains all current supported TV format */
150 u8 tv_format_supported[ARRAY_SIZE(tv_format_names)];
151 int format_supported_num;
152 struct drm_property *tv_format;
154 /* add the property for the SDVO-TV */
155 struct drm_property *left;
156 struct drm_property *right;
157 struct drm_property *top;
158 struct drm_property *bottom;
159 struct drm_property *hpos;
160 struct drm_property *vpos;
161 struct drm_property *contrast;
162 struct drm_property *saturation;
163 struct drm_property *hue;
164 struct drm_property *sharpness;
165 struct drm_property *flicker_filter;
166 struct drm_property *flicker_filter_adaptive;
167 struct drm_property *flicker_filter_2d;
168 struct drm_property *tv_chroma_filter;
169 struct drm_property *tv_luma_filter;
170 struct drm_property *dot_crawl;
172 /* add the property for the SDVO-TV/LVDS */
173 struct drm_property *brightness;
175 /* Add variable to record current setting for the above property */
176 u32 left_margin, right_margin, top_margin, bottom_margin;
178 /* this is to get the range of margin.*/
179 u32 max_hscan, max_vscan;
180 u32 max_hpos, cur_hpos;
181 u32 max_vpos, cur_vpos;
182 u32 cur_brightness, max_brightness;
183 u32 cur_contrast, max_contrast;
184 u32 cur_saturation, max_saturation;
185 u32 cur_hue, max_hue;
186 u32 cur_sharpness, max_sharpness;
187 u32 cur_flicker_filter, max_flicker_filter;
188 u32 cur_flicker_filter_adaptive, max_flicker_filter_adaptive;
189 u32 cur_flicker_filter_2d, max_flicker_filter_2d;
190 u32 cur_tv_chroma_filter, max_tv_chroma_filter;
191 u32 cur_tv_luma_filter, max_tv_luma_filter;
192 u32 cur_dot_crawl, max_dot_crawl;
195 static struct psb_intel_sdvo *to_psb_intel_sdvo(struct drm_encoder *encoder)
197 return container_of(encoder, struct psb_intel_sdvo, base.base);
200 static struct psb_intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
202 return container_of(gma_attached_encoder(connector),
203 struct psb_intel_sdvo, base);
206 static struct psb_intel_sdvo_connector *to_psb_intel_sdvo_connector(struct drm_connector *connector)
208 return container_of(to_gma_connector(connector), struct psb_intel_sdvo_connector, base);
211 static bool
212 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags);
213 static bool
214 psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
215 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
216 int type);
217 static bool
218 psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
219 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector);
222 * Writes the SDVOB or SDVOC with the given value, but always writes both
223 * SDVOB and SDVOC to work around apparent hardware issues (according to
224 * comments in the BIOS).
226 static void psb_intel_sdvo_write_sdvox(struct psb_intel_sdvo *psb_intel_sdvo, u32 val)
228 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
229 u32 bval = val, cval = val;
230 int i, j;
231 int need_aux = IS_MRST(dev) ? 1 : 0;
233 for (j = 0; j <= need_aux; j++) {
234 if (psb_intel_sdvo->sdvo_reg == SDVOB)
235 cval = REG_READ_WITH_AUX(SDVOC, j);
236 else
237 bval = REG_READ_WITH_AUX(SDVOB, j);
240 * Write the registers twice for luck. Sometimes,
241 * writing them only once doesn't appear to 'stick'.
242 * The BIOS does this too. Yay, magic
244 for (i = 0; i < 2; i++) {
245 REG_WRITE_WITH_AUX(SDVOB, bval, j);
246 REG_READ_WITH_AUX(SDVOB, j);
247 REG_WRITE_WITH_AUX(SDVOC, cval, j);
248 REG_READ_WITH_AUX(SDVOC, j);
253 static bool psb_intel_sdvo_read_byte(struct psb_intel_sdvo *psb_intel_sdvo, u8 addr, u8 *ch)
255 struct i2c_msg msgs[] = {
257 .addr = psb_intel_sdvo->slave_addr,
258 .flags = 0,
259 .len = 1,
260 .buf = &addr,
263 .addr = psb_intel_sdvo->slave_addr,
264 .flags = I2C_M_RD,
265 .len = 1,
266 .buf = ch,
269 int ret;
271 if ((ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, 2)) == 2)
272 return true;
274 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
275 return false;
278 #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
279 /** Mapping of command numbers to names, for debug output */
280 static const struct _sdvo_cmd_name {
281 u8 cmd;
282 const char *name;
283 } sdvo_cmd_names[] = {
284 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
285 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
286 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
287 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
288 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
289 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
290 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
291 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
292 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
328 /* Add the op code for SDVO enhancements */
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
336 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
337 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
342 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
356 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
370 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
371 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
374 /* HDMI op code */
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
377 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
378 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
379 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
380 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
381 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
382 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
383 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
384 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
385 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
386 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
387 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
388 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
389 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
390 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
391 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
392 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
393 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
394 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
397 #define IS_SDVOB(reg) (reg == SDVOB)
398 #define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
400 static void psb_intel_sdvo_debug_write(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
401 const void *args, int args_len)
403 int i;
405 DRM_DEBUG_KMS("%s: W: %02X ",
406 SDVO_NAME(psb_intel_sdvo), cmd);
407 for (i = 0; i < args_len; i++)
408 DRM_DEBUG_KMS("%02X ", ((u8 *)args)[i]);
409 for (; i < 8; i++)
410 DRM_DEBUG_KMS(" ");
411 for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
412 if (cmd == sdvo_cmd_names[i].cmd) {
413 DRM_DEBUG_KMS("(%s)", sdvo_cmd_names[i].name);
414 break;
417 if (i == ARRAY_SIZE(sdvo_cmd_names))
418 DRM_DEBUG_KMS("(%02X)", cmd);
419 DRM_DEBUG_KMS("\n");
422 static const char *cmd_status_names[] = {
423 "Power on",
424 "Success",
425 "Not supported",
426 "Invalid arg",
427 "Pending",
428 "Target not specified",
429 "Scaling not supported"
432 static bool psb_intel_sdvo_write_cmd(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
433 const void *args, int args_len)
435 u8 buf[args_len*2 + 2], status;
436 struct i2c_msg msgs[args_len + 3];
437 int i, ret;
439 psb_intel_sdvo_debug_write(psb_intel_sdvo, cmd, args, args_len);
441 for (i = 0; i < args_len; i++) {
442 msgs[i].addr = psb_intel_sdvo->slave_addr;
443 msgs[i].flags = 0;
444 msgs[i].len = 2;
445 msgs[i].buf = buf + 2 *i;
446 buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
447 buf[2*i + 1] = ((u8*)args)[i];
449 msgs[i].addr = psb_intel_sdvo->slave_addr;
450 msgs[i].flags = 0;
451 msgs[i].len = 2;
452 msgs[i].buf = buf + 2*i;
453 buf[2*i + 0] = SDVO_I2C_OPCODE;
454 buf[2*i + 1] = cmd;
456 /* the following two are to read the response */
457 status = SDVO_I2C_CMD_STATUS;
458 msgs[i+1].addr = psb_intel_sdvo->slave_addr;
459 msgs[i+1].flags = 0;
460 msgs[i+1].len = 1;
461 msgs[i+1].buf = &status;
463 msgs[i+2].addr = psb_intel_sdvo->slave_addr;
464 msgs[i+2].flags = I2C_M_RD;
465 msgs[i+2].len = 1;
466 msgs[i+2].buf = &status;
468 ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, i+3);
469 if (ret < 0) {
470 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
471 return false;
473 if (ret != i+3) {
474 /* failure in I2C transfer */
475 DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
476 return false;
479 return true;
482 static bool psb_intel_sdvo_read_response(struct psb_intel_sdvo *psb_intel_sdvo,
483 void *response, int response_len)
485 u8 retry = 5;
486 u8 status;
487 int i;
489 DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(psb_intel_sdvo));
492 * The documentation states that all commands will be
493 * processed within 15µs, and that we need only poll
494 * the status byte a maximum of 3 times in order for the
495 * command to be complete.
497 * Check 5 times in case the hardware failed to read the docs.
499 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
500 SDVO_I2C_CMD_STATUS,
501 &status))
502 goto log_fail;
504 while ((status == SDVO_CMD_STATUS_PENDING ||
505 status == SDVO_CMD_STATUS_TARGET_NOT_SPECIFIED) && retry--) {
506 udelay(15);
507 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
508 SDVO_I2C_CMD_STATUS,
509 &status))
510 goto log_fail;
513 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
514 DRM_DEBUG_KMS("(%s)", cmd_status_names[status]);
515 else
516 DRM_DEBUG_KMS("(??? %d)", status);
518 if (status != SDVO_CMD_STATUS_SUCCESS)
519 goto log_fail;
521 /* Read the command response */
522 for (i = 0; i < response_len; i++) {
523 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
524 SDVO_I2C_RETURN_0 + i,
525 &((u8 *)response)[i]))
526 goto log_fail;
527 DRM_DEBUG_KMS(" %02X", ((u8 *)response)[i]);
529 DRM_DEBUG_KMS("\n");
530 return true;
532 log_fail:
533 DRM_DEBUG_KMS("... failed\n");
534 return false;
537 static int psb_intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
539 if (mode->clock >= 100000)
540 return 1;
541 else if (mode->clock >= 50000)
542 return 2;
543 else
544 return 4;
547 static bool psb_intel_sdvo_set_control_bus_switch(struct psb_intel_sdvo *psb_intel_sdvo,
548 u8 ddc_bus)
550 /* This must be the immediately preceding write before the i2c xfer */
551 return psb_intel_sdvo_write_cmd(psb_intel_sdvo,
552 SDVO_CMD_SET_CONTROL_BUS_SWITCH,
553 &ddc_bus, 1);
556 static bool psb_intel_sdvo_set_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, const void *data, int len)
558 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, data, len))
559 return false;
561 return psb_intel_sdvo_read_response(psb_intel_sdvo, NULL, 0);
564 static bool
565 psb_intel_sdvo_get_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, void *value, int len)
567 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, NULL, 0))
568 return false;
570 return psb_intel_sdvo_read_response(psb_intel_sdvo, value, len);
573 static bool psb_intel_sdvo_set_target_input(struct psb_intel_sdvo *psb_intel_sdvo)
575 struct psb_intel_sdvo_set_target_input_args targets = {0};
576 return psb_intel_sdvo_set_value(psb_intel_sdvo,
577 SDVO_CMD_SET_TARGET_INPUT,
578 &targets, sizeof(targets));
582 * Return whether each input is trained.
584 * This function is making an assumption about the layout of the response,
585 * which should be checked against the docs.
587 static bool psb_intel_sdvo_get_trained_inputs(struct psb_intel_sdvo *psb_intel_sdvo, bool *input_1, bool *input_2)
589 struct psb_intel_sdvo_get_trained_inputs_response response;
591 BUILD_BUG_ON(sizeof(response) != 1);
592 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
593 &response, sizeof(response)))
594 return false;
596 *input_1 = response.input0_trained;
597 *input_2 = response.input1_trained;
598 return true;
601 static bool psb_intel_sdvo_set_active_outputs(struct psb_intel_sdvo *psb_intel_sdvo,
602 u16 outputs)
604 return psb_intel_sdvo_set_value(psb_intel_sdvo,
605 SDVO_CMD_SET_ACTIVE_OUTPUTS,
606 &outputs, sizeof(outputs));
609 static bool psb_intel_sdvo_set_encoder_power_state(struct psb_intel_sdvo *psb_intel_sdvo,
610 int mode)
612 u8 state = SDVO_ENCODER_STATE_ON;
614 switch (mode) {
615 case DRM_MODE_DPMS_ON:
616 state = SDVO_ENCODER_STATE_ON;
617 break;
618 case DRM_MODE_DPMS_STANDBY:
619 state = SDVO_ENCODER_STATE_STANDBY;
620 break;
621 case DRM_MODE_DPMS_SUSPEND:
622 state = SDVO_ENCODER_STATE_SUSPEND;
623 break;
624 case DRM_MODE_DPMS_OFF:
625 state = SDVO_ENCODER_STATE_OFF;
626 break;
629 return psb_intel_sdvo_set_value(psb_intel_sdvo,
630 SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
633 static bool psb_intel_sdvo_get_input_pixel_clock_range(struct psb_intel_sdvo *psb_intel_sdvo,
634 int *clock_min,
635 int *clock_max)
637 struct psb_intel_sdvo_pixel_clock_range clocks;
639 BUILD_BUG_ON(sizeof(clocks) != 4);
640 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
641 SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
642 &clocks, sizeof(clocks)))
643 return false;
645 /* Convert the values from units of 10 kHz to kHz. */
646 *clock_min = clocks.min * 10;
647 *clock_max = clocks.max * 10;
648 return true;
651 static bool psb_intel_sdvo_set_target_output(struct psb_intel_sdvo *psb_intel_sdvo,
652 u16 outputs)
654 return psb_intel_sdvo_set_value(psb_intel_sdvo,
655 SDVO_CMD_SET_TARGET_OUTPUT,
656 &outputs, sizeof(outputs));
659 static bool psb_intel_sdvo_set_timing(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
660 struct psb_intel_sdvo_dtd *dtd)
662 return psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
663 psb_intel_sdvo_set_value(psb_intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
666 static bool psb_intel_sdvo_set_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
667 struct psb_intel_sdvo_dtd *dtd)
669 return psb_intel_sdvo_set_timing(psb_intel_sdvo,
670 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
673 static bool psb_intel_sdvo_set_output_timing(struct psb_intel_sdvo *psb_intel_sdvo,
674 struct psb_intel_sdvo_dtd *dtd)
676 return psb_intel_sdvo_set_timing(psb_intel_sdvo,
677 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
680 static bool
681 psb_intel_sdvo_create_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
682 uint16_t clock,
683 uint16_t width,
684 uint16_t height)
686 struct psb_intel_sdvo_preferred_input_timing_args args;
688 memset(&args, 0, sizeof(args));
689 args.clock = clock;
690 args.width = width;
691 args.height = height;
692 args.interlace = 0;
694 if (psb_intel_sdvo->is_lvds &&
695 (psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
696 psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
697 args.scaled = 1;
699 return psb_intel_sdvo_set_value(psb_intel_sdvo,
700 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
701 &args, sizeof(args));
704 static bool psb_intel_sdvo_get_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
705 struct psb_intel_sdvo_dtd *dtd)
707 BUILD_BUG_ON(sizeof(dtd->part1) != 8);
708 BUILD_BUG_ON(sizeof(dtd->part2) != 8);
709 return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
710 &dtd->part1, sizeof(dtd->part1)) &&
711 psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
712 &dtd->part2, sizeof(dtd->part2));
715 static bool psb_intel_sdvo_set_clock_rate_mult(struct psb_intel_sdvo *psb_intel_sdvo, u8 val)
717 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
720 static void psb_intel_sdvo_get_dtd_from_mode(struct psb_intel_sdvo_dtd *dtd,
721 const struct drm_display_mode *mode)
723 uint16_t width, height;
724 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
725 uint16_t h_sync_offset, v_sync_offset;
727 width = mode->crtc_hdisplay;
728 height = mode->crtc_vdisplay;
730 /* do some mode translations */
731 h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
732 h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
734 v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
735 v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
737 h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
738 v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
740 dtd->part1.clock = mode->clock / 10;
741 dtd->part1.h_active = width & 0xff;
742 dtd->part1.h_blank = h_blank_len & 0xff;
743 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
744 ((h_blank_len >> 8) & 0xf);
745 dtd->part1.v_active = height & 0xff;
746 dtd->part1.v_blank = v_blank_len & 0xff;
747 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
748 ((v_blank_len >> 8) & 0xf);
750 dtd->part2.h_sync_off = h_sync_offset & 0xff;
751 dtd->part2.h_sync_width = h_sync_len & 0xff;
752 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
753 (v_sync_len & 0xf);
754 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
755 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
756 ((v_sync_len & 0x30) >> 4);
758 dtd->part2.dtd_flags = 0x18;
759 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
760 dtd->part2.dtd_flags |= 0x2;
761 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
762 dtd->part2.dtd_flags |= 0x4;
764 dtd->part2.sdvo_flags = 0;
765 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
766 dtd->part2.reserved = 0;
769 static void psb_intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
770 const struct psb_intel_sdvo_dtd *dtd)
772 mode->hdisplay = dtd->part1.h_active;
773 mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
774 mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
775 mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
776 mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
777 mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
778 mode->htotal = mode->hdisplay + dtd->part1.h_blank;
779 mode->htotal += (dtd->part1.h_high & 0xf) << 8;
781 mode->vdisplay = dtd->part1.v_active;
782 mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
783 mode->vsync_start = mode->vdisplay;
784 mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
785 mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
786 mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
787 mode->vsync_end = mode->vsync_start +
788 (dtd->part2.v_sync_off_width & 0xf);
789 mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
790 mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
791 mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
793 mode->clock = dtd->part1.clock * 10;
795 mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
796 if (dtd->part2.dtd_flags & 0x2)
797 mode->flags |= DRM_MODE_FLAG_PHSYNC;
798 if (dtd->part2.dtd_flags & 0x4)
799 mode->flags |= DRM_MODE_FLAG_PVSYNC;
802 static bool psb_intel_sdvo_check_supp_encode(struct psb_intel_sdvo *psb_intel_sdvo)
804 struct psb_intel_sdvo_encode encode;
806 BUILD_BUG_ON(sizeof(encode) != 2);
807 return psb_intel_sdvo_get_value(psb_intel_sdvo,
808 SDVO_CMD_GET_SUPP_ENCODE,
809 &encode, sizeof(encode));
812 static bool psb_intel_sdvo_set_encode(struct psb_intel_sdvo *psb_intel_sdvo,
813 uint8_t mode)
815 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
818 static bool psb_intel_sdvo_set_colorimetry(struct psb_intel_sdvo *psb_intel_sdvo,
819 uint8_t mode)
821 return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
824 #if 0
825 static void psb_intel_sdvo_dump_hdmi_buf(struct psb_intel_sdvo *psb_intel_sdvo)
827 int i, j;
828 uint8_t set_buf_index[2];
829 uint8_t av_split;
830 uint8_t buf_size;
831 uint8_t buf[48];
832 uint8_t *pos;
834 psb_intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
836 for (i = 0; i <= av_split; i++) {
837 set_buf_index[0] = i; set_buf_index[1] = 0;
838 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
839 set_buf_index, 2);
840 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
841 psb_intel_sdvo_read_response(encoder, &buf_size, 1);
843 pos = buf;
844 for (j = 0; j <= buf_size; j += 8) {
845 psb_intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
846 NULL, 0);
847 psb_intel_sdvo_read_response(encoder, pos, 8);
848 pos += 8;
852 #endif
854 static bool psb_intel_sdvo_set_avi_infoframe(struct psb_intel_sdvo *psb_intel_sdvo)
856 DRM_INFO("HDMI is not supported yet");
858 return false;
859 #if 0
860 struct dip_infoframe avi_if = {
861 .type = DIP_TYPE_AVI,
862 .ver = DIP_VERSION_AVI,
863 .len = DIP_LEN_AVI,
865 uint8_t tx_rate = SDVO_HBUF_TX_VSYNC;
866 uint8_t set_buf_index[2] = { 1, 0 };
867 uint64_t *data = (uint64_t *)&avi_if;
868 unsigned i;
870 intel_dip_infoframe_csum(&avi_if);
872 if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
873 SDVO_CMD_SET_HBUF_INDEX,
874 set_buf_index, 2))
875 return false;
877 for (i = 0; i < sizeof(avi_if); i += 8) {
878 if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
879 SDVO_CMD_SET_HBUF_DATA,
880 data, 8))
881 return false;
882 data++;
885 return psb_intel_sdvo_set_value(psb_intel_sdvo,
886 SDVO_CMD_SET_HBUF_TXRATE,
887 &tx_rate, 1);
888 #endif
891 static bool psb_intel_sdvo_set_tv_format(struct psb_intel_sdvo *psb_intel_sdvo)
893 struct psb_intel_sdvo_tv_format format;
894 uint32_t format_map;
896 format_map = 1 << psb_intel_sdvo->tv_format_index;
897 memset(&format, 0, sizeof(format));
898 memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
900 BUILD_BUG_ON(sizeof(format) != 6);
901 return psb_intel_sdvo_set_value(psb_intel_sdvo,
902 SDVO_CMD_SET_TV_FORMAT,
903 &format, sizeof(format));
906 static bool
907 psb_intel_sdvo_set_output_timings_from_mode(struct psb_intel_sdvo *psb_intel_sdvo,
908 const struct drm_display_mode *mode)
910 struct psb_intel_sdvo_dtd output_dtd;
912 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
913 psb_intel_sdvo->attached_output))
914 return false;
916 psb_intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
917 if (!psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &output_dtd))
918 return false;
920 return true;
923 static bool
924 psb_intel_sdvo_set_input_timings_for_mode(struct psb_intel_sdvo *psb_intel_sdvo,
925 const struct drm_display_mode *mode,
926 struct drm_display_mode *adjusted_mode)
928 /* Reset the input timing to the screen. Assume always input 0. */
929 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
930 return false;
932 if (!psb_intel_sdvo_create_preferred_input_timing(psb_intel_sdvo,
933 mode->clock / 10,
934 mode->hdisplay,
935 mode->vdisplay))
936 return false;
938 if (!psb_intel_sdvo_get_preferred_input_timing(psb_intel_sdvo,
939 &psb_intel_sdvo->input_dtd))
940 return false;
942 psb_intel_sdvo_get_mode_from_dtd(adjusted_mode, &psb_intel_sdvo->input_dtd);
944 drm_mode_set_crtcinfo(adjusted_mode, 0);
945 return true;
948 static bool psb_intel_sdvo_mode_fixup(struct drm_encoder *encoder,
949 const struct drm_display_mode *mode,
950 struct drm_display_mode *adjusted_mode)
952 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
953 int multiplier;
955 /* We need to construct preferred input timings based on our
956 * output timings. To do that, we have to set the output
957 * timings, even though this isn't really the right place in
958 * the sequence to do it. Oh well.
960 if (psb_intel_sdvo->is_tv) {
961 if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo, mode))
962 return false;
964 (void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
965 mode,
966 adjusted_mode);
967 } else if (psb_intel_sdvo->is_lvds) {
968 if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo,
969 psb_intel_sdvo->sdvo_lvds_fixed_mode))
970 return false;
972 (void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
973 mode,
974 adjusted_mode);
977 /* Make the CRTC code factor in the SDVO pixel multiplier. The
978 * SDVO device will factor out the multiplier during mode_set.
980 multiplier = psb_intel_sdvo_get_pixel_multiplier(adjusted_mode);
981 psb_intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
983 return true;
986 static void psb_intel_sdvo_mode_set(struct drm_encoder *encoder,
987 struct drm_display_mode *mode,
988 struct drm_display_mode *adjusted_mode)
990 struct drm_device *dev = encoder->dev;
991 struct drm_crtc *crtc = encoder->crtc;
992 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
993 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
994 u32 sdvox;
995 struct psb_intel_sdvo_in_out_map in_out;
996 struct psb_intel_sdvo_dtd input_dtd;
997 int pixel_multiplier = psb_intel_mode_get_pixel_multiplier(adjusted_mode);
998 int rate;
999 int need_aux = IS_MRST(dev) ? 1 : 0;
1001 if (!mode)
1002 return;
1004 /* First, set the input mapping for the first input to our controlled
1005 * output. This is only correct if we're a single-input device, in
1006 * which case the first input is the output from the appropriate SDVO
1007 * channel on the motherboard. In a two-input device, the first input
1008 * will be SDVOB and the second SDVOC.
1010 in_out.in0 = psb_intel_sdvo->attached_output;
1011 in_out.in1 = 0;
1013 psb_intel_sdvo_set_value(psb_intel_sdvo,
1014 SDVO_CMD_SET_IN_OUT_MAP,
1015 &in_out, sizeof(in_out));
1017 /* Set the output timings to the screen */
1018 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1019 psb_intel_sdvo->attached_output))
1020 return;
1022 /* We have tried to get input timing in mode_fixup, and filled into
1023 * adjusted_mode.
1025 if (psb_intel_sdvo->is_tv || psb_intel_sdvo->is_lvds) {
1026 input_dtd = psb_intel_sdvo->input_dtd;
1027 } else {
1028 /* Set the output timing to the screen */
1029 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
1030 psb_intel_sdvo->attached_output))
1031 return;
1033 psb_intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
1034 (void) psb_intel_sdvo_set_output_timing(psb_intel_sdvo, &input_dtd);
1037 /* Set the input timing to the screen. Assume always input 0. */
1038 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
1039 return;
1041 if (psb_intel_sdvo->has_hdmi_monitor) {
1042 psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_HDMI);
1043 psb_intel_sdvo_set_colorimetry(psb_intel_sdvo,
1044 SDVO_COLORIMETRY_RGB256);
1045 psb_intel_sdvo_set_avi_infoframe(psb_intel_sdvo);
1046 } else
1047 psb_intel_sdvo_set_encode(psb_intel_sdvo, SDVO_ENCODE_DVI);
1049 if (psb_intel_sdvo->is_tv &&
1050 !psb_intel_sdvo_set_tv_format(psb_intel_sdvo))
1051 return;
1053 (void) psb_intel_sdvo_set_input_timing(psb_intel_sdvo, &input_dtd);
1055 switch (pixel_multiplier) {
1056 default:
1057 case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1058 case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1059 case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
1061 if (!psb_intel_sdvo_set_clock_rate_mult(psb_intel_sdvo, rate))
1062 return;
1064 /* Set the SDVO control regs. */
1065 if (need_aux)
1066 sdvox = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1067 else
1068 sdvox = REG_READ(psb_intel_sdvo->sdvo_reg);
1070 switch (psb_intel_sdvo->sdvo_reg) {
1071 case SDVOB:
1072 sdvox &= SDVOB_PRESERVE_MASK;
1073 break;
1074 case SDVOC:
1075 sdvox &= SDVOC_PRESERVE_MASK;
1076 break;
1078 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1080 if (gma_crtc->pipe == 1)
1081 sdvox |= SDVO_PIPE_B_SELECT;
1082 if (psb_intel_sdvo->has_hdmi_audio)
1083 sdvox |= SDVO_AUDIO_ENABLE;
1085 /* FIXME: Check if this is needed for PSB
1086 sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1089 if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL)
1090 sdvox |= SDVO_STALL_SELECT;
1091 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, sdvox);
1094 static void psb_intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1096 struct drm_device *dev = encoder->dev;
1097 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1098 u32 temp;
1099 int i;
1100 int need_aux = IS_MRST(dev) ? 1 : 0;
1102 switch (mode) {
1103 case DRM_MODE_DPMS_ON:
1104 DRM_DEBUG("DPMS_ON");
1105 break;
1106 case DRM_MODE_DPMS_OFF:
1107 DRM_DEBUG("DPMS_OFF");
1108 break;
1109 default:
1110 DRM_DEBUG("DPMS: %d", mode);
1113 if (mode != DRM_MODE_DPMS_ON) {
1114 psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, 0);
1115 if (0)
1116 psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1118 if (mode == DRM_MODE_DPMS_OFF) {
1119 if (need_aux)
1120 temp = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1121 else
1122 temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1124 if ((temp & SDVO_ENABLE) != 0) {
1125 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp & ~SDVO_ENABLE);
1128 } else {
1129 bool input1, input2;
1130 u8 status;
1132 if (need_aux)
1133 temp = REG_READ_AUX(psb_intel_sdvo->sdvo_reg);
1134 else
1135 temp = REG_READ(psb_intel_sdvo->sdvo_reg);
1137 if ((temp & SDVO_ENABLE) == 0)
1138 psb_intel_sdvo_write_sdvox(psb_intel_sdvo, temp | SDVO_ENABLE);
1140 for (i = 0; i < 2; i++)
1141 gma_wait_for_vblank(dev);
1143 status = psb_intel_sdvo_get_trained_inputs(psb_intel_sdvo, &input1, &input2);
1144 /* Warn if the device reported failure to sync.
1145 * A lot of SDVO devices fail to notify of sync, but it's
1146 * a given it the status is a success, we succeeded.
1148 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
1149 DRM_DEBUG_KMS("First %s output reported failure to "
1150 "sync\n", SDVO_NAME(psb_intel_sdvo));
1153 if (0)
1154 psb_intel_sdvo_set_encoder_power_state(psb_intel_sdvo, mode);
1155 psb_intel_sdvo_set_active_outputs(psb_intel_sdvo, psb_intel_sdvo->attached_output);
1157 return;
1160 static int psb_intel_sdvo_mode_valid(struct drm_connector *connector,
1161 struct drm_display_mode *mode)
1163 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1165 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1166 return MODE_NO_DBLESCAN;
1168 if (psb_intel_sdvo->pixel_clock_min > mode->clock)
1169 return MODE_CLOCK_LOW;
1171 if (psb_intel_sdvo->pixel_clock_max < mode->clock)
1172 return MODE_CLOCK_HIGH;
1174 if (psb_intel_sdvo->is_lvds) {
1175 if (mode->hdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
1176 return MODE_PANEL;
1178 if (mode->vdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
1179 return MODE_PANEL;
1182 return MODE_OK;
1185 static bool psb_intel_sdvo_get_capabilities(struct psb_intel_sdvo *psb_intel_sdvo, struct psb_intel_sdvo_caps *caps)
1187 BUILD_BUG_ON(sizeof(*caps) != 8);
1188 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
1189 SDVO_CMD_GET_DEVICE_CAPS,
1190 caps, sizeof(*caps)))
1191 return false;
1193 DRM_DEBUG_KMS("SDVO capabilities:\n"
1194 " vendor_id: %d\n"
1195 " device_id: %d\n"
1196 " device_rev_id: %d\n"
1197 " sdvo_version_major: %d\n"
1198 " sdvo_version_minor: %d\n"
1199 " sdvo_inputs_mask: %d\n"
1200 " smooth_scaling: %d\n"
1201 " sharp_scaling: %d\n"
1202 " up_scaling: %d\n"
1203 " down_scaling: %d\n"
1204 " stall_support: %d\n"
1205 " output_flags: %d\n",
1206 caps->vendor_id,
1207 caps->device_id,
1208 caps->device_rev_id,
1209 caps->sdvo_version_major,
1210 caps->sdvo_version_minor,
1211 caps->sdvo_inputs_mask,
1212 caps->smooth_scaling,
1213 caps->sharp_scaling,
1214 caps->up_scaling,
1215 caps->down_scaling,
1216 caps->stall_support,
1217 caps->output_flags);
1219 return true;
1222 /* No use! */
1223 #if 0
1224 struct drm_connector* psb_intel_sdvo_find(struct drm_device *dev, int sdvoB)
1226 struct drm_connector *connector = NULL;
1227 struct psb_intel_sdvo *iout = NULL;
1228 struct psb_intel_sdvo *sdvo;
1230 /* find the sdvo connector */
1231 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1232 iout = to_psb_intel_sdvo(connector);
1234 if (iout->type != INTEL_OUTPUT_SDVO)
1235 continue;
1237 sdvo = iout->dev_priv;
1239 if (sdvo->sdvo_reg == SDVOB && sdvoB)
1240 return connector;
1242 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
1243 return connector;
1247 return NULL;
1250 int psb_intel_sdvo_supports_hotplug(struct drm_connector *connector)
1252 u8 response[2];
1253 u8 status;
1254 struct psb_intel_sdvo *psb_intel_sdvo;
1255 DRM_DEBUG_KMS("\n");
1257 if (!connector)
1258 return 0;
1260 psb_intel_sdvo = to_psb_intel_sdvo(connector);
1262 return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1263 &response, 2) && response[0];
1266 void psb_intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1268 u8 response[2];
1269 u8 status;
1270 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(connector);
1272 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1273 psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1275 if (on) {
1276 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
1277 status = psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1279 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1280 } else {
1281 response[0] = 0;
1282 response[1] = 0;
1283 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
1286 psb_intel_sdvo_write_cmd(psb_intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
1287 psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2);
1289 #endif
1291 static bool
1292 psb_intel_sdvo_multifunc_encoder(struct psb_intel_sdvo *psb_intel_sdvo)
1294 /* Is there more than one type of output? */
1295 int caps = psb_intel_sdvo->caps.output_flags & 0xf;
1296 return caps & -caps;
1299 static struct edid *
1300 psb_intel_sdvo_get_edid(struct drm_connector *connector)
1302 struct psb_intel_sdvo *sdvo = intel_attached_sdvo(connector);
1303 return drm_get_edid(connector, &sdvo->ddc);
1306 /* Mac mini hack -- use the same DDC as the analog connector */
1307 static struct edid *
1308 psb_intel_sdvo_get_analog_edid(struct drm_connector *connector)
1310 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1312 return drm_get_edid(connector,
1313 &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
1316 static enum drm_connector_status
1317 psb_intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
1319 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1320 enum drm_connector_status status;
1321 struct edid *edid;
1323 edid = psb_intel_sdvo_get_edid(connector);
1325 if (edid == NULL && psb_intel_sdvo_multifunc_encoder(psb_intel_sdvo)) {
1326 u8 ddc, saved_ddc = psb_intel_sdvo->ddc_bus;
1329 * Don't use the 1 as the argument of DDC bus switch to get
1330 * the EDID. It is used for SDVO SPD ROM.
1332 for (ddc = psb_intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
1333 psb_intel_sdvo->ddc_bus = ddc;
1334 edid = psb_intel_sdvo_get_edid(connector);
1335 if (edid)
1336 break;
1339 * If we found the EDID on the other bus,
1340 * assume that is the correct DDC bus.
1342 if (edid == NULL)
1343 psb_intel_sdvo->ddc_bus = saved_ddc;
1347 * When there is no edid and no monitor is connected with VGA
1348 * port, try to use the CRT ddc to read the EDID for DVI-connector.
1350 if (edid == NULL)
1351 edid = psb_intel_sdvo_get_analog_edid(connector);
1353 status = connector_status_unknown;
1354 if (edid != NULL) {
1355 /* DDC bus is shared, match EDID to connector type */
1356 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1357 status = connector_status_connected;
1358 if (psb_intel_sdvo->is_hdmi) {
1359 psb_intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1360 psb_intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
1362 } else
1363 status = connector_status_disconnected;
1364 kfree(edid);
1367 if (status == connector_status_connected) {
1368 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1369 if (psb_intel_sdvo_connector->force_audio)
1370 psb_intel_sdvo->has_hdmi_audio = psb_intel_sdvo_connector->force_audio > 0;
1373 return status;
1376 static enum drm_connector_status
1377 psb_intel_sdvo_detect(struct drm_connector *connector, bool force)
1379 uint16_t response;
1380 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1381 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1382 enum drm_connector_status ret;
1384 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1385 SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
1386 return connector_status_unknown;
1388 /* add 30ms delay when the output type might be TV */
1389 if (psb_intel_sdvo->caps.output_flags &
1390 (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_CVBS0))
1391 mdelay(30);
1393 if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2))
1394 return connector_status_unknown;
1396 DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1397 response & 0xff, response >> 8,
1398 psb_intel_sdvo_connector->output_flag);
1400 if (response == 0)
1401 return connector_status_disconnected;
1403 psb_intel_sdvo->attached_output = response;
1405 psb_intel_sdvo->has_hdmi_monitor = false;
1406 psb_intel_sdvo->has_hdmi_audio = false;
1408 if ((psb_intel_sdvo_connector->output_flag & response) == 0)
1409 ret = connector_status_disconnected;
1410 else if (IS_TMDS(psb_intel_sdvo_connector))
1411 ret = psb_intel_sdvo_hdmi_sink_detect(connector);
1412 else {
1413 struct edid *edid;
1415 /* if we have an edid check it matches the connection */
1416 edid = psb_intel_sdvo_get_edid(connector);
1417 if (edid == NULL)
1418 edid = psb_intel_sdvo_get_analog_edid(connector);
1419 if (edid != NULL) {
1420 if (edid->input & DRM_EDID_INPUT_DIGITAL)
1421 ret = connector_status_disconnected;
1422 else
1423 ret = connector_status_connected;
1424 kfree(edid);
1425 } else
1426 ret = connector_status_connected;
1429 /* May update encoder flag for like clock for SDVO TV, etc.*/
1430 if (ret == connector_status_connected) {
1431 psb_intel_sdvo->is_tv = false;
1432 psb_intel_sdvo->is_lvds = false;
1433 psb_intel_sdvo->base.needs_tv_clock = false;
1435 if (response & SDVO_TV_MASK) {
1436 psb_intel_sdvo->is_tv = true;
1437 psb_intel_sdvo->base.needs_tv_clock = true;
1439 if (response & SDVO_LVDS_MASK)
1440 psb_intel_sdvo->is_lvds = psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL;
1443 return ret;
1446 static void psb_intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1448 struct edid *edid;
1450 /* set the bus switch and get the modes */
1451 edid = psb_intel_sdvo_get_edid(connector);
1454 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1455 * link between analog and digital outputs. So, if the regular SDVO
1456 * DDC fails, check to see if the analog output is disconnected, in
1457 * which case we'll look there for the digital DDC data.
1459 if (edid == NULL)
1460 edid = psb_intel_sdvo_get_analog_edid(connector);
1462 if (edid != NULL) {
1463 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1464 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1465 bool connector_is_digital = !!IS_TMDS(psb_intel_sdvo_connector);
1467 if (connector_is_digital == monitor_is_digital) {
1468 drm_mode_connector_update_edid_property(connector, edid);
1469 drm_add_edid_modes(connector, edid);
1472 kfree(edid);
1477 * Set of SDVO TV modes.
1478 * Note! This is in reply order (see loop in get_tv_modes).
1479 * XXX: all 60Hz refresh?
1481 static const struct drm_display_mode sdvo_tv_modes[] = {
1482 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1483 416, 0, 200, 201, 232, 233, 0,
1484 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1485 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1486 416, 0, 240, 241, 272, 273, 0,
1487 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1488 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1489 496, 0, 300, 301, 332, 333, 0,
1490 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1491 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1492 736, 0, 350, 351, 382, 383, 0,
1493 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1494 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1495 736, 0, 400, 401, 432, 433, 0,
1496 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1497 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1498 736, 0, 480, 481, 512, 513, 0,
1499 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1500 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1501 800, 0, 480, 481, 512, 513, 0,
1502 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1503 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1504 800, 0, 576, 577, 608, 609, 0,
1505 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1506 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1507 816, 0, 350, 351, 382, 383, 0,
1508 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1509 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1510 816, 0, 400, 401, 432, 433, 0,
1511 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1512 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1513 816, 0, 480, 481, 512, 513, 0,
1514 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1515 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1516 816, 0, 540, 541, 572, 573, 0,
1517 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1518 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1519 816, 0, 576, 577, 608, 609, 0,
1520 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1521 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1522 864, 0, 576, 577, 608, 609, 0,
1523 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1524 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1525 896, 0, 600, 601, 632, 633, 0,
1526 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1527 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1528 928, 0, 624, 625, 656, 657, 0,
1529 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1530 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1531 1016, 0, 766, 767, 798, 799, 0,
1532 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1533 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1534 1120, 0, 768, 769, 800, 801, 0,
1535 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1536 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1537 1376, 0, 1024, 1025, 1056, 1057, 0,
1538 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1541 static void psb_intel_sdvo_get_tv_modes(struct drm_connector *connector)
1543 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1544 struct psb_intel_sdvo_sdtv_resolution_request tv_res;
1545 uint32_t reply = 0, format_map = 0;
1546 int i;
1548 /* Read the list of supported input resolutions for the selected TV
1549 * format.
1551 format_map = 1 << psb_intel_sdvo->tv_format_index;
1552 memcpy(&tv_res, &format_map,
1553 min(sizeof(format_map), sizeof(struct psb_intel_sdvo_sdtv_resolution_request)));
1555 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, psb_intel_sdvo->attached_output))
1556 return;
1558 BUILD_BUG_ON(sizeof(tv_res) != 3);
1559 if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1560 SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1561 &tv_res, sizeof(tv_res)))
1562 return;
1563 if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &reply, 3))
1564 return;
1566 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
1567 if (reply & (1 << i)) {
1568 struct drm_display_mode *nmode;
1569 nmode = drm_mode_duplicate(connector->dev,
1570 &sdvo_tv_modes[i]);
1571 if (nmode)
1572 drm_mode_probed_add(connector, nmode);
1576 static void psb_intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1578 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1579 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1580 struct drm_display_mode *newmode;
1583 * Attempt to get the mode list from DDC.
1584 * Assume that the preferred modes are
1585 * arranged in priority order.
1587 psb_intel_ddc_get_modes(connector, psb_intel_sdvo->i2c);
1588 if (list_empty(&connector->probed_modes) == false)
1589 goto end;
1591 /* Fetch modes from VBT */
1592 if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
1593 newmode = drm_mode_duplicate(connector->dev,
1594 dev_priv->sdvo_lvds_vbt_mode);
1595 if (newmode != NULL) {
1596 /* Guarantee the mode is preferred */
1597 newmode->type = (DRM_MODE_TYPE_PREFERRED |
1598 DRM_MODE_TYPE_DRIVER);
1599 drm_mode_probed_add(connector, newmode);
1603 end:
1604 list_for_each_entry(newmode, &connector->probed_modes, head) {
1605 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1606 psb_intel_sdvo->sdvo_lvds_fixed_mode =
1607 drm_mode_duplicate(connector->dev, newmode);
1609 drm_mode_set_crtcinfo(psb_intel_sdvo->sdvo_lvds_fixed_mode,
1612 psb_intel_sdvo->is_lvds = true;
1613 break;
1619 static int psb_intel_sdvo_get_modes(struct drm_connector *connector)
1621 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1623 if (IS_TV(psb_intel_sdvo_connector))
1624 psb_intel_sdvo_get_tv_modes(connector);
1625 else if (IS_LVDS(psb_intel_sdvo_connector))
1626 psb_intel_sdvo_get_lvds_modes(connector);
1627 else
1628 psb_intel_sdvo_get_ddc_modes(connector);
1630 return !list_empty(&connector->probed_modes);
1633 static void psb_intel_sdvo_destroy(struct drm_connector *connector)
1635 drm_connector_unregister(connector);
1636 drm_connector_cleanup(connector);
1637 kfree(connector);
1640 static bool psb_intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1642 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1643 struct edid *edid;
1644 bool has_audio = false;
1646 if (!psb_intel_sdvo->is_hdmi)
1647 return false;
1649 edid = psb_intel_sdvo_get_edid(connector);
1650 if (edid != NULL && edid->input & DRM_EDID_INPUT_DIGITAL)
1651 has_audio = drm_detect_monitor_audio(edid);
1653 return has_audio;
1656 static int
1657 psb_intel_sdvo_set_property(struct drm_connector *connector,
1658 struct drm_property *property,
1659 uint64_t val)
1661 struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1662 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1663 struct drm_psb_private *dev_priv = connector->dev->dev_private;
1664 uint16_t temp_value;
1665 uint8_t cmd;
1666 int ret;
1668 ret = drm_object_property_set_value(&connector->base, property, val);
1669 if (ret)
1670 return ret;
1672 if (property == dev_priv->force_audio_property) {
1673 int i = val;
1674 bool has_audio;
1676 if (i == psb_intel_sdvo_connector->force_audio)
1677 return 0;
1679 psb_intel_sdvo_connector->force_audio = i;
1681 if (i == 0)
1682 has_audio = psb_intel_sdvo_detect_hdmi_audio(connector);
1683 else
1684 has_audio = i > 0;
1686 if (has_audio == psb_intel_sdvo->has_hdmi_audio)
1687 return 0;
1689 psb_intel_sdvo->has_hdmi_audio = has_audio;
1690 goto done;
1693 if (property == dev_priv->broadcast_rgb_property) {
1694 if (val == !!psb_intel_sdvo->color_range)
1695 return 0;
1697 psb_intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
1698 goto done;
1701 #define CHECK_PROPERTY(name, NAME) \
1702 if (psb_intel_sdvo_connector->name == property) { \
1703 if (psb_intel_sdvo_connector->cur_##name == temp_value) return 0; \
1704 if (psb_intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
1705 cmd = SDVO_CMD_SET_##NAME; \
1706 psb_intel_sdvo_connector->cur_##name = temp_value; \
1707 goto set_value; \
1710 if (property == psb_intel_sdvo_connector->tv_format) {
1711 if (val >= ARRAY_SIZE(tv_format_names))
1712 return -EINVAL;
1714 if (psb_intel_sdvo->tv_format_index ==
1715 psb_intel_sdvo_connector->tv_format_supported[val])
1716 return 0;
1718 psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[val];
1719 goto done;
1720 } else if (IS_TV_OR_LVDS(psb_intel_sdvo_connector)) {
1721 temp_value = val;
1722 if (psb_intel_sdvo_connector->left == property) {
1723 drm_object_property_set_value(&connector->base,
1724 psb_intel_sdvo_connector->right, val);
1725 if (psb_intel_sdvo_connector->left_margin == temp_value)
1726 return 0;
1728 psb_intel_sdvo_connector->left_margin = temp_value;
1729 psb_intel_sdvo_connector->right_margin = temp_value;
1730 temp_value = psb_intel_sdvo_connector->max_hscan -
1731 psb_intel_sdvo_connector->left_margin;
1732 cmd = SDVO_CMD_SET_OVERSCAN_H;
1733 goto set_value;
1734 } else if (psb_intel_sdvo_connector->right == property) {
1735 drm_object_property_set_value(&connector->base,
1736 psb_intel_sdvo_connector->left, val);
1737 if (psb_intel_sdvo_connector->right_margin == temp_value)
1738 return 0;
1740 psb_intel_sdvo_connector->left_margin = temp_value;
1741 psb_intel_sdvo_connector->right_margin = temp_value;
1742 temp_value = psb_intel_sdvo_connector->max_hscan -
1743 psb_intel_sdvo_connector->left_margin;
1744 cmd = SDVO_CMD_SET_OVERSCAN_H;
1745 goto set_value;
1746 } else if (psb_intel_sdvo_connector->top == property) {
1747 drm_object_property_set_value(&connector->base,
1748 psb_intel_sdvo_connector->bottom, val);
1749 if (psb_intel_sdvo_connector->top_margin == temp_value)
1750 return 0;
1752 psb_intel_sdvo_connector->top_margin = temp_value;
1753 psb_intel_sdvo_connector->bottom_margin = temp_value;
1754 temp_value = psb_intel_sdvo_connector->max_vscan -
1755 psb_intel_sdvo_connector->top_margin;
1756 cmd = SDVO_CMD_SET_OVERSCAN_V;
1757 goto set_value;
1758 } else if (psb_intel_sdvo_connector->bottom == property) {
1759 drm_object_property_set_value(&connector->base,
1760 psb_intel_sdvo_connector->top, val);
1761 if (psb_intel_sdvo_connector->bottom_margin == temp_value)
1762 return 0;
1764 psb_intel_sdvo_connector->top_margin = temp_value;
1765 psb_intel_sdvo_connector->bottom_margin = temp_value;
1766 temp_value = psb_intel_sdvo_connector->max_vscan -
1767 psb_intel_sdvo_connector->top_margin;
1768 cmd = SDVO_CMD_SET_OVERSCAN_V;
1769 goto set_value;
1771 CHECK_PROPERTY(hpos, HPOS)
1772 CHECK_PROPERTY(vpos, VPOS)
1773 CHECK_PROPERTY(saturation, SATURATION)
1774 CHECK_PROPERTY(contrast, CONTRAST)
1775 CHECK_PROPERTY(hue, HUE)
1776 CHECK_PROPERTY(brightness, BRIGHTNESS)
1777 CHECK_PROPERTY(sharpness, SHARPNESS)
1778 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
1779 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
1780 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
1781 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
1782 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
1783 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
1786 return -EINVAL; /* unknown property */
1788 set_value:
1789 if (!psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &temp_value, 2))
1790 return -EIO;
1793 done:
1794 if (psb_intel_sdvo->base.base.crtc) {
1795 struct drm_crtc *crtc = psb_intel_sdvo->base.base.crtc;
1796 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
1797 crtc->y, crtc->primary->fb);
1800 return 0;
1801 #undef CHECK_PROPERTY
1804 static void psb_intel_sdvo_save(struct drm_connector *connector)
1806 struct drm_device *dev = connector->dev;
1807 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
1808 struct psb_intel_sdvo *sdvo = to_psb_intel_sdvo(&gma_encoder->base);
1810 sdvo->saveSDVO = REG_READ(sdvo->sdvo_reg);
1813 static void psb_intel_sdvo_restore(struct drm_connector *connector)
1815 struct drm_device *dev = connector->dev;
1816 struct drm_encoder *encoder = &gma_attached_encoder(connector)->base;
1817 struct psb_intel_sdvo *sdvo = to_psb_intel_sdvo(encoder);
1818 struct drm_crtc *crtc = encoder->crtc;
1820 REG_WRITE(sdvo->sdvo_reg, sdvo->saveSDVO);
1822 /* Force a full mode set on the crtc. We're supposed to have the
1823 mode_config lock already. */
1824 if (connector->status == connector_status_connected)
1825 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
1826 NULL);
1829 static const struct drm_encoder_helper_funcs psb_intel_sdvo_helper_funcs = {
1830 .dpms = psb_intel_sdvo_dpms,
1831 .mode_fixup = psb_intel_sdvo_mode_fixup,
1832 .prepare = gma_encoder_prepare,
1833 .mode_set = psb_intel_sdvo_mode_set,
1834 .commit = gma_encoder_commit,
1837 static const struct drm_connector_funcs psb_intel_sdvo_connector_funcs = {
1838 .dpms = drm_helper_connector_dpms,
1839 .detect = psb_intel_sdvo_detect,
1840 .fill_modes = drm_helper_probe_single_connector_modes,
1841 .set_property = psb_intel_sdvo_set_property,
1842 .destroy = psb_intel_sdvo_destroy,
1845 static const struct drm_connector_helper_funcs psb_intel_sdvo_connector_helper_funcs = {
1846 .get_modes = psb_intel_sdvo_get_modes,
1847 .mode_valid = psb_intel_sdvo_mode_valid,
1848 .best_encoder = gma_best_encoder,
1851 static void psb_intel_sdvo_enc_destroy(struct drm_encoder *encoder)
1853 struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1855 if (psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL)
1856 drm_mode_destroy(encoder->dev,
1857 psb_intel_sdvo->sdvo_lvds_fixed_mode);
1859 i2c_del_adapter(&psb_intel_sdvo->ddc);
1860 gma_encoder_destroy(encoder);
1863 static const struct drm_encoder_funcs psb_intel_sdvo_enc_funcs = {
1864 .destroy = psb_intel_sdvo_enc_destroy,
1867 static void
1868 psb_intel_sdvo_guess_ddc_bus(struct psb_intel_sdvo *sdvo)
1870 /* FIXME: At the moment, ddc_bus = 2 is the only thing that works.
1871 * We need to figure out if this is true for all available poulsbo
1872 * hardware, or if we need to fiddle with the guessing code above.
1873 * The problem might go away if we can parse sdvo mappings from bios */
1874 sdvo->ddc_bus = 2;
1876 #if 0
1877 uint16_t mask = 0;
1878 unsigned int num_bits;
1880 /* Make a mask of outputs less than or equal to our own priority in the
1881 * list.
1883 switch (sdvo->controlled_output) {
1884 case SDVO_OUTPUT_LVDS1:
1885 mask |= SDVO_OUTPUT_LVDS1;
1886 case SDVO_OUTPUT_LVDS0:
1887 mask |= SDVO_OUTPUT_LVDS0;
1888 case SDVO_OUTPUT_TMDS1:
1889 mask |= SDVO_OUTPUT_TMDS1;
1890 case SDVO_OUTPUT_TMDS0:
1891 mask |= SDVO_OUTPUT_TMDS0;
1892 case SDVO_OUTPUT_RGB1:
1893 mask |= SDVO_OUTPUT_RGB1;
1894 case SDVO_OUTPUT_RGB0:
1895 mask |= SDVO_OUTPUT_RGB0;
1896 break;
1899 /* Count bits to find what number we are in the priority list. */
1900 mask &= sdvo->caps.output_flags;
1901 num_bits = hweight16(mask);
1902 /* If more than 3 outputs, default to DDC bus 3 for now. */
1903 if (num_bits > 3)
1904 num_bits = 3;
1906 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1907 sdvo->ddc_bus = 1 << num_bits;
1908 #endif
1912 * Choose the appropriate DDC bus for control bus switch command for this
1913 * SDVO output based on the controlled output.
1915 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1916 * outputs, then LVDS outputs.
1918 static void
1919 psb_intel_sdvo_select_ddc_bus(struct drm_psb_private *dev_priv,
1920 struct psb_intel_sdvo *sdvo, u32 reg)
1922 struct sdvo_device_mapping *mapping;
1924 if (IS_SDVOB(reg))
1925 mapping = &(dev_priv->sdvo_mappings[0]);
1926 else
1927 mapping = &(dev_priv->sdvo_mappings[1]);
1929 if (mapping->initialized)
1930 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
1931 else
1932 psb_intel_sdvo_guess_ddc_bus(sdvo);
1935 static void
1936 psb_intel_sdvo_select_i2c_bus(struct drm_psb_private *dev_priv,
1937 struct psb_intel_sdvo *sdvo, u32 reg)
1939 struct sdvo_device_mapping *mapping;
1940 u8 pin, speed;
1942 if (IS_SDVOB(reg))
1943 mapping = &dev_priv->sdvo_mappings[0];
1944 else
1945 mapping = &dev_priv->sdvo_mappings[1];
1947 pin = GMBUS_PORT_DPB;
1948 speed = GMBUS_RATE_1MHZ >> 8;
1949 if (mapping->initialized) {
1950 pin = mapping->i2c_pin;
1951 speed = mapping->i2c_speed;
1954 if (pin < GMBUS_NUM_PORTS) {
1955 sdvo->i2c = &dev_priv->gmbus[pin].adapter;
1956 gma_intel_gmbus_set_speed(sdvo->i2c, speed);
1957 gma_intel_gmbus_force_bit(sdvo->i2c, true);
1958 } else
1959 sdvo->i2c = &dev_priv->gmbus[GMBUS_PORT_DPB].adapter;
1962 static bool
1963 psb_intel_sdvo_is_hdmi_connector(struct psb_intel_sdvo *psb_intel_sdvo, int device)
1965 return psb_intel_sdvo_check_supp_encode(psb_intel_sdvo);
1968 static u8
1969 psb_intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
1971 struct drm_psb_private *dev_priv = dev->dev_private;
1972 struct sdvo_device_mapping *my_mapping, *other_mapping;
1974 if (IS_SDVOB(sdvo_reg)) {
1975 my_mapping = &dev_priv->sdvo_mappings[0];
1976 other_mapping = &dev_priv->sdvo_mappings[1];
1977 } else {
1978 my_mapping = &dev_priv->sdvo_mappings[1];
1979 other_mapping = &dev_priv->sdvo_mappings[0];
1982 /* If the BIOS described our SDVO device, take advantage of it. */
1983 if (my_mapping->slave_addr)
1984 return my_mapping->slave_addr;
1986 /* If the BIOS only described a different SDVO device, use the
1987 * address that it isn't using.
1989 if (other_mapping->slave_addr) {
1990 if (other_mapping->slave_addr == 0x70)
1991 return 0x72;
1992 else
1993 return 0x70;
1996 /* No SDVO device info is found for another DVO port,
1997 * so use mapping assumption we had before BIOS parsing.
1999 if (IS_SDVOB(sdvo_reg))
2000 return 0x70;
2001 else
2002 return 0x72;
2005 static void
2006 psb_intel_sdvo_connector_init(struct psb_intel_sdvo_connector *connector,
2007 struct psb_intel_sdvo *encoder)
2009 drm_connector_init(encoder->base.base.dev,
2010 &connector->base.base,
2011 &psb_intel_sdvo_connector_funcs,
2012 connector->base.base.connector_type);
2014 drm_connector_helper_add(&connector->base.base,
2015 &psb_intel_sdvo_connector_helper_funcs);
2017 connector->base.base.interlace_allowed = 0;
2018 connector->base.base.doublescan_allowed = 0;
2019 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
2021 connector->base.save = psb_intel_sdvo_save;
2022 connector->base.restore = psb_intel_sdvo_restore;
2024 gma_connector_attach_encoder(&connector->base, &encoder->base);
2025 drm_connector_register(&connector->base.base);
2028 static void
2029 psb_intel_sdvo_add_hdmi_properties(struct psb_intel_sdvo_connector *connector)
2031 /* FIXME: We don't support HDMI at the moment
2032 struct drm_device *dev = connector->base.base.dev;
2034 intel_attach_force_audio_property(&connector->base.base);
2035 intel_attach_broadcast_rgb_property(&connector->base.base);
2039 static bool
2040 psb_intel_sdvo_dvi_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2042 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2043 struct drm_connector *connector;
2044 struct gma_connector *intel_connector;
2045 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2047 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2048 if (!psb_intel_sdvo_connector)
2049 return false;
2051 if (device == 0) {
2052 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
2053 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
2054 } else if (device == 1) {
2055 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
2056 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
2059 intel_connector = &psb_intel_sdvo_connector->base;
2060 connector = &intel_connector->base;
2061 // connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
2062 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2063 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2065 if (psb_intel_sdvo_is_hdmi_connector(psb_intel_sdvo, device)) {
2066 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2067 psb_intel_sdvo->is_hdmi = true;
2069 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2070 (1 << INTEL_ANALOG_CLONE_BIT));
2072 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2073 if (psb_intel_sdvo->is_hdmi)
2074 psb_intel_sdvo_add_hdmi_properties(psb_intel_sdvo_connector);
2076 return true;
2079 static bool
2080 psb_intel_sdvo_tv_init(struct psb_intel_sdvo *psb_intel_sdvo, int type)
2082 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2083 struct drm_connector *connector;
2084 struct gma_connector *intel_connector;
2085 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2087 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2088 if (!psb_intel_sdvo_connector)
2089 return false;
2091 intel_connector = &psb_intel_sdvo_connector->base;
2092 connector = &intel_connector->base;
2093 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2094 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2096 psb_intel_sdvo->controlled_output |= type;
2097 psb_intel_sdvo_connector->output_flag = type;
2099 psb_intel_sdvo->is_tv = true;
2100 psb_intel_sdvo->base.needs_tv_clock = true;
2101 psb_intel_sdvo->base.clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
2103 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2105 if (!psb_intel_sdvo_tv_create_property(psb_intel_sdvo, psb_intel_sdvo_connector, type))
2106 goto err;
2108 if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2109 goto err;
2111 return true;
2113 err:
2114 psb_intel_sdvo_destroy(connector);
2115 return false;
2118 static bool
2119 psb_intel_sdvo_analog_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2121 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2122 struct drm_connector *connector;
2123 struct gma_connector *intel_connector;
2124 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2126 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2127 if (!psb_intel_sdvo_connector)
2128 return false;
2130 intel_connector = &psb_intel_sdvo_connector->base;
2131 connector = &intel_connector->base;
2132 connector->polled = DRM_CONNECTOR_POLL_CONNECT;
2133 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2134 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2136 if (device == 0) {
2137 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2138 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2139 } else if (device == 1) {
2140 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2141 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2144 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2145 (1 << INTEL_ANALOG_CLONE_BIT));
2147 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector,
2148 psb_intel_sdvo);
2149 return true;
2152 static bool
2153 psb_intel_sdvo_lvds_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2155 struct drm_encoder *encoder = &psb_intel_sdvo->base.base;
2156 struct drm_connector *connector;
2157 struct gma_connector *intel_connector;
2158 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector;
2160 psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2161 if (!psb_intel_sdvo_connector)
2162 return false;
2164 intel_connector = &psb_intel_sdvo_connector->base;
2165 connector = &intel_connector->base;
2166 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2167 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2169 if (device == 0) {
2170 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2171 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2172 } else if (device == 1) {
2173 psb_intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2174 psb_intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2177 psb_intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
2178 (1 << INTEL_SDVO_LVDS_CLONE_BIT));
2180 psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2181 if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2182 goto err;
2184 return true;
2186 err:
2187 psb_intel_sdvo_destroy(connector);
2188 return false;
2191 static bool
2192 psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags)
2194 psb_intel_sdvo->is_tv = false;
2195 psb_intel_sdvo->base.needs_tv_clock = false;
2196 psb_intel_sdvo->is_lvds = false;
2198 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
2200 if (flags & SDVO_OUTPUT_TMDS0)
2201 if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 0))
2202 return false;
2204 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2205 if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 1))
2206 return false;
2208 /* TV has no XXX1 function block */
2209 if (flags & SDVO_OUTPUT_SVID0)
2210 if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_SVID0))
2211 return false;
2213 if (flags & SDVO_OUTPUT_CVBS0)
2214 if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_CVBS0))
2215 return false;
2217 if (flags & SDVO_OUTPUT_RGB0)
2218 if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 0))
2219 return false;
2221 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2222 if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 1))
2223 return false;
2225 if (flags & SDVO_OUTPUT_LVDS0)
2226 if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 0))
2227 return false;
2229 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2230 if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 1))
2231 return false;
2233 if ((flags & SDVO_OUTPUT_MASK) == 0) {
2234 unsigned char bytes[2];
2236 psb_intel_sdvo->controlled_output = 0;
2237 memcpy(bytes, &psb_intel_sdvo->caps.output_flags, 2);
2238 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2239 SDVO_NAME(psb_intel_sdvo),
2240 bytes[0], bytes[1]);
2241 return false;
2243 psb_intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
2245 return true;
2248 static bool psb_intel_sdvo_tv_create_property(struct psb_intel_sdvo *psb_intel_sdvo,
2249 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2250 int type)
2252 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2253 struct psb_intel_sdvo_tv_format format;
2254 uint32_t format_map, i;
2256 if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, type))
2257 return false;
2259 BUILD_BUG_ON(sizeof(format) != 6);
2260 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2261 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2262 &format, sizeof(format)))
2263 return false;
2265 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
2267 if (format_map == 0)
2268 return false;
2270 psb_intel_sdvo_connector->format_supported_num = 0;
2271 for (i = 0 ; i < ARRAY_SIZE(tv_format_names); i++)
2272 if (format_map & (1 << i))
2273 psb_intel_sdvo_connector->tv_format_supported[psb_intel_sdvo_connector->format_supported_num++] = i;
2276 psb_intel_sdvo_connector->tv_format =
2277 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2278 "mode", psb_intel_sdvo_connector->format_supported_num);
2279 if (!psb_intel_sdvo_connector->tv_format)
2280 return false;
2282 for (i = 0; i < psb_intel_sdvo_connector->format_supported_num; i++)
2283 drm_property_add_enum(
2284 psb_intel_sdvo_connector->tv_format, i,
2285 i, tv_format_names[psb_intel_sdvo_connector->tv_format_supported[i]]);
2287 psb_intel_sdvo->tv_format_index = psb_intel_sdvo_connector->tv_format_supported[0];
2288 drm_object_attach_property(&psb_intel_sdvo_connector->base.base.base,
2289 psb_intel_sdvo_connector->tv_format, 0);
2290 return true;
2294 #define ENHANCEMENT(name, NAME) do { \
2295 if (enhancements.name) { \
2296 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2297 !psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2298 return false; \
2299 psb_intel_sdvo_connector->max_##name = data_value[0]; \
2300 psb_intel_sdvo_connector->cur_##name = response; \
2301 psb_intel_sdvo_connector->name = \
2302 drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
2303 if (!psb_intel_sdvo_connector->name) return false; \
2304 drm_object_attach_property(&connector->base, \
2305 psb_intel_sdvo_connector->name, \
2306 psb_intel_sdvo_connector->cur_##name); \
2307 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2308 data_value[0], data_value[1], response); \
2310 } while(0)
2312 static bool
2313 psb_intel_sdvo_create_enhance_property_tv(struct psb_intel_sdvo *psb_intel_sdvo,
2314 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2315 struct psb_intel_sdvo_enhancements_reply enhancements)
2317 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2318 struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2319 uint16_t response, data_value[2];
2321 /* when horizontal overscan is supported, Add the left/right property */
2322 if (enhancements.overscan_h) {
2323 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2324 SDVO_CMD_GET_MAX_OVERSCAN_H,
2325 &data_value, 4))
2326 return false;
2328 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2329 SDVO_CMD_GET_OVERSCAN_H,
2330 &response, 2))
2331 return false;
2333 psb_intel_sdvo_connector->max_hscan = data_value[0];
2334 psb_intel_sdvo_connector->left_margin = data_value[0] - response;
2335 psb_intel_sdvo_connector->right_margin = psb_intel_sdvo_connector->left_margin;
2336 psb_intel_sdvo_connector->left =
2337 drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
2338 if (!psb_intel_sdvo_connector->left)
2339 return false;
2341 drm_object_attach_property(&connector->base,
2342 psb_intel_sdvo_connector->left,
2343 psb_intel_sdvo_connector->left_margin);
2345 psb_intel_sdvo_connector->right =
2346 drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
2347 if (!psb_intel_sdvo_connector->right)
2348 return false;
2350 drm_object_attach_property(&connector->base,
2351 psb_intel_sdvo_connector->right,
2352 psb_intel_sdvo_connector->right_margin);
2353 DRM_DEBUG_KMS("h_overscan: max %d, "
2354 "default %d, current %d\n",
2355 data_value[0], data_value[1], response);
2358 if (enhancements.overscan_v) {
2359 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2360 SDVO_CMD_GET_MAX_OVERSCAN_V,
2361 &data_value, 4))
2362 return false;
2364 if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2365 SDVO_CMD_GET_OVERSCAN_V,
2366 &response, 2))
2367 return false;
2369 psb_intel_sdvo_connector->max_vscan = data_value[0];
2370 psb_intel_sdvo_connector->top_margin = data_value[0] - response;
2371 psb_intel_sdvo_connector->bottom_margin = psb_intel_sdvo_connector->top_margin;
2372 psb_intel_sdvo_connector->top =
2373 drm_property_create_range(dev, 0, "top_margin", 0, data_value[0]);
2374 if (!psb_intel_sdvo_connector->top)
2375 return false;
2377 drm_object_attach_property(&connector->base,
2378 psb_intel_sdvo_connector->top,
2379 psb_intel_sdvo_connector->top_margin);
2381 psb_intel_sdvo_connector->bottom =
2382 drm_property_create_range(dev, 0, "bottom_margin", 0, data_value[0]);
2383 if (!psb_intel_sdvo_connector->bottom)
2384 return false;
2386 drm_object_attach_property(&connector->base,
2387 psb_intel_sdvo_connector->bottom,
2388 psb_intel_sdvo_connector->bottom_margin);
2389 DRM_DEBUG_KMS("v_overscan: max %d, "
2390 "default %d, current %d\n",
2391 data_value[0], data_value[1], response);
2394 ENHANCEMENT(hpos, HPOS);
2395 ENHANCEMENT(vpos, VPOS);
2396 ENHANCEMENT(saturation, SATURATION);
2397 ENHANCEMENT(contrast, CONTRAST);
2398 ENHANCEMENT(hue, HUE);
2399 ENHANCEMENT(sharpness, SHARPNESS);
2400 ENHANCEMENT(brightness, BRIGHTNESS);
2401 ENHANCEMENT(flicker_filter, FLICKER_FILTER);
2402 ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2403 ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
2404 ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
2405 ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
2407 if (enhancements.dot_crawl) {
2408 if (!psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2409 return false;
2411 psb_intel_sdvo_connector->max_dot_crawl = 1;
2412 psb_intel_sdvo_connector->cur_dot_crawl = response & 0x1;
2413 psb_intel_sdvo_connector->dot_crawl =
2414 drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
2415 if (!psb_intel_sdvo_connector->dot_crawl)
2416 return false;
2418 drm_object_attach_property(&connector->base,
2419 psb_intel_sdvo_connector->dot_crawl,
2420 psb_intel_sdvo_connector->cur_dot_crawl);
2421 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2424 return true;
2427 static bool
2428 psb_intel_sdvo_create_enhance_property_lvds(struct psb_intel_sdvo *psb_intel_sdvo,
2429 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector,
2430 struct psb_intel_sdvo_enhancements_reply enhancements)
2432 struct drm_device *dev = psb_intel_sdvo->base.base.dev;
2433 struct drm_connector *connector = &psb_intel_sdvo_connector->base.base;
2434 uint16_t response, data_value[2];
2436 ENHANCEMENT(brightness, BRIGHTNESS);
2438 return true;
2440 #undef ENHANCEMENT
2442 static bool psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
2443 struct psb_intel_sdvo_connector *psb_intel_sdvo_connector)
2445 union {
2446 struct psb_intel_sdvo_enhancements_reply reply;
2447 uint16_t response;
2448 } enhancements;
2450 BUILD_BUG_ON(sizeof(enhancements) != 2);
2452 enhancements.response = 0;
2453 psb_intel_sdvo_get_value(psb_intel_sdvo,
2454 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2455 &enhancements, sizeof(enhancements));
2456 if (enhancements.response == 0) {
2457 DRM_DEBUG_KMS("No enhancement is supported\n");
2458 return true;
2461 if (IS_TV(psb_intel_sdvo_connector))
2462 return psb_intel_sdvo_create_enhance_property_tv(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2463 else if(IS_LVDS(psb_intel_sdvo_connector))
2464 return psb_intel_sdvo_create_enhance_property_lvds(psb_intel_sdvo, psb_intel_sdvo_connector, enhancements.reply);
2465 else
2466 return true;
2469 static int psb_intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2470 struct i2c_msg *msgs,
2471 int num)
2473 struct psb_intel_sdvo *sdvo = adapter->algo_data;
2475 if (!psb_intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2476 return -EIO;
2478 return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2481 static u32 psb_intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2483 struct psb_intel_sdvo *sdvo = adapter->algo_data;
2484 return sdvo->i2c->algo->functionality(sdvo->i2c);
2487 static const struct i2c_algorithm psb_intel_sdvo_ddc_proxy = {
2488 .master_xfer = psb_intel_sdvo_ddc_proxy_xfer,
2489 .functionality = psb_intel_sdvo_ddc_proxy_func
2492 static bool
2493 psb_intel_sdvo_init_ddc_proxy(struct psb_intel_sdvo *sdvo,
2494 struct drm_device *dev)
2496 sdvo->ddc.owner = THIS_MODULE;
2497 sdvo->ddc.class = I2C_CLASS_DDC;
2498 snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2499 sdvo->ddc.dev.parent = &dev->pdev->dev;
2500 sdvo->ddc.algo_data = sdvo;
2501 sdvo->ddc.algo = &psb_intel_sdvo_ddc_proxy;
2503 return i2c_add_adapter(&sdvo->ddc) == 0;
2506 bool psb_intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
2508 struct drm_psb_private *dev_priv = dev->dev_private;
2509 struct gma_encoder *gma_encoder;
2510 struct psb_intel_sdvo *psb_intel_sdvo;
2511 int i;
2513 psb_intel_sdvo = kzalloc(sizeof(struct psb_intel_sdvo), GFP_KERNEL);
2514 if (!psb_intel_sdvo)
2515 return false;
2517 psb_intel_sdvo->sdvo_reg = sdvo_reg;
2518 psb_intel_sdvo->slave_addr = psb_intel_sdvo_get_slave_addr(dev, sdvo_reg) >> 1;
2519 psb_intel_sdvo_select_i2c_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2520 if (!psb_intel_sdvo_init_ddc_proxy(psb_intel_sdvo, dev)) {
2521 kfree(psb_intel_sdvo);
2522 return false;
2525 /* encoder type will be decided later */
2526 gma_encoder = &psb_intel_sdvo->base;
2527 gma_encoder->type = INTEL_OUTPUT_SDVO;
2528 drm_encoder_init(dev, &gma_encoder->base, &psb_intel_sdvo_enc_funcs,
2529 0, NULL);
2531 /* Read the regs to test if we can talk to the device */
2532 for (i = 0; i < 0x40; i++) {
2533 u8 byte;
2535 if (!psb_intel_sdvo_read_byte(psb_intel_sdvo, i, &byte)) {
2536 DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
2537 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2538 goto err;
2542 if (IS_SDVOB(sdvo_reg))
2543 dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
2544 else
2545 dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
2547 drm_encoder_helper_add(&gma_encoder->base, &psb_intel_sdvo_helper_funcs);
2549 /* In default case sdvo lvds is false */
2550 if (!psb_intel_sdvo_get_capabilities(psb_intel_sdvo, &psb_intel_sdvo->caps))
2551 goto err;
2553 if (psb_intel_sdvo_output_setup(psb_intel_sdvo,
2554 psb_intel_sdvo->caps.output_flags) != true) {
2555 DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
2556 IS_SDVOB(sdvo_reg) ? 'B' : 'C');
2557 goto err;
2560 psb_intel_sdvo_select_ddc_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2562 /* Set the input timing to the screen. Assume always input 0. */
2563 if (!psb_intel_sdvo_set_target_input(psb_intel_sdvo))
2564 goto err;
2566 if (!psb_intel_sdvo_get_input_pixel_clock_range(psb_intel_sdvo,
2567 &psb_intel_sdvo->pixel_clock_min,
2568 &psb_intel_sdvo->pixel_clock_max))
2569 goto err;
2571 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
2572 "clock range %dMHz - %dMHz, "
2573 "input 1: %c, input 2: %c, "
2574 "output 1: %c, output 2: %c\n",
2575 SDVO_NAME(psb_intel_sdvo),
2576 psb_intel_sdvo->caps.vendor_id, psb_intel_sdvo->caps.device_id,
2577 psb_intel_sdvo->caps.device_rev_id,
2578 psb_intel_sdvo->pixel_clock_min / 1000,
2579 psb_intel_sdvo->pixel_clock_max / 1000,
2580 (psb_intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
2581 (psb_intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
2582 /* check currently supported outputs */
2583 psb_intel_sdvo->caps.output_flags &
2584 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
2585 psb_intel_sdvo->caps.output_flags &
2586 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
2587 return true;
2589 err:
2590 drm_encoder_cleanup(&gma_encoder->base);
2591 i2c_del_adapter(&psb_intel_sdvo->ddc);
2592 kfree(psb_intel_sdvo);
2594 return false;