Subversion Repositories Kolibri OS

Rev

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

  1. /*
  2.  * Copyright © 2008 Intel Corporation
  3.  *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a
  5.  * copy of this software and associated documentation files (the "Software"),
  6.  * to deal in the Software without restriction, including without limitation
  7.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8.  * and/or sell copies of the Software, and to permit persons to whom the
  9.  * Software is furnished to do so, subject to the following conditions:
  10.  *
  11.  * The above copyright notice and this permission notice (including the next
  12.  * paragraph) shall be included in all copies or substantial portions of the
  13.  * Software.
  14.  *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21.  * IN THE SOFTWARE.
  22.  *
  23.  * Authors:
  24.  *    Keith Packard <keithp@keithp.com>
  25.  *
  26.  */
  27.  
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/export.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm_atomic_helper.h>
  33. #include <drm/drm_crtc.h>
  34. #include <drm/drm_crtc_helper.h>
  35. #include <drm/drm_edid.h>
  36. #include "intel_drv.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39.  
  40. #define DP_LINK_CHECK_TIMEOUT   (10 * 1000)
  41.  
  42. /* Compliance test status bits  */
  43. #define INTEL_DP_RESOLUTION_SHIFT_MASK  0
  44. #define INTEL_DP_RESOLUTION_PREFERRED   (1 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  45. #define INTEL_DP_RESOLUTION_STANDARD    (2 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  46. #define INTEL_DP_RESOLUTION_FAILSAFE    (3 << INTEL_DP_RESOLUTION_SHIFT_MASK)
  47.  
  48. struct dp_link_dpll {
  49.         int clock;
  50.         struct dpll dpll;
  51. };
  52.  
  53. static const struct dp_link_dpll gen4_dpll[] = {
  54.         { 162000,
  55.                 { .p1 = 2, .p2 = 10, .n = 2, .m1 = 23, .m2 = 8 } },
  56.         { 270000,
  57.                 { .p1 = 1, .p2 = 10, .n = 1, .m1 = 14, .m2 = 2 } }
  58. };
  59.  
  60. static const struct dp_link_dpll pch_dpll[] = {
  61.         { 162000,
  62.                 { .p1 = 2, .p2 = 10, .n = 1, .m1 = 12, .m2 = 9 } },
  63.         { 270000,
  64.                 { .p1 = 1, .p2 = 10, .n = 2, .m1 = 14, .m2 = 8 } }
  65. };
  66.  
  67. static const struct dp_link_dpll vlv_dpll[] = {
  68.         { 162000,
  69.                 { .p1 = 3, .p2 = 2, .n = 5, .m1 = 3, .m2 = 81 } },
  70.         { 270000,
  71.                 { .p1 = 2, .p2 = 2, .n = 1, .m1 = 2, .m2 = 27 } }
  72. };
  73.  
  74. /*
  75.  * CHV supports eDP 1.4 that have  more link rates.
  76.  * Below only provides the fixed rate but exclude variable rate.
  77.  */
  78. static const struct dp_link_dpll chv_dpll[] = {
  79.         /*
  80.          * CHV requires to program fractional division for m2.
  81.          * m2 is stored in fixed point format using formula below
  82.          * (m2_int << 22) | m2_fraction
  83.          */
  84.         { 162000,       /* m2_int = 32, m2_fraction = 1677722 */
  85.                 { .p1 = 4, .p2 = 2, .n = 1, .m1 = 2, .m2 = 0x819999a } },
  86.         { 270000,       /* m2_int = 27, m2_fraction = 0 */
  87.                 { .p1 = 4, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } },
  88.         { 540000,       /* m2_int = 27, m2_fraction = 0 */
  89.                 { .p1 = 2, .p2 = 1, .n = 1, .m1 = 2, .m2 = 0x6c00000 } }
  90. };
  91.  
  92. static const int bxt_rates[] = { 162000, 216000, 243000, 270000,
  93.                                   324000, 432000, 540000 };
  94. static const int skl_rates[] = { 162000, 216000, 270000,
  95.                                   324000, 432000, 540000 };
  96. static const int default_rates[] = { 162000, 270000, 540000 };
  97.  
  98. /**
  99.  * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
  100.  * @intel_dp: DP struct
  101.  *
  102.  * If a CPU or PCH DP output is attached to an eDP panel, this function
  103.  * will return true, and false otherwise.
  104.  */
  105. static bool is_edp(struct intel_dp *intel_dp)
  106. {
  107.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  108.  
  109.         return intel_dig_port->base.type == INTEL_OUTPUT_EDP;
  110. }
  111.  
  112. static struct drm_device *intel_dp_to_dev(struct intel_dp *intel_dp)
  113. {
  114.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  115.  
  116.         return intel_dig_port->base.base.dev;
  117. }
  118.  
  119. static struct intel_dp *intel_attached_dp(struct drm_connector *connector)
  120. {
  121.         return enc_to_intel_dp(&intel_attached_encoder(connector)->base);
  122. }
  123.  
  124. static void intel_dp_link_down(struct intel_dp *intel_dp);
  125. static bool edp_panel_vdd_on(struct intel_dp *intel_dp);
  126. static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync);
  127. static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp);
  128. static void vlv_steal_power_sequencer(struct drm_device *dev,
  129.                                       enum pipe pipe);
  130.  
  131. static unsigned int intel_dp_unused_lane_mask(int lane_count)
  132. {
  133.         return ~((1 << lane_count) - 1) & 0xf;
  134. }
  135.  
  136. static int
  137. intel_dp_max_link_bw(struct intel_dp  *intel_dp)
  138. {
  139.         int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
  140.  
  141.         switch (max_link_bw) {
  142.         case DP_LINK_BW_1_62:
  143.         case DP_LINK_BW_2_7:
  144.         case DP_LINK_BW_5_4:
  145.                 break;
  146.         default:
  147.                 WARN(1, "invalid max DP link bw val %x, using 1.62Gbps\n",
  148.                      max_link_bw);
  149.                 max_link_bw = DP_LINK_BW_1_62;
  150.                 break;
  151.         }
  152.         return max_link_bw;
  153. }
  154.  
  155. static u8 intel_dp_max_lane_count(struct intel_dp *intel_dp)
  156. {
  157.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  158.         struct drm_device *dev = intel_dig_port->base.base.dev;
  159.         u8 source_max, sink_max;
  160.  
  161.         source_max = 4;
  162.         if (HAS_DDI(dev) && intel_dig_port->port == PORT_A &&
  163.             (intel_dig_port->saved_port_bits & DDI_A_4_LANES) == 0)
  164.                 source_max = 2;
  165.  
  166.         sink_max = drm_dp_max_lane_count(intel_dp->dpcd);
  167.  
  168.         return min(source_max, sink_max);
  169. }
  170.  
  171. /*
  172.  * The units on the numbers in the next two are... bizarre.  Examples will
  173.  * make it clearer; this one parallels an example in the eDP spec.
  174.  *
  175.  * intel_dp_max_data_rate for one lane of 2.7GHz evaluates as:
  176.  *
  177.  *     270000 * 1 * 8 / 10 == 216000
  178.  *
  179.  * The actual data capacity of that configuration is 2.16Gbit/s, so the
  180.  * units are decakilobits.  ->clock in a drm_display_mode is in kilohertz -
  181.  * or equivalently, kilopixels per second - so for 1680x1050R it'd be
  182.  * 119000.  At 18bpp that's 2142000 kilobits per second.
  183.  *
  184.  * Thus the strange-looking division by 10 in intel_dp_link_required, to
  185.  * get the result in decakilobits instead of kilobits.
  186.  */
  187.  
  188. static int
  189. intel_dp_link_required(int pixel_clock, int bpp)
  190. {
  191.         return (pixel_clock * bpp + 9) / 10;
  192. }
  193.  
  194. static int
  195. intel_dp_max_data_rate(int max_link_clock, int max_lanes)
  196. {
  197.         return (max_link_clock * max_lanes * 8) / 10;
  198. }
  199.  
  200. static enum drm_mode_status
  201. intel_dp_mode_valid(struct drm_connector *connector,
  202.                     struct drm_display_mode *mode)
  203. {
  204.         struct intel_dp *intel_dp = intel_attached_dp(connector);
  205.         struct intel_connector *intel_connector = to_intel_connector(connector);
  206.         struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
  207.         int target_clock = mode->clock;
  208.         int max_rate, mode_rate, max_lanes, max_link_clock;
  209.  
  210.         if (is_edp(intel_dp) && fixed_mode) {
  211.                 if (mode->hdisplay > fixed_mode->hdisplay)
  212.                         return MODE_PANEL;
  213.  
  214.                 if (mode->vdisplay > fixed_mode->vdisplay)
  215.                         return MODE_PANEL;
  216.  
  217.                 target_clock = fixed_mode->clock;
  218.         }
  219.  
  220.         max_link_clock = intel_dp_max_link_rate(intel_dp);
  221.         max_lanes = intel_dp_max_lane_count(intel_dp);
  222.  
  223.         max_rate = intel_dp_max_data_rate(max_link_clock, max_lanes);
  224.         mode_rate = intel_dp_link_required(target_clock, 18);
  225.  
  226.         if (mode_rate > max_rate)
  227.                 return MODE_CLOCK_HIGH;
  228.  
  229.         if (mode->clock < 10000)
  230.                 return MODE_CLOCK_LOW;
  231.  
  232.         if (mode->flags & DRM_MODE_FLAG_DBLCLK)
  233.                 return MODE_H_ILLEGAL;
  234.  
  235.         return MODE_OK;
  236. }
  237.  
  238. uint32_t intel_dp_pack_aux(const uint8_t *src, int src_bytes)
  239. {
  240.         int     i;
  241.         uint32_t v = 0;
  242.  
  243.         if (src_bytes > 4)
  244.                 src_bytes = 4;
  245.         for (i = 0; i < src_bytes; i++)
  246.                 v |= ((uint32_t) src[i]) << ((3-i) * 8);
  247.         return v;
  248. }
  249.  
  250. static void intel_dp_unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  251. {
  252.         int i;
  253.         if (dst_bytes > 4)
  254.                 dst_bytes = 4;
  255.         for (i = 0; i < dst_bytes; i++)
  256.                 dst[i] = src >> ((3-i) * 8);
  257. }
  258.  
  259. static void
  260. intel_dp_init_panel_power_sequencer(struct drm_device *dev,
  261.                                     struct intel_dp *intel_dp);
  262. static void
  263. intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
  264.                                               struct intel_dp *intel_dp);
  265.  
  266. static void pps_lock(struct intel_dp *intel_dp)
  267. {
  268.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  269.         struct intel_encoder *encoder = &intel_dig_port->base;
  270.         struct drm_device *dev = encoder->base.dev;
  271.         struct drm_i915_private *dev_priv = dev->dev_private;
  272.         enum intel_display_power_domain power_domain;
  273.  
  274.         /*
  275.          * See vlv_power_sequencer_reset() why we need
  276.          * a power domain reference here.
  277.          */
  278.         power_domain = intel_display_port_aux_power_domain(encoder);
  279.         intel_display_power_get(dev_priv, power_domain);
  280.  
  281.         mutex_lock(&dev_priv->pps_mutex);
  282. }
  283.  
  284. static void pps_unlock(struct intel_dp *intel_dp)
  285. {
  286.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  287.         struct intel_encoder *encoder = &intel_dig_port->base;
  288.         struct drm_device *dev = encoder->base.dev;
  289.         struct drm_i915_private *dev_priv = dev->dev_private;
  290.         enum intel_display_power_domain power_domain;
  291.  
  292.         mutex_unlock(&dev_priv->pps_mutex);
  293.  
  294.         power_domain = intel_display_port_aux_power_domain(encoder);
  295.         intel_display_power_put(dev_priv, power_domain);
  296. }
  297.  
  298. static void
  299. vlv_power_sequencer_kick(struct intel_dp *intel_dp)
  300. {
  301.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  302.         struct drm_device *dev = intel_dig_port->base.base.dev;
  303.         struct drm_i915_private *dev_priv = dev->dev_private;
  304.         enum pipe pipe = intel_dp->pps_pipe;
  305.         bool pll_enabled, release_cl_override = false;
  306.         enum dpio_phy phy = DPIO_PHY(pipe);
  307.         enum dpio_channel ch = vlv_pipe_to_channel(pipe);
  308.         uint32_t DP;
  309.  
  310.         if (WARN(I915_READ(intel_dp->output_reg) & DP_PORT_EN,
  311.                  "skipping pipe %c power seqeuncer kick due to port %c being active\n",
  312.                  pipe_name(pipe), port_name(intel_dig_port->port)))
  313.                 return;
  314.  
  315.         DRM_DEBUG_KMS("kicking pipe %c power sequencer for port %c\n",
  316.                       pipe_name(pipe), port_name(intel_dig_port->port));
  317.  
  318.         /* Preserve the BIOS-computed detected bit. This is
  319.          * supposed to be read-only.
  320.          */
  321.         DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
  322.         DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  323.         DP |= DP_PORT_WIDTH(1);
  324.         DP |= DP_LINK_TRAIN_PAT_1;
  325.  
  326.         if (IS_CHERRYVIEW(dev))
  327.                 DP |= DP_PIPE_SELECT_CHV(pipe);
  328.         else if (pipe == PIPE_B)
  329.                 DP |= DP_PIPEB_SELECT;
  330.  
  331.         pll_enabled = I915_READ(DPLL(pipe)) & DPLL_VCO_ENABLE;
  332.  
  333.         /*
  334.          * The DPLL for the pipe must be enabled for this to work.
  335.          * So enable temporarily it if it's not already enabled.
  336.          */
  337.         if (!pll_enabled) {
  338.                 release_cl_override = IS_CHERRYVIEW(dev) &&
  339.                         !chv_phy_powergate_ch(dev_priv, phy, ch, true);
  340.  
  341.                 vlv_force_pll_on(dev, pipe, IS_CHERRYVIEW(dev) ?
  342.                                  &chv_dpll[0].dpll : &vlv_dpll[0].dpll);
  343.         }
  344.  
  345.         /*
  346.          * Similar magic as in intel_dp_enable_port().
  347.          * We _must_ do this port enable + disable trick
  348.          * to make this power seqeuencer lock onto the port.
  349.          * Otherwise even VDD force bit won't work.
  350.          */
  351.         I915_WRITE(intel_dp->output_reg, DP);
  352.         POSTING_READ(intel_dp->output_reg);
  353.  
  354.         I915_WRITE(intel_dp->output_reg, DP | DP_PORT_EN);
  355.         POSTING_READ(intel_dp->output_reg);
  356.  
  357.         I915_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
  358.         POSTING_READ(intel_dp->output_reg);
  359.  
  360.         if (!pll_enabled) {
  361.                 vlv_force_pll_off(dev, pipe);
  362.  
  363.                 if (release_cl_override)
  364.                         chv_phy_powergate_ch(dev_priv, phy, ch, false);
  365.         }
  366. }
  367.  
  368. static enum pipe
  369. vlv_power_sequencer_pipe(struct intel_dp *intel_dp)
  370. {
  371.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  372.         struct drm_device *dev = intel_dig_port->base.base.dev;
  373.         struct drm_i915_private *dev_priv = dev->dev_private;
  374.         struct intel_encoder *encoder;
  375.         unsigned int pipes = (1 << PIPE_A) | (1 << PIPE_B);
  376.         enum pipe pipe;
  377.  
  378.         lockdep_assert_held(&dev_priv->pps_mutex);
  379.  
  380.         /* We should never land here with regular DP ports */
  381.         WARN_ON(!is_edp(intel_dp));
  382.  
  383.         if (intel_dp->pps_pipe != INVALID_PIPE)
  384.                 return intel_dp->pps_pipe;
  385.  
  386.         /*
  387.          * We don't have power sequencer currently.
  388.          * Pick one that's not used by other ports.
  389.          */
  390.         list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  391.                             base.head) {
  392.                 struct intel_dp *tmp;
  393.  
  394.                 if (encoder->type != INTEL_OUTPUT_EDP)
  395.                         continue;
  396.  
  397.                 tmp = enc_to_intel_dp(&encoder->base);
  398.  
  399.                 if (tmp->pps_pipe != INVALID_PIPE)
  400.                         pipes &= ~(1 << tmp->pps_pipe);
  401.         }
  402.  
  403.         /*
  404.          * Didn't find one. This should not happen since there
  405.          * are two power sequencers and up to two eDP ports.
  406.          */
  407.         if (WARN_ON(pipes == 0))
  408.                 pipe = PIPE_A;
  409.         else
  410.                 pipe = ffs(pipes) - 1;
  411.  
  412.         vlv_steal_power_sequencer(dev, pipe);
  413.         intel_dp->pps_pipe = pipe;
  414.  
  415.         DRM_DEBUG_KMS("picked pipe %c power sequencer for port %c\n",
  416.                       pipe_name(intel_dp->pps_pipe),
  417.                       port_name(intel_dig_port->port));
  418.  
  419.         /* init power sequencer on this pipe and port */
  420.         intel_dp_init_panel_power_sequencer(dev, intel_dp);
  421.         intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  422.  
  423.         /*
  424.          * Even vdd force doesn't work until we've made
  425.          * the power sequencer lock in on the port.
  426.          */
  427.         vlv_power_sequencer_kick(intel_dp);
  428.  
  429.         return intel_dp->pps_pipe;
  430. }
  431.  
  432. typedef bool (*vlv_pipe_check)(struct drm_i915_private *dev_priv,
  433.                                enum pipe pipe);
  434.  
  435. static bool vlv_pipe_has_pp_on(struct drm_i915_private *dev_priv,
  436.                                enum pipe pipe)
  437. {
  438.         return I915_READ(VLV_PIPE_PP_STATUS(pipe)) & PP_ON;
  439. }
  440.  
  441. static bool vlv_pipe_has_vdd_on(struct drm_i915_private *dev_priv,
  442.                                 enum pipe pipe)
  443. {
  444.         return I915_READ(VLV_PIPE_PP_CONTROL(pipe)) & EDP_FORCE_VDD;
  445. }
  446.  
  447. static bool vlv_pipe_any(struct drm_i915_private *dev_priv,
  448.                          enum pipe pipe)
  449. {
  450.         return true;
  451. }
  452.  
  453. static enum pipe
  454. vlv_initial_pps_pipe(struct drm_i915_private *dev_priv,
  455.                      enum port port,
  456.                      vlv_pipe_check pipe_check)
  457. {
  458.         enum pipe pipe;
  459.  
  460.         for (pipe = PIPE_A; pipe <= PIPE_B; pipe++) {
  461.                 u32 port_sel = I915_READ(VLV_PIPE_PP_ON_DELAYS(pipe)) &
  462.                         PANEL_PORT_SELECT_MASK;
  463.  
  464.                 if (port_sel != PANEL_PORT_SELECT_VLV(port))
  465.                         continue;
  466.  
  467.                 if (!pipe_check(dev_priv, pipe))
  468.                         continue;
  469.  
  470.                 return pipe;
  471.         }
  472.  
  473.         return INVALID_PIPE;
  474. }
  475.  
  476. static void
  477. vlv_initial_power_sequencer_setup(struct intel_dp *intel_dp)
  478. {
  479.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  480.         struct drm_device *dev = intel_dig_port->base.base.dev;
  481.         struct drm_i915_private *dev_priv = dev->dev_private;
  482.         enum port port = intel_dig_port->port;
  483.  
  484.         lockdep_assert_held(&dev_priv->pps_mutex);
  485.  
  486.         /* try to find a pipe with this port selected */
  487.         /* first pick one where the panel is on */
  488.         intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  489.                                                   vlv_pipe_has_pp_on);
  490.         /* didn't find one? pick one where vdd is on */
  491.         if (intel_dp->pps_pipe == INVALID_PIPE)
  492.                 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  493.                                                           vlv_pipe_has_vdd_on);
  494.         /* didn't find one? pick one with just the correct port */
  495.         if (intel_dp->pps_pipe == INVALID_PIPE)
  496.                 intel_dp->pps_pipe = vlv_initial_pps_pipe(dev_priv, port,
  497.                                                           vlv_pipe_any);
  498.  
  499.         /* didn't find one? just let vlv_power_sequencer_pipe() pick one when needed */
  500.         if (intel_dp->pps_pipe == INVALID_PIPE) {
  501.                 DRM_DEBUG_KMS("no initial power sequencer for port %c\n",
  502.                               port_name(port));
  503.                 return;
  504.         }
  505.  
  506.         DRM_DEBUG_KMS("initial power sequencer for port %c: pipe %c\n",
  507.                       port_name(port), pipe_name(intel_dp->pps_pipe));
  508.  
  509.         intel_dp_init_panel_power_sequencer(dev, intel_dp);
  510.         intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  511. }
  512.  
  513. void vlv_power_sequencer_reset(struct drm_i915_private *dev_priv)
  514. {
  515.         struct drm_device *dev = dev_priv->dev;
  516.         struct intel_encoder *encoder;
  517.  
  518.         if (WARN_ON(!IS_VALLEYVIEW(dev)))
  519.                 return;
  520.  
  521.         /*
  522.          * We can't grab pps_mutex here due to deadlock with power_domain
  523.          * mutex when power_domain functions are called while holding pps_mutex.
  524.          * That also means that in order to use pps_pipe the code needs to
  525.          * hold both a power domain reference and pps_mutex, and the power domain
  526.          * reference get/put must be done while _not_ holding pps_mutex.
  527.          * pps_{lock,unlock}() do these steps in the correct order, so one
  528.          * should use them always.
  529.          */
  530.  
  531.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  532.                 struct intel_dp *intel_dp;
  533.  
  534.                 if (encoder->type != INTEL_OUTPUT_EDP)
  535.                         continue;
  536.  
  537.                 intel_dp = enc_to_intel_dp(&encoder->base);
  538.                 intel_dp->pps_pipe = INVALID_PIPE;
  539.         }
  540. }
  541.  
  542. static u32 _pp_ctrl_reg(struct intel_dp *intel_dp)
  543. {
  544.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  545.  
  546.         if (IS_BROXTON(dev))
  547.                 return BXT_PP_CONTROL(0);
  548.         else if (HAS_PCH_SPLIT(dev))
  549.                 return PCH_PP_CONTROL;
  550.         else
  551.                 return VLV_PIPE_PP_CONTROL(vlv_power_sequencer_pipe(intel_dp));
  552. }
  553.  
  554. static u32 _pp_stat_reg(struct intel_dp *intel_dp)
  555. {
  556.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  557.  
  558.         if (IS_BROXTON(dev))
  559.                 return BXT_PP_STATUS(0);
  560.         else if (HAS_PCH_SPLIT(dev))
  561.                 return PCH_PP_STATUS;
  562.         else
  563.                 return VLV_PIPE_PP_STATUS(vlv_power_sequencer_pipe(intel_dp));
  564. }
  565.  
  566. #if 0
  567. /* Reboot notifier handler to shutdown panel power to guarantee T12 timing
  568.    This function only applicable when panel PM state is not to be tracked */
  569. static int edp_notify_handler(struct notifier_block *this, unsigned long code,
  570.                               void *unused)
  571. {
  572.         struct intel_dp *intel_dp = container_of(this, typeof(* intel_dp),
  573.                                                  edp_notifier);
  574.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  575.         struct drm_i915_private *dev_priv = dev->dev_private;
  576.  
  577.         if (!is_edp(intel_dp) || code != SYS_RESTART)
  578.                 return 0;
  579.  
  580.         pps_lock(intel_dp);
  581.  
  582.         if (IS_VALLEYVIEW(dev)) {
  583.                 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  584.                 u32 pp_ctrl_reg, pp_div_reg;
  585.                 u32 pp_div;
  586.  
  587.                 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
  588.                 pp_div_reg  = VLV_PIPE_PP_DIVISOR(pipe);
  589.                 pp_div = I915_READ(pp_div_reg);
  590.                 pp_div &= PP_REFERENCE_DIVIDER_MASK;
  591.  
  592.                 /* 0x1F write to PP_DIV_REG sets max cycle delay */
  593.                 I915_WRITE(pp_div_reg, pp_div | 0x1F);
  594.                 I915_WRITE(pp_ctrl_reg, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
  595.                 msleep(intel_dp->panel_power_cycle_delay);
  596.         }
  597.  
  598.         pps_unlock(intel_dp);
  599.  
  600.         return 0;
  601. }
  602. #endif
  603.  
  604. static bool edp_have_panel_power(struct intel_dp *intel_dp)
  605. {
  606.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  607.         struct drm_i915_private *dev_priv = dev->dev_private;
  608.  
  609.         lockdep_assert_held(&dev_priv->pps_mutex);
  610.  
  611.         if (IS_VALLEYVIEW(dev) &&
  612.             intel_dp->pps_pipe == INVALID_PIPE)
  613.                 return false;
  614.  
  615.         return (I915_READ(_pp_stat_reg(intel_dp)) & PP_ON) != 0;
  616. }
  617.  
  618. static bool edp_have_panel_vdd(struct intel_dp *intel_dp)
  619. {
  620.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  621.         struct drm_i915_private *dev_priv = dev->dev_private;
  622.  
  623.         lockdep_assert_held(&dev_priv->pps_mutex);
  624.  
  625.         if (IS_VALLEYVIEW(dev) &&
  626.             intel_dp->pps_pipe == INVALID_PIPE)
  627.                 return false;
  628.  
  629.         return I915_READ(_pp_ctrl_reg(intel_dp)) & EDP_FORCE_VDD;
  630. }
  631.  
  632. static void
  633. intel_dp_check_edp(struct intel_dp *intel_dp)
  634. {
  635.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  636.         struct drm_i915_private *dev_priv = dev->dev_private;
  637.  
  638.         if (!is_edp(intel_dp))
  639.                 return;
  640.  
  641.         if (!edp_have_panel_power(intel_dp) && !edp_have_panel_vdd(intel_dp)) {
  642.                 WARN(1, "eDP powered off while attempting aux channel communication.\n");
  643.                 DRM_DEBUG_KMS("Status 0x%08x Control 0x%08x\n",
  644.                               I915_READ(_pp_stat_reg(intel_dp)),
  645.                               I915_READ(_pp_ctrl_reg(intel_dp)));
  646.         }
  647. }
  648.  
  649. static uint32_t
  650. intel_dp_aux_wait_done(struct intel_dp *intel_dp, bool has_aux_irq)
  651. {
  652.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  653.         struct drm_device *dev = intel_dig_port->base.base.dev;
  654.         struct drm_i915_private *dev_priv = dev->dev_private;
  655.         uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
  656.         uint32_t status;
  657.         bool done;
  658.  
  659. #define C (((status = I915_READ_NOTRACE(ch_ctl)) & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  660.         if (has_aux_irq)
  661.                 done = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
  662.                                           msecs_to_jiffies_timeout(10));
  663.         else
  664.                 done = wait_for_atomic(C, 10) == 0;
  665.         if (!done)
  666.                 DRM_ERROR("dp aux hw did not signal timeout (has irq: %i)!\n",
  667.                           has_aux_irq);
  668. #undef C
  669.  
  670.         return status;
  671. }
  672.  
  673. static uint32_t i9xx_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  674. {
  675.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  676.         struct drm_device *dev = intel_dig_port->base.base.dev;
  677.  
  678.         /*
  679.          * The clock divider is based off the hrawclk, and would like to run at
  680.          * 2MHz.  So, take the hrawclk value and divide by 2 and use that
  681.          */
  682.         return index ? 0 : intel_hrawclk(dev) / 2;
  683. }
  684.  
  685. static uint32_t ilk_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  686. {
  687.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  688.         struct drm_device *dev = intel_dig_port->base.base.dev;
  689.         struct drm_i915_private *dev_priv = dev->dev_private;
  690.  
  691.         if (index)
  692.                 return 0;
  693.  
  694.         if (intel_dig_port->port == PORT_A) {
  695.                 return DIV_ROUND_UP(dev_priv->cdclk_freq, 2000);
  696.  
  697.         } else {
  698.                 return DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
  699.         }
  700. }
  701.  
  702. static uint32_t hsw_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  703. {
  704.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  705.         struct drm_device *dev = intel_dig_port->base.base.dev;
  706.         struct drm_i915_private *dev_priv = dev->dev_private;
  707.  
  708.         if (intel_dig_port->port == PORT_A) {
  709.                 if (index)
  710.                         return 0;
  711.                 return DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 2000);
  712.         } else if (dev_priv->pch_id == INTEL_PCH_LPT_DEVICE_ID_TYPE) {
  713.                 /* Workaround for non-ULT HSW */
  714.                 switch (index) {
  715.                 case 0: return 63;
  716.                 case 1: return 72;
  717.                 default: return 0;
  718.                 }
  719.         } else  {
  720.                 return index ? 0 : DIV_ROUND_UP(intel_pch_rawclk(dev), 2);
  721.         }
  722. }
  723.  
  724. static uint32_t vlv_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  725. {
  726.         return index ? 0 : 100;
  727. }
  728.  
  729. static uint32_t skl_get_aux_clock_divider(struct intel_dp *intel_dp, int index)
  730. {
  731.         /*
  732.          * SKL doesn't need us to program the AUX clock divider (Hardware will
  733.          * derive the clock from CDCLK automatically). We still implement the
  734.          * get_aux_clock_divider vfunc to plug-in into the existing code.
  735.          */
  736.         return index ? 0 : 1;
  737. }
  738.  
  739. static uint32_t i9xx_get_aux_send_ctl(struct intel_dp *intel_dp,
  740.                                       bool has_aux_irq,
  741.                                       int send_bytes,
  742.                                       uint32_t aux_clock_divider)
  743. {
  744.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  745.         struct drm_device *dev = intel_dig_port->base.base.dev;
  746.         uint32_t precharge, timeout;
  747.  
  748.         if (IS_GEN6(dev))
  749.                 precharge = 3;
  750.         else
  751.                 precharge = 5;
  752.  
  753.         if (IS_BROADWELL(dev) && intel_dp->aux_ch_ctl_reg == DPA_AUX_CH_CTL)
  754.                 timeout = DP_AUX_CH_CTL_TIME_OUT_600us;
  755.         else
  756.                 timeout = DP_AUX_CH_CTL_TIME_OUT_400us;
  757.  
  758.         return DP_AUX_CH_CTL_SEND_BUSY |
  759.                DP_AUX_CH_CTL_DONE |
  760.                (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
  761.                DP_AUX_CH_CTL_TIME_OUT_ERROR |
  762.                timeout |
  763.                DP_AUX_CH_CTL_RECEIVE_ERROR |
  764.                (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  765.                (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  766.                (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT);
  767. }
  768.  
  769. static uint32_t skl_get_aux_send_ctl(struct intel_dp *intel_dp,
  770.                                       bool has_aux_irq,
  771.                                       int send_bytes,
  772.                                       uint32_t unused)
  773. {
  774.         return DP_AUX_CH_CTL_SEND_BUSY |
  775.                DP_AUX_CH_CTL_DONE |
  776.                (has_aux_irq ? DP_AUX_CH_CTL_INTERRUPT : 0) |
  777.                DP_AUX_CH_CTL_TIME_OUT_ERROR |
  778.                DP_AUX_CH_CTL_TIME_OUT_1600us |
  779.                DP_AUX_CH_CTL_RECEIVE_ERROR |
  780.                (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  781.                DP_AUX_CH_CTL_SYNC_PULSE_SKL(32);
  782. }
  783.  
  784. static int
  785. intel_dp_aux_ch(struct intel_dp *intel_dp,
  786.                 const uint8_t *send, int send_bytes,
  787.                 uint8_t *recv, int recv_size)
  788. {
  789.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  790.         struct drm_device *dev = intel_dig_port->base.base.dev;
  791.         struct drm_i915_private *dev_priv = dev->dev_private;
  792.         uint32_t ch_ctl = intel_dp->aux_ch_ctl_reg;
  793.         uint32_t ch_data = ch_ctl + 4;
  794.         uint32_t aux_clock_divider;
  795.         int i, ret, recv_bytes;
  796.         uint32_t status;
  797.         int try, clock = 0;
  798.         bool has_aux_irq = HAS_AUX_IRQ(dev);
  799.         bool vdd;
  800.  
  801.         pps_lock(intel_dp);
  802.  
  803.         /*
  804.          * We will be called with VDD already enabled for dpcd/edid/oui reads.
  805.          * In such cases we want to leave VDD enabled and it's up to upper layers
  806.          * to turn it off. But for eg. i2c-dev access we need to turn it on/off
  807.          * ourselves.
  808.          */
  809.         vdd = edp_panel_vdd_on(intel_dp);
  810.  
  811.         /* dp aux is extremely sensitive to irq latency, hence request the
  812.          * lowest possible wakeup latency and so prevent the cpu from going into
  813.          * deep sleep states.
  814.          */
  815.  
  816.         intel_dp_check_edp(intel_dp);
  817.  
  818.         /* Try to wait for any previous AUX channel activity */
  819.         for (try = 0; try < 3; try++) {
  820.                 status = I915_READ_NOTRACE(ch_ctl);
  821.                 if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  822.                         break;
  823.                 msleep(1);
  824.         }
  825.  
  826.         if (try == 3) {
  827.                 static u32 last_status = -1;
  828.                 const u32 status = I915_READ(ch_ctl);
  829.  
  830.                 if (status != last_status) {
  831.                         WARN(1, "dp_aux_ch not started status 0x%08x\n",
  832.                              status);
  833.                         last_status = status;
  834.                 }
  835.  
  836.                 ret = -EBUSY;
  837.                 goto out;
  838.         }
  839.  
  840.         /* Only 5 data registers! */
  841.         if (WARN_ON(send_bytes > 20 || recv_size > 20)) {
  842.                 ret = -E2BIG;
  843.                 goto out;
  844.         }
  845.  
  846.         while ((aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, clock++))) {
  847.                 u32 send_ctl = intel_dp->get_aux_send_ctl(intel_dp,
  848.                                                           has_aux_irq,
  849.                                                           send_bytes,
  850.                                                           aux_clock_divider);
  851.  
  852.                 /* Must try at least 3 times according to DP spec */
  853.                 for (try = 0; try < 5; try++) {
  854.                         /* Load the send data into the aux channel data registers */
  855.                         for (i = 0; i < send_bytes; i += 4)
  856.                                 I915_WRITE(ch_data + i,
  857.                                            intel_dp_pack_aux(send + i,
  858.                                                              send_bytes - i));
  859.  
  860.                         /* Send the command and wait for it to complete */
  861.                         I915_WRITE(ch_ctl, send_ctl);
  862.  
  863.                         status = intel_dp_aux_wait_done(intel_dp, has_aux_irq);
  864.  
  865.                         /* Clear done status and any errors */
  866.                         I915_WRITE(ch_ctl,
  867.                                    status |
  868.                                    DP_AUX_CH_CTL_DONE |
  869.                                    DP_AUX_CH_CTL_TIME_OUT_ERROR |
  870.                                    DP_AUX_CH_CTL_RECEIVE_ERROR);
  871.  
  872.                         if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR)
  873.                                 continue;
  874.  
  875.                         /* DP CTS 1.2 Core Rev 1.1, 4.2.1.1 & 4.2.1.2
  876.                          *   400us delay required for errors and timeouts
  877.                          *   Timeout errors from the HW already meet this
  878.                          *   requirement so skip to next iteration
  879.                          */
  880.                         if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  881.                                 usleep_range(400, 500);
  882.                                 continue;
  883.                         }
  884.                         if (status & DP_AUX_CH_CTL_DONE)
  885.                                 goto done;
  886.                 }
  887.         }
  888.  
  889.         if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  890.                 DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  891.                 ret = -EBUSY;
  892.                 goto out;
  893.         }
  894.  
  895. done:
  896.         /* Check for timeout or receive error.
  897.          * Timeouts occur when the sink is not connected
  898.          */
  899.         if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  900.                 DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  901.                 ret = -EIO;
  902.                 goto out;
  903.         }
  904.  
  905.         /* Timeouts occur when the device isn't connected, so they're
  906.          * "normal" -- don't fill the kernel log with these */
  907.         if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  908.                 DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
  909.                 ret = -ETIMEDOUT;
  910.                 goto out;
  911.         }
  912.  
  913.         /* Unload any bytes sent back from the other side */
  914.         recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  915.                       DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  916.         if (recv_bytes > recv_size)
  917.                 recv_bytes = recv_size;
  918.  
  919.         for (i = 0; i < recv_bytes; i += 4)
  920.                 intel_dp_unpack_aux(I915_READ(ch_data + i),
  921.                                     recv + i, recv_bytes - i);
  922.  
  923.         ret = recv_bytes;
  924. out:
  925.  
  926.         if (vdd)
  927.                 edp_panel_vdd_off(intel_dp, false);
  928.  
  929.         pps_unlock(intel_dp);
  930.  
  931.         return ret;
  932. }
  933.  
  934. #define BARE_ADDRESS_SIZE       3
  935. #define HEADER_SIZE             (BARE_ADDRESS_SIZE + 1)
  936. static ssize_t
  937. intel_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  938. {
  939.         struct intel_dp *intel_dp = container_of(aux, struct intel_dp, aux);
  940.         uint8_t txbuf[20], rxbuf[20];
  941.         size_t txsize, rxsize;
  942.         int ret;
  943.  
  944.         txbuf[0] = (msg->request << 4) |
  945.                 ((msg->address >> 16) & 0xf);
  946.         txbuf[1] = (msg->address >> 8) & 0xff;
  947.         txbuf[2] = msg->address & 0xff;
  948.         txbuf[3] = msg->size - 1;
  949.  
  950.         switch (msg->request & ~DP_AUX_I2C_MOT) {
  951.         case DP_AUX_NATIVE_WRITE:
  952.         case DP_AUX_I2C_WRITE:
  953.         case DP_AUX_I2C_WRITE_STATUS_UPDATE:
  954.                 txsize = msg->size ? HEADER_SIZE + msg->size : BARE_ADDRESS_SIZE;
  955.                 rxsize = 2; /* 0 or 1 data bytes */
  956.  
  957.                 if (WARN_ON(txsize > 20))
  958.                         return -E2BIG;
  959.  
  960.                 memcpy(txbuf + HEADER_SIZE, msg->buffer, msg->size);
  961.  
  962.                 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
  963.                 if (ret > 0) {
  964.                         msg->reply = rxbuf[0] >> 4;
  965.  
  966.                         if (ret > 1) {
  967.                                 /* Number of bytes written in a short write. */
  968.                                 ret = clamp_t(int, rxbuf[1], 0, msg->size);
  969.                         } else {
  970.                                 /* Return payload size. */
  971.                                 ret = msg->size;
  972.                         }
  973.                 }
  974.                 break;
  975.  
  976.         case DP_AUX_NATIVE_READ:
  977.         case DP_AUX_I2C_READ:
  978.                 txsize = msg->size ? HEADER_SIZE : BARE_ADDRESS_SIZE;
  979.                 rxsize = msg->size + 1;
  980.  
  981.                 if (WARN_ON(rxsize > 20))
  982.                         return -E2BIG;
  983.  
  984.                 ret = intel_dp_aux_ch(intel_dp, txbuf, txsize, rxbuf, rxsize);
  985.                 if (ret > 0) {
  986.                         msg->reply = rxbuf[0] >> 4;
  987.                         /*
  988.                          * Assume happy day, and copy the data. The caller is
  989.                          * expected to check msg->reply before touching it.
  990.                          *
  991.                          * Return payload size.
  992.                          */
  993.                         ret--;
  994.                         memcpy(msg->buffer, rxbuf + 1, ret);
  995.                 }
  996.                 break;
  997.  
  998.         default:
  999.                 ret = -EINVAL;
  1000.                 break;
  1001.         }
  1002.  
  1003.         return ret;
  1004. }
  1005.  
  1006. static void
  1007. intel_dp_aux_init(struct intel_dp *intel_dp, struct intel_connector *connector)
  1008. {
  1009.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1010.         struct drm_i915_private *dev_priv = dev->dev_private;
  1011.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1012.         enum port port = intel_dig_port->port;
  1013.         struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
  1014.         const char *name = NULL;
  1015.         uint32_t porte_aux_ctl_reg = DPA_AUX_CH_CTL;
  1016.         int ret;
  1017.  
  1018.         /* On SKL we don't have Aux for port E so we rely on VBT to set
  1019.          * a proper alternate aux channel.
  1020.          */
  1021.         if (IS_SKYLAKE(dev) && port == PORT_E) {
  1022.                 switch (info->alternate_aux_channel) {
  1023.                 case DP_AUX_B:
  1024.                         porte_aux_ctl_reg = DPB_AUX_CH_CTL;
  1025.                         break;
  1026.                 case DP_AUX_C:
  1027.                         porte_aux_ctl_reg = DPC_AUX_CH_CTL;
  1028.                         break;
  1029.                 case DP_AUX_D:
  1030.                         porte_aux_ctl_reg = DPD_AUX_CH_CTL;
  1031.                         break;
  1032.                 case DP_AUX_A:
  1033.                 default:
  1034.                         porte_aux_ctl_reg = DPA_AUX_CH_CTL;
  1035.                 }
  1036.         }
  1037.  
  1038.         switch (port) {
  1039.         case PORT_A:
  1040.                 intel_dp->aux_ch_ctl_reg = DPA_AUX_CH_CTL;
  1041.                 name = "DPDDC-A";
  1042.                 break;
  1043.         case PORT_B:
  1044.                 intel_dp->aux_ch_ctl_reg = PCH_DPB_AUX_CH_CTL;
  1045.                 name = "DPDDC-B";
  1046.                 break;
  1047.         case PORT_C:
  1048.                 intel_dp->aux_ch_ctl_reg = PCH_DPC_AUX_CH_CTL;
  1049.                 name = "DPDDC-C";
  1050.                 break;
  1051.         case PORT_D:
  1052.                 intel_dp->aux_ch_ctl_reg = PCH_DPD_AUX_CH_CTL;
  1053.                 name = "DPDDC-D";
  1054.                 break;
  1055.         case PORT_E:
  1056.                 intel_dp->aux_ch_ctl_reg = porte_aux_ctl_reg;
  1057.                 name = "DPDDC-E";
  1058.                 break;
  1059.         default:
  1060.                 BUG();
  1061.         }
  1062.  
  1063.         /*
  1064.          * The AUX_CTL register is usually DP_CTL + 0x10.
  1065.          *
  1066.          * On Haswell and Broadwell though:
  1067.          *   - Both port A DDI_BUF_CTL and DDI_AUX_CTL are on the CPU
  1068.          *   - Port B/C/D AUX channels are on the PCH, DDI_BUF_CTL on the CPU
  1069.          *
  1070.          * Skylake moves AUX_CTL back next to DDI_BUF_CTL, on the CPU.
  1071.          */
  1072.         if (!IS_HASWELL(dev) && !IS_BROADWELL(dev) && port != PORT_E)
  1073.                 intel_dp->aux_ch_ctl_reg = intel_dp->output_reg + 0x10;
  1074.  
  1075.         intel_dp->aux.name = name;
  1076.         intel_dp->aux.dev = dev->dev;
  1077.         intel_dp->aux.transfer = intel_dp_aux_transfer;
  1078.  
  1079.         DRM_DEBUG_KMS("registering %s bus for %s\n", name,
  1080.                                         "");
  1081.  
  1082.         ret = drm_dp_aux_register(&intel_dp->aux);
  1083.         if (ret < 0) {
  1084.                 DRM_ERROR("drm_dp_aux_register() for %s failed (%d)\n",
  1085.                           name, ret);
  1086.                 return;
  1087.         }
  1088.  
  1089.         ret = sysfs_create_link(&connector->base.kdev->kobj,
  1090.                                 &intel_dp->aux.ddc.dev.kobj,
  1091.                                 intel_dp->aux.ddc.dev.kobj.name);
  1092.         if (ret < 0) {
  1093.                 DRM_ERROR("sysfs_create_link() for %s failed (%d)\n", name, ret);
  1094.                 drm_dp_aux_unregister(&intel_dp->aux);
  1095.         }
  1096. }
  1097.  
  1098. static void
  1099. intel_dp_connector_unregister(struct intel_connector *intel_connector)
  1100. {
  1101.         struct intel_dp *intel_dp = intel_attached_dp(&intel_connector->base);
  1102.  
  1103.         if (!intel_connector->mst_port)
  1104.                 sysfs_remove_link(&intel_connector->base.kdev->kobj,
  1105.                                   intel_dp->aux.ddc.dev.kobj.name);
  1106.         intel_connector_unregister(intel_connector);
  1107. }
  1108.  
  1109. static void
  1110. skl_edp_set_pll_config(struct intel_crtc_state *pipe_config)
  1111. {
  1112.         u32 ctrl1;
  1113.  
  1114.         memset(&pipe_config->dpll_hw_state, 0,
  1115.                sizeof(pipe_config->dpll_hw_state));
  1116.  
  1117.         pipe_config->ddi_pll_sel = SKL_DPLL0;
  1118.         pipe_config->dpll_hw_state.cfgcr1 = 0;
  1119.         pipe_config->dpll_hw_state.cfgcr2 = 0;
  1120.  
  1121.         ctrl1 = DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  1122.         switch (pipe_config->port_clock / 2) {
  1123.         case 81000:
  1124.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  1125.                                               SKL_DPLL0);
  1126.                 break;
  1127.         case 135000:
  1128.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350,
  1129.                                               SKL_DPLL0);
  1130.                 break;
  1131.         case 270000:
  1132.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700,
  1133.                                               SKL_DPLL0);
  1134.                 break;
  1135.         case 162000:
  1136.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620,
  1137.                                               SKL_DPLL0);
  1138.                 break;
  1139.         /* TBD: For DP link rates 2.16 GHz and 4.32 GHz, VCO is 8640 which
  1140.         results in CDCLK change. Need to handle the change of CDCLK by
  1141.         disabling pipes and re-enabling them */
  1142.         case 108000:
  1143.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  1144.                                               SKL_DPLL0);
  1145.                 break;
  1146.         case 216000:
  1147.                 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160,
  1148.                                               SKL_DPLL0);
  1149.                 break;
  1150.  
  1151.         }
  1152.         pipe_config->dpll_hw_state.ctrl1 = ctrl1;
  1153. }
  1154.  
  1155. void
  1156. hsw_dp_set_ddi_pll_sel(struct intel_crtc_state *pipe_config)
  1157. {
  1158.         memset(&pipe_config->dpll_hw_state, 0,
  1159.                sizeof(pipe_config->dpll_hw_state));
  1160.  
  1161.         switch (pipe_config->port_clock / 2) {
  1162.         case 81000:
  1163.                 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_810;
  1164.                 break;
  1165.         case 135000:
  1166.                 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_1350;
  1167.                 break;
  1168.         case 270000:
  1169.                 pipe_config->ddi_pll_sel = PORT_CLK_SEL_LCPLL_2700;
  1170.                 break;
  1171.         }
  1172. }
  1173.  
  1174. static int
  1175. intel_dp_sink_rates(struct intel_dp *intel_dp, const int **sink_rates)
  1176. {
  1177.         if (intel_dp->num_sink_rates) {
  1178.                 *sink_rates = intel_dp->sink_rates;
  1179.                 return intel_dp->num_sink_rates;
  1180.         }
  1181.  
  1182.         *sink_rates = default_rates;
  1183.  
  1184.         return (intel_dp_max_link_bw(intel_dp) >> 3) + 1;
  1185. }
  1186.  
  1187. static bool intel_dp_source_supports_hbr2(struct drm_device *dev)
  1188. {
  1189.         /* WaDisableHBR2:skl */
  1190.         if (IS_SKYLAKE(dev) && INTEL_REVID(dev) <= SKL_REVID_B0)
  1191.                 return false;
  1192.  
  1193.         if ((IS_HASWELL(dev) && !IS_HSW_ULX(dev)) || IS_BROADWELL(dev) ||
  1194.             (INTEL_INFO(dev)->gen >= 9))
  1195.                 return true;
  1196.         else
  1197.                 return false;
  1198. }
  1199.  
  1200. static int
  1201. intel_dp_source_rates(struct drm_device *dev, const int **source_rates)
  1202. {
  1203.         int size;
  1204.  
  1205.         if (IS_BROXTON(dev)) {
  1206.                 *source_rates = bxt_rates;
  1207.                 size = ARRAY_SIZE(bxt_rates);
  1208.         } else if (IS_SKYLAKE(dev)) {
  1209.                 *source_rates = skl_rates;
  1210.                 size = ARRAY_SIZE(skl_rates);
  1211.         } else {
  1212.                 *source_rates = default_rates;
  1213.                 size = ARRAY_SIZE(default_rates);
  1214.         }
  1215.  
  1216.         /* This depends on the fact that 5.4 is last value in the array */
  1217.         if (!intel_dp_source_supports_hbr2(dev))
  1218.                 size--;
  1219.  
  1220.         return size;
  1221. }
  1222.  
  1223. static void
  1224. intel_dp_set_clock(struct intel_encoder *encoder,
  1225.                    struct intel_crtc_state *pipe_config)
  1226. {
  1227.         struct drm_device *dev = encoder->base.dev;
  1228.         const struct dp_link_dpll *divisor = NULL;
  1229.         int i, count = 0;
  1230.  
  1231.         if (IS_G4X(dev)) {
  1232.                 divisor = gen4_dpll;
  1233.                 count = ARRAY_SIZE(gen4_dpll);
  1234.         } else if (HAS_PCH_SPLIT(dev)) {
  1235.                 divisor = pch_dpll;
  1236.                 count = ARRAY_SIZE(pch_dpll);
  1237.         } else if (IS_CHERRYVIEW(dev)) {
  1238.                 divisor = chv_dpll;
  1239.                 count = ARRAY_SIZE(chv_dpll);
  1240.         } else if (IS_VALLEYVIEW(dev)) {
  1241.                 divisor = vlv_dpll;
  1242.                 count = ARRAY_SIZE(vlv_dpll);
  1243.         }
  1244.  
  1245.         if (divisor && count) {
  1246.                 for (i = 0; i < count; i++) {
  1247.                         if (pipe_config->port_clock == divisor[i].clock) {
  1248.                                 pipe_config->dpll = divisor[i].dpll;
  1249.                                 pipe_config->clock_set = true;
  1250.                                 break;
  1251.                         }
  1252.                 }
  1253.         }
  1254. }
  1255.  
  1256. static int intersect_rates(const int *source_rates, int source_len,
  1257.                            const int *sink_rates, int sink_len,
  1258.                            int *common_rates)
  1259. {
  1260.         int i = 0, j = 0, k = 0;
  1261.  
  1262.         while (i < source_len && j < sink_len) {
  1263.                 if (source_rates[i] == sink_rates[j]) {
  1264.                         if (WARN_ON(k >= DP_MAX_SUPPORTED_RATES))
  1265.                                 return k;
  1266.                         common_rates[k] = source_rates[i];
  1267.                         ++k;
  1268.                         ++i;
  1269.                         ++j;
  1270.                 } else if (source_rates[i] < sink_rates[j]) {
  1271.                         ++i;
  1272.                 } else {
  1273.                         ++j;
  1274.                 }
  1275.         }
  1276.         return k;
  1277. }
  1278.  
  1279. static int intel_dp_common_rates(struct intel_dp *intel_dp,
  1280.                                  int *common_rates)
  1281. {
  1282.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1283.         const int *source_rates, *sink_rates;
  1284.         int source_len, sink_len;
  1285.  
  1286.         sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
  1287.         source_len = intel_dp_source_rates(dev, &source_rates);
  1288.  
  1289.         return intersect_rates(source_rates, source_len,
  1290.                                sink_rates, sink_len,
  1291.                                common_rates);
  1292. }
  1293.  
  1294. static void snprintf_int_array(char *str, size_t len,
  1295.                                const int *array, int nelem)
  1296. {
  1297.         int i;
  1298.  
  1299.         str[0] = '\0';
  1300.  
  1301.         for (i = 0; i < nelem; i++) {
  1302.                 int r = snprintf(str, len, "%s%d", i ? ", " : "", array[i]);
  1303.                 if (r >= len)
  1304.                         return;
  1305.                 str += r;
  1306.                 len -= r;
  1307.         }
  1308. }
  1309.  
  1310. static void intel_dp_print_rates(struct intel_dp *intel_dp)
  1311. {
  1312.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1313.         const int *source_rates, *sink_rates;
  1314.         int source_len, sink_len, common_len;
  1315.         int common_rates[DP_MAX_SUPPORTED_RATES];
  1316.         char str[128]; /* FIXME: too big for stack? */
  1317.  
  1318.         if ((drm_debug & DRM_UT_KMS) == 0)
  1319.                 return;
  1320.  
  1321.         source_len = intel_dp_source_rates(dev, &source_rates);
  1322.         snprintf_int_array(str, sizeof(str), source_rates, source_len);
  1323.         DRM_DEBUG_KMS("source rates: %s\n", str);
  1324.  
  1325.         sink_len = intel_dp_sink_rates(intel_dp, &sink_rates);
  1326.         snprintf_int_array(str, sizeof(str), sink_rates, sink_len);
  1327.         DRM_DEBUG_KMS("sink rates: %s\n", str);
  1328.  
  1329.         common_len = intel_dp_common_rates(intel_dp, common_rates);
  1330.         snprintf_int_array(str, sizeof(str), common_rates, common_len);
  1331.         DRM_DEBUG_KMS("common rates: %s\n", str);
  1332. }
  1333.  
  1334. static int rate_to_index(int find, const int *rates)
  1335. {
  1336.         int i = 0;
  1337.  
  1338.         for (i = 0; i < DP_MAX_SUPPORTED_RATES; ++i)
  1339.                 if (find == rates[i])
  1340.                         break;
  1341.  
  1342.         return i;
  1343. }
  1344.  
  1345. int
  1346. intel_dp_max_link_rate(struct intel_dp *intel_dp)
  1347. {
  1348.         int rates[DP_MAX_SUPPORTED_RATES] = {};
  1349.         int len;
  1350.  
  1351.         len = intel_dp_common_rates(intel_dp, rates);
  1352.         if (WARN_ON(len <= 0))
  1353.                 return 162000;
  1354.  
  1355.         return rates[rate_to_index(0, rates) - 1];
  1356. }
  1357.  
  1358. int intel_dp_rate_select(struct intel_dp *intel_dp, int rate)
  1359. {
  1360.         return rate_to_index(rate, intel_dp->sink_rates);
  1361. }
  1362.  
  1363. static void intel_dp_compute_rate(struct intel_dp *intel_dp, int port_clock,
  1364.                                   uint8_t *link_bw, uint8_t *rate_select)
  1365. {
  1366.         if (intel_dp->num_sink_rates) {
  1367.                 *link_bw = 0;
  1368.                 *rate_select =
  1369.                         intel_dp_rate_select(intel_dp, port_clock);
  1370.         } else {
  1371.                 *link_bw = drm_dp_link_rate_to_bw_code(port_clock);
  1372.                 *rate_select = 0;
  1373.         }
  1374. }
  1375.  
  1376. bool
  1377. intel_dp_compute_config(struct intel_encoder *encoder,
  1378.                         struct intel_crtc_state *pipe_config)
  1379. {
  1380.         struct drm_device *dev = encoder->base.dev;
  1381.         struct drm_i915_private *dev_priv = dev->dev_private;
  1382.         struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  1383.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1384.         enum port port = dp_to_dig_port(intel_dp)->port;
  1385.         struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
  1386.         struct intel_connector *intel_connector = intel_dp->attached_connector;
  1387.         int lane_count, clock;
  1388.         int min_lane_count = 1;
  1389.         int max_lane_count = intel_dp_max_lane_count(intel_dp);
  1390.         /* Conveniently, the link BW constants become indices with a shift...*/
  1391.         int min_clock = 0;
  1392.         int max_clock;
  1393.         int bpp, mode_rate;
  1394.         int link_avail, link_clock;
  1395.         int common_rates[DP_MAX_SUPPORTED_RATES] = {};
  1396.         int common_len;
  1397.         uint8_t link_bw, rate_select;
  1398.  
  1399.         common_len = intel_dp_common_rates(intel_dp, common_rates);
  1400.  
  1401.         /* No common link rates between source and sink */
  1402.         WARN_ON(common_len <= 0);
  1403.  
  1404.         max_clock = common_len - 1;
  1405.  
  1406.         if (HAS_PCH_SPLIT(dev) && !HAS_DDI(dev) && port != PORT_A)
  1407.                 pipe_config->has_pch_encoder = true;
  1408.  
  1409.         pipe_config->has_dp_encoder = true;
  1410.         pipe_config->has_drrs = false;
  1411.         pipe_config->has_audio = intel_dp->has_audio && port != PORT_A;
  1412.  
  1413.         if (is_edp(intel_dp) && intel_connector->panel.fixed_mode) {
  1414.                 intel_fixed_panel_mode(intel_connector->panel.fixed_mode,
  1415.                                        adjusted_mode);
  1416.  
  1417.                 if (INTEL_INFO(dev)->gen >= 9) {
  1418.                         int ret;
  1419.                         ret = skl_update_scaler_crtc(pipe_config);
  1420.                         if (ret)
  1421.                                 return ret;
  1422.                 }
  1423.  
  1424.                 if (!HAS_PCH_SPLIT(dev))
  1425.                         intel_gmch_panel_fitting(intel_crtc, pipe_config,
  1426.                                                  intel_connector->panel.fitting_mode);
  1427.                 else
  1428.                         intel_pch_panel_fitting(intel_crtc, pipe_config,
  1429.                                                 intel_connector->panel.fitting_mode);
  1430.         }
  1431.  
  1432.         if (adjusted_mode->flags & DRM_MODE_FLAG_DBLCLK)
  1433.                 return false;
  1434.  
  1435.         DRM_DEBUG_KMS("DP link computation with max lane count %i "
  1436.                       "max bw %d pixel clock %iKHz\n",
  1437.                       max_lane_count, common_rates[max_clock],
  1438.                       adjusted_mode->crtc_clock);
  1439.  
  1440.         /* Walk through all bpp values. Luckily they're all nicely spaced with 2
  1441.          * bpc in between. */
  1442.         bpp = pipe_config->pipe_bpp;
  1443.         if (is_edp(intel_dp)) {
  1444.  
  1445.                 /* Get bpp from vbt only for panels that dont have bpp in edid */
  1446.                 if (intel_connector->base.display_info.bpc == 0 &&
  1447.                         (dev_priv->vbt.edp_bpp && dev_priv->vbt.edp_bpp < bpp)) {
  1448.                         DRM_DEBUG_KMS("clamping bpp for eDP panel to BIOS-provided %i\n",
  1449.                                       dev_priv->vbt.edp_bpp);
  1450.                         bpp = dev_priv->vbt.edp_bpp;
  1451.                 }
  1452.  
  1453.                 /*
  1454.                  * Use the maximum clock and number of lanes the eDP panel
  1455.                  * advertizes being capable of. The panels are generally
  1456.                  * designed to support only a single clock and lane
  1457.                  * configuration, and typically these values correspond to the
  1458.                  * native resolution of the panel.
  1459.                  */
  1460.                 min_lane_count = max_lane_count;
  1461.                 min_clock = max_clock;
  1462.         }
  1463.  
  1464.         for (; bpp >= 6*3; bpp -= 2*3) {
  1465.                 mode_rate = intel_dp_link_required(adjusted_mode->crtc_clock,
  1466.                                                    bpp);
  1467.  
  1468.                 for (clock = min_clock; clock <= max_clock; clock++) {
  1469.                         for (lane_count = min_lane_count;
  1470.                                 lane_count <= max_lane_count;
  1471.                                 lane_count <<= 1) {
  1472.  
  1473.                                 link_clock = common_rates[clock];
  1474.                                 link_avail = intel_dp_max_data_rate(link_clock,
  1475.                                                                     lane_count);
  1476.  
  1477.                                 if (mode_rate <= link_avail) {
  1478.                                         goto found;
  1479.                                 }
  1480.                         }
  1481.                 }
  1482.         }
  1483.  
  1484.         return false;
  1485.  
  1486. found:
  1487.         if (intel_dp->color_range_auto) {
  1488.                 /*
  1489.                  * See:
  1490.                  * CEA-861-E - 5.1 Default Encoding Parameters
  1491.                  * VESA DisplayPort Ver.1.2a - 5.1.1.1 Video Colorimetry
  1492.                  */
  1493.                 pipe_config->limited_color_range =
  1494.                         bpp != 18 && drm_match_cea_mode(adjusted_mode) > 1;
  1495.         } else {
  1496.                 pipe_config->limited_color_range =
  1497.                         intel_dp->limited_color_range;
  1498.         }
  1499.  
  1500.         pipe_config->lane_count = lane_count;
  1501.  
  1502.         pipe_config->pipe_bpp = bpp;
  1503.         pipe_config->port_clock = common_rates[clock];
  1504.  
  1505.         intel_dp_compute_rate(intel_dp, pipe_config->port_clock,
  1506.                               &link_bw, &rate_select);
  1507.  
  1508.         DRM_DEBUG_KMS("DP link bw %02x rate select %02x lane count %d clock %d bpp %d\n",
  1509.                       link_bw, rate_select, pipe_config->lane_count,
  1510.                       pipe_config->port_clock, bpp);
  1511.         DRM_DEBUG_KMS("DP link bw required %i available %i\n",
  1512.                       mode_rate, link_avail);
  1513.  
  1514.         intel_link_compute_m_n(bpp, lane_count,
  1515.                                adjusted_mode->crtc_clock,
  1516.                                pipe_config->port_clock,
  1517.                                &pipe_config->dp_m_n);
  1518.  
  1519.         if (intel_connector->panel.downclock_mode != NULL &&
  1520.                 dev_priv->drrs.type == SEAMLESS_DRRS_SUPPORT) {
  1521.                         pipe_config->has_drrs = true;
  1522.                         intel_link_compute_m_n(bpp, lane_count,
  1523.                                 intel_connector->panel.downclock_mode->clock,
  1524.                                 pipe_config->port_clock,
  1525.                                 &pipe_config->dp_m2_n2);
  1526.         }
  1527.  
  1528.         if (IS_SKYLAKE(dev) && is_edp(intel_dp))
  1529.                 skl_edp_set_pll_config(pipe_config);
  1530.         else if (IS_BROXTON(dev))
  1531.                 /* handled in ddi */;
  1532.         else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  1533.                 hsw_dp_set_ddi_pll_sel(pipe_config);
  1534.         else
  1535.                 intel_dp_set_clock(encoder, pipe_config);
  1536.  
  1537.         return true;
  1538. }
  1539.  
  1540. static void ironlake_set_pll_cpu_edp(struct intel_dp *intel_dp)
  1541. {
  1542.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  1543.         struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
  1544.         struct drm_device *dev = crtc->base.dev;
  1545.         struct drm_i915_private *dev_priv = dev->dev_private;
  1546.         u32 dpa_ctl;
  1547.  
  1548.         DRM_DEBUG_KMS("eDP PLL enable for clock %d\n",
  1549.                       crtc->config->port_clock);
  1550.         dpa_ctl = I915_READ(DP_A);
  1551.         dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1552.  
  1553.         if (crtc->config->port_clock == 162000) {
  1554.                 /* For a long time we've carried around a ILK-DevA w/a for the
  1555.                  * 160MHz clock. If we're really unlucky, it's still required.
  1556.                  */
  1557.                 DRM_DEBUG_KMS("160MHz cpu eDP clock, might need ilk devA w/a\n");
  1558.                 dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1559.                 intel_dp->DP |= DP_PLL_FREQ_160MHZ;
  1560.         } else {
  1561.                 dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1562.                 intel_dp->DP |= DP_PLL_FREQ_270MHZ;
  1563.         }
  1564.  
  1565.         I915_WRITE(DP_A, dpa_ctl);
  1566.  
  1567.         POSTING_READ(DP_A);
  1568.         udelay(500);
  1569. }
  1570.  
  1571. void intel_dp_set_link_params(struct intel_dp *intel_dp,
  1572.                               const struct intel_crtc_state *pipe_config)
  1573. {
  1574.         intel_dp->link_rate = pipe_config->port_clock;
  1575.         intel_dp->lane_count = pipe_config->lane_count;
  1576. }
  1577.  
  1578. static void intel_dp_prepare(struct intel_encoder *encoder)
  1579. {
  1580.         struct drm_device *dev = encoder->base.dev;
  1581.         struct drm_i915_private *dev_priv = dev->dev_private;
  1582.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  1583.         enum port port = dp_to_dig_port(intel_dp)->port;
  1584.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  1585.         const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  1586.  
  1587.         intel_dp_set_link_params(intel_dp, crtc->config);
  1588.  
  1589.         /*
  1590.          * There are four kinds of DP registers:
  1591.          *
  1592.          *      IBX PCH
  1593.          *      SNB CPU
  1594.          *      IVB CPU
  1595.          *      CPT PCH
  1596.          *
  1597.          * IBX PCH and CPU are the same for almost everything,
  1598.          * except that the CPU DP PLL is configured in this
  1599.          * register
  1600.          *
  1601.          * CPT PCH is quite different, having many bits moved
  1602.          * to the TRANS_DP_CTL register instead. That
  1603.          * configuration happens (oddly) in ironlake_pch_enable
  1604.          */
  1605.  
  1606.         /* Preserve the BIOS-computed detected bit. This is
  1607.          * supposed to be read-only.
  1608.          */
  1609.         intel_dp->DP = I915_READ(intel_dp->output_reg) & DP_DETECTED;
  1610.  
  1611.         /* Handle DP bits in common between all three register formats */
  1612.         intel_dp->DP |= DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
  1613.         intel_dp->DP |= DP_PORT_WIDTH(crtc->config->lane_count);
  1614.  
  1615.         if (crtc->config->has_audio)
  1616.                 intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
  1617.  
  1618.         /* Split out the IBX/CPU vs CPT settings */
  1619.  
  1620.         if (IS_GEN7(dev) && port == PORT_A) {
  1621.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  1622.                         intel_dp->DP |= DP_SYNC_HS_HIGH;
  1623.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  1624.                         intel_dp->DP |= DP_SYNC_VS_HIGH;
  1625.                 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  1626.  
  1627.                 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1628.                         intel_dp->DP |= DP_ENHANCED_FRAMING;
  1629.  
  1630.                 intel_dp->DP |= crtc->pipe << 29;
  1631.         } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
  1632.                 u32 trans_dp;
  1633.  
  1634.                 intel_dp->DP |= DP_LINK_TRAIN_OFF_CPT;
  1635.  
  1636.                 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
  1637.                 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1638.                         trans_dp |= TRANS_DP_ENH_FRAMING;
  1639.                 else
  1640.                         trans_dp &= ~TRANS_DP_ENH_FRAMING;
  1641.                 I915_WRITE(TRANS_DP_CTL(crtc->pipe), trans_dp);
  1642.         } else {
  1643.                 if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
  1644.                     crtc->config->limited_color_range)
  1645.                         intel_dp->DP |= DP_COLOR_RANGE_16_235;
  1646.  
  1647.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  1648.                         intel_dp->DP |= DP_SYNC_HS_HIGH;
  1649.                 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  1650.                         intel_dp->DP |= DP_SYNC_VS_HIGH;
  1651.                 intel_dp->DP |= DP_LINK_TRAIN_OFF;
  1652.  
  1653.                 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  1654.                         intel_dp->DP |= DP_ENHANCED_FRAMING;
  1655.  
  1656.                 if (IS_CHERRYVIEW(dev))
  1657.                         intel_dp->DP |= DP_PIPE_SELECT_CHV(crtc->pipe);
  1658.                 else if (crtc->pipe == PIPE_B)
  1659.                         intel_dp->DP |= DP_PIPEB_SELECT;
  1660.         }
  1661. }
  1662.  
  1663. #define IDLE_ON_MASK            (PP_ON | PP_SEQUENCE_MASK | 0                     | PP_SEQUENCE_STATE_MASK)
  1664. #define IDLE_ON_VALUE           (PP_ON | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_ON_IDLE)
  1665.  
  1666. #define IDLE_OFF_MASK           (PP_ON | PP_SEQUENCE_MASK | 0                     | 0)
  1667. #define IDLE_OFF_VALUE          (0     | PP_SEQUENCE_NONE | 0                     | 0)
  1668.  
  1669. #define IDLE_CYCLE_MASK         (PP_ON | PP_SEQUENCE_MASK | PP_CYCLE_DELAY_ACTIVE | PP_SEQUENCE_STATE_MASK)
  1670. #define IDLE_CYCLE_VALUE        (0     | PP_SEQUENCE_NONE | 0                     | PP_SEQUENCE_STATE_OFF_IDLE)
  1671.  
  1672. static void wait_panel_status(struct intel_dp *intel_dp,
  1673.                                        u32 mask,
  1674.                                        u32 value)
  1675. {
  1676.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1677.         struct drm_i915_private *dev_priv = dev->dev_private;
  1678.         u32 pp_stat_reg, pp_ctrl_reg;
  1679.  
  1680.         lockdep_assert_held(&dev_priv->pps_mutex);
  1681.  
  1682.         pp_stat_reg = _pp_stat_reg(intel_dp);
  1683.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1684.  
  1685.         DRM_DEBUG_KMS("mask %08x value %08x status %08x control %08x\n",
  1686.                         mask, value,
  1687.                         I915_READ(pp_stat_reg),
  1688.                         I915_READ(pp_ctrl_reg));
  1689.  
  1690.         if (_wait_for((I915_READ(pp_stat_reg) & mask) == value, 5000, 10)) {
  1691.                 DRM_ERROR("Panel status timeout: status %08x control %08x\n",
  1692.                                 I915_READ(pp_stat_reg),
  1693.                                 I915_READ(pp_ctrl_reg));
  1694.         }
  1695.  
  1696.         DRM_DEBUG_KMS("Wait complete\n");
  1697. }
  1698.  
  1699. static void wait_panel_on(struct intel_dp *intel_dp)
  1700. {
  1701.         DRM_DEBUG_KMS("Wait for panel power on\n");
  1702.         wait_panel_status(intel_dp, IDLE_ON_MASK, IDLE_ON_VALUE);
  1703. }
  1704.  
  1705. static void wait_panel_off(struct intel_dp *intel_dp)
  1706. {
  1707.         DRM_DEBUG_KMS("Wait for panel power off time\n");
  1708.         wait_panel_status(intel_dp, IDLE_OFF_MASK, IDLE_OFF_VALUE);
  1709. }
  1710.  
  1711. static void wait_panel_power_cycle(struct intel_dp *intel_dp)
  1712. {
  1713.         DRM_DEBUG_KMS("Wait for panel power cycle\n");
  1714.  
  1715.         /* When we disable the VDD override bit last we have to do the manual
  1716.          * wait. */
  1717.         wait_remaining_ms_from_jiffies(intel_dp->last_power_cycle,
  1718.                                        intel_dp->panel_power_cycle_delay);
  1719.  
  1720.         wait_panel_status(intel_dp, IDLE_CYCLE_MASK, IDLE_CYCLE_VALUE);
  1721. }
  1722.  
  1723. static void wait_backlight_on(struct intel_dp *intel_dp)
  1724. {
  1725.         wait_remaining_ms_from_jiffies(intel_dp->last_power_on,
  1726.                                        intel_dp->backlight_on_delay);
  1727. }
  1728.  
  1729. static void edp_wait_backlight_off(struct intel_dp *intel_dp)
  1730. {
  1731.         wait_remaining_ms_from_jiffies(intel_dp->last_backlight_off,
  1732.                                        intel_dp->backlight_off_delay);
  1733. }
  1734.  
  1735. /* Read the current pp_control value, unlocking the register if it
  1736.  * is locked
  1737.  */
  1738.  
  1739. static  u32 ironlake_get_pp_control(struct intel_dp *intel_dp)
  1740. {
  1741.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1742.         struct drm_i915_private *dev_priv = dev->dev_private;
  1743.         u32 control;
  1744.  
  1745.         lockdep_assert_held(&dev_priv->pps_mutex);
  1746.  
  1747.         control = I915_READ(_pp_ctrl_reg(intel_dp));
  1748.         if (!IS_BROXTON(dev)) {
  1749.                 control &= ~PANEL_UNLOCK_MASK;
  1750.                 control |= PANEL_UNLOCK_REGS;
  1751.         }
  1752.         return control;
  1753. }
  1754.  
  1755. /*
  1756.  * Must be paired with edp_panel_vdd_off().
  1757.  * Must hold pps_mutex around the whole on/off sequence.
  1758.  * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
  1759.  */
  1760. static bool edp_panel_vdd_on(struct intel_dp *intel_dp)
  1761. {
  1762.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1763.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1764.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1765.         struct drm_i915_private *dev_priv = dev->dev_private;
  1766.         enum intel_display_power_domain power_domain;
  1767.         u32 pp;
  1768.         u32 pp_stat_reg, pp_ctrl_reg;
  1769.         bool need_to_disable = !intel_dp->want_panel_vdd;
  1770.  
  1771.         lockdep_assert_held(&dev_priv->pps_mutex);
  1772.  
  1773.         if (!is_edp(intel_dp))
  1774.                 return false;
  1775.  
  1776.         cancel_delayed_work(&intel_dp->panel_vdd_work);
  1777.         intel_dp->want_panel_vdd = true;
  1778.  
  1779.         if (edp_have_panel_vdd(intel_dp))
  1780.                 return need_to_disable;
  1781.  
  1782.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  1783.         intel_display_power_get(dev_priv, power_domain);
  1784.  
  1785.         DRM_DEBUG_KMS("Turning eDP port %c VDD on\n",
  1786.                       port_name(intel_dig_port->port));
  1787.  
  1788.         if (!edp_have_panel_power(intel_dp))
  1789.                 wait_panel_power_cycle(intel_dp);
  1790.  
  1791.         pp = ironlake_get_pp_control(intel_dp);
  1792.         pp |= EDP_FORCE_VDD;
  1793.  
  1794.         pp_stat_reg = _pp_stat_reg(intel_dp);
  1795.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1796.  
  1797.         I915_WRITE(pp_ctrl_reg, pp);
  1798.         POSTING_READ(pp_ctrl_reg);
  1799.         DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
  1800.                         I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
  1801.         /*
  1802.          * If the panel wasn't on, delay before accessing aux channel
  1803.          */
  1804.         if (!edp_have_panel_power(intel_dp)) {
  1805.                 DRM_DEBUG_KMS("eDP port %c panel power wasn't enabled\n",
  1806.                               port_name(intel_dig_port->port));
  1807.                 msleep(intel_dp->panel_power_up_delay);
  1808.         }
  1809.  
  1810.         return need_to_disable;
  1811. }
  1812.  
  1813. /*
  1814.  * Must be paired with intel_edp_panel_vdd_off() or
  1815.  * intel_edp_panel_off().
  1816.  * Nested calls to these functions are not allowed since
  1817.  * we drop the lock. Caller must use some higher level
  1818.  * locking to prevent nested calls from other threads.
  1819.  */
  1820. void intel_edp_panel_vdd_on(struct intel_dp *intel_dp)
  1821. {
  1822.         bool vdd;
  1823.  
  1824.         if (!is_edp(intel_dp))
  1825.                 return;
  1826.  
  1827.         pps_lock(intel_dp);
  1828.         vdd = edp_panel_vdd_on(intel_dp);
  1829.         pps_unlock(intel_dp);
  1830.  
  1831.         I915_STATE_WARN(!vdd, "eDP port %c VDD already requested on\n",
  1832.              port_name(dp_to_dig_port(intel_dp)->port));
  1833. }
  1834.  
  1835. static void edp_panel_vdd_off_sync(struct intel_dp *intel_dp)
  1836. {
  1837.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1838.         struct drm_i915_private *dev_priv = dev->dev_private;
  1839.         struct intel_digital_port *intel_dig_port =
  1840.                 dp_to_dig_port(intel_dp);
  1841.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1842.         enum intel_display_power_domain power_domain;
  1843.         u32 pp;
  1844.         u32 pp_stat_reg, pp_ctrl_reg;
  1845.  
  1846.         lockdep_assert_held(&dev_priv->pps_mutex);
  1847.  
  1848.         WARN_ON(intel_dp->want_panel_vdd);
  1849.  
  1850.         if (!edp_have_panel_vdd(intel_dp))
  1851.                 return;
  1852.  
  1853.         DRM_DEBUG_KMS("Turning eDP port %c VDD off\n",
  1854.                       port_name(intel_dig_port->port));
  1855.  
  1856.         pp = ironlake_get_pp_control(intel_dp);
  1857.         pp &= ~EDP_FORCE_VDD;
  1858.  
  1859.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1860.         pp_stat_reg = _pp_stat_reg(intel_dp);
  1861.  
  1862.         I915_WRITE(pp_ctrl_reg, pp);
  1863.         POSTING_READ(pp_ctrl_reg);
  1864.  
  1865.         /* Make sure sequencer is idle before allowing subsequent activity */
  1866.         DRM_DEBUG_KMS("PP_STATUS: 0x%08x PP_CONTROL: 0x%08x\n",
  1867.         I915_READ(pp_stat_reg), I915_READ(pp_ctrl_reg));
  1868.  
  1869.         if ((pp & POWER_TARGET_ON) == 0)
  1870.                 intel_dp->last_power_cycle = jiffies;
  1871.  
  1872.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  1873.         intel_display_power_put(dev_priv, power_domain);
  1874. }
  1875.  
  1876. static void edp_panel_vdd_work(struct work_struct *__work)
  1877. {
  1878.         struct intel_dp *intel_dp = container_of(to_delayed_work(__work),
  1879.                                                  struct intel_dp, panel_vdd_work);
  1880.  
  1881.         pps_lock(intel_dp);
  1882.         if (!intel_dp->want_panel_vdd)
  1883.                 edp_panel_vdd_off_sync(intel_dp);
  1884.         pps_unlock(intel_dp);
  1885. }
  1886.  
  1887. static void edp_panel_vdd_schedule_off(struct intel_dp *intel_dp)
  1888. {
  1889.         unsigned long delay;
  1890.  
  1891.         /*
  1892.          * Queue the timer to fire a long time from now (relative to the power
  1893.          * down delay) to keep the panel power up across a sequence of
  1894.          * operations.
  1895.          */
  1896.         delay = msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5);
  1897.         schedule_delayed_work(&intel_dp->panel_vdd_work, delay);
  1898. }
  1899.  
  1900. /*
  1901.  * Must be paired with edp_panel_vdd_on().
  1902.  * Must hold pps_mutex around the whole on/off sequence.
  1903.  * Can be nested with intel_edp_panel_vdd_{on,off}() calls.
  1904.  */
  1905. static void edp_panel_vdd_off(struct intel_dp *intel_dp, bool sync)
  1906. {
  1907.         struct drm_i915_private *dev_priv =
  1908.                 intel_dp_to_dev(intel_dp)->dev_private;
  1909.  
  1910.         lockdep_assert_held(&dev_priv->pps_mutex);
  1911.  
  1912.         if (!is_edp(intel_dp))
  1913.                 return;
  1914.  
  1915.         I915_STATE_WARN(!intel_dp->want_panel_vdd, "eDP port %c VDD not forced on",
  1916.              port_name(dp_to_dig_port(intel_dp)->port));
  1917.  
  1918.         intel_dp->want_panel_vdd = false;
  1919.  
  1920.         if (sync)
  1921.                 edp_panel_vdd_off_sync(intel_dp);
  1922.         else
  1923.                 edp_panel_vdd_schedule_off(intel_dp);
  1924. }
  1925.  
  1926. static void edp_panel_on(struct intel_dp *intel_dp)
  1927. {
  1928.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1929.         struct drm_i915_private *dev_priv = dev->dev_private;
  1930.         u32 pp;
  1931.         u32 pp_ctrl_reg;
  1932.  
  1933.         lockdep_assert_held(&dev_priv->pps_mutex);
  1934.  
  1935.         if (!is_edp(intel_dp))
  1936.                 return;
  1937.  
  1938.         DRM_DEBUG_KMS("Turn eDP port %c panel power on\n",
  1939.                       port_name(dp_to_dig_port(intel_dp)->port));
  1940.  
  1941.         if (WARN(edp_have_panel_power(intel_dp),
  1942.                  "eDP port %c panel power already on\n",
  1943.                  port_name(dp_to_dig_port(intel_dp)->port)))
  1944.                 return;
  1945.  
  1946.         wait_panel_power_cycle(intel_dp);
  1947.  
  1948.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  1949.         pp = ironlake_get_pp_control(intel_dp);
  1950.         if (IS_GEN5(dev)) {
  1951.                 /* ILK workaround: disable reset around power sequence */
  1952.                 pp &= ~PANEL_POWER_RESET;
  1953.                 I915_WRITE(pp_ctrl_reg, pp);
  1954.                 POSTING_READ(pp_ctrl_reg);
  1955.         }
  1956.  
  1957.         pp |= POWER_TARGET_ON;
  1958.         if (!IS_GEN5(dev))
  1959.                 pp |= PANEL_POWER_RESET;
  1960.  
  1961.         I915_WRITE(pp_ctrl_reg, pp);
  1962.         POSTING_READ(pp_ctrl_reg);
  1963.  
  1964.         wait_panel_on(intel_dp);
  1965.         intel_dp->last_power_on = jiffies;
  1966.  
  1967.         if (IS_GEN5(dev)) {
  1968.                 pp |= PANEL_POWER_RESET; /* restore panel reset bit */
  1969.                 I915_WRITE(pp_ctrl_reg, pp);
  1970.                 POSTING_READ(pp_ctrl_reg);
  1971.         }
  1972. }
  1973.  
  1974. void intel_edp_panel_on(struct intel_dp *intel_dp)
  1975. {
  1976.         if (!is_edp(intel_dp))
  1977.                 return;
  1978.  
  1979.         pps_lock(intel_dp);
  1980.         edp_panel_on(intel_dp);
  1981.         pps_unlock(intel_dp);
  1982. }
  1983.  
  1984.  
  1985. static void edp_panel_off(struct intel_dp *intel_dp)
  1986. {
  1987.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  1988.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  1989.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  1990.         struct drm_i915_private *dev_priv = dev->dev_private;
  1991.         enum intel_display_power_domain power_domain;
  1992.         u32 pp;
  1993.         u32 pp_ctrl_reg;
  1994.  
  1995.         lockdep_assert_held(&dev_priv->pps_mutex);
  1996.  
  1997.         if (!is_edp(intel_dp))
  1998.                 return;
  1999.  
  2000.         DRM_DEBUG_KMS("Turn eDP port %c panel power off\n",
  2001.                       port_name(dp_to_dig_port(intel_dp)->port));
  2002.  
  2003.         WARN(!intel_dp->want_panel_vdd, "Need eDP port %c VDD to turn off panel\n",
  2004.              port_name(dp_to_dig_port(intel_dp)->port));
  2005.  
  2006.         pp = ironlake_get_pp_control(intel_dp);
  2007.         /* We need to switch off panel power _and_ force vdd, for otherwise some
  2008.          * panels get very unhappy and cease to work. */
  2009.         pp &= ~(POWER_TARGET_ON | PANEL_POWER_RESET | EDP_FORCE_VDD |
  2010.                 EDP_BLC_ENABLE);
  2011.  
  2012.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  2013.  
  2014.         intel_dp->want_panel_vdd = false;
  2015.  
  2016.         I915_WRITE(pp_ctrl_reg, pp);
  2017.         POSTING_READ(pp_ctrl_reg);
  2018.  
  2019.         intel_dp->last_power_cycle = jiffies;
  2020.         wait_panel_off(intel_dp);
  2021.  
  2022.         /* We got a reference when we enabled the VDD. */
  2023.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  2024.         intel_display_power_put(dev_priv, power_domain);
  2025. }
  2026.  
  2027. void intel_edp_panel_off(struct intel_dp *intel_dp)
  2028. {
  2029.         if (!is_edp(intel_dp))
  2030.                 return;
  2031.  
  2032.         pps_lock(intel_dp);
  2033.         edp_panel_off(intel_dp);
  2034.         pps_unlock(intel_dp);
  2035. }
  2036.  
  2037. /* Enable backlight in the panel power control. */
  2038. static void _intel_edp_backlight_on(struct intel_dp *intel_dp)
  2039. {
  2040.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2041.         struct drm_device *dev = intel_dig_port->base.base.dev;
  2042.         struct drm_i915_private *dev_priv = dev->dev_private;
  2043.         u32 pp;
  2044.         u32 pp_ctrl_reg;
  2045.  
  2046.         /*
  2047.          * If we enable the backlight right away following a panel power
  2048.          * on, we may see slight flicker as the panel syncs with the eDP
  2049.          * link.  So delay a bit to make sure the image is solid before
  2050.          * allowing it to appear.
  2051.          */
  2052.         wait_backlight_on(intel_dp);
  2053.  
  2054.         pps_lock(intel_dp);
  2055.  
  2056.         pp = ironlake_get_pp_control(intel_dp);
  2057.         pp |= EDP_BLC_ENABLE;
  2058.  
  2059.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  2060.  
  2061.         I915_WRITE(pp_ctrl_reg, pp);
  2062.         POSTING_READ(pp_ctrl_reg);
  2063.  
  2064.         pps_unlock(intel_dp);
  2065. }
  2066.  
  2067. /* Enable backlight PWM and backlight PP control. */
  2068. void intel_edp_backlight_on(struct intel_dp *intel_dp)
  2069. {
  2070.         if (!is_edp(intel_dp))
  2071.                 return;
  2072.  
  2073.         DRM_DEBUG_KMS("\n");
  2074.  
  2075.         intel_panel_enable_backlight(intel_dp->attached_connector);
  2076.         _intel_edp_backlight_on(intel_dp);
  2077. }
  2078.  
  2079. /* Disable backlight in the panel power control. */
  2080. static void _intel_edp_backlight_off(struct intel_dp *intel_dp)
  2081. {
  2082.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2083.         struct drm_i915_private *dev_priv = dev->dev_private;
  2084.         u32 pp;
  2085.         u32 pp_ctrl_reg;
  2086.  
  2087.         if (!is_edp(intel_dp))
  2088.                 return;
  2089.  
  2090.         pps_lock(intel_dp);
  2091.  
  2092.         pp = ironlake_get_pp_control(intel_dp);
  2093.         pp &= ~EDP_BLC_ENABLE;
  2094.  
  2095.         pp_ctrl_reg = _pp_ctrl_reg(intel_dp);
  2096.  
  2097.         I915_WRITE(pp_ctrl_reg, pp);
  2098.         POSTING_READ(pp_ctrl_reg);
  2099.  
  2100.         pps_unlock(intel_dp);
  2101.  
  2102.         intel_dp->last_backlight_off = jiffies;
  2103.         edp_wait_backlight_off(intel_dp);
  2104. }
  2105.  
  2106. /* Disable backlight PP control and backlight PWM. */
  2107. void intel_edp_backlight_off(struct intel_dp *intel_dp)
  2108. {
  2109.         if (!is_edp(intel_dp))
  2110.                 return;
  2111.  
  2112.         DRM_DEBUG_KMS("\n");
  2113.  
  2114.         _intel_edp_backlight_off(intel_dp);
  2115.         intel_panel_disable_backlight(intel_dp->attached_connector);
  2116. }
  2117.  
  2118. /*
  2119.  * Hook for controlling the panel power control backlight through the bl_power
  2120.  * sysfs attribute. Take care to handle multiple calls.
  2121.  */
  2122. static void intel_edp_backlight_power(struct intel_connector *connector,
  2123.                                       bool enable)
  2124. {
  2125.         struct intel_dp *intel_dp = intel_attached_dp(&connector->base);
  2126.         bool is_enabled;
  2127.  
  2128.         pps_lock(intel_dp);
  2129.         is_enabled = ironlake_get_pp_control(intel_dp) & EDP_BLC_ENABLE;
  2130.         pps_unlock(intel_dp);
  2131.  
  2132.         if (is_enabled == enable)
  2133.                 return;
  2134.  
  2135.         DRM_DEBUG_KMS("panel power control backlight %s\n",
  2136.                       enable ? "enable" : "disable");
  2137.  
  2138.         if (enable)
  2139.                 _intel_edp_backlight_on(intel_dp);
  2140.         else
  2141.                 _intel_edp_backlight_off(intel_dp);
  2142. }
  2143.  
  2144. static void ironlake_edp_pll_on(struct intel_dp *intel_dp)
  2145. {
  2146.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2147.         struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
  2148.         struct drm_device *dev = crtc->dev;
  2149.         struct drm_i915_private *dev_priv = dev->dev_private;
  2150.         u32 dpa_ctl;
  2151.  
  2152.         assert_pipe_disabled(dev_priv,
  2153.                              to_intel_crtc(crtc)->pipe);
  2154.  
  2155.         DRM_DEBUG_KMS("\n");
  2156.         dpa_ctl = I915_READ(DP_A);
  2157.         WARN(dpa_ctl & DP_PLL_ENABLE, "dp pll on, should be off\n");
  2158.         WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
  2159.  
  2160.         /* We don't adjust intel_dp->DP while tearing down the link, to
  2161.          * facilitate link retraining (e.g. after hotplug). Hence clear all
  2162.          * enable bits here to ensure that we don't enable too much. */
  2163.         intel_dp->DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
  2164.         intel_dp->DP |= DP_PLL_ENABLE;
  2165.         I915_WRITE(DP_A, intel_dp->DP);
  2166.         POSTING_READ(DP_A);
  2167.         udelay(200);
  2168. }
  2169.  
  2170. static void ironlake_edp_pll_off(struct intel_dp *intel_dp)
  2171. {
  2172.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2173.         struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
  2174.         struct drm_device *dev = crtc->dev;
  2175.         struct drm_i915_private *dev_priv = dev->dev_private;
  2176.         u32 dpa_ctl;
  2177.  
  2178.         assert_pipe_disabled(dev_priv,
  2179.                              to_intel_crtc(crtc)->pipe);
  2180.  
  2181.         dpa_ctl = I915_READ(DP_A);
  2182.         WARN((dpa_ctl & DP_PLL_ENABLE) == 0,
  2183.              "dp pll off, should be on\n");
  2184.         WARN(dpa_ctl & DP_PORT_EN, "dp port still on, should be off\n");
  2185.  
  2186.         /* We can't rely on the value tracked for the DP register in
  2187.          * intel_dp->DP because link_down must not change that (otherwise link
  2188.          * re-training will fail. */
  2189.         dpa_ctl &= ~DP_PLL_ENABLE;
  2190.         I915_WRITE(DP_A, dpa_ctl);
  2191.         POSTING_READ(DP_A);
  2192.         udelay(200);
  2193. }
  2194.  
  2195. /* If the sink supports it, try to set the power state appropriately */
  2196. void intel_dp_sink_dpms(struct intel_dp *intel_dp, int mode)
  2197. {
  2198.         int ret, i;
  2199.  
  2200.         /* Should have a valid DPCD by this point */
  2201.         if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
  2202.                 return;
  2203.  
  2204.         if (mode != DRM_MODE_DPMS_ON) {
  2205.                 ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
  2206.                                          DP_SET_POWER_D3);
  2207.         } else {
  2208.                 /*
  2209.                  * When turning on, we need to retry for 1ms to give the sink
  2210.                  * time to wake up.
  2211.                  */
  2212.                 for (i = 0; i < 3; i++) {
  2213.                         ret = drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
  2214.                                                  DP_SET_POWER_D0);
  2215.                         if (ret == 1)
  2216.                                 break;
  2217.                         msleep(1);
  2218.                 }
  2219.         }
  2220.  
  2221.         if (ret != 1)
  2222.                 DRM_DEBUG_KMS("failed to %s sink power state\n",
  2223.                               mode == DRM_MODE_DPMS_ON ? "enable" : "disable");
  2224. }
  2225.  
  2226. static bool intel_dp_get_hw_state(struct intel_encoder *encoder,
  2227.                                   enum pipe *pipe)
  2228. {
  2229.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2230.         enum port port = dp_to_dig_port(intel_dp)->port;
  2231.         struct drm_device *dev = encoder->base.dev;
  2232.         struct drm_i915_private *dev_priv = dev->dev_private;
  2233.         enum intel_display_power_domain power_domain;
  2234.         u32 tmp;
  2235.  
  2236.         power_domain = intel_display_port_power_domain(encoder);
  2237.         if (!intel_display_power_is_enabled(dev_priv, power_domain))
  2238.                 return false;
  2239.  
  2240.         tmp = I915_READ(intel_dp->output_reg);
  2241.  
  2242.         if (!(tmp & DP_PORT_EN))
  2243.                 return false;
  2244.  
  2245.         if (IS_GEN7(dev) && port == PORT_A) {
  2246.                 *pipe = PORT_TO_PIPE_CPT(tmp);
  2247.         } else if (HAS_PCH_CPT(dev) && port != PORT_A) {
  2248.                 enum pipe p;
  2249.  
  2250.                 for_each_pipe(dev_priv, p) {
  2251.                         u32 trans_dp = I915_READ(TRANS_DP_CTL(p));
  2252.                         if (TRANS_DP_PIPE_TO_PORT(trans_dp) == port) {
  2253.                                 *pipe = p;
  2254.                                 return true;
  2255.                         }
  2256.                 }
  2257.  
  2258.                 DRM_DEBUG_KMS("No pipe for dp port 0x%x found\n",
  2259.                               intel_dp->output_reg);
  2260.         } else if (IS_CHERRYVIEW(dev)) {
  2261.                 *pipe = DP_PORT_TO_PIPE_CHV(tmp);
  2262.         } else {
  2263.                 *pipe = PORT_TO_PIPE(tmp);
  2264.         }
  2265.  
  2266.         return true;
  2267. }
  2268.  
  2269. static void intel_dp_get_config(struct intel_encoder *encoder,
  2270.                                 struct intel_crtc_state *pipe_config)
  2271. {
  2272.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2273.         u32 tmp, flags = 0;
  2274.         struct drm_device *dev = encoder->base.dev;
  2275.         struct drm_i915_private *dev_priv = dev->dev_private;
  2276.         enum port port = dp_to_dig_port(intel_dp)->port;
  2277.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2278.         int dotclock;
  2279.  
  2280.         tmp = I915_READ(intel_dp->output_reg);
  2281.  
  2282.         pipe_config->has_audio = tmp & DP_AUDIO_OUTPUT_ENABLE && port != PORT_A;
  2283.  
  2284.         if (HAS_PCH_CPT(dev) && port != PORT_A) {
  2285.                 u32 trans_dp = I915_READ(TRANS_DP_CTL(crtc->pipe));
  2286.  
  2287.                 if (trans_dp & TRANS_DP_HSYNC_ACTIVE_HIGH)
  2288.                         flags |= DRM_MODE_FLAG_PHSYNC;
  2289.                 else
  2290.                         flags |= DRM_MODE_FLAG_NHSYNC;
  2291.  
  2292.                 if (trans_dp & TRANS_DP_VSYNC_ACTIVE_HIGH)
  2293.                         flags |= DRM_MODE_FLAG_PVSYNC;
  2294.                 else
  2295.                         flags |= DRM_MODE_FLAG_NVSYNC;
  2296.         } else {
  2297.                 if (tmp & DP_SYNC_HS_HIGH)
  2298.                         flags |= DRM_MODE_FLAG_PHSYNC;
  2299.                 else
  2300.                         flags |= DRM_MODE_FLAG_NHSYNC;
  2301.  
  2302.                 if (tmp & DP_SYNC_VS_HIGH)
  2303.                         flags |= DRM_MODE_FLAG_PVSYNC;
  2304.                 else
  2305.                         flags |= DRM_MODE_FLAG_NVSYNC;
  2306.         }
  2307.  
  2308.         pipe_config->base.adjusted_mode.flags |= flags;
  2309.  
  2310.         if (!HAS_PCH_SPLIT(dev) && !IS_VALLEYVIEW(dev) &&
  2311.             tmp & DP_COLOR_RANGE_16_235)
  2312.                 pipe_config->limited_color_range = true;
  2313.  
  2314.         pipe_config->has_dp_encoder = true;
  2315.  
  2316.         pipe_config->lane_count =
  2317.                 ((tmp & DP_PORT_WIDTH_MASK) >> DP_PORT_WIDTH_SHIFT) + 1;
  2318.  
  2319.         intel_dp_get_m_n(crtc, pipe_config);
  2320.  
  2321.         if (port == PORT_A) {
  2322.                 if ((I915_READ(DP_A) & DP_PLL_FREQ_MASK) == DP_PLL_FREQ_160MHZ)
  2323.                         pipe_config->port_clock = 162000;
  2324.                 else
  2325.                         pipe_config->port_clock = 270000;
  2326.         }
  2327.  
  2328.         dotclock = intel_dotclock_calculate(pipe_config->port_clock,
  2329.                                             &pipe_config->dp_m_n);
  2330.  
  2331.         if (HAS_PCH_SPLIT(dev_priv->dev) && port != PORT_A)
  2332.                 ironlake_check_encoder_dotclock(pipe_config, dotclock);
  2333.  
  2334.         pipe_config->base.adjusted_mode.crtc_clock = dotclock;
  2335.  
  2336.         if (is_edp(intel_dp) && dev_priv->vbt.edp_bpp &&
  2337.             pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
  2338.                 /*
  2339.                  * This is a big fat ugly hack.
  2340.                  *
  2341.                  * Some machines in UEFI boot mode provide us a VBT that has 18
  2342.                  * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
  2343.                  * unknown we fail to light up. Yet the same BIOS boots up with
  2344.                  * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
  2345.                  * max, not what it tells us to use.
  2346.                  *
  2347.                  * Note: This will still be broken if the eDP panel is not lit
  2348.                  * up by the BIOS, and thus we can't get the mode at module
  2349.                  * load.
  2350.                  */
  2351.                 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
  2352.                               pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
  2353.                 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
  2354.         }
  2355. }
  2356.  
  2357. static void intel_disable_dp(struct intel_encoder *encoder)
  2358. {
  2359.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2360.         struct drm_device *dev = encoder->base.dev;
  2361.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2362.  
  2363.         if (crtc->config->has_audio)
  2364.                 intel_audio_codec_disable(encoder);
  2365.  
  2366.         if (HAS_PSR(dev) && !HAS_DDI(dev))
  2367.                 intel_psr_disable(intel_dp);
  2368.  
  2369.         /* Make sure the panel is off before trying to change the mode. But also
  2370.          * ensure that we have vdd while we switch off the panel. */
  2371.         intel_edp_panel_vdd_on(intel_dp);
  2372.         intel_edp_backlight_off(intel_dp);
  2373.         intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
  2374.         intel_edp_panel_off(intel_dp);
  2375.  
  2376.         /* disable the port before the pipe on g4x */
  2377.         if (INTEL_INFO(dev)->gen < 5)
  2378.                 intel_dp_link_down(intel_dp);
  2379. }
  2380.  
  2381. static void ilk_post_disable_dp(struct intel_encoder *encoder)
  2382. {
  2383.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2384.         enum port port = dp_to_dig_port(intel_dp)->port;
  2385.  
  2386.         intel_dp_link_down(intel_dp);
  2387.         if (port == PORT_A)
  2388.                 ironlake_edp_pll_off(intel_dp);
  2389. }
  2390.  
  2391. static void vlv_post_disable_dp(struct intel_encoder *encoder)
  2392. {
  2393.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2394.  
  2395.         intel_dp_link_down(intel_dp);
  2396. }
  2397.  
  2398. static void chv_data_lane_soft_reset(struct intel_encoder *encoder,
  2399.                                      bool reset)
  2400. {
  2401.         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  2402.         enum dpio_channel ch = vlv_dport_to_channel(enc_to_dig_port(&encoder->base));
  2403.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2404.         enum pipe pipe = crtc->pipe;
  2405.         uint32_t val;
  2406.  
  2407.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW0(ch));
  2408.         if (reset)
  2409.                 val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  2410.         else
  2411.                 val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
  2412.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW0(ch), val);
  2413.  
  2414.         if (crtc->config->lane_count > 2) {
  2415.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW0(ch));
  2416.                 if (reset)
  2417.                         val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
  2418.                 else
  2419.                         val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
  2420.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW0(ch), val);
  2421.         }
  2422.  
  2423.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW1(ch));
  2424.         val |= CHV_PCS_REQ_SOFTRESET_EN;
  2425.         if (reset)
  2426.                 val &= ~DPIO_PCS_CLK_SOFT_RESET;
  2427.         else
  2428.                 val |= DPIO_PCS_CLK_SOFT_RESET;
  2429.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW1(ch), val);
  2430.  
  2431.         if (crtc->config->lane_count > 2) {
  2432.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW1(ch));
  2433.                 val |= CHV_PCS_REQ_SOFTRESET_EN;
  2434.                 if (reset)
  2435.                         val &= ~DPIO_PCS_CLK_SOFT_RESET;
  2436.                 else
  2437.                         val |= DPIO_PCS_CLK_SOFT_RESET;
  2438.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW1(ch), val);
  2439.         }
  2440. }
  2441.  
  2442. static void chv_post_disable_dp(struct intel_encoder *encoder)
  2443. {
  2444.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2445.         struct drm_device *dev = encoder->base.dev;
  2446.         struct drm_i915_private *dev_priv = dev->dev_private;
  2447.  
  2448.         intel_dp_link_down(intel_dp);
  2449.  
  2450.         mutex_lock(&dev_priv->sb_lock);
  2451.  
  2452.         /* Assert data lane reset */
  2453.         chv_data_lane_soft_reset(encoder, true);
  2454.  
  2455.         mutex_unlock(&dev_priv->sb_lock);
  2456. }
  2457.  
  2458. static void
  2459. _intel_dp_set_link_train(struct intel_dp *intel_dp,
  2460.                          uint32_t *DP,
  2461.                          uint8_t dp_train_pat)
  2462. {
  2463.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2464.         struct drm_device *dev = intel_dig_port->base.base.dev;
  2465.         struct drm_i915_private *dev_priv = dev->dev_private;
  2466.         enum port port = intel_dig_port->port;
  2467.  
  2468.         if (HAS_DDI(dev)) {
  2469.                 uint32_t temp = I915_READ(DP_TP_CTL(port));
  2470.  
  2471.                 if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
  2472.                         temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
  2473.                 else
  2474.                         temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
  2475.  
  2476.                 temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
  2477.                 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2478.                 case DP_TRAINING_PATTERN_DISABLE:
  2479.                         temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
  2480.  
  2481.                         break;
  2482.                 case DP_TRAINING_PATTERN_1:
  2483.                         temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
  2484.                         break;
  2485.                 case DP_TRAINING_PATTERN_2:
  2486.                         temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
  2487.                         break;
  2488.                 case DP_TRAINING_PATTERN_3:
  2489.                         temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
  2490.                         break;
  2491.                 }
  2492.                 I915_WRITE(DP_TP_CTL(port), temp);
  2493.  
  2494.         } else if ((IS_GEN7(dev) && port == PORT_A) ||
  2495.                    (HAS_PCH_CPT(dev) && port != PORT_A)) {
  2496.                 *DP &= ~DP_LINK_TRAIN_MASK_CPT;
  2497.  
  2498.                 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2499.                 case DP_TRAINING_PATTERN_DISABLE:
  2500.                         *DP |= DP_LINK_TRAIN_OFF_CPT;
  2501.                         break;
  2502.                 case DP_TRAINING_PATTERN_1:
  2503.                         *DP |= DP_LINK_TRAIN_PAT_1_CPT;
  2504.                         break;
  2505.                 case DP_TRAINING_PATTERN_2:
  2506.                         *DP |= DP_LINK_TRAIN_PAT_2_CPT;
  2507.                         break;
  2508.                 case DP_TRAINING_PATTERN_3:
  2509.                         DRM_ERROR("DP training pattern 3 not supported\n");
  2510.                         *DP |= DP_LINK_TRAIN_PAT_2_CPT;
  2511.                         break;
  2512.                 }
  2513.  
  2514.         } else {
  2515.                 if (IS_CHERRYVIEW(dev))
  2516.                         *DP &= ~DP_LINK_TRAIN_MASK_CHV;
  2517.                 else
  2518.                         *DP &= ~DP_LINK_TRAIN_MASK;
  2519.  
  2520.                 switch (dp_train_pat & DP_TRAINING_PATTERN_MASK) {
  2521.                 case DP_TRAINING_PATTERN_DISABLE:
  2522.                         *DP |= DP_LINK_TRAIN_OFF;
  2523.                         break;
  2524.                 case DP_TRAINING_PATTERN_1:
  2525.                         *DP |= DP_LINK_TRAIN_PAT_1;
  2526.                         break;
  2527.                 case DP_TRAINING_PATTERN_2:
  2528.                         *DP |= DP_LINK_TRAIN_PAT_2;
  2529.                         break;
  2530.                 case DP_TRAINING_PATTERN_3:
  2531.                         if (IS_CHERRYVIEW(dev)) {
  2532.                                 *DP |= DP_LINK_TRAIN_PAT_3_CHV;
  2533.                         } else {
  2534.                                 DRM_ERROR("DP training pattern 3 not supported\n");
  2535.                                 *DP |= DP_LINK_TRAIN_PAT_2;
  2536.                         }
  2537.                         break;
  2538.                 }
  2539.         }
  2540. }
  2541.  
  2542. static void intel_dp_enable_port(struct intel_dp *intel_dp)
  2543. {
  2544.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  2545.         struct drm_i915_private *dev_priv = dev->dev_private;
  2546.  
  2547.         /* enable with pattern 1 (as per spec) */
  2548.         _intel_dp_set_link_train(intel_dp, &intel_dp->DP,
  2549.                                  DP_TRAINING_PATTERN_1);
  2550.  
  2551.         I915_WRITE(intel_dp->output_reg, intel_dp->DP);
  2552.         POSTING_READ(intel_dp->output_reg);
  2553.  
  2554.         /*
  2555.          * Magic for VLV/CHV. We _must_ first set up the register
  2556.          * without actually enabling the port, and then do another
  2557.          * write to enable the port. Otherwise link training will
  2558.          * fail when the power sequencer is freshly used for this port.
  2559.          */
  2560.         intel_dp->DP |= DP_PORT_EN;
  2561.  
  2562.         I915_WRITE(intel_dp->output_reg, intel_dp->DP);
  2563.         POSTING_READ(intel_dp->output_reg);
  2564. }
  2565.  
  2566. static void intel_enable_dp(struct intel_encoder *encoder)
  2567. {
  2568.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2569.         struct drm_device *dev = encoder->base.dev;
  2570.         struct drm_i915_private *dev_priv = dev->dev_private;
  2571.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2572.         uint32_t dp_reg = I915_READ(intel_dp->output_reg);
  2573.  
  2574.         if (WARN_ON(dp_reg & DP_PORT_EN))
  2575.                 return;
  2576.  
  2577.         pps_lock(intel_dp);
  2578.  
  2579.         if (IS_VALLEYVIEW(dev))
  2580.                 vlv_init_panel_power_sequencer(intel_dp);
  2581.  
  2582.         intel_dp_enable_port(intel_dp);
  2583.  
  2584.         edp_panel_vdd_on(intel_dp);
  2585.         edp_panel_on(intel_dp);
  2586.         edp_panel_vdd_off(intel_dp, true);
  2587.  
  2588.         pps_unlock(intel_dp);
  2589.  
  2590.         if (IS_VALLEYVIEW(dev)) {
  2591.                 unsigned int lane_mask = 0x0;
  2592.  
  2593.                 if (IS_CHERRYVIEW(dev))
  2594.                         lane_mask = intel_dp_unused_lane_mask(crtc->config->lane_count);
  2595.  
  2596.                 vlv_wait_port_ready(dev_priv, dp_to_dig_port(intel_dp),
  2597.                                     lane_mask);
  2598.         }
  2599.  
  2600.         intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
  2601.         intel_dp_start_link_train(intel_dp);
  2602.         intel_dp_stop_link_train(intel_dp);
  2603.  
  2604.         if (crtc->config->has_audio) {
  2605.                 DRM_DEBUG_DRIVER("Enabling DP audio on pipe %c\n",
  2606.                                  pipe_name(crtc->pipe));
  2607.                 intel_audio_codec_enable(encoder);
  2608.         }
  2609. }
  2610.  
  2611. static void g4x_enable_dp(struct intel_encoder *encoder)
  2612. {
  2613.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2614.  
  2615.         intel_enable_dp(encoder);
  2616.         intel_edp_backlight_on(intel_dp);
  2617. }
  2618.  
  2619. static void vlv_enable_dp(struct intel_encoder *encoder)
  2620. {
  2621.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2622.  
  2623.         intel_edp_backlight_on(intel_dp);
  2624.         intel_psr_enable(intel_dp);
  2625. }
  2626.  
  2627. static void g4x_pre_enable_dp(struct intel_encoder *encoder)
  2628. {
  2629.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2630.         struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2631.  
  2632.         intel_dp_prepare(encoder);
  2633.  
  2634.         /* Only ilk+ has port A */
  2635.         if (dport->port == PORT_A) {
  2636.                 ironlake_set_pll_cpu_edp(intel_dp);
  2637.                 ironlake_edp_pll_on(intel_dp);
  2638.         }
  2639. }
  2640.  
  2641. static void vlv_detach_power_sequencer(struct intel_dp *intel_dp)
  2642. {
  2643.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2644.         struct drm_i915_private *dev_priv = intel_dig_port->base.base.dev->dev_private;
  2645.         enum pipe pipe = intel_dp->pps_pipe;
  2646.         int pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  2647.  
  2648.         edp_panel_vdd_off_sync(intel_dp);
  2649.  
  2650.         /*
  2651.          * VLV seems to get confused when multiple power seqeuencers
  2652.          * have the same port selected (even if only one has power/vdd
  2653.          * enabled). The failure manifests as vlv_wait_port_ready() failing
  2654.          * CHV on the other hand doesn't seem to mind having the same port
  2655.          * selected in multiple power seqeuencers, but let's clear the
  2656.          * port select always when logically disconnecting a power sequencer
  2657.          * from a port.
  2658.          */
  2659.         DRM_DEBUG_KMS("detaching pipe %c power sequencer from port %c\n",
  2660.                       pipe_name(pipe), port_name(intel_dig_port->port));
  2661.         I915_WRITE(pp_on_reg, 0);
  2662.         POSTING_READ(pp_on_reg);
  2663.  
  2664.         intel_dp->pps_pipe = INVALID_PIPE;
  2665. }
  2666.  
  2667. static void vlv_steal_power_sequencer(struct drm_device *dev,
  2668.                                       enum pipe pipe)
  2669. {
  2670.         struct drm_i915_private *dev_priv = dev->dev_private;
  2671.         struct intel_encoder *encoder;
  2672.  
  2673.         lockdep_assert_held(&dev_priv->pps_mutex);
  2674.  
  2675.         if (WARN_ON(pipe != PIPE_A && pipe != PIPE_B))
  2676.                 return;
  2677.  
  2678.         list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  2679.                             base.head) {
  2680.                 struct intel_dp *intel_dp;
  2681.                 enum port port;
  2682.  
  2683.                 if (encoder->type != INTEL_OUTPUT_EDP)
  2684.                         continue;
  2685.  
  2686.                 intel_dp = enc_to_intel_dp(&encoder->base);
  2687.                 port = dp_to_dig_port(intel_dp)->port;
  2688.  
  2689.                 if (intel_dp->pps_pipe != pipe)
  2690.                         continue;
  2691.  
  2692.                 DRM_DEBUG_KMS("stealing pipe %c power sequencer from port %c\n",
  2693.                               pipe_name(pipe), port_name(port));
  2694.  
  2695.                 WARN(encoder->base.crtc,
  2696.                      "stealing pipe %c power sequencer from active eDP port %c\n",
  2697.                      pipe_name(pipe), port_name(port));
  2698.  
  2699.                 /* make sure vdd is off before we steal it */
  2700.                 vlv_detach_power_sequencer(intel_dp);
  2701.         }
  2702. }
  2703.  
  2704. static void vlv_init_panel_power_sequencer(struct intel_dp *intel_dp)
  2705. {
  2706.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  2707.         struct intel_encoder *encoder = &intel_dig_port->base;
  2708.         struct drm_device *dev = encoder->base.dev;
  2709.         struct drm_i915_private *dev_priv = dev->dev_private;
  2710.         struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
  2711.  
  2712.         lockdep_assert_held(&dev_priv->pps_mutex);
  2713.  
  2714.         if (!is_edp(intel_dp))
  2715.                 return;
  2716.  
  2717.         if (intel_dp->pps_pipe == crtc->pipe)
  2718.                 return;
  2719.  
  2720.         /*
  2721.          * If another power sequencer was being used on this
  2722.          * port previously make sure to turn off vdd there while
  2723.          * we still have control of it.
  2724.          */
  2725.         if (intel_dp->pps_pipe != INVALID_PIPE)
  2726.                 vlv_detach_power_sequencer(intel_dp);
  2727.  
  2728.         /*
  2729.          * We may be stealing the power
  2730.          * sequencer from another port.
  2731.          */
  2732.         vlv_steal_power_sequencer(dev, crtc->pipe);
  2733.  
  2734.         /* now it's all ours */
  2735.         intel_dp->pps_pipe = crtc->pipe;
  2736.  
  2737.         DRM_DEBUG_KMS("initializing pipe %c power sequencer for port %c\n",
  2738.                       pipe_name(intel_dp->pps_pipe), port_name(intel_dig_port->port));
  2739.  
  2740.         /* init power sequencer on this pipe and port */
  2741.         intel_dp_init_panel_power_sequencer(dev, intel_dp);
  2742.         intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  2743. }
  2744.  
  2745. static void vlv_pre_enable_dp(struct intel_encoder *encoder)
  2746. {
  2747.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2748.         struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2749.         struct drm_device *dev = encoder->base.dev;
  2750.         struct drm_i915_private *dev_priv = dev->dev_private;
  2751.         struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
  2752.         enum dpio_channel port = vlv_dport_to_channel(dport);
  2753.         int pipe = intel_crtc->pipe;
  2754.         u32 val;
  2755.  
  2756.         mutex_lock(&dev_priv->sb_lock);
  2757.  
  2758.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(port));
  2759.         val = 0;
  2760.         if (pipe)
  2761.                 val |= (1<<21);
  2762.         else
  2763.                 val &= ~(1<<21);
  2764.         val |= 0x001000c4;
  2765.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW8(port), val);
  2766.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW14(port), 0x00760018);
  2767.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW23(port), 0x00400888);
  2768.  
  2769.         mutex_unlock(&dev_priv->sb_lock);
  2770.  
  2771.         intel_enable_dp(encoder);
  2772. }
  2773.  
  2774. static void vlv_dp_pre_pll_enable(struct intel_encoder *encoder)
  2775. {
  2776.         struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
  2777.         struct drm_device *dev = encoder->base.dev;
  2778.         struct drm_i915_private *dev_priv = dev->dev_private;
  2779.         struct intel_crtc *intel_crtc =
  2780.                 to_intel_crtc(encoder->base.crtc);
  2781.         enum dpio_channel port = vlv_dport_to_channel(dport);
  2782.         int pipe = intel_crtc->pipe;
  2783.  
  2784.         intel_dp_prepare(encoder);
  2785.  
  2786.         /* Program Tx lane resets to default */
  2787.         mutex_lock(&dev_priv->sb_lock);
  2788.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW0(port),
  2789.                          DPIO_PCS_TX_LANE2_RESET |
  2790.                          DPIO_PCS_TX_LANE1_RESET);
  2791.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW1(port),
  2792.                          DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
  2793.                          DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
  2794.                          (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
  2795.                                  DPIO_PCS_CLK_SOFT_RESET);
  2796.  
  2797.         /* Fix up inter-pair skew failure */
  2798.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW12(port), 0x00750f00);
  2799.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW11(port), 0x00001500);
  2800.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW14(port), 0x40400000);
  2801.         mutex_unlock(&dev_priv->sb_lock);
  2802. }
  2803.  
  2804. static void chv_pre_enable_dp(struct intel_encoder *encoder)
  2805. {
  2806.         struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
  2807.         struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  2808.         struct drm_device *dev = encoder->base.dev;
  2809.         struct drm_i915_private *dev_priv = dev->dev_private;
  2810.         struct intel_crtc *intel_crtc =
  2811.                 to_intel_crtc(encoder->base.crtc);
  2812.         enum dpio_channel ch = vlv_dport_to_channel(dport);
  2813.         int pipe = intel_crtc->pipe;
  2814.         int data, i, stagger;
  2815.         u32 val;
  2816.  
  2817.         mutex_lock(&dev_priv->sb_lock);
  2818.  
  2819.         /* allow hardware to manage TX FIFO reset source */
  2820.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
  2821.         val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
  2822.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
  2823.  
  2824.         if (intel_crtc->config->lane_count > 2) {
  2825.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
  2826.                 val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
  2827.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
  2828.         }
  2829.  
  2830.         /* Program Tx lane latency optimal setting*/
  2831.         for (i = 0; i < intel_crtc->config->lane_count; i++) {
  2832.                 /* Set the upar bit */
  2833.                 if (intel_crtc->config->lane_count == 1)
  2834.                         data = 0x0;
  2835.                 else
  2836.                         data = (i == 1) ? 0x0 : 0x1;
  2837.                 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW14(ch, i),
  2838.                                 data << DPIO_UPAR_SHIFT);
  2839.         }
  2840.  
  2841.         /* Data lane stagger programming */
  2842.         if (intel_crtc->config->port_clock > 270000)
  2843.                 stagger = 0x18;
  2844.         else if (intel_crtc->config->port_clock > 135000)
  2845.                 stagger = 0xd;
  2846.         else if (intel_crtc->config->port_clock > 67500)
  2847.                 stagger = 0x7;
  2848.         else if (intel_crtc->config->port_clock > 33750)
  2849.                 stagger = 0x4;
  2850.         else
  2851.                 stagger = 0x2;
  2852.  
  2853.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW11(ch));
  2854.         val |= DPIO_TX2_STAGGER_MASK(0x1f);
  2855.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW11(ch), val);
  2856.  
  2857.         if (intel_crtc->config->lane_count > 2) {
  2858.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW11(ch));
  2859.                 val |= DPIO_TX2_STAGGER_MASK(0x1f);
  2860.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW11(ch), val);
  2861.         }
  2862.  
  2863.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW12(ch),
  2864.                        DPIO_LANESTAGGER_STRAP(stagger) |
  2865.                        DPIO_LANESTAGGER_STRAP_OVRD |
  2866.                        DPIO_TX1_STAGGER_MASK(0x1f) |
  2867.                        DPIO_TX1_STAGGER_MULT(6) |
  2868.                        DPIO_TX2_STAGGER_MULT(0));
  2869.  
  2870.         if (intel_crtc->config->lane_count > 2) {
  2871.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW12(ch),
  2872.                                DPIO_LANESTAGGER_STRAP(stagger) |
  2873.                                DPIO_LANESTAGGER_STRAP_OVRD |
  2874.                                DPIO_TX1_STAGGER_MASK(0x1f) |
  2875.                                DPIO_TX1_STAGGER_MULT(7) |
  2876.                                DPIO_TX2_STAGGER_MULT(5));
  2877.         }
  2878.  
  2879.         /* Deassert data lane reset */
  2880.         chv_data_lane_soft_reset(encoder, false);
  2881.  
  2882.         mutex_unlock(&dev_priv->sb_lock);
  2883.  
  2884.         intel_enable_dp(encoder);
  2885.  
  2886.         /* Second common lane will stay alive on its own now */
  2887.         if (dport->release_cl2_override) {
  2888.                 chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, false);
  2889.                 dport->release_cl2_override = false;
  2890.         }
  2891. }
  2892.  
  2893. static void chv_dp_pre_pll_enable(struct intel_encoder *encoder)
  2894. {
  2895.         struct intel_digital_port *dport = enc_to_dig_port(&encoder->base);
  2896.         struct drm_device *dev = encoder->base.dev;
  2897.         struct drm_i915_private *dev_priv = dev->dev_private;
  2898.         struct intel_crtc *intel_crtc =
  2899.                 to_intel_crtc(encoder->base.crtc);
  2900.         enum dpio_channel ch = vlv_dport_to_channel(dport);
  2901.         enum pipe pipe = intel_crtc->pipe;
  2902.         unsigned int lane_mask =
  2903.                 intel_dp_unused_lane_mask(intel_crtc->config->lane_count);
  2904.         u32 val;
  2905.  
  2906.         intel_dp_prepare(encoder);
  2907.  
  2908.         /*
  2909.          * Must trick the second common lane into life.
  2910.          * Otherwise we can't even access the PLL.
  2911.          */
  2912.         if (ch == DPIO_CH0 && pipe == PIPE_B)
  2913.                 dport->release_cl2_override =
  2914.                         !chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, true);
  2915.  
  2916.         chv_phy_powergate_lanes(encoder, true, lane_mask);
  2917.  
  2918.         mutex_lock(&dev_priv->sb_lock);
  2919.  
  2920.         /* Assert data lane reset */
  2921.         chv_data_lane_soft_reset(encoder, true);
  2922.  
  2923.         /* program left/right clock distribution */
  2924.         if (pipe != PIPE_B) {
  2925.                 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  2926.                 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  2927.                 if (ch == DPIO_CH0)
  2928.                         val |= CHV_BUFLEFTENA1_FORCE;
  2929.                 if (ch == DPIO_CH1)
  2930.                         val |= CHV_BUFRIGHTENA1_FORCE;
  2931.                 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  2932.         } else {
  2933.                 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  2934.                 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  2935.                 if (ch == DPIO_CH0)
  2936.                         val |= CHV_BUFLEFTENA2_FORCE;
  2937.                 if (ch == DPIO_CH1)
  2938.                         val |= CHV_BUFRIGHTENA2_FORCE;
  2939.                 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  2940.         }
  2941.  
  2942.         /* program clock channel usage */
  2943.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW8(ch));
  2944.         val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
  2945.         if (pipe != PIPE_B)
  2946.                 val &= ~CHV_PCS_USEDCLKCHANNEL;
  2947.         else
  2948.                 val |= CHV_PCS_USEDCLKCHANNEL;
  2949.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW8(ch), val);
  2950.  
  2951.         if (intel_crtc->config->lane_count > 2) {
  2952.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW8(ch));
  2953.                 val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
  2954.                 if (pipe != PIPE_B)
  2955.                         val &= ~CHV_PCS_USEDCLKCHANNEL;
  2956.                 else
  2957.                         val |= CHV_PCS_USEDCLKCHANNEL;
  2958.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW8(ch), val);
  2959.         }
  2960.  
  2961.         /*
  2962.          * This a a bit weird since generally CL
  2963.          * matches the pipe, but here we need to
  2964.          * pick the CL based on the port.
  2965.          */
  2966.         val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW19(ch));
  2967.         if (pipe != PIPE_B)
  2968.                 val &= ~CHV_CMN_USEDCLKCHANNEL;
  2969.         else
  2970.                 val |= CHV_CMN_USEDCLKCHANNEL;
  2971.         vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW19(ch), val);
  2972.  
  2973.         mutex_unlock(&dev_priv->sb_lock);
  2974. }
  2975.  
  2976. static void chv_dp_post_pll_disable(struct intel_encoder *encoder)
  2977. {
  2978.         struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
  2979.         enum pipe pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  2980.         u32 val;
  2981.  
  2982.         mutex_lock(&dev_priv->sb_lock);
  2983.  
  2984.         /* disable left/right clock distribution */
  2985.         if (pipe != PIPE_B) {
  2986.                 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  2987.                 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  2988.                 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  2989.         } else {
  2990.                 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  2991.                 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  2992.                 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  2993.         }
  2994.  
  2995.         mutex_unlock(&dev_priv->sb_lock);
  2996.  
  2997.         /*
  2998.          * Leave the power down bit cleared for at least one
  2999.          * lane so that chv_powergate_phy_ch() will power
  3000.          * on something when the channel is otherwise unused.
  3001.          * When the port is off and the override is removed
  3002.          * the lanes power down anyway, so otherwise it doesn't
  3003.          * really matter what the state of power down bits is
  3004.          * after this.
  3005.          */
  3006.         chv_phy_powergate_lanes(encoder, false, 0x0);
  3007. }
  3008.  
  3009. /*
  3010.  * Native read with retry for link status and receiver capability reads for
  3011.  * cases where the sink may still be asleep.
  3012.  *
  3013.  * Sinks are *supposed* to come up within 1ms from an off state, but we're also
  3014.  * supposed to retry 3 times per the spec.
  3015.  */
  3016. static ssize_t
  3017. intel_dp_dpcd_read_wake(struct drm_dp_aux *aux, unsigned int offset,
  3018.                         void *buffer, size_t size)
  3019. {
  3020.         ssize_t ret;
  3021.         int i;
  3022.  
  3023.         /*
  3024.          * Sometime we just get the same incorrect byte repeated
  3025.          * over the entire buffer. Doing just one throw away read
  3026.          * initially seems to "solve" it.
  3027.          */
  3028.         drm_dp_dpcd_read(aux, DP_DPCD_REV, buffer, 1);
  3029.  
  3030.         for (i = 0; i < 3; i++) {
  3031.                 ret = drm_dp_dpcd_read(aux, offset, buffer, size);
  3032.                 if (ret == size)
  3033.                         return ret;
  3034.                 msleep(1);
  3035.         }
  3036.  
  3037.         return ret;
  3038. }
  3039.  
  3040. /*
  3041.  * Fetch AUX CH registers 0x202 - 0x207 which contain
  3042.  * link status information
  3043.  */
  3044. static bool
  3045. intel_dp_get_link_status(struct intel_dp *intel_dp, uint8_t link_status[DP_LINK_STATUS_SIZE])
  3046. {
  3047.         return intel_dp_dpcd_read_wake(&intel_dp->aux,
  3048.                                        DP_LANE0_1_STATUS,
  3049.                                        link_status,
  3050.                                        DP_LINK_STATUS_SIZE) == DP_LINK_STATUS_SIZE;
  3051. }
  3052.  
  3053. /* These are source-specific values. */
  3054. static uint8_t
  3055. intel_dp_voltage_max(struct intel_dp *intel_dp)
  3056. {
  3057.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3058.         struct drm_i915_private *dev_priv = dev->dev_private;
  3059.         enum port port = dp_to_dig_port(intel_dp)->port;
  3060.  
  3061.         if (IS_BROXTON(dev))
  3062.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  3063.         else if (INTEL_INFO(dev)->gen >= 9) {
  3064.                 if (dev_priv->edp_low_vswing && port == PORT_A)
  3065.                         return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  3066.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  3067.         } else if (IS_VALLEYVIEW(dev))
  3068.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  3069.         else if (IS_GEN7(dev) && port == PORT_A)
  3070.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  3071.         else if (HAS_PCH_CPT(dev) && port != PORT_A)
  3072.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  3073.         else
  3074.                 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
  3075. }
  3076.  
  3077. static uint8_t
  3078. intel_dp_pre_emphasis_max(struct intel_dp *intel_dp, uint8_t voltage_swing)
  3079. {
  3080.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3081.         enum port port = dp_to_dig_port(intel_dp)->port;
  3082.  
  3083.         if (INTEL_INFO(dev)->gen >= 9) {
  3084.                 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3085.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3086.                         return DP_TRAIN_PRE_EMPH_LEVEL_3;
  3087.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3088.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3089.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3090.                         return DP_TRAIN_PRE_EMPH_LEVEL_1;
  3091.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3092.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3093.                 default:
  3094.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3095.                 }
  3096.         } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  3097.                 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3098.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3099.                         return DP_TRAIN_PRE_EMPH_LEVEL_3;
  3100.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3101.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3102.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3103.                         return DP_TRAIN_PRE_EMPH_LEVEL_1;
  3104.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3105.                 default:
  3106.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3107.                 }
  3108.         } else if (IS_VALLEYVIEW(dev)) {
  3109.                 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3110.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3111.                         return DP_TRAIN_PRE_EMPH_LEVEL_3;
  3112.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3113.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3114.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3115.                         return DP_TRAIN_PRE_EMPH_LEVEL_1;
  3116.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3117.                 default:
  3118.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3119.                 }
  3120.         } else if (IS_GEN7(dev) && port == PORT_A) {
  3121.                 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3122.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3123.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3124.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3125.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3126.                         return DP_TRAIN_PRE_EMPH_LEVEL_1;
  3127.                 default:
  3128.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3129.                 }
  3130.         } else {
  3131.                 switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3132.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3133.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3134.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3135.                         return DP_TRAIN_PRE_EMPH_LEVEL_2;
  3136.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3137.                         return DP_TRAIN_PRE_EMPH_LEVEL_1;
  3138.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3139.                 default:
  3140.                         return DP_TRAIN_PRE_EMPH_LEVEL_0;
  3141.                 }
  3142.         }
  3143. }
  3144.  
  3145. static uint32_t vlv_signal_levels(struct intel_dp *intel_dp)
  3146. {
  3147.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3148.         struct drm_i915_private *dev_priv = dev->dev_private;
  3149.         struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  3150.         struct intel_crtc *intel_crtc =
  3151.                 to_intel_crtc(dport->base.base.crtc);
  3152.         unsigned long demph_reg_value, preemph_reg_value,
  3153.                 uniqtranscale_reg_value;
  3154.         uint8_t train_set = intel_dp->train_set[0];
  3155.         enum dpio_channel port = vlv_dport_to_channel(dport);
  3156.         int pipe = intel_crtc->pipe;
  3157.  
  3158.         switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  3159.         case DP_TRAIN_PRE_EMPH_LEVEL_0:
  3160.                 preemph_reg_value = 0x0004000;
  3161.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3162.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3163.                         demph_reg_value = 0x2B405555;
  3164.                         uniqtranscale_reg_value = 0x552AB83A;
  3165.                         break;
  3166.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3167.                         demph_reg_value = 0x2B404040;
  3168.                         uniqtranscale_reg_value = 0x5548B83A;
  3169.                         break;
  3170.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3171.                         demph_reg_value = 0x2B245555;
  3172.                         uniqtranscale_reg_value = 0x5560B83A;
  3173.                         break;
  3174.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3175.                         demph_reg_value = 0x2B405555;
  3176.                         uniqtranscale_reg_value = 0x5598DA3A;
  3177.                         break;
  3178.                 default:
  3179.                         return 0;
  3180.                 }
  3181.                 break;
  3182.         case DP_TRAIN_PRE_EMPH_LEVEL_1:
  3183.                 preemph_reg_value = 0x0002000;
  3184.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3185.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3186.                         demph_reg_value = 0x2B404040;
  3187.                         uniqtranscale_reg_value = 0x5552B83A;
  3188.                         break;
  3189.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3190.                         demph_reg_value = 0x2B404848;
  3191.                         uniqtranscale_reg_value = 0x5580B83A;
  3192.                         break;
  3193.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3194.                         demph_reg_value = 0x2B404040;
  3195.                         uniqtranscale_reg_value = 0x55ADDA3A;
  3196.                         break;
  3197.                 default:
  3198.                         return 0;
  3199.                 }
  3200.                 break;
  3201.         case DP_TRAIN_PRE_EMPH_LEVEL_2:
  3202.                 preemph_reg_value = 0x0000000;
  3203.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3204.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3205.                         demph_reg_value = 0x2B305555;
  3206.                         uniqtranscale_reg_value = 0x5570B83A;
  3207.                         break;
  3208.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3209.                         demph_reg_value = 0x2B2B4040;
  3210.                         uniqtranscale_reg_value = 0x55ADDA3A;
  3211.                         break;
  3212.                 default:
  3213.                         return 0;
  3214.                 }
  3215.                 break;
  3216.         case DP_TRAIN_PRE_EMPH_LEVEL_3:
  3217.                 preemph_reg_value = 0x0006000;
  3218.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3219.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3220.                         demph_reg_value = 0x1B405555;
  3221.                         uniqtranscale_reg_value = 0x55ADDA3A;
  3222.                         break;
  3223.                 default:
  3224.                         return 0;
  3225.                 }
  3226.                 break;
  3227.         default:
  3228.                 return 0;
  3229.         }
  3230.  
  3231.         mutex_lock(&dev_priv->sb_lock);
  3232.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x00000000);
  3233.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW4(port), demph_reg_value);
  3234.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW2(port),
  3235.                          uniqtranscale_reg_value);
  3236.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW3(port), 0x0C782040);
  3237.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW11(port), 0x00030000);
  3238.         vlv_dpio_write(dev_priv, pipe, VLV_PCS_DW9(port), preemph_reg_value);
  3239.         vlv_dpio_write(dev_priv, pipe, VLV_TX_DW5(port), 0x80000000);
  3240.         mutex_unlock(&dev_priv->sb_lock);
  3241.  
  3242.         return 0;
  3243. }
  3244.  
  3245. static bool chv_need_uniq_trans_scale(uint8_t train_set)
  3246. {
  3247.         return (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) == DP_TRAIN_PRE_EMPH_LEVEL_0 &&
  3248.                 (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) == DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
  3249. }
  3250.  
  3251. static uint32_t chv_signal_levels(struct intel_dp *intel_dp)
  3252. {
  3253.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  3254.         struct drm_i915_private *dev_priv = dev->dev_private;
  3255.         struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
  3256.         struct intel_crtc *intel_crtc = to_intel_crtc(dport->base.base.crtc);
  3257.         u32 deemph_reg_value, margin_reg_value, val;
  3258.         uint8_t train_set = intel_dp->train_set[0];
  3259.         enum dpio_channel ch = vlv_dport_to_channel(dport);
  3260.         enum pipe pipe = intel_crtc->pipe;
  3261.         int i;
  3262.  
  3263.         switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  3264.         case DP_TRAIN_PRE_EMPH_LEVEL_0:
  3265.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3266.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3267.                         deemph_reg_value = 128;
  3268.                         margin_reg_value = 52;
  3269.                         break;
  3270.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3271.                         deemph_reg_value = 128;
  3272.                         margin_reg_value = 77;
  3273.                         break;
  3274.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3275.                         deemph_reg_value = 128;
  3276.                         margin_reg_value = 102;
  3277.                         break;
  3278.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3279.                         deemph_reg_value = 128;
  3280.                         margin_reg_value = 154;
  3281.                         /* FIXME extra to set for 1200 */
  3282.                         break;
  3283.                 default:
  3284.                         return 0;
  3285.                 }
  3286.                 break;
  3287.         case DP_TRAIN_PRE_EMPH_LEVEL_1:
  3288.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3289.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3290.                         deemph_reg_value = 85;
  3291.                         margin_reg_value = 78;
  3292.                         break;
  3293.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3294.                         deemph_reg_value = 85;
  3295.                         margin_reg_value = 116;
  3296.                         break;
  3297.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3298.                         deemph_reg_value = 85;
  3299.                         margin_reg_value = 154;
  3300.                         break;
  3301.                 default:
  3302.                         return 0;
  3303.                 }
  3304.                 break;
  3305.         case DP_TRAIN_PRE_EMPH_LEVEL_2:
  3306.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3307.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3308.                         deemph_reg_value = 64;
  3309.                         margin_reg_value = 104;
  3310.                         break;
  3311.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3312.                         deemph_reg_value = 64;
  3313.                         margin_reg_value = 154;
  3314.                         break;
  3315.                 default:
  3316.                         return 0;
  3317.                 }
  3318.                 break;
  3319.         case DP_TRAIN_PRE_EMPH_LEVEL_3:
  3320.                 switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3321.                 case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3322.                         deemph_reg_value = 43;
  3323.                         margin_reg_value = 154;
  3324.                         break;
  3325.                 default:
  3326.                         return 0;
  3327.                 }
  3328.                 break;
  3329.         default:
  3330.                 return 0;
  3331.         }
  3332.  
  3333.         mutex_lock(&dev_priv->sb_lock);
  3334.  
  3335.         /* Clear calc init */
  3336.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
  3337.         val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
  3338.         val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
  3339.         val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
  3340.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
  3341.  
  3342.         if (intel_crtc->config->lane_count > 2) {
  3343.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
  3344.                 val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
  3345.                 val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
  3346.                 val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
  3347.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
  3348.         }
  3349.  
  3350.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW9(ch));
  3351.         val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
  3352.         val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
  3353.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW9(ch), val);
  3354.  
  3355.         if (intel_crtc->config->lane_count > 2) {
  3356.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW9(ch));
  3357.                 val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
  3358.                 val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
  3359.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW9(ch), val);
  3360.         }
  3361.  
  3362.         /* Program swing deemph */
  3363.         for (i = 0; i < intel_crtc->config->lane_count; i++) {
  3364.                 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW4(ch, i));
  3365.                 val &= ~DPIO_SWING_DEEMPH9P5_MASK;
  3366.                 val |= deemph_reg_value << DPIO_SWING_DEEMPH9P5_SHIFT;
  3367.                 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW4(ch, i), val);
  3368.         }
  3369.  
  3370.         /* Program swing margin */
  3371.         for (i = 0; i < intel_crtc->config->lane_count; i++) {
  3372.                 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW2(ch, i));
  3373.  
  3374.                 val &= ~DPIO_SWING_MARGIN000_MASK;
  3375.                 val |= margin_reg_value << DPIO_SWING_MARGIN000_SHIFT;
  3376.  
  3377.                 /*
  3378.                  * Supposedly this value shouldn't matter when unique transition
  3379.                  * scale is disabled, but in fact it does matter. Let's just
  3380.                  * always program the same value and hope it's OK.
  3381.                  */
  3382.                 val &= ~(0xff << DPIO_UNIQ_TRANS_SCALE_SHIFT);
  3383.                 val |= 0x9a << DPIO_UNIQ_TRANS_SCALE_SHIFT;
  3384.  
  3385.                 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW2(ch, i), val);
  3386.         }
  3387.  
  3388.         /*
  3389.          * The document said it needs to set bit 27 for ch0 and bit 26
  3390.          * for ch1. Might be a typo in the doc.
  3391.          * For now, for this unique transition scale selection, set bit
  3392.          * 27 for ch0 and ch1.
  3393.          */
  3394.         for (i = 0; i < intel_crtc->config->lane_count; i++) {
  3395.                 val = vlv_dpio_read(dev_priv, pipe, CHV_TX_DW3(ch, i));
  3396.                 if (chv_need_uniq_trans_scale(train_set))
  3397.                         val |= DPIO_TX_UNIQ_TRANS_SCALE_EN;
  3398.                 else
  3399.                         val &= ~DPIO_TX_UNIQ_TRANS_SCALE_EN;
  3400.                 vlv_dpio_write(dev_priv, pipe, CHV_TX_DW3(ch, i), val);
  3401.         }
  3402.  
  3403.         /* Start swing calculation */
  3404.         val = vlv_dpio_read(dev_priv, pipe, VLV_PCS01_DW10(ch));
  3405.         val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
  3406.         vlv_dpio_write(dev_priv, pipe, VLV_PCS01_DW10(ch), val);
  3407.  
  3408.         if (intel_crtc->config->lane_count > 2) {
  3409.                 val = vlv_dpio_read(dev_priv, pipe, VLV_PCS23_DW10(ch));
  3410.                 val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
  3411.                 vlv_dpio_write(dev_priv, pipe, VLV_PCS23_DW10(ch), val);
  3412.         }
  3413.  
  3414.         mutex_unlock(&dev_priv->sb_lock);
  3415.  
  3416.         return 0;
  3417. }
  3418.  
  3419. static void
  3420. intel_get_adjust_train(struct intel_dp *intel_dp,
  3421.                        const uint8_t link_status[DP_LINK_STATUS_SIZE])
  3422. {
  3423.         uint8_t v = 0;
  3424.         uint8_t p = 0;
  3425.         int lane;
  3426.         uint8_t voltage_max;
  3427.         uint8_t preemph_max;
  3428.  
  3429.         for (lane = 0; lane < intel_dp->lane_count; lane++) {
  3430.                 uint8_t this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
  3431.                 uint8_t this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
  3432.  
  3433.                 if (this_v > v)
  3434.                         v = this_v;
  3435.                 if (this_p > p)
  3436.                         p = this_p;
  3437.         }
  3438.  
  3439.         voltage_max = intel_dp_voltage_max(intel_dp);
  3440.         if (v >= voltage_max)
  3441.                 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
  3442.  
  3443.         preemph_max = intel_dp_pre_emphasis_max(intel_dp, v);
  3444.         if (p >= preemph_max)
  3445.                 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  3446.  
  3447.         for (lane = 0; lane < 4; lane++)
  3448.                 intel_dp->train_set[lane] = v | p;
  3449. }
  3450.  
  3451. static uint32_t
  3452. gen4_signal_levels(uint8_t train_set)
  3453. {
  3454.         uint32_t        signal_levels = 0;
  3455.  
  3456.         switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  3457.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
  3458.         default:
  3459.                 signal_levels |= DP_VOLTAGE_0_4;
  3460.                 break;
  3461.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
  3462.                 signal_levels |= DP_VOLTAGE_0_6;
  3463.                 break;
  3464.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
  3465.                 signal_levels |= DP_VOLTAGE_0_8;
  3466.                 break;
  3467.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
  3468.                 signal_levels |= DP_VOLTAGE_1_2;
  3469.                 break;
  3470.         }
  3471.         switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  3472.         case DP_TRAIN_PRE_EMPH_LEVEL_0:
  3473.         default:
  3474.                 signal_levels |= DP_PRE_EMPHASIS_0;
  3475.                 break;
  3476.         case DP_TRAIN_PRE_EMPH_LEVEL_1:
  3477.                 signal_levels |= DP_PRE_EMPHASIS_3_5;
  3478.                 break;
  3479.         case DP_TRAIN_PRE_EMPH_LEVEL_2:
  3480.                 signal_levels |= DP_PRE_EMPHASIS_6;
  3481.                 break;
  3482.         case DP_TRAIN_PRE_EMPH_LEVEL_3:
  3483.                 signal_levels |= DP_PRE_EMPHASIS_9_5;
  3484.                 break;
  3485.         }
  3486.         return signal_levels;
  3487. }
  3488.  
  3489. /* Gen6's DP voltage swing and pre-emphasis control */
  3490. static uint32_t
  3491. gen6_edp_signal_levels(uint8_t train_set)
  3492. {
  3493.         int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  3494.                                          DP_TRAIN_PRE_EMPHASIS_MASK);
  3495.         switch (signal_levels) {
  3496.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3497.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3498.                 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  3499.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3500.                 return EDP_LINK_TRAIN_400MV_3_5DB_SNB_B;
  3501.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  3502.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  3503.                 return EDP_LINK_TRAIN_400_600MV_6DB_SNB_B;
  3504.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3505.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3506.                 return EDP_LINK_TRAIN_600_800MV_3_5DB_SNB_B;
  3507.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3508.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3509.                 return EDP_LINK_TRAIN_800_1200MV_0DB_SNB_B;
  3510.         default:
  3511.                 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  3512.                               "0x%x\n", signal_levels);
  3513.                 return EDP_LINK_TRAIN_400_600MV_0DB_SNB_B;
  3514.         }
  3515. }
  3516.  
  3517. /* Gen7's DP voltage swing and pre-emphasis control */
  3518. static uint32_t
  3519. gen7_edp_signal_levels(uint8_t train_set)
  3520. {
  3521.         int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
  3522.                                          DP_TRAIN_PRE_EMPHASIS_MASK);
  3523.         switch (signal_levels) {
  3524.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3525.                 return EDP_LINK_TRAIN_400MV_0DB_IVB;
  3526.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3527.                 return EDP_LINK_TRAIN_400MV_3_5DB_IVB;
  3528.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2:
  3529.                 return EDP_LINK_TRAIN_400MV_6DB_IVB;
  3530.  
  3531.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3532.                 return EDP_LINK_TRAIN_600MV_0DB_IVB;
  3533.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3534.                 return EDP_LINK_TRAIN_600MV_3_5DB_IVB;
  3535.  
  3536.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0:
  3537.                 return EDP_LINK_TRAIN_800MV_0DB_IVB;
  3538.         case DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1:
  3539.                 return EDP_LINK_TRAIN_800MV_3_5DB_IVB;
  3540.  
  3541.         default:
  3542.                 DRM_DEBUG_KMS("Unsupported voltage swing/pre-emphasis level:"
  3543.                               "0x%x\n", signal_levels);
  3544.                 return EDP_LINK_TRAIN_500MV_0DB_IVB;
  3545.         }
  3546. }
  3547.  
  3548. /* Properly updates "DP" with the correct signal levels. */
  3549. static void
  3550. intel_dp_set_signal_levels(struct intel_dp *intel_dp, uint32_t *DP)
  3551. {
  3552.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3553.         enum port port = intel_dig_port->port;
  3554.         struct drm_device *dev = intel_dig_port->base.base.dev;
  3555.         uint32_t signal_levels, mask = 0;
  3556.         uint8_t train_set = intel_dp->train_set[0];
  3557.  
  3558.         if (HAS_DDI(dev)) {
  3559.                 signal_levels = ddi_signal_levels(intel_dp);
  3560.  
  3561.                 if (IS_BROXTON(dev))
  3562.                         signal_levels = 0;
  3563.                 else
  3564.                         mask = DDI_BUF_EMP_MASK;
  3565.         } else if (IS_CHERRYVIEW(dev)) {
  3566.                 signal_levels = chv_signal_levels(intel_dp);
  3567.         } else if (IS_VALLEYVIEW(dev)) {
  3568.                 signal_levels = vlv_signal_levels(intel_dp);
  3569.         } else if (IS_GEN7(dev) && port == PORT_A) {
  3570.                 signal_levels = gen7_edp_signal_levels(train_set);
  3571.                 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_IVB;
  3572.         } else if (IS_GEN6(dev) && port == PORT_A) {
  3573.                 signal_levels = gen6_edp_signal_levels(train_set);
  3574.                 mask = EDP_LINK_TRAIN_VOL_EMP_MASK_SNB;
  3575.         } else {
  3576.                 signal_levels = gen4_signal_levels(train_set);
  3577.                 mask = DP_VOLTAGE_MASK | DP_PRE_EMPHASIS_MASK;
  3578.         }
  3579.  
  3580.         if (mask)
  3581.                 DRM_DEBUG_KMS("Using signal levels %08x\n", signal_levels);
  3582.  
  3583.         DRM_DEBUG_KMS("Using vswing level %d\n",
  3584.                 train_set & DP_TRAIN_VOLTAGE_SWING_MASK);
  3585.         DRM_DEBUG_KMS("Using pre-emphasis level %d\n",
  3586.                 (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  3587.                         DP_TRAIN_PRE_EMPHASIS_SHIFT);
  3588.  
  3589.         *DP = (*DP & ~mask) | signal_levels;
  3590. }
  3591.  
  3592. static bool
  3593. intel_dp_set_link_train(struct intel_dp *intel_dp,
  3594.                         uint32_t *DP,
  3595.                         uint8_t dp_train_pat)
  3596. {
  3597.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3598.         struct drm_i915_private *dev_priv =
  3599.                 to_i915(intel_dig_port->base.base.dev);
  3600.         uint8_t buf[sizeof(intel_dp->train_set) + 1];
  3601.         int ret, len;
  3602.  
  3603.         _intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
  3604.  
  3605.         I915_WRITE(intel_dp->output_reg, *DP);
  3606.         POSTING_READ(intel_dp->output_reg);
  3607.  
  3608.         buf[0] = dp_train_pat;
  3609.         if ((dp_train_pat & DP_TRAINING_PATTERN_MASK) ==
  3610.             DP_TRAINING_PATTERN_DISABLE) {
  3611.                 /* don't write DP_TRAINING_LANEx_SET on disable */
  3612.                 len = 1;
  3613.         } else {
  3614.                 /* DP_TRAINING_LANEx_SET follow DP_TRAINING_PATTERN_SET */
  3615.                 memcpy(buf + 1, intel_dp->train_set, intel_dp->lane_count);
  3616.                 len = intel_dp->lane_count + 1;
  3617.         }
  3618.  
  3619.         ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_PATTERN_SET,
  3620.                                 buf, len);
  3621.  
  3622.         return ret == len;
  3623. }
  3624.  
  3625. static bool
  3626. intel_dp_reset_link_train(struct intel_dp *intel_dp, uint32_t *DP,
  3627.                         uint8_t dp_train_pat)
  3628. {
  3629.                 memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
  3630.         intel_dp_set_signal_levels(intel_dp, DP);
  3631.         return intel_dp_set_link_train(intel_dp, DP, dp_train_pat);
  3632. }
  3633.  
  3634. static bool
  3635. intel_dp_update_link_train(struct intel_dp *intel_dp, uint32_t *DP,
  3636.                            const uint8_t link_status[DP_LINK_STATUS_SIZE])
  3637. {
  3638.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3639.         struct drm_i915_private *dev_priv =
  3640.                 to_i915(intel_dig_port->base.base.dev);
  3641.         int ret;
  3642.  
  3643.         intel_get_adjust_train(intel_dp, link_status);
  3644.         intel_dp_set_signal_levels(intel_dp, DP);
  3645.  
  3646.         I915_WRITE(intel_dp->output_reg, *DP);
  3647.         POSTING_READ(intel_dp->output_reg);
  3648.  
  3649.         ret = drm_dp_dpcd_write(&intel_dp->aux, DP_TRAINING_LANE0_SET,
  3650.                                 intel_dp->train_set, intel_dp->lane_count);
  3651.  
  3652.         return ret == intel_dp->lane_count;
  3653. }
  3654.  
  3655. static void intel_dp_set_idle_link_train(struct intel_dp *intel_dp)
  3656. {
  3657.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3658.         struct drm_device *dev = intel_dig_port->base.base.dev;
  3659.         struct drm_i915_private *dev_priv = dev->dev_private;
  3660.         enum port port = intel_dig_port->port;
  3661.         uint32_t val;
  3662.  
  3663.         if (!HAS_DDI(dev))
  3664.                 return;
  3665.  
  3666.         val = I915_READ(DP_TP_CTL(port));
  3667.         val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
  3668.         val |= DP_TP_CTL_LINK_TRAIN_IDLE;
  3669.         I915_WRITE(DP_TP_CTL(port), val);
  3670.  
  3671.         /*
  3672.          * On PORT_A we can have only eDP in SST mode. There the only reason
  3673.          * we need to set idle transmission mode is to work around a HW issue
  3674.          * where we enable the pipe while not in idle link-training mode.
  3675.          * In this case there is requirement to wait for a minimum number of
  3676.          * idle patterns to be sent.
  3677.          */
  3678.         if (port == PORT_A)
  3679.                 return;
  3680.  
  3681.         if (wait_for((I915_READ(DP_TP_STATUS(port)) & DP_TP_STATUS_IDLE_DONE),
  3682.                      1))
  3683.                 DRM_ERROR("Timed out waiting for DP idle patterns\n");
  3684. }
  3685.  
  3686. /* Enable corresponding port and start training pattern 1 */
  3687. static void
  3688. intel_dp_link_training_clock_recovery(struct intel_dp *intel_dp)
  3689. {
  3690.         struct drm_encoder *encoder = &dp_to_dig_port(intel_dp)->base.base;
  3691.         struct drm_device *dev = encoder->dev;
  3692.         int i;
  3693.         uint8_t voltage;
  3694.         int voltage_tries, loop_tries;
  3695.         uint32_t DP = intel_dp->DP;
  3696.         uint8_t link_config[2];
  3697.         uint8_t link_bw, rate_select;
  3698.  
  3699.         if (HAS_DDI(dev))
  3700.                 intel_ddi_prepare_link_retrain(encoder);
  3701.  
  3702.         intel_dp_compute_rate(intel_dp, intel_dp->link_rate,
  3703.                               &link_bw, &rate_select);
  3704.  
  3705.         /* Write the link configuration data */
  3706.         link_config[0] = link_bw;
  3707.         link_config[1] = intel_dp->lane_count;
  3708.         if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
  3709.                 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  3710.         drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_BW_SET, link_config, 2);
  3711.         if (intel_dp->num_sink_rates)
  3712.                 drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_RATE_SET,
  3713.                                   &rate_select, 1);
  3714.  
  3715.         link_config[0] = 0;
  3716.         link_config[1] = DP_SET_ANSI_8B10B;
  3717.         drm_dp_dpcd_write(&intel_dp->aux, DP_DOWNSPREAD_CTRL, link_config, 2);
  3718.  
  3719.         DP |= DP_PORT_EN;
  3720.  
  3721.         /* clock recovery */
  3722.         if (!intel_dp_reset_link_train(intel_dp, &DP,
  3723.                                        DP_TRAINING_PATTERN_1 |
  3724.                                        DP_LINK_SCRAMBLING_DISABLE)) {
  3725.                 DRM_ERROR("failed to enable link training\n");
  3726.                 return;
  3727.         }
  3728.  
  3729.         voltage = 0xff;
  3730.         voltage_tries = 0;
  3731.         loop_tries = 0;
  3732.         for (;;) {
  3733.                 uint8_t link_status[DP_LINK_STATUS_SIZE];
  3734.  
  3735.                 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
  3736.                 if (!intel_dp_get_link_status(intel_dp, link_status)) {
  3737.                         DRM_ERROR("failed to get link status\n");
  3738.                         break;
  3739.                 }
  3740.  
  3741.                 if (drm_dp_clock_recovery_ok(link_status, intel_dp->lane_count)) {
  3742.                         DRM_DEBUG_KMS("clock recovery OK\n");
  3743.                         break;
  3744.                 }
  3745.  
  3746.  
  3747.                 /* Check to see if we've tried the max voltage */
  3748.                 for (i = 0; i < intel_dp->lane_count; i++)
  3749.                         if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  3750.                                 break;
  3751.                 if (i == intel_dp->lane_count) {
  3752.                         ++loop_tries;
  3753.                         if (loop_tries == 5) {
  3754.                                 DRM_ERROR("too many full retries, give up\n");
  3755.                                 break;
  3756.                         }
  3757.                         intel_dp_reset_link_train(intel_dp, &DP,
  3758.                                                   DP_TRAINING_PATTERN_1 |
  3759.                                                   DP_LINK_SCRAMBLING_DISABLE);
  3760.                         voltage_tries = 0;
  3761.                         continue;
  3762.                 }
  3763.  
  3764.                 /* Check to see if we've tried the same voltage 5 times */
  3765.                 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  3766.                         ++voltage_tries;
  3767.                         if (voltage_tries == 5) {
  3768.                                 DRM_ERROR("too many voltage retries, give up\n");
  3769.                                 break;
  3770.                         }
  3771.                 } else
  3772.                         voltage_tries = 0;
  3773.                 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  3774.  
  3775.                 /* Update training set as requested by target */
  3776.                 if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
  3777.                         DRM_ERROR("failed to update link training\n");
  3778.                         break;
  3779.                 }
  3780.         }
  3781.  
  3782.         intel_dp->DP = DP;
  3783. }
  3784.  
  3785. static void
  3786. intel_dp_link_training_channel_equalization(struct intel_dp *intel_dp)
  3787. {
  3788.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3789.         struct drm_device *dev = dig_port->base.base.dev;
  3790.         bool channel_eq = false;
  3791.         int tries, cr_tries;
  3792.         uint32_t DP = intel_dp->DP;
  3793.         uint32_t training_pattern = DP_TRAINING_PATTERN_2;
  3794.  
  3795.         /*
  3796.          * Training Pattern 3 for HBR2 or 1.2 devices that support it.
  3797.          *
  3798.          * Intel platforms that support HBR2 also support TPS3. TPS3 support is
  3799.          * also mandatory for downstream devices that support HBR2.
  3800.          *
  3801.          * Due to WaDisableHBR2 SKL < B0 is the only exception where TPS3 is
  3802.          * supported but still not enabled.
  3803.          */
  3804.         if (intel_dp_source_supports_hbr2(dev) &&
  3805.             drm_dp_tps3_supported(intel_dp->dpcd))
  3806.                 training_pattern = DP_TRAINING_PATTERN_3;
  3807.         else if (intel_dp->link_rate == 540000)
  3808.                 DRM_ERROR("5.4 Gbps link rate without HBR2/TPS3 support\n");
  3809.  
  3810.         /* channel equalization */
  3811.         if (!intel_dp_set_link_train(intel_dp, &DP,
  3812.                                      training_pattern |
  3813.                                      DP_LINK_SCRAMBLING_DISABLE)) {
  3814.                 DRM_ERROR("failed to start channel equalization\n");
  3815.                 return;
  3816.         }
  3817.  
  3818.         tries = 0;
  3819.         cr_tries = 0;
  3820.         channel_eq = false;
  3821.         for (;;) {
  3822.                 uint8_t link_status[DP_LINK_STATUS_SIZE];
  3823.  
  3824.                 if (cr_tries > 5) {
  3825.                         DRM_ERROR("failed to train DP, aborting\n");
  3826.                         break;
  3827.                 }
  3828.  
  3829.                 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
  3830.                 if (!intel_dp_get_link_status(intel_dp, link_status)) {
  3831.                         DRM_ERROR("failed to get link status\n");
  3832.                         break;
  3833.                 }
  3834.  
  3835.                 /* Make sure clock is still ok */
  3836.                 if (!drm_dp_clock_recovery_ok(link_status,
  3837.                                               intel_dp->lane_count)) {
  3838.                         intel_dp_link_training_clock_recovery(intel_dp);
  3839.                         intel_dp_set_link_train(intel_dp, &DP,
  3840.                                                 training_pattern |
  3841.                                                 DP_LINK_SCRAMBLING_DISABLE);
  3842.                         cr_tries++;
  3843.                         continue;
  3844.                 }
  3845.  
  3846.                 if (drm_dp_channel_eq_ok(link_status,
  3847.                                          intel_dp->lane_count)) {
  3848.                         channel_eq = true;
  3849.                         break;
  3850.                 }
  3851.  
  3852.                 /* Try 5 times, then try clock recovery if that fails */
  3853.                 if (tries > 5) {
  3854.                         intel_dp_link_training_clock_recovery(intel_dp);
  3855.                         intel_dp_set_link_train(intel_dp, &DP,
  3856.                                                 training_pattern |
  3857.                                                 DP_LINK_SCRAMBLING_DISABLE);
  3858.                         tries = 0;
  3859.                         cr_tries++;
  3860.                         continue;
  3861.                 }
  3862.  
  3863.                 /* Update training set as requested by target */
  3864.                 if (!intel_dp_update_link_train(intel_dp, &DP, link_status)) {
  3865.                         DRM_ERROR("failed to update link training\n");
  3866.                         break;
  3867.                 }
  3868.                 ++tries;
  3869.         }
  3870.  
  3871.         intel_dp_set_idle_link_train(intel_dp);
  3872.  
  3873.         intel_dp->DP = DP;
  3874.  
  3875.         if (channel_eq)
  3876.                 DRM_DEBUG_KMS("Channel EQ done. DP Training successful\n");
  3877.         }
  3878.  
  3879. void intel_dp_stop_link_train(struct intel_dp *intel_dp)
  3880. {
  3881.         intel_dp_set_link_train(intel_dp, &intel_dp->DP,
  3882.                                 DP_TRAINING_PATTERN_DISABLE);
  3883. }
  3884.  
  3885. void
  3886. intel_dp_start_link_train(struct intel_dp *intel_dp)
  3887. {
  3888.         intel_dp_link_training_clock_recovery(intel_dp);
  3889.         intel_dp_link_training_channel_equalization(intel_dp);
  3890. }
  3891.  
  3892. static void
  3893. intel_dp_link_down(struct intel_dp *intel_dp)
  3894. {
  3895.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  3896.         struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
  3897.         enum port port = intel_dig_port->port;
  3898.         struct drm_device *dev = intel_dig_port->base.base.dev;
  3899.         struct drm_i915_private *dev_priv = dev->dev_private;
  3900.         uint32_t DP = intel_dp->DP;
  3901.  
  3902.         if (WARN_ON(HAS_DDI(dev)))
  3903.                 return;
  3904.  
  3905.         if (WARN_ON((I915_READ(intel_dp->output_reg) & DP_PORT_EN) == 0))
  3906.                 return;
  3907.  
  3908.         DRM_DEBUG_KMS("\n");
  3909.  
  3910.         if ((IS_GEN7(dev) && port == PORT_A) ||
  3911.             (HAS_PCH_CPT(dev) && port != PORT_A)) {
  3912.                 DP &= ~DP_LINK_TRAIN_MASK_CPT;
  3913.                 DP |= DP_LINK_TRAIN_PAT_IDLE_CPT;
  3914.         } else {
  3915.                 if (IS_CHERRYVIEW(dev))
  3916.                         DP &= ~DP_LINK_TRAIN_MASK_CHV;
  3917.                 else
  3918.                         DP &= ~DP_LINK_TRAIN_MASK;
  3919.                 DP |= DP_LINK_TRAIN_PAT_IDLE;
  3920.         }
  3921.         I915_WRITE(intel_dp->output_reg, DP);
  3922.         POSTING_READ(intel_dp->output_reg);
  3923.  
  3924.         DP &= ~(DP_PORT_EN | DP_AUDIO_OUTPUT_ENABLE);
  3925.         I915_WRITE(intel_dp->output_reg, DP);
  3926.         POSTING_READ(intel_dp->output_reg);
  3927.  
  3928.         /*
  3929.          * HW workaround for IBX, we need to move the port
  3930.          * to transcoder A after disabling it to allow the
  3931.          * matching HDMI port to be enabled on transcoder A.
  3932.          */
  3933.         if (HAS_PCH_IBX(dev) && crtc->pipe == PIPE_B && port != PORT_A) {
  3934.                 /* always enable with pattern 1 (as per spec) */
  3935.                 DP &= ~(DP_PIPEB_SELECT | DP_LINK_TRAIN_MASK);
  3936.                 DP |= DP_PORT_EN | DP_LINK_TRAIN_PAT_1;
  3937.                 I915_WRITE(intel_dp->output_reg, DP);
  3938.                 POSTING_READ(intel_dp->output_reg);
  3939.  
  3940.                 DP &= ~DP_PORT_EN;
  3941.                 I915_WRITE(intel_dp->output_reg, DP);
  3942.                 POSTING_READ(intel_dp->output_reg);
  3943.         }
  3944.  
  3945.         msleep(intel_dp->panel_power_down_delay);
  3946. }
  3947.  
  3948. static bool
  3949. intel_dp_get_dpcd(struct intel_dp *intel_dp)
  3950. {
  3951.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  3952.         struct drm_device *dev = dig_port->base.base.dev;
  3953.         struct drm_i915_private *dev_priv = dev->dev_private;
  3954.         uint8_t rev;
  3955.  
  3956.         if (intel_dp_dpcd_read_wake(&intel_dp->aux, 0x000, intel_dp->dpcd,
  3957.                                     sizeof(intel_dp->dpcd)) < 0)
  3958.                 return false; /* aux transfer failed */
  3959.  
  3960.         DRM_DEBUG_KMS("DPCD: %*ph\n", (int) sizeof(intel_dp->dpcd), intel_dp->dpcd);
  3961.  
  3962.         if (intel_dp->dpcd[DP_DPCD_REV] == 0)
  3963.                 return false; /* DPCD not present */
  3964.  
  3965.         /* Check if the panel supports PSR */
  3966.         memset(intel_dp->psr_dpcd, 0, sizeof(intel_dp->psr_dpcd));
  3967.         if (is_edp(intel_dp)) {
  3968.                 intel_dp_dpcd_read_wake(&intel_dp->aux, DP_PSR_SUPPORT,
  3969.                                         intel_dp->psr_dpcd,
  3970.                                         sizeof(intel_dp->psr_dpcd));
  3971.                 if (intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED) {
  3972.                         dev_priv->psr.sink_support = true;
  3973.                         DRM_DEBUG_KMS("Detected EDP PSR Panel.\n");
  3974.                 }
  3975.  
  3976.                 if (INTEL_INFO(dev)->gen >= 9 &&
  3977.                         (intel_dp->psr_dpcd[0] & DP_PSR2_IS_SUPPORTED)) {
  3978.                         uint8_t frame_sync_cap;
  3979.  
  3980.                         dev_priv->psr.sink_support = true;
  3981.                         intel_dp_dpcd_read_wake(&intel_dp->aux,
  3982.                                         DP_SINK_DEVICE_AUX_FRAME_SYNC_CAP,
  3983.                                         &frame_sync_cap, 1);
  3984.                         dev_priv->psr.aux_frame_sync = frame_sync_cap ? true : false;
  3985.                         /* PSR2 needs frame sync as well */
  3986.                         dev_priv->psr.psr2_support = dev_priv->psr.aux_frame_sync;
  3987.                         DRM_DEBUG_KMS("PSR2 %s on sink",
  3988.                                 dev_priv->psr.psr2_support ? "supported" : "not supported");
  3989.                 }
  3990.         }
  3991.  
  3992.         DRM_DEBUG_KMS("Display Port TPS3 support: source %s, sink %s\n",
  3993.                       yesno(intel_dp_source_supports_hbr2(dev)),
  3994.                       yesno(drm_dp_tps3_supported(intel_dp->dpcd)));
  3995.  
  3996.         /* Intermediate frequency support */
  3997.         if (is_edp(intel_dp) &&
  3998.             (intel_dp->dpcd[DP_EDP_CONFIGURATION_CAP] & DP_DPCD_DISPLAY_CONTROL_CAPABLE) &&
  3999.             (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_EDP_DPCD_REV, &rev, 1) == 1) &&
  4000.             (rev >= 0x03)) { /* eDp v1.4 or higher */
  4001.                 __le16 sink_rates[DP_MAX_SUPPORTED_RATES];
  4002.                 int i;
  4003.  
  4004.                 intel_dp_dpcd_read_wake(&intel_dp->aux,
  4005.                                 DP_SUPPORTED_LINK_RATES,
  4006.                                 sink_rates,
  4007.                                 sizeof(sink_rates));
  4008.  
  4009.                 for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {
  4010.                         int val = le16_to_cpu(sink_rates[i]);
  4011.  
  4012.                         if (val == 0)
  4013.                                 break;
  4014.  
  4015.                         /* Value read is in kHz while drm clock is saved in deca-kHz */
  4016.                         intel_dp->sink_rates[i] = (val * 200) / 10;
  4017.                 }
  4018.                 intel_dp->num_sink_rates = i;
  4019.         }
  4020.  
  4021.         intel_dp_print_rates(intel_dp);
  4022.  
  4023.         if (!(intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  4024.               DP_DWN_STRM_PORT_PRESENT))
  4025.                 return true; /* native DP sink */
  4026.  
  4027.         if (intel_dp->dpcd[DP_DPCD_REV] == 0x10)
  4028.                 return true; /* no per-port downstream info */
  4029.  
  4030.         if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_DOWNSTREAM_PORT_0,
  4031.                                     intel_dp->downstream_ports,
  4032.                                     DP_MAX_DOWNSTREAM_PORTS) < 0)
  4033.                 return false; /* downstream port status fetch failed */
  4034.  
  4035.         return true;
  4036. }
  4037.  
  4038. static void
  4039. intel_dp_probe_oui(struct intel_dp *intel_dp)
  4040. {
  4041.         u8 buf[3];
  4042.  
  4043.         if (!(intel_dp->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  4044.                 return;
  4045.  
  4046.         if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_OUI, buf, 3) == 3)
  4047.                 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
  4048.                               buf[0], buf[1], buf[2]);
  4049.  
  4050.         if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_BRANCH_OUI, buf, 3) == 3)
  4051.                 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
  4052.                               buf[0], buf[1], buf[2]);
  4053. }
  4054.  
  4055. static bool
  4056. intel_dp_probe_mst(struct intel_dp *intel_dp)
  4057. {
  4058.         u8 buf[1];
  4059.  
  4060.         if (!intel_dp->can_mst)
  4061.                 return false;
  4062.  
  4063.         if (intel_dp->dpcd[DP_DPCD_REV] < 0x12)
  4064.                 return false;
  4065.  
  4066.         if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_MSTM_CAP, buf, 1)) {
  4067.                 if (buf[0] & DP_MST_CAP) {
  4068.                         DRM_DEBUG_KMS("Sink is MST capable\n");
  4069.                         intel_dp->is_mst = true;
  4070.                 } else {
  4071.                         DRM_DEBUG_KMS("Sink is not MST capable\n");
  4072.                         intel_dp->is_mst = false;
  4073.                 }
  4074.         }
  4075.  
  4076.         drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  4077.         return intel_dp->is_mst;
  4078. }
  4079.  
  4080. static int intel_dp_sink_crc_stop(struct intel_dp *intel_dp)
  4081. {
  4082.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  4083.         struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  4084.         u8 buf;
  4085.         int ret = 0;
  4086.  
  4087.         if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0) {
  4088.                 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
  4089.                 ret = -EIO;
  4090.                 goto out;
  4091.         }
  4092.  
  4093.         if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
  4094.                                buf & ~DP_TEST_SINK_START) < 0) {
  4095.                 DRM_DEBUG_KMS("Sink CRC couldn't be stopped properly\n");
  4096.                 ret = -EIO;
  4097.                 goto out;
  4098.         }
  4099.  
  4100.         intel_dp->sink_crc.started = false;
  4101.  out:
  4102.         hsw_enable_ips(intel_crtc);
  4103.         return ret;
  4104. }
  4105.  
  4106. static int intel_dp_sink_crc_start(struct intel_dp *intel_dp)
  4107. {
  4108.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  4109.         struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  4110.         u8 buf;
  4111.         int ret;
  4112.  
  4113.         if (intel_dp->sink_crc.started) {
  4114.                 ret = intel_dp_sink_crc_stop(intel_dp);
  4115.                 if (ret)
  4116.                         return ret;
  4117.         }
  4118.  
  4119.         if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK_MISC, &buf) < 0)
  4120.                 return -EIO;
  4121.  
  4122.         if (!(buf & DP_TEST_CRC_SUPPORTED))
  4123.                 return -ENOTTY;
  4124.  
  4125.         intel_dp->sink_crc.last_count = buf & DP_TEST_COUNT_MASK;
  4126.  
  4127.         if (drm_dp_dpcd_readb(&intel_dp->aux, DP_TEST_SINK, &buf) < 0)
  4128.                 return -EIO;
  4129.  
  4130.         hsw_disable_ips(intel_crtc);
  4131.  
  4132.         if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_TEST_SINK,
  4133.                                buf | DP_TEST_SINK_START) < 0) {
  4134.                 hsw_enable_ips(intel_crtc);
  4135.                 return -EIO;
  4136.         }
  4137.  
  4138.         intel_dp->sink_crc.started = true;
  4139.         return 0;
  4140. }
  4141.  
  4142. int intel_dp_sink_crc(struct intel_dp *intel_dp, u8 *crc)
  4143. {
  4144.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  4145.         struct drm_device *dev = dig_port->base.base.dev;
  4146.         struct intel_crtc *intel_crtc = to_intel_crtc(dig_port->base.base.crtc);
  4147.         u8 buf;
  4148.         int count, ret;
  4149.         int attempts = 6;
  4150.         bool old_equal_new;
  4151.  
  4152.         ret = intel_dp_sink_crc_start(intel_dp);
  4153.         if (ret)
  4154.                 return ret;
  4155.  
  4156.         do {
  4157.                 intel_wait_for_vblank(dev, intel_crtc->pipe);
  4158.  
  4159.                 if (drm_dp_dpcd_readb(&intel_dp->aux,
  4160.                                       DP_TEST_SINK_MISC, &buf) < 0) {
  4161.                         ret = -EIO;
  4162.                         goto stop;
  4163.                 }
  4164.                 count = buf & DP_TEST_COUNT_MASK;
  4165.  
  4166.                 /*
  4167.                  * Count might be reset during the loop. In this case
  4168.                  * last known count needs to be reset as well.
  4169.                  */
  4170.                 if (count == 0)
  4171.                         intel_dp->sink_crc.last_count = 0;
  4172.  
  4173.                 if (drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_CRC_R_CR, crc, 6) < 0) {
  4174.                         ret = -EIO;
  4175.                         goto stop;
  4176.                 }
  4177.  
  4178.                 old_equal_new = (count == intel_dp->sink_crc.last_count &&
  4179.                                  !memcmp(intel_dp->sink_crc.last_crc, crc,
  4180.                                          6 * sizeof(u8)));
  4181.  
  4182.         } while (--attempts && (count == 0 || old_equal_new));
  4183.  
  4184.         intel_dp->sink_crc.last_count = buf & DP_TEST_COUNT_MASK;
  4185.         memcpy(intel_dp->sink_crc.last_crc, crc, 6 * sizeof(u8));
  4186.  
  4187.         if (attempts == 0) {
  4188.                 if (old_equal_new) {
  4189.                         DRM_DEBUG_KMS("Unreliable Sink CRC counter: Current returned CRC is identical to the previous one\n");
  4190.                 } else {
  4191.                         DRM_ERROR("Panel is unable to calculate any CRC after 6 vblanks\n");
  4192.                         ret = -ETIMEDOUT;
  4193.                         goto stop;
  4194.                 }
  4195.         }
  4196.  
  4197. stop:
  4198.         intel_dp_sink_crc_stop(intel_dp);
  4199.         return ret;
  4200. }
  4201.  
  4202. static bool
  4203. intel_dp_get_sink_irq(struct intel_dp *intel_dp, u8 *sink_irq_vector)
  4204. {
  4205.         return intel_dp_dpcd_read_wake(&intel_dp->aux,
  4206.                                        DP_DEVICE_SERVICE_IRQ_VECTOR,
  4207.                                        sink_irq_vector, 1) == 1;
  4208. }
  4209.  
  4210. static bool
  4211. intel_dp_get_sink_irq_esi(struct intel_dp *intel_dp, u8 *sink_irq_vector)
  4212. {
  4213.         int ret;
  4214.  
  4215.         ret = intel_dp_dpcd_read_wake(&intel_dp->aux,
  4216.                                              DP_SINK_COUNT_ESI,
  4217.                                              sink_irq_vector, 14);
  4218.         if (ret != 14)
  4219.                 return false;
  4220.  
  4221.         return true;
  4222. }
  4223.  
  4224. static uint8_t intel_dp_autotest_link_training(struct intel_dp *intel_dp)
  4225. {
  4226.         uint8_t test_result = DP_TEST_ACK;
  4227.         return test_result;
  4228. }
  4229.  
  4230. static uint8_t intel_dp_autotest_video_pattern(struct intel_dp *intel_dp)
  4231. {
  4232.         uint8_t test_result = DP_TEST_NAK;
  4233.         return test_result;
  4234. }
  4235.  
  4236. static uint8_t intel_dp_autotest_edid(struct intel_dp *intel_dp)
  4237. {
  4238.         uint8_t test_result = DP_TEST_NAK;
  4239.         struct intel_connector *intel_connector = intel_dp->attached_connector;
  4240.         struct drm_connector *connector = &intel_connector->base;
  4241.  
  4242.         if (intel_connector->detect_edid == NULL ||
  4243.             connector->edid_corrupt ||
  4244.             intel_dp->aux.i2c_defer_count > 6) {
  4245.                 /* Check EDID read for NACKs, DEFERs and corruption
  4246.                  * (DP CTS 1.2 Core r1.1)
  4247.                  *    4.2.2.4 : Failed EDID read, I2C_NAK
  4248.                  *    4.2.2.5 : Failed EDID read, I2C_DEFER
  4249.                  *    4.2.2.6 : EDID corruption detected
  4250.                  * Use failsafe mode for all cases
  4251.                  */
  4252.                 if (intel_dp->aux.i2c_nack_count > 0 ||
  4253.                         intel_dp->aux.i2c_defer_count > 0)
  4254.                         DRM_DEBUG_KMS("EDID read had %d NACKs, %d DEFERs\n",
  4255.                                       intel_dp->aux.i2c_nack_count,
  4256.                                       intel_dp->aux.i2c_defer_count);
  4257.                 intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_FAILSAFE;
  4258.         } else {
  4259.                 struct edid *block = intel_connector->detect_edid;
  4260.  
  4261.                 /* We have to write the checksum
  4262.                  * of the last block read
  4263.                  */
  4264.                 block += intel_connector->detect_edid->extensions;
  4265.  
  4266.                 if (!drm_dp_dpcd_write(&intel_dp->aux,
  4267.                                         DP_TEST_EDID_CHECKSUM,
  4268.                                         &block->checksum,
  4269.                                         1))
  4270.                         DRM_DEBUG_KMS("Failed to write EDID checksum\n");
  4271.  
  4272.                 test_result = DP_TEST_ACK | DP_TEST_EDID_CHECKSUM_WRITE;
  4273.                 intel_dp->compliance_test_data = INTEL_DP_RESOLUTION_STANDARD;
  4274.         }
  4275.  
  4276.         /* Set test active flag here so userspace doesn't interrupt things */
  4277.         intel_dp->compliance_test_active = 1;
  4278.  
  4279.         return test_result;
  4280. }
  4281.  
  4282. static uint8_t intel_dp_autotest_phy_pattern(struct intel_dp *intel_dp)
  4283. {
  4284.         uint8_t test_result = DP_TEST_NAK;
  4285.         return test_result;
  4286. }
  4287.  
  4288. static void intel_dp_handle_test_request(struct intel_dp *intel_dp)
  4289. {
  4290.         uint8_t response = DP_TEST_NAK;
  4291.         uint8_t rxdata = 0;
  4292.         int status = 0;
  4293.  
  4294.         intel_dp->compliance_test_active = 0;
  4295.         intel_dp->compliance_test_type = 0;
  4296.         intel_dp->compliance_test_data = 0;
  4297.  
  4298.         intel_dp->aux.i2c_nack_count = 0;
  4299.         intel_dp->aux.i2c_defer_count = 0;
  4300.  
  4301.         status = drm_dp_dpcd_read(&intel_dp->aux, DP_TEST_REQUEST, &rxdata, 1);
  4302.         if (status <= 0) {
  4303.                 DRM_DEBUG_KMS("Could not read test request from sink\n");
  4304.                 goto update_status;
  4305.         }
  4306.  
  4307.         switch (rxdata) {
  4308.         case DP_TEST_LINK_TRAINING:
  4309.                 DRM_DEBUG_KMS("LINK_TRAINING test requested\n");
  4310.                 intel_dp->compliance_test_type = DP_TEST_LINK_TRAINING;
  4311.                 response = intel_dp_autotest_link_training(intel_dp);
  4312.                 break;
  4313.         case DP_TEST_LINK_VIDEO_PATTERN:
  4314.                 DRM_DEBUG_KMS("TEST_PATTERN test requested\n");
  4315.                 intel_dp->compliance_test_type = DP_TEST_LINK_VIDEO_PATTERN;
  4316.                 response = intel_dp_autotest_video_pattern(intel_dp);
  4317.                 break;
  4318.         case DP_TEST_LINK_EDID_READ:
  4319.                 DRM_DEBUG_KMS("EDID test requested\n");
  4320.                 intel_dp->compliance_test_type = DP_TEST_LINK_EDID_READ;
  4321.                 response = intel_dp_autotest_edid(intel_dp);
  4322.                 break;
  4323.         case DP_TEST_LINK_PHY_TEST_PATTERN:
  4324.                 DRM_DEBUG_KMS("PHY_PATTERN test requested\n");
  4325.                 intel_dp->compliance_test_type = DP_TEST_LINK_PHY_TEST_PATTERN;
  4326.                 response = intel_dp_autotest_phy_pattern(intel_dp);
  4327.                 break;
  4328.         default:
  4329.                 DRM_DEBUG_KMS("Invalid test request '%02x'\n", rxdata);
  4330.                 break;
  4331.         }
  4332.  
  4333. update_status:
  4334.         status = drm_dp_dpcd_write(&intel_dp->aux,
  4335.                                    DP_TEST_RESPONSE,
  4336.                                    &response, 1);
  4337.         if (status <= 0)
  4338.                 DRM_DEBUG_KMS("Could not write test response to sink\n");
  4339. }
  4340.  
  4341. static int
  4342. intel_dp_check_mst_status(struct intel_dp *intel_dp)
  4343. {
  4344.         bool bret;
  4345.  
  4346.         if (intel_dp->is_mst) {
  4347.                 u8 esi[16] = { 0 };
  4348.                 int ret = 0;
  4349.                 int retry;
  4350.                 bool handled;
  4351.                 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
  4352. go_again:
  4353.                 if (bret == true) {
  4354.  
  4355.                         /* check link status - esi[10] = 0x200c */
  4356.                         if (intel_dp->active_mst_links &&
  4357.                             !drm_dp_channel_eq_ok(&esi[10], intel_dp->lane_count)) {
  4358.                                 DRM_DEBUG_KMS("channel EQ not ok, retraining\n");
  4359.                                 intel_dp_start_link_train(intel_dp);
  4360.                                 intel_dp_stop_link_train(intel_dp);
  4361.                         }
  4362.  
  4363.                         DRM_DEBUG_KMS("got esi %3ph\n", esi);
  4364.                         ret = drm_dp_mst_hpd_irq(&intel_dp->mst_mgr, esi, &handled);
  4365.  
  4366.                         if (handled) {
  4367.                                 for (retry = 0; retry < 3; retry++) {
  4368.                                         int wret;
  4369.                                         wret = drm_dp_dpcd_write(&intel_dp->aux,
  4370.                                                                  DP_SINK_COUNT_ESI+1,
  4371.                                                                  &esi[1], 3);
  4372.                                         if (wret == 3) {
  4373.                                                 break;
  4374.                                         }
  4375.                                 }
  4376.  
  4377.                                 bret = intel_dp_get_sink_irq_esi(intel_dp, esi);
  4378.                                 if (bret == true) {
  4379.                                         DRM_DEBUG_KMS("got esi2 %3ph\n", esi);
  4380.                                         goto go_again;
  4381.                                 }
  4382.                         } else
  4383.                                 ret = 0;
  4384.  
  4385.                         return ret;
  4386.                 } else {
  4387.                         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4388.                         DRM_DEBUG_KMS("failed to get ESI - device may have failed\n");
  4389.                         intel_dp->is_mst = false;
  4390.                         drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  4391.                         /* send a hotplug event */
  4392.                         drm_kms_helper_hotplug_event(intel_dig_port->base.base.dev);
  4393.                 }
  4394.         }
  4395.         return -EINVAL;
  4396. }
  4397.  
  4398. /*
  4399.  * According to DP spec
  4400.  * 5.1.2:
  4401.  *  1. Read DPCD
  4402.  *  2. Configure link according to Receiver Capabilities
  4403.  *  3. Use Link Training from 2.5.3.3 and 3.5.1.3
  4404.  *  4. Check link status on receipt of hot-plug interrupt
  4405.  */
  4406. static void
  4407. intel_dp_check_link_status(struct intel_dp *intel_dp)
  4408. {
  4409.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4410.         struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
  4411.         u8 sink_irq_vector;
  4412.         u8 link_status[DP_LINK_STATUS_SIZE];
  4413.  
  4414.         WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  4415.  
  4416.         if (!intel_encoder->base.crtc)
  4417.                 return;
  4418.  
  4419.         if (!to_intel_crtc(intel_encoder->base.crtc)->active)
  4420.                 return;
  4421.  
  4422.         /* Try to read receiver status if the link appears to be up */
  4423.         if (!intel_dp_get_link_status(intel_dp, link_status)) {
  4424.                 return;
  4425.         }
  4426.  
  4427.         /* Now read the DPCD to see if it's actually running */
  4428.         if (!intel_dp_get_dpcd(intel_dp)) {
  4429.                 return;
  4430.         }
  4431.  
  4432.         /* Try to read the source of the interrupt */
  4433.         if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  4434.             intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
  4435.                 /* Clear interrupt source */
  4436.                 drm_dp_dpcd_writeb(&intel_dp->aux,
  4437.                                    DP_DEVICE_SERVICE_IRQ_VECTOR,
  4438.                                    sink_irq_vector);
  4439.  
  4440.                 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
  4441.                         DRM_DEBUG_DRIVER("Test request in short pulse not handled\n");
  4442.                 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
  4443.                         DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
  4444.         }
  4445.  
  4446.         if (!drm_dp_channel_eq_ok(link_status, intel_dp->lane_count)) {
  4447.                 DRM_DEBUG_KMS("%s: channel EQ not ok, retraining\n",
  4448.                               intel_encoder->base.name);
  4449.                 intel_dp_start_link_train(intel_dp);
  4450.                 intel_dp_stop_link_train(intel_dp);
  4451.         }
  4452. }
  4453.  
  4454. /* XXX this is probably wrong for multiple downstream ports */
  4455. static enum drm_connector_status
  4456. intel_dp_detect_dpcd(struct intel_dp *intel_dp)
  4457. {
  4458.         uint8_t *dpcd = intel_dp->dpcd;
  4459.         uint8_t type;
  4460.  
  4461.         if (!intel_dp_get_dpcd(intel_dp))
  4462.                 return connector_status_disconnected;
  4463.  
  4464.         /* if there's no downstream port, we're done */
  4465.         if (!(dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT))
  4466.                 return connector_status_connected;
  4467.  
  4468.         /* If we're HPD-aware, SINK_COUNT changes dynamically */
  4469.         if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  4470.             intel_dp->downstream_ports[0] & DP_DS_PORT_HPD) {
  4471.                 uint8_t reg;
  4472.  
  4473.                 if (intel_dp_dpcd_read_wake(&intel_dp->aux, DP_SINK_COUNT,
  4474.                                             &reg, 1) < 0)
  4475.                         return connector_status_unknown;
  4476.  
  4477.                 return DP_GET_SINK_COUNT(reg) ? connector_status_connected
  4478.                                               : connector_status_disconnected;
  4479.         }
  4480.  
  4481.         /* If no HPD, poke DDC gently */
  4482.         if (drm_probe_ddc(&intel_dp->aux.ddc))
  4483.                 return connector_status_connected;
  4484.  
  4485.         /* Well we tried, say unknown for unreliable port types */
  4486.         if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
  4487.                 type = intel_dp->downstream_ports[0] & DP_DS_PORT_TYPE_MASK;
  4488.                 if (type == DP_DS_PORT_TYPE_VGA ||
  4489.                     type == DP_DS_PORT_TYPE_NON_EDID)
  4490.                         return connector_status_unknown;
  4491.         } else {
  4492.                 type = intel_dp->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
  4493.                         DP_DWN_STRM_PORT_TYPE_MASK;
  4494.                 if (type == DP_DWN_STRM_PORT_TYPE_ANALOG ||
  4495.                     type == DP_DWN_STRM_PORT_TYPE_OTHER)
  4496.                         return connector_status_unknown;
  4497.         }
  4498.  
  4499.         /* Anything else is out of spec, warn and ignore */
  4500.         DRM_DEBUG_KMS("Broken DP branch device, ignoring\n");
  4501.         return connector_status_disconnected;
  4502. }
  4503.  
  4504. static enum drm_connector_status
  4505. edp_detect(struct intel_dp *intel_dp)
  4506. {
  4507.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4508.         enum drm_connector_status status;
  4509.  
  4510.         status = intel_panel_detect(dev);
  4511.         if (status == connector_status_unknown)
  4512.                 status = connector_status_connected;
  4513.  
  4514.         return status;
  4515. }
  4516.  
  4517. static bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  4518.                                        struct intel_digital_port *port)
  4519. {
  4520.         u32 bit;
  4521.  
  4522.         switch (port->port) {
  4523.         case PORT_A:
  4524.                 return true;
  4525.         case PORT_B:
  4526.                 bit = SDE_PORTB_HOTPLUG;
  4527.                 break;
  4528.         case PORT_C:
  4529.                 bit = SDE_PORTC_HOTPLUG;
  4530.                 break;
  4531.         case PORT_D:
  4532.                 bit = SDE_PORTD_HOTPLUG;
  4533.                 break;
  4534.         default:
  4535.                 MISSING_CASE(port->port);
  4536.                 return false;
  4537.         }
  4538.  
  4539.         return I915_READ(SDEISR) & bit;
  4540. }
  4541.  
  4542. static bool cpt_digital_port_connected(struct drm_i915_private *dev_priv,
  4543.                                        struct intel_digital_port *port)
  4544. {
  4545.         u32 bit;
  4546.  
  4547.         switch (port->port) {
  4548.         case PORT_A:
  4549.                 return true;
  4550.         case PORT_B:
  4551.                 bit = SDE_PORTB_HOTPLUG_CPT;
  4552.                 break;
  4553.         case PORT_C:
  4554.                 bit = SDE_PORTC_HOTPLUG_CPT;
  4555.                 break;
  4556.         case PORT_D:
  4557.                 bit = SDE_PORTD_HOTPLUG_CPT;
  4558.                 break;
  4559.         case PORT_E:
  4560.                 bit = SDE_PORTE_HOTPLUG_SPT;
  4561.                 break;
  4562.         default:
  4563.                 MISSING_CASE(port->port);
  4564.                 return false;
  4565.         }
  4566.  
  4567.         return I915_READ(SDEISR) & bit;
  4568. }
  4569.  
  4570. static bool g4x_digital_port_connected(struct drm_i915_private *dev_priv,
  4571.                                        struct intel_digital_port *port)
  4572. {
  4573.         u32 bit;
  4574.  
  4575.         switch (port->port) {
  4576.         case PORT_B:
  4577.                 bit = PORTB_HOTPLUG_LIVE_STATUS_G4X;
  4578.                 break;
  4579.         case PORT_C:
  4580.                 bit = PORTC_HOTPLUG_LIVE_STATUS_G4X;
  4581.                 break;
  4582.         case PORT_D:
  4583.                 bit = PORTD_HOTPLUG_LIVE_STATUS_G4X;
  4584.                 break;
  4585.         default:
  4586.                 MISSING_CASE(port->port);
  4587.                 return false;
  4588.         }
  4589.  
  4590.         return I915_READ(PORT_HOTPLUG_STAT) & bit;
  4591. }
  4592.  
  4593. static bool gm45_digital_port_connected(struct drm_i915_private *dev_priv,
  4594.                                        struct intel_digital_port *port)
  4595. {
  4596.         u32 bit;
  4597.  
  4598.         switch (port->port) {
  4599.         case PORT_B:
  4600.                 bit = PORTB_HOTPLUG_LIVE_STATUS_GM45;
  4601.                 break;
  4602.         case PORT_C:
  4603.                 bit = PORTC_HOTPLUG_LIVE_STATUS_GM45;
  4604.                 break;
  4605.         case PORT_D:
  4606.                 bit = PORTD_HOTPLUG_LIVE_STATUS_GM45;
  4607.                 break;
  4608.         default:
  4609.                 MISSING_CASE(port->port);
  4610.                 return false;
  4611.         }
  4612.  
  4613.         return I915_READ(PORT_HOTPLUG_STAT) & bit;
  4614. }
  4615.  
  4616. static bool bxt_digital_port_connected(struct drm_i915_private *dev_priv,
  4617.                                        struct intel_digital_port *intel_dig_port)
  4618. {
  4619.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  4620.         enum port port;
  4621.         u32 bit;
  4622.  
  4623.         intel_hpd_pin_to_port(intel_encoder->hpd_pin, &port);
  4624.         switch (port) {
  4625.         case PORT_A:
  4626.                 bit = BXT_DE_PORT_HP_DDIA;
  4627.                 break;
  4628.         case PORT_B:
  4629.                 bit = BXT_DE_PORT_HP_DDIB;
  4630.                 break;
  4631.         case PORT_C:
  4632.                 bit = BXT_DE_PORT_HP_DDIC;
  4633.                 break;
  4634.         default:
  4635.                 MISSING_CASE(port);
  4636.                 return false;
  4637.         }
  4638.  
  4639.         return I915_READ(GEN8_DE_PORT_ISR) & bit;
  4640. }
  4641.  
  4642. /*
  4643.  * intel_digital_port_connected - is the specified port connected?
  4644.  * @dev_priv: i915 private structure
  4645.  * @port: the port to test
  4646.  *
  4647.  * Return %true if @port is connected, %false otherwise.
  4648.  */
  4649. bool intel_digital_port_connected(struct drm_i915_private *dev_priv,
  4650.                                          struct intel_digital_port *port)
  4651. {
  4652.         if (HAS_PCH_IBX(dev_priv))
  4653.                 return ibx_digital_port_connected(dev_priv, port);
  4654.         if (HAS_PCH_SPLIT(dev_priv))
  4655.                 return cpt_digital_port_connected(dev_priv, port);
  4656.         else if (IS_BROXTON(dev_priv))
  4657.                 return bxt_digital_port_connected(dev_priv, port);
  4658.         else if (IS_GM45(dev_priv))
  4659.                 return gm45_digital_port_connected(dev_priv, port);
  4660.         else
  4661.                 return g4x_digital_port_connected(dev_priv, port);
  4662. }
  4663.  
  4664. static enum drm_connector_status
  4665. ironlake_dp_detect(struct intel_dp *intel_dp)
  4666. {
  4667.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4668.         struct drm_i915_private *dev_priv = dev->dev_private;
  4669.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4670.  
  4671.         if (!intel_digital_port_connected(dev_priv, intel_dig_port))
  4672.                 return connector_status_disconnected;
  4673.  
  4674.         return intel_dp_detect_dpcd(intel_dp);
  4675. }
  4676.  
  4677. static enum drm_connector_status
  4678. g4x_dp_detect(struct intel_dp *intel_dp)
  4679. {
  4680.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  4681.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4682.  
  4683.         /* Can't disconnect eDP, but you can close the lid... */
  4684.         if (is_edp(intel_dp)) {
  4685.                 enum drm_connector_status status;
  4686.  
  4687.                 status = intel_panel_detect(dev);
  4688.                 if (status == connector_status_unknown)
  4689.                         status = connector_status_connected;
  4690.                 return status;
  4691.         }
  4692.  
  4693.         if (!intel_digital_port_connected(dev->dev_private, intel_dig_port))
  4694.                 return connector_status_disconnected;
  4695.  
  4696.         return intel_dp_detect_dpcd(intel_dp);
  4697. }
  4698.  
  4699. static struct edid *
  4700. intel_dp_get_edid(struct intel_dp *intel_dp)
  4701. {
  4702.         struct intel_connector *intel_connector = intel_dp->attached_connector;
  4703.  
  4704.         /* use cached edid if we have one */
  4705.         if (intel_connector->edid) {
  4706.                 /* invalid edid */
  4707.                 if (IS_ERR(intel_connector->edid))
  4708.                         return NULL;
  4709.  
  4710.                 return drm_edid_duplicate(intel_connector->edid);
  4711.         } else
  4712.                 return drm_get_edid(&intel_connector->base,
  4713.                                     &intel_dp->aux.ddc);
  4714. }
  4715.  
  4716. static void
  4717. intel_dp_set_edid(struct intel_dp *intel_dp)
  4718. {
  4719.         struct intel_connector *intel_connector = intel_dp->attached_connector;
  4720.         struct edid *edid;
  4721.  
  4722.         edid = intel_dp_get_edid(intel_dp);
  4723.         intel_connector->detect_edid = edid;
  4724.  
  4725.         if (intel_dp->force_audio != HDMI_AUDIO_AUTO)
  4726.                 intel_dp->has_audio = intel_dp->force_audio == HDMI_AUDIO_ON;
  4727.         else
  4728.                 intel_dp->has_audio = drm_detect_monitor_audio(edid);
  4729. }
  4730.  
  4731. static void
  4732. intel_dp_unset_edid(struct intel_dp *intel_dp)
  4733. {
  4734.         struct intel_connector *intel_connector = intel_dp->attached_connector;
  4735.  
  4736.         kfree(intel_connector->detect_edid);
  4737.         intel_connector->detect_edid = NULL;
  4738.  
  4739.         intel_dp->has_audio = false;
  4740. }
  4741.  
  4742. static enum drm_connector_status
  4743. intel_dp_detect(struct drm_connector *connector, bool force)
  4744. {
  4745.         struct intel_dp *intel_dp = intel_attached_dp(connector);
  4746.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  4747.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  4748.         struct drm_device *dev = connector->dev;
  4749.         enum drm_connector_status status;
  4750.         enum intel_display_power_domain power_domain;
  4751.         bool ret;
  4752.         u8 sink_irq_vector;
  4753.  
  4754.         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4755.                       connector->base.id, connector->name);
  4756.         intel_dp_unset_edid(intel_dp);
  4757.  
  4758.         if (intel_dp->is_mst) {
  4759.                 /* MST devices are disconnected from a monitor POV */
  4760.                 if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4761.                         intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4762.                 return connector_status_disconnected;
  4763.         }
  4764.  
  4765.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  4766.         intel_display_power_get(to_i915(dev), power_domain);
  4767.  
  4768.         /* Can't disconnect eDP, but you can close the lid... */
  4769.         if (is_edp(intel_dp))
  4770.                 status = edp_detect(intel_dp);
  4771.         else if (HAS_PCH_SPLIT(dev))
  4772.                 status = ironlake_dp_detect(intel_dp);
  4773.         else
  4774.                 status = g4x_dp_detect(intel_dp);
  4775.         if (status != connector_status_connected)
  4776.                 goto out;
  4777.  
  4778.         intel_dp_probe_oui(intel_dp);
  4779.  
  4780.         ret = intel_dp_probe_mst(intel_dp);
  4781.         if (ret) {
  4782.                 /* if we are in MST mode then this connector
  4783.                    won't appear connected or have anything with EDID on it */
  4784.                 if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4785.                         intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4786.                 status = connector_status_disconnected;
  4787.                 goto out;
  4788.         }
  4789.  
  4790.         intel_dp_set_edid(intel_dp);
  4791.  
  4792.         if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4793.                 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4794.         status = connector_status_connected;
  4795.  
  4796.         /* Try to read the source of the interrupt */
  4797.         if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
  4798.             intel_dp_get_sink_irq(intel_dp, &sink_irq_vector)) {
  4799.                 /* Clear interrupt source */
  4800.                 drm_dp_dpcd_writeb(&intel_dp->aux,
  4801.                                    DP_DEVICE_SERVICE_IRQ_VECTOR,
  4802.                                    sink_irq_vector);
  4803.  
  4804.                 if (sink_irq_vector & DP_AUTOMATED_TEST_REQUEST)
  4805.                         intel_dp_handle_test_request(intel_dp);
  4806.                 if (sink_irq_vector & (DP_CP_IRQ | DP_SINK_SPECIFIC_IRQ))
  4807.                         DRM_DEBUG_DRIVER("CP or sink specific irq unhandled\n");
  4808.         }
  4809.  
  4810. out:
  4811.         intel_display_power_put(to_i915(dev), power_domain);
  4812.         return status;
  4813. }
  4814.  
  4815. static void
  4816. intel_dp_force(struct drm_connector *connector)
  4817. {
  4818.         struct intel_dp *intel_dp = intel_attached_dp(connector);
  4819.         struct intel_encoder *intel_encoder = &dp_to_dig_port(intel_dp)->base;
  4820.         struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4821.         enum intel_display_power_domain power_domain;
  4822.  
  4823.         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4824.                       connector->base.id, connector->name);
  4825.         intel_dp_unset_edid(intel_dp);
  4826.  
  4827.         if (connector->status != connector_status_connected)
  4828.                 return;
  4829.  
  4830.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  4831.         intel_display_power_get(dev_priv, power_domain);
  4832.  
  4833.         intel_dp_set_edid(intel_dp);
  4834.  
  4835.         intel_display_power_put(dev_priv, power_domain);
  4836.  
  4837.         if (intel_encoder->type != INTEL_OUTPUT_EDP)
  4838.                 intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  4839. }
  4840.  
  4841. static int intel_dp_get_modes(struct drm_connector *connector)
  4842. {
  4843.         struct intel_connector *intel_connector = to_intel_connector(connector);
  4844.         struct edid *edid;
  4845.  
  4846.         edid = intel_connector->detect_edid;
  4847.         if (edid) {
  4848.                 int ret = intel_connector_update_modes(connector, edid);
  4849.                 if (ret)
  4850.                         return ret;
  4851.         }
  4852.  
  4853.         /* if eDP has no EDID, fall back to fixed mode */
  4854.         if (is_edp(intel_attached_dp(connector)) &&
  4855.             intel_connector->panel.fixed_mode) {
  4856.                 struct drm_display_mode *mode;
  4857.  
  4858.                 mode = drm_mode_duplicate(connector->dev,
  4859.                                           intel_connector->panel.fixed_mode);
  4860.                 if (mode) {
  4861.                         drm_mode_probed_add(connector, mode);
  4862.                         return 1;
  4863.                 }
  4864.         }
  4865.  
  4866.         return 0;
  4867. }
  4868.  
  4869. static bool
  4870. intel_dp_detect_audio(struct drm_connector *connector)
  4871. {
  4872.         bool has_audio = false;
  4873.         struct edid *edid;
  4874.  
  4875.         edid = to_intel_connector(connector)->detect_edid;
  4876.         if (edid)
  4877.                 has_audio = drm_detect_monitor_audio(edid);
  4878.  
  4879.         return has_audio;
  4880. }
  4881.  
  4882. static int
  4883. intel_dp_set_property(struct drm_connector *connector,
  4884.                       struct drm_property *property,
  4885.                       uint64_t val)
  4886. {
  4887.         struct drm_i915_private *dev_priv = connector->dev->dev_private;
  4888.         struct intel_connector *intel_connector = to_intel_connector(connector);
  4889.         struct intel_encoder *intel_encoder = intel_attached_encoder(connector);
  4890.         struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
  4891.         int ret;
  4892.  
  4893.         ret = drm_object_property_set_value(&connector->base, property, val);
  4894.         if (ret)
  4895.                 return ret;
  4896.  
  4897.         if (property == dev_priv->force_audio_property) {
  4898.                 int i = val;
  4899.                 bool has_audio;
  4900.  
  4901.                 if (i == intel_dp->force_audio)
  4902.                         return 0;
  4903.  
  4904.                 intel_dp->force_audio = i;
  4905.  
  4906.                 if (i == HDMI_AUDIO_AUTO)
  4907.                         has_audio = intel_dp_detect_audio(connector);
  4908.                 else
  4909.                         has_audio = (i == HDMI_AUDIO_ON);
  4910.  
  4911.                 if (has_audio == intel_dp->has_audio)
  4912.                         return 0;
  4913.  
  4914.                 intel_dp->has_audio = has_audio;
  4915.                 goto done;
  4916.         }
  4917.  
  4918.         if (property == dev_priv->broadcast_rgb_property) {
  4919.                 bool old_auto = intel_dp->color_range_auto;
  4920.                 bool old_range = intel_dp->limited_color_range;
  4921.  
  4922.                 switch (val) {
  4923.                 case INTEL_BROADCAST_RGB_AUTO:
  4924.                         intel_dp->color_range_auto = true;
  4925.                         break;
  4926.                 case INTEL_BROADCAST_RGB_FULL:
  4927.                         intel_dp->color_range_auto = false;
  4928.                         intel_dp->limited_color_range = false;
  4929.                         break;
  4930.                 case INTEL_BROADCAST_RGB_LIMITED:
  4931.                         intel_dp->color_range_auto = false;
  4932.                         intel_dp->limited_color_range = true;
  4933.                         break;
  4934.                 default:
  4935.                         return -EINVAL;
  4936.                 }
  4937.  
  4938.                 if (old_auto == intel_dp->color_range_auto &&
  4939.                     old_range == intel_dp->limited_color_range)
  4940.                         return 0;
  4941.  
  4942.                 goto done;
  4943.         }
  4944.  
  4945.         if (is_edp(intel_dp) &&
  4946.             property == connector->dev->mode_config.scaling_mode_property) {
  4947.                 if (val == DRM_MODE_SCALE_NONE) {
  4948.                         DRM_DEBUG_KMS("no scaling not supported\n");
  4949.                         return -EINVAL;
  4950.                 }
  4951.  
  4952.                 if (intel_connector->panel.fitting_mode == val) {
  4953.                         /* the eDP scaling property is not changed */
  4954.                         return 0;
  4955.                 }
  4956.                 intel_connector->panel.fitting_mode = val;
  4957.  
  4958.                 goto done;
  4959.         }
  4960.  
  4961.         return -EINVAL;
  4962.  
  4963. done:
  4964.         if (intel_encoder->base.crtc)
  4965.                 intel_crtc_restore_mode(intel_encoder->base.crtc);
  4966.  
  4967.         return 0;
  4968. }
  4969.  
  4970. static void
  4971. intel_dp_connector_destroy(struct drm_connector *connector)
  4972. {
  4973.         struct intel_connector *intel_connector = to_intel_connector(connector);
  4974.  
  4975.         kfree(intel_connector->detect_edid);
  4976.  
  4977.         if (!IS_ERR_OR_NULL(intel_connector->edid))
  4978.                 kfree(intel_connector->edid);
  4979.  
  4980.         /* Can't call is_edp() since the encoder may have been destroyed
  4981.          * already. */
  4982.         if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  4983.                 intel_panel_fini(&intel_connector->panel);
  4984.  
  4985.         drm_connector_cleanup(connector);
  4986.         kfree(connector);
  4987. }
  4988.  
  4989. void intel_dp_encoder_destroy(struct drm_encoder *encoder)
  4990. {
  4991.         struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
  4992.         struct intel_dp *intel_dp = &intel_dig_port->dp;
  4993.  
  4994.         drm_dp_aux_unregister(&intel_dp->aux);
  4995.         intel_dp_mst_encoder_cleanup(intel_dig_port);
  4996.         if (is_edp(intel_dp)) {
  4997.                 cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  4998.                 /*
  4999.                  * vdd might still be enabled do to the delayed vdd off.
  5000.                  * Make sure vdd is actually turned off here.
  5001.                  */
  5002.                 pps_lock(intel_dp);
  5003.                 edp_panel_vdd_off_sync(intel_dp);
  5004.                 pps_unlock(intel_dp);
  5005.  
  5006.         }
  5007.         drm_encoder_cleanup(encoder);
  5008.         kfree(intel_dig_port);
  5009. }
  5010.  
  5011. void intel_dp_encoder_suspend(struct intel_encoder *intel_encoder)
  5012. {
  5013.         struct intel_dp *intel_dp = enc_to_intel_dp(&intel_encoder->base);
  5014.  
  5015.         if (!is_edp(intel_dp))
  5016.                 return;
  5017.  
  5018.         /*
  5019.          * vdd might still be enabled do to the delayed vdd off.
  5020.          * Make sure vdd is actually turned off here.
  5021.          */
  5022.         cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  5023.         pps_lock(intel_dp);
  5024.         edp_panel_vdd_off_sync(intel_dp);
  5025.         pps_unlock(intel_dp);
  5026. }
  5027.  
  5028. static void intel_edp_panel_vdd_sanitize(struct intel_dp *intel_dp)
  5029. {
  5030.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  5031.         struct drm_device *dev = intel_dig_port->base.base.dev;
  5032.         struct drm_i915_private *dev_priv = dev->dev_private;
  5033.         enum intel_display_power_domain power_domain;
  5034.  
  5035.         lockdep_assert_held(&dev_priv->pps_mutex);
  5036.  
  5037.         if (!edp_have_panel_vdd(intel_dp))
  5038.                 return;
  5039.  
  5040.         /*
  5041.          * The VDD bit needs a power domain reference, so if the bit is
  5042.          * already enabled when we boot or resume, grab this reference and
  5043.          * schedule a vdd off, so we don't hold on to the reference
  5044.          * indefinitely.
  5045.          */
  5046.         DRM_DEBUG_KMS("VDD left on by BIOS, adjusting state tracking\n");
  5047.         power_domain = intel_display_port_aux_power_domain(&intel_dig_port->base);
  5048.         intel_display_power_get(dev_priv, power_domain);
  5049.  
  5050.         edp_panel_vdd_schedule_off(intel_dp);
  5051. }
  5052.  
  5053. void intel_dp_encoder_reset(struct drm_encoder *encoder)
  5054. {
  5055.         struct drm_i915_private *dev_priv = to_i915(encoder->dev);
  5056.         struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
  5057.  
  5058.         if (!HAS_DDI(dev_priv))
  5059.                 intel_dp->DP = I915_READ(intel_dp->output_reg);
  5060.  
  5061.         if (to_intel_encoder(encoder)->type != INTEL_OUTPUT_EDP)
  5062.                 return;
  5063.  
  5064.         pps_lock(intel_dp);
  5065.  
  5066.         /*
  5067.          * Read out the current power sequencer assignment,
  5068.          * in case the BIOS did something with it.
  5069.          */
  5070.         if (IS_VALLEYVIEW(encoder->dev))
  5071.                 vlv_initial_power_sequencer_setup(intel_dp);
  5072.  
  5073.         intel_edp_panel_vdd_sanitize(intel_dp);
  5074.  
  5075.         pps_unlock(intel_dp);
  5076. }
  5077.  
  5078. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  5079.         .dpms = drm_atomic_helper_connector_dpms,
  5080.         .detect = intel_dp_detect,
  5081.         .force = intel_dp_force,
  5082.         .fill_modes = drm_helper_probe_single_connector_modes,
  5083.         .set_property = intel_dp_set_property,
  5084.         .atomic_get_property = intel_connector_atomic_get_property,
  5085.         .destroy = intel_dp_connector_destroy,
  5086.         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  5087.         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  5088. };
  5089.  
  5090. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  5091.         .get_modes = intel_dp_get_modes,
  5092.         .mode_valid = intel_dp_mode_valid,
  5093.         .best_encoder = intel_best_encoder,
  5094. };
  5095.  
  5096. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  5097.         .reset = intel_dp_encoder_reset,
  5098.         .destroy = intel_dp_encoder_destroy,
  5099. };
  5100.  
  5101. enum irqreturn
  5102. intel_dp_hpd_pulse(struct intel_digital_port *intel_dig_port, bool long_hpd)
  5103. {
  5104.         struct intel_dp *intel_dp = &intel_dig_port->dp;
  5105.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  5106.         struct drm_device *dev = intel_dig_port->base.base.dev;
  5107.         struct drm_i915_private *dev_priv = dev->dev_private;
  5108.         enum intel_display_power_domain power_domain;
  5109.         enum irqreturn ret = IRQ_NONE;
  5110.  
  5111.         if (intel_dig_port->base.type != INTEL_OUTPUT_EDP &&
  5112.             intel_dig_port->base.type != INTEL_OUTPUT_HDMI)
  5113.                 intel_dig_port->base.type = INTEL_OUTPUT_DISPLAYPORT;
  5114.  
  5115.         if (long_hpd && intel_dig_port->base.type == INTEL_OUTPUT_EDP) {
  5116.                 /*
  5117.                  * vdd off can generate a long pulse on eDP which
  5118.                  * would require vdd on to handle it, and thus we
  5119.                  * would end up in an endless cycle of
  5120.                  * "vdd off -> long hpd -> vdd on -> detect -> vdd off -> ..."
  5121.                  */
  5122.                 DRM_DEBUG_KMS("ignoring long hpd on eDP port %c\n",
  5123.                               port_name(intel_dig_port->port));
  5124.                 return IRQ_HANDLED;
  5125.         }
  5126.  
  5127.         DRM_DEBUG_KMS("got hpd irq on port %c - %s\n",
  5128.                       port_name(intel_dig_port->port),
  5129.                       long_hpd ? "long" : "short");
  5130.  
  5131.         power_domain = intel_display_port_aux_power_domain(intel_encoder);
  5132.         intel_display_power_get(dev_priv, power_domain);
  5133.  
  5134.         if (long_hpd) {
  5135.                 if (!intel_digital_port_connected(dev_priv, intel_dig_port))
  5136.                         goto mst_fail;
  5137.  
  5138.                 if (!intel_dp_get_dpcd(intel_dp)) {
  5139.                         goto mst_fail;
  5140.                 }
  5141.  
  5142.                 intel_dp_probe_oui(intel_dp);
  5143.  
  5144.                 if (!intel_dp_probe_mst(intel_dp)) {
  5145.                         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  5146.                         intel_dp_check_link_status(intel_dp);
  5147.                         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  5148.                         goto mst_fail;
  5149.                 }
  5150.         } else {
  5151.                 if (intel_dp->is_mst) {
  5152.                         if (intel_dp_check_mst_status(intel_dp) == -EINVAL)
  5153.                                 goto mst_fail;
  5154.                 }
  5155.  
  5156.                 if (!intel_dp->is_mst) {
  5157.                         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  5158.                         intel_dp_check_link_status(intel_dp);
  5159.                         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  5160.                 }
  5161.         }
  5162.  
  5163.         ret = IRQ_HANDLED;
  5164.  
  5165.         goto put_power;
  5166. mst_fail:
  5167.         /* if we were in MST mode, and device is not there get out of MST mode */
  5168.         if (intel_dp->is_mst) {
  5169.                 DRM_DEBUG_KMS("MST device may have disappeared %d vs %d\n", intel_dp->is_mst, intel_dp->mst_mgr.mst_state);
  5170.                 intel_dp->is_mst = false;
  5171.                 drm_dp_mst_topology_mgr_set_mst(&intel_dp->mst_mgr, intel_dp->is_mst);
  5172.         }
  5173. put_power:
  5174.         intel_display_power_put(dev_priv, power_domain);
  5175.  
  5176.         return ret;
  5177. }
  5178.  
  5179. /* Return which DP Port should be selected for Transcoder DP control */
  5180. int
  5181. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  5182. {
  5183.         struct drm_device *dev = crtc->dev;
  5184.         struct intel_encoder *intel_encoder;
  5185.         struct intel_dp *intel_dp;
  5186.  
  5187.         for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5188.                 intel_dp = enc_to_intel_dp(&intel_encoder->base);
  5189.  
  5190.                 if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  5191.                     intel_encoder->type == INTEL_OUTPUT_EDP)
  5192.                         return intel_dp->output_reg;
  5193.         }
  5194.  
  5195.         return -1;
  5196. }
  5197.  
  5198. /* check the VBT to see whether the eDP is on another port */
  5199. bool intel_dp_is_edp(struct drm_device *dev, enum port port)
  5200. {
  5201.         struct drm_i915_private *dev_priv = dev->dev_private;
  5202.         union child_device_config *p_child;
  5203.         int i;
  5204.         static const short port_mapping[] = {
  5205.                 [PORT_B] = DVO_PORT_DPB,
  5206.                 [PORT_C] = DVO_PORT_DPC,
  5207.                 [PORT_D] = DVO_PORT_DPD,
  5208.                 [PORT_E] = DVO_PORT_DPE,
  5209.         };
  5210.  
  5211.         /*
  5212.          * eDP not supported on g4x. so bail out early just
  5213.          * for a bit extra safety in case the VBT is bonkers.
  5214.          */
  5215.         if (INTEL_INFO(dev)->gen < 5)
  5216.                 return false;
  5217.  
  5218.         if (port == PORT_A)
  5219.                 return true;
  5220.  
  5221.         if (!dev_priv->vbt.child_dev_num)
  5222.                 return false;
  5223.  
  5224.         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
  5225.                 p_child = dev_priv->vbt.child_dev + i;
  5226.  
  5227.                 if (p_child->common.dvo_port == port_mapping[port] &&
  5228.                     (p_child->common.device_type & DEVICE_TYPE_eDP_BITS) ==
  5229.                     (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
  5230.                         return true;
  5231.         }
  5232.         return false;
  5233. }
  5234.  
  5235. void
  5236. intel_dp_add_properties(struct intel_dp *intel_dp, struct drm_connector *connector)
  5237. {
  5238.         struct intel_connector *intel_connector = to_intel_connector(connector);
  5239.  
  5240.         intel_attach_force_audio_property(connector);
  5241.         intel_attach_broadcast_rgb_property(connector);
  5242.         intel_dp->color_range_auto = true;
  5243.  
  5244.         if (is_edp(intel_dp)) {
  5245.                 drm_mode_create_scaling_mode_property(connector->dev);
  5246.                 drm_object_attach_property(
  5247.                         &connector->base,
  5248.                         connector->dev->mode_config.scaling_mode_property,
  5249.                         DRM_MODE_SCALE_ASPECT);
  5250.                 intel_connector->panel.fitting_mode = DRM_MODE_SCALE_ASPECT;
  5251.         }
  5252. }
  5253.  
  5254. static void intel_dp_init_panel_power_timestamps(struct intel_dp *intel_dp)
  5255. {
  5256.         intel_dp->last_power_cycle = jiffies;
  5257.         intel_dp->last_power_on = jiffies;
  5258.         intel_dp->last_backlight_off = jiffies;
  5259. }
  5260.  
  5261. static void
  5262. intel_dp_init_panel_power_sequencer(struct drm_device *dev,
  5263.                                     struct intel_dp *intel_dp)
  5264. {
  5265.         struct drm_i915_private *dev_priv = dev->dev_private;
  5266.         struct edp_power_seq cur, vbt, spec,
  5267.                 *final = &intel_dp->pps_delays;
  5268.         u32 pp_on, pp_off, pp_div = 0, pp_ctl = 0;
  5269.         int pp_ctrl_reg, pp_on_reg, pp_off_reg, pp_div_reg = 0;
  5270.  
  5271.         lockdep_assert_held(&dev_priv->pps_mutex);
  5272.  
  5273.         /* already initialized? */
  5274.         if (final->t11_t12 != 0)
  5275.                 return;
  5276.  
  5277.         if (IS_BROXTON(dev)) {
  5278.                 /*
  5279.                  * TODO: BXT has 2 sets of PPS registers.
  5280.                  * Correct Register for Broxton need to be identified
  5281.                  * using VBT. hardcoding for now
  5282.                  */
  5283.                 pp_ctrl_reg = BXT_PP_CONTROL(0);
  5284.                 pp_on_reg = BXT_PP_ON_DELAYS(0);
  5285.                 pp_off_reg = BXT_PP_OFF_DELAYS(0);
  5286.         } else if (HAS_PCH_SPLIT(dev)) {
  5287.                 pp_ctrl_reg = PCH_PP_CONTROL;
  5288.                 pp_on_reg = PCH_PP_ON_DELAYS;
  5289.                 pp_off_reg = PCH_PP_OFF_DELAYS;
  5290.                 pp_div_reg = PCH_PP_DIVISOR;
  5291.         } else {
  5292.                 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  5293.  
  5294.                 pp_ctrl_reg = VLV_PIPE_PP_CONTROL(pipe);
  5295.                 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  5296.                 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
  5297.                 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
  5298.         }
  5299.  
  5300.         /* Workaround: Need to write PP_CONTROL with the unlock key as
  5301.          * the very first thing. */
  5302.         pp_ctl = ironlake_get_pp_control(intel_dp);
  5303.  
  5304.         pp_on = I915_READ(pp_on_reg);
  5305.         pp_off = I915_READ(pp_off_reg);
  5306.         if (!IS_BROXTON(dev)) {
  5307.                 I915_WRITE(pp_ctrl_reg, pp_ctl);
  5308.                 pp_div = I915_READ(pp_div_reg);
  5309.         }
  5310.  
  5311.         /* Pull timing values out of registers */
  5312.         cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
  5313.                 PANEL_POWER_UP_DELAY_SHIFT;
  5314.  
  5315.         cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
  5316.                 PANEL_LIGHT_ON_DELAY_SHIFT;
  5317.  
  5318.         cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
  5319.                 PANEL_LIGHT_OFF_DELAY_SHIFT;
  5320.  
  5321.         cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
  5322.                 PANEL_POWER_DOWN_DELAY_SHIFT;
  5323.  
  5324.         if (IS_BROXTON(dev)) {
  5325.                 u16 tmp = (pp_ctl & BXT_POWER_CYCLE_DELAY_MASK) >>
  5326.                         BXT_POWER_CYCLE_DELAY_SHIFT;
  5327.                 if (tmp > 0)
  5328.                         cur.t11_t12 = (tmp - 1) * 1000;
  5329.                 else
  5330.                         cur.t11_t12 = 0;
  5331.         } else {
  5332.                 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
  5333.                        PANEL_POWER_CYCLE_DELAY_SHIFT) * 1000;
  5334.         }
  5335.  
  5336.         DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  5337.                       cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
  5338.  
  5339.         vbt = dev_priv->vbt.edp_pps;
  5340.  
  5341.         /* Upper limits from eDP 1.3 spec. Note that we use the clunky units of
  5342.          * our hw here, which are all in 100usec. */
  5343.         spec.t1_t3 = 210 * 10;
  5344.         spec.t8 = 50 * 10; /* no limit for t8, use t7 instead */
  5345.         spec.t9 = 50 * 10; /* no limit for t9, make it symmetric with t8 */
  5346.         spec.t10 = 500 * 10;
  5347.         /* This one is special and actually in units of 100ms, but zero
  5348.          * based in the hw (so we need to add 100 ms). But the sw vbt
  5349.          * table multiplies it with 1000 to make it in units of 100usec,
  5350.          * too. */
  5351.         spec.t11_t12 = (510 + 100) * 10;
  5352.  
  5353.         DRM_DEBUG_KMS("vbt t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
  5354.                       vbt.t1_t3, vbt.t8, vbt.t9, vbt.t10, vbt.t11_t12);
  5355.  
  5356.         /* Use the max of the register settings and vbt. If both are
  5357.          * unset, fall back to the spec limits. */
  5358. #define assign_final(field)     final->field = (max(cur.field, vbt.field) == 0 ? \
  5359.                                        spec.field : \
  5360.                                        max(cur.field, vbt.field))
  5361.         assign_final(t1_t3);
  5362.         assign_final(t8);
  5363.         assign_final(t9);
  5364.         assign_final(t10);
  5365.         assign_final(t11_t12);
  5366. #undef assign_final
  5367.  
  5368. #define get_delay(field)        (DIV_ROUND_UP(final->field, 10))
  5369.         intel_dp->panel_power_up_delay = get_delay(t1_t3);
  5370.         intel_dp->backlight_on_delay = get_delay(t8);
  5371.         intel_dp->backlight_off_delay = get_delay(t9);
  5372.         intel_dp->panel_power_down_delay = get_delay(t10);
  5373.         intel_dp->panel_power_cycle_delay = get_delay(t11_t12);
  5374. #undef get_delay
  5375.  
  5376.         DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
  5377.                       intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
  5378.                       intel_dp->panel_power_cycle_delay);
  5379.  
  5380.         DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
  5381.                       intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
  5382. }
  5383.  
  5384. static void
  5385. intel_dp_init_panel_power_sequencer_registers(struct drm_device *dev,
  5386.                                               struct intel_dp *intel_dp)
  5387. {
  5388.         struct drm_i915_private *dev_priv = dev->dev_private;
  5389.         u32 pp_on, pp_off, pp_div, port_sel = 0;
  5390.         int div = HAS_PCH_SPLIT(dev) ? intel_pch_rawclk(dev) : intel_hrawclk(dev);
  5391.         int pp_on_reg, pp_off_reg, pp_div_reg = 0, pp_ctrl_reg;
  5392.         enum port port = dp_to_dig_port(intel_dp)->port;
  5393.         const struct edp_power_seq *seq = &intel_dp->pps_delays;
  5394.  
  5395.         lockdep_assert_held(&dev_priv->pps_mutex);
  5396.  
  5397.         if (IS_BROXTON(dev)) {
  5398.                 /*
  5399.                  * TODO: BXT has 2 sets of PPS registers.
  5400.                  * Correct Register for Broxton need to be identified
  5401.                  * using VBT. hardcoding for now
  5402.                  */
  5403.                 pp_ctrl_reg = BXT_PP_CONTROL(0);
  5404.                 pp_on_reg = BXT_PP_ON_DELAYS(0);
  5405.                 pp_off_reg = BXT_PP_OFF_DELAYS(0);
  5406.  
  5407.         } else if (HAS_PCH_SPLIT(dev)) {
  5408.                 pp_on_reg = PCH_PP_ON_DELAYS;
  5409.                 pp_off_reg = PCH_PP_OFF_DELAYS;
  5410.                 pp_div_reg = PCH_PP_DIVISOR;
  5411.         } else {
  5412.                 enum pipe pipe = vlv_power_sequencer_pipe(intel_dp);
  5413.  
  5414.                 pp_on_reg = VLV_PIPE_PP_ON_DELAYS(pipe);
  5415.                 pp_off_reg = VLV_PIPE_PP_OFF_DELAYS(pipe);
  5416.                 pp_div_reg = VLV_PIPE_PP_DIVISOR(pipe);
  5417.         }
  5418.  
  5419.         /*
  5420.          * And finally store the new values in the power sequencer. The
  5421.          * backlight delays are set to 1 because we do manual waits on them. For
  5422.          * T8, even BSpec recommends doing it. For T9, if we don't do this,
  5423.          * we'll end up waiting for the backlight off delay twice: once when we
  5424.          * do the manual sleep, and once when we disable the panel and wait for
  5425.          * the PP_STATUS bit to become zero.
  5426.          */
  5427.         pp_on = (seq->t1_t3 << PANEL_POWER_UP_DELAY_SHIFT) |
  5428.                 (1 << PANEL_LIGHT_ON_DELAY_SHIFT);
  5429.         pp_off = (1 << PANEL_LIGHT_OFF_DELAY_SHIFT) |
  5430.                  (seq->t10 << PANEL_POWER_DOWN_DELAY_SHIFT);
  5431.         /* Compute the divisor for the pp clock, simply match the Bspec
  5432.          * formula. */
  5433.         if (IS_BROXTON(dev)) {
  5434.                 pp_div = I915_READ(pp_ctrl_reg);
  5435.                 pp_div &= ~BXT_POWER_CYCLE_DELAY_MASK;
  5436.                 pp_div |= (DIV_ROUND_UP((seq->t11_t12 + 1), 1000)
  5437.                                 << BXT_POWER_CYCLE_DELAY_SHIFT);
  5438.         } else {
  5439.                 pp_div = ((100 * div)/2 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
  5440.                 pp_div |= (DIV_ROUND_UP(seq->t11_t12, 1000)
  5441.                                 << PANEL_POWER_CYCLE_DELAY_SHIFT);
  5442.         }
  5443.  
  5444.         /* Haswell doesn't have any port selection bits for the panel
  5445.          * power sequencer any more. */
  5446.         if (IS_VALLEYVIEW(dev)) {
  5447.                 port_sel = PANEL_PORT_SELECT_VLV(port);
  5448.         } else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  5449.                 if (port == PORT_A)
  5450.                         port_sel = PANEL_PORT_SELECT_DPA;
  5451.                 else
  5452.                         port_sel = PANEL_PORT_SELECT_DPD;
  5453.         }
  5454.  
  5455.         pp_on |= port_sel;
  5456.  
  5457.         I915_WRITE(pp_on_reg, pp_on);
  5458.         I915_WRITE(pp_off_reg, pp_off);
  5459.         if (IS_BROXTON(dev))
  5460.                 I915_WRITE(pp_ctrl_reg, pp_div);
  5461.         else
  5462.                 I915_WRITE(pp_div_reg, pp_div);
  5463.  
  5464.         DRM_DEBUG_KMS("panel power sequencer register settings: PP_ON %#x, PP_OFF %#x, PP_DIV %#x\n",
  5465.                       I915_READ(pp_on_reg),
  5466.                       I915_READ(pp_off_reg),
  5467.                       IS_BROXTON(dev) ?
  5468.                       (I915_READ(pp_ctrl_reg) & BXT_POWER_CYCLE_DELAY_MASK) :
  5469.                       I915_READ(pp_div_reg));
  5470. }
  5471.  
  5472. /**
  5473.  * intel_dp_set_drrs_state - program registers for RR switch to take effect
  5474.  * @dev: DRM device
  5475.  * @refresh_rate: RR to be programmed
  5476.  *
  5477.  * This function gets called when refresh rate (RR) has to be changed from
  5478.  * one frequency to another. Switches can be between high and low RR
  5479.  * supported by the panel or to any other RR based on media playback (in
  5480.  * this case, RR value needs to be passed from user space).
  5481.  *
  5482.  * The caller of this function needs to take a lock on dev_priv->drrs.
  5483.  */
  5484. static void intel_dp_set_drrs_state(struct drm_device *dev, int refresh_rate)
  5485. {
  5486.         struct drm_i915_private *dev_priv = dev->dev_private;
  5487.         struct intel_encoder *encoder;
  5488.         struct intel_digital_port *dig_port = NULL;
  5489.         struct intel_dp *intel_dp = dev_priv->drrs.dp;
  5490.         struct intel_crtc_state *config = NULL;
  5491.         struct intel_crtc *intel_crtc = NULL;
  5492.         enum drrs_refresh_rate_type index = DRRS_HIGH_RR;
  5493.  
  5494.         if (refresh_rate <= 0) {
  5495.                 DRM_DEBUG_KMS("Refresh rate should be positive non-zero.\n");
  5496.                 return;
  5497.         }
  5498.  
  5499.         if (intel_dp == NULL) {
  5500.                 DRM_DEBUG_KMS("DRRS not supported.\n");
  5501.                 return;
  5502.         }
  5503.  
  5504.         /*
  5505.          * FIXME: This needs proper synchronization with psr state for some
  5506.          * platforms that cannot have PSR and DRRS enabled at the same time.
  5507.          */
  5508.  
  5509.         dig_port = dp_to_dig_port(intel_dp);
  5510.         encoder = &dig_port->base;
  5511.         intel_crtc = to_intel_crtc(encoder->base.crtc);
  5512.  
  5513.         if (!intel_crtc) {
  5514.                 DRM_DEBUG_KMS("DRRS: intel_crtc not initialized\n");
  5515.                 return;
  5516.         }
  5517.  
  5518.         config = intel_crtc->config;
  5519.  
  5520.         if (dev_priv->drrs.type < SEAMLESS_DRRS_SUPPORT) {
  5521.                 DRM_DEBUG_KMS("Only Seamless DRRS supported.\n");
  5522.                 return;
  5523.         }
  5524.  
  5525.         if (intel_dp->attached_connector->panel.downclock_mode->vrefresh ==
  5526.                         refresh_rate)
  5527.                 index = DRRS_LOW_RR;
  5528.  
  5529.         if (index == dev_priv->drrs.refresh_rate_type) {
  5530.                 DRM_DEBUG_KMS(
  5531.                         "DRRS requested for previously set RR...ignoring\n");
  5532.                 return;
  5533.         }
  5534.  
  5535.         if (!intel_crtc->active) {
  5536.                 DRM_DEBUG_KMS("eDP encoder disabled. CRTC not Active\n");
  5537.                 return;
  5538.         }
  5539.  
  5540.         if (INTEL_INFO(dev)->gen >= 8 && !IS_CHERRYVIEW(dev)) {
  5541.                 switch (index) {
  5542.                 case DRRS_HIGH_RR:
  5543.                         intel_dp_set_m_n(intel_crtc, M1_N1);
  5544.                         break;
  5545.                 case DRRS_LOW_RR:
  5546.                         intel_dp_set_m_n(intel_crtc, M2_N2);
  5547.                         break;
  5548.                 case DRRS_MAX_RR:
  5549.                 default:
  5550.                         DRM_ERROR("Unsupported refreshrate type\n");
  5551.                 }
  5552.         } else if (INTEL_INFO(dev)->gen > 6) {
  5553.                 u32 reg = PIPECONF(intel_crtc->config->cpu_transcoder);
  5554.                 u32 val;
  5555.  
  5556.                 val = I915_READ(reg);
  5557.                 if (index > DRRS_HIGH_RR) {
  5558.                         if (IS_VALLEYVIEW(dev))
  5559.                                 val |= PIPECONF_EDP_RR_MODE_SWITCH_VLV;
  5560.                         else
  5561.                                 val |= PIPECONF_EDP_RR_MODE_SWITCH;
  5562.                 } else {
  5563.                         if (IS_VALLEYVIEW(dev))
  5564.                                 val &= ~PIPECONF_EDP_RR_MODE_SWITCH_VLV;
  5565.                         else
  5566.                                 val &= ~PIPECONF_EDP_RR_MODE_SWITCH;
  5567.                 }
  5568.                 I915_WRITE(reg, val);
  5569.         }
  5570.  
  5571.         dev_priv->drrs.refresh_rate_type = index;
  5572.  
  5573.         DRM_DEBUG_KMS("eDP Refresh Rate set to : %dHz\n", refresh_rate);
  5574. }
  5575.  
  5576. /**
  5577.  * intel_edp_drrs_enable - init drrs struct if supported
  5578.  * @intel_dp: DP struct
  5579.  *
  5580.  * Initializes frontbuffer_bits and drrs.dp
  5581.  */
  5582. void intel_edp_drrs_enable(struct intel_dp *intel_dp)
  5583. {
  5584.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  5585.         struct drm_i915_private *dev_priv = dev->dev_private;
  5586.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  5587.         struct drm_crtc *crtc = dig_port->base.base.crtc;
  5588.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5589.  
  5590.         if (!intel_crtc->config->has_drrs) {
  5591.                 DRM_DEBUG_KMS("Panel doesn't support DRRS\n");
  5592.                 return;
  5593.         }
  5594.  
  5595.         mutex_lock(&dev_priv->drrs.mutex);
  5596.         if (WARN_ON(dev_priv->drrs.dp)) {
  5597.                 DRM_ERROR("DRRS already enabled\n");
  5598.                 goto unlock;
  5599.         }
  5600.  
  5601.         dev_priv->drrs.busy_frontbuffer_bits = 0;
  5602.  
  5603.         dev_priv->drrs.dp = intel_dp;
  5604.  
  5605. unlock:
  5606.         mutex_unlock(&dev_priv->drrs.mutex);
  5607. }
  5608.  
  5609. /**
  5610.  * intel_edp_drrs_disable - Disable DRRS
  5611.  * @intel_dp: DP struct
  5612.  *
  5613.  */
  5614. void intel_edp_drrs_disable(struct intel_dp *intel_dp)
  5615. {
  5616.         struct drm_device *dev = intel_dp_to_dev(intel_dp);
  5617.         struct drm_i915_private *dev_priv = dev->dev_private;
  5618.         struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
  5619.         struct drm_crtc *crtc = dig_port->base.base.crtc;
  5620.         struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5621.  
  5622.         if (!intel_crtc->config->has_drrs)
  5623.                 return;
  5624.  
  5625.         mutex_lock(&dev_priv->drrs.mutex);
  5626.         if (!dev_priv->drrs.dp) {
  5627.                 mutex_unlock(&dev_priv->drrs.mutex);
  5628.                 return;
  5629.         }
  5630.  
  5631.         if (dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  5632.                 intel_dp_set_drrs_state(dev_priv->dev,
  5633.                         intel_dp->attached_connector->panel.
  5634.                         fixed_mode->vrefresh);
  5635.  
  5636.         dev_priv->drrs.dp = NULL;
  5637.         mutex_unlock(&dev_priv->drrs.mutex);
  5638.  
  5639.         cancel_delayed_work_sync(&dev_priv->drrs.work);
  5640. }
  5641.  
  5642. static void intel_edp_drrs_downclock_work(struct work_struct *work)
  5643. {
  5644.         struct drm_i915_private *dev_priv =
  5645.                 container_of(work, typeof(*dev_priv), drrs.work.work);
  5646.         struct intel_dp *intel_dp;
  5647.  
  5648.         mutex_lock(&dev_priv->drrs.mutex);
  5649.  
  5650.         intel_dp = dev_priv->drrs.dp;
  5651.  
  5652.         if (!intel_dp)
  5653.                 goto unlock;
  5654.  
  5655.         /*
  5656.          * The delayed work can race with an invalidate hence we need to
  5657.          * recheck.
  5658.          */
  5659.  
  5660.         if (dev_priv->drrs.busy_frontbuffer_bits)
  5661.                 goto unlock;
  5662.  
  5663.         if (dev_priv->drrs.refresh_rate_type != DRRS_LOW_RR)
  5664.                 intel_dp_set_drrs_state(dev_priv->dev,
  5665.                         intel_dp->attached_connector->panel.
  5666.                         downclock_mode->vrefresh);
  5667.  
  5668. unlock:
  5669.         mutex_unlock(&dev_priv->drrs.mutex);
  5670. }
  5671.  
  5672. /**
  5673.  * intel_edp_drrs_invalidate - Disable Idleness DRRS
  5674.  * @dev: DRM device
  5675.  * @frontbuffer_bits: frontbuffer plane tracking bits
  5676.  *
  5677.  * This function gets called everytime rendering on the given planes start.
  5678.  * Hence DRRS needs to be Upclocked, i.e. (LOW_RR -> HIGH_RR).
  5679.  *
  5680.  * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
  5681.  */
  5682. void intel_edp_drrs_invalidate(struct drm_device *dev,
  5683.                 unsigned frontbuffer_bits)
  5684. {
  5685.         struct drm_i915_private *dev_priv = dev->dev_private;
  5686.         struct drm_crtc *crtc;
  5687.         enum pipe pipe;
  5688.  
  5689.         if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
  5690.                 return;
  5691.  
  5692.         cancel_delayed_work(&dev_priv->drrs.work);
  5693.  
  5694.         mutex_lock(&dev_priv->drrs.mutex);
  5695.         if (!dev_priv->drrs.dp) {
  5696.                 mutex_unlock(&dev_priv->drrs.mutex);
  5697.                 return;
  5698.         }
  5699.  
  5700.         crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
  5701.         pipe = to_intel_crtc(crtc)->pipe;
  5702.  
  5703.         frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
  5704.         dev_priv->drrs.busy_frontbuffer_bits |= frontbuffer_bits;
  5705.  
  5706.         /* invalidate means busy screen hence upclock */
  5707.         if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  5708.                 intel_dp_set_drrs_state(dev_priv->dev,
  5709.                                 dev_priv->drrs.dp->attached_connector->panel.
  5710.                                 fixed_mode->vrefresh);
  5711.  
  5712.         mutex_unlock(&dev_priv->drrs.mutex);
  5713. }
  5714.  
  5715. /**
  5716.  * intel_edp_drrs_flush - Restart Idleness DRRS
  5717.  * @dev: DRM device
  5718.  * @frontbuffer_bits: frontbuffer plane tracking bits
  5719.  *
  5720.  * This function gets called every time rendering on the given planes has
  5721.  * completed or flip on a crtc is completed. So DRRS should be upclocked
  5722.  * (LOW_RR -> HIGH_RR). And also Idleness detection should be started again,
  5723.  * if no other planes are dirty.
  5724.  *
  5725.  * Dirty frontbuffers relevant to DRRS are tracked in busy_frontbuffer_bits.
  5726.  */
  5727. void intel_edp_drrs_flush(struct drm_device *dev,
  5728.                 unsigned frontbuffer_bits)
  5729. {
  5730.         struct drm_i915_private *dev_priv = dev->dev_private;
  5731.         struct drm_crtc *crtc;
  5732.         enum pipe pipe;
  5733.  
  5734.         if (dev_priv->drrs.type == DRRS_NOT_SUPPORTED)
  5735.                 return;
  5736.  
  5737.         cancel_delayed_work(&dev_priv->drrs.work);
  5738.  
  5739.         mutex_lock(&dev_priv->drrs.mutex);
  5740.         if (!dev_priv->drrs.dp) {
  5741.                 mutex_unlock(&dev_priv->drrs.mutex);
  5742.                 return;
  5743.         }
  5744.  
  5745.         crtc = dp_to_dig_port(dev_priv->drrs.dp)->base.base.crtc;
  5746.         pipe = to_intel_crtc(crtc)->pipe;
  5747.  
  5748.         frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
  5749.         dev_priv->drrs.busy_frontbuffer_bits &= ~frontbuffer_bits;
  5750.  
  5751.         /* flush means busy screen hence upclock */
  5752.         if (frontbuffer_bits && dev_priv->drrs.refresh_rate_type == DRRS_LOW_RR)
  5753.                 intel_dp_set_drrs_state(dev_priv->dev,
  5754.                                 dev_priv->drrs.dp->attached_connector->panel.
  5755.                                 fixed_mode->vrefresh);
  5756.  
  5757.         /*
  5758.          * flush also means no more activity hence schedule downclock, if all
  5759.          * other fbs are quiescent too
  5760.          */
  5761.         if (!dev_priv->drrs.busy_frontbuffer_bits)
  5762.                 schedule_delayed_work(&dev_priv->drrs.work,
  5763.                                 msecs_to_jiffies(1000));
  5764.         mutex_unlock(&dev_priv->drrs.mutex);
  5765. }
  5766.  
  5767. /**
  5768.  * DOC: Display Refresh Rate Switching (DRRS)
  5769.  *
  5770.  * Display Refresh Rate Switching (DRRS) is a power conservation feature
  5771.  * which enables swtching between low and high refresh rates,
  5772.  * dynamically, based on the usage scenario. This feature is applicable
  5773.  * for internal panels.
  5774.  *
  5775.  * Indication that the panel supports DRRS is given by the panel EDID, which
  5776.  * would list multiple refresh rates for one resolution.
  5777.  *
  5778.  * DRRS is of 2 types - static and seamless.
  5779.  * Static DRRS involves changing refresh rate (RR) by doing a full modeset
  5780.  * (may appear as a blink on screen) and is used in dock-undock scenario.
  5781.  * Seamless DRRS involves changing RR without any visual effect to the user
  5782.  * and can be used during normal system usage. This is done by programming
  5783.  * certain registers.
  5784.  *
  5785.  * Support for static/seamless DRRS may be indicated in the VBT based on
  5786.  * inputs from the panel spec.
  5787.  *
  5788.  * DRRS saves power by switching to low RR based on usage scenarios.
  5789.  *
  5790.  * eDP DRRS:-
  5791.  *        The implementation is based on frontbuffer tracking implementation.
  5792.  * When there is a disturbance on the screen triggered by user activity or a
  5793.  * periodic system activity, DRRS is disabled (RR is changed to high RR).
  5794.  * When there is no movement on screen, after a timeout of 1 second, a switch
  5795.  * to low RR is made.
  5796.  *        For integration with frontbuffer tracking code,
  5797.  * intel_edp_drrs_invalidate() and intel_edp_drrs_flush() are called.
  5798.  *
  5799.  * DRRS can be further extended to support other internal panels and also
  5800.  * the scenario of video playback wherein RR is set based on the rate
  5801.  * requested by userspace.
  5802.  */
  5803.  
  5804. /**
  5805.  * intel_dp_drrs_init - Init basic DRRS work and mutex.
  5806.  * @intel_connector: eDP connector
  5807.  * @fixed_mode: preferred mode of panel
  5808.  *
  5809.  * This function is  called only once at driver load to initialize basic
  5810.  * DRRS stuff.
  5811.  *
  5812.  * Returns:
  5813.  * Downclock mode if panel supports it, else return NULL.
  5814.  * DRRS support is determined by the presence of downclock mode (apart
  5815.  * from VBT setting).
  5816.  */
  5817. static struct drm_display_mode *
  5818. intel_dp_drrs_init(struct intel_connector *intel_connector,
  5819.                 struct drm_display_mode *fixed_mode)
  5820. {
  5821.         struct drm_connector *connector = &intel_connector->base;
  5822.         struct drm_device *dev = connector->dev;
  5823.         struct drm_i915_private *dev_priv = dev->dev_private;
  5824.         struct drm_display_mode *downclock_mode = NULL;
  5825.  
  5826.         INIT_DELAYED_WORK(&dev_priv->drrs.work, intel_edp_drrs_downclock_work);
  5827.         mutex_init(&dev_priv->drrs.mutex);
  5828.  
  5829.         if (INTEL_INFO(dev)->gen <= 6) {
  5830.                 DRM_DEBUG_KMS("DRRS supported for Gen7 and above\n");
  5831.                 return NULL;
  5832.         }
  5833.  
  5834.         if (dev_priv->vbt.drrs_type != SEAMLESS_DRRS_SUPPORT) {
  5835.                 DRM_DEBUG_KMS("VBT doesn't support DRRS\n");
  5836.                 return NULL;
  5837.         }
  5838.  
  5839.         downclock_mode = intel_find_panel_downclock
  5840.                                         (dev, fixed_mode, connector);
  5841.  
  5842.         if (!downclock_mode) {
  5843.                 DRM_DEBUG_KMS("Downclock mode is not found. DRRS not supported\n");
  5844.                 return NULL;
  5845.         }
  5846.  
  5847.         dev_priv->drrs.type = dev_priv->vbt.drrs_type;
  5848.  
  5849.         dev_priv->drrs.refresh_rate_type = DRRS_HIGH_RR;
  5850.         DRM_DEBUG_KMS("seamless DRRS supported for eDP panel.\n");
  5851.         return downclock_mode;
  5852. }
  5853.  
  5854. static bool intel_edp_init_connector(struct intel_dp *intel_dp,
  5855.                                      struct intel_connector *intel_connector)
  5856. {
  5857.         struct drm_connector *connector = &intel_connector->base;
  5858.         struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
  5859.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  5860.         struct drm_device *dev = intel_encoder->base.dev;
  5861.         struct drm_i915_private *dev_priv = dev->dev_private;
  5862.         struct drm_display_mode *fixed_mode = NULL;
  5863.         struct drm_display_mode *downclock_mode = NULL;
  5864.         bool has_dpcd;
  5865.         struct drm_display_mode *scan;
  5866.         struct edid *edid;
  5867.         enum pipe pipe = INVALID_PIPE;
  5868.  
  5869.         if (!is_edp(intel_dp))
  5870.                 return true;
  5871.  
  5872.         pps_lock(intel_dp);
  5873.         intel_edp_panel_vdd_sanitize(intel_dp);
  5874.         pps_unlock(intel_dp);
  5875.  
  5876.         /* Cache DPCD and EDID for edp. */
  5877.         has_dpcd = intel_dp_get_dpcd(intel_dp);
  5878.  
  5879.         if (has_dpcd) {
  5880.                 if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11)
  5881.                         dev_priv->no_aux_handshake =
  5882.                                 intel_dp->dpcd[DP_MAX_DOWNSPREAD] &
  5883.                                 DP_NO_AUX_HANDSHAKE_LINK_TRAINING;
  5884.         } else {
  5885.                 /* if this fails, presume the device is a ghost */
  5886.                 DRM_INFO("failed to retrieve link info, disabling eDP\n");
  5887.                 return false;
  5888.         }
  5889.  
  5890.         /* We now know it's not a ghost, init power sequence regs. */
  5891.         pps_lock(intel_dp);
  5892.         intel_dp_init_panel_power_sequencer_registers(dev, intel_dp);
  5893.         pps_unlock(intel_dp);
  5894.  
  5895.         mutex_lock(&dev->mode_config.mutex);
  5896.         edid = drm_get_edid(connector, &intel_dp->aux.ddc);
  5897.         if (edid) {
  5898.                 if (drm_add_edid_modes(connector, edid)) {
  5899.                         drm_mode_connector_update_edid_property(connector,
  5900.                                                                 edid);
  5901.                         drm_edid_to_eld(connector, edid);
  5902.                 } else {
  5903.                         kfree(edid);
  5904.                         edid = ERR_PTR(-EINVAL);
  5905.                 }
  5906.         } else {
  5907.                 edid = ERR_PTR(-ENOENT);
  5908.         }
  5909.         intel_connector->edid = edid;
  5910.  
  5911.         /* prefer fixed mode from EDID if available */
  5912.         list_for_each_entry(scan, &connector->probed_modes, head) {
  5913.                 if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
  5914.                         fixed_mode = drm_mode_duplicate(dev, scan);
  5915.                         downclock_mode = intel_dp_drrs_init(
  5916.                                                 intel_connector, fixed_mode);
  5917.                         break;
  5918.                 }
  5919.         }
  5920.  
  5921.         /* fallback to VBT if available for eDP */
  5922.         if (!fixed_mode && dev_priv->vbt.lfp_lvds_vbt_mode) {
  5923.                 fixed_mode = drm_mode_duplicate(dev,
  5924.                                         dev_priv->vbt.lfp_lvds_vbt_mode);
  5925.                 if (fixed_mode)
  5926.                         fixed_mode->type |= DRM_MODE_TYPE_PREFERRED;
  5927.         }
  5928.         mutex_unlock(&dev->mode_config.mutex);
  5929.  
  5930.         if (IS_VALLEYVIEW(dev)) {
  5931.  
  5932.                 /*
  5933.                  * Figure out the current pipe for the initial backlight setup.
  5934.                  * If the current pipe isn't valid, try the PPS pipe, and if that
  5935.                  * fails just assume pipe A.
  5936.                  */
  5937.                 if (IS_CHERRYVIEW(dev))
  5938.                         pipe = DP_PORT_TO_PIPE_CHV(intel_dp->DP);
  5939.                 else
  5940.                         pipe = PORT_TO_PIPE(intel_dp->DP);
  5941.  
  5942.                 if (pipe != PIPE_A && pipe != PIPE_B)
  5943.                         pipe = intel_dp->pps_pipe;
  5944.  
  5945.                 if (pipe != PIPE_A && pipe != PIPE_B)
  5946.                         pipe = PIPE_A;
  5947.  
  5948.                 DRM_DEBUG_KMS("using pipe %c for initial backlight setup\n",
  5949.                               pipe_name(pipe));
  5950.         }
  5951.  
  5952.         intel_panel_init(&intel_connector->panel, fixed_mode, downclock_mode);
  5953.         intel_connector->panel.backlight.power = intel_edp_backlight_power;
  5954.         intel_panel_setup_backlight(connector, pipe);
  5955.  
  5956.         return true;
  5957. }
  5958.  
  5959. bool
  5960. intel_dp_init_connector(struct intel_digital_port *intel_dig_port,
  5961.                         struct intel_connector *intel_connector)
  5962. {
  5963.         struct drm_connector *connector = &intel_connector->base;
  5964.         struct intel_dp *intel_dp = &intel_dig_port->dp;
  5965.         struct intel_encoder *intel_encoder = &intel_dig_port->base;
  5966.         struct drm_device *dev = intel_encoder->base.dev;
  5967.         struct drm_i915_private *dev_priv = dev->dev_private;
  5968.         enum port port = intel_dig_port->port;
  5969.         int type;
  5970.  
  5971.         intel_dp->pps_pipe = INVALID_PIPE;
  5972.  
  5973.         /* intel_dp vfuncs */
  5974.         if (INTEL_INFO(dev)->gen >= 9)
  5975.                 intel_dp->get_aux_clock_divider = skl_get_aux_clock_divider;
  5976.         else if (IS_VALLEYVIEW(dev))
  5977.                 intel_dp->get_aux_clock_divider = vlv_get_aux_clock_divider;
  5978.         else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  5979.                 intel_dp->get_aux_clock_divider = hsw_get_aux_clock_divider;
  5980.         else if (HAS_PCH_SPLIT(dev))
  5981.                 intel_dp->get_aux_clock_divider = ilk_get_aux_clock_divider;
  5982.         else
  5983.                 intel_dp->get_aux_clock_divider = i9xx_get_aux_clock_divider;
  5984.  
  5985.         if (INTEL_INFO(dev)->gen >= 9)
  5986.                 intel_dp->get_aux_send_ctl = skl_get_aux_send_ctl;
  5987.         else
  5988.                 intel_dp->get_aux_send_ctl = i9xx_get_aux_send_ctl;
  5989.  
  5990.         /* Preserve the current hw state. */
  5991.         intel_dp->DP = I915_READ(intel_dp->output_reg);
  5992.         intel_dp->attached_connector = intel_connector;
  5993.  
  5994.         if (intel_dp_is_edp(dev, port))
  5995.                 type = DRM_MODE_CONNECTOR_eDP;
  5996.         else
  5997.                 type = DRM_MODE_CONNECTOR_DisplayPort;
  5998.  
  5999.         /*
  6000.          * For eDP we always set the encoder type to INTEL_OUTPUT_EDP, but
  6001.          * for DP the encoder type can be set by the caller to
  6002.          * INTEL_OUTPUT_UNKNOWN for DDI, so don't rewrite it.
  6003.          */
  6004.         if (type == DRM_MODE_CONNECTOR_eDP)
  6005.                 intel_encoder->type = INTEL_OUTPUT_EDP;
  6006.  
  6007.         /* eDP only on port B and/or C on vlv/chv */
  6008.         if (WARN_ON(IS_VALLEYVIEW(dev) && is_edp(intel_dp) &&
  6009.                     port != PORT_B && port != PORT_C))
  6010.                 return false;
  6011.  
  6012.         DRM_DEBUG_KMS("Adding %s connector on port %c\n",
  6013.                         type == DRM_MODE_CONNECTOR_eDP ? "eDP" : "DP",
  6014.                         port_name(port));
  6015.  
  6016.         drm_connector_init(dev, connector, &intel_dp_connector_funcs, type);
  6017.         drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  6018.  
  6019.         connector->interlace_allowed = true;
  6020.         connector->doublescan_allowed = 0;
  6021.  
  6022.         INIT_DELAYED_WORK(&intel_dp->panel_vdd_work,
  6023.                           edp_panel_vdd_work);
  6024.  
  6025.         intel_connector_attach_encoder(intel_connector, intel_encoder);
  6026.         drm_connector_register(connector);
  6027.  
  6028.         if (HAS_DDI(dev))
  6029.                 intel_connector->get_hw_state = intel_ddi_connector_get_hw_state;
  6030.         else
  6031.                 intel_connector->get_hw_state = intel_connector_get_hw_state;
  6032.         intel_connector->unregister = intel_dp_connector_unregister;
  6033.  
  6034.         /* Set up the hotplug pin. */
  6035.         switch (port) {
  6036.         case PORT_A:
  6037.                 intel_encoder->hpd_pin = HPD_PORT_A;
  6038.                 break;
  6039.         case PORT_B:
  6040.                 intel_encoder->hpd_pin = HPD_PORT_B;
  6041.                 if (IS_BROXTON(dev_priv) && (INTEL_REVID(dev) < BXT_REVID_B0))
  6042.                         intel_encoder->hpd_pin = HPD_PORT_A;
  6043.                 break;
  6044.         case PORT_C:
  6045.                 intel_encoder->hpd_pin = HPD_PORT_C;
  6046.                 break;
  6047.         case PORT_D:
  6048.                 intel_encoder->hpd_pin = HPD_PORT_D;
  6049.                 break;
  6050.         case PORT_E:
  6051.                 intel_encoder->hpd_pin = HPD_PORT_E;
  6052.                 break;
  6053.         default:
  6054.                 BUG();
  6055.         }
  6056.  
  6057.         if (is_edp(intel_dp)) {
  6058.                 pps_lock(intel_dp);
  6059.                 intel_dp_init_panel_power_timestamps(intel_dp);
  6060.                 if (IS_VALLEYVIEW(dev))
  6061.                         vlv_initial_power_sequencer_setup(intel_dp);
  6062.                 else
  6063.                         intel_dp_init_panel_power_sequencer(dev, intel_dp);
  6064.                 pps_unlock(intel_dp);
  6065.         }
  6066.  
  6067.         intel_dp_aux_init(intel_dp, intel_connector);
  6068.  
  6069.         /* init MST on ports that can support it */
  6070.         if (HAS_DP_MST(dev) &&
  6071.             (port == PORT_B || port == PORT_C || port == PORT_D))
  6072.                 intel_dp_mst_encoder_init(intel_dig_port,
  6073.                                           intel_connector->base.base.id);
  6074.  
  6075.         if (!intel_edp_init_connector(intel_dp, intel_connector)) {
  6076.                 drm_dp_aux_unregister(&intel_dp->aux);
  6077.                 if (is_edp(intel_dp)) {
  6078.                         cancel_delayed_work_sync(&intel_dp->panel_vdd_work);
  6079.                         /*
  6080.                          * vdd might still be enabled do to the delayed vdd off.
  6081.                          * Make sure vdd is actually turned off here.
  6082.                          */
  6083.                         pps_lock(intel_dp);
  6084.                         edp_panel_vdd_off_sync(intel_dp);
  6085.                         pps_unlock(intel_dp);
  6086.                 }
  6087.                 drm_connector_unregister(connector);
  6088.                 drm_connector_cleanup(connector);
  6089.                 return false;
  6090.         }
  6091.  
  6092.         intel_dp_add_properties(intel_dp, connector);
  6093.  
  6094.         /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  6095.          * 0xd.  Failure to do so will result in spurious interrupts being
  6096.          * generated on the port when a cable is not attached.
  6097.          */
  6098.         if (IS_G4X(dev) && !IS_GM45(dev)) {
  6099.                 u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  6100.                 I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  6101.         }
  6102.  
  6103.         i915_debugfs_connector_add(connector);
  6104.  
  6105.         return true;
  6106. }
  6107.  
  6108. bool intel_dp_init(struct drm_device *dev,
  6109.                    int output_reg,
  6110.                    enum port port)
  6111. {
  6112.         struct drm_i915_private *dev_priv = dev->dev_private;
  6113.         struct intel_digital_port *intel_dig_port;
  6114.         struct intel_encoder *intel_encoder;
  6115.         struct drm_encoder *encoder;
  6116.         struct intel_connector *intel_connector;
  6117.  
  6118.         intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
  6119.         if (!intel_dig_port)
  6120.                 return false;
  6121.  
  6122.         intel_connector = intel_connector_alloc();
  6123.         if (!intel_connector)
  6124.                 goto err_connector_alloc;
  6125.  
  6126.         intel_encoder = &intel_dig_port->base;
  6127.         encoder = &intel_encoder->base;
  6128.  
  6129.         drm_encoder_init(dev, &intel_encoder->base, &intel_dp_enc_funcs,
  6130.                          DRM_MODE_ENCODER_TMDS);
  6131.  
  6132.         intel_encoder->compute_config = intel_dp_compute_config;
  6133.         intel_encoder->disable = intel_disable_dp;
  6134.         intel_encoder->get_hw_state = intel_dp_get_hw_state;
  6135.         intel_encoder->get_config = intel_dp_get_config;
  6136.         intel_encoder->suspend = intel_dp_encoder_suspend;
  6137.         if (IS_CHERRYVIEW(dev)) {
  6138.                 intel_encoder->pre_pll_enable = chv_dp_pre_pll_enable;
  6139.                 intel_encoder->pre_enable = chv_pre_enable_dp;
  6140.                 intel_encoder->enable = vlv_enable_dp;
  6141.                 intel_encoder->post_disable = chv_post_disable_dp;
  6142.                 intel_encoder->post_pll_disable = chv_dp_post_pll_disable;
  6143.         } else if (IS_VALLEYVIEW(dev)) {
  6144.                 intel_encoder->pre_pll_enable = vlv_dp_pre_pll_enable;
  6145.                 intel_encoder->pre_enable = vlv_pre_enable_dp;
  6146.                 intel_encoder->enable = vlv_enable_dp;
  6147.                 intel_encoder->post_disable = vlv_post_disable_dp;
  6148.         } else {
  6149.                 intel_encoder->pre_enable = g4x_pre_enable_dp;
  6150.                 intel_encoder->enable = g4x_enable_dp;
  6151.                 if (INTEL_INFO(dev)->gen >= 5)
  6152.                         intel_encoder->post_disable = ilk_post_disable_dp;
  6153.         }
  6154.  
  6155.         intel_dig_port->port = port;
  6156.         intel_dig_port->dp.output_reg = output_reg;
  6157.  
  6158.         intel_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
  6159.         if (IS_CHERRYVIEW(dev)) {
  6160.                 if (port == PORT_D)
  6161.                         intel_encoder->crtc_mask = 1 << 2;
  6162.                 else
  6163.                         intel_encoder->crtc_mask = (1 << 0) | (1 << 1);
  6164.         } else {
  6165.                 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
  6166.         }
  6167.         intel_encoder->cloneable = 0;
  6168.  
  6169.         intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
  6170.         dev_priv->hotplug.irq_port[port] = intel_dig_port;
  6171.  
  6172.         if (!intel_dp_init_connector(intel_dig_port, intel_connector))
  6173.                 goto err_init_connector;
  6174.  
  6175.         return true;
  6176.  
  6177. err_init_connector:
  6178.         drm_encoder_cleanup(encoder);
  6179.         kfree(intel_connector);
  6180. err_connector_alloc:
  6181.         kfree(intel_dig_port);
  6182.         return false;
  6183. }
  6184.  
  6185. void intel_dp_mst_suspend(struct drm_device *dev)
  6186. {
  6187.         struct drm_i915_private *dev_priv = dev->dev_private;
  6188.         int i;
  6189.  
  6190.         /* disable MST */
  6191.         for (i = 0; i < I915_MAX_PORTS; i++) {
  6192.                 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
  6193.                 if (!intel_dig_port)
  6194.                         continue;
  6195.  
  6196.                 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
  6197.                         if (!intel_dig_port->dp.can_mst)
  6198.                                 continue;
  6199.                         if (intel_dig_port->dp.is_mst)
  6200.                                 drm_dp_mst_topology_mgr_suspend(&intel_dig_port->dp.mst_mgr);
  6201.                 }
  6202.         }
  6203. }
  6204.  
  6205. void intel_dp_mst_resume(struct drm_device *dev)
  6206. {
  6207.         struct drm_i915_private *dev_priv = dev->dev_private;
  6208.         int i;
  6209.  
  6210.         for (i = 0; i < I915_MAX_PORTS; i++) {
  6211.                 struct intel_digital_port *intel_dig_port = dev_priv->hotplug.irq_port[i];
  6212.                 if (!intel_dig_port)
  6213.                         continue;
  6214.                 if (intel_dig_port->base.type == INTEL_OUTPUT_DISPLAYPORT) {
  6215.                         int ret;
  6216.  
  6217.                         if (!intel_dig_port->dp.can_mst)
  6218.                                 continue;
  6219.  
  6220.                         ret = drm_dp_mst_topology_mgr_resume(&intel_dig_port->dp.mst_mgr);
  6221.                         if (ret != 0) {
  6222.                                 intel_dp_check_mst_status(&intel_dig_port->dp);
  6223.                         }
  6224.                 }
  6225.         }
  6226. }
  6227.