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