linux/drivers/gpu/drm/gma500/psb_intel_sdvo.c
<<
>>
Prefs
   1/*
   2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
   3 * Copyright © 2006-2007 Intel Corporation
   4 *   Jesse Barnes <jesse.barnes@intel.com>
   5 *
   6 * Permission is hereby granted, free of charge, to any person obtaining a
   7 * copy of this software and associated documentation files (the "Software"),
   8 * to deal in the Software without restriction, including without limitation
   9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10 * and/or sell copies of the Software, and to permit persons to whom the
  11 * Software is furnished to do so, subject to the following conditions:
  12 *
  13 * The above copyright notice and this permission notice (including the next
  14 * paragraph) shall be included in all copies or substantial portions of the
  15 * Software.
  16 *
  17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23 * DEALINGS IN THE SOFTWARE.
  24 *
  25 * Authors:
  26 *      Eric Anholt <eric@anholt.net>
  27 */
  28#include <linux/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"
  41
  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)
  46
  47#define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
  48                         SDVO_TV_MASK)
  49
  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))
  54
  55
  56static 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"
  64};
  65
  66#define TV_FORMAT_NUM  (sizeof(tv_format_names) / sizeof(*tv_format_names))
  67
  68struct psb_intel_sdvo {
  69        struct psb_intel_encoder base;
  70
  71        struct i2c_adapter *i2c;
  72        u8 slave_addr;
  73
  74        struct i2c_adapter ddc;
  75
  76        /* Register for the SDVO device: SDVOB or SDVOC */
  77        int sdvo_reg;
  78
  79        /* Active outputs controlled by this SDVO output */
  80        uint16_t controlled_output;
  81
  82        /*
  83         * Capabilities of the SDVO device returned by
  84         * i830_sdvo_get_capabilities()
  85         */
  86        struct psb_intel_sdvo_caps caps;
  87
  88        /* Pixel clock limitations reported by the SDVO device, in kHz */
  89        int pixel_clock_min, pixel_clock_max;
  90
  91        /*
  92        * For multiple function SDVO device,
  93        * this is for current attached outputs.
  94        */
  95        uint16_t attached_output;
  96
  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.
 100         */
 101        uint32_t color_range;
 102
 103        /**
 104         * This is set if we're going to treat the device as TV-out.
 105         *
 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).
 109         */
 110        bool is_tv;
 111
 112        /* This is for current tv format name */
 113        int tv_format_index;
 114
 115        /**
 116         * This is set if we treat the device as HDMI, instead of DVI.
 117         */
 118        bool is_hdmi;
 119        bool has_hdmi_monitor;
 120        bool has_hdmi_audio;
 121
 122        /**
 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.
 125         */
 126        bool is_lvds;
 127
 128        /**
 129         * This is sdvo fixed pannel mode pointer
 130         */
 131        struct drm_display_mode *sdvo_lvds_fixed_mode;
 132
 133        /* DDC bus used by this SDVO encoder */
 134        uint8_t ddc_bus;
 135
 136        /* Input timings for adjusted_mode */
 137        struct psb_intel_sdvo_dtd input_dtd;
 138};
 139
 140struct psb_intel_sdvo_connector {
 141        struct psb_intel_connector base;
 142
 143        /* Mark the type of connector */
 144        uint16_t output_flag;
 145
 146        int force_audio;
 147
 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;
 152
 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;
 170
 171        /* add the property for the SDVO-TV/LVDS */
 172        struct drm_property *brightness;
 173
 174        /* Add variable to record current setting for the above property */
 175        u32     left_margin, right_margin, top_margin, bottom_margin;
 176
 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;
 192};
 193
 194static struct psb_intel_sdvo *to_psb_intel_sdvo(struct drm_encoder *encoder)
 195{
 196        return container_of(encoder, struct psb_intel_sdvo, base.base);
 197}
 198
 199static struct psb_intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
 200{
 201        return container_of(psb_intel_attached_encoder(connector),
 202                            struct psb_intel_sdvo, base);
 203}
 204
 205static struct psb_intel_sdvo_connector *to_psb_intel_sdvo_connector(struct drm_connector *connector)
 206{
 207        return container_of(to_psb_intel_connector(connector), struct psb_intel_sdvo_connector, base);
 208}
 209
 210static bool
 211psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags);
 212static bool
 213psb_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);
 216static bool
 217psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
 218                                   struct psb_intel_sdvo_connector *psb_intel_sdvo_connector);
 219
 220/**
 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).
 224 */
 225static void psb_intel_sdvo_write_sdvox(struct psb_intel_sdvo *psb_intel_sdvo, u32 val)
 226{
 227        struct drm_device *dev = psb_intel_sdvo->base.base.dev;
 228        u32 bval = val, cval = val;
 229        int i;
 230
 231        if (psb_intel_sdvo->sdvo_reg == SDVOB) {
 232                cval = REG_READ(SDVOC);
 233        } else {
 234                bval = REG_READ(SDVOB);
 235        }
 236        /*
 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
 240         */
 241        for (i = 0; i < 2; i++)
 242        {
 243                REG_WRITE(SDVOB, bval);
 244                REG_READ(SDVOB);
 245                REG_WRITE(SDVOC, cval);
 246                REG_READ(SDVOC);
 247        }
 248}
 249
 250static bool psb_intel_sdvo_read_byte(struct psb_intel_sdvo *psb_intel_sdvo, u8 addr, u8 *ch)
 251{
 252        struct i2c_msg msgs[] = {
 253                {
 254                        .addr = psb_intel_sdvo->slave_addr,
 255                        .flags = 0,
 256                        .len = 1,
 257                        .buf = &addr,
 258                },
 259                {
 260                        .addr = psb_intel_sdvo->slave_addr,
 261                        .flags = I2C_M_RD,
 262                        .len = 1,
 263                        .buf = ch,
 264                }
 265        };
 266        int ret;
 267
 268        if ((ret = i2c_transfer(psb_intel_sdvo->i2c, msgs, 2)) == 2)
 269                return true;
 270
 271        DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
 272        return false;
 273}
 274
 275#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
 276/** Mapping of command numbers to names, for debug output */
 277static 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),
 324
 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),
 370
 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),
 392};
 393
 394#define IS_SDVOB(reg)   (reg == SDVOB)
 395#define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
 396
 397static void psb_intel_sdvo_debug_write(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
 398                                   const void *args, int args_len)
 399{
 400        int i;
 401
 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;
 412                }
 413        }
 414        if (i == ARRAY_SIZE(sdvo_cmd_names))
 415                DRM_LOG_KMS("(%02X)", cmd);
 416        DRM_LOG_KMS("\n");
 417}
 418
 419static 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"
 427};
 428
 429static bool psb_intel_sdvo_write_cmd(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
 430                                 const void *args, int args_len)
 431{
 432        u8 buf[args_len*2 + 2], status;
 433        struct i2c_msg msgs[args_len + 3];
 434        int i, ret;
 435
 436        psb_intel_sdvo_debug_write(psb_intel_sdvo, cmd, args, args_len);
 437
 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];
 445        }
 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;
 452
 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;
 459
 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;
 464
 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;
 469        }
 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;
 474        }
 475
 476        return true;
 477}
 478
 479static bool psb_intel_sdvo_read_response(struct psb_intel_sdvo *psb_intel_sdvo,
 480                                     void *response, int response_len)
 481{
 482        u8 retry = 5;
 483        u8 status;
 484        int i;
 485
 486        DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(psb_intel_sdvo));
 487
 488        /*
 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.
 493         *
 494         * Check 5 times in case the hardware failed to read the docs.
 495         */
 496        if (!psb_intel_sdvo_read_byte(psb_intel_sdvo,
 497                                  SDVO_I2C_CMD_STATUS,
 498                                  &status))
 499                goto log_fail;
 500
 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;
 507        }
 508
 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);
 513
 514        if (status != SDVO_CMD_STATUS_SUCCESS)
 515                goto log_fail;
 516
 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]);
 524        }
 525        DRM_LOG_KMS("\n");
 526        return true;
 527
 528log_fail:
 529        DRM_LOG_KMS("... failed\n");
 530        return false;
 531}
 532
 533static int psb_intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
 534{
 535        if (mode->clock >= 100000)
 536                return 1;
 537        else if (mode->clock >= 50000)
 538                return 2;
 539        else
 540                return 4;
 541}
 542
 543static bool psb_intel_sdvo_set_control_bus_switch(struct psb_intel_sdvo *psb_intel_sdvo,
 544                                              u8 ddc_bus)
 545{
 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);
 550}
 551
 552static bool psb_intel_sdvo_set_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, const void *data, int len)
 553{
 554        if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, data, len))
 555                return false;
 556
 557        return psb_intel_sdvo_read_response(psb_intel_sdvo, NULL, 0);
 558}
 559
 560static bool
 561psb_intel_sdvo_get_value(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd, void *value, int len)
 562{
 563        if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo, cmd, NULL, 0))
 564                return false;
 565
 566        return psb_intel_sdvo_read_response(psb_intel_sdvo, value, len);
 567}
 568
 569static bool psb_intel_sdvo_set_target_input(struct psb_intel_sdvo *psb_intel_sdvo)
 570{
 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));
 575}
 576
 577/**
 578 * Return whether each input is trained.
 579 *
 580 * This function is making an assumption about the layout of the response,
 581 * which should be checked against the docs.
 582 */
 583static bool psb_intel_sdvo_get_trained_inputs(struct psb_intel_sdvo *psb_intel_sdvo, bool *input_1, bool *input_2)
 584{
 585        struct psb_intel_sdvo_get_trained_inputs_response response;
 586
 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;
 591
 592        *input_1 = response.input0_trained;
 593        *input_2 = response.input1_trained;
 594        return true;
 595}
 596
 597static bool psb_intel_sdvo_set_active_outputs(struct psb_intel_sdvo *psb_intel_sdvo,
 598                                          u16 outputs)
 599{
 600        return psb_intel_sdvo_set_value(psb_intel_sdvo,
 601                                    SDVO_CMD_SET_ACTIVE_OUTPUTS,
 602                                    &outputs, sizeof(outputs));
 603}
 604
 605static bool psb_intel_sdvo_set_encoder_power_state(struct psb_intel_sdvo *psb_intel_sdvo,
 606                                               int mode)
 607{
 608        u8 state = SDVO_ENCODER_STATE_ON;
 609
 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;
 623        }
 624
 625        return psb_intel_sdvo_set_value(psb_intel_sdvo,
 626                                    SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
 627}
 628
 629static bool psb_intel_sdvo_get_input_pixel_clock_range(struct psb_intel_sdvo *psb_intel_sdvo,
 630                                                   int *clock_min,
 631                                                   int *clock_max)
 632{
 633        struct psb_intel_sdvo_pixel_clock_range clocks;
 634
 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;
 640
 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;
 645}
 646
 647static bool psb_intel_sdvo_set_target_output(struct psb_intel_sdvo *psb_intel_sdvo,
 648                                         u16 outputs)
 649{
 650        return psb_intel_sdvo_set_value(psb_intel_sdvo,
 651                                    SDVO_CMD_SET_TARGET_OUTPUT,
 652                                    &outputs, sizeof(outputs));
 653}
 654
 655static bool psb_intel_sdvo_set_timing(struct psb_intel_sdvo *psb_intel_sdvo, u8 cmd,
 656                                  struct psb_intel_sdvo_dtd *dtd)
 657{
 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));
 660}
 661
 662static bool psb_intel_sdvo_set_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
 663                                         struct psb_intel_sdvo_dtd *dtd)
 664{
 665        return psb_intel_sdvo_set_timing(psb_intel_sdvo,
 666                                     SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
 667}
 668
 669static bool psb_intel_sdvo_set_output_timing(struct psb_intel_sdvo *psb_intel_sdvo,
 670                                         struct psb_intel_sdvo_dtd *dtd)
 671{
 672        return psb_intel_sdvo_set_timing(psb_intel_sdvo,
 673                                     SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
 674}
 675
 676static bool
 677psb_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)
 681{
 682        struct psb_intel_sdvo_preferred_input_timing_args args;
 683
 684        memset(&args, 0, sizeof(args));
 685        args.clock = clock;
 686        args.width = width;
 687        args.height = height;
 688        args.interlace = 0;
 689
 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;
 694
 695        return psb_intel_sdvo_set_value(psb_intel_sdvo,
 696                                    SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
 697                                    &args, sizeof(args));
 698}
 699
 700static bool psb_intel_sdvo_get_preferred_input_timing(struct psb_intel_sdvo *psb_intel_sdvo,
 701                                                  struct psb_intel_sdvo_dtd *dtd)
 702{
 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));
 709}
 710
 711static bool psb_intel_sdvo_set_clock_rate_mult(struct psb_intel_sdvo *psb_intel_sdvo, u8 val)
 712{
 713        return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
 714}
 715
 716static void psb_intel_sdvo_get_dtd_from_mode(struct psb_intel_sdvo_dtd *dtd,
 717                                         const struct drm_display_mode *mode)
 718{
 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;
 722
 723        width = mode->crtc_hdisplay;
 724        height = mode->crtc_vdisplay;
 725
 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;
 729
 730        v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
 731        v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
 732
 733        h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
 734        v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
 735
 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);
 745
 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);
 753
 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;
 759
 760        dtd->part2.sdvo_flags = 0;
 761        dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
 762        dtd->part2.reserved = 0;
 763}
 764
 765static void psb_intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
 766                                         const struct psb_intel_sdvo_dtd *dtd)
 767{
 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;
 776
 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;
 788
 789        mode->clock = dtd->part1.clock * 10;
 790
 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;
 796}
 797
 798static bool psb_intel_sdvo_check_supp_encode(struct psb_intel_sdvo *psb_intel_sdvo)
 799{
 800        struct psb_intel_sdvo_encode encode;
 801
 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));
 806}
 807
 808static bool psb_intel_sdvo_set_encode(struct psb_intel_sdvo *psb_intel_sdvo,
 809                                  uint8_t mode)
 810{
 811        return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
 812}
 813
 814static bool psb_intel_sdvo_set_colorimetry(struct psb_intel_sdvo *psb_intel_sdvo,
 815                                       uint8_t mode)
 816{
 817        return psb_intel_sdvo_set_value(psb_intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
 818}
 819
 820#if 0
 821static void psb_intel_sdvo_dump_hdmi_buf(struct psb_intel_sdvo *psb_intel_sdvo)
 822{
 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;
 829
 830        psb_intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
 831
 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);
 838
 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;
 845                }
 846        }
 847}
 848#endif
 849
 850static bool psb_intel_sdvo_set_avi_infoframe(struct psb_intel_sdvo *psb_intel_sdvo)
 851{
 852        DRM_INFO("HDMI is not supported yet");
 853
 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,
 860        };
 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;
 865
 866        intel_dip_infoframe_csum(&avi_if);
 867
 868        if (!psb_intel_sdvo_set_value(psb_intel_sdvo,
 869                                  SDVO_CMD_SET_HBUF_INDEX,
 870                                  set_buf_index, 2))
 871                return false;
 872
 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++;
 879        }
 880
 881        return psb_intel_sdvo_set_value(psb_intel_sdvo,
 882                                    SDVO_CMD_SET_HBUF_TXRATE,
 883                                    &tx_rate, 1);
 884#endif
 885}
 886
 887static bool psb_intel_sdvo_set_tv_format(struct psb_intel_sdvo *psb_intel_sdvo)
 888{
 889        struct psb_intel_sdvo_tv_format format;
 890        uint32_t format_map;
 891
 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)));
 895
 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));
 900}
 901
 902static bool
 903psb_intel_sdvo_set_output_timings_from_mode(struct psb_intel_sdvo *psb_intel_sdvo,
 904                                        struct drm_display_mode *mode)
 905{
 906        struct psb_intel_sdvo_dtd output_dtd;
 907
 908        if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo,
 909                                          psb_intel_sdvo->attached_output))
 910                return false;
 911
 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;
 915
 916        return true;
 917}
 918
 919static bool
 920psb_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)
 923{
 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;
 927
 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;
 933
 934        if (!psb_intel_sdvo_get_preferred_input_timing(psb_intel_sdvo,
 935                                                   &psb_intel_sdvo->input_dtd))
 936                return false;
 937
 938        psb_intel_sdvo_get_mode_from_dtd(adjusted_mode, &psb_intel_sdvo->input_dtd);
 939
 940        drm_mode_set_crtcinfo(adjusted_mode, 0);
 941        return true;
 942}
 943
 944static bool psb_intel_sdvo_mode_fixup(struct drm_encoder *encoder,
 945                                  struct drm_display_mode *mode,
 946                                  struct drm_display_mode *adjusted_mode)
 947{
 948        struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
 949        int multiplier;
 950
 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.
 955         */
 956        if (psb_intel_sdvo->is_tv) {
 957                if (!psb_intel_sdvo_set_output_timings_from_mode(psb_intel_sdvo, mode))
 958                        return false;
 959
 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;
 967
 968                (void) psb_intel_sdvo_set_input_timings_for_mode(psb_intel_sdvo,
 969                                                             mode,
 970                                                             adjusted_mode);
 971        }
 972
 973        /* Make the CRTC code factor in the SDVO pixel multiplier.  The
 974         * SDVO device will factor out the multiplier during mode_set.
 975         */
 976        multiplier = psb_intel_sdvo_get_pixel_multiplier(adjusted_mode);
 977        psb_intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
 978
 979        return true;
 980}
 981
 982static void psb_intel_sdvo_mode_set(struct drm_encoder *encoder,
 983                                struct drm_display_mode *mode,
 984                                struct drm_display_mode *adjusted_mode)
 985{
 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;
 995
 996        if (!mode)
 997                return;
 998
 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.
1004         */
1005        in_out.in0 = psb_intel_sdvo->attached_output;
1006        in_out.in1 = 0;
1007
1008        psb_intel_sdvo_set_value(psb_intel_sdvo,
1009                             SDVO_CMD_SET_IN_OUT_MAP,
1010                             &in_out, sizeof(in_out));
1011
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;
1016
1017        /* We have tried to get input timing in mode_fixup, and filled into
1018         * adjusted_mode.
1019         */
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;
1027
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);
1030        }
1031
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;
1035
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);
1043
1044        if (psb_intel_sdvo->is_tv &&
1045            !psb_intel_sdvo_set_tv_format(psb_intel_sdvo))
1046                return;
1047
1048        (void) psb_intel_sdvo_set_input_timing(psb_intel_sdvo, &input_dtd);
1049
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;
1055        }
1056        if (!psb_intel_sdvo_set_clock_rate_mult(psb_intel_sdvo, rate))
1057                return;
1058
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;
1068        }
1069        sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1070
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;
1075
1076        /* FIXME: Check if this is needed for PSB
1077        sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
1078        */
1079
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);
1083}
1084
1085static void psb_intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
1086{
1087        struct drm_device *dev = encoder->dev;
1088        struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1089        u32 temp;
1090
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);
1100        }
1101
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);
1106
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);
1111                        }
1112                }
1113        } else {
1114                bool input1, input2;
1115                int i;
1116                u8 status;
1117
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);
1123
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.
1128                 */
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));
1132                }
1133
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);
1137        }
1138        return;
1139}
1140
1141static int psb_intel_sdvo_mode_valid(struct drm_connector *connector,
1142                                 struct drm_display_mode *mode)
1143{
1144        struct drm_psb_private *dev_priv = connector->dev->dev_private;
1145        struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1146
1147        if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1148                return MODE_NO_DBLESCAN;
1149
1150        if (psb_intel_sdvo->pixel_clock_min > mode->clock)
1151                return MODE_CLOCK_LOW;
1152
1153        if (psb_intel_sdvo->pixel_clock_max < mode->clock)
1154                return MODE_CLOCK_HIGH;
1155
1156        if (psb_intel_sdvo->is_lvds) {
1157                if (mode->hdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
1158                        return MODE_PANEL;
1159
1160                if (mode->vdisplay > psb_intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
1161                        return MODE_PANEL;
1162        }
1163
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;
1168
1169        return MODE_OK;
1170}
1171
1172static bool psb_intel_sdvo_get_capabilities(struct psb_intel_sdvo *psb_intel_sdvo, struct psb_intel_sdvo_caps *caps)
1173{
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;
1179
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);
1205
1206        return true;
1207}
1208
1209/* No use! */
1210#if 0
1211struct drm_connector* psb_intel_sdvo_find(struct drm_device *dev, int sdvoB)
1212{
1213        struct drm_connector *connector = NULL;
1214        struct psb_intel_sdvo *iout = NULL;
1215        struct psb_intel_sdvo *sdvo;
1216
1217        /* find the sdvo connector */
1218        list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1219                iout = to_psb_intel_sdvo(connector);
1220
1221                if (iout->type != INTEL_OUTPUT_SDVO)
1222                        continue;
1223
1224                sdvo = iout->dev_priv;
1225
1226                if (sdvo->sdvo_reg == SDVOB && sdvoB)
1227                        return connector;
1228
1229                if (sdvo->sdvo_reg == SDVOC && !sdvoB)
1230                        return connector;
1231
1232        }
1233
1234        return NULL;
1235}
1236
1237int psb_intel_sdvo_supports_hotplug(struct drm_connector *connector)
1238{
1239        u8 response[2];
1240        u8 status;
1241        struct psb_intel_sdvo *psb_intel_sdvo;
1242        DRM_DEBUG_KMS("\n");
1243
1244        if (!connector)
1245                return 0;
1246
1247        psb_intel_sdvo = to_psb_intel_sdvo(connector);
1248
1249        return psb_intel_sdvo_get_value(psb_intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1250                                    &response, 2) && response[0];
1251}
1252
1253void psb_intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
1254{
1255        u8 response[2];
1256        u8 status;
1257        struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(connector);
1258
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);
1261
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);
1265
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);
1271        }
1272
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);
1275}
1276#endif
1277
1278static bool
1279psb_intel_sdvo_multifunc_encoder(struct psb_intel_sdvo *psb_intel_sdvo)
1280{
1281        /* Is there more than one type of output? */
1282        int caps = psb_intel_sdvo->caps.output_flags & 0xf;
1283        return caps & -caps;
1284}
1285
1286static struct edid *
1287psb_intel_sdvo_get_edid(struct drm_connector *connector)
1288{
1289        struct psb_intel_sdvo *sdvo = intel_attached_sdvo(connector);
1290        return drm_get_edid(connector, &sdvo->ddc);
1291}
1292
1293/* Mac mini hack -- use the same DDC as the analog connector */
1294static struct edid *
1295psb_intel_sdvo_get_analog_edid(struct drm_connector *connector)
1296{
1297        struct drm_psb_private *dev_priv = connector->dev->dev_private;
1298
1299        return drm_get_edid(connector,
1300                            &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
1301        return NULL;
1302}
1303
1304enum drm_connector_status
1305psb_intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
1306{
1307        struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1308        enum drm_connector_status status;
1309        struct edid *edid;
1310
1311        edid = psb_intel_sdvo_get_edid(connector);
1312
1313        if (edid == NULL && psb_intel_sdvo_multifunc_encoder(psb_intel_sdvo)) {
1314                u8 ddc, saved_ddc = psb_intel_sdvo->ddc_bus;
1315
1316                /*
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.
1319                 */
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;
1325                }
1326                /*
1327                 * If we found the EDID on the other bus,
1328                 * assume that is the correct DDC bus.
1329                 */
1330                if (edid == NULL)
1331                        psb_intel_sdvo->ddc_bus = saved_ddc;
1332        }
1333
1334        /*
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.
1337         */
1338        if (edid == NULL)
1339                edid = psb_intel_sdvo_get_analog_edid(connector);
1340
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);
1349                        }
1350                } else
1351                        status = connector_status_disconnected;
1352                connector->display_info.raw_edid = NULL;
1353                kfree(edid);
1354        }
1355
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;
1360        }
1361
1362        return status;
1363}
1364
1365static enum drm_connector_status
1366psb_intel_sdvo_detect(struct drm_connector *connector, bool force)
1367{
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;
1372
1373        if (!psb_intel_sdvo_write_cmd(psb_intel_sdvo,
1374                                  SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
1375                return connector_status_unknown;
1376
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);
1381
1382        if (!psb_intel_sdvo_read_response(psb_intel_sdvo, &response, 2))
1383                return connector_status_unknown;
1384
1385        DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1386                      response & 0xff, response >> 8,
1387                      psb_intel_sdvo_connector->output_flag);
1388
1389        if (response == 0)
1390                return connector_status_disconnected;
1391
1392        psb_intel_sdvo->attached_output = response;
1393
1394        psb_intel_sdvo->has_hdmi_monitor = false;
1395        psb_intel_sdvo->has_hdmi_audio = false;
1396
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;
1403
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;
1417        }
1418
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;
1424
1425                if (response & SDVO_TV_MASK) {
1426                        psb_intel_sdvo->is_tv = true;
1427                        psb_intel_sdvo->base.needs_tv_clock = true;
1428                }
1429                if (response & SDVO_LVDS_MASK)
1430                        psb_intel_sdvo->is_lvds = psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL;
1431        }
1432
1433        return ret;
1434}
1435
1436static void psb_intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1437{
1438        struct edid *edid;
1439
1440        /* set the bus switch and get the modes */
1441        edid = psb_intel_sdvo_get_edid(connector);
1442
1443        /*
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.
1448         */
1449        if (edid == NULL)
1450                edid = psb_intel_sdvo_get_analog_edid(connector);
1451
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);
1456
1457                if (connector_is_digital == monitor_is_digital) {
1458                        drm_mode_connector_update_edid_property(connector, edid);
1459                        drm_add_edid_modes(connector, edid);
1460                }
1461
1462                connector->display_info.raw_edid = NULL;
1463                kfree(edid);
1464        }
1465}
1466
1467/*
1468 * Set of SDVO TV modes.
1469 * Note!  This is in reply order (see loop in get_tv_modes).
1470 * XXX: all 60Hz refresh?
1471 */
1472static 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) },
1530};
1531
1532static void psb_intel_sdvo_get_tv_modes(struct drm_connector *connector)
1533{
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;
1538
1539        /* Read the list of supported input resolutions for the selected TV
1540         * format.
1541         */
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)));
1545
1546        if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, psb_intel_sdvo->attached_output))
1547                return;
1548
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;
1556
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);
1564                }
1565}
1566
1567static void psb_intel_sdvo_get_lvds_modes(struct drm_connector *connector)
1568{
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;
1572
1573        /*
1574         * Attempt to get the mode list from DDC.
1575         * Assume that the preferred modes are
1576         * arranged in priority order.
1577         */
1578        psb_intel_ddc_get_modes(connector, psb_intel_sdvo->i2c);
1579        if (list_empty(&connector->probed_modes) == false)
1580                goto end;
1581
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);
1591                }
1592        }
1593
1594end:
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);
1599
1600                        drm_mode_set_crtcinfo(psb_intel_sdvo->sdvo_lvds_fixed_mode,
1601                                              0);
1602
1603                        psb_intel_sdvo->is_lvds = true;
1604                        break;
1605                }
1606        }
1607
1608}
1609
1610static int psb_intel_sdvo_get_modes(struct drm_connector *connector)
1611{
1612        struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1613
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);
1620
1621        return !list_empty(&connector->probed_modes);
1622}
1623
1624static void
1625psb_intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
1626{
1627        struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1628        struct drm_device *dev = connector->dev;
1629
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);
1664}
1665
1666static void psb_intel_sdvo_destroy(struct drm_connector *connector)
1667{
1668        struct psb_intel_sdvo_connector *psb_intel_sdvo_connector = to_psb_intel_sdvo_connector(connector);
1669
1670        if (psb_intel_sdvo_connector->tv_format)
1671                drm_property_destroy(connector->dev,
1672                                     psb_intel_sdvo_connector->tv_format);
1673
1674        psb_intel_sdvo_destroy_enhance_property(connector);
1675        drm_sysfs_connector_remove(connector);
1676        drm_connector_cleanup(connector);
1677        kfree(connector);
1678}
1679
1680static bool psb_intel_sdvo_detect_hdmi_audio(struct drm_connector *connector)
1681{
1682        struct psb_intel_sdvo *psb_intel_sdvo = intel_attached_sdvo(connector);
1683        struct edid *edid;
1684        bool has_audio = false;
1685
1686        if (!psb_intel_sdvo->is_hdmi)
1687                return false;
1688
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);
1692
1693        return has_audio;
1694}
1695
1696static int
1697psb_intel_sdvo_set_property(struct drm_connector *connector,
1698                        struct drm_property *property,
1699                        uint64_t val)
1700{
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;
1707
1708        ret = drm_connector_property_set_value(connector, property, val);
1709        if (ret)
1710                return ret;
1711
1712        if (property == dev_priv->force_audio_property) {
1713                int i = val;
1714                bool has_audio;
1715
1716                if (i == psb_intel_sdvo_connector->force_audio)
1717                        return 0;
1718
1719                psb_intel_sdvo_connector->force_audio = i;
1720
1721                if (i == 0)
1722                        has_audio = psb_intel_sdvo_detect_hdmi_audio(connector);
1723                else
1724                        has_audio = i > 0;
1725
1726                if (has_audio == psb_intel_sdvo->has_hdmi_audio)
1727                        return 0;
1728
1729                psb_intel_sdvo->has_hdmi_audio = has_audio;
1730                goto done;
1731        }
1732
1733        if (property == dev_priv->broadcast_rgb_property) {
1734                if (val == !!psb_intel_sdvo->color_range)
1735                        return 0;
1736
1737                psb_intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
1738                goto done;
1739        }
1740
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; \
1748        }
1749
1750        if (property == psb_intel_sdvo_connector->tv_format) {
1751                if (val >= TV_FORMAT_NUM)
1752                        return -EINVAL;
1753
1754                if (psb_intel_sdvo->tv_format_index ==
1755                    psb_intel_sdvo_connector->tv_format_supported[val])
1756                        return 0;
1757
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;
1767
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;
1779
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;
1791
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;
1803
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;
1810                }
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)
1824        }
1825
1826        return -EINVAL; /* unknown property */
1827
1828set_value:
1829        if (!psb_intel_sdvo_set_value(psb_intel_sdvo, cmd, &temp_value, 2))
1830                return -EIO;
1831
1832
1833done:
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);
1838        }
1839
1840        return 0;
1841#undef CHECK_PROPERTY
1842}
1843
1844static 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,
1850};
1851
1852static 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,
1858};
1859
1860static 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,
1864};
1865
1866static void psb_intel_sdvo_enc_destroy(struct drm_encoder *encoder)
1867{
1868        struct psb_intel_sdvo *psb_intel_sdvo = to_psb_intel_sdvo(encoder);
1869
1870        if (psb_intel_sdvo->sdvo_lvds_fixed_mode != NULL)
1871                drm_mode_destroy(encoder->dev,
1872                                 psb_intel_sdvo->sdvo_lvds_fixed_mode);
1873
1874        i2c_del_adapter(&psb_intel_sdvo->ddc);
1875        psb_intel_encoder_destroy(encoder);
1876}
1877
1878static const struct drm_encoder_funcs psb_intel_sdvo_enc_funcs = {
1879        .destroy = psb_intel_sdvo_enc_destroy,
1880};
1881
1882static void
1883psb_intel_sdvo_guess_ddc_bus(struct psb_intel_sdvo *sdvo)
1884{
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;
1890
1891#if 0
1892        uint16_t mask = 0;
1893        unsigned int num_bits;
1894
1895        /* Make a mask of outputs less than or equal to our own priority in the
1896         * list.
1897         */
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;
1912        }
1913
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;
1920
1921        /* Corresponds to SDVO_CONTROL_BUS_DDCx */
1922        sdvo->ddc_bus = 1 << num_bits;
1923#endif
1924}
1925
1926/**
1927 * Choose the appropriate DDC bus for control bus switch command for this
1928 * SDVO output based on the controlled output.
1929 *
1930 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
1931 * outputs, then LVDS outputs.
1932 */
1933static void
1934psb_intel_sdvo_select_ddc_bus(struct drm_psb_private *dev_priv,
1935                          struct psb_intel_sdvo *sdvo, u32 reg)
1936{
1937        struct sdvo_device_mapping *mapping;
1938
1939        if (IS_SDVOB(reg))
1940                mapping = &(dev_priv->sdvo_mappings[0]);
1941        else
1942                mapping = &(dev_priv->sdvo_mappings[1]);
1943
1944        if (mapping->initialized)
1945                sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
1946        else
1947                psb_intel_sdvo_guess_ddc_bus(sdvo);
1948}
1949
1950static void
1951psb_intel_sdvo_select_i2c_bus(struct drm_psb_private *dev_priv,
1952                          struct psb_intel_sdvo *sdvo, u32 reg)
1953{
1954        struct sdvo_device_mapping *mapping;
1955        u8 pin, speed;
1956
1957        if (IS_SDVOB(reg))
1958                mapping = &dev_priv->sdvo_mappings[0];
1959        else
1960                mapping = &dev_priv->sdvo_mappings[1];
1961
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;
1967        }
1968
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;
1975}
1976
1977static bool
1978psb_intel_sdvo_is_hdmi_connector(struct psb_intel_sdvo *psb_intel_sdvo, int device)
1979{
1980        return psb_intel_sdvo_check_supp_encode(psb_intel_sdvo);
1981}
1982
1983static u8
1984psb_intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
1985{
1986        struct drm_psb_private *dev_priv = dev->dev_private;
1987        struct sdvo_device_mapping *my_mapping, *other_mapping;
1988
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];
1995        }
1996
1997        /* If the BIOS described our SDVO device, take advantage of it. */
1998        if (my_mapping->slave_addr)
1999                return my_mapping->slave_addr;
2000
2001        /* If the BIOS only described a different SDVO device, use the
2002         * address that it isn't using.
2003         */
2004        if (other_mapping->slave_addr) {
2005                if (other_mapping->slave_addr == 0x70)
2006                        return 0x72;
2007                else
2008                        return 0x70;
2009        }
2010
2011        /* No SDVO device info is found for another DVO port,
2012         * so use mapping assumption we had before BIOS parsing.
2013         */
2014        if (IS_SDVOB(sdvo_reg))
2015                return 0x70;
2016        else
2017                return 0x72;
2018}
2019
2020static void
2021psb_intel_sdvo_connector_init(struct psb_intel_sdvo_connector *connector,
2022                          struct psb_intel_sdvo *encoder)
2023{
2024        drm_connector_init(encoder->base.base.dev,
2025                           &connector->base.base,
2026                           &psb_intel_sdvo_connector_funcs,
2027                           connector->base.base.connector_type);
2028
2029        drm_connector_helper_add(&connector->base.base,
2030                                 &psb_intel_sdvo_connector_helper_funcs);
2031
2032        connector->base.base.interlace_allowed = 0;
2033        connector->base.base.doublescan_allowed = 0;
2034        connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
2035
2036        psb_intel_connector_attach_encoder(&connector->base, &encoder->base);
2037        drm_sysfs_connector_add(&connector->base.base);
2038}
2039
2040static void
2041psb_intel_sdvo_add_hdmi_properties(struct psb_intel_sdvo_connector *connector)
2042{
2043        /* FIXME: We don't support HDMI at the moment
2044        struct drm_device *dev = connector->base.base.dev;
2045
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);
2049        */
2050}
2051
2052static bool
2053psb_intel_sdvo_dvi_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2054{
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;
2059
2060        psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2061        if (!psb_intel_sdvo_connector)
2062                return false;
2063
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;
2070        }
2071
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;
2077
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;
2081        }
2082        psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2083                                       (1 << INTEL_ANALOG_CLONE_BIT));
2084
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);
2088
2089        return true;
2090}
2091
2092static bool
2093psb_intel_sdvo_tv_init(struct psb_intel_sdvo *psb_intel_sdvo, int type)
2094{
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;
2099
2100        psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2101        if (!psb_intel_sdvo_connector)
2102                return false;
2103
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;
2108
2109        psb_intel_sdvo->controlled_output |= type;
2110        psb_intel_sdvo_connector->output_flag = type;
2111
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;
2115
2116        psb_intel_sdvo_connector_init(psb_intel_sdvo_connector, psb_intel_sdvo);
2117
2118        if (!psb_intel_sdvo_tv_create_property(psb_intel_sdvo, psb_intel_sdvo_connector, type))
2119                goto err;
2120
2121        if (!psb_intel_sdvo_create_enhance_property(psb_intel_sdvo, psb_intel_sdvo_connector))
2122                goto err;
2123
2124        return true;
2125
2126err:
2127        psb_intel_sdvo_destroy(connector);
2128        return false;
2129}
2130
2131static bool
2132psb_intel_sdvo_analog_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2133{
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;
2138
2139        psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2140        if (!psb_intel_sdvo_connector)
2141                return false;
2142
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;
2148
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;
2155        }
2156
2157        psb_intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
2158                                       (1 << INTEL_ANALOG_CLONE_BIT));
2159
2160        psb_intel_sdvo_connector_init(psb_intel_sdvo_connector,
2161                                  psb_intel_sdvo);
2162        return true;
2163}
2164
2165static bool
2166psb_intel_sdvo_lvds_init(struct psb_intel_sdvo *psb_intel_sdvo, int device)
2167{
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;
2172
2173        psb_intel_sdvo_connector = kzalloc(sizeof(struct psb_intel_sdvo_connector), GFP_KERNEL);
2174        if (!psb_intel_sdvo_connector)
2175                return false;
2176
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;
2181
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;
2188        }
2189
2190        psb_intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
2191                                       (1 << INTEL_SDVO_LVDS_CLONE_BIT));
2192
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;
2196
2197        return true;
2198
2199err:
2200        psb_intel_sdvo_destroy(connector);
2201        return false;
2202}
2203
2204static bool
2205psb_intel_sdvo_output_setup(struct psb_intel_sdvo *psb_intel_sdvo, uint16_t flags)
2206{
2207        psb_intel_sdvo->is_tv = false;
2208        psb_intel_sdvo->base.needs_tv_clock = false;
2209        psb_intel_sdvo->is_lvds = false;
2210
2211        /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
2212
2213        if (flags & SDVO_OUTPUT_TMDS0)
2214                if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 0))
2215                        return false;
2216
2217        if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2218                if (!psb_intel_sdvo_dvi_init(psb_intel_sdvo, 1))
2219                        return false;
2220
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;
2225
2226        if (flags & SDVO_OUTPUT_CVBS0)
2227                if (!psb_intel_sdvo_tv_init(psb_intel_sdvo, SDVO_OUTPUT_CVBS0))
2228                        return false;
2229
2230        if (flags & SDVO_OUTPUT_RGB0)
2231                if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 0))
2232                        return false;
2233
2234        if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2235                if (!psb_intel_sdvo_analog_init(psb_intel_sdvo, 1))
2236                        return false;
2237
2238        if (flags & SDVO_OUTPUT_LVDS0)
2239                if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 0))
2240                        return false;
2241
2242        if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2243                if (!psb_intel_sdvo_lvds_init(psb_intel_sdvo, 1))
2244                        return false;
2245
2246        if ((flags & SDVO_OUTPUT_MASK) == 0) {
2247                unsigned char bytes[2];
2248
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;
2255        }
2256        psb_intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
2257
2258        return true;
2259}
2260
2261static 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)
2264{
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;
2268
2269        if (!psb_intel_sdvo_set_target_output(psb_intel_sdvo, type))
2270                return false;
2271
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;
2277
2278        memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
2279
2280        if (format_map == 0)
2281                return false;
2282
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;
2287
2288
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;
2294
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]]);
2299
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;
2304
2305}
2306
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); \
2324        } \
2325} while(0)
2326
2327static bool
2328psb_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)
2331{
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];
2335
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;
2342
2343                if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2344                                          SDVO_CMD_GET_OVERSCAN_H,
2345                                          &response, 2))
2346                        return false;
2347
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;
2356
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);
2362
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;
2368
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);
2377        }
2378
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;
2384
2385                if (!psb_intel_sdvo_get_value(psb_intel_sdvo,
2386                                          SDVO_CMD_GET_OVERSCAN_V,
2387                                          &response, 2))
2388                        return false;
2389
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;
2398
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);
2404
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;
2410
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);
2419        }
2420
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);
2433
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;
2437
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;
2444
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);
2451        }
2452
2453        return true;
2454}
2455
2456static bool
2457psb_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)
2460{
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];
2464
2465        ENHANCEMENT(brightness, BRIGHTNESS);
2466
2467        return true;
2468}
2469#undef ENHANCEMENT
2470
2471static bool psb_intel_sdvo_create_enhance_property(struct psb_intel_sdvo *psb_intel_sdvo,
2472                                               struct psb_intel_sdvo_connector *psb_intel_sdvo_connector)
2473{
2474        union {
2475                struct psb_intel_sdvo_enhancements_reply reply;
2476                uint16_t response;
2477        } enhancements;
2478
2479        BUILD_BUG_ON(sizeof(enhancements) != 2);
2480
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;
2488        }
2489
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;
2496}
2497
2498static int psb_intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2499                                     struct i2c_msg *msgs,
2500                                     int num)
2501{
2502        struct psb_intel_sdvo *sdvo = adapter->algo_data;
2503
2504        if (!psb_intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2505                return -EIO;
2506
2507        return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2508}
2509
2510static u32 psb_intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2511{
2512        struct psb_intel_sdvo *sdvo = adapter->algo_data;
2513        return sdvo->i2c->algo->functionality(sdvo->i2c);
2514}
2515
2516static 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
2519};
2520
2521static bool
2522psb_intel_sdvo_init_ddc_proxy(struct psb_intel_sdvo *sdvo,
2523                          struct drm_device *dev)
2524{
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;
2531
2532        return i2c_add_adapter(&sdvo->ddc) == 0;
2533}
2534
2535bool psb_intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
2536{
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;
2541
2542        psb_intel_sdvo = kzalloc(sizeof(struct psb_intel_sdvo), GFP_KERNEL);
2543        if (!psb_intel_sdvo)
2544                return false;
2545
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;
2552        }
2553
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);
2558
2559        /* Read the regs to test if we can talk to the device */
2560        for (i = 0; i < 0x40; i++) {
2561                u8 byte;
2562
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;
2567                }
2568        }
2569
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;
2574
2575        drm_encoder_helper_add(&psb_intel_encoder->base, &psb_intel_sdvo_helper_funcs);
2576
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;
2580
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;
2586        }
2587
2588        psb_intel_sdvo_select_ddc_bus(dev_priv, psb_intel_sdvo, sdvo_reg);
2589
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;
2593
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;
2598
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;
2616
2617err:
2618        drm_encoder_cleanup(&psb_intel_encoder->base);
2619        i2c_del_adapter(&psb_intel_sdvo->ddc);
2620        kfree(psb_intel_sdvo);
2621
2622        return false;
2623}
2624