Home | History | Annotate | Line # | Download | only in display
      1  1.1  riastrad /*	$NetBSD: intel_crt.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2006-2007 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  1.1  riastrad  * DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors:
     26  1.1  riastrad  *	Eric Anholt <eric (at) anholt.net>
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_crt.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <linux/dmi.h>
     33  1.1  riastrad #include <linux/i2c.h>
     34  1.1  riastrad #include <linux/slab.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include <drm/drm_atomic_helper.h>
     37  1.1  riastrad #include <drm/drm_crtc.h>
     38  1.1  riastrad #include <drm/drm_edid.h>
     39  1.1  riastrad #include <drm/drm_probe_helper.h>
     40  1.1  riastrad #include <drm/i915_drm.h>
     41  1.1  riastrad 
     42  1.1  riastrad #include "i915_drv.h"
     43  1.1  riastrad #include "intel_connector.h"
     44  1.1  riastrad #include "intel_crt.h"
     45  1.1  riastrad #include "intel_ddi.h"
     46  1.1  riastrad #include "intel_display_types.h"
     47  1.1  riastrad #include "intel_fifo_underrun.h"
     48  1.1  riastrad #include "intel_gmbus.h"
     49  1.1  riastrad #include "intel_hotplug.h"
     50  1.1  riastrad 
     51  1.1  riastrad /* Here's the desired hotplug mode */
     52  1.1  riastrad #define ADPA_HOTPLUG_BITS (ADPA_CRT_HOTPLUG_PERIOD_128 |		\
     53  1.1  riastrad 			   ADPA_CRT_HOTPLUG_WARMUP_10MS |		\
     54  1.1  riastrad 			   ADPA_CRT_HOTPLUG_SAMPLE_4S |			\
     55  1.1  riastrad 			   ADPA_CRT_HOTPLUG_VOLTAGE_50 |		\
     56  1.1  riastrad 			   ADPA_CRT_HOTPLUG_VOLREF_325MV |		\
     57  1.1  riastrad 			   ADPA_CRT_HOTPLUG_ENABLE)
     58  1.1  riastrad 
     59  1.1  riastrad struct intel_crt {
     60  1.1  riastrad 	struct intel_encoder base;
     61  1.1  riastrad 	/* DPMS state is stored in the connector, which we need in the
     62  1.1  riastrad 	 * encoder's enable/disable callbacks */
     63  1.1  riastrad 	struct intel_connector *connector;
     64  1.1  riastrad 	bool force_hotplug_required;
     65  1.1  riastrad 	i915_reg_t adpa_reg;
     66  1.1  riastrad };
     67  1.1  riastrad 
     68  1.1  riastrad static struct intel_crt *intel_encoder_to_crt(struct intel_encoder *encoder)
     69  1.1  riastrad {
     70  1.1  riastrad 	return container_of(encoder, struct intel_crt, base);
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad static struct intel_crt *intel_attached_crt(struct intel_connector *connector)
     74  1.1  riastrad {
     75  1.1  riastrad 	return intel_encoder_to_crt(intel_attached_encoder(connector));
     76  1.1  riastrad }
     77  1.1  riastrad 
     78  1.1  riastrad bool intel_crt_port_enabled(struct drm_i915_private *dev_priv,
     79  1.1  riastrad 			    i915_reg_t adpa_reg, enum pipe *pipe)
     80  1.1  riastrad {
     81  1.1  riastrad 	u32 val;
     82  1.1  riastrad 
     83  1.1  riastrad 	val = I915_READ(adpa_reg);
     84  1.1  riastrad 
     85  1.1  riastrad 	/* asserts want to know the pipe even if the port is disabled */
     86  1.1  riastrad 	if (HAS_PCH_CPT(dev_priv))
     87  1.1  riastrad 		*pipe = (val & ADPA_PIPE_SEL_MASK_CPT) >> ADPA_PIPE_SEL_SHIFT_CPT;
     88  1.1  riastrad 	else
     89  1.1  riastrad 		*pipe = (val & ADPA_PIPE_SEL_MASK) >> ADPA_PIPE_SEL_SHIFT;
     90  1.1  riastrad 
     91  1.1  riastrad 	return val & ADPA_DAC_ENABLE;
     92  1.1  riastrad }
     93  1.1  riastrad 
     94  1.1  riastrad static bool intel_crt_get_hw_state(struct intel_encoder *encoder,
     95  1.1  riastrad 				   enum pipe *pipe)
     96  1.1  riastrad {
     97  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
     98  1.1  riastrad 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
     99  1.1  riastrad 	intel_wakeref_t wakeref;
    100  1.1  riastrad 	bool ret;
    101  1.1  riastrad 
    102  1.1  riastrad 	wakeref = intel_display_power_get_if_enabled(dev_priv,
    103  1.1  riastrad 						     encoder->power_domain);
    104  1.1  riastrad 	if (!wakeref)
    105  1.1  riastrad 		return false;
    106  1.1  riastrad 
    107  1.1  riastrad 	ret = intel_crt_port_enabled(dev_priv, crt->adpa_reg, pipe);
    108  1.1  riastrad 
    109  1.1  riastrad 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
    110  1.1  riastrad 
    111  1.1  riastrad 	return ret;
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad static unsigned int intel_crt_get_flags(struct intel_encoder *encoder)
    115  1.1  riastrad {
    116  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    117  1.1  riastrad 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
    118  1.1  riastrad 	u32 tmp, flags = 0;
    119  1.1  riastrad 
    120  1.1  riastrad 	tmp = I915_READ(crt->adpa_reg);
    121  1.1  riastrad 
    122  1.1  riastrad 	if (tmp & ADPA_HSYNC_ACTIVE_HIGH)
    123  1.1  riastrad 		flags |= DRM_MODE_FLAG_PHSYNC;
    124  1.1  riastrad 	else
    125  1.1  riastrad 		flags |= DRM_MODE_FLAG_NHSYNC;
    126  1.1  riastrad 
    127  1.1  riastrad 	if (tmp & ADPA_VSYNC_ACTIVE_HIGH)
    128  1.1  riastrad 		flags |= DRM_MODE_FLAG_PVSYNC;
    129  1.1  riastrad 	else
    130  1.1  riastrad 		flags |= DRM_MODE_FLAG_NVSYNC;
    131  1.1  riastrad 
    132  1.1  riastrad 	return flags;
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad static void intel_crt_get_config(struct intel_encoder *encoder,
    136  1.1  riastrad 				 struct intel_crtc_state *pipe_config)
    137  1.1  riastrad {
    138  1.1  riastrad 	pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
    139  1.1  riastrad 
    140  1.1  riastrad 	pipe_config->hw.adjusted_mode.flags |= intel_crt_get_flags(encoder);
    141  1.1  riastrad 
    142  1.1  riastrad 	pipe_config->hw.adjusted_mode.crtc_clock = pipe_config->port_clock;
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.1  riastrad static void hsw_crt_get_config(struct intel_encoder *encoder,
    146  1.1  riastrad 			       struct intel_crtc_state *pipe_config)
    147  1.1  riastrad {
    148  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    149  1.1  riastrad 
    150  1.1  riastrad 	intel_ddi_get_config(encoder, pipe_config);
    151  1.1  riastrad 
    152  1.1  riastrad 	pipe_config->hw.adjusted_mode.flags &= ~(DRM_MODE_FLAG_PHSYNC |
    153  1.1  riastrad 					      DRM_MODE_FLAG_NHSYNC |
    154  1.1  riastrad 					      DRM_MODE_FLAG_PVSYNC |
    155  1.1  riastrad 					      DRM_MODE_FLAG_NVSYNC);
    156  1.1  riastrad 	pipe_config->hw.adjusted_mode.flags |= intel_crt_get_flags(encoder);
    157  1.1  riastrad 
    158  1.1  riastrad 	pipe_config->hw.adjusted_mode.crtc_clock = lpt_get_iclkip(dev_priv);
    159  1.1  riastrad }
    160  1.1  riastrad 
    161  1.1  riastrad /* Note: The caller is required to filter out dpms modes not supported by the
    162  1.1  riastrad  * platform. */
    163  1.1  riastrad static void intel_crt_set_dpms(struct intel_encoder *encoder,
    164  1.1  riastrad 			       const struct intel_crtc_state *crtc_state,
    165  1.1  riastrad 			       int mode)
    166  1.1  riastrad {
    167  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    168  1.1  riastrad 	struct intel_crt *crt = intel_encoder_to_crt(encoder);
    169  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    170  1.1  riastrad 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
    171  1.1  riastrad 	u32 adpa;
    172  1.1  riastrad 
    173  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 5)
    174  1.1  riastrad 		adpa = ADPA_HOTPLUG_BITS;
    175  1.1  riastrad 	else
    176  1.1  riastrad 		adpa = 0;
    177  1.1  riastrad 
    178  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
    179  1.1  riastrad 		adpa |= ADPA_HSYNC_ACTIVE_HIGH;
    180  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
    181  1.1  riastrad 		adpa |= ADPA_VSYNC_ACTIVE_HIGH;
    182  1.1  riastrad 
    183  1.1  riastrad 	/* For CPT allow 3 pipe config, for others just use A or B */
    184  1.1  riastrad 	if (HAS_PCH_LPT(dev_priv))
    185  1.1  riastrad 		; /* Those bits don't exist here */
    186  1.1  riastrad 	else if (HAS_PCH_CPT(dev_priv))
    187  1.1  riastrad 		adpa |= ADPA_PIPE_SEL_CPT(crtc->pipe);
    188  1.1  riastrad 	else
    189  1.1  riastrad 		adpa |= ADPA_PIPE_SEL(crtc->pipe);
    190  1.1  riastrad 
    191  1.1  riastrad 	if (!HAS_PCH_SPLIT(dev_priv))
    192  1.1  riastrad 		I915_WRITE(BCLRPAT(crtc->pipe), 0);
    193  1.1  riastrad 
    194  1.1  riastrad 	switch (mode) {
    195  1.1  riastrad 	case DRM_MODE_DPMS_ON:
    196  1.1  riastrad 		adpa |= ADPA_DAC_ENABLE;
    197  1.1  riastrad 		break;
    198  1.1  riastrad 	case DRM_MODE_DPMS_STANDBY:
    199  1.1  riastrad 		adpa |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
    200  1.1  riastrad 		break;
    201  1.1  riastrad 	case DRM_MODE_DPMS_SUSPEND:
    202  1.1  riastrad 		adpa |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
    203  1.1  riastrad 		break;
    204  1.1  riastrad 	case DRM_MODE_DPMS_OFF:
    205  1.1  riastrad 		adpa |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
    206  1.1  riastrad 		break;
    207  1.1  riastrad 	}
    208  1.1  riastrad 
    209  1.1  riastrad 	I915_WRITE(crt->adpa_reg, adpa);
    210  1.1  riastrad }
    211  1.1  riastrad 
    212  1.1  riastrad static void intel_disable_crt(struct intel_encoder *encoder,
    213  1.1  riastrad 			      const struct intel_crtc_state *old_crtc_state,
    214  1.1  riastrad 			      const struct drm_connector_state *old_conn_state)
    215  1.1  riastrad {
    216  1.1  riastrad 	intel_crt_set_dpms(encoder, old_crtc_state, DRM_MODE_DPMS_OFF);
    217  1.1  riastrad }
    218  1.1  riastrad 
    219  1.1  riastrad static void pch_disable_crt(struct intel_encoder *encoder,
    220  1.1  riastrad 			    const struct intel_crtc_state *old_crtc_state,
    221  1.1  riastrad 			    const struct drm_connector_state *old_conn_state)
    222  1.1  riastrad {
    223  1.1  riastrad }
    224  1.1  riastrad 
    225  1.1  riastrad static void pch_post_disable_crt(struct intel_encoder *encoder,
    226  1.1  riastrad 				 const struct intel_crtc_state *old_crtc_state,
    227  1.1  riastrad 				 const struct drm_connector_state *old_conn_state)
    228  1.1  riastrad {
    229  1.1  riastrad 	intel_disable_crt(encoder, old_crtc_state, old_conn_state);
    230  1.1  riastrad }
    231  1.1  riastrad 
    232  1.1  riastrad static void hsw_disable_crt(struct intel_encoder *encoder,
    233  1.1  riastrad 			    const struct intel_crtc_state *old_crtc_state,
    234  1.1  riastrad 			    const struct drm_connector_state *old_conn_state)
    235  1.1  riastrad {
    236  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    237  1.1  riastrad 
    238  1.1  riastrad 	WARN_ON(!old_crtc_state->has_pch_encoder);
    239  1.1  riastrad 
    240  1.1  riastrad 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
    241  1.1  riastrad }
    242  1.1  riastrad 
    243  1.1  riastrad static void hsw_post_disable_crt(struct intel_encoder *encoder,
    244  1.1  riastrad 				 const struct intel_crtc_state *old_crtc_state,
    245  1.1  riastrad 				 const struct drm_connector_state *old_conn_state)
    246  1.1  riastrad {
    247  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    248  1.1  riastrad 
    249  1.1  riastrad 	intel_crtc_vblank_off(old_crtc_state);
    250  1.1  riastrad 
    251  1.1  riastrad 	intel_disable_pipe(old_crtc_state);
    252  1.1  riastrad 
    253  1.1  riastrad 	intel_ddi_disable_transcoder_func(old_crtc_state);
    254  1.1  riastrad 
    255  1.1  riastrad 	ilk_pfit_disable(old_crtc_state);
    256  1.1  riastrad 
    257  1.1  riastrad 	intel_ddi_disable_pipe_clock(old_crtc_state);
    258  1.1  riastrad 
    259  1.1  riastrad 	pch_post_disable_crt(encoder, old_crtc_state, old_conn_state);
    260  1.1  riastrad 
    261  1.1  riastrad 	lpt_disable_pch_transcoder(dev_priv);
    262  1.1  riastrad 	lpt_disable_iclkip(dev_priv);
    263  1.1  riastrad 
    264  1.1  riastrad 	intel_ddi_fdi_post_disable(encoder, old_crtc_state, old_conn_state);
    265  1.1  riastrad 
    266  1.1  riastrad 	WARN_ON(!old_crtc_state->has_pch_encoder);
    267  1.1  riastrad 
    268  1.1  riastrad 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
    269  1.1  riastrad }
    270  1.1  riastrad 
    271  1.1  riastrad static void hsw_pre_pll_enable_crt(struct intel_encoder *encoder,
    272  1.1  riastrad 				   const struct intel_crtc_state *crtc_state,
    273  1.1  riastrad 				   const struct drm_connector_state *conn_state)
    274  1.1  riastrad {
    275  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    276  1.1  riastrad 
    277  1.1  riastrad 	WARN_ON(!crtc_state->has_pch_encoder);
    278  1.1  riastrad 
    279  1.1  riastrad 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
    280  1.1  riastrad }
    281  1.1  riastrad 
    282  1.1  riastrad static void hsw_pre_enable_crt(struct intel_encoder *encoder,
    283  1.1  riastrad 			       const struct intel_crtc_state *crtc_state,
    284  1.1  riastrad 			       const struct drm_connector_state *conn_state)
    285  1.1  riastrad {
    286  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    287  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    288  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    289  1.1  riastrad 
    290  1.1  riastrad 	WARN_ON(!crtc_state->has_pch_encoder);
    291  1.1  riastrad 
    292  1.1  riastrad 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
    293  1.1  riastrad 
    294  1.1  riastrad 	hsw_fdi_link_train(encoder, crtc_state);
    295  1.1  riastrad 
    296  1.1  riastrad 	intel_ddi_enable_pipe_clock(crtc_state);
    297  1.1  riastrad }
    298  1.1  riastrad 
    299  1.1  riastrad static void hsw_enable_crt(struct intel_encoder *encoder,
    300  1.1  riastrad 			   const struct intel_crtc_state *crtc_state,
    301  1.1  riastrad 			   const struct drm_connector_state *conn_state)
    302  1.1  riastrad {
    303  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    304  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    305  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    306  1.1  riastrad 
    307  1.1  riastrad 	WARN_ON(!crtc_state->has_pch_encoder);
    308  1.1  riastrad 
    309  1.1  riastrad 	intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
    310  1.1  riastrad 
    311  1.1  riastrad 	intel_wait_for_vblank(dev_priv, pipe);
    312  1.1  riastrad 	intel_wait_for_vblank(dev_priv, pipe);
    313  1.1  riastrad 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
    314  1.1  riastrad 	intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
    315  1.1  riastrad }
    316  1.1  riastrad 
    317  1.1  riastrad static void intel_enable_crt(struct intel_encoder *encoder,
    318  1.1  riastrad 			     const struct intel_crtc_state *crtc_state,
    319  1.1  riastrad 			     const struct drm_connector_state *conn_state)
    320  1.1  riastrad {
    321  1.1  riastrad 	intel_crt_set_dpms(encoder, crtc_state, DRM_MODE_DPMS_ON);
    322  1.1  riastrad }
    323  1.1  riastrad 
    324  1.1  riastrad static enum drm_mode_status
    325  1.1  riastrad intel_crt_mode_valid(struct drm_connector *connector,
    326  1.1  riastrad 		     struct drm_display_mode *mode)
    327  1.1  riastrad {
    328  1.1  riastrad 	struct drm_device *dev = connector->dev;
    329  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    330  1.1  riastrad 	int max_dotclk = dev_priv->max_dotclk_freq;
    331  1.1  riastrad 	int max_clock;
    332  1.1  riastrad 
    333  1.1  riastrad 	if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
    334  1.1  riastrad 		return MODE_NO_DBLESCAN;
    335  1.1  riastrad 
    336  1.1  riastrad 	if (mode->clock < 25000)
    337  1.1  riastrad 		return MODE_CLOCK_LOW;
    338  1.1  riastrad 
    339  1.1  riastrad 	if (HAS_PCH_LPT(dev_priv))
    340  1.1  riastrad 		max_clock = 180000;
    341  1.1  riastrad 	else if (IS_VALLEYVIEW(dev_priv))
    342  1.1  riastrad 		/*
    343  1.1  riastrad 		 * 270 MHz due to current DPLL limits,
    344  1.1  riastrad 		 * DAC limit supposedly 355 MHz.
    345  1.1  riastrad 		 */
    346  1.1  riastrad 		max_clock = 270000;
    347  1.1  riastrad 	else if (IS_GEN_RANGE(dev_priv, 3, 4))
    348  1.1  riastrad 		max_clock = 400000;
    349  1.1  riastrad 	else
    350  1.1  riastrad 		max_clock = 350000;
    351  1.1  riastrad 	if (mode->clock > max_clock)
    352  1.1  riastrad 		return MODE_CLOCK_HIGH;
    353  1.1  riastrad 
    354  1.1  riastrad 	if (mode->clock > max_dotclk)
    355  1.1  riastrad 		return MODE_CLOCK_HIGH;
    356  1.1  riastrad 
    357  1.1  riastrad 	/* The FDI receiver on LPT only supports 8bpc and only has 2 lanes. */
    358  1.1  riastrad 	if (HAS_PCH_LPT(dev_priv) &&
    359  1.1  riastrad 	    ilk_get_lanes_required(mode->clock, 270000, 24) > 2)
    360  1.1  riastrad 		return MODE_CLOCK_HIGH;
    361  1.1  riastrad 
    362  1.1  riastrad 	/* HSW/BDW FDI limited to 4k */
    363  1.1  riastrad 	if (mode->hdisplay > 4096)
    364  1.1  riastrad 		return MODE_H_ILLEGAL;
    365  1.1  riastrad 
    366  1.1  riastrad 	return MODE_OK;
    367  1.1  riastrad }
    368  1.1  riastrad 
    369  1.1  riastrad static int intel_crt_compute_config(struct intel_encoder *encoder,
    370  1.1  riastrad 				    struct intel_crtc_state *pipe_config,
    371  1.1  riastrad 				    struct drm_connector_state *conn_state)
    372  1.1  riastrad {
    373  1.1  riastrad 	struct drm_display_mode *adjusted_mode =
    374  1.1  riastrad 		&pipe_config->hw.adjusted_mode;
    375  1.1  riastrad 
    376  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
    377  1.1  riastrad 		return -EINVAL;
    378  1.1  riastrad 
    379  1.1  riastrad 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
    380  1.1  riastrad 
    381  1.1  riastrad 	return 0;
    382  1.1  riastrad }
    383  1.1  riastrad 
    384  1.1  riastrad static int pch_crt_compute_config(struct intel_encoder *encoder,
    385  1.1  riastrad 				  struct intel_crtc_state *pipe_config,
    386  1.1  riastrad 				  struct drm_connector_state *conn_state)
    387  1.1  riastrad {
    388  1.1  riastrad 	struct drm_display_mode *adjusted_mode =
    389  1.1  riastrad 		&pipe_config->hw.adjusted_mode;
    390  1.1  riastrad 
    391  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
    392  1.1  riastrad 		return -EINVAL;
    393  1.1  riastrad 
    394  1.1  riastrad 	pipe_config->has_pch_encoder = true;
    395  1.1  riastrad 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
    396  1.1  riastrad 
    397  1.1  riastrad 	return 0;
    398  1.1  riastrad }
    399  1.1  riastrad 
    400  1.1  riastrad static int hsw_crt_compute_config(struct intel_encoder *encoder,
    401  1.1  riastrad 				  struct intel_crtc_state *pipe_config,
    402  1.1  riastrad 				  struct drm_connector_state *conn_state)
    403  1.1  riastrad {
    404  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    405  1.1  riastrad 	struct drm_display_mode *adjusted_mode =
    406  1.1  riastrad 		&pipe_config->hw.adjusted_mode;
    407  1.1  riastrad 
    408  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
    409  1.1  riastrad 		return -EINVAL;
    410  1.1  riastrad 
    411  1.1  riastrad 	/* HSW/BDW FDI limited to 4k */
    412  1.1  riastrad 	if (adjusted_mode->crtc_hdisplay > 4096 ||
    413  1.1  riastrad 	    adjusted_mode->crtc_hblank_start > 4096)
    414  1.1  riastrad 		return -EINVAL;
    415  1.1  riastrad 
    416  1.1  riastrad 	pipe_config->has_pch_encoder = true;
    417  1.1  riastrad 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
    418  1.1  riastrad 
    419  1.1  riastrad 	/* LPT FDI RX only supports 8bpc. */
    420  1.1  riastrad 	if (HAS_PCH_LPT(dev_priv)) {
    421  1.1  riastrad 		if (pipe_config->bw_constrained && pipe_config->pipe_bpp < 24) {
    422  1.1  riastrad 			DRM_DEBUG_KMS("LPT only supports 24bpp\n");
    423  1.1  riastrad 			return -EINVAL;
    424  1.1  riastrad 		}
    425  1.1  riastrad 
    426  1.1  riastrad 		pipe_config->pipe_bpp = 24;
    427  1.1  riastrad 	}
    428  1.1  riastrad 
    429  1.1  riastrad 	/* FDI must always be 2.7 GHz */
    430  1.1  riastrad 	pipe_config->port_clock = 135000 * 2;
    431  1.1  riastrad 
    432  1.1  riastrad 	return 0;
    433  1.1  riastrad }
    434  1.1  riastrad 
    435  1.1  riastrad static bool ilk_crt_detect_hotplug(struct drm_connector *connector)
    436  1.1  riastrad {
    437  1.1  riastrad 	struct drm_device *dev = connector->dev;
    438  1.1  riastrad 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
    439  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    440  1.1  riastrad 	u32 adpa;
    441  1.1  riastrad 	bool ret;
    442  1.1  riastrad 
    443  1.1  riastrad 	/* The first time through, trigger an explicit detection cycle */
    444  1.1  riastrad 	if (crt->force_hotplug_required) {
    445  1.1  riastrad 		bool turn_off_dac = HAS_PCH_SPLIT(dev_priv);
    446  1.1  riastrad 		u32 save_adpa;
    447  1.1  riastrad 
    448  1.1  riastrad 		crt->force_hotplug_required = false;
    449  1.1  riastrad 
    450  1.1  riastrad 		save_adpa = adpa = I915_READ(crt->adpa_reg);
    451  1.1  riastrad 		DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
    452  1.1  riastrad 
    453  1.1  riastrad 		adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
    454  1.1  riastrad 		if (turn_off_dac)
    455  1.1  riastrad 			adpa &= ~ADPA_DAC_ENABLE;
    456  1.1  riastrad 
    457  1.1  riastrad 		I915_WRITE(crt->adpa_reg, adpa);
    458  1.1  riastrad 
    459  1.1  riastrad 		if (intel_de_wait_for_clear(dev_priv,
    460  1.1  riastrad 					    crt->adpa_reg,
    461  1.1  riastrad 					    ADPA_CRT_HOTPLUG_FORCE_TRIGGER,
    462  1.1  riastrad 					    1000))
    463  1.1  riastrad 			DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
    464  1.1  riastrad 
    465  1.1  riastrad 		if (turn_off_dac) {
    466  1.1  riastrad 			I915_WRITE(crt->adpa_reg, save_adpa);
    467  1.1  riastrad 			POSTING_READ(crt->adpa_reg);
    468  1.1  riastrad 		}
    469  1.1  riastrad 	}
    470  1.1  riastrad 
    471  1.1  riastrad 	/* Check the status to see if both blue and green are on now */
    472  1.1  riastrad 	adpa = I915_READ(crt->adpa_reg);
    473  1.1  riastrad 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
    474  1.1  riastrad 		ret = true;
    475  1.1  riastrad 	else
    476  1.1  riastrad 		ret = false;
    477  1.1  riastrad 	DRM_DEBUG_KMS("ironlake hotplug adpa=0x%x, result %d\n", adpa, ret);
    478  1.1  riastrad 
    479  1.1  riastrad 	return ret;
    480  1.1  riastrad }
    481  1.1  riastrad 
    482  1.1  riastrad static bool valleyview_crt_detect_hotplug(struct drm_connector *connector)
    483  1.1  riastrad {
    484  1.1  riastrad 	struct drm_device *dev = connector->dev;
    485  1.1  riastrad 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
    486  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    487  1.1  riastrad 	bool reenable_hpd;
    488  1.1  riastrad 	u32 adpa;
    489  1.1  riastrad 	bool ret;
    490  1.1  riastrad 	u32 save_adpa;
    491  1.1  riastrad 
    492  1.1  riastrad 	/*
    493  1.1  riastrad 	 * Doing a force trigger causes a hpd interrupt to get sent, which can
    494  1.1  riastrad 	 * get us stuck in a loop if we're polling:
    495  1.1  riastrad 	 *  - We enable power wells and reset the ADPA
    496  1.1  riastrad 	 *  - output_poll_exec does force probe on VGA, triggering a hpd
    497  1.1  riastrad 	 *  - HPD handler waits for poll to unlock dev->mode_config.mutex
    498  1.1  riastrad 	 *  - output_poll_exec shuts off the ADPA, unlocks
    499  1.1  riastrad 	 *    dev->mode_config.mutex
    500  1.1  riastrad 	 *  - HPD handler runs, resets ADPA and brings us back to the start
    501  1.1  riastrad 	 *
    502  1.1  riastrad 	 * Just disable HPD interrupts here to prevent this
    503  1.1  riastrad 	 */
    504  1.1  riastrad 	reenable_hpd = intel_hpd_disable(dev_priv, crt->base.hpd_pin);
    505  1.1  riastrad 
    506  1.1  riastrad 	save_adpa = adpa = I915_READ(crt->adpa_reg);
    507  1.1  riastrad 	DRM_DEBUG_KMS("trigger hotplug detect cycle: adpa=0x%x\n", adpa);
    508  1.1  riastrad 
    509  1.1  riastrad 	adpa |= ADPA_CRT_HOTPLUG_FORCE_TRIGGER;
    510  1.1  riastrad 
    511  1.1  riastrad 	I915_WRITE(crt->adpa_reg, adpa);
    512  1.1  riastrad 
    513  1.1  riastrad 	if (intel_de_wait_for_clear(dev_priv, crt->adpa_reg,
    514  1.1  riastrad 				    ADPA_CRT_HOTPLUG_FORCE_TRIGGER, 1000)) {
    515  1.1  riastrad 		DRM_DEBUG_KMS("timed out waiting for FORCE_TRIGGER");
    516  1.1  riastrad 		I915_WRITE(crt->adpa_reg, save_adpa);
    517  1.1  riastrad 	}
    518  1.1  riastrad 
    519  1.1  riastrad 	/* Check the status to see if both blue and green are on now */
    520  1.1  riastrad 	adpa = I915_READ(crt->adpa_reg);
    521  1.1  riastrad 	if ((adpa & ADPA_CRT_HOTPLUG_MONITOR_MASK) != 0)
    522  1.1  riastrad 		ret = true;
    523  1.1  riastrad 	else
    524  1.1  riastrad 		ret = false;
    525  1.1  riastrad 
    526  1.1  riastrad 	DRM_DEBUG_KMS("valleyview hotplug adpa=0x%x, result %d\n", adpa, ret);
    527  1.1  riastrad 
    528  1.1  riastrad 	if (reenable_hpd)
    529  1.1  riastrad 		intel_hpd_enable(dev_priv, crt->base.hpd_pin);
    530  1.1  riastrad 
    531  1.1  riastrad 	return ret;
    532  1.1  riastrad }
    533  1.1  riastrad 
    534  1.1  riastrad static bool intel_crt_detect_hotplug(struct drm_connector *connector)
    535  1.1  riastrad {
    536  1.1  riastrad 	struct drm_device *dev = connector->dev;
    537  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    538  1.1  riastrad 	u32 stat;
    539  1.1  riastrad 	bool ret = false;
    540  1.1  riastrad 	int i, tries = 0;
    541  1.1  riastrad 
    542  1.1  riastrad 	if (HAS_PCH_SPLIT(dev_priv))
    543  1.1  riastrad 		return ilk_crt_detect_hotplug(connector);
    544  1.1  riastrad 
    545  1.1  riastrad 	if (IS_VALLEYVIEW(dev_priv))
    546  1.1  riastrad 		return valleyview_crt_detect_hotplug(connector);
    547  1.1  riastrad 
    548  1.1  riastrad 	/*
    549  1.1  riastrad 	 * On 4 series desktop, CRT detect sequence need to be done twice
    550  1.1  riastrad 	 * to get a reliable result.
    551  1.1  riastrad 	 */
    552  1.1  riastrad 
    553  1.1  riastrad 	if (IS_G45(dev_priv))
    554  1.1  riastrad 		tries = 2;
    555  1.1  riastrad 	else
    556  1.1  riastrad 		tries = 1;
    557  1.1  riastrad 
    558  1.1  riastrad 	for (i = 0; i < tries ; i++) {
    559  1.1  riastrad 		/* turn on the FORCE_DETECT */
    560  1.1  riastrad 		i915_hotplug_interrupt_update(dev_priv,
    561  1.1  riastrad 					      CRT_HOTPLUG_FORCE_DETECT,
    562  1.1  riastrad 					      CRT_HOTPLUG_FORCE_DETECT);
    563  1.1  riastrad 		/* wait for FORCE_DETECT to go off */
    564  1.1  riastrad 		if (intel_de_wait_for_clear(dev_priv, PORT_HOTPLUG_EN,
    565  1.1  riastrad 					    CRT_HOTPLUG_FORCE_DETECT, 1000))
    566  1.1  riastrad 			DRM_DEBUG_KMS("timed out waiting for FORCE_DETECT to go off");
    567  1.1  riastrad 	}
    568  1.1  riastrad 
    569  1.1  riastrad 	stat = I915_READ(PORT_HOTPLUG_STAT);
    570  1.1  riastrad 	if ((stat & CRT_HOTPLUG_MONITOR_MASK) != CRT_HOTPLUG_MONITOR_NONE)
    571  1.1  riastrad 		ret = true;
    572  1.1  riastrad 
    573  1.1  riastrad 	/* clear the interrupt we just generated, if any */
    574  1.1  riastrad 	I915_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
    575  1.1  riastrad 
    576  1.1  riastrad 	i915_hotplug_interrupt_update(dev_priv, CRT_HOTPLUG_FORCE_DETECT, 0);
    577  1.1  riastrad 
    578  1.1  riastrad 	return ret;
    579  1.1  riastrad }
    580  1.1  riastrad 
    581  1.1  riastrad static struct edid *intel_crt_get_edid(struct drm_connector *connector,
    582  1.1  riastrad 				struct i2c_adapter *i2c)
    583  1.1  riastrad {
    584  1.1  riastrad 	struct edid *edid;
    585  1.1  riastrad 
    586  1.1  riastrad 	edid = drm_get_edid(connector, i2c);
    587  1.1  riastrad 
    588  1.1  riastrad 	if (!edid && !intel_gmbus_is_forced_bit(i2c)) {
    589  1.1  riastrad 		DRM_DEBUG_KMS("CRT GMBUS EDID read failed, retry using GPIO bit-banging\n");
    590  1.1  riastrad 		intel_gmbus_force_bit(i2c, true);
    591  1.1  riastrad 		edid = drm_get_edid(connector, i2c);
    592  1.1  riastrad 		intel_gmbus_force_bit(i2c, false);
    593  1.1  riastrad 	}
    594  1.1  riastrad 
    595  1.1  riastrad 	return edid;
    596  1.1  riastrad }
    597  1.1  riastrad 
    598  1.1  riastrad /* local version of intel_ddc_get_modes() to use intel_crt_get_edid() */
    599  1.1  riastrad static int intel_crt_ddc_get_modes(struct drm_connector *connector,
    600  1.1  riastrad 				struct i2c_adapter *adapter)
    601  1.1  riastrad {
    602  1.1  riastrad 	struct edid *edid;
    603  1.1  riastrad 	int ret;
    604  1.1  riastrad 
    605  1.1  riastrad 	edid = intel_crt_get_edid(connector, adapter);
    606  1.1  riastrad 	if (!edid)
    607  1.1  riastrad 		return 0;
    608  1.1  riastrad 
    609  1.1  riastrad 	ret = intel_connector_update_modes(connector, edid);
    610  1.1  riastrad 	kfree(edid);
    611  1.1  riastrad 
    612  1.1  riastrad 	return ret;
    613  1.1  riastrad }
    614  1.1  riastrad 
    615  1.1  riastrad static bool intel_crt_detect_ddc(struct drm_connector *connector)
    616  1.1  riastrad {
    617  1.1  riastrad 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
    618  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(crt->base.base.dev);
    619  1.1  riastrad 	struct edid *edid;
    620  1.1  riastrad 	struct i2c_adapter *i2c;
    621  1.1  riastrad 	bool ret = false;
    622  1.1  riastrad 
    623  1.1  riastrad 	BUG_ON(crt->base.type != INTEL_OUTPUT_ANALOG);
    624  1.1  riastrad 
    625  1.1  riastrad 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
    626  1.1  riastrad 	edid = intel_crt_get_edid(connector, i2c);
    627  1.1  riastrad 
    628  1.1  riastrad 	if (edid) {
    629  1.1  riastrad 		bool is_digital = edid->input & DRM_EDID_INPUT_DIGITAL;
    630  1.1  riastrad 
    631  1.1  riastrad 		/*
    632  1.1  riastrad 		 * This may be a DVI-I connector with a shared DDC
    633  1.1  riastrad 		 * link between analog and digital outputs, so we
    634  1.1  riastrad 		 * have to check the EDID input spec of the attached device.
    635  1.1  riastrad 		 */
    636  1.1  riastrad 		if (!is_digital) {
    637  1.1  riastrad 			DRM_DEBUG_KMS("CRT detected via DDC:0x50 [EDID]\n");
    638  1.1  riastrad 			ret = true;
    639  1.1  riastrad 		} else {
    640  1.1  riastrad 			DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [EDID reports a digital panel]\n");
    641  1.1  riastrad 		}
    642  1.1  riastrad 	} else {
    643  1.1  riastrad 		DRM_DEBUG_KMS("CRT not detected via DDC:0x50 [no valid EDID found]\n");
    644  1.1  riastrad 	}
    645  1.1  riastrad 
    646  1.1  riastrad 	kfree(edid);
    647  1.1  riastrad 
    648  1.1  riastrad 	return ret;
    649  1.1  riastrad }
    650  1.1  riastrad 
    651  1.1  riastrad static enum drm_connector_status
    652  1.1  riastrad intel_crt_load_detect(struct intel_crt *crt, u32 pipe)
    653  1.1  riastrad {
    654  1.1  riastrad 	struct drm_device *dev = crt->base.base.dev;
    655  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    656  1.1  riastrad 	struct intel_uncore *uncore = &dev_priv->uncore;
    657  1.1  riastrad 	u32 save_bclrpat;
    658  1.1  riastrad 	u32 save_vtotal;
    659  1.1  riastrad 	u32 vtotal, vactive;
    660  1.1  riastrad 	u32 vsample;
    661  1.1  riastrad 	u32 vblank, vblank_start, vblank_end;
    662  1.1  riastrad 	u32 dsl;
    663  1.1  riastrad 	i915_reg_t bclrpat_reg, vtotal_reg,
    664  1.1  riastrad 		vblank_reg, vsync_reg, pipeconf_reg, pipe_dsl_reg;
    665  1.1  riastrad 	u8 st00;
    666  1.1  riastrad 	enum drm_connector_status status;
    667  1.1  riastrad 
    668  1.1  riastrad 	DRM_DEBUG_KMS("starting load-detect on CRT\n");
    669  1.1  riastrad 
    670  1.1  riastrad 	bclrpat_reg = BCLRPAT(pipe);
    671  1.1  riastrad 	vtotal_reg = VTOTAL(pipe);
    672  1.1  riastrad 	vblank_reg = VBLANK(pipe);
    673  1.1  riastrad 	vsync_reg = VSYNC(pipe);
    674  1.1  riastrad 	pipeconf_reg = PIPECONF(pipe);
    675  1.1  riastrad 	pipe_dsl_reg = PIPEDSL(pipe);
    676  1.1  riastrad 
    677  1.1  riastrad 	save_bclrpat = intel_uncore_read(uncore, bclrpat_reg);
    678  1.1  riastrad 	save_vtotal = intel_uncore_read(uncore, vtotal_reg);
    679  1.1  riastrad 	vblank = intel_uncore_read(uncore, vblank_reg);
    680  1.1  riastrad 
    681  1.1  riastrad 	vtotal = ((save_vtotal >> 16) & 0xfff) + 1;
    682  1.1  riastrad 	vactive = (save_vtotal & 0x7ff) + 1;
    683  1.1  riastrad 
    684  1.1  riastrad 	vblank_start = (vblank & 0xfff) + 1;
    685  1.1  riastrad 	vblank_end = ((vblank >> 16) & 0xfff) + 1;
    686  1.1  riastrad 
    687  1.1  riastrad 	/* Set the border color to purple. */
    688  1.1  riastrad 	intel_uncore_write(uncore, bclrpat_reg, 0x500050);
    689  1.1  riastrad 
    690  1.1  riastrad 	if (!IS_GEN(dev_priv, 2)) {
    691  1.1  riastrad 		u32 pipeconf = intel_uncore_read(uncore, pipeconf_reg);
    692  1.1  riastrad 		intel_uncore_write(uncore,
    693  1.1  riastrad 				   pipeconf_reg,
    694  1.1  riastrad 				   pipeconf | PIPECONF_FORCE_BORDER);
    695  1.1  riastrad 		intel_uncore_posting_read(uncore, pipeconf_reg);
    696  1.1  riastrad 		/* Wait for next Vblank to substitue
    697  1.1  riastrad 		 * border color for Color info */
    698  1.1  riastrad 		intel_wait_for_vblank(dev_priv, pipe);
    699  1.1  riastrad 		st00 = intel_uncore_read8(uncore, _VGA_MSR_WRITE);
    700  1.1  riastrad 		status = ((st00 & (1 << 4)) != 0) ?
    701  1.1  riastrad 			connector_status_connected :
    702  1.1  riastrad 			connector_status_disconnected;
    703  1.1  riastrad 
    704  1.1  riastrad 		intel_uncore_write(uncore, pipeconf_reg, pipeconf);
    705  1.1  riastrad 	} else {
    706  1.1  riastrad 		bool restore_vblank = false;
    707  1.1  riastrad 		int count, detect;
    708  1.1  riastrad 
    709  1.1  riastrad 		/*
    710  1.1  riastrad 		* If there isn't any border, add some.
    711  1.1  riastrad 		* Yes, this will flicker
    712  1.1  riastrad 		*/
    713  1.1  riastrad 		if (vblank_start <= vactive && vblank_end >= vtotal) {
    714  1.1  riastrad 			u32 vsync = I915_READ(vsync_reg);
    715  1.1  riastrad 			u32 vsync_start = (vsync & 0xffff) + 1;
    716  1.1  riastrad 
    717  1.1  riastrad 			vblank_start = vsync_start;
    718  1.1  riastrad 			intel_uncore_write(uncore,
    719  1.1  riastrad 					   vblank_reg,
    720  1.1  riastrad 					   (vblank_start - 1) |
    721  1.1  riastrad 					   ((vblank_end - 1) << 16));
    722  1.1  riastrad 			restore_vblank = true;
    723  1.1  riastrad 		}
    724  1.1  riastrad 		/* sample in the vertical border, selecting the larger one */
    725  1.1  riastrad 		if (vblank_start - vactive >= vtotal - vblank_end)
    726  1.1  riastrad 			vsample = (vblank_start + vactive) >> 1;
    727  1.1  riastrad 		else
    728  1.1  riastrad 			vsample = (vtotal + vblank_end) >> 1;
    729  1.1  riastrad 
    730  1.1  riastrad 		/*
    731  1.1  riastrad 		 * Wait for the border to be displayed
    732  1.1  riastrad 		 */
    733  1.1  riastrad 		while (intel_uncore_read(uncore, pipe_dsl_reg) >= vactive)
    734  1.1  riastrad 			;
    735  1.1  riastrad 		while ((dsl = intel_uncore_read(uncore, pipe_dsl_reg)) <=
    736  1.1  riastrad 		       vsample)
    737  1.1  riastrad 			;
    738  1.1  riastrad 		/*
    739  1.1  riastrad 		 * Watch ST00 for an entire scanline
    740  1.1  riastrad 		 */
    741  1.1  riastrad 		detect = 0;
    742  1.1  riastrad 		count = 0;
    743  1.1  riastrad 		do {
    744  1.1  riastrad 			count++;
    745  1.1  riastrad 			/* Read the ST00 VGA status register */
    746  1.1  riastrad 			st00 = intel_uncore_read8(uncore, _VGA_MSR_WRITE);
    747  1.1  riastrad 			if (st00 & (1 << 4))
    748  1.1  riastrad 				detect++;
    749  1.1  riastrad 		} while ((intel_uncore_read(uncore, pipe_dsl_reg) == dsl));
    750  1.1  riastrad 
    751  1.1  riastrad 		/* restore vblank if necessary */
    752  1.1  riastrad 		if (restore_vblank)
    753  1.1  riastrad 			intel_uncore_write(uncore, vblank_reg, vblank);
    754  1.1  riastrad 		/*
    755  1.1  riastrad 		 * If more than 3/4 of the scanline detected a monitor,
    756  1.1  riastrad 		 * then it is assumed to be present. This works even on i830,
    757  1.1  riastrad 		 * where there isn't any way to force the border color across
    758  1.1  riastrad 		 * the screen
    759  1.1  riastrad 		 */
    760  1.1  riastrad 		status = detect * 4 > count * 3 ?
    761  1.1  riastrad 			 connector_status_connected :
    762  1.1  riastrad 			 connector_status_disconnected;
    763  1.1  riastrad 	}
    764  1.1  riastrad 
    765  1.1  riastrad 	/* Restore previous settings */
    766  1.1  riastrad 	intel_uncore_write(uncore, bclrpat_reg, save_bclrpat);
    767  1.1  riastrad 
    768  1.1  riastrad 	return status;
    769  1.1  riastrad }
    770  1.1  riastrad 
    771  1.1  riastrad static int intel_spurious_crt_detect_dmi_callback(const struct dmi_system_id *id)
    772  1.1  riastrad {
    773  1.1  riastrad 	DRM_DEBUG_DRIVER("Skipping CRT detection for %s\n", id->ident);
    774  1.1  riastrad 	return 1;
    775  1.1  riastrad }
    776  1.1  riastrad 
    777  1.1  riastrad static const struct dmi_system_id intel_spurious_crt_detect[] = {
    778  1.1  riastrad 	{
    779  1.1  riastrad 		.callback = intel_spurious_crt_detect_dmi_callback,
    780  1.1  riastrad 		.ident = "ACER ZGB",
    781  1.1  riastrad 		.matches = {
    782  1.1  riastrad 			DMI_MATCH(DMI_SYS_VENDOR, "ACER"),
    783  1.1  riastrad 			DMI_MATCH(DMI_PRODUCT_NAME, "ZGB"),
    784  1.1  riastrad 		},
    785  1.1  riastrad 	},
    786  1.1  riastrad 	{
    787  1.1  riastrad 		.callback = intel_spurious_crt_detect_dmi_callback,
    788  1.1  riastrad 		.ident = "Intel DZ77BH-55K",
    789  1.1  riastrad 		.matches = {
    790  1.1  riastrad 			DMI_MATCH(DMI_BOARD_VENDOR, "Intel Corporation"),
    791  1.1  riastrad 			DMI_MATCH(DMI_BOARD_NAME, "DZ77BH-55K"),
    792  1.1  riastrad 		},
    793  1.1  riastrad 	},
    794  1.1  riastrad 	{ }
    795  1.1  riastrad };
    796  1.1  riastrad 
    797  1.1  riastrad static int
    798  1.1  riastrad intel_crt_detect(struct drm_connector *connector,
    799  1.1  riastrad 		 struct drm_modeset_acquire_ctx *ctx,
    800  1.1  riastrad 		 bool force)
    801  1.1  riastrad {
    802  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
    803  1.1  riastrad 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
    804  1.1  riastrad 	struct intel_encoder *intel_encoder = &crt->base;
    805  1.1  riastrad 	intel_wakeref_t wakeref;
    806  1.1  riastrad 	int status, ret;
    807  1.1  riastrad 	struct intel_load_detect_pipe tmp;
    808  1.1  riastrad 
    809  1.1  riastrad 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] force=%d\n",
    810  1.1  riastrad 		      connector->base.id, connector->name,
    811  1.1  riastrad 		      force);
    812  1.1  riastrad 
    813  1.1  riastrad 	if (i915_modparams.load_detect_test) {
    814  1.1  riastrad 		wakeref = intel_display_power_get(dev_priv,
    815  1.1  riastrad 						  intel_encoder->power_domain);
    816  1.1  riastrad 		goto load_detect;
    817  1.1  riastrad 	}
    818  1.1  riastrad 
    819  1.1  riastrad 	/* Skip machines without VGA that falsely report hotplug events */
    820  1.1  riastrad 	if (dmi_check_system(intel_spurious_crt_detect))
    821  1.1  riastrad 		return connector_status_disconnected;
    822  1.1  riastrad 
    823  1.1  riastrad 	wakeref = intel_display_power_get(dev_priv,
    824  1.1  riastrad 					  intel_encoder->power_domain);
    825  1.1  riastrad 
    826  1.1  riastrad 	if (I915_HAS_HOTPLUG(dev_priv)) {
    827  1.1  riastrad 		/* We can not rely on the HPD pin always being correctly wired
    828  1.1  riastrad 		 * up, for example many KVM do not pass it through, and so
    829  1.1  riastrad 		 * only trust an assertion that the monitor is connected.
    830  1.1  riastrad 		 */
    831  1.1  riastrad 		if (intel_crt_detect_hotplug(connector)) {
    832  1.1  riastrad 			DRM_DEBUG_KMS("CRT detected via hotplug\n");
    833  1.1  riastrad 			status = connector_status_connected;
    834  1.1  riastrad 			goto out;
    835  1.1  riastrad 		} else
    836  1.1  riastrad 			DRM_DEBUG_KMS("CRT not detected via hotplug\n");
    837  1.1  riastrad 	}
    838  1.1  riastrad 
    839  1.1  riastrad 	if (intel_crt_detect_ddc(connector)) {
    840  1.1  riastrad 		status = connector_status_connected;
    841  1.1  riastrad 		goto out;
    842  1.1  riastrad 	}
    843  1.1  riastrad 
    844  1.1  riastrad 	/* Load detection is broken on HPD capable machines. Whoever wants a
    845  1.1  riastrad 	 * broken monitor (without edid) to work behind a broken kvm (that fails
    846  1.1  riastrad 	 * to have the right resistors for HP detection) needs to fix this up.
    847  1.1  riastrad 	 * For now just bail out. */
    848  1.1  riastrad 	if (I915_HAS_HOTPLUG(dev_priv)) {
    849  1.1  riastrad 		status = connector_status_disconnected;
    850  1.1  riastrad 		goto out;
    851  1.1  riastrad 	}
    852  1.1  riastrad 
    853  1.1  riastrad load_detect:
    854  1.1  riastrad 	if (!force) {
    855  1.1  riastrad 		status = connector->status;
    856  1.1  riastrad 		goto out;
    857  1.1  riastrad 	}
    858  1.1  riastrad 
    859  1.1  riastrad 	/* for pre-945g platforms use load detect */
    860  1.1  riastrad 	ret = intel_get_load_detect_pipe(connector, &tmp, ctx);
    861  1.1  riastrad 	if (ret > 0) {
    862  1.1  riastrad 		if (intel_crt_detect_ddc(connector))
    863  1.1  riastrad 			status = connector_status_connected;
    864  1.1  riastrad 		else if (INTEL_GEN(dev_priv) < 4)
    865  1.1  riastrad 			status = intel_crt_load_detect(crt,
    866  1.1  riastrad 				to_intel_crtc(connector->state->crtc)->pipe);
    867  1.1  riastrad 		else if (i915_modparams.load_detect_test)
    868  1.1  riastrad 			status = connector_status_disconnected;
    869  1.1  riastrad 		else
    870  1.1  riastrad 			status = connector_status_unknown;
    871  1.1  riastrad 		intel_release_load_detect_pipe(connector, &tmp, ctx);
    872  1.1  riastrad 	} else if (ret == 0) {
    873  1.1  riastrad 		status = connector_status_unknown;
    874  1.1  riastrad 	} else {
    875  1.1  riastrad 		status = ret;
    876  1.1  riastrad 	}
    877  1.1  riastrad 
    878  1.1  riastrad out:
    879  1.1  riastrad 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
    880  1.1  riastrad 
    881  1.1  riastrad 	/*
    882  1.1  riastrad 	 * Make sure the refs for power wells enabled during detect are
    883  1.1  riastrad 	 * dropped to avoid a new detect cycle triggered by HPD polling.
    884  1.1  riastrad 	 */
    885  1.1  riastrad 	intel_display_power_flush_work(dev_priv);
    886  1.1  riastrad 
    887  1.1  riastrad 	return status;
    888  1.1  riastrad }
    889  1.1  riastrad 
    890  1.1  riastrad static int intel_crt_get_modes(struct drm_connector *connector)
    891  1.1  riastrad {
    892  1.1  riastrad 	struct drm_device *dev = connector->dev;
    893  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
    894  1.1  riastrad 	struct intel_crt *crt = intel_attached_crt(to_intel_connector(connector));
    895  1.1  riastrad 	struct intel_encoder *intel_encoder = &crt->base;
    896  1.1  riastrad 	intel_wakeref_t wakeref;
    897  1.1  riastrad 	struct i2c_adapter *i2c;
    898  1.1  riastrad 	int ret;
    899  1.1  riastrad 
    900  1.1  riastrad 	wakeref = intel_display_power_get(dev_priv,
    901  1.1  riastrad 					  intel_encoder->power_domain);
    902  1.1  riastrad 
    903  1.1  riastrad 	i2c = intel_gmbus_get_adapter(dev_priv, dev_priv->vbt.crt_ddc_pin);
    904  1.1  riastrad 	ret = intel_crt_ddc_get_modes(connector, i2c);
    905  1.1  riastrad 	if (ret || !IS_G4X(dev_priv))
    906  1.1  riastrad 		goto out;
    907  1.1  riastrad 
    908  1.1  riastrad 	/* Try to probe digital port for output in DVI-I -> VGA mode. */
    909  1.1  riastrad 	i2c = intel_gmbus_get_adapter(dev_priv, GMBUS_PIN_DPB);
    910  1.1  riastrad 	ret = intel_crt_ddc_get_modes(connector, i2c);
    911  1.1  riastrad 
    912  1.1  riastrad out:
    913  1.1  riastrad 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
    914  1.1  riastrad 
    915  1.1  riastrad 	return ret;
    916  1.1  riastrad }
    917  1.1  riastrad 
    918  1.1  riastrad void intel_crt_reset(struct drm_encoder *encoder)
    919  1.1  riastrad {
    920  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->dev);
    921  1.1  riastrad 	struct intel_crt *crt = intel_encoder_to_crt(to_intel_encoder(encoder));
    922  1.1  riastrad 
    923  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 5) {
    924  1.1  riastrad 		u32 adpa;
    925  1.1  riastrad 
    926  1.1  riastrad 		adpa = I915_READ(crt->adpa_reg);
    927  1.1  riastrad 		adpa &= ~ADPA_CRT_HOTPLUG_MASK;
    928  1.1  riastrad 		adpa |= ADPA_HOTPLUG_BITS;
    929  1.1  riastrad 		I915_WRITE(crt->adpa_reg, adpa);
    930  1.1  riastrad 		POSTING_READ(crt->adpa_reg);
    931  1.1  riastrad 
    932  1.1  riastrad 		DRM_DEBUG_KMS("crt adpa set to 0x%x\n", adpa);
    933  1.1  riastrad 		crt->force_hotplug_required = true;
    934  1.1  riastrad 	}
    935  1.1  riastrad 
    936  1.1  riastrad }
    937  1.1  riastrad 
    938  1.1  riastrad /*
    939  1.1  riastrad  * Routines for controlling stuff on the analog port
    940  1.1  riastrad  */
    941  1.1  riastrad 
    942  1.1  riastrad static const struct drm_connector_funcs intel_crt_connector_funcs = {
    943  1.1  riastrad 	.fill_modes = drm_helper_probe_single_connector_modes,
    944  1.1  riastrad 	.late_register = intel_connector_register,
    945  1.1  riastrad 	.early_unregister = intel_connector_unregister,
    946  1.1  riastrad 	.destroy = intel_connector_destroy,
    947  1.1  riastrad 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
    948  1.1  riastrad 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
    949  1.1  riastrad };
    950  1.1  riastrad 
    951  1.1  riastrad static const struct drm_connector_helper_funcs intel_crt_connector_helper_funcs = {
    952  1.1  riastrad 	.detect_ctx = intel_crt_detect,
    953  1.1  riastrad 	.mode_valid = intel_crt_mode_valid,
    954  1.1  riastrad 	.get_modes = intel_crt_get_modes,
    955  1.1  riastrad };
    956  1.1  riastrad 
    957  1.1  riastrad static const struct drm_encoder_funcs intel_crt_enc_funcs = {
    958  1.1  riastrad 	.reset = intel_crt_reset,
    959  1.1  riastrad 	.destroy = intel_encoder_destroy,
    960  1.1  riastrad };
    961  1.1  riastrad 
    962  1.1  riastrad void intel_crt_init(struct drm_i915_private *dev_priv)
    963  1.1  riastrad {
    964  1.1  riastrad 	struct drm_connector *connector;
    965  1.1  riastrad 	struct intel_crt *crt;
    966  1.1  riastrad 	struct intel_connector *intel_connector;
    967  1.1  riastrad 	i915_reg_t adpa_reg;
    968  1.1  riastrad 	u32 adpa;
    969  1.1  riastrad 
    970  1.1  riastrad 	if (HAS_PCH_SPLIT(dev_priv))
    971  1.1  riastrad 		adpa_reg = PCH_ADPA;
    972  1.1  riastrad 	else if (IS_VALLEYVIEW(dev_priv))
    973  1.1  riastrad 		adpa_reg = VLV_ADPA;
    974  1.1  riastrad 	else
    975  1.1  riastrad 		adpa_reg = ADPA;
    976  1.1  riastrad 
    977  1.1  riastrad 	adpa = I915_READ(adpa_reg);
    978  1.1  riastrad 	if ((adpa & ADPA_DAC_ENABLE) == 0) {
    979  1.1  riastrad 		/*
    980  1.1  riastrad 		 * On some machines (some IVB at least) CRT can be
    981  1.1  riastrad 		 * fused off, but there's no known fuse bit to
    982  1.1  riastrad 		 * indicate that. On these machine the ADPA register
    983  1.1  riastrad 		 * works normally, except the DAC enable bit won't
    984  1.1  riastrad 		 * take. So the only way to tell is attempt to enable
    985  1.1  riastrad 		 * it and see what happens.
    986  1.1  riastrad 		 */
    987  1.1  riastrad 		I915_WRITE(adpa_reg, adpa | ADPA_DAC_ENABLE |
    988  1.1  riastrad 			   ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
    989  1.1  riastrad 		if ((I915_READ(adpa_reg) & ADPA_DAC_ENABLE) == 0)
    990  1.1  riastrad 			return;
    991  1.1  riastrad 		I915_WRITE(adpa_reg, adpa);
    992  1.1  riastrad 	}
    993  1.1  riastrad 
    994  1.1  riastrad 	crt = kzalloc(sizeof(struct intel_crt), GFP_KERNEL);
    995  1.1  riastrad 	if (!crt)
    996  1.1  riastrad 		return;
    997  1.1  riastrad 
    998  1.1  riastrad 	intel_connector = intel_connector_alloc();
    999  1.1  riastrad 	if (!intel_connector) {
   1000  1.1  riastrad 		kfree(crt);
   1001  1.1  riastrad 		return;
   1002  1.1  riastrad 	}
   1003  1.1  riastrad 
   1004  1.1  riastrad 	connector = &intel_connector->base;
   1005  1.1  riastrad 	crt->connector = intel_connector;
   1006  1.1  riastrad 	drm_connector_init(&dev_priv->drm, &intel_connector->base,
   1007  1.1  riastrad 			   &intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
   1008  1.1  riastrad 
   1009  1.1  riastrad 	drm_encoder_init(&dev_priv->drm, &crt->base.base, &intel_crt_enc_funcs,
   1010  1.1  riastrad 			 DRM_MODE_ENCODER_DAC, "CRT");
   1011  1.1  riastrad 
   1012  1.1  riastrad 	intel_connector_attach_encoder(intel_connector, &crt->base);
   1013  1.1  riastrad 
   1014  1.1  riastrad 	crt->base.type = INTEL_OUTPUT_ANALOG;
   1015  1.1  riastrad 	crt->base.cloneable = (1 << INTEL_OUTPUT_DVO) | (1 << INTEL_OUTPUT_HDMI);
   1016  1.1  riastrad 	if (IS_I830(dev_priv))
   1017  1.1  riastrad 		crt->base.pipe_mask = BIT(PIPE_A);
   1018  1.1  riastrad 	else
   1019  1.1  riastrad 		crt->base.pipe_mask = ~0;
   1020  1.1  riastrad 
   1021  1.1  riastrad 	if (IS_GEN(dev_priv, 2))
   1022  1.1  riastrad 		connector->interlace_allowed = 0;
   1023  1.1  riastrad 	else
   1024  1.1  riastrad 		connector->interlace_allowed = 1;
   1025  1.1  riastrad 	connector->doublescan_allowed = 0;
   1026  1.1  riastrad 
   1027  1.1  riastrad 	crt->adpa_reg = adpa_reg;
   1028  1.1  riastrad 
   1029  1.1  riastrad 	crt->base.power_domain = POWER_DOMAIN_PORT_CRT;
   1030  1.1  riastrad 
   1031  1.1  riastrad 	if (I915_HAS_HOTPLUG(dev_priv) &&
   1032  1.1  riastrad 	    !dmi_check_system(intel_spurious_crt_detect)) {
   1033  1.1  riastrad 		crt->base.hpd_pin = HPD_CRT;
   1034  1.1  riastrad 		crt->base.hotplug = intel_encoder_hotplug;
   1035  1.1  riastrad 	}
   1036  1.1  riastrad 
   1037  1.1  riastrad 	if (HAS_DDI(dev_priv)) {
   1038  1.1  riastrad 		crt->base.port = PORT_E;
   1039  1.1  riastrad 		crt->base.get_config = hsw_crt_get_config;
   1040  1.1  riastrad 		crt->base.get_hw_state = intel_ddi_get_hw_state;
   1041  1.1  riastrad 		crt->base.compute_config = hsw_crt_compute_config;
   1042  1.1  riastrad 		crt->base.pre_pll_enable = hsw_pre_pll_enable_crt;
   1043  1.1  riastrad 		crt->base.pre_enable = hsw_pre_enable_crt;
   1044  1.1  riastrad 		crt->base.enable = hsw_enable_crt;
   1045  1.1  riastrad 		crt->base.disable = hsw_disable_crt;
   1046  1.1  riastrad 		crt->base.post_disable = hsw_post_disable_crt;
   1047  1.1  riastrad 	} else {
   1048  1.1  riastrad 		if (HAS_PCH_SPLIT(dev_priv)) {
   1049  1.1  riastrad 			crt->base.compute_config = pch_crt_compute_config;
   1050  1.1  riastrad 			crt->base.disable = pch_disable_crt;
   1051  1.1  riastrad 			crt->base.post_disable = pch_post_disable_crt;
   1052  1.1  riastrad 		} else {
   1053  1.1  riastrad 			crt->base.compute_config = intel_crt_compute_config;
   1054  1.1  riastrad 			crt->base.disable = intel_disable_crt;
   1055  1.1  riastrad 		}
   1056  1.1  riastrad 		crt->base.port = PORT_NONE;
   1057  1.1  riastrad 		crt->base.get_config = intel_crt_get_config;
   1058  1.1  riastrad 		crt->base.get_hw_state = intel_crt_get_hw_state;
   1059  1.1  riastrad 		crt->base.enable = intel_enable_crt;
   1060  1.1  riastrad 	}
   1061  1.1  riastrad 	intel_connector->get_hw_state = intel_connector_get_hw_state;
   1062  1.1  riastrad 
   1063  1.1  riastrad 	drm_connector_helper_add(connector, &intel_crt_connector_helper_funcs);
   1064  1.1  riastrad 
   1065  1.1  riastrad 	if (!I915_HAS_HOTPLUG(dev_priv))
   1066  1.1  riastrad 		intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
   1067  1.1  riastrad 
   1068  1.1  riastrad 	/*
   1069  1.1  riastrad 	 * Configure the automatic hotplug detection stuff
   1070  1.1  riastrad 	 */
   1071  1.1  riastrad 	crt->force_hotplug_required = false;
   1072  1.1  riastrad 
   1073  1.1  riastrad 	/*
   1074  1.1  riastrad 	 * TODO: find a proper way to discover whether we need to set the the
   1075  1.1  riastrad 	 * polarity and link reversal bits or not, instead of relying on the
   1076  1.1  riastrad 	 * BIOS.
   1077  1.1  riastrad 	 */
   1078  1.1  riastrad 	if (HAS_PCH_LPT(dev_priv)) {
   1079  1.1  riastrad 		u32 fdi_config = FDI_RX_POLARITY_REVERSED_LPT |
   1080  1.1  riastrad 				 FDI_RX_LINK_REVERSAL_OVERRIDE;
   1081  1.1  riastrad 
   1082  1.1  riastrad 		dev_priv->fdi_rx_config = I915_READ(FDI_RX_CTL(PIPE_A)) & fdi_config;
   1083  1.1  riastrad 	}
   1084  1.1  riastrad 
   1085  1.1  riastrad 	intel_crt_reset(&crt->base.base);
   1086  1.1  riastrad }
   1087