linux/drivers/gpu/drm/i915/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/i2c.h>
  29#include <linux/slab.h>
  30#include <linux/delay.h>
  31#include <linux/export.h>
  32#include <drm/drmP.h>
  33#include <drm/drm_atomic_helper.h>
  34#include <drm/drm_crtc.h>
  35#include <drm/drm_edid.h>
  36#include "intel_drv.h"
  37#include <drm/i915_drm.h>
  38#include "i915_drv.h"
  39#include "intel_sdvo_regs.h"
  40
  41#define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
  42#define SDVO_RGB_MASK  (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
  43#define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
  44#define SDVO_TV_MASK   (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_YPRPB0)
  45
  46#define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
  47                        SDVO_TV_MASK)
  48
  49#define IS_TV(c)        (c->output_flag & SDVO_TV_MASK)
  50#define IS_TMDS(c)      (c->output_flag & SDVO_TMDS_MASK)
  51#define IS_LVDS(c)      (c->output_flag & SDVO_LVDS_MASK)
  52#define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
  53#define IS_DIGITAL(c) (c->output_flag & (SDVO_TMDS_MASK | SDVO_LVDS_MASK))
  54
  55
  56static const char * const 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  ARRAY_SIZE(tv_format_names)
  67
  68struct intel_sdvo {
  69        struct 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        i915_reg_t 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         * intel_sdvo_get_capabilities()
  85         */
  86        struct 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         * Hotplug activation bits for this device
  99         */
 100        uint16_t hotplug_active;
 101
 102        /**
 103         * This is set if we're going to treat the device as TV-out.
 104         *
 105         * While we have these nice friendly flags for output types that ought
 106         * to decide this for us, the S-Video output on our HDMI+S-Video card
 107         * shows up as RGB1 (VGA).
 108         */
 109        bool is_tv;
 110
 111        enum port port;
 112
 113        /**
 114         * This is set if we treat the device as HDMI, instead of DVI.
 115         */
 116        bool is_hdmi;
 117        bool has_hdmi_monitor;
 118        bool has_hdmi_audio;
 119        bool rgb_quant_range_selectable;
 120
 121        /**
 122         * This is set if we detect output of sdvo device as LVDS and
 123         * have a valid fixed mode to use with the panel.
 124         */
 125        bool is_lvds;
 126
 127        /**
 128         * This is sdvo fixed pannel mode pointer
 129         */
 130        struct drm_display_mode *sdvo_lvds_fixed_mode;
 131
 132        /* DDC bus used by this SDVO encoder */
 133        uint8_t ddc_bus;
 134
 135        /*
 136         * the sdvo flag gets lost in round trip: dtd->adjusted_mode->dtd
 137         */
 138        uint8_t dtd_sdvo_flags;
 139};
 140
 141struct intel_sdvo_connector {
 142        struct intel_connector base;
 143
 144        /* Mark the type of connector */
 145        uint16_t output_flag;
 146
 147        /* This contains all current supported TV format */
 148        u8 tv_format_supported[TV_FORMAT_NUM];
 149        int   format_supported_num;
 150        struct drm_property *tv_format;
 151
 152        /* add the property for the SDVO-TV */
 153        struct drm_property *left;
 154        struct drm_property *right;
 155        struct drm_property *top;
 156        struct drm_property *bottom;
 157        struct drm_property *hpos;
 158        struct drm_property *vpos;
 159        struct drm_property *contrast;
 160        struct drm_property *saturation;
 161        struct drm_property *hue;
 162        struct drm_property *sharpness;
 163        struct drm_property *flicker_filter;
 164        struct drm_property *flicker_filter_adaptive;
 165        struct drm_property *flicker_filter_2d;
 166        struct drm_property *tv_chroma_filter;
 167        struct drm_property *tv_luma_filter;
 168        struct drm_property *dot_crawl;
 169
 170        /* add the property for the SDVO-TV/LVDS */
 171        struct drm_property *brightness;
 172
 173        /* this is to get the range of margin.*/
 174        u32 max_hscan, max_vscan;
 175};
 176
 177struct intel_sdvo_connector_state {
 178        /* base.base: tv.saturation/contrast/hue/brightness */
 179        struct intel_digital_connector_state base;
 180
 181        struct {
 182                unsigned overscan_h, overscan_v, hpos, vpos, sharpness;
 183                unsigned flicker_filter, flicker_filter_2d, flicker_filter_adaptive;
 184                unsigned chroma_filter, luma_filter, dot_crawl;
 185        } tv;
 186};
 187
 188static struct intel_sdvo *to_sdvo(struct intel_encoder *encoder)
 189{
 190        return container_of(encoder, struct intel_sdvo, base);
 191}
 192
 193static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
 194{
 195        return to_sdvo(intel_attached_encoder(connector));
 196}
 197
 198static struct intel_sdvo_connector *
 199to_intel_sdvo_connector(struct drm_connector *connector)
 200{
 201        return container_of(connector, struct intel_sdvo_connector, base.base);
 202}
 203
 204#define to_intel_sdvo_connector_state(conn_state) \
 205        container_of((conn_state), struct intel_sdvo_connector_state, base.base)
 206
 207static bool
 208intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
 209static bool
 210intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
 211                              struct intel_sdvo_connector *intel_sdvo_connector,
 212                              int type);
 213static bool
 214intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
 215                                   struct intel_sdvo_connector *intel_sdvo_connector);
 216
 217/*
 218 * Writes the SDVOB or SDVOC with the given value, but always writes both
 219 * SDVOB and SDVOC to work around apparent hardware issues (according to
 220 * comments in the BIOS).
 221 */
 222static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
 223{
 224        struct drm_device *dev = intel_sdvo->base.base.dev;
 225        struct drm_i915_private *dev_priv = to_i915(dev);
 226        u32 bval = val, cval = val;
 227        int i;
 228
 229        if (HAS_PCH_SPLIT(dev_priv)) {
 230                I915_WRITE(intel_sdvo->sdvo_reg, val);
 231                POSTING_READ(intel_sdvo->sdvo_reg);
 232                /*
 233                 * HW workaround, need to write this twice for issue
 234                 * that may result in first write getting masked.
 235                 */
 236                if (HAS_PCH_IBX(dev_priv)) {
 237                        I915_WRITE(intel_sdvo->sdvo_reg, val);
 238                        POSTING_READ(intel_sdvo->sdvo_reg);
 239                }
 240                return;
 241        }
 242
 243        if (intel_sdvo->port == PORT_B)
 244                cval = I915_READ(GEN3_SDVOC);
 245        else
 246                bval = I915_READ(GEN3_SDVOB);
 247
 248        /*
 249         * Write the registers twice for luck. Sometimes,
 250         * writing them only once doesn't appear to 'stick'.
 251         * The BIOS does this too. Yay, magic
 252         */
 253        for (i = 0; i < 2; i++) {
 254                I915_WRITE(GEN3_SDVOB, bval);
 255                POSTING_READ(GEN3_SDVOB);
 256
 257                I915_WRITE(GEN3_SDVOC, cval);
 258                POSTING_READ(GEN3_SDVOC);
 259        }
 260}
 261
 262static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
 263{
 264        struct i2c_msg msgs[] = {
 265                {
 266                        .addr = intel_sdvo->slave_addr,
 267                        .flags = 0,
 268                        .len = 1,
 269                        .buf = &addr,
 270                },
 271                {
 272                        .addr = intel_sdvo->slave_addr,
 273                        .flags = I2C_M_RD,
 274                        .len = 1,
 275                        .buf = ch,
 276                }
 277        };
 278        int ret;
 279
 280        if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
 281                return true;
 282
 283        DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
 284        return false;
 285}
 286
 287#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
 288/** Mapping of command numbers to names, for debug output */
 289static const struct _sdvo_cmd_name {
 290        u8 cmd;
 291        const char *name;
 292} __attribute__ ((packed)) sdvo_cmd_names[] = {
 293        SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
 294        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
 295        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
 296        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
 297        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
 298        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
 299        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
 300        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
 301        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
 302        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
 303        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
 304        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
 305        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
 306        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
 307        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
 308        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
 309        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
 310        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
 311        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
 312        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
 313        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
 314        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
 315        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
 316        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
 317        SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
 318        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
 319        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
 320        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
 321        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
 322        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
 323        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
 324        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
 325        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
 326        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
 327        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
 328        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
 329        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
 330        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
 331        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
 332        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
 333        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
 334        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
 335        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
 336
 337        /* Add the op code for SDVO enhancements */
 338        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
 339        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
 340        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
 341        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
 342        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
 343        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
 344        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
 345        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
 346        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
 347        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
 348        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
 349        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
 350        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
 351        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
 352        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
 353        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
 354        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
 355        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
 356        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
 357        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
 358        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
 359        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
 360        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
 361        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
 362        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
 363        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
 364        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
 365        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
 366        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
 367        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
 368        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
 369        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
 370        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
 371        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
 372        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
 373        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
 374        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
 375        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
 376        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
 377        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
 378        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
 379        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
 380        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
 381        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
 382
 383        /* HDMI op code */
 384        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
 385        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
 386        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
 387        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
 388        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
 389        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
 390        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
 391        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
 392        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
 393        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
 394        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
 395        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
 396        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
 397        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
 398        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
 399        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
 400        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
 401        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
 402        SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
 403        SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
 404};
 405
 406#define SDVO_NAME(svdo) ((svdo)->port == PORT_B ? "SDVOB" : "SDVOC")
 407
 408static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
 409                                   const void *args, int args_len)
 410{
 411        int i, pos = 0;
 412#define BUF_LEN 256
 413        char buffer[BUF_LEN];
 414
 415#define BUF_PRINT(args...) \
 416        pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
 417
 418
 419        for (i = 0; i < args_len; i++) {
 420                BUF_PRINT("%02X ", ((u8 *)args)[i]);
 421        }
 422        for (; i < 8; i++) {
 423                BUF_PRINT("   ");
 424        }
 425        for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
 426                if (cmd == sdvo_cmd_names[i].cmd) {
 427                        BUF_PRINT("(%s)", sdvo_cmd_names[i].name);
 428                        break;
 429                }
 430        }
 431        if (i == ARRAY_SIZE(sdvo_cmd_names)) {
 432                BUF_PRINT("(%02X)", cmd);
 433        }
 434        BUG_ON(pos >= BUF_LEN - 1);
 435#undef BUF_PRINT
 436#undef BUF_LEN
 437
 438        DRM_DEBUG_KMS("%s: W: %02X %s\n", SDVO_NAME(intel_sdvo), cmd, buffer);
 439}
 440
 441static const char * const cmd_status_names[] = {
 442        "Power on",
 443        "Success",
 444        "Not supported",
 445        "Invalid arg",
 446        "Pending",
 447        "Target not specified",
 448        "Scaling not supported"
 449};
 450
 451static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
 452                                 const void *args, int args_len)
 453{
 454        u8 *buf, status;
 455        struct i2c_msg *msgs;
 456        int i, ret = true;
 457
 458        /* Would be simpler to allocate both in one go ? */
 459        buf = kzalloc(args_len * 2 + 2, GFP_KERNEL);
 460        if (!buf)
 461                return false;
 462
 463        msgs = kcalloc(args_len + 3, sizeof(*msgs), GFP_KERNEL);
 464        if (!msgs) {
 465                kfree(buf);
 466                return false;
 467        }
 468
 469        intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
 470
 471        for (i = 0; i < args_len; i++) {
 472                msgs[i].addr = intel_sdvo->slave_addr;
 473                msgs[i].flags = 0;
 474                msgs[i].len = 2;
 475                msgs[i].buf = buf + 2 *i;
 476                buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
 477                buf[2*i + 1] = ((u8*)args)[i];
 478        }
 479        msgs[i].addr = intel_sdvo->slave_addr;
 480        msgs[i].flags = 0;
 481        msgs[i].len = 2;
 482        msgs[i].buf = buf + 2*i;
 483        buf[2*i + 0] = SDVO_I2C_OPCODE;
 484        buf[2*i + 1] = cmd;
 485
 486        /* the following two are to read the response */
 487        status = SDVO_I2C_CMD_STATUS;
 488        msgs[i+1].addr = intel_sdvo->slave_addr;
 489        msgs[i+1].flags = 0;
 490        msgs[i+1].len = 1;
 491        msgs[i+1].buf = &status;
 492
 493        msgs[i+2].addr = intel_sdvo->slave_addr;
 494        msgs[i+2].flags = I2C_M_RD;
 495        msgs[i+2].len = 1;
 496        msgs[i+2].buf = &status;
 497
 498        ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
 499        if (ret < 0) {
 500                DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
 501                ret = false;
 502                goto out;
 503        }
 504        if (ret != i+3) {
 505                /* failure in I2C transfer */
 506                DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
 507                ret = false;
 508        }
 509
 510out:
 511        kfree(msgs);
 512        kfree(buf);
 513        return ret;
 514}
 515
 516static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
 517                                     void *response, int response_len)
 518{
 519        u8 retry = 15; /* 5 quick checks, followed by 10 long checks */
 520        u8 status;
 521        int i, pos = 0;
 522#define BUF_LEN 256
 523        char buffer[BUF_LEN];
 524
 525
 526        /*
 527         * The documentation states that all commands will be
 528         * processed within 15µs, and that we need only poll
 529         * the status byte a maximum of 3 times in order for the
 530         * command to be complete.
 531         *
 532         * Check 5 times in case the hardware failed to read the docs.
 533         *
 534         * Also beware that the first response by many devices is to
 535         * reply PENDING and stall for time. TVs are notorious for
 536         * requiring longer than specified to complete their replies.
 537         * Originally (in the DDX long ago), the delay was only ever 15ms
 538         * with an additional delay of 30ms applied for TVs added later after
 539         * many experiments. To accommodate both sets of delays, we do a
 540         * sequence of slow checks if the device is falling behind and fails
 541         * to reply within 5*15µs.
 542         */
 543        if (!intel_sdvo_read_byte(intel_sdvo,
 544                                  SDVO_I2C_CMD_STATUS,
 545                                  &status))
 546                goto log_fail;
 547
 548        while ((status == SDVO_CMD_STATUS_PENDING ||
 549                status == SDVO_CMD_STATUS_TARGET_NOT_SPECIFIED) && --retry) {
 550                if (retry < 10)
 551                        msleep(15);
 552                else
 553                        udelay(15);
 554
 555                if (!intel_sdvo_read_byte(intel_sdvo,
 556                                          SDVO_I2C_CMD_STATUS,
 557                                          &status))
 558                        goto log_fail;
 559        }
 560
 561#define BUF_PRINT(args...) \
 562        pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
 563
 564        if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
 565                BUF_PRINT("(%s)", cmd_status_names[status]);
 566        else
 567                BUF_PRINT("(??? %d)", status);
 568
 569        if (status != SDVO_CMD_STATUS_SUCCESS)
 570                goto log_fail;
 571
 572        /* Read the command response */
 573        for (i = 0; i < response_len; i++) {
 574                if (!intel_sdvo_read_byte(intel_sdvo,
 575                                          SDVO_I2C_RETURN_0 + i,
 576                                          &((u8 *)response)[i]))
 577                        goto log_fail;
 578                BUF_PRINT(" %02X", ((u8 *)response)[i]);
 579        }
 580        BUG_ON(pos >= BUF_LEN - 1);
 581#undef BUF_PRINT
 582#undef BUF_LEN
 583
 584        DRM_DEBUG_KMS("%s: R: %s\n", SDVO_NAME(intel_sdvo), buffer);
 585        return true;
 586
 587log_fail:
 588        DRM_DEBUG_KMS("%s: R: ... failed\n", SDVO_NAME(intel_sdvo));
 589        return false;
 590}
 591
 592static int intel_sdvo_get_pixel_multiplier(const struct drm_display_mode *adjusted_mode)
 593{
 594        if (adjusted_mode->crtc_clock >= 100000)
 595                return 1;
 596        else if (adjusted_mode->crtc_clock >= 50000)
 597                return 2;
 598        else
 599                return 4;
 600}
 601
 602static bool intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
 603                                              u8 ddc_bus)
 604{
 605        /* This must be the immediately preceding write before the i2c xfer */
 606        return intel_sdvo_write_cmd(intel_sdvo,
 607                                    SDVO_CMD_SET_CONTROL_BUS_SWITCH,
 608                                    &ddc_bus, 1);
 609}
 610
 611static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
 612{
 613        if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
 614                return false;
 615
 616        return intel_sdvo_read_response(intel_sdvo, NULL, 0);
 617}
 618
 619static bool
 620intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
 621{
 622        if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
 623                return false;
 624
 625        return intel_sdvo_read_response(intel_sdvo, value, len);
 626}
 627
 628static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
 629{
 630        struct intel_sdvo_set_target_input_args targets = {0};
 631        return intel_sdvo_set_value(intel_sdvo,
 632                                    SDVO_CMD_SET_TARGET_INPUT,
 633                                    &targets, sizeof(targets));
 634}
 635
 636/*
 637 * Return whether each input is trained.
 638 *
 639 * This function is making an assumption about the layout of the response,
 640 * which should be checked against the docs.
 641 */
 642static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
 643{
 644        struct intel_sdvo_get_trained_inputs_response response;
 645
 646        BUILD_BUG_ON(sizeof(response) != 1);
 647        if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
 648                                  &response, sizeof(response)))
 649                return false;
 650
 651        *input_1 = response.input0_trained;
 652        *input_2 = response.input1_trained;
 653        return true;
 654}
 655
 656static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
 657                                          u16 outputs)
 658{
 659        return intel_sdvo_set_value(intel_sdvo,
 660                                    SDVO_CMD_SET_ACTIVE_OUTPUTS,
 661                                    &outputs, sizeof(outputs));
 662}
 663
 664static bool intel_sdvo_get_active_outputs(struct intel_sdvo *intel_sdvo,
 665                                          u16 *outputs)
 666{
 667        return intel_sdvo_get_value(intel_sdvo,
 668                                    SDVO_CMD_GET_ACTIVE_OUTPUTS,
 669                                    outputs, sizeof(*outputs));
 670}
 671
 672static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
 673                                               int mode)
 674{
 675        u8 state = SDVO_ENCODER_STATE_ON;
 676
 677        switch (mode) {
 678        case DRM_MODE_DPMS_ON:
 679                state = SDVO_ENCODER_STATE_ON;
 680                break;
 681        case DRM_MODE_DPMS_STANDBY:
 682                state = SDVO_ENCODER_STATE_STANDBY;
 683                break;
 684        case DRM_MODE_DPMS_SUSPEND:
 685                state = SDVO_ENCODER_STATE_SUSPEND;
 686                break;
 687        case DRM_MODE_DPMS_OFF:
 688                state = SDVO_ENCODER_STATE_OFF;
 689                break;
 690        }
 691
 692        return intel_sdvo_set_value(intel_sdvo,
 693                                    SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
 694}
 695
 696static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
 697                                                   int *clock_min,
 698                                                   int *clock_max)
 699{
 700        struct intel_sdvo_pixel_clock_range clocks;
 701
 702        BUILD_BUG_ON(sizeof(clocks) != 4);
 703        if (!intel_sdvo_get_value(intel_sdvo,
 704                                  SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
 705                                  &clocks, sizeof(clocks)))
 706                return false;
 707
 708        /* Convert the values from units of 10 kHz to kHz. */
 709        *clock_min = clocks.min * 10;
 710        *clock_max = clocks.max * 10;
 711        return true;
 712}
 713
 714static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
 715                                         u16 outputs)
 716{
 717        return intel_sdvo_set_value(intel_sdvo,
 718                                    SDVO_CMD_SET_TARGET_OUTPUT,
 719                                    &outputs, sizeof(outputs));
 720}
 721
 722static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
 723                                  struct intel_sdvo_dtd *dtd)
 724{
 725        return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
 726                intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
 727}
 728
 729static bool intel_sdvo_get_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
 730                                  struct intel_sdvo_dtd *dtd)
 731{
 732        return intel_sdvo_get_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
 733                intel_sdvo_get_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
 734}
 735
 736static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
 737                                         struct intel_sdvo_dtd *dtd)
 738{
 739        return intel_sdvo_set_timing(intel_sdvo,
 740                                     SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
 741}
 742
 743static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
 744                                         struct intel_sdvo_dtd *dtd)
 745{
 746        return intel_sdvo_set_timing(intel_sdvo,
 747                                     SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
 748}
 749
 750static bool intel_sdvo_get_input_timing(struct intel_sdvo *intel_sdvo,
 751                                        struct intel_sdvo_dtd *dtd)
 752{
 753        return intel_sdvo_get_timing(intel_sdvo,
 754                                     SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
 755}
 756
 757static bool
 758intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
 759                                         uint16_t clock,
 760                                         uint16_t width,
 761                                         uint16_t height)
 762{
 763        struct intel_sdvo_preferred_input_timing_args args;
 764
 765        memset(&args, 0, sizeof(args));
 766        args.clock = clock;
 767        args.width = width;
 768        args.height = height;
 769        args.interlace = 0;
 770
 771        if (intel_sdvo->is_lvds &&
 772           (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
 773            intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
 774                args.scaled = 1;
 775
 776        return intel_sdvo_set_value(intel_sdvo,
 777                                    SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
 778                                    &args, sizeof(args));
 779}
 780
 781static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
 782                                                  struct intel_sdvo_dtd *dtd)
 783{
 784        BUILD_BUG_ON(sizeof(dtd->part1) != 8);
 785        BUILD_BUG_ON(sizeof(dtd->part2) != 8);
 786        return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
 787                                    &dtd->part1, sizeof(dtd->part1)) &&
 788                intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
 789                                     &dtd->part2, sizeof(dtd->part2));
 790}
 791
 792static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
 793{
 794        return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
 795}
 796
 797static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
 798                                         const struct drm_display_mode *mode)
 799{
 800        uint16_t width, height;
 801        uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
 802        uint16_t h_sync_offset, v_sync_offset;
 803        int mode_clock;
 804
 805        memset(dtd, 0, sizeof(*dtd));
 806
 807        width = mode->hdisplay;
 808        height = mode->vdisplay;
 809
 810        /* do some mode translations */
 811        h_blank_len = mode->htotal - mode->hdisplay;
 812        h_sync_len = mode->hsync_end - mode->hsync_start;
 813
 814        v_blank_len = mode->vtotal - mode->vdisplay;
 815        v_sync_len = mode->vsync_end - mode->vsync_start;
 816
 817        h_sync_offset = mode->hsync_start - mode->hdisplay;
 818        v_sync_offset = mode->vsync_start - mode->vdisplay;
 819
 820        mode_clock = mode->clock;
 821        mode_clock /= 10;
 822        dtd->part1.clock = mode_clock;
 823
 824        dtd->part1.h_active = width & 0xff;
 825        dtd->part1.h_blank = h_blank_len & 0xff;
 826        dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
 827                ((h_blank_len >> 8) & 0xf);
 828        dtd->part1.v_active = height & 0xff;
 829        dtd->part1.v_blank = v_blank_len & 0xff;
 830        dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
 831                ((v_blank_len >> 8) & 0xf);
 832
 833        dtd->part2.h_sync_off = h_sync_offset & 0xff;
 834        dtd->part2.h_sync_width = h_sync_len & 0xff;
 835        dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
 836                (v_sync_len & 0xf);
 837        dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
 838                ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
 839                ((v_sync_len & 0x30) >> 4);
 840
 841        dtd->part2.dtd_flags = 0x18;
 842        if (mode->flags & DRM_MODE_FLAG_INTERLACE)
 843                dtd->part2.dtd_flags |= DTD_FLAG_INTERLACE;
 844        if (mode->flags & DRM_MODE_FLAG_PHSYNC)
 845                dtd->part2.dtd_flags |= DTD_FLAG_HSYNC_POSITIVE;
 846        if (mode->flags & DRM_MODE_FLAG_PVSYNC)
 847                dtd->part2.dtd_flags |= DTD_FLAG_VSYNC_POSITIVE;
 848
 849        dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
 850}
 851
 852static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode *pmode,
 853                                         const struct intel_sdvo_dtd *dtd)
 854{
 855        struct drm_display_mode mode = {};
 856
 857        mode.hdisplay = dtd->part1.h_active;
 858        mode.hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
 859        mode.hsync_start = mode.hdisplay + dtd->part2.h_sync_off;
 860        mode.hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
 861        mode.hsync_end = mode.hsync_start + dtd->part2.h_sync_width;
 862        mode.hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
 863        mode.htotal = mode.hdisplay + dtd->part1.h_blank;
 864        mode.htotal += (dtd->part1.h_high & 0xf) << 8;
 865
 866        mode.vdisplay = dtd->part1.v_active;
 867        mode.vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
 868        mode.vsync_start = mode.vdisplay;
 869        mode.vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
 870        mode.vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
 871        mode.vsync_start += dtd->part2.v_sync_off_high & 0xc0;
 872        mode.vsync_end = mode.vsync_start +
 873                (dtd->part2.v_sync_off_width & 0xf);
 874        mode.vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
 875        mode.vtotal = mode.vdisplay + dtd->part1.v_blank;
 876        mode.vtotal += (dtd->part1.v_high & 0xf) << 8;
 877
 878        mode.clock = dtd->part1.clock * 10;
 879
 880        if (dtd->part2.dtd_flags & DTD_FLAG_INTERLACE)
 881                mode.flags |= DRM_MODE_FLAG_INTERLACE;
 882        if (dtd->part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
 883                mode.flags |= DRM_MODE_FLAG_PHSYNC;
 884        else
 885                mode.flags |= DRM_MODE_FLAG_NHSYNC;
 886        if (dtd->part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
 887                mode.flags |= DRM_MODE_FLAG_PVSYNC;
 888        else
 889                mode.flags |= DRM_MODE_FLAG_NVSYNC;
 890
 891        drm_mode_set_crtcinfo(&mode, 0);
 892
 893        drm_mode_copy(pmode, &mode);
 894}
 895
 896static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
 897{
 898        struct intel_sdvo_encode encode;
 899
 900        BUILD_BUG_ON(sizeof(encode) != 2);
 901        return intel_sdvo_get_value(intel_sdvo,
 902                                  SDVO_CMD_GET_SUPP_ENCODE,
 903                                  &encode, sizeof(encode));
 904}
 905
 906static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
 907                                  uint8_t mode)
 908{
 909        return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
 910}
 911
 912static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
 913                                       uint8_t mode)
 914{
 915        return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
 916}
 917
 918#if 0
 919static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
 920{
 921        int i, j;
 922        uint8_t set_buf_index[2];
 923        uint8_t av_split;
 924        uint8_t buf_size;
 925        uint8_t buf[48];
 926        uint8_t *pos;
 927
 928        intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
 929
 930        for (i = 0; i <= av_split; i++) {
 931                set_buf_index[0] = i; set_buf_index[1] = 0;
 932                intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
 933                                     set_buf_index, 2);
 934                intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
 935                intel_sdvo_read_response(encoder, &buf_size, 1);
 936
 937                pos = buf;
 938                for (j = 0; j <= buf_size; j += 8) {
 939                        intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
 940                                             NULL, 0);
 941                        intel_sdvo_read_response(encoder, pos, 8);
 942                        pos += 8;
 943                }
 944        }
 945}
 946#endif
 947
 948static bool intel_sdvo_write_infoframe(struct intel_sdvo *intel_sdvo,
 949                                       unsigned if_index, uint8_t tx_rate,
 950                                       const uint8_t *data, unsigned length)
 951{
 952        uint8_t set_buf_index[2] = { if_index, 0 };
 953        uint8_t hbuf_size, tmp[8];
 954        int i;
 955
 956        if (!intel_sdvo_set_value(intel_sdvo,
 957                                  SDVO_CMD_SET_HBUF_INDEX,
 958                                  set_buf_index, 2))
 959                return false;
 960
 961        if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HBUF_INFO,
 962                                  &hbuf_size, 1))
 963                return false;
 964
 965        /* Buffer size is 0 based, hooray! */
 966        hbuf_size++;
 967
 968        DRM_DEBUG_KMS("writing sdvo hbuf: %i, hbuf_size %i, hbuf_size: %i\n",
 969                      if_index, length, hbuf_size);
 970
 971        for (i = 0; i < hbuf_size; i += 8) {
 972                memset(tmp, 0, 8);
 973                if (i < length)
 974                        memcpy(tmp, data + i, min_t(unsigned, 8, length - i));
 975
 976                if (!intel_sdvo_set_value(intel_sdvo,
 977                                          SDVO_CMD_SET_HBUF_DATA,
 978                                          tmp, 8))
 979                        return false;
 980        }
 981
 982        return intel_sdvo_set_value(intel_sdvo,
 983                                    SDVO_CMD_SET_HBUF_TXRATE,
 984                                    &tx_rate, 1);
 985}
 986
 987static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo,
 988                                         const struct intel_crtc_state *pipe_config)
 989{
 990        uint8_t sdvo_data[HDMI_INFOFRAME_SIZE(AVI)];
 991        union hdmi_infoframe frame;
 992        int ret;
 993        ssize_t len;
 994
 995        ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
 996                                                       &pipe_config->base.adjusted_mode,
 997                                                       false);
 998        if (ret < 0) {
 999                DRM_ERROR("couldn't fill AVI infoframe\n");
1000                return false;
1001        }
1002
1003        if (intel_sdvo->rgb_quant_range_selectable) {
1004                if (pipe_config->limited_color_range)
1005                        frame.avi.quantization_range =
1006                                HDMI_QUANTIZATION_RANGE_LIMITED;
1007                else
1008                        frame.avi.quantization_range =
1009                                HDMI_QUANTIZATION_RANGE_FULL;
1010        }
1011
1012        len = hdmi_infoframe_pack(&frame, sdvo_data, sizeof(sdvo_data));
1013        if (len < 0)
1014                return false;
1015
1016        return intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_AVI_IF,
1017                                          SDVO_HBUF_TX_VSYNC,
1018                                          sdvo_data, sizeof(sdvo_data));
1019}
1020
1021static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo,
1022                                     const struct drm_connector_state *conn_state)
1023{
1024        struct intel_sdvo_tv_format format;
1025        uint32_t format_map;
1026
1027        format_map = 1 << conn_state->tv.mode;
1028        memset(&format, 0, sizeof(format));
1029        memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
1030
1031        BUILD_BUG_ON(sizeof(format) != 6);
1032        return intel_sdvo_set_value(intel_sdvo,
1033                                    SDVO_CMD_SET_TV_FORMAT,
1034                                    &format, sizeof(format));
1035}
1036
1037static bool
1038intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
1039                                        const struct drm_display_mode *mode)
1040{
1041        struct intel_sdvo_dtd output_dtd;
1042
1043        if (!intel_sdvo_set_target_output(intel_sdvo,
1044                                          intel_sdvo->attached_output))
1045                return false;
1046
1047        intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1048        if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1049                return false;
1050
1051        return true;
1052}
1053
1054/*
1055 * Asks the sdvo controller for the preferred input mode given the output mode.
1056 * Unfortunately we have to set up the full output mode to do that.
1057 */
1058static bool
1059intel_sdvo_get_preferred_input_mode(struct intel_sdvo *intel_sdvo,
1060                                    const struct drm_display_mode *mode,
1061                                    struct drm_display_mode *adjusted_mode)
1062{
1063        struct intel_sdvo_dtd input_dtd;
1064
1065        /* Reset the input timing to the screen. Assume always input 0. */
1066        if (!intel_sdvo_set_target_input(intel_sdvo))
1067                return false;
1068
1069        if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
1070                                                      mode->clock / 10,
1071                                                      mode->hdisplay,
1072                                                      mode->vdisplay))
1073                return false;
1074
1075        if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
1076                                                   &input_dtd))
1077                return false;
1078
1079        intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
1080        intel_sdvo->dtd_sdvo_flags = input_dtd.part2.sdvo_flags;
1081
1082        return true;
1083}
1084
1085static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc_state *pipe_config)
1086{
1087        unsigned dotclock = pipe_config->port_clock;
1088        struct dpll *clock = &pipe_config->dpll;
1089
1090        /*
1091         * SDVO TV has fixed PLL values depend on its clock range,
1092         * this mirrors vbios setting.
1093         */
1094        if (dotclock >= 100000 && dotclock < 140500) {
1095                clock->p1 = 2;
1096                clock->p2 = 10;
1097                clock->n = 3;
1098                clock->m1 = 16;
1099                clock->m2 = 8;
1100        } else if (dotclock >= 140500 && dotclock <= 200000) {
1101                clock->p1 = 1;
1102                clock->p2 = 10;
1103                clock->n = 6;
1104                clock->m1 = 12;
1105                clock->m2 = 8;
1106        } else {
1107                WARN(1, "SDVO TV clock out of range: %i\n", dotclock);
1108        }
1109
1110        pipe_config->clock_set = true;
1111}
1112
1113static bool intel_sdvo_compute_config(struct intel_encoder *encoder,
1114                                      struct intel_crtc_state *pipe_config,
1115                                      struct drm_connector_state *conn_state)
1116{
1117        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1118        struct intel_sdvo_connector_state *intel_sdvo_state =
1119                to_intel_sdvo_connector_state(conn_state);
1120        struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1121        struct drm_display_mode *mode = &pipe_config->base.mode;
1122
1123        DRM_DEBUG_KMS("forcing bpc to 8 for SDVO\n");
1124        pipe_config->pipe_bpp = 8*3;
1125
1126        if (HAS_PCH_SPLIT(to_i915(encoder->base.dev)))
1127                pipe_config->has_pch_encoder = true;
1128
1129        /*
1130         * We need to construct preferred input timings based on our
1131         * output timings.  To do that, we have to set the output
1132         * timings, even though this isn't really the right place in
1133         * the sequence to do it. Oh well.
1134         */
1135        if (intel_sdvo->is_tv) {
1136                if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
1137                        return false;
1138
1139                (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1140                                                           mode,
1141                                                           adjusted_mode);
1142                pipe_config->sdvo_tv_clock = true;
1143        } else if (intel_sdvo->is_lvds) {
1144                if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
1145                                                             intel_sdvo->sdvo_lvds_fixed_mode))
1146                        return false;
1147
1148                (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1149                                                           mode,
1150                                                           adjusted_mode);
1151        }
1152
1153        if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
1154                return false;
1155
1156        /*
1157         * Make the CRTC code factor in the SDVO pixel multiplier.  The
1158         * SDVO device will factor out the multiplier during mode_set.
1159         */
1160        pipe_config->pixel_multiplier =
1161                intel_sdvo_get_pixel_multiplier(adjusted_mode);
1162
1163        if (intel_sdvo_state->base.force_audio != HDMI_AUDIO_OFF_DVI)
1164                pipe_config->has_hdmi_sink = intel_sdvo->has_hdmi_monitor;
1165
1166        if (intel_sdvo_state->base.force_audio == HDMI_AUDIO_ON ||
1167            (intel_sdvo_state->base.force_audio == HDMI_AUDIO_AUTO && intel_sdvo->has_hdmi_audio))
1168                pipe_config->has_audio = true;
1169
1170        if (intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
1171                /*
1172                 * See CEA-861-E - 5.1 Default Encoding Parameters
1173                 *
1174                 * FIXME: This bit is only valid when using TMDS encoding and 8
1175                 * bit per color mode.
1176                 */
1177                if (pipe_config->has_hdmi_sink &&
1178                    drm_match_cea_mode(adjusted_mode) > 1)
1179                        pipe_config->limited_color_range = true;
1180        } else {
1181                if (pipe_config->has_hdmi_sink &&
1182                    intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED)
1183                        pipe_config->limited_color_range = true;
1184        }
1185
1186        /* Clock computation needs to happen after pixel multiplier. */
1187        if (intel_sdvo->is_tv)
1188                i9xx_adjust_sdvo_tv_clock(pipe_config);
1189
1190        /* Set user selected PAR to incoming mode's member */
1191        if (intel_sdvo->is_hdmi)
1192                adjusted_mode->picture_aspect_ratio = conn_state->picture_aspect_ratio;
1193
1194        return true;
1195}
1196
1197#define UPDATE_PROPERTY(input, NAME) \
1198        do { \
1199                val = input; \
1200                intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_##NAME, &val, sizeof(val)); \
1201        } while (0)
1202
1203static void intel_sdvo_update_props(struct intel_sdvo *intel_sdvo,
1204                                    const struct intel_sdvo_connector_state *sdvo_state)
1205{
1206        const struct drm_connector_state *conn_state = &sdvo_state->base.base;
1207        struct intel_sdvo_connector *intel_sdvo_conn =
1208                to_intel_sdvo_connector(conn_state->connector);
1209        uint16_t val;
1210
1211        if (intel_sdvo_conn->left)
1212                UPDATE_PROPERTY(sdvo_state->tv.overscan_h, OVERSCAN_H);
1213
1214        if (intel_sdvo_conn->top)
1215                UPDATE_PROPERTY(sdvo_state->tv.overscan_v, OVERSCAN_V);
1216
1217        if (intel_sdvo_conn->hpos)
1218                UPDATE_PROPERTY(sdvo_state->tv.hpos, HPOS);
1219
1220        if (intel_sdvo_conn->vpos)
1221                UPDATE_PROPERTY(sdvo_state->tv.vpos, VPOS);
1222
1223        if (intel_sdvo_conn->saturation)
1224                UPDATE_PROPERTY(conn_state->tv.saturation, SATURATION);
1225
1226        if (intel_sdvo_conn->contrast)
1227                UPDATE_PROPERTY(conn_state->tv.contrast, CONTRAST);
1228
1229        if (intel_sdvo_conn->hue)
1230                UPDATE_PROPERTY(conn_state->tv.hue, HUE);
1231
1232        if (intel_sdvo_conn->brightness)
1233                UPDATE_PROPERTY(conn_state->tv.brightness, BRIGHTNESS);
1234
1235        if (intel_sdvo_conn->sharpness)
1236                UPDATE_PROPERTY(sdvo_state->tv.sharpness, SHARPNESS);
1237
1238        if (intel_sdvo_conn->flicker_filter)
1239                UPDATE_PROPERTY(sdvo_state->tv.flicker_filter, FLICKER_FILTER);
1240
1241        if (intel_sdvo_conn->flicker_filter_2d)
1242                UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_2d, FLICKER_FILTER_2D);
1243
1244        if (intel_sdvo_conn->flicker_filter_adaptive)
1245                UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
1246
1247        if (intel_sdvo_conn->tv_chroma_filter)
1248                UPDATE_PROPERTY(sdvo_state->tv.chroma_filter, TV_CHROMA_FILTER);
1249
1250        if (intel_sdvo_conn->tv_luma_filter)
1251                UPDATE_PROPERTY(sdvo_state->tv.luma_filter, TV_LUMA_FILTER);
1252
1253        if (intel_sdvo_conn->dot_crawl)
1254                UPDATE_PROPERTY(sdvo_state->tv.dot_crawl, DOT_CRAWL);
1255
1256#undef UPDATE_PROPERTY
1257}
1258
1259static void intel_sdvo_pre_enable(struct intel_encoder *intel_encoder,
1260                                  const struct intel_crtc_state *crtc_state,
1261                                  const struct drm_connector_state *conn_state)
1262{
1263        struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
1264        struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1265        const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
1266        const struct intel_sdvo_connector_state *sdvo_state =
1267                to_intel_sdvo_connector_state(conn_state);
1268        const struct drm_display_mode *mode = &crtc_state->base.mode;
1269        struct intel_sdvo *intel_sdvo = to_sdvo(intel_encoder);
1270        u32 sdvox;
1271        struct intel_sdvo_in_out_map in_out;
1272        struct intel_sdvo_dtd input_dtd, output_dtd;
1273        int rate;
1274
1275        intel_sdvo_update_props(intel_sdvo, sdvo_state);
1276
1277        /*
1278         * First, set the input mapping for the first input to our controlled
1279         * output. This is only correct if we're a single-input device, in
1280         * which case the first input is the output from the appropriate SDVO
1281         * channel on the motherboard.  In a two-input device, the first input
1282         * will be SDVOB and the second SDVOC.
1283         */
1284        in_out.in0 = intel_sdvo->attached_output;
1285        in_out.in1 = 0;
1286
1287        intel_sdvo_set_value(intel_sdvo,
1288                             SDVO_CMD_SET_IN_OUT_MAP,
1289                             &in_out, sizeof(in_out));
1290
1291        /* Set the output timings to the screen */
1292        if (!intel_sdvo_set_target_output(intel_sdvo,
1293                                          intel_sdvo->attached_output))
1294                return;
1295
1296        /* lvds has a special fixed output timing. */
1297        if (intel_sdvo->is_lvds)
1298                intel_sdvo_get_dtd_from_mode(&output_dtd,
1299                                             intel_sdvo->sdvo_lvds_fixed_mode);
1300        else
1301                intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1302        if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1303                DRM_INFO("Setting output timings on %s failed\n",
1304                         SDVO_NAME(intel_sdvo));
1305
1306        /* Set the input timing to the screen. Assume always input 0. */
1307        if (!intel_sdvo_set_target_input(intel_sdvo))
1308                return;
1309
1310        if (crtc_state->has_hdmi_sink) {
1311                intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
1312                intel_sdvo_set_colorimetry(intel_sdvo,
1313                                           SDVO_COLORIMETRY_RGB256);
1314                intel_sdvo_set_avi_infoframe(intel_sdvo, crtc_state);
1315        } else
1316                intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
1317
1318        if (intel_sdvo->is_tv &&
1319            !intel_sdvo_set_tv_format(intel_sdvo, conn_state))
1320                return;
1321
1322        intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
1323
1324        if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
1325                input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
1326        if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
1327                DRM_INFO("Setting input timings on %s failed\n",
1328                         SDVO_NAME(intel_sdvo));
1329
1330        switch (crtc_state->pixel_multiplier) {
1331        default:
1332                WARN(1, "unknown pixel multiplier specified\n");
1333        case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1334        case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1335        case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
1336        }
1337        if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
1338                return;
1339
1340        /* Set the SDVO control regs. */
1341        if (INTEL_GEN(dev_priv) >= 4) {
1342                /* The real mode polarity is set by the SDVO commands, using
1343                 * struct intel_sdvo_dtd. */
1344                sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
1345                if (!HAS_PCH_SPLIT(dev_priv) && crtc_state->limited_color_range)
1346                        sdvox |= HDMI_COLOR_RANGE_16_235;
1347                if (INTEL_GEN(dev_priv) < 5)
1348                        sdvox |= SDVO_BORDER_ENABLE;
1349        } else {
1350                sdvox = I915_READ(intel_sdvo->sdvo_reg);
1351                if (intel_sdvo->port == PORT_B)
1352                        sdvox &= SDVOB_PRESERVE_MASK;
1353                else
1354                        sdvox &= SDVOC_PRESERVE_MASK;
1355                sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1356        }
1357
1358        if (HAS_PCH_CPT(dev_priv))
1359                sdvox |= SDVO_PIPE_SEL_CPT(crtc->pipe);
1360        else
1361                sdvox |= SDVO_PIPE_SEL(crtc->pipe);
1362
1363        if (crtc_state->has_audio) {
1364                WARN_ON_ONCE(INTEL_GEN(dev_priv) < 4);
1365                sdvox |= SDVO_AUDIO_ENABLE;
1366        }
1367
1368        if (INTEL_GEN(dev_priv) >= 4) {
1369                /* done in crtc_mode_set as the dpll_md reg must be written early */
1370        } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
1371                   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
1372                /* done in crtc_mode_set as it lives inside the dpll register */
1373        } else {
1374                sdvox |= (crtc_state->pixel_multiplier - 1)
1375                        << SDVO_PORT_MULTIPLY_SHIFT;
1376        }
1377
1378        if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
1379            INTEL_GEN(dev_priv) < 5)
1380                sdvox |= SDVO_STALL_SELECT;
1381        intel_sdvo_write_sdvox(intel_sdvo, sdvox);
1382}
1383
1384static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
1385{
1386        struct intel_sdvo_connector *intel_sdvo_connector =
1387                to_intel_sdvo_connector(&connector->base);
1388        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
1389        u16 active_outputs = 0;
1390
1391        intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
1392
1393        if (active_outputs & intel_sdvo_connector->output_flag)
1394                return true;
1395        else
1396                return false;
1397}
1398
1399static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
1400                                    enum pipe *pipe)
1401{
1402        struct drm_device *dev = encoder->base.dev;
1403        struct drm_i915_private *dev_priv = to_i915(dev);
1404        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1405        u16 active_outputs = 0;
1406        u32 tmp;
1407
1408        tmp = I915_READ(intel_sdvo->sdvo_reg);
1409        intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
1410
1411        if (!(tmp & SDVO_ENABLE) && (active_outputs == 0))
1412                return false;
1413
1414        if (HAS_PCH_CPT(dev_priv))
1415                *pipe = PORT_TO_PIPE_CPT(tmp);
1416        else
1417                *pipe = PORT_TO_PIPE(tmp);
1418
1419        return true;
1420}
1421
1422static void intel_sdvo_get_config(struct intel_encoder *encoder,
1423                                  struct intel_crtc_state *pipe_config)
1424{
1425        struct drm_device *dev = encoder->base.dev;
1426        struct drm_i915_private *dev_priv = to_i915(dev);
1427        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1428        struct intel_sdvo_dtd dtd;
1429        int encoder_pixel_multiplier = 0;
1430        int dotclock;
1431        u32 flags = 0, sdvox;
1432        u8 val;
1433        bool ret;
1434
1435        pipe_config->output_types |= BIT(INTEL_OUTPUT_SDVO);
1436
1437        sdvox = I915_READ(intel_sdvo->sdvo_reg);
1438
1439        ret = intel_sdvo_get_input_timing(intel_sdvo, &dtd);
1440        if (!ret) {
1441                /*
1442                 * Some sdvo encoders are not spec compliant and don't
1443                 * implement the mandatory get_timings function.
1444                 */
1445                DRM_DEBUG_DRIVER("failed to retrieve SDVO DTD\n");
1446                pipe_config->quirks |= PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS;
1447        } else {
1448                if (dtd.part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
1449                        flags |= DRM_MODE_FLAG_PHSYNC;
1450                else
1451                        flags |= DRM_MODE_FLAG_NHSYNC;
1452
1453                if (dtd.part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
1454                        flags |= DRM_MODE_FLAG_PVSYNC;
1455                else
1456                        flags |= DRM_MODE_FLAG_NVSYNC;
1457        }
1458
1459        pipe_config->base.adjusted_mode.flags |= flags;
1460
1461        /*
1462         * pixel multiplier readout is tricky: Only on i915g/gm it is stored in
1463         * the sdvo port register, on all other platforms it is part of the dpll
1464         * state. Since the general pipe state readout happens before the
1465         * encoder->get_config we so already have a valid pixel multplier on all
1466         * other platfroms.
1467         */
1468        if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
1469                pipe_config->pixel_multiplier =
1470                        ((sdvox & SDVO_PORT_MULTIPLY_MASK)
1471                         >> SDVO_PORT_MULTIPLY_SHIFT) + 1;
1472        }
1473
1474        dotclock = pipe_config->port_clock;
1475
1476        if (pipe_config->pixel_multiplier)
1477                dotclock /= pipe_config->pixel_multiplier;
1478
1479        pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1480
1481        /* Cross check the port pixel multiplier with the sdvo encoder state. */
1482        if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_CLOCK_RATE_MULT,
1483                                 &val, 1)) {
1484                switch (val) {
1485                case SDVO_CLOCK_RATE_MULT_1X:
1486                        encoder_pixel_multiplier = 1;
1487                        break;
1488                case SDVO_CLOCK_RATE_MULT_2X:
1489                        encoder_pixel_multiplier = 2;
1490                        break;
1491                case SDVO_CLOCK_RATE_MULT_4X:
1492                        encoder_pixel_multiplier = 4;
1493                        break;
1494                }
1495        }
1496
1497        if (sdvox & HDMI_COLOR_RANGE_16_235)
1498                pipe_config->limited_color_range = true;
1499
1500        if (sdvox & SDVO_AUDIO_ENABLE)
1501                pipe_config->has_audio = true;
1502
1503        if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_ENCODE,
1504                                 &val, 1)) {
1505                if (val == SDVO_ENCODE_HDMI)
1506                        pipe_config->has_hdmi_sink = true;
1507        }
1508
1509        WARN(encoder_pixel_multiplier != pipe_config->pixel_multiplier,
1510             "SDVO pixel multiplier mismatch, port: %i, encoder: %i\n",
1511             pipe_config->pixel_multiplier, encoder_pixel_multiplier);
1512}
1513
1514static void intel_disable_sdvo(struct intel_encoder *encoder,
1515                               const struct intel_crtc_state *old_crtc_state,
1516                               const struct drm_connector_state *conn_state)
1517{
1518        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1519        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1520        struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
1521        u32 temp;
1522
1523        intel_sdvo_set_active_outputs(intel_sdvo, 0);
1524        if (0)
1525                intel_sdvo_set_encoder_power_state(intel_sdvo,
1526                                                   DRM_MODE_DPMS_OFF);
1527
1528        temp = I915_READ(intel_sdvo->sdvo_reg);
1529
1530        temp &= ~SDVO_ENABLE;
1531        intel_sdvo_write_sdvox(intel_sdvo, temp);
1532
1533        /*
1534         * HW workaround for IBX, we need to move the port
1535         * to transcoder A after disabling it to allow the
1536         * matching DP port to be enabled on transcoder A.
1537         */
1538        if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B) {
1539                /*
1540                 * We get CPU/PCH FIFO underruns on the other pipe when
1541                 * doing the workaround. Sweep them under the rug.
1542                 */
1543                intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
1544                intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
1545
1546                temp &= ~SDVO_PIPE_B_SELECT;
1547                temp |= SDVO_ENABLE;
1548                intel_sdvo_write_sdvox(intel_sdvo, temp);
1549
1550                temp &= ~SDVO_ENABLE;
1551                intel_sdvo_write_sdvox(intel_sdvo, temp);
1552
1553                intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
1554                intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
1555                intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
1556        }
1557}
1558
1559static void pch_disable_sdvo(struct intel_encoder *encoder,
1560                             const struct intel_crtc_state *old_crtc_state,
1561                             const struct drm_connector_state *old_conn_state)
1562{
1563}
1564
1565static void pch_post_disable_sdvo(struct intel_encoder *encoder,
1566                                  const struct intel_crtc_state *old_crtc_state,
1567                                  const struct drm_connector_state *old_conn_state)
1568{
1569        intel_disable_sdvo(encoder, old_crtc_state, old_conn_state);
1570}
1571
1572static void intel_enable_sdvo(struct intel_encoder *encoder,
1573                              const struct intel_crtc_state *pipe_config,
1574                              const struct drm_connector_state *conn_state)
1575{
1576        struct drm_device *dev = encoder->base.dev;
1577        struct drm_i915_private *dev_priv = to_i915(dev);
1578        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1579        struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
1580        u32 temp;
1581        bool input1, input2;
1582        int i;
1583        bool success;
1584
1585        temp = I915_READ(intel_sdvo->sdvo_reg);
1586        temp |= SDVO_ENABLE;
1587        intel_sdvo_write_sdvox(intel_sdvo, temp);
1588
1589        for (i = 0; i < 2; i++)
1590                intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
1591
1592        success = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
1593        /*
1594         * Warn if the device reported failure to sync.
1595         *
1596         * A lot of SDVO devices fail to notify of sync, but it's
1597         * a given it the status is a success, we succeeded.
1598         */
1599        if (success && !input1) {
1600                DRM_DEBUG_KMS("First %s output reported failure to "
1601                                "sync\n", SDVO_NAME(intel_sdvo));
1602        }
1603
1604        if (0)
1605                intel_sdvo_set_encoder_power_state(intel_sdvo,
1606                                                   DRM_MODE_DPMS_ON);
1607        intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
1608}
1609
1610static enum drm_mode_status
1611intel_sdvo_mode_valid(struct drm_connector *connector,
1612                      struct drm_display_mode *mode)
1613{
1614        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1615        int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
1616
1617        if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
1618                return MODE_NO_DBLESCAN;
1619
1620        if (intel_sdvo->pixel_clock_min > mode->clock)
1621                return MODE_CLOCK_LOW;
1622
1623        if (intel_sdvo->pixel_clock_max < mode->clock)
1624                return MODE_CLOCK_HIGH;
1625
1626        if (mode->clock > max_dotclk)
1627                return MODE_CLOCK_HIGH;
1628
1629        if (intel_sdvo->is_lvds) {
1630                if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
1631                        return MODE_PANEL;
1632
1633                if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
1634                        return MODE_PANEL;
1635        }
1636
1637        return MODE_OK;
1638}
1639
1640static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
1641{
1642        BUILD_BUG_ON(sizeof(*caps) != 8);
1643        if (!intel_sdvo_get_value(intel_sdvo,
1644                                  SDVO_CMD_GET_DEVICE_CAPS,
1645                                  caps, sizeof(*caps)))
1646                return false;
1647
1648        DRM_DEBUG_KMS("SDVO capabilities:\n"
1649                      "  vendor_id: %d\n"
1650                      "  device_id: %d\n"
1651                      "  device_rev_id: %d\n"
1652                      "  sdvo_version_major: %d\n"
1653                      "  sdvo_version_minor: %d\n"
1654                      "  sdvo_inputs_mask: %d\n"
1655                      "  smooth_scaling: %d\n"
1656                      "  sharp_scaling: %d\n"
1657                      "  up_scaling: %d\n"
1658                      "  down_scaling: %d\n"
1659                      "  stall_support: %d\n"
1660                      "  output_flags: %d\n",
1661                      caps->vendor_id,
1662                      caps->device_id,
1663                      caps->device_rev_id,
1664                      caps->sdvo_version_major,
1665                      caps->sdvo_version_minor,
1666                      caps->sdvo_inputs_mask,
1667                      caps->smooth_scaling,
1668                      caps->sharp_scaling,
1669                      caps->up_scaling,
1670                      caps->down_scaling,
1671                      caps->stall_support,
1672                      caps->output_flags);
1673
1674        return true;
1675}
1676
1677static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
1678{
1679        struct drm_i915_private *dev_priv = to_i915(intel_sdvo->base.base.dev);
1680        uint16_t hotplug;
1681
1682        if (!I915_HAS_HOTPLUG(dev_priv))
1683                return 0;
1684
1685        /*
1686         * HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
1687         * on the line.
1688         */
1689        if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
1690                return 0;
1691
1692        if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1693                                        &hotplug, sizeof(hotplug)))
1694                return 0;
1695
1696        return hotplug;
1697}
1698
1699static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
1700{
1701        struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
1702
1703        intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
1704                             &intel_sdvo->hotplug_active, 2);
1705}
1706
1707static bool intel_sdvo_hotplug(struct intel_encoder *encoder,
1708                               struct intel_connector *connector)
1709{
1710        intel_sdvo_enable_hotplug(encoder);
1711
1712        return intel_encoder_hotplug(encoder, connector);
1713}
1714
1715static bool
1716intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
1717{
1718        /* Is there more than one type of output? */
1719        return hweight16(intel_sdvo->caps.output_flags) > 1;
1720}
1721
1722static struct edid *
1723intel_sdvo_get_edid(struct drm_connector *connector)
1724{
1725        struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
1726        return drm_get_edid(connector, &sdvo->ddc);
1727}
1728
1729/* Mac mini hack -- use the same DDC as the analog connector */
1730static struct edid *
1731intel_sdvo_get_analog_edid(struct drm_connector *connector)
1732{
1733        struct drm_i915_private *dev_priv = to_i915(connector->dev);
1734
1735        return drm_get_edid(connector,
1736                            intel_gmbus_get_adapter(dev_priv,
1737                                                    dev_priv->vbt.crt_ddc_pin));
1738}
1739
1740static enum drm_connector_status
1741intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
1742{
1743        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1744        enum drm_connector_status status;
1745        struct edid *edid;
1746
1747        edid = intel_sdvo_get_edid(connector);
1748
1749        if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
1750                u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
1751
1752                /*
1753                 * Don't use the 1 as the argument of DDC bus switch to get
1754                 * the EDID. It is used for SDVO SPD ROM.
1755                 */
1756                for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
1757                        intel_sdvo->ddc_bus = ddc;
1758                        edid = intel_sdvo_get_edid(connector);
1759                        if (edid)
1760                                break;
1761                }
1762                /*
1763                 * If we found the EDID on the other bus,
1764                 * assume that is the correct DDC bus.
1765                 */
1766                if (edid == NULL)
1767                        intel_sdvo->ddc_bus = saved_ddc;
1768        }
1769
1770        /*
1771         * When there is no edid and no monitor is connected with VGA
1772         * port, try to use the CRT ddc to read the EDID for DVI-connector.
1773         */
1774        if (edid == NULL)
1775                edid = intel_sdvo_get_analog_edid(connector);
1776
1777        status = connector_status_unknown;
1778        if (edid != NULL) {
1779                /* DDC bus is shared, match EDID to connector type */
1780                if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1781                        status = connector_status_connected;
1782                        if (intel_sdvo->is_hdmi) {
1783                                intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1784                                intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
1785                                intel_sdvo->rgb_quant_range_selectable =
1786                                        drm_rgb_quant_range_selectable(edid);
1787                        }
1788                } else
1789                        status = connector_status_disconnected;
1790                kfree(edid);
1791        }
1792
1793        return status;
1794}
1795
1796static bool
1797intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
1798                                  struct edid *edid)
1799{
1800        bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1801        bool connector_is_digital = !!IS_DIGITAL(sdvo);
1802
1803        DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
1804                      connector_is_digital, monitor_is_digital);
1805        return connector_is_digital == monitor_is_digital;
1806}
1807
1808static enum drm_connector_status
1809intel_sdvo_detect(struct drm_connector *connector, bool force)
1810{
1811        uint16_t response;
1812        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1813        struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
1814        enum drm_connector_status ret;
1815
1816        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1817                      connector->base.id, connector->name);
1818
1819        if (!intel_sdvo_get_value(intel_sdvo,
1820                                  SDVO_CMD_GET_ATTACHED_DISPLAYS,
1821                                  &response, 2))
1822                return connector_status_unknown;
1823
1824        DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1825                      response & 0xff, response >> 8,
1826                      intel_sdvo_connector->output_flag);
1827
1828        if (response == 0)
1829                return connector_status_disconnected;
1830
1831        intel_sdvo->attached_output = response;
1832
1833        intel_sdvo->has_hdmi_monitor = false;
1834        intel_sdvo->has_hdmi_audio = false;
1835        intel_sdvo->rgb_quant_range_selectable = false;
1836
1837        if ((intel_sdvo_connector->output_flag & response) == 0)
1838                ret = connector_status_disconnected;
1839        else if (IS_TMDS(intel_sdvo_connector))
1840                ret = intel_sdvo_tmds_sink_detect(connector);
1841        else {
1842                struct edid *edid;
1843
1844                /* if we have an edid check it matches the connection */
1845                edid = intel_sdvo_get_edid(connector);
1846                if (edid == NULL)
1847                        edid = intel_sdvo_get_analog_edid(connector);
1848                if (edid != NULL) {
1849                        if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
1850                                                              edid))
1851                                ret = connector_status_connected;
1852                        else
1853                                ret = connector_status_disconnected;
1854
1855                        kfree(edid);
1856                } else
1857                        ret = connector_status_connected;
1858        }
1859
1860        /* May update encoder flag for like clock for SDVO TV, etc.*/
1861        if (ret == connector_status_connected) {
1862                intel_sdvo->is_tv = false;
1863                intel_sdvo->is_lvds = false;
1864
1865                if (response & SDVO_TV_MASK)
1866                        intel_sdvo->is_tv = true;
1867                if (response & SDVO_LVDS_MASK)
1868                        intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
1869        }
1870
1871        return ret;
1872}
1873
1874static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
1875{
1876        struct edid *edid;
1877
1878        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1879                      connector->base.id, connector->name);
1880
1881        /* set the bus switch and get the modes */
1882        edid = intel_sdvo_get_edid(connector);
1883
1884        /*
1885         * Mac mini hack.  On this device, the DVI-I connector shares one DDC
1886         * link between analog and digital outputs. So, if the regular SDVO
1887         * DDC fails, check to see if the analog output is disconnected, in
1888         * which case we'll look there for the digital DDC data.
1889         */
1890        if (edid == NULL)
1891                edid = intel_sdvo_get_analog_edid(connector);
1892
1893        if (edid != NULL) {
1894                if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
1895                                                      edid)) {
1896                        drm_mode_connector_update_edid_property(connector, edid);
1897                        drm_add_edid_modes(connector, edid);
1898                }
1899
1900                kfree(edid);
1901        }
1902}
1903
1904/*
1905 * Set of SDVO TV modes.
1906 * Note!  This is in reply order (see loop in get_tv_modes).
1907 * XXX: all 60Hz refresh?
1908 */
1909static const struct drm_display_mode sdvo_tv_modes[] = {
1910        { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1911                   416, 0, 200, 201, 232, 233, 0,
1912                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1913        { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1914                   416, 0, 240, 241, 272, 273, 0,
1915                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1916        { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1917                   496, 0, 300, 301, 332, 333, 0,
1918                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1919        { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1920                   736, 0, 350, 351, 382, 383, 0,
1921                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1922        { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1923                   736, 0, 400, 401, 432, 433, 0,
1924                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1925        { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1926                   736, 0, 480, 481, 512, 513, 0,
1927                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1928        { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1929                   800, 0, 480, 481, 512, 513, 0,
1930                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1931        { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1932                   800, 0, 576, 577, 608, 609, 0,
1933                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1934        { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1935                   816, 0, 350, 351, 382, 383, 0,
1936                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1937        { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1938                   816, 0, 400, 401, 432, 433, 0,
1939                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1940        { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1941                   816, 0, 480, 481, 512, 513, 0,
1942                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1943        { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1944                   816, 0, 540, 541, 572, 573, 0,
1945                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1946        { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1947                   816, 0, 576, 577, 608, 609, 0,
1948                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1949        { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1950                   864, 0, 576, 577, 608, 609, 0,
1951                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1952        { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1953                   896, 0, 600, 601, 632, 633, 0,
1954                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1955        { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1956                   928, 0, 624, 625, 656, 657, 0,
1957                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1958        { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1959                   1016, 0, 766, 767, 798, 799, 0,
1960                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1961        { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1962                   1120, 0, 768, 769, 800, 801, 0,
1963                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1964        { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1965                   1376, 0, 1024, 1025, 1056, 1057, 0,
1966                   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1967};
1968
1969static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1970{
1971        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
1972        const struct drm_connector_state *conn_state = connector->state;
1973        struct intel_sdvo_sdtv_resolution_request tv_res;
1974        uint32_t reply = 0, format_map = 0;
1975        int i;
1976
1977        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1978                      connector->base.id, connector->name);
1979
1980        /*
1981         * Read the list of supported input resolutions for the selected TV
1982         * format.
1983         */
1984        format_map = 1 << conn_state->tv.mode;
1985        memcpy(&tv_res, &format_map,
1986               min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
1987
1988        if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
1989                return;
1990
1991        BUILD_BUG_ON(sizeof(tv_res) != 3);
1992        if (!intel_sdvo_write_cmd(intel_sdvo,
1993                                  SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
1994                                  &tv_res, sizeof(tv_res)))
1995                return;
1996        if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
1997                return;
1998
1999        for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
2000                if (reply & (1 << i)) {
2001                        struct drm_display_mode *nmode;
2002                        nmode = drm_mode_duplicate(connector->dev,
2003                                                   &sdvo_tv_modes[i]);
2004                        if (nmode)
2005                                drm_mode_probed_add(connector, nmode);
2006                }
2007}
2008
2009static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
2010{
2011        struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
2012        struct drm_i915_private *dev_priv = to_i915(connector->dev);
2013        struct drm_display_mode *newmode;
2014
2015        DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2016                      connector->base.id, connector->name);
2017
2018        /*
2019         * Fetch modes from VBT. For SDVO prefer the VBT mode since some
2020         * SDVO->LVDS transcoders can't cope with the EDID mode.
2021         */
2022        if (dev_priv->vbt.sdvo_lvds_vbt_mode != NULL) {
2023                newmode = drm_mode_duplicate(connector->dev,
2024                                             dev_priv->vbt.sdvo_lvds_vbt_mode);
2025                if (newmode != NULL) {
2026                        /* Guarantee the mode is preferred */
2027                        newmode->type = (DRM_MODE_TYPE_PREFERRED |
2028                                         DRM_MODE_TYPE_DRIVER);
2029                        drm_mode_probed_add(connector, newmode);
2030                }
2031        }
2032
2033        /*
2034         * Attempt to get the mode list from DDC.
2035         * Assume that the preferred modes are
2036         * arranged in priority order.
2037         */
2038        intel_ddc_get_modes(connector, &intel_sdvo->ddc);
2039
2040        list_for_each_entry(newmode, &connector->probed_modes, head) {
2041                if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
2042                        intel_sdvo->sdvo_lvds_fixed_mode =
2043                                drm_mode_duplicate(connector->dev, newmode);
2044
2045                        intel_sdvo->is_lvds = true;
2046                        break;
2047                }
2048        }
2049}
2050
2051static int intel_sdvo_get_modes(struct drm_connector *connector)
2052{
2053        struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
2054
2055        if (IS_TV(intel_sdvo_connector))
2056                intel_sdvo_get_tv_modes(connector);
2057        else if (IS_LVDS(intel_sdvo_connector))
2058                intel_sdvo_get_lvds_modes(connector);
2059        else
2060                intel_sdvo_get_ddc_modes(connector);
2061
2062        return !list_empty(&connector->probed_modes);
2063}
2064
2065static void intel_sdvo_destroy(struct drm_connector *connector)
2066{
2067        struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
2068
2069        drm_connector_cleanup(connector);
2070        kfree(intel_sdvo_connector);
2071}
2072
2073static int
2074intel_sdvo_connector_atomic_get_property(struct drm_connector *connector,
2075                                         const struct drm_connector_state *state,
2076                                         struct drm_property *property,
2077                                         uint64_t *val)
2078{
2079        struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
2080        const struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state((void *)state);
2081
2082        if (property == intel_sdvo_connector->tv_format) {
2083                int i;
2084
2085                for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
2086                        if (state->tv.mode == intel_sdvo_connector->tv_format_supported[i]) {
2087                                *val = i;
2088
2089                                return 0;
2090                        }
2091
2092                WARN_ON(1);
2093                *val = 0;
2094        } else if (property == intel_sdvo_connector->top ||
2095                   property == intel_sdvo_connector->bottom)
2096                *val = intel_sdvo_connector->max_vscan - sdvo_state->tv.overscan_v;
2097        else if (property == intel_sdvo_connector->left ||
2098                 property == intel_sdvo_connector->right)
2099                *val = intel_sdvo_connector->max_hscan - sdvo_state->tv.overscan_h;
2100        else if (property == intel_sdvo_connector->hpos)
2101                *val = sdvo_state->tv.hpos;
2102        else if (property == intel_sdvo_connector->vpos)
2103                *val = sdvo_state->tv.vpos;
2104        else if (property == intel_sdvo_connector->saturation)
2105                *val = state->tv.saturation;
2106        else if (property == intel_sdvo_connector->contrast)
2107                *val = state->tv.contrast;
2108        else if (property == intel_sdvo_connector->hue)
2109                *val = state->tv.hue;
2110        else if (property == intel_sdvo_connector->brightness)
2111                *val = state->tv.brightness;
2112        else if (property == intel_sdvo_connector->sharpness)
2113                *val = sdvo_state->tv.sharpness;
2114        else if (property == intel_sdvo_connector->flicker_filter)
2115                *val = sdvo_state->tv.flicker_filter;
2116        else if (property == intel_sdvo_connector->flicker_filter_2d)
2117                *val = sdvo_state->tv.flicker_filter_2d;
2118        else if (property == intel_sdvo_connector->flicker_filter_adaptive)
2119                *val = sdvo_state->tv.flicker_filter_adaptive;
2120        else if (property == intel_sdvo_connector->tv_chroma_filter)
2121                *val = sdvo_state->tv.chroma_filter;
2122        else if (property == intel_sdvo_connector->tv_luma_filter)
2123                *val = sdvo_state->tv.luma_filter;
2124        else if (property == intel_sdvo_connector->dot_crawl)
2125                *val = sdvo_state->tv.dot_crawl;
2126        else
2127                return intel_digital_connector_atomic_get_property(connector, state, property, val);
2128
2129        return 0;
2130}
2131
2132static int
2133intel_sdvo_connector_atomic_set_property(struct drm_connector *connector,
2134                                         struct drm_connector_state *state,
2135                                         struct drm_property *property,
2136                                         uint64_t val)
2137{
2138        struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
2139        struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state(state);
2140
2141        if (property == intel_sdvo_connector->tv_format) {
2142                state->tv.mode = intel_sdvo_connector->tv_format_supported[val];
2143
2144                if (state->crtc) {
2145                        struct drm_crtc_state *crtc_state =
2146                                drm_atomic_get_new_crtc_state(state->state, state->crtc);
2147
2148                        crtc_state->connectors_changed = true;
2149                }
2150        } else if (property == intel_sdvo_connector->top ||
2151                   property == intel_sdvo_connector->bottom)
2152                /* Cannot set these independent from each other */
2153                sdvo_state->tv.overscan_v = intel_sdvo_connector->max_vscan - val;
2154        else if (property == intel_sdvo_connector->left ||
2155                 property == intel_sdvo_connector->right)
2156                /* Cannot set these independent from each other */
2157                sdvo_state->tv.overscan_h = intel_sdvo_connector->max_hscan - val;
2158        else if (property == intel_sdvo_connector->hpos)
2159                sdvo_state->tv.hpos = val;
2160        else if (property == intel_sdvo_connector->vpos)
2161                sdvo_state->tv.vpos = val;
2162        else if (property == intel_sdvo_connector->saturation)
2163                state->tv.saturation = val;
2164        else if (property == intel_sdvo_connector->contrast)
2165                state->tv.contrast = val;
2166        else if (property == intel_sdvo_connector->hue)
2167                state->tv.hue = val;
2168        else if (property == intel_sdvo_connector->brightness)
2169                state->tv.brightness = val;
2170        else if (property == intel_sdvo_connector->sharpness)
2171                sdvo_state->tv.sharpness = val;
2172        else if (property == intel_sdvo_connector->flicker_filter)
2173                sdvo_state->tv.flicker_filter = val;
2174        else if (property == intel_sdvo_connector->flicker_filter_2d)
2175                sdvo_state->tv.flicker_filter_2d = val;
2176        else if (property == intel_sdvo_connector->flicker_filter_adaptive)
2177                sdvo_state->tv.flicker_filter_adaptive = val;
2178        else if (property == intel_sdvo_connector->tv_chroma_filter)
2179                sdvo_state->tv.chroma_filter = val;
2180        else if (property == intel_sdvo_connector->tv_luma_filter)
2181                sdvo_state->tv.luma_filter = val;
2182        else if (property == intel_sdvo_connector->dot_crawl)
2183                sdvo_state->tv.dot_crawl = val;
2184        else
2185                return intel_digital_connector_atomic_set_property(connector, state, property, val);
2186
2187        return 0;
2188}
2189
2190static int
2191intel_sdvo_connector_register(struct drm_connector *connector)
2192{
2193        struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
2194        int ret;
2195
2196        ret = intel_connector_register(connector);
2197        if (ret)
2198                return ret;
2199
2200        return sysfs_create_link(&connector->kdev->kobj,
2201                                 &sdvo->ddc.dev.kobj,
2202                                 sdvo->ddc.dev.kobj.name);
2203}
2204
2205static void
2206intel_sdvo_connector_unregister(struct drm_connector *connector)
2207{
2208        struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
2209
2210        sysfs_remove_link(&connector->kdev->kobj,
2211                          sdvo->ddc.dev.kobj.name);
2212        intel_connector_unregister(connector);
2213}
2214
2215static struct drm_connector_state *
2216intel_sdvo_connector_duplicate_state(struct drm_connector *connector)
2217{
2218        struct intel_sdvo_connector_state *state;
2219
2220        state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
2221        if (!state)
2222                return NULL;
2223
2224        __drm_atomic_helper_connector_duplicate_state(connector, &state->base.base);
2225        return &state->base.base;
2226}
2227
2228static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
2229        .detect = intel_sdvo_detect,
2230        .fill_modes = drm_helper_probe_single_connector_modes,
2231        .atomic_get_property = intel_sdvo_connector_atomic_get_property,
2232        .atomic_set_property = intel_sdvo_connector_atomic_set_property,
2233        .late_register = intel_sdvo_connector_register,
2234        .early_unregister = intel_sdvo_connector_unregister,
2235        .destroy = intel_sdvo_destroy,
2236        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
2237        .atomic_duplicate_state = intel_sdvo_connector_duplicate_state,
2238};
2239
2240static int intel_sdvo_atomic_check(struct drm_connector *conn,
2241                                   struct drm_connector_state *new_conn_state)
2242{
2243        struct drm_atomic_state *state = new_conn_state->state;
2244        struct drm_connector_state *old_conn_state =
2245                drm_atomic_get_old_connector_state(state, conn);
2246        struct intel_sdvo_connector_state *old_state =
2247                to_intel_sdvo_connector_state(old_conn_state);
2248        struct intel_sdvo_connector_state *new_state =
2249                to_intel_sdvo_connector_state(new_conn_state);
2250
2251        if (new_conn_state->crtc &&
2252            (memcmp(&old_state->tv, &new_state->tv, sizeof(old_state->tv)) ||
2253             memcmp(&old_conn_state->tv, &new_conn_state->tv, sizeof(old_conn_state->tv)))) {
2254                struct drm_crtc_state *crtc_state =
2255                        drm_atomic_get_new_crtc_state(new_conn_state->state,
2256                                                      new_conn_state->crtc);
2257
2258                crtc_state->connectors_changed = true;
2259        }
2260
2261        return intel_digital_connector_atomic_check(conn, new_conn_state);
2262}
2263
2264static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2265        .get_modes = intel_sdvo_get_modes,
2266        .mode_valid = intel_sdvo_mode_valid,
2267        .atomic_check = intel_sdvo_atomic_check,
2268};
2269
2270static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
2271{
2272        struct intel_sdvo *intel_sdvo = to_sdvo(to_intel_encoder(encoder));
2273
2274        if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
2275                drm_mode_destroy(encoder->dev,
2276                                 intel_sdvo->sdvo_lvds_fixed_mode);
2277
2278        i2c_del_adapter(&intel_sdvo->ddc);
2279        intel_encoder_destroy(encoder);
2280}
2281
2282static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2283        .destroy = intel_sdvo_enc_destroy,
2284};
2285
2286static void
2287intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
2288{
2289        uint16_t mask = 0;
2290        unsigned int num_bits;
2291
2292        /*
2293         * Make a mask of outputs less than or equal to our own priority in the
2294         * list.
2295         */
2296        switch (sdvo->controlled_output) {
2297        case SDVO_OUTPUT_LVDS1:
2298                mask |= SDVO_OUTPUT_LVDS1;
2299        case SDVO_OUTPUT_LVDS0:
2300                mask |= SDVO_OUTPUT_LVDS0;
2301        case SDVO_OUTPUT_TMDS1:
2302                mask |= SDVO_OUTPUT_TMDS1;
2303        case SDVO_OUTPUT_TMDS0:
2304                mask |= SDVO_OUTPUT_TMDS0;
2305        case SDVO_OUTPUT_RGB1:
2306                mask |= SDVO_OUTPUT_RGB1;
2307        case SDVO_OUTPUT_RGB0:
2308                mask |= SDVO_OUTPUT_RGB0;
2309                break;
2310        }
2311
2312        /* Count bits to find what number we are in the priority list. */
2313        mask &= sdvo->caps.output_flags;
2314        num_bits = hweight16(mask);
2315        /* If more than 3 outputs, default to DDC bus 3 for now. */
2316        if (num_bits > 3)
2317                num_bits = 3;
2318
2319        /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2320        sdvo->ddc_bus = 1 << num_bits;
2321}
2322
2323/*
2324 * Choose the appropriate DDC bus for control bus switch command for this
2325 * SDVO output based on the controlled output.
2326 *
2327 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2328 * outputs, then LVDS outputs.
2329 */
2330static void
2331intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
2332                          struct intel_sdvo *sdvo)
2333{
2334        struct sdvo_device_mapping *mapping;
2335
2336        if (sdvo->port == PORT_B)
2337                mapping = &dev_priv->vbt.sdvo_mappings[0];
2338        else
2339                mapping = &dev_priv->vbt.sdvo_mappings[1];
2340
2341        if (mapping->initialized)
2342                sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
2343        else
2344                intel_sdvo_guess_ddc_bus(sdvo);
2345}
2346
2347static void
2348intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
2349                          struct intel_sdvo *sdvo)
2350{
2351        struct sdvo_device_mapping *mapping;
2352        u8 pin;
2353
2354        if (sdvo->port == PORT_B)
2355                mapping = &dev_priv->vbt.sdvo_mappings[0];
2356        else
2357                mapping = &dev_priv->vbt.sdvo_mappings[1];
2358
2359        if (mapping->initialized &&
2360            intel_gmbus_is_valid_pin(dev_priv, mapping->i2c_pin))
2361                pin = mapping->i2c_pin;
2362        else
2363                pin = GMBUS_PIN_DPB;
2364
2365        sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
2366
2367        /*
2368         * With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
2369         * our code totally fails once we start using gmbus. Hence fall back to
2370         * bit banging for now.
2371         */
2372        intel_gmbus_force_bit(sdvo->i2c, true);
2373}
2374
2375/* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
2376static void
2377intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
2378{
2379        intel_gmbus_force_bit(sdvo->i2c, false);
2380}
2381
2382static bool
2383intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
2384{
2385        return intel_sdvo_check_supp_encode(intel_sdvo);
2386}
2387
2388static u8
2389intel_sdvo_get_slave_addr(struct drm_i915_private *dev_priv,
2390                          struct intel_sdvo *sdvo)
2391{
2392        struct sdvo_device_mapping *my_mapping, *other_mapping;
2393
2394        if (sdvo->port == PORT_B) {
2395                my_mapping = &dev_priv->vbt.sdvo_mappings[0];
2396                other_mapping = &dev_priv->vbt.sdvo_mappings[1];
2397        } else {
2398                my_mapping = &dev_priv->vbt.sdvo_mappings[1];
2399                other_mapping = &dev_priv->vbt.sdvo_mappings[0];
2400        }
2401
2402        /* If the BIOS described our SDVO device, take advantage of it. */
2403        if (my_mapping->slave_addr)
2404                return my_mapping->slave_addr;
2405
2406        /*
2407         * If the BIOS only described a different SDVO device, use the
2408         * address that it isn't using.
2409         */
2410        if (other_mapping->slave_addr) {
2411                if (other_mapping->slave_addr == 0x70)
2412                        return 0x72;
2413                else
2414                        return 0x70;
2415        }
2416
2417        /*
2418         * No SDVO device info is found for another DVO port,
2419         * so use mapping assumption we had before BIOS parsing.
2420         */
2421        if (sdvo->port == PORT_B)
2422                return 0x70;
2423        else
2424                return 0x72;
2425}
2426
2427static int
2428intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
2429                          struct intel_sdvo *encoder)
2430{
2431        struct drm_connector *drm_connector;
2432        int ret;
2433
2434        drm_connector = &connector->base.base;
2435        ret = drm_connector_init(encoder->base.base.dev,
2436                           drm_connector,
2437                           &intel_sdvo_connector_funcs,
2438                           connector->base.base.connector_type);
2439        if (ret < 0)
2440                return ret;
2441
2442        drm_connector_helper_add(drm_connector,
2443                                 &intel_sdvo_connector_helper_funcs);
2444
2445        connector->base.base.interlace_allowed = 1;
2446        connector->base.base.doublescan_allowed = 0;
2447        connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
2448        connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
2449
2450        intel_connector_attach_encoder(&connector->base, &encoder->base);
2451
2452        return 0;
2453}
2454
2455static void
2456intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
2457                               struct intel_sdvo_connector *connector)
2458{
2459        struct drm_i915_private *dev_priv = to_i915(connector->base.base.dev);
2460
2461        intel_attach_force_audio_property(&connector->base.base);
2462        if (INTEL_GEN(dev_priv) >= 4 && IS_MOBILE(dev_priv)) {
2463                intel_attach_broadcast_rgb_property(&connector->base.base);
2464        }
2465        intel_attach_aspect_ratio_property(&connector->base.base);
2466        connector->base.base.state->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
2467}
2468
2469static struct intel_sdvo_connector *intel_sdvo_connector_alloc(void)
2470{
2471        struct intel_sdvo_connector *sdvo_connector;
2472        struct intel_sdvo_connector_state *conn_state;
2473
2474        sdvo_connector = kzalloc(sizeof(*sdvo_connector), GFP_KERNEL);
2475        if (!sdvo_connector)
2476                return NULL;
2477
2478        conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
2479        if (!conn_state) {
2480                kfree(sdvo_connector);
2481                return NULL;
2482        }
2483
2484        __drm_atomic_helper_connector_reset(&sdvo_connector->base.base,
2485                                            &conn_state->base.base);
2486
2487        return sdvo_connector;
2488}
2489
2490static bool
2491intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
2492{
2493        struct drm_encoder *encoder = &intel_sdvo->base.base;
2494        struct drm_i915_private *dev_priv = to_i915(encoder->dev);
2495        struct drm_connector *connector;
2496        struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
2497        struct intel_connector *intel_connector;
2498        struct intel_sdvo_connector *intel_sdvo_connector;
2499
2500        DRM_DEBUG_KMS("initialising DVI device %d\n", device);
2501
2502        intel_sdvo_connector = intel_sdvo_connector_alloc();
2503        if (!intel_sdvo_connector)
2504                return false;
2505
2506        if (device == 0) {
2507                intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
2508                intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
2509        } else if (device == 1) {
2510                intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
2511                intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
2512        }
2513
2514        intel_connector = &intel_sdvo_connector->base;
2515        connector = &intel_connector->base;
2516        if (intel_sdvo_get_hotplug_support(intel_sdvo) &
2517                intel_sdvo_connector->output_flag) {
2518                intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
2519                /*
2520                 * Some SDVO devices have one-shot hotplug interrupts.
2521                 * Ensure that they get re-enabled when an interrupt happens.
2522                 */
2523                intel_encoder->hotplug = intel_sdvo_hotplug;
2524                intel_sdvo_enable_hotplug(intel_encoder);
2525        } else {
2526                intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
2527        }
2528        encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2529        connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2530
2531        /* gen3 doesn't do the hdmi bits in the SDVO register */
2532        if (INTEL_GEN(dev_priv) >= 4 &&
2533            intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
2534                connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
2535                intel_sdvo->is_hdmi = true;
2536        }
2537
2538        if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2539                kfree(intel_sdvo_connector);
2540                return false;
2541        }
2542
2543        if (intel_sdvo->is_hdmi)
2544                intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
2545
2546        return true;
2547}
2548
2549static bool
2550intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
2551{
2552        struct drm_encoder *encoder = &intel_sdvo->base.base;
2553        struct drm_connector *connector;
2554        struct intel_connector *intel_connector;
2555        struct intel_sdvo_connector *intel_sdvo_connector;
2556
2557        DRM_DEBUG_KMS("initialising TV type %d\n", type);
2558
2559        intel_sdvo_connector = intel_sdvo_connector_alloc();
2560        if (!intel_sdvo_connector)
2561                return false;
2562
2563        intel_connector = &intel_sdvo_connector->base;
2564        connector = &intel_connector->base;
2565        encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2566        connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
2567
2568        intel_sdvo->controlled_output |= type;
2569        intel_sdvo_connector->output_flag = type;
2570
2571        intel_sdvo->is_tv = true;
2572
2573        if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2574                kfree(intel_sdvo_connector);
2575                return false;
2576        }
2577
2578        if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
2579                goto err;
2580
2581        if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
2582                goto err;
2583
2584        return true;
2585
2586err:
2587        intel_sdvo_destroy(connector);
2588        return false;
2589}
2590
2591static bool
2592intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
2593{
2594        struct drm_encoder *encoder = &intel_sdvo->base.base;
2595        struct drm_connector *connector;
2596        struct intel_connector *intel_connector;
2597        struct intel_sdvo_connector *intel_sdvo_connector;
2598
2599        DRM_DEBUG_KMS("initialising analog device %d\n", device);
2600
2601        intel_sdvo_connector = intel_sdvo_connector_alloc();
2602        if (!intel_sdvo_connector)
2603                return false;
2604
2605        intel_connector = &intel_sdvo_connector->base;
2606        connector = &intel_connector->base;
2607        intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
2608        encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2609        connector->connector_type = DRM_MODE_CONNECTOR_VGA;
2610
2611        if (device == 0) {
2612                intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2613                intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2614        } else if (device == 1) {
2615                intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2616                intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2617        }
2618
2619        if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2620                kfree(intel_sdvo_connector);
2621                return false;
2622        }
2623
2624        return true;
2625}
2626
2627static bool
2628intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
2629{
2630        struct drm_encoder *encoder = &intel_sdvo->base.base;
2631        struct drm_connector *connector;
2632        struct intel_connector *intel_connector;
2633        struct intel_sdvo_connector *intel_sdvo_connector;
2634
2635        DRM_DEBUG_KMS("initialising LVDS device %d\n", device);
2636
2637        intel_sdvo_connector = intel_sdvo_connector_alloc();
2638        if (!intel_sdvo_connector)
2639                return false;
2640
2641        intel_connector = &intel_sdvo_connector->base;
2642        connector = &intel_connector->base;
2643        encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2644        connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2645
2646        if (device == 0) {
2647                intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2648                intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2649        } else if (device == 1) {
2650                intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2651                intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2652        }
2653
2654        if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2655                kfree(intel_sdvo_connector);
2656                return false;
2657        }
2658
2659        if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
2660                goto err;
2661
2662        return true;
2663
2664err:
2665        intel_sdvo_destroy(connector);
2666        return false;
2667}
2668
2669static bool
2670intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
2671{
2672        intel_sdvo->is_tv = false;
2673        intel_sdvo->is_lvds = false;
2674
2675        /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
2676
2677        if (flags & SDVO_OUTPUT_TMDS0)
2678                if (!intel_sdvo_dvi_init(intel_sdvo, 0))
2679                        return false;
2680
2681        if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
2682                if (!intel_sdvo_dvi_init(intel_sdvo, 1))
2683                        return false;
2684
2685        /* TV has no XXX1 function block */
2686        if (flags & SDVO_OUTPUT_SVID0)
2687                if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
2688                        return false;
2689
2690        if (flags & SDVO_OUTPUT_CVBS0)
2691                if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
2692                        return false;
2693
2694        if (flags & SDVO_OUTPUT_YPRPB0)
2695                if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
2696                        return false;
2697
2698        if (flags & SDVO_OUTPUT_RGB0)
2699                if (!intel_sdvo_analog_init(intel_sdvo, 0))
2700                        return false;
2701
2702        if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
2703                if (!intel_sdvo_analog_init(intel_sdvo, 1))
2704                        return false;
2705
2706        if (flags & SDVO_OUTPUT_LVDS0)
2707                if (!intel_sdvo_lvds_init(intel_sdvo, 0))
2708                        return false;
2709
2710        if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
2711                if (!intel_sdvo_lvds_init(intel_sdvo, 1))
2712                        return false;
2713
2714        if ((flags & SDVO_OUTPUT_MASK) == 0) {
2715                unsigned char bytes[2];
2716
2717                intel_sdvo->controlled_output = 0;
2718                memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
2719                DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
2720                              SDVO_NAME(intel_sdvo),
2721                              bytes[0], bytes[1]);
2722                return false;
2723        }
2724        intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
2725
2726        return true;
2727}
2728
2729static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
2730{
2731        struct drm_device *dev = intel_sdvo->base.base.dev;
2732        struct drm_connector *connector, *tmp;
2733
2734        list_for_each_entry_safe(connector, tmp,
2735                                 &dev->mode_config.connector_list, head) {
2736                if (intel_attached_encoder(connector) == &intel_sdvo->base) {
2737                        drm_connector_unregister(connector);
2738                        intel_sdvo_destroy(connector);
2739                }
2740        }
2741}
2742
2743static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
2744                                          struct intel_sdvo_connector *intel_sdvo_connector,
2745                                          int type)
2746{
2747        struct drm_device *dev = intel_sdvo->base.base.dev;
2748        struct intel_sdvo_tv_format format;
2749        uint32_t format_map, i;
2750
2751        if (!intel_sdvo_set_target_output(intel_sdvo, type))
2752                return false;
2753
2754        BUILD_BUG_ON(sizeof(format) != 6);
2755        if (!intel_sdvo_get_value(intel_sdvo,
2756                                  SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2757                                  &format, sizeof(format)))
2758                return false;
2759
2760        memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
2761
2762        if (format_map == 0)
2763                return false;
2764
2765        intel_sdvo_connector->format_supported_num = 0;
2766        for (i = 0 ; i < TV_FORMAT_NUM; i++)
2767                if (format_map & (1 << i))
2768                        intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
2769
2770
2771        intel_sdvo_connector->tv_format =
2772                        drm_property_create(dev, DRM_MODE_PROP_ENUM,
2773                                            "mode", intel_sdvo_connector->format_supported_num);
2774        if (!intel_sdvo_connector->tv_format)
2775                return false;
2776
2777        for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
2778                drm_property_add_enum(
2779                                intel_sdvo_connector->tv_format, i,
2780                                i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
2781
2782        intel_sdvo_connector->base.base.state->tv.mode = intel_sdvo_connector->tv_format_supported[0];
2783        drm_object_attach_property(&intel_sdvo_connector->base.base.base,
2784                                   intel_sdvo_connector->tv_format, 0);
2785        return true;
2786
2787}
2788
2789#define _ENHANCEMENT(state_assignment, name, NAME) do { \
2790        if (enhancements.name) { \
2791                if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2792                    !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2793                        return false; \
2794                intel_sdvo_connector->name = \
2795                        drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
2796                if (!intel_sdvo_connector->name) return false; \
2797                state_assignment = response; \
2798                drm_object_attach_property(&connector->base, \
2799                                           intel_sdvo_connector->name, 0); \
2800                DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2801                              data_value[0], data_value[1], response); \
2802        } \
2803} while (0)
2804
2805#define ENHANCEMENT(state, name, NAME) _ENHANCEMENT((state)->name, name, NAME)
2806
2807static bool
2808intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
2809                                      struct intel_sdvo_connector *intel_sdvo_connector,
2810                                      struct intel_sdvo_enhancements_reply enhancements)
2811{
2812        struct drm_device *dev = intel_sdvo->base.base.dev;
2813        struct drm_connector *connector = &intel_sdvo_connector->base.base;
2814        struct drm_connector_state *conn_state = connector->state;
2815        struct intel_sdvo_connector_state *sdvo_state =
2816                to_intel_sdvo_connector_state(conn_state);
2817        uint16_t response, data_value[2];
2818
2819        /* when horizontal overscan is supported, Add the left/right property */
2820        if (enhancements.overscan_h) {
2821                if (!intel_sdvo_get_value(intel_sdvo,
2822                                          SDVO_CMD_GET_MAX_OVERSCAN_H,
2823                                          &data_value, 4))
2824                        return false;
2825
2826                if (!intel_sdvo_get_value(intel_sdvo,
2827                                          SDVO_CMD_GET_OVERSCAN_H,
2828                                          &response, 2))
2829                        return false;
2830
2831                sdvo_state->tv.overscan_h = response;
2832
2833                intel_sdvo_connector->max_hscan = data_value[0];
2834                intel_sdvo_connector->left =
2835                        drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
2836                if (!intel_sdvo_connector->left)
2837                        return false;
2838
2839                drm_object_attach_property(&connector->base,
2840                                           intel_sdvo_connector->left, 0);
2841
2842                intel_sdvo_connector->right =
2843                        drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
2844                if (!intel_sdvo_connector->right)
2845                        return false;
2846
2847                drm_object_attach_property(&connector->base,
2848                                              intel_sdvo_connector->right, 0);
2849                DRM_DEBUG_KMS("h_overscan: max %d, "
2850                              "default %d, current %d\n",
2851                              data_value[0], data_value[1], response);
2852        }
2853
2854        if (enhancements.overscan_v) {
2855                if (!intel_sdvo_get_value(intel_sdvo,
2856                                          SDVO_CMD_GET_MAX_OVERSCAN_V,
2857                                          &data_value, 4))
2858                        return false;
2859
2860                if (!intel_sdvo_get_value(intel_sdvo,
2861                                          SDVO_CMD_GET_OVERSCAN_V,
2862                                          &response, 2))
2863                        return false;
2864
2865                sdvo_state->tv.overscan_v = response;
2866
2867                intel_sdvo_connector->max_vscan = data_value[0];
2868                intel_sdvo_connector->top =
2869                        drm_property_create_range(dev, 0,
2870                                            "top_margin", 0, data_value[0]);
2871                if (!intel_sdvo_connector->top)
2872                        return false;
2873
2874                drm_object_attach_property(&connector->base,
2875                                           intel_sdvo_connector->top, 0);
2876
2877                intel_sdvo_connector->bottom =
2878                        drm_property_create_range(dev, 0,
2879                                            "bottom_margin", 0, data_value[0]);
2880                if (!intel_sdvo_connector->bottom)
2881                        return false;
2882
2883                drm_object_attach_property(&connector->base,
2884                                              intel_sdvo_connector->bottom, 0);
2885                DRM_DEBUG_KMS("v_overscan: max %d, "
2886                              "default %d, current %d\n",
2887                              data_value[0], data_value[1], response);
2888        }
2889
2890        ENHANCEMENT(&sdvo_state->tv, hpos, HPOS);
2891        ENHANCEMENT(&sdvo_state->tv, vpos, VPOS);
2892        ENHANCEMENT(&conn_state->tv, saturation, SATURATION);
2893        ENHANCEMENT(&conn_state->tv, contrast, CONTRAST);
2894        ENHANCEMENT(&conn_state->tv, hue, HUE);
2895        ENHANCEMENT(&conn_state->tv, brightness, BRIGHTNESS);
2896        ENHANCEMENT(&sdvo_state->tv, sharpness, SHARPNESS);
2897        ENHANCEMENT(&sdvo_state->tv, flicker_filter, FLICKER_FILTER);
2898        ENHANCEMENT(&sdvo_state->tv, flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2899        ENHANCEMENT(&sdvo_state->tv, flicker_filter_2d, FLICKER_FILTER_2D);
2900        _ENHANCEMENT(sdvo_state->tv.chroma_filter, tv_chroma_filter, TV_CHROMA_FILTER);
2901        _ENHANCEMENT(sdvo_state->tv.luma_filter, tv_luma_filter, TV_LUMA_FILTER);
2902
2903        if (enhancements.dot_crawl) {
2904                if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2905                        return false;
2906
2907                sdvo_state->tv.dot_crawl = response & 0x1;
2908                intel_sdvo_connector->dot_crawl =
2909                        drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
2910                if (!intel_sdvo_connector->dot_crawl)
2911                        return false;
2912
2913                drm_object_attach_property(&connector->base,
2914                                           intel_sdvo_connector->dot_crawl, 0);
2915                DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2916        }
2917
2918        return true;
2919}
2920
2921static bool
2922intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
2923                                        struct intel_sdvo_connector *intel_sdvo_connector,
2924                                        struct intel_sdvo_enhancements_reply enhancements)
2925{
2926        struct drm_device *dev = intel_sdvo->base.base.dev;
2927        struct drm_connector *connector = &intel_sdvo_connector->base.base;
2928        uint16_t response, data_value[2];
2929
2930        ENHANCEMENT(&connector->state->tv, brightness, BRIGHTNESS);
2931
2932        return true;
2933}
2934#undef ENHANCEMENT
2935#undef _ENHANCEMENT
2936
2937static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
2938                                               struct intel_sdvo_connector *intel_sdvo_connector)
2939{
2940        union {
2941                struct intel_sdvo_enhancements_reply reply;
2942                uint16_t response;
2943        } enhancements;
2944
2945        BUILD_BUG_ON(sizeof(enhancements) != 2);
2946
2947        if (!intel_sdvo_get_value(intel_sdvo,
2948                                  SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2949                                  &enhancements, sizeof(enhancements)) ||
2950            enhancements.response == 0) {
2951                DRM_DEBUG_KMS("No enhancement is supported\n");
2952                return true;
2953        }
2954
2955        if (IS_TV(intel_sdvo_connector))
2956                return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2957        else if (IS_LVDS(intel_sdvo_connector))
2958                return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2959        else
2960                return true;
2961}
2962
2963static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2964                                     struct i2c_msg *msgs,
2965                                     int num)
2966{
2967        struct intel_sdvo *sdvo = adapter->algo_data;
2968
2969        if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
2970                return -EIO;
2971
2972        return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2973}
2974
2975static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2976{
2977        struct intel_sdvo *sdvo = adapter->algo_data;
2978        return sdvo->i2c->algo->functionality(sdvo->i2c);
2979}
2980
2981static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
2982        .master_xfer    = intel_sdvo_ddc_proxy_xfer,
2983        .functionality  = intel_sdvo_ddc_proxy_func
2984};
2985
2986static bool
2987intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
2988                          struct drm_i915_private *dev_priv)
2989{
2990        struct pci_dev *pdev = dev_priv->drm.pdev;
2991
2992        sdvo->ddc.owner = THIS_MODULE;
2993        sdvo->ddc.class = I2C_CLASS_DDC;
2994        snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
2995        sdvo->ddc.dev.parent = &pdev->dev;
2996        sdvo->ddc.algo_data = sdvo;
2997        sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
2998
2999        return i2c_add_adapter(&sdvo->ddc) == 0;
3000}
3001
3002static void assert_sdvo_port_valid(const struct drm_i915_private *dev_priv,
3003                                   enum port port)
3004{
3005        if (HAS_PCH_SPLIT(dev_priv))
3006                WARN_ON(port != PORT_B);
3007        else
3008                WARN_ON(port != PORT_B && port != PORT_C);
3009}
3010
3011bool intel_sdvo_init(struct drm_i915_private *dev_priv,
3012                     i915_reg_t sdvo_reg, enum port port)
3013{
3014        struct intel_encoder *intel_encoder;
3015        struct intel_sdvo *intel_sdvo;
3016        int i;
3017
3018        assert_sdvo_port_valid(dev_priv, port);
3019
3020        intel_sdvo = kzalloc(sizeof(*intel_sdvo), GFP_KERNEL);
3021        if (!intel_sdvo)
3022                return false;
3023
3024        intel_sdvo->sdvo_reg = sdvo_reg;
3025        intel_sdvo->port = port;
3026        intel_sdvo->slave_addr =
3027                intel_sdvo_get_slave_addr(dev_priv, intel_sdvo) >> 1;
3028        intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo);
3029        if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev_priv))
3030                goto err_i2c_bus;
3031
3032        /* encoder type will be decided later */
3033        intel_encoder = &intel_sdvo->base;
3034        intel_encoder->type = INTEL_OUTPUT_SDVO;
3035        intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
3036        intel_encoder->port = port;
3037        drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
3038                         &intel_sdvo_enc_funcs, 0,
3039                         "SDVO %c", port_name(port));
3040
3041        /* Read the regs to test if we can talk to the device */
3042        for (i = 0; i < 0x40; i++) {
3043                u8 byte;
3044
3045                if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
3046                        DRM_DEBUG_KMS("No SDVO device found on %s\n",
3047                                      SDVO_NAME(intel_sdvo));
3048                        goto err;
3049                }
3050        }
3051
3052        intel_encoder->compute_config = intel_sdvo_compute_config;
3053        if (HAS_PCH_SPLIT(dev_priv)) {
3054                intel_encoder->disable = pch_disable_sdvo;
3055                intel_encoder->post_disable = pch_post_disable_sdvo;
3056        } else {
3057                intel_encoder->disable = intel_disable_sdvo;
3058        }
3059        intel_encoder->pre_enable = intel_sdvo_pre_enable;
3060        intel_encoder->enable = intel_enable_sdvo;
3061        intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
3062        intel_encoder->get_config = intel_sdvo_get_config;
3063
3064        /* In default case sdvo lvds is false */
3065        if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
3066                goto err;
3067
3068        if (intel_sdvo_output_setup(intel_sdvo,
3069                                    intel_sdvo->caps.output_flags) != true) {
3070                DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
3071                              SDVO_NAME(intel_sdvo));
3072                /* Output_setup can leave behind connectors! */
3073                goto err_output;
3074        }
3075
3076        /*
3077         * Only enable the hotplug irq if we need it, to work around noisy
3078         * hotplug lines.
3079         */
3080        if (intel_sdvo->hotplug_active) {
3081                if (intel_sdvo->port == PORT_B)
3082                        intel_encoder->hpd_pin = HPD_SDVO_B;
3083                else
3084                        intel_encoder->hpd_pin = HPD_SDVO_C;
3085        }
3086
3087        /*
3088         * Cloning SDVO with anything is often impossible, since the SDVO
3089         * encoder can request a special input timing mode. And even if that's
3090         * not the case we have evidence that cloning a plain unscaled mode with
3091         * VGA doesn't really work. Furthermore the cloning flags are way too
3092         * simplistic anyway to express such constraints, so just give up on
3093         * cloning for SDVO encoders.
3094         */
3095        intel_sdvo->base.cloneable = 0;
3096
3097        intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo);
3098
3099        /* Set the input timing to the screen. Assume always input 0. */
3100        if (!intel_sdvo_set_target_input(intel_sdvo))
3101                goto err_output;
3102
3103        if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
3104                                                    &intel_sdvo->pixel_clock_min,
3105                                                    &intel_sdvo->pixel_clock_max))
3106                goto err_output;
3107
3108        DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
3109                        "clock range %dMHz - %dMHz, "
3110                        "input 1: %c, input 2: %c, "
3111                        "output 1: %c, output 2: %c\n",
3112                        SDVO_NAME(intel_sdvo),
3113                        intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
3114                        intel_sdvo->caps.device_rev_id,
3115                        intel_sdvo->pixel_clock_min / 1000,
3116                        intel_sdvo->pixel_clock_max / 1000,
3117                        (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
3118                        (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
3119                        /* check currently supported outputs */
3120                        intel_sdvo->caps.output_flags &
3121                        (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
3122                        intel_sdvo->caps.output_flags &
3123                        (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
3124        return true;
3125
3126err_output:
3127        intel_sdvo_output_cleanup(intel_sdvo);
3128
3129err:
3130        drm_encoder_cleanup(&intel_encoder->base);
3131        i2c_del_adapter(&intel_sdvo->ddc);
3132err_i2c_bus:
3133        intel_sdvo_unselect_i2c_bus(intel_sdvo);
3134        kfree(intel_sdvo);
3135
3136        return false;
3137}
3138