Subversion Repositories Kolibri OS

Rev

Rev 2342 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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 "drmP.h"
  32. #include "drm.h"
  33. #include "drm_crtc.h"
  34. #include "drm_edid.h"
  35. #include "intel_drv.h"
  36. #include "i915_drm.h"
  37. #include "i915_drv.h"
  38. #include "intel_sdvo_regs.h"
  39.  
  40. unsigned int hweight16(unsigned int w)
  41. {
  42.     unsigned int res = w - ((w >> 1) & 0x5555);
  43.     res = (res & 0x3333) + ((res >> 2) & 0x3333);
  44.     res = (res + (res >> 4)) & 0x0F0F;
  45.     return (res + (res >> 8)) & 0x00FF;
  46. }
  47.  
  48.  
  49. #define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
  50. #define SDVO_RGB_MASK  (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
  51. #define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
  52. #define SDVO_TV_MASK   (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0)
  53.  
  54. #define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
  55.                          SDVO_TV_MASK)
  56.  
  57. #define IS_TV(c)    (c->output_flag & SDVO_TV_MASK)
  58. #define IS_TMDS(c)  (c->output_flag & SDVO_TMDS_MASK)
  59. #define IS_LVDS(c)  (c->output_flag & SDVO_LVDS_MASK)
  60. #define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
  61.  
  62.  
  63. static const char *tv_format_names[] = {
  64.     "NTSC_M"   , "NTSC_J"  , "NTSC_443",
  65.     "PAL_B"    , "PAL_D"   , "PAL_G"   ,
  66.     "PAL_H"    , "PAL_I"   , "PAL_M"   ,
  67.     "PAL_N"    , "PAL_NC"  , "PAL_60"  ,
  68.     "SECAM_B"  , "SECAM_D" , "SECAM_G" ,
  69.     "SECAM_K"  , "SECAM_K1", "SECAM_L" ,
  70.     "SECAM_60"
  71. };
  72.  
  73. #define TV_FORMAT_NUM  (sizeof(tv_format_names) / sizeof(*tv_format_names))
  74.  
  75. struct intel_sdvo {
  76.     struct intel_encoder base;
  77.  
  78.     struct i2c_adapter *i2c;
  79.     u8 slave_addr;
  80.  
  81.     struct i2c_adapter ddc;
  82.  
  83.     /* Register for the SDVO device: SDVOB or SDVOC */
  84.     int sdvo_reg;
  85.  
  86.     /* Active outputs controlled by this SDVO output */
  87.     uint16_t controlled_output;
  88.  
  89.     /*
  90.      * Capabilities of the SDVO device returned by
  91.      * i830_sdvo_get_capabilities()
  92.      */
  93.     struct intel_sdvo_caps caps;
  94.  
  95.     /* Pixel clock limitations reported by the SDVO device, in kHz */
  96.     int pixel_clock_min, pixel_clock_max;
  97.  
  98.     /*
  99.     * For multiple function SDVO device,
  100.     * this is for current attached outputs.
  101.     */
  102.     uint16_t attached_output;
  103.  
  104.     /**
  105.      * This is used to select the color range of RBG outputs in HDMI mode.
  106.      * It is only valid when using TMDS encoding and 8 bit per color mode.
  107.      */
  108.     uint32_t color_range;
  109.  
  110.     /**
  111.      * This is set if we're going to treat the device as TV-out.
  112.      *
  113.      * While we have these nice friendly flags for output types that ought
  114.      * to decide this for us, the S-Video output on our HDMI+S-Video card
  115.      * shows up as RGB1 (VGA).
  116.      */
  117.     bool is_tv;
  118.  
  119.     /* This is for current tv format name */
  120.     int tv_format_index;
  121.  
  122.     /**
  123.      * This is set if we treat the device as HDMI, instead of DVI.
  124.      */
  125.     bool is_hdmi;
  126.     bool has_hdmi_monitor;
  127.     bool has_hdmi_audio;
  128.  
  129.     /**
  130.      * This is set if we detect output of sdvo device as LVDS and
  131.      * have a valid fixed mode to use with the panel.
  132.      */
  133.     bool is_lvds;
  134.  
  135.     /**
  136.      * This is sdvo fixed pannel mode pointer
  137.      */
  138.     struct drm_display_mode *sdvo_lvds_fixed_mode;
  139.  
  140.     /* DDC bus used by this SDVO encoder */
  141.     uint8_t ddc_bus;
  142.  
  143.     /* Input timings for adjusted_mode */
  144.     struct intel_sdvo_dtd input_dtd;
  145. };
  146.  
  147. struct intel_sdvo_connector {
  148.     struct intel_connector base;
  149.  
  150.     /* Mark the type of connector */
  151.     uint16_t output_flag;
  152.  
  153.     int force_audio;
  154.  
  155.     /* This contains all current supported TV format */
  156.     u8 tv_format_supported[TV_FORMAT_NUM];
  157.     int   format_supported_num;
  158.     struct drm_property *tv_format;
  159.  
  160.     /* add the property for the SDVO-TV */
  161.     struct drm_property *left;
  162.     struct drm_property *right;
  163.     struct drm_property *top;
  164.     struct drm_property *bottom;
  165.     struct drm_property *hpos;
  166.     struct drm_property *vpos;
  167.     struct drm_property *contrast;
  168.     struct drm_property *saturation;
  169.     struct drm_property *hue;
  170.     struct drm_property *sharpness;
  171.     struct drm_property *flicker_filter;
  172.     struct drm_property *flicker_filter_adaptive;
  173.     struct drm_property *flicker_filter_2d;
  174.     struct drm_property *tv_chroma_filter;
  175.     struct drm_property *tv_luma_filter;
  176.     struct drm_property *dot_crawl;
  177.  
  178.     /* add the property for the SDVO-TV/LVDS */
  179.     struct drm_property *brightness;
  180.  
  181.     /* Add variable to record current setting for the above property */
  182.     u32 left_margin, right_margin, top_margin, bottom_margin;
  183.  
  184.     /* this is to get the range of margin.*/
  185.     u32 max_hscan,  max_vscan;
  186.     u32 max_hpos, cur_hpos;
  187.     u32 max_vpos, cur_vpos;
  188.     u32 cur_brightness, max_brightness;
  189.     u32 cur_contrast,   max_contrast;
  190.     u32 cur_saturation, max_saturation;
  191.     u32 cur_hue,    max_hue;
  192.     u32 cur_sharpness,  max_sharpness;
  193.     u32 cur_flicker_filter,     max_flicker_filter;
  194.     u32 cur_flicker_filter_adaptive,    max_flicker_filter_adaptive;
  195.     u32 cur_flicker_filter_2d,      max_flicker_filter_2d;
  196.     u32 cur_tv_chroma_filter,   max_tv_chroma_filter;
  197.     u32 cur_tv_luma_filter, max_tv_luma_filter;
  198.     u32 cur_dot_crawl,  max_dot_crawl;
  199. };
  200.  
  201. static struct intel_sdvo *to_intel_sdvo(struct drm_encoder *encoder)
  202. {
  203.     return container_of(encoder, struct intel_sdvo, base.base);
  204. }
  205.  
  206. static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
  207. {
  208.         return container_of(intel_attached_encoder(connector),
  209.                             struct intel_sdvo, base);
  210. }
  211.  
  212. static struct intel_sdvo_connector *to_intel_sdvo_connector(struct drm_connector *connector)
  213. {
  214.         return container_of(to_intel_connector(connector), struct intel_sdvo_connector, base);
  215. }
  216.  
  217. static bool
  218. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
  219. static bool
  220. intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  221.                               struct intel_sdvo_connector *intel_sdvo_connector,
  222.                               int type);
  223. static bool
  224. intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  225.                                    struct intel_sdvo_connector *intel_sdvo_connector);
  226.  
  227. /**
  228.  * Writes the SDVOB or SDVOC with the given value, but always writes both
  229.  * SDVOB and SDVOC to work around apparent hardware issues (according to
  230.  * comments in the BIOS).
  231.  */
  232. static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
  233. {
  234.         struct drm_device *dev = intel_sdvo->base.base.dev;
  235.         struct drm_i915_private *dev_priv = dev->dev_private;
  236.         u32 bval = val, cval = val;
  237.         int i;
  238.  
  239.         if (intel_sdvo->sdvo_reg == PCH_SDVOB) {
  240.                 I915_WRITE(intel_sdvo->sdvo_reg, val);
  241.                 I915_READ(intel_sdvo->sdvo_reg);
  242.                 return;
  243.         }
  244.  
  245.         if (intel_sdvo->sdvo_reg == SDVOB) {
  246.                 cval = I915_READ(SDVOC);
  247.         } else {
  248.                 bval = I915_READ(SDVOB);
  249.         }
  250.         /*
  251.          * Write the registers twice for luck. Sometimes,
  252.          * writing them only once doesn't appear to 'stick'.
  253.          * The BIOS does this too. Yay, magic
  254.          */
  255.         for (i = 0; i < 2; i++)
  256.         {
  257.                 I915_WRITE(SDVOB, bval);
  258.                 I915_READ(SDVOB);
  259.                 I915_WRITE(SDVOC, cval);
  260.                 I915_READ(SDVOC);
  261.         }
  262. }
  263.  
  264. static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
  265. {
  266.         struct i2c_msg msgs[] = {
  267.                 {
  268.                         .addr = intel_sdvo->slave_addr,
  269.                         .flags = 0,
  270.                         .len = 1,
  271.                         .buf = &addr,
  272.                 },
  273.                 {
  274.                         .addr = intel_sdvo->slave_addr,
  275.                         .flags = I2C_M_RD,
  276.                         .len = 1,
  277.                         .buf = ch,
  278.                 }
  279.         };
  280.         int ret;
  281.  
  282.         if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
  283.                 return true;
  284.  
  285.         DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
  286.         return false;
  287. }
  288.  
  289. #define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
  290. /** Mapping of command numbers to names, for debug output */
  291. static const struct _sdvo_cmd_name {
  292.         u8 cmd;
  293.         const char *name;
  294. } sdvo_cmd_names[] = {
  295.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
  296.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
  297.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
  298.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
  299.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
  300.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
  301.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
  302.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
  303.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
  304.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
  305.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
  306.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
  307.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
  308.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
  309.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
  310.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
  311.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
  312.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  313.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
  314.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
  315.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
  316.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
  317.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
  318.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
  319.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
  320.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
  321.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
  322.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
  323.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
  324.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
  325.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
  326.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
  327.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
  328.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
  329.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
  330.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
  331.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
  332.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
  333.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
  334.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
  335.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
  336.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
  337.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
  338.  
  339.     /* Add the op code for SDVO enhancements */
  340.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
  341.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
  342.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
  343.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
  344.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
  345.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
  346.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
  347.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
  348.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
  349.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
  350.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
  351.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
  352.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
  353.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
  354.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
  355.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
  356.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
  357.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
  358.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
  359.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
  360.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
  361.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
  362.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
  363.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
  364.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
  365.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
  366.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
  367.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
  368.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
  369.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
  370.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
  371.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
  372.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
  373.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
  374.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
  375.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
  376.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
  377.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
  378.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
  379.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
  380.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
  381.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
  382.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
  383.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
  384.  
  385.     /* HDMI op code */
  386.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
  387.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
  388.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
  389.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
  390.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
  391.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
  392.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
  393.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
  394.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
  395.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
  396.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
  397.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
  398.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
  399.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
  400.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
  401.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
  402.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
  403.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
  404.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
  405.     SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
  406. };
  407.  
  408. #define IS_SDVOB(reg)   (reg == SDVOB || reg == PCH_SDVOB)
  409. #define SDVO_NAME(svdo) (IS_SDVOB((svdo)->sdvo_reg) ? "SDVOB" : "SDVOC")
  410.  
  411. static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
  412.                                    const void *args, int args_len)
  413. {
  414.         int i;
  415.  
  416.         DRM_DEBUG_KMS("%s: W: %02X ",
  417.                                 SDVO_NAME(intel_sdvo), cmd);
  418.         for (i = 0; i < args_len; i++)
  419.                 DRM_LOG_KMS("%02X ", ((u8 *)args)[i]);
  420.         for (; i < 8; i++)
  421.                 DRM_LOG_KMS("   ");
  422.         for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
  423.                 if (cmd == sdvo_cmd_names[i].cmd) {
  424.                         DRM_LOG_KMS("(%s)", sdvo_cmd_names[i].name);
  425.                         break;
  426.                 }
  427.         }
  428.         if (i == ARRAY_SIZE(sdvo_cmd_names))
  429.                 DRM_LOG_KMS("(%02X)", cmd);
  430.         DRM_LOG_KMS("\n");
  431. }
  432.  
  433. static const char *cmd_status_names[] = {
  434.         "Power on",
  435.         "Success",
  436.         "Not supported",
  437.         "Invalid arg",
  438.         "Pending",
  439.         "Target not specified",
  440.         "Scaling not supported"
  441. };
  442.  
  443. static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
  444.                                  const void *args, int args_len)
  445. {
  446.         u8 buf[args_len*2 + 2], status;
  447.         struct i2c_msg msgs[args_len + 3];
  448.         int i, ret;
  449.  
  450.         intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
  451.  
  452.         for (i = 0; i < args_len; i++) {
  453.                 msgs[i].addr = intel_sdvo->slave_addr;
  454.                 msgs[i].flags = 0;
  455.                 msgs[i].len = 2;
  456.                 msgs[i].buf = buf + 2 *i;
  457.                 buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
  458.                 buf[2*i + 1] = ((u8*)args)[i];
  459.         }
  460.         msgs[i].addr = intel_sdvo->slave_addr;
  461.         msgs[i].flags = 0;
  462.         msgs[i].len = 2;
  463.         msgs[i].buf = buf + 2*i;
  464.         buf[2*i + 0] = SDVO_I2C_OPCODE;
  465.         buf[2*i + 1] = cmd;
  466.  
  467.         /* the following two are to read the response */
  468.         status = SDVO_I2C_CMD_STATUS;
  469.         msgs[i+1].addr = intel_sdvo->slave_addr;
  470.         msgs[i+1].flags = 0;
  471.         msgs[i+1].len = 1;
  472.         msgs[i+1].buf = &status;
  473.  
  474.         msgs[i+2].addr = intel_sdvo->slave_addr;
  475.         msgs[i+2].flags = I2C_M_RD;
  476.         msgs[i+2].len = 1;
  477.         msgs[i+2].buf = &status;
  478.  
  479.         ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
  480.         if (ret < 0) {
  481.                 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
  482.                 return false;
  483.         }
  484.         if (ret != i+3) {
  485.                 /* failure in I2C transfer */
  486.                 DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
  487.                 return false;
  488.         }
  489.  
  490.         return true;
  491. }
  492.  
  493. static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
  494.                                      void *response, int response_len)
  495. {
  496.         u8 retry = 5;
  497.         u8 status;
  498.         int i;
  499.  
  500.         DRM_DEBUG_KMS("%s: R: ", SDVO_NAME(intel_sdvo));
  501.  
  502.         /*
  503.          * The documentation states that all commands will be
  504.          * processed within 15µs, and that we need only poll
  505.          * the status byte a maximum of 3 times in order for the
  506.          * command to be complete.
  507.          *
  508.          * Check 5 times in case the hardware failed to read the docs.
  509.          */
  510.         if (!intel_sdvo_read_byte(intel_sdvo,
  511.                                   SDVO_I2C_CMD_STATUS,
  512.                                   &status))
  513.                 goto log_fail;
  514.  
  515.         while (status == SDVO_CMD_STATUS_PENDING && retry--) {
  516.                 udelay(15);
  517.                 if (!intel_sdvo_read_byte(intel_sdvo,
  518.                                           SDVO_I2C_CMD_STATUS,
  519.                                           &status))
  520.                         goto log_fail;
  521.         }
  522.  
  523.         if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
  524.                 DRM_LOG_KMS("(%s)", cmd_status_names[status]);
  525.         else
  526.                 DRM_LOG_KMS("(??? %d)", status);
  527.  
  528.         if (status != SDVO_CMD_STATUS_SUCCESS)
  529.                 goto log_fail;
  530.  
  531.         /* Read the command response */
  532.         for (i = 0; i < response_len; i++) {
  533.                 if (!intel_sdvo_read_byte(intel_sdvo,
  534.                                           SDVO_I2C_RETURN_0 + i,
  535.                                           &((u8 *)response)[i]))
  536.                         goto log_fail;
  537.                 DRM_LOG_KMS(" %02X", ((u8 *)response)[i]);
  538.         }
  539.         DRM_LOG_KMS("\n");
  540.         return true;
  541.  
  542. log_fail:
  543.         DRM_LOG_KMS("... failed\n");
  544.         return false;
  545. }
  546.  
  547. static int intel_sdvo_get_pixel_multiplier(struct drm_display_mode *mode)
  548. {
  549.         if (mode->clock >= 100000)
  550.                 return 1;
  551.         else if (mode->clock >= 50000)
  552.                 return 2;
  553.         else
  554.                 return 4;
  555. }
  556.  
  557. static bool intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
  558.                                               u8 ddc_bus)
  559. {
  560.         /* This must be the immediately preceding write before the i2c xfer */
  561.         return intel_sdvo_write_cmd(intel_sdvo,
  562.                                     SDVO_CMD_SET_CONTROL_BUS_SWITCH,
  563.                                     &ddc_bus, 1);
  564. }
  565.  
  566. static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
  567. {
  568.         if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
  569.                 return false;
  570.  
  571.         return intel_sdvo_read_response(intel_sdvo, NULL, 0);
  572. }
  573.  
  574. static bool
  575. intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
  576. {
  577.         if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
  578.                 return false;
  579.  
  580.         return intel_sdvo_read_response(intel_sdvo, value, len);
  581. }
  582.  
  583. static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
  584. {
  585.         struct intel_sdvo_set_target_input_args targets = {0};
  586.         return intel_sdvo_set_value(intel_sdvo,
  587.                                     SDVO_CMD_SET_TARGET_INPUT,
  588.                                     &targets, sizeof(targets));
  589. }
  590.  
  591. /**
  592.  * Return whether each input is trained.
  593.  *
  594.  * This function is making an assumption about the layout of the response,
  595.  * which should be checked against the docs.
  596.  */
  597. static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
  598. {
  599.         struct intel_sdvo_get_trained_inputs_response response;
  600.  
  601.         BUILD_BUG_ON(sizeof(response) != 1);
  602.         if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
  603.                                   &response, sizeof(response)))
  604.                 return false;
  605.  
  606.         *input_1 = response.input0_trained;
  607.         *input_2 = response.input1_trained;
  608.         return true;
  609. }
  610.  
  611. static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
  612.                                           u16 outputs)
  613. {
  614.         return intel_sdvo_set_value(intel_sdvo,
  615.                                     SDVO_CMD_SET_ACTIVE_OUTPUTS,
  616.                                     &outputs, sizeof(outputs));
  617. }
  618.  
  619.  
  620.  
  621.  
  622. static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
  623.                                                    int *clock_min,
  624.                                                    int *clock_max)
  625. {
  626.         struct intel_sdvo_pixel_clock_range clocks;
  627.  
  628.         BUILD_BUG_ON(sizeof(clocks) != 4);
  629.         if (!intel_sdvo_get_value(intel_sdvo,
  630.                                   SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
  631.                                   &clocks, sizeof(clocks)))
  632.                 return false;
  633.  
  634.         /* Convert the values from units of 10 kHz to kHz. */
  635.         *clock_min = clocks.min * 10;
  636.         *clock_max = clocks.max * 10;
  637.         return true;
  638. }
  639.  
  640. static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
  641.                                          u16 outputs)
  642. {
  643.         return intel_sdvo_set_value(intel_sdvo,
  644.                                     SDVO_CMD_SET_TARGET_OUTPUT,
  645.                                     &outputs, sizeof(outputs));
  646. }
  647.  
  648. static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
  649.                                   struct intel_sdvo_dtd *dtd)
  650. {
  651.         return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
  652.                 intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
  653. }
  654.  
  655. static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
  656.                                          struct intel_sdvo_dtd *dtd)
  657. {
  658.         return intel_sdvo_set_timing(intel_sdvo,
  659.                                      SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
  660. }
  661.  
  662. static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
  663.                                          struct intel_sdvo_dtd *dtd)
  664. {
  665.         return intel_sdvo_set_timing(intel_sdvo,
  666.                                      SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
  667. }
  668.  
  669. static bool
  670. intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  671.                                          uint16_t clock,
  672.                                          uint16_t width,
  673.                                          uint16_t height)
  674. {
  675.         struct intel_sdvo_preferred_input_timing_args args;
  676.  
  677.         memset(&args, 0, sizeof(args));
  678.         args.clock = clock;
  679.         args.width = width;
  680.         args.height = height;
  681.         args.interlace = 0;
  682.  
  683.         if (intel_sdvo->is_lvds &&
  684.            (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
  685.             intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
  686.                 args.scaled = 1;
  687.  
  688.         return intel_sdvo_set_value(intel_sdvo,
  689.                                     SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
  690.                                     &args, sizeof(args));
  691. }
  692.  
  693. static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
  694.                                                   struct intel_sdvo_dtd *dtd)
  695. {
  696.         BUILD_BUG_ON(sizeof(dtd->part1) != 8);
  697.         BUILD_BUG_ON(sizeof(dtd->part2) != 8);
  698.         return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
  699.                                     &dtd->part1, sizeof(dtd->part1)) &&
  700.                 intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
  701.                                      &dtd->part2, sizeof(dtd->part2));
  702. }
  703.  
  704. static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
  705. {
  706.         return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
  707. }
  708.  
  709. static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
  710.                                          const struct drm_display_mode *mode)
  711. {
  712.         uint16_t width, height;
  713.         uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
  714.         uint16_t h_sync_offset, v_sync_offset;
  715.  
  716.         width = mode->crtc_hdisplay;
  717.         height = mode->crtc_vdisplay;
  718.  
  719.         /* do some mode translations */
  720.         h_blank_len = mode->crtc_hblank_end - mode->crtc_hblank_start;
  721.         h_sync_len = mode->crtc_hsync_end - mode->crtc_hsync_start;
  722.  
  723.         v_blank_len = mode->crtc_vblank_end - mode->crtc_vblank_start;
  724.         v_sync_len = mode->crtc_vsync_end - mode->crtc_vsync_start;
  725.  
  726.         h_sync_offset = mode->crtc_hsync_start - mode->crtc_hblank_start;
  727.         v_sync_offset = mode->crtc_vsync_start - mode->crtc_vblank_start;
  728.  
  729.         dtd->part1.clock = mode->clock / 10;
  730.         dtd->part1.h_active = width & 0xff;
  731.         dtd->part1.h_blank = h_blank_len & 0xff;
  732.         dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
  733.                 ((h_blank_len >> 8) & 0xf);
  734.         dtd->part1.v_active = height & 0xff;
  735.         dtd->part1.v_blank = v_blank_len & 0xff;
  736.         dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
  737.                 ((v_blank_len >> 8) & 0xf);
  738.  
  739.         dtd->part2.h_sync_off = h_sync_offset & 0xff;
  740.         dtd->part2.h_sync_width = h_sync_len & 0xff;
  741.         dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
  742.                 (v_sync_len & 0xf);
  743.         dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
  744.                 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
  745.                 ((v_sync_len & 0x30) >> 4);
  746.  
  747.         dtd->part2.dtd_flags = 0x18;
  748.         if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  749.                 dtd->part2.dtd_flags |= 0x2;
  750.         if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  751.                 dtd->part2.dtd_flags |= 0x4;
  752.  
  753.         dtd->part2.sdvo_flags = 0;
  754.         dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
  755.         dtd->part2.reserved = 0;
  756. }
  757.  
  758. static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode * mode,
  759.                                          const struct intel_sdvo_dtd *dtd)
  760. {
  761.         mode->hdisplay = dtd->part1.h_active;
  762.         mode->hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
  763.         mode->hsync_start = mode->hdisplay + dtd->part2.h_sync_off;
  764.         mode->hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
  765.         mode->hsync_end = mode->hsync_start + dtd->part2.h_sync_width;
  766.         mode->hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
  767.         mode->htotal = mode->hdisplay + dtd->part1.h_blank;
  768.         mode->htotal += (dtd->part1.h_high & 0xf) << 8;
  769.  
  770.         mode->vdisplay = dtd->part1.v_active;
  771.         mode->vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
  772.         mode->vsync_start = mode->vdisplay;
  773.         mode->vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
  774.         mode->vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
  775.         mode->vsync_start += dtd->part2.v_sync_off_high & 0xc0;
  776.         mode->vsync_end = mode->vsync_start +
  777.                 (dtd->part2.v_sync_off_width & 0xf);
  778.         mode->vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
  779.         mode->vtotal = mode->vdisplay + dtd->part1.v_blank;
  780.         mode->vtotal += (dtd->part1.v_high & 0xf) << 8;
  781.  
  782.         mode->clock = dtd->part1.clock * 10;
  783.  
  784.         mode->flags &= ~(DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC);
  785.         if (dtd->part2.dtd_flags & 0x2)
  786.                 mode->flags |= DRM_MODE_FLAG_PHSYNC;
  787.         if (dtd->part2.dtd_flags & 0x4)
  788.                 mode->flags |= DRM_MODE_FLAG_PVSYNC;
  789. }
  790.  
  791. static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
  792. {
  793.         struct intel_sdvo_encode encode;
  794.  
  795.         BUILD_BUG_ON(sizeof(encode) != 2);
  796.         return intel_sdvo_get_value(intel_sdvo,
  797.                                   SDVO_CMD_GET_SUPP_ENCODE,
  798.                                   &encode, sizeof(encode));
  799. }
  800.  
  801. static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
  802.                                   uint8_t mode)
  803. {
  804.         return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
  805. }
  806.  
  807. static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
  808.                                        uint8_t mode)
  809. {
  810.         return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
  811. }
  812.  
  813. #if 0
  814. static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
  815. {
  816.         int i, j;
  817.         uint8_t set_buf_index[2];
  818.         uint8_t av_split;
  819.         uint8_t buf_size;
  820.         uint8_t buf[48];
  821.         uint8_t *pos;
  822.  
  823.         intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
  824.  
  825.         for (i = 0; i <= av_split; i++) {
  826.                 set_buf_index[0] = i; set_buf_index[1] = 0;
  827.                 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
  828.                                      set_buf_index, 2);
  829.                 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
  830.                 intel_sdvo_read_response(encoder, &buf_size, 1);
  831.  
  832.                 pos = buf;
  833.                 for (j = 0; j <= buf_size; j += 8) {
  834.                         intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
  835.                                              NULL, 0);
  836.                         intel_sdvo_read_response(encoder, pos, 8);
  837.                         pos += 8;
  838.                 }
  839.         }
  840. }
  841. #endif
  842.  
  843. static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo)
  844. {
  845.         struct dip_infoframe avi_if = {
  846.                 .type = DIP_TYPE_AVI,
  847.                 .ver = DIP_VERSION_AVI,
  848.                 .len = DIP_LEN_AVI,
  849.         };
  850.         uint8_t tx_rate = SDVO_HBUF_TX_VSYNC;
  851.         uint8_t set_buf_index[2] = { 1, 0 };
  852.         uint64_t *data = (uint64_t *)&avi_if;
  853.         unsigned i;
  854.  
  855.         intel_dip_infoframe_csum(&avi_if);
  856.  
  857.         if (!intel_sdvo_set_value(intel_sdvo,
  858.                                   SDVO_CMD_SET_HBUF_INDEX,
  859.                                   set_buf_index, 2))
  860.                 return false;
  861.  
  862.         for (i = 0; i < sizeof(avi_if); i += 8) {
  863.                 if (!intel_sdvo_set_value(intel_sdvo,
  864.                                           SDVO_CMD_SET_HBUF_DATA,
  865.                                           data, 8))
  866.                         return false;
  867.                 data++;
  868.         }
  869.  
  870.         return intel_sdvo_set_value(intel_sdvo,
  871.                                     SDVO_CMD_SET_HBUF_TXRATE,
  872.                                     &tx_rate, 1);
  873. }
  874.  
  875. static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo)
  876. {
  877.         struct intel_sdvo_tv_format format;
  878.         uint32_t format_map;
  879.  
  880.         format_map = 1 << intel_sdvo->tv_format_index;
  881.         memset(&format, 0, sizeof(format));
  882.         memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
  883.  
  884.         BUILD_BUG_ON(sizeof(format) != 6);
  885.         return intel_sdvo_set_value(intel_sdvo,
  886.                                     SDVO_CMD_SET_TV_FORMAT,
  887.                                     &format, sizeof(format));
  888. }
  889.  
  890. static bool
  891. intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
  892.                                         struct drm_display_mode *mode)
  893. {
  894.         struct intel_sdvo_dtd output_dtd;
  895.  
  896.         if (!intel_sdvo_set_target_output(intel_sdvo,
  897.                                           intel_sdvo->attached_output))
  898.                 return false;
  899.  
  900.         intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
  901.         if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
  902.                 return false;
  903.  
  904.         return true;
  905. }
  906.  
  907. static bool
  908. intel_sdvo_set_input_timings_for_mode(struct intel_sdvo *intel_sdvo,
  909.                                         struct drm_display_mode *mode,
  910.                                         struct drm_display_mode *adjusted_mode)
  911. {
  912.         /* Reset the input timing to the screen. Assume always input 0. */
  913.         if (!intel_sdvo_set_target_input(intel_sdvo))
  914.                 return false;
  915.  
  916.         if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
  917.                                                       mode->clock / 10,
  918.                                                       mode->hdisplay,
  919.                                                       mode->vdisplay))
  920.                 return false;
  921.  
  922.         if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
  923.                                                    &intel_sdvo->input_dtd))
  924.                 return false;
  925.  
  926.         intel_sdvo_get_mode_from_dtd(adjusted_mode, &intel_sdvo->input_dtd);
  927.  
  928.         drm_mode_set_crtcinfo(adjusted_mode, 0);
  929.         return true;
  930. }
  931.  
  932. static bool intel_sdvo_mode_fixup(struct drm_encoder *encoder,
  933.                                   struct drm_display_mode *mode,
  934.                                   struct drm_display_mode *adjusted_mode)
  935. {
  936.         struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  937.         int multiplier;
  938.  
  939.         /* We need to construct preferred input timings based on our
  940.          * output timings.  To do that, we have to set the output
  941.          * timings, even though this isn't really the right place in
  942.          * the sequence to do it. Oh well.
  943.          */
  944.         if (intel_sdvo->is_tv) {
  945.                 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
  946.                         return false;
  947.  
  948.                 (void) intel_sdvo_set_input_timings_for_mode(intel_sdvo,
  949.                                                              mode,
  950.                                                              adjusted_mode);
  951.         } else if (intel_sdvo->is_lvds) {
  952.                 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
  953.                                                              intel_sdvo->sdvo_lvds_fixed_mode))
  954.                         return false;
  955.  
  956.                 (void) intel_sdvo_set_input_timings_for_mode(intel_sdvo,
  957.                                                              mode,
  958.                                                              adjusted_mode);
  959.         }
  960.  
  961.         /* Make the CRTC code factor in the SDVO pixel multiplier.  The
  962.          * SDVO device will factor out the multiplier during mode_set.
  963.          */
  964.         multiplier = intel_sdvo_get_pixel_multiplier(adjusted_mode);
  965.         intel_mode_set_pixel_multiplier(adjusted_mode, multiplier);
  966.  
  967.         return true;
  968. }
  969.  
  970. static void intel_sdvo_mode_set(struct drm_encoder *encoder,
  971.                                 struct drm_display_mode *mode,
  972.                                 struct drm_display_mode *adjusted_mode)
  973. {
  974.         struct drm_device *dev = encoder->dev;
  975.         struct drm_i915_private *dev_priv = dev->dev_private;
  976.         struct drm_crtc *crtc = encoder->crtc;
  977.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  978.         struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  979.         u32 sdvox;
  980.         struct intel_sdvo_in_out_map in_out;
  981.         struct intel_sdvo_dtd input_dtd;
  982.         int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  983.         int rate;
  984.  
  985.         if (!mode)
  986.                 return;
  987.  
  988.         /* First, set the input mapping for the first input to our controlled
  989.          * output. This is only correct if we're a single-input device, in
  990.          * which case the first input is the output from the appropriate SDVO
  991.          * channel on the motherboard.  In a two-input device, the first input
  992.          * will be SDVOB and the second SDVOC.
  993.          */
  994.         in_out.in0 = intel_sdvo->attached_output;
  995.         in_out.in1 = 0;
  996.  
  997.         intel_sdvo_set_value(intel_sdvo,
  998.                              SDVO_CMD_SET_IN_OUT_MAP,
  999.                              &in_out, sizeof(in_out));
  1000.  
  1001.         /* Set the output timings to the screen */
  1002.         if (!intel_sdvo_set_target_output(intel_sdvo,
  1003.                                           intel_sdvo->attached_output))
  1004.                 return;
  1005.  
  1006.         /* We have tried to get input timing in mode_fixup, and filled into
  1007.          * adjusted_mode.
  1008.          */
  1009.         if (intel_sdvo->is_tv || intel_sdvo->is_lvds) {
  1010.                 input_dtd = intel_sdvo->input_dtd;
  1011.         } else {
  1012.                 /* Set the output timing to the screen */
  1013.                 if (!intel_sdvo_set_target_output(intel_sdvo,
  1014.                                                   intel_sdvo->attached_output))
  1015.                         return;
  1016.  
  1017.                 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
  1018.                 (void) intel_sdvo_set_output_timing(intel_sdvo, &input_dtd);
  1019.         }
  1020.  
  1021.         /* Set the input timing to the screen. Assume always input 0. */
  1022.         if (!intel_sdvo_set_target_input(intel_sdvo))
  1023.                 return;
  1024.  
  1025.         if (intel_sdvo->has_hdmi_monitor) {
  1026.                 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
  1027.                 intel_sdvo_set_colorimetry(intel_sdvo,
  1028.                                            SDVO_COLORIMETRY_RGB256);
  1029.                 intel_sdvo_set_avi_infoframe(intel_sdvo);
  1030.         } else
  1031.                 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
  1032.  
  1033.         if (intel_sdvo->is_tv &&
  1034.             !intel_sdvo_set_tv_format(intel_sdvo))
  1035.                 return;
  1036.  
  1037.         (void) intel_sdvo_set_input_timing(intel_sdvo, &input_dtd);
  1038.  
  1039.         switch (pixel_multiplier) {
  1040.         default:
  1041.         case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
  1042.         case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
  1043.         case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
  1044.         }
  1045.         if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
  1046.                 return;
  1047.  
  1048.         /* Set the SDVO control regs. */
  1049.         if (INTEL_INFO(dev)->gen >= 4) {
  1050.                 sdvox = 0;
  1051.                 if (intel_sdvo->is_hdmi)
  1052.                         sdvox |= intel_sdvo->color_range;
  1053.                 if (INTEL_INFO(dev)->gen < 5)
  1054.                         sdvox |= SDVO_BORDER_ENABLE;
  1055.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  1056.                         sdvox |= SDVO_VSYNC_ACTIVE_HIGH;
  1057.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  1058.                         sdvox |= SDVO_HSYNC_ACTIVE_HIGH;
  1059.         } else {
  1060.                 sdvox = I915_READ(intel_sdvo->sdvo_reg);
  1061.                 switch (intel_sdvo->sdvo_reg) {
  1062.                 case SDVOB:
  1063.                         sdvox &= SDVOB_PRESERVE_MASK;
  1064.                         break;
  1065.                 case SDVOC:
  1066.                         sdvox &= SDVOC_PRESERVE_MASK;
  1067.                         break;
  1068.                 }
  1069.                 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
  1070.         }
  1071.         if (intel_crtc->pipe == 1)
  1072.                 sdvox |= SDVO_PIPE_B_SELECT;
  1073.         if (intel_sdvo->has_hdmi_audio)
  1074.                 sdvox |= SDVO_AUDIO_ENABLE;
  1075.  
  1076.         if (INTEL_INFO(dev)->gen >= 4) {
  1077.                 /* done in crtc_mode_set as the dpll_md reg must be written early */
  1078.         } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  1079.                 /* done in crtc_mode_set as it lives inside the dpll register */
  1080.         } else {
  1081.                 sdvox |= (pixel_multiplier - 1) << SDVO_PORT_MULTIPLY_SHIFT;
  1082.         }
  1083.  
  1084.         if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
  1085.             INTEL_INFO(dev)->gen < 5)
  1086.                 sdvox |= SDVO_STALL_SELECT;
  1087.         intel_sdvo_write_sdvox(intel_sdvo, sdvox);
  1088. }
  1089.  
  1090. static void intel_sdvo_dpms(struct drm_encoder *encoder, int mode)
  1091. {
  1092.         struct drm_device *dev = encoder->dev;
  1093.         struct drm_i915_private *dev_priv = dev->dev_private;
  1094.         struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  1095.         struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
  1096.         u32 temp;
  1097.  
  1098.         if (mode != DRM_MODE_DPMS_ON) {
  1099.                 intel_sdvo_set_active_outputs(intel_sdvo, 0);
  1100.                 if (0)
  1101.                         intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1102.  
  1103.                 if (mode == DRM_MODE_DPMS_OFF) {
  1104.                         temp = I915_READ(intel_sdvo->sdvo_reg);
  1105.                         if ((temp & SDVO_ENABLE) != 0) {
  1106.                                 intel_sdvo_write_sdvox(intel_sdvo, temp & ~SDVO_ENABLE);
  1107.                         }
  1108.                 }
  1109.         } else {
  1110.                 bool input1, input2;
  1111.                 int i;
  1112.                 u8 status;
  1113.  
  1114.                 temp = I915_READ(intel_sdvo->sdvo_reg);
  1115.                 if ((temp & SDVO_ENABLE) == 0)
  1116.                         intel_sdvo_write_sdvox(intel_sdvo, temp | SDVO_ENABLE);
  1117.                 for (i = 0; i < 2; i++)
  1118.                         intel_wait_for_vblank(dev, intel_crtc->pipe);
  1119.  
  1120.                 status = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
  1121.                 /* Warn if the device reported failure to sync.
  1122.                  * A lot of SDVO devices fail to notify of sync, but it's
  1123.                  * a given it the status is a success, we succeeded.
  1124.                  */
  1125.                 if (status == SDVO_CMD_STATUS_SUCCESS && !input1) {
  1126.                         DRM_DEBUG_KMS("First %s output reported failure to "
  1127.                                         "sync\n", SDVO_NAME(intel_sdvo));
  1128.                 }
  1129.  
  1130.                 if (0)
  1131.                         intel_sdvo_set_encoder_power_state(intel_sdvo, mode);
  1132.                 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
  1133.         }
  1134.         return;
  1135. }
  1136.  
  1137. static int intel_sdvo_mode_valid(struct drm_connector *connector,
  1138.                                  struct drm_display_mode *mode)
  1139. {
  1140.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1141.  
  1142.         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
  1143.                 return MODE_NO_DBLESCAN;
  1144.  
  1145.         if (intel_sdvo->pixel_clock_min > mode->clock)
  1146.                 return MODE_CLOCK_LOW;
  1147.  
  1148.         if (intel_sdvo->pixel_clock_max < mode->clock)
  1149.                 return MODE_CLOCK_HIGH;
  1150.  
  1151.         if (intel_sdvo->is_lvds) {
  1152.                 if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
  1153.                         return MODE_PANEL;
  1154.  
  1155.                 if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
  1156.                         return MODE_PANEL;
  1157.         }
  1158.  
  1159.         return MODE_OK;
  1160. }
  1161.  
  1162. static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
  1163. {
  1164.         BUILD_BUG_ON(sizeof(*caps) != 8);
  1165.         if (!intel_sdvo_get_value(intel_sdvo,
  1166.                                   SDVO_CMD_GET_DEVICE_CAPS,
  1167.                                   caps, sizeof(*caps)))
  1168.                 return false;
  1169.  
  1170.         DRM_DEBUG_KMS("SDVO capabilities:\n"
  1171.                       "  vendor_id: %d\n"
  1172.                       "  device_id: %d\n"
  1173.                       "  device_rev_id: %d\n"
  1174.                       "  sdvo_version_major: %d\n"
  1175.                       "  sdvo_version_minor: %d\n"
  1176.                       "  sdvo_inputs_mask: %d\n"
  1177.                       "  smooth_scaling: %d\n"
  1178.                       "  sharp_scaling: %d\n"
  1179.                       "  up_scaling: %d\n"
  1180.                       "  down_scaling: %d\n"
  1181.                       "  stall_support: %d\n"
  1182.                       "  output_flags: %d\n",
  1183.                       caps->vendor_id,
  1184.                       caps->device_id,
  1185.                       caps->device_rev_id,
  1186.                       caps->sdvo_version_major,
  1187.                       caps->sdvo_version_minor,
  1188.                       caps->sdvo_inputs_mask,
  1189.                       caps->smooth_scaling,
  1190.                       caps->sharp_scaling,
  1191.                       caps->up_scaling,
  1192.                       caps->down_scaling,
  1193.                       caps->stall_support,
  1194.                       caps->output_flags);
  1195.  
  1196.         return true;
  1197. }
  1198.  
  1199. /* No use! */
  1200. #if 0
  1201. struct drm_connector* intel_sdvo_find(struct drm_device *dev, int sdvoB)
  1202. {
  1203.         struct drm_connector *connector = NULL;
  1204.         struct intel_sdvo *iout = NULL;
  1205.         struct intel_sdvo *sdvo;
  1206.  
  1207.         /* find the sdvo connector */
  1208.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1209.                 iout = to_intel_sdvo(connector);
  1210.  
  1211.                 if (iout->type != INTEL_OUTPUT_SDVO)
  1212.                         continue;
  1213.  
  1214.                 sdvo = iout->dev_priv;
  1215.  
  1216.                 if (sdvo->sdvo_reg == SDVOB && sdvoB)
  1217.                         return connector;
  1218.  
  1219.                 if (sdvo->sdvo_reg == SDVOC && !sdvoB)
  1220.                         return connector;
  1221.  
  1222.         }
  1223.  
  1224.         return NULL;
  1225. }
  1226.  
  1227. int intel_sdvo_supports_hotplug(struct drm_connector *connector)
  1228. {
  1229.         u8 response[2];
  1230.         u8 status;
  1231.         struct intel_sdvo *intel_sdvo;
  1232.         DRM_DEBUG_KMS("\n");
  1233.  
  1234.         if (!connector)
  1235.                 return 0;
  1236.  
  1237.         intel_sdvo = to_intel_sdvo(connector);
  1238.  
  1239.         return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
  1240.                                     &response, 2) && response[0];
  1241. }
  1242.  
  1243. void intel_sdvo_set_hotplug(struct drm_connector *connector, int on)
  1244. {
  1245.         u8 response[2];
  1246.         u8 status;
  1247.         struct intel_sdvo *intel_sdvo = to_intel_sdvo(connector);
  1248.  
  1249.         intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1250.         intel_sdvo_read_response(intel_sdvo, &response, 2);
  1251.  
  1252.         if (on) {
  1253.                 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT, NULL, 0);
  1254.                 status = intel_sdvo_read_response(intel_sdvo, &response, 2);
  1255.  
  1256.                 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1257.         } else {
  1258.                 response[0] = 0;
  1259.                 response[1] = 0;
  1260.                 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG, &response, 2);
  1261.         }
  1262.  
  1263.         intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_GET_ACTIVE_HOT_PLUG, NULL, 0);
  1264.         intel_sdvo_read_response(intel_sdvo, &response, 2);
  1265. }
  1266. #endif
  1267.  
  1268. static bool
  1269. intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
  1270. {
  1271.         /* Is there more than one type of output? */
  1272.         int caps = intel_sdvo->caps.output_flags & 0xf;
  1273.         return caps & -caps;
  1274. }
  1275.  
  1276. static struct edid *
  1277. intel_sdvo_get_edid(struct drm_connector *connector)
  1278. {
  1279.         struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
  1280.         return drm_get_edid(connector, &sdvo->ddc);
  1281. }
  1282.  
  1283. /* Mac mini hack -- use the same DDC as the analog connector */
  1284. static struct edid *
  1285. intel_sdvo_get_analog_edid(struct drm_connector *connector)
  1286. {
  1287.         struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1288.  
  1289.         return drm_get_edid(connector,
  1290.                             &dev_priv->gmbus[dev_priv->crt_ddc_pin].adapter);
  1291. }
  1292.  
  1293. enum drm_connector_status
  1294. intel_sdvo_hdmi_sink_detect(struct drm_connector *connector)
  1295. {
  1296.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1297.         enum drm_connector_status status;
  1298.         struct edid *edid;
  1299.  
  1300.         edid = intel_sdvo_get_edid(connector);
  1301.  
  1302.         if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
  1303.                 u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
  1304.  
  1305.                 /*
  1306.                  * Don't use the 1 as the argument of DDC bus switch to get
  1307.                  * the EDID. It is used for SDVO SPD ROM.
  1308.                  */
  1309.                 for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
  1310.                         intel_sdvo->ddc_bus = ddc;
  1311.                         edid = intel_sdvo_get_edid(connector);
  1312.                         if (edid)
  1313.                                 break;
  1314.                 }
  1315.                 /*
  1316.                  * If we found the EDID on the other bus,
  1317.                  * assume that is the correct DDC bus.
  1318.                  */
  1319.                 if (edid == NULL)
  1320.                         intel_sdvo->ddc_bus = saved_ddc;
  1321.         }
  1322.  
  1323.         /*
  1324.          * When there is no edid and no monitor is connected with VGA
  1325.          * port, try to use the CRT ddc to read the EDID for DVI-connector.
  1326.          */
  1327.         if (edid == NULL)
  1328.                 edid = intel_sdvo_get_analog_edid(connector);
  1329.  
  1330.         status = connector_status_unknown;
  1331.         if (edid != NULL) {
  1332.                 /* DDC bus is shared, match EDID to connector type */
  1333.                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
  1334.                         status = connector_status_connected;
  1335.                         if (intel_sdvo->is_hdmi) {
  1336.                                 intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
  1337.                                 intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
  1338.                         }
  1339.                 } else
  1340.                         status = connector_status_disconnected;
  1341.                 connector->display_info.raw_edid = NULL;
  1342.                 kfree(edid);
  1343.         }
  1344.  
  1345.         if (status == connector_status_connected) {
  1346.                 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1347.                 if (intel_sdvo_connector->force_audio)
  1348.                         intel_sdvo->has_hdmi_audio = intel_sdvo_connector->force_audio > 0;
  1349.         }
  1350.  
  1351.         return status;
  1352. }
  1353.  
  1354. static enum drm_connector_status
  1355. intel_sdvo_detect(struct drm_connector *connector, bool force)
  1356. {
  1357.         uint16_t response;
  1358.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1359.         struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1360.         enum drm_connector_status ret;
  1361.  
  1362.         if (!intel_sdvo_write_cmd(intel_sdvo,
  1363.                                   SDVO_CMD_GET_ATTACHED_DISPLAYS, NULL, 0))
  1364.                 return connector_status_unknown;
  1365.  
  1366.         /* add 30ms delay when the output type might be TV */
  1367.         if (intel_sdvo->caps.output_flags &
  1368.             (SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_CVBS0))
  1369.                 mdelay(30);
  1370.  
  1371.         if (!intel_sdvo_read_response(intel_sdvo, &response, 2))
  1372.                 return connector_status_unknown;
  1373.  
  1374.         DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
  1375.                       response & 0xff, response >> 8,
  1376.                       intel_sdvo_connector->output_flag);
  1377.  
  1378.         if (response == 0)
  1379.                 return connector_status_disconnected;
  1380.  
  1381.         intel_sdvo->attached_output = response;
  1382.  
  1383.         intel_sdvo->has_hdmi_monitor = false;
  1384.         intel_sdvo->has_hdmi_audio = false;
  1385.  
  1386.         if ((intel_sdvo_connector->output_flag & response) == 0)
  1387.                 ret = connector_status_disconnected;
  1388.         else if (IS_TMDS(intel_sdvo_connector))
  1389.                 ret = intel_sdvo_hdmi_sink_detect(connector);
  1390.         else {
  1391.                 struct edid *edid;
  1392.  
  1393.                 /* if we have an edid check it matches the connection */
  1394.                 edid = intel_sdvo_get_edid(connector);
  1395.                 if (edid == NULL)
  1396.                         edid = intel_sdvo_get_analog_edid(connector);
  1397.                 if (edid != NULL) {
  1398.                         if (edid->input & DRM_EDID_INPUT_DIGITAL)
  1399.                                 ret = connector_status_disconnected;
  1400.                         else
  1401.                                 ret = connector_status_connected;
  1402.                         connector->display_info.raw_edid = NULL;
  1403.                         kfree(edid);
  1404.                 } else
  1405.                         ret = connector_status_connected;
  1406.         }
  1407.  
  1408.         /* May update encoder flag for like clock for SDVO TV, etc.*/
  1409.         if (ret == connector_status_connected) {
  1410.                 intel_sdvo->is_tv = false;
  1411.                 intel_sdvo->is_lvds = false;
  1412.                 intel_sdvo->base.needs_tv_clock = false;
  1413.  
  1414.                 if (response & SDVO_TV_MASK) {
  1415.                         intel_sdvo->is_tv = true;
  1416.                         intel_sdvo->base.needs_tv_clock = true;
  1417.                 }
  1418.                 if (response & SDVO_LVDS_MASK)
  1419.                         intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
  1420.         }
  1421.  
  1422.         return ret;
  1423. }
  1424.  
  1425. static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
  1426. {
  1427.         struct edid *edid;
  1428.  
  1429.         /* set the bus switch and get the modes */
  1430.         edid = intel_sdvo_get_edid(connector);
  1431.  
  1432.         /*
  1433.          * Mac mini hack.  On this device, the DVI-I connector shares one DDC
  1434.          * link between analog and digital outputs. So, if the regular SDVO
  1435.          * DDC fails, check to see if the analog output is disconnected, in
  1436.          * which case we'll look there for the digital DDC data.
  1437.          */
  1438.         if (edid == NULL)
  1439.                 edid = intel_sdvo_get_analog_edid(connector);
  1440.  
  1441.         if (edid != NULL) {
  1442.                 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1443.                 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
  1444.                 bool connector_is_digital = !!IS_TMDS(intel_sdvo_connector);
  1445.  
  1446.                 if (connector_is_digital == monitor_is_digital) {
  1447.                         drm_mode_connector_update_edid_property(connector, edid);
  1448.                         drm_add_edid_modes(connector, edid);
  1449.                 }
  1450.  
  1451.                 connector->display_info.raw_edid = NULL;
  1452.                 kfree(edid);
  1453.         }
  1454. }
  1455.  
  1456. /*
  1457.  * Set of SDVO TV modes.
  1458.  * Note!  This is in reply order (see loop in get_tv_modes).
  1459.  * XXX: all 60Hz refresh?
  1460.  */
  1461. static const struct drm_display_mode sdvo_tv_modes[] = {
  1462.         { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
  1463.                    416, 0, 200, 201, 232, 233, 0,
  1464.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1465.         { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
  1466.                    416, 0, 240, 241, 272, 273, 0,
  1467.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1468.         { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
  1469.                    496, 0, 300, 301, 332, 333, 0,
  1470.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1471.         { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
  1472.                    736, 0, 350, 351, 382, 383, 0,
  1473.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1474.         { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
  1475.                    736, 0, 400, 401, 432, 433, 0,
  1476.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1477.         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
  1478.                    736, 0, 480, 481, 512, 513, 0,
  1479.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1480.         { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
  1481.                    800, 0, 480, 481, 512, 513, 0,
  1482.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1483.         { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
  1484.                    800, 0, 576, 577, 608, 609, 0,
  1485.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1486.         { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
  1487.                    816, 0, 350, 351, 382, 383, 0,
  1488.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1489.         { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
  1490.                    816, 0, 400, 401, 432, 433, 0,
  1491.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1492.         { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
  1493.                    816, 0, 480, 481, 512, 513, 0,
  1494.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1495.         { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
  1496.                    816, 0, 540, 541, 572, 573, 0,
  1497.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1498.         { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
  1499.                    816, 0, 576, 577, 608, 609, 0,
  1500.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1501.         { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
  1502.                    864, 0, 576, 577, 608, 609, 0,
  1503.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1504.         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
  1505.                    896, 0, 600, 601, 632, 633, 0,
  1506.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1507.         { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
  1508.                    928, 0, 624, 625, 656, 657, 0,
  1509.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1510.         { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
  1511.                    1016, 0, 766, 767, 798, 799, 0,
  1512.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1513.         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
  1514.                    1120, 0, 768, 769, 800, 801, 0,
  1515.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1516.         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
  1517.                    1376, 0, 1024, 1025, 1056, 1057, 0,
  1518.                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
  1519. };
  1520.  
  1521. static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
  1522. {
  1523.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1524.         struct intel_sdvo_sdtv_resolution_request tv_res;
  1525.         uint32_t reply = 0, format_map = 0;
  1526.         int i;
  1527.  
  1528.         /* Read the list of supported input resolutions for the selected TV
  1529.          * format.
  1530.          */
  1531.         format_map = 1 << intel_sdvo->tv_format_index;
  1532.         memcpy(&tv_res, &format_map,
  1533.                min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
  1534.  
  1535.         if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
  1536.                 return;
  1537.  
  1538.         BUILD_BUG_ON(sizeof(tv_res) != 3);
  1539.         if (!intel_sdvo_write_cmd(intel_sdvo,
  1540.                                   SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
  1541.                                   &tv_res, sizeof(tv_res)))
  1542.                 return;
  1543.         if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
  1544.                 return;
  1545.  
  1546.         for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
  1547.                 if (reply & (1 << i)) {
  1548.                         struct drm_display_mode *nmode;
  1549.                         nmode = drm_mode_duplicate(connector->dev,
  1550.                                                    &sdvo_tv_modes[i]);
  1551.                         if (nmode)
  1552.                                 drm_mode_probed_add(connector, nmode);
  1553.                 }
  1554. }
  1555.  
  1556. static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
  1557. {
  1558.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1559.         struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1560.         struct drm_display_mode *newmode;
  1561.  
  1562.         /*
  1563.          * Attempt to get the mode list from DDC.
  1564.          * Assume that the preferred modes are
  1565.          * arranged in priority order.
  1566.          */
  1567.         intel_ddc_get_modes(connector, intel_sdvo->i2c);
  1568.         if (list_empty(&connector->probed_modes) == false)
  1569.                 goto end;
  1570.  
  1571.         /* Fetch modes from VBT */
  1572.         if (dev_priv->sdvo_lvds_vbt_mode != NULL) {
  1573.                 newmode = drm_mode_duplicate(connector->dev,
  1574.                                              dev_priv->sdvo_lvds_vbt_mode);
  1575.                 if (newmode != NULL) {
  1576.                         /* Guarantee the mode is preferred */
  1577.                         newmode->type = (DRM_MODE_TYPE_PREFERRED |
  1578.                                          DRM_MODE_TYPE_DRIVER);
  1579.                         drm_mode_probed_add(connector, newmode);
  1580.                 }
  1581.         }
  1582.  
  1583. end:
  1584.         list_for_each_entry(newmode, &connector->probed_modes, head) {
  1585.                 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
  1586.                         intel_sdvo->sdvo_lvds_fixed_mode =
  1587.                                 drm_mode_duplicate(connector->dev, newmode);
  1588.  
  1589.                         drm_mode_set_crtcinfo(intel_sdvo->sdvo_lvds_fixed_mode,
  1590.                                               0);
  1591.  
  1592.                         intel_sdvo->is_lvds = true;
  1593.                         break;
  1594.                 }
  1595.         }
  1596.  
  1597. }
  1598.  
  1599. static int intel_sdvo_get_modes(struct drm_connector *connector)
  1600. {
  1601.         struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1602.  
  1603.         if (IS_TV(intel_sdvo_connector))
  1604.                 intel_sdvo_get_tv_modes(connector);
  1605.         else if (IS_LVDS(intel_sdvo_connector))
  1606.                 intel_sdvo_get_lvds_modes(connector);
  1607.         else
  1608.                 intel_sdvo_get_ddc_modes(connector);
  1609.  
  1610.         return !list_empty(&connector->probed_modes);
  1611. }
  1612.  
  1613. static void
  1614. intel_sdvo_destroy_enhance_property(struct drm_connector *connector)
  1615. {
  1616.         struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1617.         struct drm_device *dev = connector->dev;
  1618.  
  1619.         if (intel_sdvo_connector->left)
  1620.                 drm_property_destroy(dev, intel_sdvo_connector->left);
  1621.         if (intel_sdvo_connector->right)
  1622.                 drm_property_destroy(dev, intel_sdvo_connector->right);
  1623.         if (intel_sdvo_connector->top)
  1624.                 drm_property_destroy(dev, intel_sdvo_connector->top);
  1625.         if (intel_sdvo_connector->bottom)
  1626.                 drm_property_destroy(dev, intel_sdvo_connector->bottom);
  1627.         if (intel_sdvo_connector->hpos)
  1628.                 drm_property_destroy(dev, intel_sdvo_connector->hpos);
  1629.         if (intel_sdvo_connector->vpos)
  1630.                 drm_property_destroy(dev, intel_sdvo_connector->vpos);
  1631.         if (intel_sdvo_connector->saturation)
  1632.                 drm_property_destroy(dev, intel_sdvo_connector->saturation);
  1633.         if (intel_sdvo_connector->contrast)
  1634.                 drm_property_destroy(dev, intel_sdvo_connector->contrast);
  1635.         if (intel_sdvo_connector->hue)
  1636.                 drm_property_destroy(dev, intel_sdvo_connector->hue);
  1637.         if (intel_sdvo_connector->sharpness)
  1638.                 drm_property_destroy(dev, intel_sdvo_connector->sharpness);
  1639.         if (intel_sdvo_connector->flicker_filter)
  1640.                 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter);
  1641.         if (intel_sdvo_connector->flicker_filter_2d)
  1642.                 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_2d);
  1643.         if (intel_sdvo_connector->flicker_filter_adaptive)
  1644.                 drm_property_destroy(dev, intel_sdvo_connector->flicker_filter_adaptive);
  1645.         if (intel_sdvo_connector->tv_luma_filter)
  1646.                 drm_property_destroy(dev, intel_sdvo_connector->tv_luma_filter);
  1647.         if (intel_sdvo_connector->tv_chroma_filter)
  1648.                 drm_property_destroy(dev, intel_sdvo_connector->tv_chroma_filter);
  1649.         if (intel_sdvo_connector->dot_crawl)
  1650.                 drm_property_destroy(dev, intel_sdvo_connector->dot_crawl);
  1651.         if (intel_sdvo_connector->brightness)
  1652.                 drm_property_destroy(dev, intel_sdvo_connector->brightness);
  1653. }
  1654.  
  1655. static void intel_sdvo_destroy(struct drm_connector *connector)
  1656. {
  1657.         struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1658.  
  1659.         if (intel_sdvo_connector->tv_format)
  1660.                 drm_property_destroy(connector->dev,
  1661.                                      intel_sdvo_connector->tv_format);
  1662.  
  1663.         intel_sdvo_destroy_enhance_property(connector);
  1664.         drm_sysfs_connector_remove(connector);
  1665.         drm_connector_cleanup(connector);
  1666.         kfree(connector);
  1667. }
  1668.  
  1669.  
  1670.  
  1671.  
  1672.  
  1673. static int
  1674. intel_sdvo_set_property(struct drm_connector *connector,
  1675.                         struct drm_property *property,
  1676.                         uint64_t val)
  1677. {
  1678.         struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
  1679.         struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
  1680.         struct drm_i915_private *dev_priv = connector->dev->dev_private;
  1681.         uint16_t temp_value;
  1682.         uint8_t cmd;
  1683.         int ret;
  1684.  
  1685.         ret = drm_connector_property_set_value(connector, property, val);
  1686.         if (ret)
  1687.                 return ret;
  1688.  
  1689. #if 0
  1690.         if (property == dev_priv->force_audio_property) {
  1691.                 int i = val;
  1692.                 bool has_audio;
  1693.  
  1694.                 if (i == intel_sdvo_connector->force_audio)
  1695.                         return 0;
  1696.  
  1697.                 intel_sdvo_connector->force_audio = i;
  1698.  
  1699.                 if (i == 0)
  1700.                         has_audio = intel_sdvo_detect_hdmi_audio(connector);
  1701.                 else
  1702.                         has_audio = i > 0;
  1703.  
  1704.                 if (has_audio == intel_sdvo->has_hdmi_audio)
  1705.                         return 0;
  1706.  
  1707.                 intel_sdvo->has_hdmi_audio = has_audio;
  1708.                 goto done;
  1709.         }
  1710.  
  1711.         if (property == dev_priv->broadcast_rgb_property) {
  1712.                 if (val == !!intel_sdvo->color_range)
  1713.                         return 0;
  1714.  
  1715.                 intel_sdvo->color_range = val ? SDVO_COLOR_RANGE_16_235 : 0;
  1716.                 goto done;
  1717.         }
  1718. #endif
  1719.  
  1720. #define CHECK_PROPERTY(name, NAME) \
  1721.         if (intel_sdvo_connector->name == property) { \
  1722.                 if (intel_sdvo_connector->cur_##name == temp_value) return 0; \
  1723.                 if (intel_sdvo_connector->max_##name < temp_value) return -EINVAL; \
  1724.                 cmd = SDVO_CMD_SET_##NAME; \
  1725.                 intel_sdvo_connector->cur_##name = temp_value; \
  1726.                 goto set_value; \
  1727.         }
  1728.  
  1729.         if (property == intel_sdvo_connector->tv_format) {
  1730.                 if (val >= TV_FORMAT_NUM)
  1731.                         return -EINVAL;
  1732.  
  1733.                 if (intel_sdvo->tv_format_index ==
  1734.                     intel_sdvo_connector->tv_format_supported[val])
  1735.                         return 0;
  1736.  
  1737.                 intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[val];
  1738.                 goto done;
  1739.         } else if (IS_TV_OR_LVDS(intel_sdvo_connector)) {
  1740.                 temp_value = val;
  1741.                 if (intel_sdvo_connector->left == property) {
  1742.                         drm_connector_property_set_value(connector,
  1743.                                                          intel_sdvo_connector->right, val);
  1744.                         if (intel_sdvo_connector->left_margin == temp_value)
  1745.                                 return 0;
  1746.  
  1747.                         intel_sdvo_connector->left_margin = temp_value;
  1748.                         intel_sdvo_connector->right_margin = temp_value;
  1749.                         temp_value = intel_sdvo_connector->max_hscan -
  1750.                                 intel_sdvo_connector->left_margin;
  1751.                         cmd = SDVO_CMD_SET_OVERSCAN_H;
  1752.                         goto set_value;
  1753.                 } else if (intel_sdvo_connector->right == property) {
  1754.                         drm_connector_property_set_value(connector,
  1755.                                                          intel_sdvo_connector->left, val);
  1756.                         if (intel_sdvo_connector->right_margin == temp_value)
  1757.                                 return 0;
  1758.  
  1759.                         intel_sdvo_connector->left_margin = temp_value;
  1760.                         intel_sdvo_connector->right_margin = temp_value;
  1761.                         temp_value = intel_sdvo_connector->max_hscan -
  1762.                                 intel_sdvo_connector->left_margin;
  1763.                         cmd = SDVO_CMD_SET_OVERSCAN_H;
  1764.                         goto set_value;
  1765.                 } else if (intel_sdvo_connector->top == property) {
  1766.                         drm_connector_property_set_value(connector,
  1767.                                                          intel_sdvo_connector->bottom, val);
  1768.                         if (intel_sdvo_connector->top_margin == temp_value)
  1769.                                 return 0;
  1770.  
  1771.                         intel_sdvo_connector->top_margin = temp_value;
  1772.                         intel_sdvo_connector->bottom_margin = temp_value;
  1773.                         temp_value = intel_sdvo_connector->max_vscan -
  1774.                                 intel_sdvo_connector->top_margin;
  1775.                         cmd = SDVO_CMD_SET_OVERSCAN_V;
  1776.                         goto set_value;
  1777.                 } else if (intel_sdvo_connector->bottom == property) {
  1778.                         drm_connector_property_set_value(connector,
  1779.                                                          intel_sdvo_connector->top, val);
  1780.                         if (intel_sdvo_connector->bottom_margin == temp_value)
  1781.                                 return 0;
  1782.  
  1783.                         intel_sdvo_connector->top_margin = temp_value;
  1784.                         intel_sdvo_connector->bottom_margin = temp_value;
  1785.                         temp_value = intel_sdvo_connector->max_vscan -
  1786.                                 intel_sdvo_connector->top_margin;
  1787.                         cmd = SDVO_CMD_SET_OVERSCAN_V;
  1788.                         goto set_value;
  1789.                 }
  1790.                 CHECK_PROPERTY(hpos, HPOS)
  1791.                 CHECK_PROPERTY(vpos, VPOS)
  1792.                 CHECK_PROPERTY(saturation, SATURATION)
  1793.                 CHECK_PROPERTY(contrast, CONTRAST)
  1794.                 CHECK_PROPERTY(hue, HUE)
  1795.                 CHECK_PROPERTY(brightness, BRIGHTNESS)
  1796.                 CHECK_PROPERTY(sharpness, SHARPNESS)
  1797.                 CHECK_PROPERTY(flicker_filter, FLICKER_FILTER)
  1798.                 CHECK_PROPERTY(flicker_filter_2d, FLICKER_FILTER_2D)
  1799.                 CHECK_PROPERTY(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE)
  1800.                 CHECK_PROPERTY(tv_chroma_filter, TV_CHROMA_FILTER)
  1801.                 CHECK_PROPERTY(tv_luma_filter, TV_LUMA_FILTER)
  1802.                 CHECK_PROPERTY(dot_crawl, DOT_CRAWL)
  1803.         }
  1804.  
  1805.         return -EINVAL; /* unknown property */
  1806.  
  1807. set_value:
  1808.         if (!intel_sdvo_set_value(intel_sdvo, cmd, &temp_value, 2))
  1809.                 return -EIO;
  1810.  
  1811.  
  1812. done:
  1813.         if (intel_sdvo->base.base.crtc) {
  1814.                 struct drm_crtc *crtc = intel_sdvo->base.base.crtc;
  1815.                 drm_crtc_helper_set_mode(crtc, &crtc->mode, crtc->x,
  1816.                                          crtc->y, crtc->fb);
  1817.         }
  1818.  
  1819.         return 0;
  1820. #undef CHECK_PROPERTY
  1821. }
  1822.  
  1823. static const struct drm_encoder_helper_funcs intel_sdvo_helper_funcs = {
  1824.         .dpms = intel_sdvo_dpms,
  1825.         .mode_fixup = intel_sdvo_mode_fixup,
  1826.         .prepare = intel_encoder_prepare,
  1827.         .mode_set = intel_sdvo_mode_set,
  1828.         .commit = intel_encoder_commit,
  1829. };
  1830.  
  1831. static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
  1832.         .dpms = drm_helper_connector_dpms,
  1833.         .detect = intel_sdvo_detect,
  1834.         .fill_modes = drm_helper_probe_single_connector_modes,
  1835.         .set_property = intel_sdvo_set_property,
  1836.         .destroy = intel_sdvo_destroy,
  1837. };
  1838.  
  1839. static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
  1840.         .get_modes = intel_sdvo_get_modes,
  1841.         .mode_valid = intel_sdvo_mode_valid,
  1842.         .best_encoder = intel_best_encoder,
  1843. };
  1844.  
  1845. static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
  1846. {
  1847.         struct intel_sdvo *intel_sdvo = to_intel_sdvo(encoder);
  1848.  
  1849.         if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
  1850.                 drm_mode_destroy(encoder->dev,
  1851.                                  intel_sdvo->sdvo_lvds_fixed_mode);
  1852.  
  1853. //   i2c_del_adapter(&intel_sdvo->ddc);
  1854.         intel_encoder_destroy(encoder);
  1855. }
  1856.  
  1857. static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
  1858.         .destroy = intel_sdvo_enc_destroy,
  1859. };
  1860.  
  1861. static void
  1862. intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
  1863. {
  1864.         uint16_t mask = 0;
  1865.         unsigned int num_bits;
  1866.  
  1867.         /* Make a mask of outputs less than or equal to our own priority in the
  1868.          * list.
  1869.          */
  1870.         switch (sdvo->controlled_output) {
  1871.         case SDVO_OUTPUT_LVDS1:
  1872.                 mask |= SDVO_OUTPUT_LVDS1;
  1873.         case SDVO_OUTPUT_LVDS0:
  1874.                 mask |= SDVO_OUTPUT_LVDS0;
  1875.         case SDVO_OUTPUT_TMDS1:
  1876.                 mask |= SDVO_OUTPUT_TMDS1;
  1877.         case SDVO_OUTPUT_TMDS0:
  1878.                 mask |= SDVO_OUTPUT_TMDS0;
  1879.         case SDVO_OUTPUT_RGB1:
  1880.                 mask |= SDVO_OUTPUT_RGB1;
  1881.         case SDVO_OUTPUT_RGB0:
  1882.                 mask |= SDVO_OUTPUT_RGB0;
  1883.                 break;
  1884.         }
  1885.  
  1886.         /* Count bits to find what number we are in the priority list. */
  1887.         mask &= sdvo->caps.output_flags;
  1888.         num_bits = hweight16(mask);
  1889.         /* If more than 3 outputs, default to DDC bus 3 for now. */
  1890.         if (num_bits > 3)
  1891.                 num_bits = 3;
  1892.  
  1893.         /* Corresponds to SDVO_CONTROL_BUS_DDCx */
  1894.         sdvo->ddc_bus = 1 << num_bits;
  1895. }
  1896.  
  1897. /**
  1898.  * Choose the appropriate DDC bus for control bus switch command for this
  1899.  * SDVO output based on the controlled output.
  1900.  *
  1901.  * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
  1902.  * outputs, then LVDS outputs.
  1903.  */
  1904. static void
  1905. intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
  1906.                           struct intel_sdvo *sdvo, u32 reg)
  1907. {
  1908.         struct sdvo_device_mapping *mapping;
  1909.  
  1910.         if (IS_SDVOB(reg))
  1911.                 mapping = &(dev_priv->sdvo_mappings[0]);
  1912.         else
  1913.                 mapping = &(dev_priv->sdvo_mappings[1]);
  1914.  
  1915.         if (mapping->initialized)
  1916.                 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
  1917.         else
  1918.                 intel_sdvo_guess_ddc_bus(sdvo);
  1919. }
  1920.  
  1921. static void
  1922. intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
  1923.                           struct intel_sdvo *sdvo, u32 reg)
  1924. {
  1925.         struct sdvo_device_mapping *mapping;
  1926.         u8 pin, speed;
  1927.  
  1928.         if (IS_SDVOB(reg))
  1929.                 mapping = &dev_priv->sdvo_mappings[0];
  1930.         else
  1931.                 mapping = &dev_priv->sdvo_mappings[1];
  1932.  
  1933.         pin = GMBUS_PORT_DPB;
  1934.         speed = GMBUS_RATE_1MHZ >> 8;
  1935.         if (mapping->initialized) {
  1936.                 pin = mapping->i2c_pin;
  1937.                 speed = mapping->i2c_speed;
  1938.         }
  1939.  
  1940.         if (pin < GMBUS_NUM_PORTS) {
  1941.                 sdvo->i2c = &dev_priv->gmbus[pin].adapter;
  1942.                 intel_gmbus_set_speed(sdvo->i2c, speed);
  1943.                 intel_gmbus_force_bit(sdvo->i2c, true);
  1944.         } else
  1945.                 sdvo->i2c = &dev_priv->gmbus[GMBUS_PORT_DPB].adapter;
  1946. }
  1947.  
  1948. static bool
  1949. intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
  1950. {
  1951.         return intel_sdvo_check_supp_encode(intel_sdvo);
  1952. }
  1953.  
  1954. static u8
  1955. intel_sdvo_get_slave_addr(struct drm_device *dev, int sdvo_reg)
  1956. {
  1957.         struct drm_i915_private *dev_priv = dev->dev_private;
  1958.         struct sdvo_device_mapping *my_mapping, *other_mapping;
  1959.  
  1960.         if (IS_SDVOB(sdvo_reg)) {
  1961.                 my_mapping = &dev_priv->sdvo_mappings[0];
  1962.                 other_mapping = &dev_priv->sdvo_mappings[1];
  1963.         } else {
  1964.                 my_mapping = &dev_priv->sdvo_mappings[1];
  1965.                 other_mapping = &dev_priv->sdvo_mappings[0];
  1966.         }
  1967.  
  1968.         /* If the BIOS described our SDVO device, take advantage of it. */
  1969.         if (my_mapping->slave_addr)
  1970.                 return my_mapping->slave_addr;
  1971.  
  1972.         /* If the BIOS only described a different SDVO device, use the
  1973.          * address that it isn't using.
  1974.          */
  1975.         if (other_mapping->slave_addr) {
  1976.                 if (other_mapping->slave_addr == 0x70)
  1977.                         return 0x72;
  1978.                 else
  1979.                         return 0x70;
  1980.         }
  1981.  
  1982.         /* No SDVO device info is found for another DVO port,
  1983.          * so use mapping assumption we had before BIOS parsing.
  1984.          */
  1985.         if (IS_SDVOB(sdvo_reg))
  1986.                 return 0x70;
  1987.         else
  1988.                 return 0x72;
  1989. }
  1990.  
  1991. static void
  1992. intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
  1993.                           struct intel_sdvo *encoder)
  1994. {
  1995.         drm_connector_init(encoder->base.base.dev,
  1996.                            &connector->base.base,
  1997.                            &intel_sdvo_connector_funcs,
  1998.                            connector->base.base.connector_type);
  1999.  
  2000.         drm_connector_helper_add(&connector->base.base,
  2001.                                  &intel_sdvo_connector_helper_funcs);
  2002.  
  2003.         connector->base.base.interlace_allowed = 0;
  2004.         connector->base.base.doublescan_allowed = 0;
  2005.         connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
  2006.  
  2007.         intel_connector_attach_encoder(&connector->base, &encoder->base);
  2008.         drm_sysfs_connector_add(&connector->base.base);
  2009. }
  2010.  
  2011. static void
  2012. intel_sdvo_add_hdmi_properties(struct intel_sdvo_connector *connector)
  2013. {
  2014.         struct drm_device *dev = connector->base.base.dev;
  2015.  
  2016.         intel_attach_force_audio_property(&connector->base.base);
  2017.         if (INTEL_INFO(dev)->gen >= 4 && IS_MOBILE(dev))
  2018.                 intel_attach_broadcast_rgb_property(&connector->base.base);
  2019. }
  2020.  
  2021. static bool
  2022. intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
  2023. {
  2024.         struct drm_encoder *encoder = &intel_sdvo->base.base;
  2025.         struct drm_connector *connector;
  2026.         struct intel_connector *intel_connector;
  2027.         struct intel_sdvo_connector *intel_sdvo_connector;
  2028.  
  2029.         intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2030.         if (!intel_sdvo_connector)
  2031.                 return false;
  2032.  
  2033.         if (device == 0) {
  2034.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
  2035.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
  2036.         } else if (device == 1) {
  2037.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
  2038.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
  2039.         }
  2040.  
  2041.         intel_connector = &intel_sdvo_connector->base;
  2042.         connector = &intel_connector->base;
  2043.         connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
  2044.         encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
  2045.         connector->connector_type = DRM_MODE_CONNECTOR_DVID;
  2046.  
  2047.         if (intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
  2048.                 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
  2049.                 intel_sdvo->is_hdmi = true;
  2050.         }
  2051.         intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
  2052.                                        (1 << INTEL_ANALOG_CLONE_BIT));
  2053.  
  2054.         intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  2055.         if (intel_sdvo->is_hdmi)
  2056.                 intel_sdvo_add_hdmi_properties(intel_sdvo_connector);
  2057.  
  2058.         return true;
  2059. }
  2060.  
  2061. static bool
  2062. intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
  2063. {
  2064.         struct drm_encoder *encoder = &intel_sdvo->base.base;
  2065.         struct drm_connector *connector;
  2066.         struct intel_connector *intel_connector;
  2067.         struct intel_sdvo_connector *intel_sdvo_connector;
  2068.  
  2069.         intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2070.         if (!intel_sdvo_connector)
  2071.                 return false;
  2072.  
  2073.         intel_connector = &intel_sdvo_connector->base;
  2074.         connector = &intel_connector->base;
  2075.         encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
  2076.         connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
  2077.  
  2078.         intel_sdvo->controlled_output |= type;
  2079.         intel_sdvo_connector->output_flag = type;
  2080.  
  2081.         intel_sdvo->is_tv = true;
  2082.         intel_sdvo->base.needs_tv_clock = true;
  2083.         intel_sdvo->base.clone_mask = 1 << INTEL_SDVO_TV_CLONE_BIT;
  2084.  
  2085.         intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  2086.  
  2087.         if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
  2088.                 goto err;
  2089.  
  2090.         if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2091.                 goto err;
  2092.  
  2093.         return true;
  2094.  
  2095. err:
  2096.         intel_sdvo_destroy(connector);
  2097.         return false;
  2098. }
  2099.  
  2100. static bool
  2101. intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
  2102. {
  2103.         struct drm_encoder *encoder = &intel_sdvo->base.base;
  2104.         struct drm_connector *connector;
  2105.         struct intel_connector *intel_connector;
  2106.         struct intel_sdvo_connector *intel_sdvo_connector;
  2107.  
  2108.         intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2109.         if (!intel_sdvo_connector)
  2110.                 return false;
  2111.  
  2112.         intel_connector = &intel_sdvo_connector->base;
  2113.         connector = &intel_connector->base;
  2114.         connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  2115.         encoder->encoder_type = DRM_MODE_ENCODER_DAC;
  2116.         connector->connector_type = DRM_MODE_CONNECTOR_VGA;
  2117.  
  2118.         if (device == 0) {
  2119.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
  2120.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
  2121.         } else if (device == 1) {
  2122.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
  2123.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
  2124.         }
  2125.  
  2126.         intel_sdvo->base.clone_mask = ((1 << INTEL_SDVO_NON_TV_CLONE_BIT) |
  2127.                                        (1 << INTEL_ANALOG_CLONE_BIT));
  2128.  
  2129.         intel_sdvo_connector_init(intel_sdvo_connector,
  2130.                                   intel_sdvo);
  2131.         return true;
  2132. }
  2133.  
  2134. static bool
  2135. intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
  2136. {
  2137.         struct drm_encoder *encoder = &intel_sdvo->base.base;
  2138.         struct drm_connector *connector;
  2139.         struct intel_connector *intel_connector;
  2140.         struct intel_sdvo_connector *intel_sdvo_connector;
  2141.  
  2142.         intel_sdvo_connector = kzalloc(sizeof(struct intel_sdvo_connector), GFP_KERNEL);
  2143.         if (!intel_sdvo_connector)
  2144.                 return false;
  2145.  
  2146.         intel_connector = &intel_sdvo_connector->base;
  2147.         connector = &intel_connector->base;
  2148.         encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
  2149.         connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
  2150.  
  2151.         if (device == 0) {
  2152.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
  2153.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
  2154.         } else if (device == 1) {
  2155.                 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
  2156.                 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
  2157.         }
  2158.  
  2159.         intel_sdvo->base.clone_mask = ((1 << INTEL_ANALOG_CLONE_BIT) |
  2160.                                        (1 << INTEL_SDVO_LVDS_CLONE_BIT));
  2161.  
  2162.         intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo);
  2163.         if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
  2164.                 goto err;
  2165.  
  2166.         return true;
  2167.  
  2168. err:
  2169.         intel_sdvo_destroy(connector);
  2170.         return false;
  2171. }
  2172.  
  2173. static bool
  2174. intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
  2175. {
  2176.         intel_sdvo->is_tv = false;
  2177.         intel_sdvo->base.needs_tv_clock = false;
  2178.         intel_sdvo->is_lvds = false;
  2179.  
  2180.         /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
  2181.  
  2182.         if (flags & SDVO_OUTPUT_TMDS0)
  2183.                 if (!intel_sdvo_dvi_init(intel_sdvo, 0))
  2184.                         return false;
  2185.  
  2186.         if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
  2187.                 if (!intel_sdvo_dvi_init(intel_sdvo, 1))
  2188.                         return false;
  2189.  
  2190.         /* TV has no XXX1 function block */
  2191.         if (flags & SDVO_OUTPUT_SVID0)
  2192.                 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
  2193.                         return false;
  2194.  
  2195.         if (flags & SDVO_OUTPUT_CVBS0)
  2196.                 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
  2197.                         return false;
  2198.  
  2199.         if (flags & SDVO_OUTPUT_RGB0)
  2200.                 if (!intel_sdvo_analog_init(intel_sdvo, 0))
  2201.                         return false;
  2202.  
  2203.         if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
  2204.                 if (!intel_sdvo_analog_init(intel_sdvo, 1))
  2205.                         return false;
  2206.  
  2207.         if (flags & SDVO_OUTPUT_LVDS0)
  2208.                 if (!intel_sdvo_lvds_init(intel_sdvo, 0))
  2209.                         return false;
  2210.  
  2211.         if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
  2212.                 if (!intel_sdvo_lvds_init(intel_sdvo, 1))
  2213.                         return false;
  2214.  
  2215.         if ((flags & SDVO_OUTPUT_MASK) == 0) {
  2216.                 unsigned char bytes[2];
  2217.  
  2218.                 intel_sdvo->controlled_output = 0;
  2219.                 memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
  2220.                 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
  2221.                               SDVO_NAME(intel_sdvo),
  2222.                               bytes[0], bytes[1]);
  2223.                 return false;
  2224.         }
  2225.         intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1);
  2226.  
  2227.         return true;
  2228. }
  2229.  
  2230. static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
  2231.                                           struct intel_sdvo_connector *intel_sdvo_connector,
  2232.                                           int type)
  2233. {
  2234.         struct drm_device *dev = intel_sdvo->base.base.dev;
  2235.         struct intel_sdvo_tv_format format;
  2236.         uint32_t format_map, i;
  2237.  
  2238.         if (!intel_sdvo_set_target_output(intel_sdvo, type))
  2239.                 return false;
  2240.  
  2241.         BUILD_BUG_ON(sizeof(format) != 6);
  2242.         if (!intel_sdvo_get_value(intel_sdvo,
  2243.                                   SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
  2244.                                   &format, sizeof(format)))
  2245.                 return false;
  2246.  
  2247.         memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
  2248.  
  2249.         if (format_map == 0)
  2250.                 return false;
  2251.  
  2252.         intel_sdvo_connector->format_supported_num = 0;
  2253.         for (i = 0 ; i < TV_FORMAT_NUM; i++)
  2254.                 if (format_map & (1 << i))
  2255.                         intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
  2256.  
  2257.  
  2258.         intel_sdvo_connector->tv_format =
  2259.                         drm_property_create(dev, DRM_MODE_PROP_ENUM,
  2260.                                             "mode", intel_sdvo_connector->format_supported_num);
  2261.         if (!intel_sdvo_connector->tv_format)
  2262.                 return false;
  2263.  
  2264.         for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
  2265.                 drm_property_add_enum(
  2266.                                 intel_sdvo_connector->tv_format, i,
  2267.                                 i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
  2268.  
  2269.         intel_sdvo->tv_format_index = intel_sdvo_connector->tv_format_supported[0];
  2270.         drm_connector_attach_property(&intel_sdvo_connector->base.base,
  2271.                                       intel_sdvo_connector->tv_format, 0);
  2272.         return true;
  2273.  
  2274. }
  2275.  
  2276. #define ENHANCEMENT(name, NAME) do { \
  2277.         if (enhancements.name) { \
  2278.                 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
  2279.                     !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
  2280.                         return false; \
  2281.                 intel_sdvo_connector->max_##name = data_value[0]; \
  2282.                 intel_sdvo_connector->cur_##name = response; \
  2283.                 intel_sdvo_connector->name = \
  2284.                         drm_property_create(dev, DRM_MODE_PROP_RANGE, #name, 2); \
  2285.                 if (!intel_sdvo_connector->name) return false; \
  2286.                 intel_sdvo_connector->name->values[0] = 0; \
  2287.                 intel_sdvo_connector->name->values[1] = data_value[0]; \
  2288.                 drm_connector_attach_property(connector, \
  2289.                                               intel_sdvo_connector->name, \
  2290.                                               intel_sdvo_connector->cur_##name); \
  2291.                 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
  2292.                               data_value[0], data_value[1], response); \
  2293.         } \
  2294. } while(0)
  2295.  
  2296. static bool
  2297. intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
  2298.                                       struct intel_sdvo_connector *intel_sdvo_connector,
  2299.                                       struct intel_sdvo_enhancements_reply enhancements)
  2300. {
  2301.         struct drm_device *dev = intel_sdvo->base.base.dev;
  2302.         struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2303.         uint16_t response, data_value[2];
  2304.  
  2305.         /* when horizontal overscan is supported, Add the left/right  property */
  2306.         if (enhancements.overscan_h) {
  2307.                 if (!intel_sdvo_get_value(intel_sdvo,
  2308.                                           SDVO_CMD_GET_MAX_OVERSCAN_H,
  2309.                                           &data_value, 4))
  2310.                         return false;
  2311.  
  2312.                 if (!intel_sdvo_get_value(intel_sdvo,
  2313.                                           SDVO_CMD_GET_OVERSCAN_H,
  2314.                                           &response, 2))
  2315.                         return false;
  2316.  
  2317.                 intel_sdvo_connector->max_hscan = data_value[0];
  2318.                 intel_sdvo_connector->left_margin = data_value[0] - response;
  2319.                 intel_sdvo_connector->right_margin = intel_sdvo_connector->left_margin;
  2320.                 intel_sdvo_connector->left =
  2321.                         drm_property_create(dev, DRM_MODE_PROP_RANGE,
  2322.                                             "left_margin", 2);
  2323.                 if (!intel_sdvo_connector->left)
  2324.                         return false;
  2325.  
  2326.                 intel_sdvo_connector->left->values[0] = 0;
  2327.                 intel_sdvo_connector->left->values[1] = data_value[0];
  2328.                 drm_connector_attach_property(connector,
  2329.                                               intel_sdvo_connector->left,
  2330.                                               intel_sdvo_connector->left_margin);
  2331.  
  2332.                 intel_sdvo_connector->right =
  2333.                         drm_property_create(dev, DRM_MODE_PROP_RANGE,
  2334.                                             "right_margin", 2);
  2335.                 if (!intel_sdvo_connector->right)
  2336.                         return false;
  2337.  
  2338.                 intel_sdvo_connector->right->values[0] = 0;
  2339.                 intel_sdvo_connector->right->values[1] = data_value[0];
  2340.                 drm_connector_attach_property(connector,
  2341.                                               intel_sdvo_connector->right,
  2342.                                               intel_sdvo_connector->right_margin);
  2343.                 DRM_DEBUG_KMS("h_overscan: max %d, "
  2344.                               "default %d, current %d\n",
  2345.                               data_value[0], data_value[1], response);
  2346.         }
  2347.  
  2348.         if (enhancements.overscan_v) {
  2349.                 if (!intel_sdvo_get_value(intel_sdvo,
  2350.                                           SDVO_CMD_GET_MAX_OVERSCAN_V,
  2351.                                           &data_value, 4))
  2352.                         return false;
  2353.  
  2354.                 if (!intel_sdvo_get_value(intel_sdvo,
  2355.                                           SDVO_CMD_GET_OVERSCAN_V,
  2356.                                           &response, 2))
  2357.                         return false;
  2358.  
  2359.                 intel_sdvo_connector->max_vscan = data_value[0];
  2360.                 intel_sdvo_connector->top_margin = data_value[0] - response;
  2361.                 intel_sdvo_connector->bottom_margin = intel_sdvo_connector->top_margin;
  2362.                 intel_sdvo_connector->top =
  2363.                         drm_property_create(dev, DRM_MODE_PROP_RANGE,
  2364.                                             "top_margin", 2);
  2365.                 if (!intel_sdvo_connector->top)
  2366.                         return false;
  2367.  
  2368.                 intel_sdvo_connector->top->values[0] = 0;
  2369.                 intel_sdvo_connector->top->values[1] = data_value[0];
  2370.                 drm_connector_attach_property(connector,
  2371.                                               intel_sdvo_connector->top,
  2372.                                               intel_sdvo_connector->top_margin);
  2373.  
  2374.                 intel_sdvo_connector->bottom =
  2375.                         drm_property_create(dev, DRM_MODE_PROP_RANGE,
  2376.                                             "bottom_margin", 2);
  2377.                 if (!intel_sdvo_connector->bottom)
  2378.                         return false;
  2379.  
  2380.                 intel_sdvo_connector->bottom->values[0] = 0;
  2381.                 intel_sdvo_connector->bottom->values[1] = data_value[0];
  2382.                 drm_connector_attach_property(connector,
  2383.                                               intel_sdvo_connector->bottom,
  2384.                                               intel_sdvo_connector->bottom_margin);
  2385.                 DRM_DEBUG_KMS("v_overscan: max %d, "
  2386.                               "default %d, current %d\n",
  2387.                               data_value[0], data_value[1], response);
  2388.         }
  2389.  
  2390.         ENHANCEMENT(hpos, HPOS);
  2391.         ENHANCEMENT(vpos, VPOS);
  2392.         ENHANCEMENT(saturation, SATURATION);
  2393.         ENHANCEMENT(contrast, CONTRAST);
  2394.         ENHANCEMENT(hue, HUE);
  2395.         ENHANCEMENT(sharpness, SHARPNESS);
  2396.         ENHANCEMENT(brightness, BRIGHTNESS);
  2397.         ENHANCEMENT(flicker_filter, FLICKER_FILTER);
  2398.         ENHANCEMENT(flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
  2399.         ENHANCEMENT(flicker_filter_2d, FLICKER_FILTER_2D);
  2400.         ENHANCEMENT(tv_chroma_filter, TV_CHROMA_FILTER);
  2401.         ENHANCEMENT(tv_luma_filter, TV_LUMA_FILTER);
  2402.  
  2403.         if (enhancements.dot_crawl) {
  2404.                 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
  2405.                         return false;
  2406.  
  2407.                 intel_sdvo_connector->max_dot_crawl = 1;
  2408.                 intel_sdvo_connector->cur_dot_crawl = response & 0x1;
  2409.                 intel_sdvo_connector->dot_crawl =
  2410.                         drm_property_create(dev, DRM_MODE_PROP_RANGE, "dot_crawl", 2);
  2411.                 if (!intel_sdvo_connector->dot_crawl)
  2412.                         return false;
  2413.  
  2414.                 intel_sdvo_connector->dot_crawl->values[0] = 0;
  2415.                 intel_sdvo_connector->dot_crawl->values[1] = 1;
  2416.                 drm_connector_attach_property(connector,
  2417.                                               intel_sdvo_connector->dot_crawl,
  2418.                                               intel_sdvo_connector->cur_dot_crawl);
  2419.                 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
  2420.         }
  2421.  
  2422.         return true;
  2423. }
  2424.  
  2425. static bool
  2426. intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
  2427.                                         struct intel_sdvo_connector *intel_sdvo_connector,
  2428.                                         struct intel_sdvo_enhancements_reply enhancements)
  2429. {
  2430.         struct drm_device *dev = intel_sdvo->base.base.dev;
  2431.         struct drm_connector *connector = &intel_sdvo_connector->base.base;
  2432.         uint16_t response, data_value[2];
  2433.  
  2434.         ENHANCEMENT(brightness, BRIGHTNESS);
  2435.  
  2436.         return true;
  2437. }
  2438. #undef ENHANCEMENT
  2439.  
  2440. static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
  2441.                                                struct intel_sdvo_connector *intel_sdvo_connector)
  2442. {
  2443.         union {
  2444.                 struct intel_sdvo_enhancements_reply reply;
  2445.                 uint16_t response;
  2446.         } enhancements;
  2447.  
  2448.         BUILD_BUG_ON(sizeof(enhancements) != 2);
  2449.  
  2450.         enhancements.response = 0;
  2451.         intel_sdvo_get_value(intel_sdvo,
  2452.                              SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
  2453.                              &enhancements, sizeof(enhancements));
  2454.         if (enhancements.response == 0) {
  2455.                 DRM_DEBUG_KMS("No enhancement is supported\n");
  2456.                 return true;
  2457.         }
  2458.  
  2459.         if (IS_TV(intel_sdvo_connector))
  2460.                 return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2461.         else if(IS_LVDS(intel_sdvo_connector))
  2462.                 return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
  2463.         else
  2464.                 return true;
  2465. }
  2466.  
  2467. static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
  2468.                                      struct i2c_msg *msgs,
  2469.                                      int num)
  2470. {
  2471.         struct intel_sdvo *sdvo = adapter->algo_data;
  2472.  
  2473.         if (!intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
  2474.                 return -EIO;
  2475.  
  2476.         return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
  2477. }
  2478.  
  2479. static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
  2480. {
  2481.         struct intel_sdvo *sdvo = adapter->algo_data;
  2482.         return sdvo->i2c->algo->functionality(sdvo->i2c);
  2483. }
  2484.  
  2485. static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
  2486.         .master_xfer    = intel_sdvo_ddc_proxy_xfer,
  2487.         .functionality  = intel_sdvo_ddc_proxy_func
  2488. };
  2489.  
  2490. static bool
  2491. intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
  2492.                           struct drm_device *dev)
  2493. {
  2494. //   sdvo->ddc.owner = THIS_MODULE;
  2495.         sdvo->ddc.class = I2C_CLASS_DDC;
  2496.         snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
  2497.         sdvo->ddc.dev.parent = &dev->pdev->dev;
  2498.         sdvo->ddc.algo_data = sdvo;
  2499.         sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
  2500.  
  2501.     return 1; //i2c_add_adapter(&sdvo->ddc) == 0;
  2502. }
  2503.  
  2504. bool intel_sdvo_init(struct drm_device *dev, int sdvo_reg)
  2505. {
  2506.     struct drm_i915_private *dev_priv = dev->dev_private;
  2507.     struct intel_encoder *intel_encoder;
  2508.     struct intel_sdvo *intel_sdvo;
  2509.     int i;
  2510.  
  2511.     intel_sdvo = kzalloc(sizeof(struct intel_sdvo), GFP_KERNEL);
  2512.     if (!intel_sdvo)
  2513.         return false;
  2514.  
  2515.     intel_sdvo->sdvo_reg = sdvo_reg;
  2516.     intel_sdvo->slave_addr = intel_sdvo_get_slave_addr(dev, sdvo_reg) >> 1;
  2517.     intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo, sdvo_reg);
  2518.     if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev)) {
  2519.         kfree(intel_sdvo);
  2520.         return false;
  2521.     }
  2522.  
  2523.     /* encoder type will be decided later */
  2524.     intel_encoder = &intel_sdvo->base;
  2525.     intel_encoder->type = INTEL_OUTPUT_SDVO;
  2526.     drm_encoder_init(dev, &intel_encoder->base, &intel_sdvo_enc_funcs, 0);
  2527.  
  2528.     /* Read the regs to test if we can talk to the device */
  2529.     for (i = 0; i < 0x40; i++) {
  2530.         u8 byte;
  2531.  
  2532.         if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
  2533.             DRM_DEBUG_KMS("No SDVO device found on SDVO%c\n",
  2534.                       IS_SDVOB(sdvo_reg) ? 'B' : 'C');
  2535.             goto err;
  2536.         }
  2537.     }
  2538.  
  2539.     if (IS_SDVOB(sdvo_reg))
  2540.         dev_priv->hotplug_supported_mask |= SDVOB_HOTPLUG_INT_STATUS;
  2541.     else
  2542.         dev_priv->hotplug_supported_mask |= SDVOC_HOTPLUG_INT_STATUS;
  2543.  
  2544.     drm_encoder_helper_add(&intel_encoder->base, &intel_sdvo_helper_funcs);
  2545.  
  2546.     /* In default case sdvo lvds is false */
  2547.     if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
  2548.         goto err;
  2549.  
  2550.     if (intel_sdvo_output_setup(intel_sdvo,
  2551.                     intel_sdvo->caps.output_flags) != true) {
  2552.         DRM_DEBUG_KMS("SDVO output failed to setup on SDVO%c\n",
  2553.                   IS_SDVOB(sdvo_reg) ? 'B' : 'C');
  2554.         goto err;
  2555.     }
  2556.  
  2557.     intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo, sdvo_reg);
  2558.  
  2559.     /* Set the input timing to the screen. Assume always input 0. */
  2560.     if (!intel_sdvo_set_target_input(intel_sdvo))
  2561.         goto err;
  2562.  
  2563.     if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
  2564.                             &intel_sdvo->pixel_clock_min,
  2565.                             &intel_sdvo->pixel_clock_max))
  2566.         goto err;
  2567.  
  2568.     DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
  2569.             "clock range %dMHz - %dMHz, "
  2570.             "input 1: %c, input 2: %c, "
  2571.             "output 1: %c, output 2: %c\n",
  2572.             SDVO_NAME(intel_sdvo),
  2573.             intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
  2574.             intel_sdvo->caps.device_rev_id,
  2575.             intel_sdvo->pixel_clock_min / 1000,
  2576.             intel_sdvo->pixel_clock_max / 1000,
  2577.             (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
  2578.             (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
  2579.             /* check currently supported outputs */
  2580.             intel_sdvo->caps.output_flags &
  2581.             (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
  2582.             intel_sdvo->caps.output_flags &
  2583.             (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
  2584.     return true;
  2585.  
  2586. err:
  2587.     drm_encoder_cleanup(&intel_encoder->base);
  2588. //    i2c_del_adapter(&intel_sdvo->ddc);
  2589.     kfree(intel_sdvo);
  2590.  
  2591.     return false;
  2592. }
  2593.