blob: 96e213ec202d88e023cd9ee224382e83eb4ce78b [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2007 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Eric Anholt <eric@anholt.net>
27 */
28#include <linux/i2c.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/delay.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040031#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
Matt Roperc6f95f22015-01-22 16:50:32 -080033#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/drm_crtc.h>
35#include <drm/drm_edid.h>
Chris Wilsonea5b2132010-08-04 13:50:23 +010036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
39#include "intel_sdvo_regs.h"
40
Zhenyu Wang14571b42010-03-30 14:06:33 +080041#define SDVO_TMDS_MASK (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_TMDS1)
42#define SDVO_RGB_MASK (SDVO_OUTPUT_RGB0 | SDVO_OUTPUT_RGB1)
43#define SDVO_LVDS_MASK (SDVO_OUTPUT_LVDS0 | SDVO_OUTPUT_LVDS1)
Chris Wilsona0b1c7a2011-09-30 22:56:41 +010044#define SDVO_TV_MASK (SDVO_OUTPUT_CVBS0 | SDVO_OUTPUT_SVID0 | SDVO_OUTPUT_YPRPB0)
Zhenyu Wang14571b42010-03-30 14:06:33 +080045
46#define SDVO_OUTPUT_MASK (SDVO_TMDS_MASK | SDVO_RGB_MASK | SDVO_LVDS_MASK |\
Akshay Joshi0206e352011-08-16 15:34:10 -040047 SDVO_TV_MASK)
Zhenyu Wang14571b42010-03-30 14:06:33 +080048
49#define IS_TV(c) (c->output_flag & SDVO_TV_MASK)
Chris Wilson139467432011-02-09 20:01:16 +000050#define IS_TMDS(c) (c->output_flag & SDVO_TMDS_MASK)
Zhenyu Wang14571b42010-03-30 14:06:33 +080051#define IS_LVDS(c) (c->output_flag & SDVO_LVDS_MASK)
Chris Wilson32aad862010-08-04 13:50:25 +010052#define IS_TV_OR_LVDS(c) (c->output_flag & (SDVO_TV_MASK | SDVO_LVDS_MASK))
Chris Wilson52220082011-06-20 14:45:50 +010053#define IS_DIGITAL(c) (c->output_flag & (SDVO_TMDS_MASK | SDVO_LVDS_MASK))
Zhenyu Wang14571b42010-03-30 14:06:33 +080054
Jesse Barnes79e53942008-11-07 14:24:08 -080055
Ville Syrjälä4d9194d2015-08-21 20:45:29 +030056static const char * const tv_format_names[] = {
Zhao Yakuice6feab2009-08-24 13:50:26 +080057 "NTSC_M" , "NTSC_J" , "NTSC_443",
58 "PAL_B" , "PAL_D" , "PAL_G" ,
59 "PAL_H" , "PAL_I" , "PAL_M" ,
60 "PAL_N" , "PAL_NC" , "PAL_60" ,
61 "SECAM_B" , "SECAM_D" , "SECAM_G" ,
62 "SECAM_K" , "SECAM_K1", "SECAM_L" ,
63 "SECAM_60"
64};
65
Ville Syrjälä53abb672015-08-21 20:45:28 +030066#define TV_FORMAT_NUM ARRAY_SIZE(tv_format_names)
Zhao Yakuice6feab2009-08-24 13:50:26 +080067
Chris Wilsonea5b2132010-08-04 13:50:23 +010068struct intel_sdvo {
69 struct intel_encoder base;
70
Chris Wilsonf899fc62010-07-20 15:44:45 -070071 struct i2c_adapter *i2c;
Keith Packardf9c10a92009-05-30 12:16:25 -070072 u8 slave_addr;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080073
Chris Wilsone957d772010-09-24 12:52:03 +010074 struct i2c_adapter ddc;
75
Jesse Barnese2f0ba92009-02-02 15:11:52 -080076 /* Register for the SDVO device: SDVOB or SDVOC */
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020077 i915_reg_t sdvo_reg;
Jesse Barnes79e53942008-11-07 14:24:08 -080078
Jesse Barnese2f0ba92009-02-02 15:11:52 -080079 /* Active outputs controlled by this SDVO output */
80 uint16_t controlled_output;
Jesse Barnes79e53942008-11-07 14:24:08 -080081
Jesse Barnese2f0ba92009-02-02 15:11:52 -080082 /*
83 * Capabilities of the SDVO device returned by
Damien Lespiau19d415a2013-06-10 13:28:42 +010084 * intel_sdvo_get_capabilities()
Jesse Barnese2f0ba92009-02-02 15:11:52 -080085 */
Jesse Barnes79e53942008-11-07 14:24:08 -080086 struct intel_sdvo_caps caps;
Jesse Barnese2f0ba92009-02-02 15:11:52 -080087
88 /* Pixel clock limitations reported by the SDVO device, in kHz */
Jesse Barnes79e53942008-11-07 14:24:08 -080089 int pixel_clock_min, pixel_clock_max;
90
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +080091 /*
92 * For multiple function SDVO device,
93 * this is for current attached outputs.
94 */
95 uint16_t attached_output;
96
Simon Farnsworthcc68c812011-09-21 17:13:30 +010097 /*
98 * Hotplug activation bits for this device
99 */
Jani Nikula5fa7ac92012-08-29 16:43:58 +0300100 uint16_t hotplug_active;
Simon Farnsworthcc68c812011-09-21 17:13:30 +0100101
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800102 /**
103 * This is set if we're going to treat the device as TV-out.
104 *
105 * While we have these nice friendly flags for output types that ought
106 * to decide this for us, the S-Video output on our HDMI+S-Video card
107 * shows up as RGB1 (VGA).
108 */
109 bool is_tv;
110
Ville Syrjälä2a5c0832015-11-06 21:29:59 +0200111 enum port port;
Daniel Vettereef4eac2012-03-23 23:43:35 +0100112
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800113 /**
114 * This is set if we treat the device as HDMI, instead of DVI.
115 */
116 bool is_hdmi;
Chris Wilsonda79de92010-11-22 11:12:46 +0000117 bool has_hdmi_monitor;
118 bool has_hdmi_audio;
Ville Syrjäläabedc072013-01-17 16:31:31 +0200119 bool rgb_quant_range_selectable;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800120
Ma Ling7086c872009-05-13 11:20:06 +0800121 /**
Chris Wilson6c9547f2010-08-25 10:05:17 +0100122 * This is set if we detect output of sdvo device as LVDS and
123 * have a valid fixed mode to use with the panel.
Ma Ling7086c872009-05-13 11:20:06 +0800124 */
125 bool is_lvds;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800126
127 /**
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800128 * This is sdvo fixed pannel mode pointer
129 */
130 struct drm_display_mode *sdvo_lvds_fixed_mode;
131
Eric Anholtc751ce42010-03-25 11:48:48 -0700132 /* DDC bus used by this SDVO encoder */
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800133 uint8_t ddc_bus;
Egbert Eiche7518232012-10-13 14:29:31 +0200134
135 /*
136 * the sdvo flag gets lost in round trip: dtd->adjusted_mode->dtd
137 */
138 uint8_t dtd_sdvo_flags;
Zhenyu Wang14571b42010-03-30 14:06:33 +0800139};
140
141struct intel_sdvo_connector {
Chris Wilson615fb932010-08-04 13:50:24 +0100142 struct intel_connector base;
143
Zhenyu Wang14571b42010-03-30 14:06:33 +0800144 /* Mark the type of connector */
145 uint16_t output_flag;
146
147 /* This contains all current supported TV format */
Chris Wilson40039752010-08-04 13:50:26 +0100148 u8 tv_format_supported[TV_FORMAT_NUM];
Zhenyu Wang14571b42010-03-30 14:06:33 +0800149 int format_supported_num;
Chris Wilsonc5521702010-08-04 13:50:28 +0100150 struct drm_property *tv_format;
Zhenyu Wang14571b42010-03-30 14:06:33 +0800151
Zhao Yakuib9219c52009-09-10 15:45:46 +0800152 /* add the property for the SDVO-TV */
Chris Wilsonc5521702010-08-04 13:50:28 +0100153 struct drm_property *left;
154 struct drm_property *right;
155 struct drm_property *top;
156 struct drm_property *bottom;
157 struct drm_property *hpos;
158 struct drm_property *vpos;
159 struct drm_property *contrast;
160 struct drm_property *saturation;
161 struct drm_property *hue;
162 struct drm_property *sharpness;
163 struct drm_property *flicker_filter;
164 struct drm_property *flicker_filter_adaptive;
165 struct drm_property *flicker_filter_2d;
166 struct drm_property *tv_chroma_filter;
167 struct drm_property *tv_luma_filter;
Chris Wilsone0442182010-08-04 13:50:29 +0100168 struct drm_property *dot_crawl;
Zhao Yakuib9219c52009-09-10 15:45:46 +0800169
170 /* add the property for the SDVO-TV/LVDS */
Chris Wilsonc5521702010-08-04 13:50:28 +0100171 struct drm_property *brightness;
Zhao Yakuib9219c52009-09-10 15:45:46 +0800172
Zhao Yakuib9219c52009-09-10 15:45:46 +0800173 /* this is to get the range of margin.*/
Maarten Lankhorst630d30a2017-05-01 15:38:04 +0200174 u32 max_hscan, max_vscan;
175};
176
177struct intel_sdvo_connector_state {
178 /* base.base: tv.saturation/contrast/hue/brightness */
179 struct intel_digital_connector_state base;
180
181 struct {
182 unsigned overscan_h, overscan_v, hpos, vpos, sharpness;
183 unsigned flicker_filter, flicker_filter_2d, flicker_filter_adaptive;
184 unsigned chroma_filter, luma_filter, dot_crawl;
185 } tv;
Jesse Barnes79e53942008-11-07 14:24:08 -0800186};
187
Daniel Vetter8aca63a2013-07-21 21:37:01 +0200188static struct intel_sdvo *to_sdvo(struct intel_encoder *encoder)
Chris Wilsonea5b2132010-08-04 13:50:23 +0100189{
Daniel Vetter8aca63a2013-07-21 21:37:01 +0200190 return container_of(encoder, struct intel_sdvo, base);
Chris Wilsonea5b2132010-08-04 13:50:23 +0100191}
192
Chris Wilsondf0e9242010-09-09 16:20:55 +0100193static struct intel_sdvo *intel_attached_sdvo(struct drm_connector *connector)
194{
Daniel Vetter8aca63a2013-07-21 21:37:01 +0200195 return to_sdvo(intel_attached_encoder(connector));
Chris Wilsondf0e9242010-09-09 16:20:55 +0100196}
197
Maarten Lankhorst630d30a2017-05-01 15:38:04 +0200198static struct intel_sdvo_connector *
199to_intel_sdvo_connector(struct drm_connector *connector)
Chris Wilson615fb932010-08-04 13:50:24 +0100200{
Maarten Lankhorst630d30a2017-05-01 15:38:04 +0200201 return container_of(connector, struct intel_sdvo_connector, base.base);
202}
203
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +0300204#define to_intel_sdvo_connector_state(conn_state) \
205 container_of((conn_state), struct intel_sdvo_connector_state, base.base)
Chris Wilson615fb932010-08-04 13:50:24 +0100206
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800207static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +0100208intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags);
Chris Wilson32aad862010-08-04 13:50:25 +0100209static bool
210intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
211 struct intel_sdvo_connector *intel_sdvo_connector,
212 int type);
213static bool
214intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
215 struct intel_sdvo_connector *intel_sdvo_connector);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +0800216
Chris Wilsonc16336b2018-02-14 09:09:05 +0000217/*
Jesse Barnes79e53942008-11-07 14:24:08 -0800218 * Writes the SDVOB or SDVOC with the given value, but always writes both
219 * SDVOB and SDVOC to work around apparent hardware issues (according to
220 * comments in the BIOS).
221 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100222static void intel_sdvo_write_sdvox(struct intel_sdvo *intel_sdvo, u32 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800223{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100224 struct drm_device *dev = intel_sdvo->base.base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100225 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -0800226 u32 bval = val, cval = val;
227 int i;
228
Ville Syrjälä2a5c0832015-11-06 21:29:59 +0200229 if (HAS_PCH_SPLIT(dev_priv)) {
Chris Wilsonea5b2132010-08-04 13:50:23 +0100230 I915_WRITE(intel_sdvo->sdvo_reg, val);
Ville Syrjäläabab6312015-05-05 17:17:32 +0300231 POSTING_READ(intel_sdvo->sdvo_reg);
Ville Syrjäläe8504ee2015-05-05 17:17:33 +0300232 /*
233 * HW workaround, need to write this twice for issue
234 * that may result in first write getting masked.
235 */
Tvrtko Ursulin6e266952016-10-13 11:02:53 +0100236 if (HAS_PCH_IBX(dev_priv)) {
Ville Syrjäläe8504ee2015-05-05 17:17:33 +0300237 I915_WRITE(intel_sdvo->sdvo_reg, val);
238 POSTING_READ(intel_sdvo->sdvo_reg);
239 }
Zhao Yakui461ed3c2010-03-30 15:11:33 +0800240 return;
241 }
242
Ville Syrjälä2a5c0832015-11-06 21:29:59 +0200243 if (intel_sdvo->port == PORT_B)
Paulo Zanonie2debe92013-02-18 19:00:27 -0300244 cval = I915_READ(GEN3_SDVOC);
245 else
246 bval = I915_READ(GEN3_SDVOB);
247
Jesse Barnes79e53942008-11-07 14:24:08 -0800248 /*
249 * Write the registers twice for luck. Sometimes,
250 * writing them only once doesn't appear to 'stick'.
251 * The BIOS does this too. Yay, magic
252 */
Chris Wilsonc16336b2018-02-14 09:09:05 +0000253 for (i = 0; i < 2; i++) {
Paulo Zanonie2debe92013-02-18 19:00:27 -0300254 I915_WRITE(GEN3_SDVOB, bval);
Ville Syrjäläabab6312015-05-05 17:17:32 +0300255 POSTING_READ(GEN3_SDVOB);
Chris Wilsonc16336b2018-02-14 09:09:05 +0000256
Paulo Zanonie2debe92013-02-18 19:00:27 -0300257 I915_WRITE(GEN3_SDVOC, cval);
Ville Syrjäläabab6312015-05-05 17:17:32 +0300258 POSTING_READ(GEN3_SDVOC);
Jesse Barnes79e53942008-11-07 14:24:08 -0800259 }
260}
261
Chris Wilson32aad862010-08-04 13:50:25 +0100262static bool intel_sdvo_read_byte(struct intel_sdvo *intel_sdvo, u8 addr, u8 *ch)
Jesse Barnes79e53942008-11-07 14:24:08 -0800263{
Jesse Barnes79e53942008-11-07 14:24:08 -0800264 struct i2c_msg msgs[] = {
265 {
Chris Wilsone957d772010-09-24 12:52:03 +0100266 .addr = intel_sdvo->slave_addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800267 .flags = 0,
268 .len = 1,
Chris Wilsone957d772010-09-24 12:52:03 +0100269 .buf = &addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800270 },
271 {
Chris Wilsone957d772010-09-24 12:52:03 +0100272 .addr = intel_sdvo->slave_addr,
Jesse Barnes79e53942008-11-07 14:24:08 -0800273 .flags = I2C_M_RD,
274 .len = 1,
Chris Wilsone957d772010-09-24 12:52:03 +0100275 .buf = ch,
Jesse Barnes79e53942008-11-07 14:24:08 -0800276 }
277 };
Chris Wilson32aad862010-08-04 13:50:25 +0100278 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -0800279
Chris Wilsonf899fc62010-07-20 15:44:45 -0700280 if ((ret = i2c_transfer(intel_sdvo->i2c, msgs, 2)) == 2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800281 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -0800282
Zhao Yakui8a4c47f2009-07-20 13:48:04 +0800283 DRM_DEBUG_KMS("i2c transfer returned %d\n", ret);
Jesse Barnes79e53942008-11-07 14:24:08 -0800284 return false;
285}
286
Jesse Barnes79e53942008-11-07 14:24:08 -0800287#define SDVO_CMD_NAME_ENTRY(cmd) {cmd, #cmd}
288/** Mapping of command numbers to names, for debug output */
Tobias Klauser005568b2009-02-09 22:02:42 +0100289static const struct _sdvo_cmd_name {
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800290 u8 cmd;
Chris Wilson2e88e402010-08-07 11:01:27 +0100291 const char *name;
Tvrtko Ursulin579627e2016-10-13 11:09:24 +0100292} __attribute__ ((packed)) sdvo_cmd_names[] = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400293 SDVO_CMD_NAME_ENTRY(SDVO_CMD_RESET),
294 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DEVICE_CAPS),
295 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FIRMWARE_REV),
296 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TRAINED_INPUTS),
297 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_OUTPUTS),
298 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_OUTPUTS),
299 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_IN_OUT_MAP),
300 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_IN_OUT_MAP),
301 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ATTACHED_DISPLAYS),
302 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HOT_PLUG_SUPPORT),
303 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ACTIVE_HOT_PLUG),
304 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ACTIVE_HOT_PLUG),
305 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INTERRUPT_EVENT_SOURCE),
306 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_INPUT),
307 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TARGET_OUTPUT),
308 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART1),
309 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_TIMINGS_PART2),
310 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
311 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART2),
312 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_INPUT_TIMINGS_PART1),
313 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART1),
314 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OUTPUT_TIMINGS_PART2),
315 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART1),
316 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_TIMINGS_PART2),
317 SDVO_CMD_NAME_ENTRY(SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING),
318 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1),
319 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2),
320 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE),
321 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OUTPUT_PIXEL_CLOCK_RANGE),
322 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_CLOCK_RATE_MULTS),
323 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CLOCK_RATE_MULT),
324 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CLOCK_RATE_MULT),
325 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_TV_FORMATS),
326 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_FORMAT),
327 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_FORMAT),
328 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_POWER_STATES),
329 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_POWER_STATE),
330 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODER_POWER_STATE),
331 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DISPLAY_POWER_STATE),
332 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTROL_BUS_SWITCH),
333 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT),
334 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SCALED_HDTV_RESOLUTION_SUPPORT),
335 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS),
Chris Wilsonc5521702010-08-04 13:50:28 +0100336
Akshay Joshi0206e352011-08-16 15:34:10 -0400337 /* Add the op code for SDVO enhancements */
338 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HPOS),
339 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HPOS),
340 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HPOS),
341 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_VPOS),
342 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_VPOS),
343 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_VPOS),
344 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SATURATION),
345 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SATURATION),
346 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SATURATION),
347 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_HUE),
348 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HUE),
349 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HUE),
350 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_CONTRAST),
351 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_CONTRAST),
352 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_CONTRAST),
353 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_BRIGHTNESS),
354 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_BRIGHTNESS),
355 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_BRIGHTNESS),
356 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_H),
357 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_H),
358 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_H),
359 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_OVERSCAN_V),
360 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_OVERSCAN_V),
361 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_OVERSCAN_V),
362 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER),
363 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER),
364 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER),
365 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_ADAPTIVE),
366 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_ADAPTIVE),
367 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_ADAPTIVE),
368 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_FLICKER_FILTER_2D),
369 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_FLICKER_FILTER_2D),
370 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_FLICKER_FILTER_2D),
371 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_SHARPNESS),
372 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SHARPNESS),
373 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_SHARPNESS),
374 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_DOT_CRAWL),
375 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_DOT_CRAWL),
376 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_CHROMA_FILTER),
377 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_CHROMA_FILTER),
378 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_CHROMA_FILTER),
379 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_MAX_TV_LUMA_FILTER),
380 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_TV_LUMA_FILTER),
381 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_TV_LUMA_FILTER),
Chris Wilsonc5521702010-08-04 13:50:28 +0100382
Akshay Joshi0206e352011-08-16 15:34:10 -0400383 /* HDMI op code */
384 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_SUPP_ENCODE),
385 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_ENCODE),
386 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_ENCODE),
387 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_PIXEL_REPLI),
388 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_PIXEL_REPLI),
389 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY_CAP),
390 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_COLORIMETRY),
391 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_COLORIMETRY),
392 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_ENCRYPT_PREFER),
393 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_AUDIO_STAT),
394 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_AUDIO_STAT),
395 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INDEX),
396 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_INDEX),
397 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_INFO),
398 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_AV_SPLIT),
399 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_AV_SPLIT),
400 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_TXRATE),
401 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_TXRATE),
402 SDVO_CMD_NAME_ENTRY(SDVO_CMD_SET_HBUF_DATA),
403 SDVO_CMD_NAME_ENTRY(SDVO_CMD_GET_HBUF_DATA),
Jesse Barnes79e53942008-11-07 14:24:08 -0800404};
405
Ville Syrjälä2a5c0832015-11-06 21:29:59 +0200406#define SDVO_NAME(svdo) ((svdo)->port == PORT_B ? "SDVOB" : "SDVOC")
Jesse Barnes79e53942008-11-07 14:24:08 -0800407
Chris Wilsonea5b2132010-08-04 13:50:23 +0100408static void intel_sdvo_debug_write(struct intel_sdvo *intel_sdvo, u8 cmd,
Chris Wilson32aad862010-08-04 13:50:25 +0100409 const void *args, int args_len)
Jesse Barnes79e53942008-11-07 14:24:08 -0800410{
Daniel Vetter84fcb462013-11-27 16:03:01 +0100411 int i, pos = 0;
412#define BUF_LEN 256
413 char buffer[BUF_LEN];
Jesse Barnes79e53942008-11-07 14:24:08 -0800414
Daniel Vetter84fcb462013-11-27 16:03:01 +0100415#define BUF_PRINT(args...) \
416 pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
417
418
419 for (i = 0; i < args_len; i++) {
420 BUF_PRINT("%02X ", ((u8 *)args)[i]);
421 }
422 for (; i < 8; i++) {
423 BUF_PRINT(" ");
424 }
Kulikov Vasiliy04ad3272010-06-28 15:54:56 +0400425 for (i = 0; i < ARRAY_SIZE(sdvo_cmd_names); i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800426 if (cmd == sdvo_cmd_names[i].cmd) {
Daniel Vetter84fcb462013-11-27 16:03:01 +0100427 BUF_PRINT("(%s)", sdvo_cmd_names[i].name);
Jesse Barnes79e53942008-11-07 14:24:08 -0800428 break;
429 }
430 }
Daniel Vetter84fcb462013-11-27 16:03:01 +0100431 if (i == ARRAY_SIZE(sdvo_cmd_names)) {
432 BUF_PRINT("(%02X)", cmd);
433 }
434 BUG_ON(pos >= BUF_LEN - 1);
435#undef BUF_PRINT
436#undef BUF_LEN
437
438 DRM_DEBUG_KMS("%s: W: %02X %s\n", SDVO_NAME(intel_sdvo), cmd, buffer);
Jesse Barnes79e53942008-11-07 14:24:08 -0800439}
Jesse Barnes79e53942008-11-07 14:24:08 -0800440
Ville Syrjälä4d9194d2015-08-21 20:45:29 +0300441static const char * const cmd_status_names[] = {
Jesse Barnes79e53942008-11-07 14:24:08 -0800442 "Power on",
443 "Success",
444 "Not supported",
445 "Invalid arg",
446 "Pending",
447 "Target not specified",
448 "Scaling not supported"
449};
450
Daniel Vettera85066842017-07-26 15:26:47 +0200451static bool __intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
452 const void *args, int args_len,
453 bool unlocked)
Chris Wilsone957d772010-09-24 12:52:03 +0100454{
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700455 u8 *buf, status;
456 struct i2c_msg *msgs;
457 int i, ret = true;
458
Daniel Vettera85066842017-07-26 15:26:47 +0200459 /* Would be simpler to allocate both in one go ? */
Mihnea Dobrescu-Balaur5c67eeb2013-03-10 14:22:48 +0200460 buf = kzalloc(args_len * 2 + 2, GFP_KERNEL);
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700461 if (!buf)
462 return false;
463
464 msgs = kcalloc(args_len + 3, sizeof(*msgs), GFP_KERNEL);
Alan Cox0274df32012-07-25 13:51:04 +0100465 if (!msgs) {
Daniel Vettera85066842017-07-26 15:26:47 +0200466 kfree(buf);
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700467 return false;
Daniel Vettera85066842017-07-26 15:26:47 +0200468 }
Chris Wilsone957d772010-09-24 12:52:03 +0100469
470 intel_sdvo_debug_write(intel_sdvo, cmd, args, args_len);
471
472 for (i = 0; i < args_len; i++) {
473 msgs[i].addr = intel_sdvo->slave_addr;
474 msgs[i].flags = 0;
475 msgs[i].len = 2;
476 msgs[i].buf = buf + 2 *i;
477 buf[2*i + 0] = SDVO_I2C_ARG_0 - i;
478 buf[2*i + 1] = ((u8*)args)[i];
479 }
480 msgs[i].addr = intel_sdvo->slave_addr;
481 msgs[i].flags = 0;
482 msgs[i].len = 2;
483 msgs[i].buf = buf + 2*i;
484 buf[2*i + 0] = SDVO_I2C_OPCODE;
485 buf[2*i + 1] = cmd;
486
487 /* the following two are to read the response */
488 status = SDVO_I2C_CMD_STATUS;
489 msgs[i+1].addr = intel_sdvo->slave_addr;
490 msgs[i+1].flags = 0;
491 msgs[i+1].len = 1;
492 msgs[i+1].buf = &status;
493
494 msgs[i+2].addr = intel_sdvo->slave_addr;
495 msgs[i+2].flags = I2C_M_RD;
496 msgs[i+2].len = 1;
497 msgs[i+2].buf = &status;
498
Daniel Vettera85066842017-07-26 15:26:47 +0200499 if (unlocked)
500 ret = i2c_transfer(intel_sdvo->i2c, msgs, i+3);
501 else
502 ret = __i2c_transfer(intel_sdvo->i2c, msgs, i+3);
Chris Wilsone957d772010-09-24 12:52:03 +0100503 if (ret < 0) {
504 DRM_DEBUG_KMS("I2c transfer returned %d\n", ret);
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700505 ret = false;
506 goto out;
Chris Wilsone957d772010-09-24 12:52:03 +0100507 }
508 if (ret != i+3) {
509 /* failure in I2C transfer */
510 DRM_DEBUG_KMS("I2c transfer returned %d/%d\n", ret, i+3);
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700511 ret = false;
Chris Wilsone957d772010-09-24 12:52:03 +0100512 }
513
Ben Widawsky3bf3f452012-04-16 14:07:41 -0700514out:
515 kfree(msgs);
516 kfree(buf);
517 return ret;
Chris Wilsone957d772010-09-24 12:52:03 +0100518}
519
Daniel Vettera85066842017-07-26 15:26:47 +0200520static bool intel_sdvo_write_cmd(struct intel_sdvo *intel_sdvo, u8 cmd,
521 const void *args, int args_len)
522{
523 return __intel_sdvo_write_cmd(intel_sdvo, cmd, args, args_len, true);
524}
525
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100526static bool intel_sdvo_read_response(struct intel_sdvo *intel_sdvo,
527 void *response, int response_len)
Jesse Barnes79e53942008-11-07 14:24:08 -0800528{
Chris Wilsonfc373812012-11-23 11:57:56 +0000529 u8 retry = 15; /* 5 quick checks, followed by 10 long checks */
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100530 u8 status;
Daniel Vetter84fcb462013-11-27 16:03:01 +0100531 int i, pos = 0;
532#define BUF_LEN 256
533 char buffer[BUF_LEN];
Jesse Barnes79e53942008-11-07 14:24:08 -0800534
Chris Wilsond121a5d2011-01-25 15:00:01 +0000535
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100536 /*
537 * The documentation states that all commands will be
538 * processed within 15µs, and that we need only poll
539 * the status byte a maximum of 3 times in order for the
540 * command to be complete.
541 *
542 * Check 5 times in case the hardware failed to read the docs.
Chris Wilsonfc373812012-11-23 11:57:56 +0000543 *
544 * Also beware that the first response by many devices is to
545 * reply PENDING and stall for time. TVs are notorious for
546 * requiring longer than specified to complete their replies.
547 * Originally (in the DDX long ago), the delay was only ever 15ms
548 * with an additional delay of 30ms applied for TVs added later after
549 * many experiments. To accommodate both sets of delays, we do a
550 * sequence of slow checks if the device is falling behind and fails
551 * to reply within 5*15µs.
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100552 */
Chris Wilsond121a5d2011-01-25 15:00:01 +0000553 if (!intel_sdvo_read_byte(intel_sdvo,
554 SDVO_I2C_CMD_STATUS,
555 &status))
556 goto log_fail;
557
Guillaume Clement1ad87e72013-08-10 21:57:57 +0200558 while ((status == SDVO_CMD_STATUS_PENDING ||
Chris Wilson46a3f4a2013-09-24 12:55:40 +0100559 status == SDVO_CMD_STATUS_TARGET_NOT_SPECIFIED) && --retry) {
Chris Wilsonfc373812012-11-23 11:57:56 +0000560 if (retry < 10)
561 msleep(15);
562 else
563 udelay(15);
564
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100565 if (!intel_sdvo_read_byte(intel_sdvo,
566 SDVO_I2C_CMD_STATUS,
567 &status))
Chris Wilsond121a5d2011-01-25 15:00:01 +0000568 goto log_fail;
569 }
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100570
Daniel Vetter84fcb462013-11-27 16:03:01 +0100571#define BUF_PRINT(args...) \
572 pos += snprintf(buffer + pos, max_t(int, BUF_LEN - pos, 0), args)
573
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 if (status <= SDVO_CMD_STATUS_SCALING_NOT_SUPP)
Daniel Vetter84fcb462013-11-27 16:03:01 +0100575 BUF_PRINT("(%s)", cmd_status_names[status]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800576 else
Daniel Vetter84fcb462013-11-27 16:03:01 +0100577 BUF_PRINT("(??? %d)", status);
Jesse Barnes79e53942008-11-07 14:24:08 -0800578
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100579 if (status != SDVO_CMD_STATUS_SUCCESS)
580 goto log_fail;
Jesse Barnes79e53942008-11-07 14:24:08 -0800581
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100582 /* Read the command response */
583 for (i = 0; i < response_len; i++) {
584 if (!intel_sdvo_read_byte(intel_sdvo,
585 SDVO_I2C_RETURN_0 + i,
586 &((u8 *)response)[i]))
587 goto log_fail;
Daniel Vetter84fcb462013-11-27 16:03:01 +0100588 BUF_PRINT(" %02X", ((u8 *)response)[i]);
Jesse Barnes79e53942008-11-07 14:24:08 -0800589 }
Daniel Vetter84fcb462013-11-27 16:03:01 +0100590 BUG_ON(pos >= BUF_LEN - 1);
591#undef BUF_PRINT
592#undef BUF_LEN
593
594 DRM_DEBUG_KMS("%s: R: %s\n", SDVO_NAME(intel_sdvo), buffer);
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100595 return true;
596
597log_fail:
Daniel Vetter84fcb462013-11-27 16:03:01 +0100598 DRM_DEBUG_KMS("%s: R: ... failed\n", SDVO_NAME(intel_sdvo));
Chris Wilsonb5c616a2010-09-09 19:06:13 +0100599 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800600}
601
Ville Syrjälä5e7234c2015-09-25 16:37:43 +0300602static int intel_sdvo_get_pixel_multiplier(const struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800603{
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300604 if (adjusted_mode->crtc_clock >= 100000)
Jesse Barnes79e53942008-11-07 14:24:08 -0800605 return 1;
Ville Syrjäläaad941d2015-09-25 16:38:56 +0300606 else if (adjusted_mode->crtc_clock >= 50000)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607 return 2;
608 else
609 return 4;
610}
611
Daniel Vettera85066842017-07-26 15:26:47 +0200612static bool __intel_sdvo_set_control_bus_switch(struct intel_sdvo *intel_sdvo,
613 u8 ddc_bus)
Jesse Barnes79e53942008-11-07 14:24:08 -0800614{
Chris Wilsond121a5d2011-01-25 15:00:01 +0000615 /* This must be the immediately preceding write before the i2c xfer */
Daniel Vettera85066842017-07-26 15:26:47 +0200616 return __intel_sdvo_write_cmd(intel_sdvo,
617 SDVO_CMD_SET_CONTROL_BUS_SWITCH,
618 &ddc_bus, 1, false);
Jesse Barnes79e53942008-11-07 14:24:08 -0800619}
620
Chris Wilson32aad862010-08-04 13:50:25 +0100621static bool intel_sdvo_set_value(struct intel_sdvo *intel_sdvo, u8 cmd, const void *data, int len)
622{
Chris Wilsond121a5d2011-01-25 15:00:01 +0000623 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, data, len))
624 return false;
625
626 return intel_sdvo_read_response(intel_sdvo, NULL, 0);
Chris Wilson32aad862010-08-04 13:50:25 +0100627}
628
629static bool
630intel_sdvo_get_value(struct intel_sdvo *intel_sdvo, u8 cmd, void *value, int len)
631{
632 if (!intel_sdvo_write_cmd(intel_sdvo, cmd, NULL, 0))
633 return false;
634
635 return intel_sdvo_read_response(intel_sdvo, value, len);
636}
637
638static bool intel_sdvo_set_target_input(struct intel_sdvo *intel_sdvo)
Jesse Barnes79e53942008-11-07 14:24:08 -0800639{
640 struct intel_sdvo_set_target_input_args targets = {0};
Chris Wilson32aad862010-08-04 13:50:25 +0100641 return intel_sdvo_set_value(intel_sdvo,
642 SDVO_CMD_SET_TARGET_INPUT,
643 &targets, sizeof(targets));
Jesse Barnes79e53942008-11-07 14:24:08 -0800644}
645
Chris Wilsonc16336b2018-02-14 09:09:05 +0000646/*
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 * Return whether each input is trained.
648 *
649 * This function is making an assumption about the layout of the response,
650 * which should be checked against the docs.
651 */
Chris Wilsonea5b2132010-08-04 13:50:23 +0100652static bool intel_sdvo_get_trained_inputs(struct intel_sdvo *intel_sdvo, bool *input_1, bool *input_2)
Jesse Barnes79e53942008-11-07 14:24:08 -0800653{
654 struct intel_sdvo_get_trained_inputs_response response;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655
Chris Wilson1a3665c2011-01-25 13:59:37 +0000656 BUILD_BUG_ON(sizeof(response) != 1);
Chris Wilson32aad862010-08-04 13:50:25 +0100657 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_TRAINED_INPUTS,
658 &response, sizeof(response)))
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 return false;
660
661 *input_1 = response.input0_trained;
662 *input_2 = response.input1_trained;
663 return true;
664}
665
Chris Wilsonea5b2132010-08-04 13:50:23 +0100666static bool intel_sdvo_set_active_outputs(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800667 u16 outputs)
668{
Chris Wilson32aad862010-08-04 13:50:25 +0100669 return intel_sdvo_set_value(intel_sdvo,
670 SDVO_CMD_SET_ACTIVE_OUTPUTS,
671 &outputs, sizeof(outputs));
Jesse Barnes79e53942008-11-07 14:24:08 -0800672}
673
Daniel Vetter4ac41f42012-07-02 14:54:00 +0200674static bool intel_sdvo_get_active_outputs(struct intel_sdvo *intel_sdvo,
675 u16 *outputs)
676{
677 return intel_sdvo_get_value(intel_sdvo,
678 SDVO_CMD_GET_ACTIVE_OUTPUTS,
679 outputs, sizeof(*outputs));
680}
681
Chris Wilsonea5b2132010-08-04 13:50:23 +0100682static bool intel_sdvo_set_encoder_power_state(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 int mode)
684{
Chris Wilson32aad862010-08-04 13:50:25 +0100685 u8 state = SDVO_ENCODER_STATE_ON;
Jesse Barnes79e53942008-11-07 14:24:08 -0800686
687 switch (mode) {
688 case DRM_MODE_DPMS_ON:
689 state = SDVO_ENCODER_STATE_ON;
690 break;
691 case DRM_MODE_DPMS_STANDBY:
692 state = SDVO_ENCODER_STATE_STANDBY;
693 break;
694 case DRM_MODE_DPMS_SUSPEND:
695 state = SDVO_ENCODER_STATE_SUSPEND;
696 break;
697 case DRM_MODE_DPMS_OFF:
698 state = SDVO_ENCODER_STATE_OFF;
699 break;
700 }
701
Chris Wilson32aad862010-08-04 13:50:25 +0100702 return intel_sdvo_set_value(intel_sdvo,
703 SDVO_CMD_SET_ENCODER_POWER_STATE, &state, sizeof(state));
Jesse Barnes79e53942008-11-07 14:24:08 -0800704}
705
Chris Wilsonea5b2132010-08-04 13:50:23 +0100706static bool intel_sdvo_get_input_pixel_clock_range(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800707 int *clock_min,
708 int *clock_max)
709{
710 struct intel_sdvo_pixel_clock_range clocks;
Jesse Barnes79e53942008-11-07 14:24:08 -0800711
Chris Wilson1a3665c2011-01-25 13:59:37 +0000712 BUILD_BUG_ON(sizeof(clocks) != 4);
Chris Wilson32aad862010-08-04 13:50:25 +0100713 if (!intel_sdvo_get_value(intel_sdvo,
714 SDVO_CMD_GET_INPUT_PIXEL_CLOCK_RANGE,
715 &clocks, sizeof(clocks)))
Jesse Barnes79e53942008-11-07 14:24:08 -0800716 return false;
717
718 /* Convert the values from units of 10 kHz to kHz. */
719 *clock_min = clocks.min * 10;
720 *clock_max = clocks.max * 10;
Jesse Barnes79e53942008-11-07 14:24:08 -0800721 return true;
722}
723
Chris Wilsonea5b2132010-08-04 13:50:23 +0100724static bool intel_sdvo_set_target_output(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800725 u16 outputs)
726{
Chris Wilson32aad862010-08-04 13:50:25 +0100727 return intel_sdvo_set_value(intel_sdvo,
728 SDVO_CMD_SET_TARGET_OUTPUT,
729 &outputs, sizeof(outputs));
Jesse Barnes79e53942008-11-07 14:24:08 -0800730}
731
Chris Wilsonea5b2132010-08-04 13:50:23 +0100732static bool intel_sdvo_set_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
Jesse Barnes79e53942008-11-07 14:24:08 -0800733 struct intel_sdvo_dtd *dtd)
734{
Chris Wilson32aad862010-08-04 13:50:25 +0100735 return intel_sdvo_set_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
736 intel_sdvo_set_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
Jesse Barnes79e53942008-11-07 14:24:08 -0800737}
738
Jesse Barnes045ac3b2013-05-14 17:08:26 -0700739static bool intel_sdvo_get_timing(struct intel_sdvo *intel_sdvo, u8 cmd,
740 struct intel_sdvo_dtd *dtd)
741{
742 return intel_sdvo_get_value(intel_sdvo, cmd, &dtd->part1, sizeof(dtd->part1)) &&
743 intel_sdvo_get_value(intel_sdvo, cmd + 1, &dtd->part2, sizeof(dtd->part2));
744}
745
Chris Wilsonea5b2132010-08-04 13:50:23 +0100746static bool intel_sdvo_set_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800747 struct intel_sdvo_dtd *dtd)
748{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100749 return intel_sdvo_set_timing(intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800750 SDVO_CMD_SET_INPUT_TIMINGS_PART1, dtd);
751}
752
Chris Wilsonea5b2132010-08-04 13:50:23 +0100753static bool intel_sdvo_set_output_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800754 struct intel_sdvo_dtd *dtd)
755{
Chris Wilsonea5b2132010-08-04 13:50:23 +0100756 return intel_sdvo_set_timing(intel_sdvo,
Jesse Barnes79e53942008-11-07 14:24:08 -0800757 SDVO_CMD_SET_OUTPUT_TIMINGS_PART1, dtd);
758}
759
Jesse Barnes045ac3b2013-05-14 17:08:26 -0700760static bool intel_sdvo_get_input_timing(struct intel_sdvo *intel_sdvo,
761 struct intel_sdvo_dtd *dtd)
762{
763 return intel_sdvo_get_timing(intel_sdvo,
764 SDVO_CMD_GET_INPUT_TIMINGS_PART1, dtd);
765}
766
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800767static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +0100768intel_sdvo_create_preferred_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800769 uint16_t clock,
770 uint16_t width,
771 uint16_t height)
772{
773 struct intel_sdvo_preferred_input_timing_args args;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800774
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800775 memset(&args, 0, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800776 args.clock = clock;
777 args.width = width;
778 args.height = height;
Zhenyu Wange642c6f2009-03-24 14:02:42 +0800779 args.interlace = 0;
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800780
Chris Wilsonea5b2132010-08-04 13:50:23 +0100781 if (intel_sdvo->is_lvds &&
782 (intel_sdvo->sdvo_lvds_fixed_mode->hdisplay != width ||
783 intel_sdvo->sdvo_lvds_fixed_mode->vdisplay != height))
ling.ma@intel.com12682a92009-06-30 11:35:35 +0800784 args.scaled = 1;
785
Chris Wilson32aad862010-08-04 13:50:25 +0100786 return intel_sdvo_set_value(intel_sdvo,
787 SDVO_CMD_CREATE_PREFERRED_INPUT_TIMING,
788 &args, sizeof(args));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800789}
790
Chris Wilsonea5b2132010-08-04 13:50:23 +0100791static bool intel_sdvo_get_preferred_input_timing(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800792 struct intel_sdvo_dtd *dtd)
793{
Chris Wilson1a3665c2011-01-25 13:59:37 +0000794 BUILD_BUG_ON(sizeof(dtd->part1) != 8);
795 BUILD_BUG_ON(sizeof(dtd->part2) != 8);
Chris Wilson32aad862010-08-04 13:50:25 +0100796 return intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART1,
797 &dtd->part1, sizeof(dtd->part1)) &&
798 intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_PREFERRED_INPUT_TIMING_PART2,
799 &dtd->part2, sizeof(dtd->part2));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800800}
Jesse Barnes79e53942008-11-07 14:24:08 -0800801
Chris Wilsonea5b2132010-08-04 13:50:23 +0100802static bool intel_sdvo_set_clock_rate_mult(struct intel_sdvo *intel_sdvo, u8 val)
Jesse Barnes79e53942008-11-07 14:24:08 -0800803{
Chris Wilson32aad862010-08-04 13:50:25 +0100804 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_CLOCK_RATE_MULT, &val, 1);
Jesse Barnes79e53942008-11-07 14:24:08 -0800805}
806
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800807static void intel_sdvo_get_dtd_from_mode(struct intel_sdvo_dtd *dtd,
Chris Wilson32aad862010-08-04 13:50:25 +0100808 const struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -0800809{
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800810 uint16_t width, height;
811 uint16_t h_blank_len, h_sync_len, v_blank_len, v_sync_len;
812 uint16_t h_sync_offset, v_sync_offset;
Daniel Vetter66518192012-04-01 19:16:18 +0200813 int mode_clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800814
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200815 memset(dtd, 0, sizeof(*dtd));
816
Daniel Vetterc6ebd4c2012-04-24 18:27:57 +0200817 width = mode->hdisplay;
818 height = mode->vdisplay;
Jesse Barnes79e53942008-11-07 14:24:08 -0800819
820 /* do some mode translations */
Daniel Vetterc6ebd4c2012-04-24 18:27:57 +0200821 h_blank_len = mode->htotal - mode->hdisplay;
822 h_sync_len = mode->hsync_end - mode->hsync_start;
Jesse Barnes79e53942008-11-07 14:24:08 -0800823
Daniel Vetterc6ebd4c2012-04-24 18:27:57 +0200824 v_blank_len = mode->vtotal - mode->vdisplay;
825 v_sync_len = mode->vsync_end - mode->vsync_start;
Jesse Barnes79e53942008-11-07 14:24:08 -0800826
Daniel Vetterc6ebd4c2012-04-24 18:27:57 +0200827 h_sync_offset = mode->hsync_start - mode->hdisplay;
828 v_sync_offset = mode->vsync_start - mode->vdisplay;
Jesse Barnes79e53942008-11-07 14:24:08 -0800829
Daniel Vetter66518192012-04-01 19:16:18 +0200830 mode_clock = mode->clock;
Daniel Vetter66518192012-04-01 19:16:18 +0200831 mode_clock /= 10;
832 dtd->part1.clock = mode_clock;
833
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800834 dtd->part1.h_active = width & 0xff;
835 dtd->part1.h_blank = h_blank_len & 0xff;
836 dtd->part1.h_high = (((width >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800837 ((h_blank_len >> 8) & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800838 dtd->part1.v_active = height & 0xff;
839 dtd->part1.v_blank = v_blank_len & 0xff;
840 dtd->part1.v_high = (((height >> 8) & 0xf) << 4) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800841 ((v_blank_len >> 8) & 0xf);
842
Zhenyu Wang171a9e92009-03-24 14:02:41 +0800843 dtd->part2.h_sync_off = h_sync_offset & 0xff;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800844 dtd->part2.h_sync_width = h_sync_len & 0xff;
845 dtd->part2.v_sync_off_width = (v_sync_offset & 0xf) << 4 |
Jesse Barnes79e53942008-11-07 14:24:08 -0800846 (v_sync_len & 0xf);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800847 dtd->part2.sync_off_width_high = ((h_sync_offset & 0x300) >> 2) |
Jesse Barnes79e53942008-11-07 14:24:08 -0800848 ((h_sync_len & 0x300) >> 4) | ((v_sync_offset & 0x30) >> 2) |
849 ((v_sync_len & 0x30) >> 4);
850
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800851 dtd->part2.dtd_flags = 0x18;
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200852 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
853 dtd->part2.dtd_flags |= DTD_FLAG_INTERLACE;
Jesse Barnes79e53942008-11-07 14:24:08 -0800854 if (mode->flags & DRM_MODE_FLAG_PHSYNC)
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200855 dtd->part2.dtd_flags |= DTD_FLAG_HSYNC_POSITIVE;
Jesse Barnes79e53942008-11-07 14:24:08 -0800856 if (mode->flags & DRM_MODE_FLAG_PVSYNC)
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200857 dtd->part2.dtd_flags |= DTD_FLAG_VSYNC_POSITIVE;
Jesse Barnes79e53942008-11-07 14:24:08 -0800858
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800859 dtd->part2.v_sync_off_high = v_sync_offset & 0xc0;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800860}
Jesse Barnes79e53942008-11-07 14:24:08 -0800861
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200862static void intel_sdvo_get_mode_from_dtd(struct drm_display_mode *pmode,
Chris Wilson32aad862010-08-04 13:50:25 +0100863 const struct intel_sdvo_dtd *dtd)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800864{
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200865 struct drm_display_mode mode = {};
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800866
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200867 mode.hdisplay = dtd->part1.h_active;
868 mode.hdisplay += ((dtd->part1.h_high >> 4) & 0x0f) << 8;
869 mode.hsync_start = mode.hdisplay + dtd->part2.h_sync_off;
870 mode.hsync_start += (dtd->part2.sync_off_width_high & 0xc0) << 2;
871 mode.hsync_end = mode.hsync_start + dtd->part2.h_sync_width;
872 mode.hsync_end += (dtd->part2.sync_off_width_high & 0x30) << 4;
873 mode.htotal = mode.hdisplay + dtd->part1.h_blank;
874 mode.htotal += (dtd->part1.h_high & 0xf) << 8;
875
876 mode.vdisplay = dtd->part1.v_active;
877 mode.vdisplay += ((dtd->part1.v_high >> 4) & 0x0f) << 8;
878 mode.vsync_start = mode.vdisplay;
879 mode.vsync_start += (dtd->part2.v_sync_off_width >> 4) & 0xf;
880 mode.vsync_start += (dtd->part2.sync_off_width_high & 0x0c) << 2;
881 mode.vsync_start += dtd->part2.v_sync_off_high & 0xc0;
882 mode.vsync_end = mode.vsync_start +
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800883 (dtd->part2.v_sync_off_width & 0xf);
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200884 mode.vsync_end += (dtd->part2.sync_off_width_high & 0x3) << 4;
885 mode.vtotal = mode.vdisplay + dtd->part1.v_blank;
886 mode.vtotal += (dtd->part1.v_high & 0xf) << 8;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800887
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200888 mode.clock = dtd->part1.clock * 10;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800889
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200890 if (dtd->part2.dtd_flags & DTD_FLAG_INTERLACE)
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200891 mode.flags |= DRM_MODE_FLAG_INTERLACE;
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200892 if (dtd->part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200893 mode.flags |= DRM_MODE_FLAG_PHSYNC;
Daniel Vetter3cea2102013-09-10 10:02:48 +0200894 else
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200895 mode.flags |= DRM_MODE_FLAG_NHSYNC;
Daniel Vetter59d92bf2012-05-12 22:22:58 +0200896 if (dtd->part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200897 mode.flags |= DRM_MODE_FLAG_PVSYNC;
Daniel Vetter3cea2102013-09-10 10:02:48 +0200898 else
Daniel Vetter1c4a8142013-09-11 09:58:49 +0200899 mode.flags |= DRM_MODE_FLAG_NVSYNC;
900
901 drm_mode_set_crtcinfo(&mode, 0);
902
903 drm_mode_copy(pmode, &mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800904}
905
Chris Wilsone27d8532010-10-22 09:15:22 +0100906static bool intel_sdvo_check_supp_encode(struct intel_sdvo *intel_sdvo)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800907{
Chris Wilsone27d8532010-10-22 09:15:22 +0100908 struct intel_sdvo_encode encode;
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800909
Chris Wilson1a3665c2011-01-25 13:59:37 +0000910 BUILD_BUG_ON(sizeof(encode) != 2);
Chris Wilsone27d8532010-10-22 09:15:22 +0100911 return intel_sdvo_get_value(intel_sdvo,
912 SDVO_CMD_GET_SUPP_ENCODE,
913 &encode, sizeof(encode));
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800914}
915
Chris Wilsonea5b2132010-08-04 13:50:23 +0100916static bool intel_sdvo_set_encode(struct intel_sdvo *intel_sdvo,
Eric Anholtc751ce42010-03-25 11:48:48 -0700917 uint8_t mode)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800918{
Chris Wilson32aad862010-08-04 13:50:25 +0100919 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_ENCODE, &mode, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800920}
921
Chris Wilsonea5b2132010-08-04 13:50:23 +0100922static bool intel_sdvo_set_colorimetry(struct intel_sdvo *intel_sdvo,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800923 uint8_t mode)
924{
Chris Wilson32aad862010-08-04 13:50:25 +0100925 return intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_COLORIMETRY, &mode, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800926}
927
928#if 0
Chris Wilsonea5b2132010-08-04 13:50:23 +0100929static void intel_sdvo_dump_hdmi_buf(struct intel_sdvo *intel_sdvo)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800930{
931 int i, j;
932 uint8_t set_buf_index[2];
933 uint8_t av_split;
934 uint8_t buf_size;
935 uint8_t buf[48];
936 uint8_t *pos;
937
Chris Wilson32aad862010-08-04 13:50:25 +0100938 intel_sdvo_get_value(encoder, SDVO_CMD_GET_HBUF_AV_SPLIT, &av_split, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800939
940 for (i = 0; i <= av_split; i++) {
941 set_buf_index[0] = i; set_buf_index[1] = 0;
Eric Anholtc751ce42010-03-25 11:48:48 -0700942 intel_sdvo_write_cmd(encoder, SDVO_CMD_SET_HBUF_INDEX,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800943 set_buf_index, 2);
Eric Anholtc751ce42010-03-25 11:48:48 -0700944 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_INFO, NULL, 0);
945 intel_sdvo_read_response(encoder, &buf_size, 1);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800946
947 pos = buf;
948 for (j = 0; j <= buf_size; j += 8) {
Eric Anholtc751ce42010-03-25 11:48:48 -0700949 intel_sdvo_write_cmd(encoder, SDVO_CMD_GET_HBUF_DATA,
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800950 NULL, 0);
Eric Anholtc751ce42010-03-25 11:48:48 -0700951 intel_sdvo_read_response(encoder, pos, 8);
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800952 pos += 8;
953 }
954 }
955}
956#endif
957
Daniel Vetterb6e0e542012-10-21 12:52:39 +0200958static bool intel_sdvo_write_infoframe(struct intel_sdvo *intel_sdvo,
959 unsigned if_index, uint8_t tx_rate,
Ville Syrjäläfff63862013-12-10 15:19:08 +0200960 const uint8_t *data, unsigned length)
Daniel Vetterb6e0e542012-10-21 12:52:39 +0200961{
962 uint8_t set_buf_index[2] = { if_index, 0 };
963 uint8_t hbuf_size, tmp[8];
964 int i;
965
966 if (!intel_sdvo_set_value(intel_sdvo,
967 SDVO_CMD_SET_HBUF_INDEX,
968 set_buf_index, 2))
969 return false;
970
971 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HBUF_INFO,
972 &hbuf_size, 1))
973 return false;
974
975 /* Buffer size is 0 based, hooray! */
976 hbuf_size++;
977
978 DRM_DEBUG_KMS("writing sdvo hbuf: %i, hbuf_size %i, hbuf_size: %i\n",
979 if_index, length, hbuf_size);
980
981 for (i = 0; i < hbuf_size; i += 8) {
982 memset(tmp, 0, 8);
983 if (i < length)
984 memcpy(tmp, data + i, min_t(unsigned, 8, length - i));
985
986 if (!intel_sdvo_set_value(intel_sdvo,
987 SDVO_CMD_SET_HBUF_DATA,
988 tmp, 8))
989 return false;
990 }
991
992 return intel_sdvo_set_value(intel_sdvo,
993 SDVO_CMD_SET_HBUF_TXRATE,
994 &tx_rate, 1);
995}
996
Ville Syrjäläabedc072013-01-17 16:31:31 +0200997static bool intel_sdvo_set_avi_infoframe(struct intel_sdvo *intel_sdvo,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +0300998 const struct intel_crtc_state *pipe_config)
Jesse Barnese2f0ba92009-02-02 15:11:52 -0800999{
Damien Lespiau15dcd352013-08-06 20:32:20 +01001000 uint8_t sdvo_data[HDMI_INFOFRAME_SIZE(AVI)];
Damien Lespiau15dcd352013-08-06 20:32:20 +01001001 union hdmi_infoframe frame;
1002 int ret;
1003 ssize_t len;
1004
1005 ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
Shashank Sharma0c1f5282017-07-13 21:03:07 +05301006 &pipe_config->base.adjusted_mode,
1007 false);
Damien Lespiau15dcd352013-08-06 20:32:20 +01001008 if (ret < 0) {
1009 DRM_ERROR("couldn't fill AVI infoframe\n");
1010 return false;
1011 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001012
Ville Syrjäläabedc072013-01-17 16:31:31 +02001013 if (intel_sdvo->rgb_quant_range_selectable) {
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001014 if (pipe_config->limited_color_range)
Damien Lespiau15dcd352013-08-06 20:32:20 +01001015 frame.avi.quantization_range =
1016 HDMI_QUANTIZATION_RANGE_LIMITED;
Ville Syrjäläabedc072013-01-17 16:31:31 +02001017 else
Damien Lespiau15dcd352013-08-06 20:32:20 +01001018 frame.avi.quantization_range =
1019 HDMI_QUANTIZATION_RANGE_FULL;
Ville Syrjäläabedc072013-01-17 16:31:31 +02001020 }
1021
Damien Lespiau15dcd352013-08-06 20:32:20 +01001022 len = hdmi_infoframe_pack(&frame, sdvo_data, sizeof(sdvo_data));
1023 if (len < 0)
1024 return false;
Daniel Vetter81014b92012-05-12 20:22:00 +02001025
Daniel Vetterb6e0e542012-10-21 12:52:39 +02001026 return intel_sdvo_write_infoframe(intel_sdvo, SDVO_HBUF_INDEX_AVI_IF,
1027 SDVO_HBUF_TX_VSYNC,
1028 sdvo_data, sizeof(sdvo_data));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001029}
1030
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001031static bool intel_sdvo_set_tv_format(struct intel_sdvo *intel_sdvo,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001032 const struct drm_connector_state *conn_state)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001033{
Zhao Yakuice6feab2009-08-24 13:50:26 +08001034 struct intel_sdvo_tv_format format;
Chris Wilson40039752010-08-04 13:50:26 +01001035 uint32_t format_map;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001036
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001037 format_map = 1 << conn_state->tv.mode;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001038 memset(&format, 0, sizeof(format));
Chris Wilson32aad862010-08-04 13:50:25 +01001039 memcpy(&format, &format_map, min(sizeof(format), sizeof(format_map)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08001040
Chris Wilson32aad862010-08-04 13:50:25 +01001041 BUILD_BUG_ON(sizeof(format) != 6);
1042 return intel_sdvo_set_value(intel_sdvo,
1043 SDVO_CMD_SET_TV_FORMAT,
1044 &format, sizeof(format));
1045}
Zhao Yakuice6feab2009-08-24 13:50:26 +08001046
Chris Wilson32aad862010-08-04 13:50:25 +01001047static bool
1048intel_sdvo_set_output_timings_from_mode(struct intel_sdvo *intel_sdvo,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001049 const struct drm_display_mode *mode)
Chris Wilson32aad862010-08-04 13:50:25 +01001050{
1051 struct intel_sdvo_dtd output_dtd;
1052
1053 if (!intel_sdvo_set_target_output(intel_sdvo,
1054 intel_sdvo->attached_output))
1055 return false;
1056
1057 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
1058 if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1059 return false;
1060
1061 return true;
1062}
1063
Chris Wilsonc16336b2018-02-14 09:09:05 +00001064/*
1065 * Asks the sdvo controller for the preferred input mode given the output mode.
1066 * Unfortunately we have to set up the full output mode to do that.
1067 */
Chris Wilson32aad862010-08-04 13:50:25 +01001068static bool
Daniel Vetterc9a29692012-04-10 13:55:47 +02001069intel_sdvo_get_preferred_input_mode(struct intel_sdvo *intel_sdvo,
Laurent Pincharte811f5a2012-07-17 17:56:50 +02001070 const struct drm_display_mode *mode,
Daniel Vetterc9a29692012-04-10 13:55:47 +02001071 struct drm_display_mode *adjusted_mode)
Chris Wilson32aad862010-08-04 13:50:25 +01001072{
Daniel Vetterc9a29692012-04-10 13:55:47 +02001073 struct intel_sdvo_dtd input_dtd;
1074
Chris Wilson32aad862010-08-04 13:50:25 +01001075 /* Reset the input timing to the screen. Assume always input 0. */
1076 if (!intel_sdvo_set_target_input(intel_sdvo))
1077 return false;
1078
1079 if (!intel_sdvo_create_preferred_input_timing(intel_sdvo,
1080 mode->clock / 10,
1081 mode->hdisplay,
1082 mode->vdisplay))
1083 return false;
1084
1085 if (!intel_sdvo_get_preferred_input_timing(intel_sdvo,
Daniel Vetterc9a29692012-04-10 13:55:47 +02001086 &input_dtd))
Chris Wilson32aad862010-08-04 13:50:25 +01001087 return false;
1088
Daniel Vetterc9a29692012-04-10 13:55:47 +02001089 intel_sdvo_get_mode_from_dtd(adjusted_mode, &input_dtd);
Egbert Eiche7518232012-10-13 14:29:31 +02001090 intel_sdvo->dtd_sdvo_flags = input_dtd.part2.sdvo_flags;
Chris Wilson32aad862010-08-04 13:50:25 +01001091
Chris Wilson32aad862010-08-04 13:50:25 +01001092 return true;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001093}
1094
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001095static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc_state *pipe_config)
Daniel Vetter70484552013-04-30 14:01:41 +02001096{
Ville Syrjälä3c52f4e2013-09-06 23:28:59 +03001097 unsigned dotclock = pipe_config->port_clock;
Daniel Vetter70484552013-04-30 14:01:41 +02001098 struct dpll *clock = &pipe_config->dpll;
1099
Chris Wilsonc16336b2018-02-14 09:09:05 +00001100 /*
1101 * SDVO TV has fixed PLL values depend on its clock range,
1102 * this mirrors vbios setting.
1103 */
Daniel Vetter70484552013-04-30 14:01:41 +02001104 if (dotclock >= 100000 && dotclock < 140500) {
1105 clock->p1 = 2;
1106 clock->p2 = 10;
1107 clock->n = 3;
1108 clock->m1 = 16;
1109 clock->m2 = 8;
1110 } else if (dotclock >= 140500 && dotclock <= 200000) {
1111 clock->p1 = 1;
1112 clock->p2 = 10;
1113 clock->n = 6;
1114 clock->m1 = 12;
1115 clock->m2 = 8;
1116 } else {
1117 WARN(1, "SDVO TV clock out of range: %i\n", dotclock);
1118 }
1119
1120 pipe_config->clock_set = true;
1121}
1122
Daniel Vetter6cc5f342013-03-27 00:44:53 +01001123static bool intel_sdvo_compute_config(struct intel_encoder *encoder,
Maarten Lankhorst0a478c22016-08-09 17:04:05 +02001124 struct intel_crtc_state *pipe_config,
1125 struct drm_connector_state *conn_state)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001126{
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001127 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001128 struct intel_sdvo_connector_state *intel_sdvo_state =
1129 to_intel_sdvo_connector_state(conn_state);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02001130 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1131 struct drm_display_mode *mode = &pipe_config->base.mode;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001132
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01001133 DRM_DEBUG_KMS("forcing bpc to 8 for SDVO\n");
1134 pipe_config->pipe_bpp = 8*3;
1135
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001136 if (HAS_PCH_SPLIT(to_i915(encoder->base.dev)))
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01001137 pipe_config->has_pch_encoder = true;
1138
Chris Wilsonc16336b2018-02-14 09:09:05 +00001139 /*
1140 * We need to construct preferred input timings based on our
Chris Wilson32aad862010-08-04 13:50:25 +01001141 * output timings. To do that, we have to set the output
1142 * timings, even though this isn't really the right place in
1143 * the sequence to do it. Oh well.
1144 */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001145 if (intel_sdvo->is_tv) {
Chris Wilson32aad862010-08-04 13:50:25 +01001146 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo, mode))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001147 return false;
Chris Wilson32aad862010-08-04 13:50:25 +01001148
Daniel Vetterc9a29692012-04-10 13:55:47 +02001149 (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1150 mode,
1151 adjusted_mode);
Daniel Vetter09ede542013-04-30 14:01:45 +02001152 pipe_config->sdvo_tv_clock = true;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001153 } else if (intel_sdvo->is_lvds) {
Chris Wilson32aad862010-08-04 13:50:25 +01001154 if (!intel_sdvo_set_output_timings_from_mode(intel_sdvo,
Chris Wilson6c9547f2010-08-25 10:05:17 +01001155 intel_sdvo->sdvo_lvds_fixed_mode))
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001156 return false;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001157
Daniel Vetterc9a29692012-04-10 13:55:47 +02001158 (void) intel_sdvo_get_preferred_input_mode(intel_sdvo,
1159 mode,
1160 adjusted_mode);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001161 }
Chris Wilson32aad862010-08-04 13:50:25 +01001162
Chris Wilsonc16336b2018-02-14 09:09:05 +00001163 /*
1164 * Make the CRTC code factor in the SDVO pixel multiplier. The
Chris Wilson6c9547f2010-08-25 10:05:17 +01001165 * SDVO device will factor out the multiplier during mode_set.
Chris Wilson32aad862010-08-04 13:50:25 +01001166 */
Daniel Vetter6cc5f342013-03-27 00:44:53 +01001167 pipe_config->pixel_multiplier =
1168 intel_sdvo_get_pixel_multiplier(adjusted_mode);
Chris Wilson32aad862010-08-04 13:50:25 +01001169
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001170 if (intel_sdvo_state->base.force_audio != HDMI_AUDIO_OFF_DVI)
Maarten Lankhorstb32962f2017-05-01 15:38:03 +02001171 pipe_config->has_hdmi_sink = intel_sdvo->has_hdmi_monitor;
1172
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001173 if (intel_sdvo_state->base.force_audio == HDMI_AUDIO_ON ||
1174 (intel_sdvo_state->base.force_audio == HDMI_AUDIO_AUTO && intel_sdvo->has_hdmi_audio))
Maarten Lankhorstb32962f2017-05-01 15:38:03 +02001175 pipe_config->has_audio = true;
Daniel Vetter9f040032014-04-24 23:54:50 +02001176
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001177 if (intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_AUTO) {
Chris Wilsonc16336b2018-02-14 09:09:05 +00001178 /*
1179 * See CEA-861-E - 5.1 Default Encoding Parameters
1180 *
1181 * FIXME: This bit is only valid when using TMDS encoding and 8
1182 * bit per color mode.
1183 */
Daniel Vetter9f040032014-04-24 23:54:50 +02001184 if (pipe_config->has_hdmi_sink &&
Thierry Reding18316c82012-12-20 15:41:44 +01001185 drm_match_cea_mode(adjusted_mode) > 1)
Daniel Vetter69f5acc2014-04-24 23:54:48 +02001186 pipe_config->limited_color_range = true;
1187 } else {
Daniel Vetter9f040032014-04-24 23:54:50 +02001188 if (pipe_config->has_hdmi_sink &&
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001189 intel_sdvo_state->base.broadcast_rgb == INTEL_BROADCAST_RGB_LIMITED)
Daniel Vetter69f5acc2014-04-24 23:54:48 +02001190 pipe_config->limited_color_range = true;
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02001191 }
1192
Daniel Vetter70484552013-04-30 14:01:41 +02001193 /* Clock computation needs to happen after pixel multiplier. */
1194 if (intel_sdvo->is_tv)
1195 i9xx_adjust_sdvo_tv_clock(pipe_config);
1196
Ville Syrjälä7949dd42015-09-25 16:39:30 +03001197 /* Set user selected PAR to incoming mode's member */
1198 if (intel_sdvo->is_hdmi)
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02001199 adjusted_mode->picture_aspect_ratio = conn_state->picture_aspect_ratio;
Ville Syrjälä7949dd42015-09-25 16:39:30 +03001200
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001201 return true;
1202}
1203
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001204#define UPDATE_PROPERTY(input, NAME) \
1205 do { \
1206 val = input; \
1207 intel_sdvo_set_value(intel_sdvo, SDVO_CMD_SET_##NAME, &val, sizeof(val)); \
1208 } while (0)
1209
1210static void intel_sdvo_update_props(struct intel_sdvo *intel_sdvo,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001211 const struct intel_sdvo_connector_state *sdvo_state)
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001212{
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001213 const struct drm_connector_state *conn_state = &sdvo_state->base.base;
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001214 struct intel_sdvo_connector *intel_sdvo_conn =
1215 to_intel_sdvo_connector(conn_state->connector);
1216 uint16_t val;
1217
1218 if (intel_sdvo_conn->left)
1219 UPDATE_PROPERTY(sdvo_state->tv.overscan_h, OVERSCAN_H);
1220
1221 if (intel_sdvo_conn->top)
1222 UPDATE_PROPERTY(sdvo_state->tv.overscan_v, OVERSCAN_V);
1223
1224 if (intel_sdvo_conn->hpos)
1225 UPDATE_PROPERTY(sdvo_state->tv.hpos, HPOS);
1226
1227 if (intel_sdvo_conn->vpos)
1228 UPDATE_PROPERTY(sdvo_state->tv.vpos, VPOS);
1229
1230 if (intel_sdvo_conn->saturation)
1231 UPDATE_PROPERTY(conn_state->tv.saturation, SATURATION);
1232
1233 if (intel_sdvo_conn->contrast)
1234 UPDATE_PROPERTY(conn_state->tv.contrast, CONTRAST);
1235
1236 if (intel_sdvo_conn->hue)
1237 UPDATE_PROPERTY(conn_state->tv.hue, HUE);
1238
1239 if (intel_sdvo_conn->brightness)
1240 UPDATE_PROPERTY(conn_state->tv.brightness, BRIGHTNESS);
1241
1242 if (intel_sdvo_conn->sharpness)
1243 UPDATE_PROPERTY(sdvo_state->tv.sharpness, SHARPNESS);
1244
1245 if (intel_sdvo_conn->flicker_filter)
1246 UPDATE_PROPERTY(sdvo_state->tv.flicker_filter, FLICKER_FILTER);
1247
1248 if (intel_sdvo_conn->flicker_filter_2d)
1249 UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_2d, FLICKER_FILTER_2D);
1250
1251 if (intel_sdvo_conn->flicker_filter_adaptive)
1252 UPDATE_PROPERTY(sdvo_state->tv.flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
1253
1254 if (intel_sdvo_conn->tv_chroma_filter)
1255 UPDATE_PROPERTY(sdvo_state->tv.chroma_filter, TV_CHROMA_FILTER);
1256
1257 if (intel_sdvo_conn->tv_luma_filter)
1258 UPDATE_PROPERTY(sdvo_state->tv.luma_filter, TV_LUMA_FILTER);
1259
1260 if (intel_sdvo_conn->dot_crawl)
1261 UPDATE_PROPERTY(sdvo_state->tv.dot_crawl, DOT_CRAWL);
1262
1263#undef UPDATE_PROPERTY
1264}
1265
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001266static void intel_sdvo_pre_enable(struct intel_encoder *intel_encoder,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001267 const struct intel_crtc_state *crtc_state,
1268 const struct drm_connector_state *conn_state)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001269{
Tvrtko Ursulin66478472016-11-16 08:55:40 +00001270 struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001271 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1272 const struct drm_display_mode *adjusted_mode = &crtc_state->base.adjusted_mode;
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001273 const struct intel_sdvo_connector_state *sdvo_state =
1274 to_intel_sdvo_connector_state(conn_state);
1275 const struct drm_display_mode *mode = &crtc_state->base.mode;
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001276 struct intel_sdvo *intel_sdvo = to_sdvo(intel_encoder);
Chris Wilson6c9547f2010-08-25 10:05:17 +01001277 u32 sdvox;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001278 struct intel_sdvo_in_out_map in_out;
Daniel Vetter66518192012-04-01 19:16:18 +02001279 struct intel_sdvo_dtd input_dtd, output_dtd;
Chris Wilson6c9547f2010-08-25 10:05:17 +01001280 int rate;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001281
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001282 intel_sdvo_update_props(intel_sdvo, sdvo_state);
1283
Chris Wilsonc16336b2018-02-14 09:09:05 +00001284 /*
1285 * First, set the input mapping for the first input to our controlled
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001286 * output. This is only correct if we're a single-input device, in
1287 * which case the first input is the output from the appropriate SDVO
1288 * channel on the motherboard. In a two-input device, the first input
1289 * will be SDVOB and the second SDVOC.
1290 */
Chris Wilsonea5b2132010-08-04 13:50:23 +01001291 in_out.in0 = intel_sdvo->attached_output;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001292 in_out.in1 = 0;
1293
Pavel Roskinc74696b2010-09-02 14:46:34 -04001294 intel_sdvo_set_value(intel_sdvo,
1295 SDVO_CMD_SET_IN_OUT_MAP,
1296 &in_out, sizeof(in_out));
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001297
Chris Wilson6c9547f2010-08-25 10:05:17 +01001298 /* Set the output timings to the screen */
1299 if (!intel_sdvo_set_target_output(intel_sdvo,
1300 intel_sdvo->attached_output))
1301 return;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001302
Daniel Vetter66518192012-04-01 19:16:18 +02001303 /* lvds has a special fixed output timing. */
1304 if (intel_sdvo->is_lvds)
1305 intel_sdvo_get_dtd_from_mode(&output_dtd,
1306 intel_sdvo->sdvo_lvds_fixed_mode);
1307 else
1308 intel_sdvo_get_dtd_from_mode(&output_dtd, mode);
Daniel Vetterc8d4bb52012-04-10 13:55:48 +02001309 if (!intel_sdvo_set_output_timing(intel_sdvo, &output_dtd))
1310 DRM_INFO("Setting output timings on %s failed\n",
1311 SDVO_NAME(intel_sdvo));
Jesse Barnes79e53942008-11-07 14:24:08 -08001312
1313 /* Set the input timing to the screen. Assume always input 0. */
Chris Wilson32aad862010-08-04 13:50:25 +01001314 if (!intel_sdvo_set_target_input(intel_sdvo))
1315 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001316
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001317 if (crtc_state->has_hdmi_sink) {
Chris Wilson97aaf912011-01-04 20:10:52 +00001318 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_HDMI);
1319 intel_sdvo_set_colorimetry(intel_sdvo,
1320 SDVO_COLORIMETRY_RGB256);
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001321 intel_sdvo_set_avi_infoframe(intel_sdvo, crtc_state);
Chris Wilson97aaf912011-01-04 20:10:52 +00001322 } else
1323 intel_sdvo_set_encode(intel_sdvo, SDVO_ENCODE_DVI);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001324
Chris Wilson6c9547f2010-08-25 10:05:17 +01001325 if (intel_sdvo->is_tv &&
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001326 !intel_sdvo_set_tv_format(intel_sdvo, conn_state))
Chris Wilson6c9547f2010-08-25 10:05:17 +01001327 return;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001328
Daniel Vetter66518192012-04-01 19:16:18 +02001329 intel_sdvo_get_dtd_from_mode(&input_dtd, adjusted_mode);
Daniel Vettereeb47932013-09-03 20:40:36 +02001330
Egbert Eiche7518232012-10-13 14:29:31 +02001331 if (intel_sdvo->is_tv || intel_sdvo->is_lvds)
1332 input_dtd.part2.sdvo_flags = intel_sdvo->dtd_sdvo_flags;
Daniel Vetterc8d4bb52012-04-10 13:55:48 +02001333 if (!intel_sdvo_set_input_timing(intel_sdvo, &input_dtd))
1334 DRM_INFO("Setting input timings on %s failed\n",
1335 SDVO_NAME(intel_sdvo));
Jesse Barnes79e53942008-11-07 14:24:08 -08001336
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001337 switch (crtc_state->pixel_multiplier) {
Chris Wilson6c9547f2010-08-25 10:05:17 +01001338 default:
Yannick Guerrinifd0753c2015-02-28 17:20:41 +01001339 WARN(1, "unknown pixel multiplier specified\n");
Chris Wilson32aad862010-08-04 13:50:25 +01001340 case 1: rate = SDVO_CLOCK_RATE_MULT_1X; break;
1341 case 2: rate = SDVO_CLOCK_RATE_MULT_2X; break;
1342 case 4: rate = SDVO_CLOCK_RATE_MULT_4X; break;
Jesse Barnes79e53942008-11-07 14:24:08 -08001343 }
Chris Wilson32aad862010-08-04 13:50:25 +01001344 if (!intel_sdvo_set_clock_rate_mult(intel_sdvo, rate))
1345 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08001346
1347 /* Set the SDVO control regs. */
Tvrtko Ursulin66478472016-11-16 08:55:40 +00001348 if (INTEL_GEN(dev_priv) >= 4) {
Paulo Zanoniba68e082012-01-06 19:45:34 -02001349 /* The real mode polarity is set by the SDVO commands, using
1350 * struct intel_sdvo_dtd. */
1351 sdvox = SDVO_VSYNC_ACTIVE_HIGH | SDVO_HSYNC_ACTIVE_HIGH;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001352 if (!HAS_PCH_SPLIT(dev_priv) && crtc_state->limited_color_range)
Daniel Vetter69f5acc2014-04-24 23:54:48 +02001353 sdvox |= HDMI_COLOR_RANGE_16_235;
Tvrtko Ursulin66478472016-11-16 08:55:40 +00001354 if (INTEL_GEN(dev_priv) < 5)
Chris Wilson6714afb2010-12-17 04:10:51 +00001355 sdvox |= SDVO_BORDER_ENABLE;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001356 } else {
Chris Wilson6c9547f2010-08-25 10:05:17 +01001357 sdvox = I915_READ(intel_sdvo->sdvo_reg);
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02001358 if (intel_sdvo->port == PORT_B)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001359 sdvox &= SDVOB_PRESERVE_MASK;
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02001360 else
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001361 sdvox &= SDVOC_PRESERVE_MASK;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001362 sdvox |= (9 << 19) | SDVO_BORDER_ENABLE;
1363 }
Paulo Zanoni3573c412011-10-14 18:16:22 -03001364
Ville Syrjäläb9eb89b2017-06-20 16:03:06 +03001365 if (HAS_PCH_CPT(dev_priv))
Daniel Vettereeb47932013-09-03 20:40:36 +02001366 sdvox |= SDVO_PIPE_SEL_CPT(crtc->pipe);
Paulo Zanoni3573c412011-10-14 18:16:22 -03001367 else
Daniel Vettereeb47932013-09-03 20:40:36 +02001368 sdvox |= SDVO_PIPE_SEL(crtc->pipe);
Paulo Zanoni3573c412011-10-14 18:16:22 -03001369
Daniel Vetterde44e252017-07-26 21:32:51 +02001370 if (crtc_state->has_audio) {
1371 WARN_ON_ONCE(INTEL_GEN(dev_priv) < 4);
Chris Wilson6c9547f2010-08-25 10:05:17 +01001372 sdvox |= SDVO_AUDIO_ENABLE;
Daniel Vetterde44e252017-07-26 21:32:51 +02001373 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001374
Tvrtko Ursulin66478472016-11-16 08:55:40 +00001375 if (INTEL_GEN(dev_priv) >= 4) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001376 /* done in crtc_mode_set as the dpll_md reg must be written early */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001377 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
Jani Nikula73f67aa2016-12-07 22:48:09 +02001378 IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001379 /* done in crtc_mode_set as it lives inside the dpll register */
Jesse Barnes79e53942008-11-07 14:24:08 -08001380 } else {
Maarten Lankhorstf9fe0532016-08-09 17:04:10 +02001381 sdvox |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter6cc5f342013-03-27 00:44:53 +01001382 << SDVO_PORT_MULTIPLY_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08001383 }
1384
Chris Wilson6714afb2010-12-17 04:10:51 +00001385 if (input_dtd.part2.sdvo_flags & SDVO_NEED_TO_STALL &&
Tvrtko Ursulin66478472016-11-16 08:55:40 +00001386 INTEL_GEN(dev_priv) < 5)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001387 sdvox |= SDVO_STALL_SELECT;
Chris Wilsonea5b2132010-08-04 13:50:23 +01001388 intel_sdvo_write_sdvox(intel_sdvo, sdvox);
Jesse Barnes79e53942008-11-07 14:24:08 -08001389}
1390
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001391static bool intel_sdvo_connector_get_hw_state(struct intel_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001392{
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001393 struct intel_sdvo_connector *intel_sdvo_connector =
1394 to_intel_sdvo_connector(&connector->base);
1395 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(&connector->base);
Damien Lespiau2f28c502013-06-10 13:49:25 +01001396 u16 active_outputs = 0;
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001397
1398 intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
1399
1400 if (active_outputs & intel_sdvo_connector->output_flag)
1401 return true;
1402 else
1403 return false;
1404}
1405
1406static bool intel_sdvo_get_hw_state(struct intel_encoder *encoder,
1407 enum pipe *pipe)
1408{
1409 struct drm_device *dev = encoder->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001410 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001411 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Damien Lespiau2f28c502013-06-10 13:49:25 +01001412 u16 active_outputs = 0;
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001413 u32 tmp;
1414
1415 tmp = I915_READ(intel_sdvo->sdvo_reg);
Egbert Eich7a7d1fb2013-04-04 16:04:02 -04001416 intel_sdvo_get_active_outputs(intel_sdvo, &active_outputs);
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001417
Egbert Eich7a7d1fb2013-04-04 16:04:02 -04001418 if (!(tmp & SDVO_ENABLE) && (active_outputs == 0))
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001419 return false;
1420
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01001421 if (HAS_PCH_CPT(dev_priv))
Daniel Vetter4ac41f42012-07-02 14:54:00 +02001422 *pipe = PORT_TO_PIPE_CPT(tmp);
1423 else
1424 *pipe = PORT_TO_PIPE(tmp);
1425
1426 return true;
1427}
1428
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001429static void intel_sdvo_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001430 struct intel_crtc_state *pipe_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001431{
Daniel Vetter6c49f242013-06-06 12:45:25 +02001432 struct drm_device *dev = encoder->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001433 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001434 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001435 struct intel_sdvo_dtd dtd;
Daniel Vetter6c49f242013-06-06 12:45:25 +02001436 int encoder_pixel_multiplier = 0;
Ville Syrjälä18442d02013-09-13 16:00:08 +03001437 int dotclock;
Daniel Vetter6c49f242013-06-06 12:45:25 +02001438 u32 flags = 0, sdvox;
1439 u8 val;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001440 bool ret;
1441
Ville Syrjäläe1214b92017-10-27 22:31:23 +03001442 pipe_config->output_types |= BIT(INTEL_OUTPUT_SDVO);
1443
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02001444 sdvox = I915_READ(intel_sdvo->sdvo_reg);
1445
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001446 ret = intel_sdvo_get_input_timing(intel_sdvo, &dtd);
1447 if (!ret) {
Chris Wilsonc16336b2018-02-14 09:09:05 +00001448 /*
1449 * Some sdvo encoders are not spec compliant and don't
1450 * implement the mandatory get_timings function.
1451 */
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001452 DRM_DEBUG_DRIVER("failed to retrieve SDVO DTD\n");
Daniel Vetterbb760062013-06-06 14:55:52 +02001453 pipe_config->quirks |= PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS;
1454 } else {
1455 if (dtd.part2.dtd_flags & DTD_FLAG_HSYNC_POSITIVE)
1456 flags |= DRM_MODE_FLAG_PHSYNC;
1457 else
1458 flags |= DRM_MODE_FLAG_NHSYNC;
1459
1460 if (dtd.part2.dtd_flags & DTD_FLAG_VSYNC_POSITIVE)
1461 flags |= DRM_MODE_FLAG_PVSYNC;
1462 else
1463 flags |= DRM_MODE_FLAG_NVSYNC;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001464 }
1465
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02001466 pipe_config->base.adjusted_mode.flags |= flags;
Daniel Vetter6c49f242013-06-06 12:45:25 +02001467
Daniel Vetterfdafa9e2013-06-12 11:47:24 +02001468 /*
1469 * pixel multiplier readout is tricky: Only on i915g/gm it is stored in
1470 * the sdvo port register, on all other platforms it is part of the dpll
1471 * state. Since the general pipe state readout happens before the
1472 * encoder->get_config we so already have a valid pixel multplier on all
1473 * other platfroms.
1474 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001475 if (IS_I915G(dev_priv) || IS_I915GM(dev_priv)) {
Daniel Vetter6c49f242013-06-06 12:45:25 +02001476 pipe_config->pixel_multiplier =
1477 ((sdvox & SDVO_PORT_MULTIPLY_MASK)
1478 >> SDVO_PORT_MULTIPLY_SHIFT) + 1;
1479 }
1480
Ville Syrjälä2b858862014-06-09 16:20:46 +03001481 dotclock = pipe_config->port_clock;
Ville Syrjäläe3b247d2016-02-17 21:41:09 +02001482
Ville Syrjälä2b858862014-06-09 16:20:46 +03001483 if (pipe_config->pixel_multiplier)
1484 dotclock /= pipe_config->pixel_multiplier;
Ville Syrjälä18442d02013-09-13 16:00:08 +03001485
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02001486 pipe_config->base.adjusted_mode.crtc_clock = dotclock;
Ville Syrjälä18442d02013-09-13 16:00:08 +03001487
Daniel Vetter6c49f242013-06-06 12:45:25 +02001488 /* Cross check the port pixel multiplier with the sdvo encoder state. */
Damien Lespiau53b91402013-07-12 16:24:40 +01001489 if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_CLOCK_RATE_MULT,
1490 &val, 1)) {
1491 switch (val) {
1492 case SDVO_CLOCK_RATE_MULT_1X:
1493 encoder_pixel_multiplier = 1;
1494 break;
1495 case SDVO_CLOCK_RATE_MULT_2X:
1496 encoder_pixel_multiplier = 2;
1497 break;
1498 case SDVO_CLOCK_RATE_MULT_4X:
1499 encoder_pixel_multiplier = 4;
1500 break;
1501 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02001502 }
Daniel Vetterfdafa9e2013-06-12 11:47:24 +02001503
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02001504 if (sdvox & HDMI_COLOR_RANGE_16_235)
1505 pipe_config->limited_color_range = true;
1506
Daniel Vetterde44e252017-07-26 21:32:51 +02001507 if (sdvox & SDVO_AUDIO_ENABLE)
1508 pipe_config->has_audio = true;
1509
Daniel Vetter9f040032014-04-24 23:54:50 +02001510 if (intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_ENCODE,
1511 &val, 1)) {
1512 if (val == SDVO_ENCODE_HDMI)
1513 pipe_config->has_hdmi_sink = true;
1514 }
1515
Daniel Vetter6c49f242013-06-06 12:45:25 +02001516 WARN(encoder_pixel_multiplier != pipe_config->pixel_multiplier,
1517 "SDVO pixel multiplier mismatch, port: %i, encoder: %i\n",
1518 pipe_config->pixel_multiplier, encoder_pixel_multiplier);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07001519}
1520
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001521static void intel_disable_sdvo(struct intel_encoder *encoder,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001522 const struct intel_crtc_state *old_crtc_state,
1523 const struct drm_connector_state *conn_state)
Daniel Vetterce22c322012-07-01 15:31:04 +02001524{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001525 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001526 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Ville Syrjälä463320a2017-10-31 22:51:16 +02001527 struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08001528 u32 temp;
1529
Daniel Vetterce22c322012-07-01 15:31:04 +02001530 intel_sdvo_set_active_outputs(intel_sdvo, 0);
1531 if (0)
1532 intel_sdvo_set_encoder_power_state(intel_sdvo,
1533 DRM_MODE_DPMS_OFF);
1534
1535 temp = I915_READ(intel_sdvo->sdvo_reg);
Chris Wilson776ca7c2012-11-21 10:44:23 +00001536
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03001537 temp &= ~SDVO_ENABLE;
1538 intel_sdvo_write_sdvox(intel_sdvo, temp);
Chris Wilson776ca7c2012-11-21 10:44:23 +00001539
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03001540 /*
1541 * HW workaround for IBX, we need to move the port
1542 * to transcoder A after disabling it to allow the
1543 * matching DP port to be enabled on transcoder A.
1544 */
1545 if (HAS_PCH_IBX(dev_priv) && crtc->pipe == PIPE_B) {
Ville Syrjälä0c241d52015-10-30 19:23:22 +02001546 /*
1547 * We get CPU/PCH FIFO underruns on the other pipe when
1548 * doing the workaround. Sweep them under the rug.
1549 */
1550 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, false);
1551 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, false);
1552
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03001553 temp &= ~SDVO_PIPE_B_SELECT;
1554 temp |= SDVO_ENABLE;
1555 intel_sdvo_write_sdvox(intel_sdvo, temp);
Chris Wilson776ca7c2012-11-21 10:44:23 +00001556
Ville Syrjälä1612c8b2015-05-05 17:17:34 +03001557 temp &= ~SDVO_ENABLE;
1558 intel_sdvo_write_sdvox(intel_sdvo, temp);
Ville Syrjälä0c241d52015-10-30 19:23:22 +02001559
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02001560 intel_wait_for_vblank_if_active(dev_priv, PIPE_A);
Ville Syrjälä0c241d52015-10-30 19:23:22 +02001561 intel_set_cpu_fifo_underrun_reporting(dev_priv, PIPE_A, true);
1562 intel_set_pch_fifo_underrun_reporting(dev_priv, PIPE_A, true);
Daniel Vetterce22c322012-07-01 15:31:04 +02001563 }
1564}
1565
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001566static void pch_disable_sdvo(struct intel_encoder *encoder,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001567 const struct intel_crtc_state *old_crtc_state,
1568 const struct drm_connector_state *old_conn_state)
Ville Syrjälä3c65d1d2015-05-05 17:17:36 +03001569{
1570}
1571
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001572static void pch_post_disable_sdvo(struct intel_encoder *encoder,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001573 const struct intel_crtc_state *old_crtc_state,
1574 const struct drm_connector_state *old_conn_state)
Ville Syrjälä3c65d1d2015-05-05 17:17:36 +03001575{
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001576 intel_disable_sdvo(encoder, old_crtc_state, old_conn_state);
Ville Syrjälä3c65d1d2015-05-05 17:17:36 +03001577}
1578
Maarten Lankhorstfd6bbda2016-08-09 17:04:04 +02001579static void intel_enable_sdvo(struct intel_encoder *encoder,
Ville Syrjälä5f88a9c2017-08-18 16:49:58 +03001580 const struct intel_crtc_state *pipe_config,
1581 const struct drm_connector_state *conn_state)
Daniel Vetterce22c322012-07-01 15:31:04 +02001582{
1583 struct drm_device *dev = encoder->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +01001584 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001585 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Ville Syrjälä463320a2017-10-31 22:51:16 +02001586 struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
Daniel Vetterce22c322012-07-01 15:31:04 +02001587 u32 temp;
1588 bool input1, input2;
1589 int i;
Jani Nikulad0a7b6d2014-03-21 14:56:32 +02001590 bool success;
Daniel Vetterce22c322012-07-01 15:31:04 +02001591
1592 temp = I915_READ(intel_sdvo->sdvo_reg);
Ville Syrjälä3c65d1d2015-05-05 17:17:36 +03001593 temp |= SDVO_ENABLE;
1594 intel_sdvo_write_sdvox(intel_sdvo, temp);
Chris Wilson776ca7c2012-11-21 10:44:23 +00001595
Daniel Vetterce22c322012-07-01 15:31:04 +02001596 for (i = 0; i < 2; i++)
Ville Syrjälä0f0f74b2016-10-31 22:37:06 +02001597 intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
Daniel Vetterce22c322012-07-01 15:31:04 +02001598
Jani Nikulad0a7b6d2014-03-21 14:56:32 +02001599 success = intel_sdvo_get_trained_inputs(intel_sdvo, &input1, &input2);
Chris Wilsonc16336b2018-02-14 09:09:05 +00001600 /*
1601 * Warn if the device reported failure to sync.
1602 *
Daniel Vetterce22c322012-07-01 15:31:04 +02001603 * A lot of SDVO devices fail to notify of sync, but it's
1604 * a given it the status is a success, we succeeded.
1605 */
Jani Nikulad0a7b6d2014-03-21 14:56:32 +02001606 if (success && !input1) {
Daniel Vetterce22c322012-07-01 15:31:04 +02001607 DRM_DEBUG_KMS("First %s output reported failure to "
1608 "sync\n", SDVO_NAME(intel_sdvo));
1609 }
1610
1611 if (0)
1612 intel_sdvo_set_encoder_power_state(intel_sdvo,
1613 DRM_MODE_DPMS_ON);
1614 intel_sdvo_set_active_outputs(intel_sdvo, intel_sdvo->attached_output);
1615}
1616
Damien Lespiauc19de8e2013-11-28 15:29:18 +00001617static enum drm_mode_status
1618intel_sdvo_mode_valid(struct drm_connector *connector,
1619 struct drm_display_mode *mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08001620{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001621 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Mika Kahola24b23882016-02-02 15:16:41 +02001622 int max_dotclk = to_i915(connector->dev)->max_dotclk_freq;
Jesse Barnes79e53942008-11-07 14:24:08 -08001623
Chris Wilsonea5b2132010-08-04 13:50:23 +01001624 if (intel_sdvo->pixel_clock_min > mode->clock)
Jesse Barnes79e53942008-11-07 14:24:08 -08001625 return MODE_CLOCK_LOW;
1626
Chris Wilsonea5b2132010-08-04 13:50:23 +01001627 if (intel_sdvo->pixel_clock_max < mode->clock)
Jesse Barnes79e53942008-11-07 14:24:08 -08001628 return MODE_CLOCK_HIGH;
1629
Mika Kahola24b23882016-02-02 15:16:41 +02001630 if (mode->clock > max_dotclk)
1631 return MODE_CLOCK_HIGH;
1632
Chris Wilson85454232010-08-08 14:28:23 +01001633 if (intel_sdvo->is_lvds) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001634 if (mode->hdisplay > intel_sdvo->sdvo_lvds_fixed_mode->hdisplay)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001635 return MODE_PANEL;
1636
Chris Wilsonea5b2132010-08-04 13:50:23 +01001637 if (mode->vdisplay > intel_sdvo->sdvo_lvds_fixed_mode->vdisplay)
ling.ma@intel.com12682a92009-06-30 11:35:35 +08001638 return MODE_PANEL;
1639 }
1640
Jesse Barnes79e53942008-11-07 14:24:08 -08001641 return MODE_OK;
1642}
1643
Chris Wilsonea5b2132010-08-04 13:50:23 +01001644static bool intel_sdvo_get_capabilities(struct intel_sdvo *intel_sdvo, struct intel_sdvo_caps *caps)
Jesse Barnes79e53942008-11-07 14:24:08 -08001645{
Chris Wilson1a3665c2011-01-25 13:59:37 +00001646 BUILD_BUG_ON(sizeof(*caps) != 8);
Chris Wilsone957d772010-09-24 12:52:03 +01001647 if (!intel_sdvo_get_value(intel_sdvo,
1648 SDVO_CMD_GET_DEVICE_CAPS,
1649 caps, sizeof(*caps)))
1650 return false;
1651
1652 DRM_DEBUG_KMS("SDVO capabilities:\n"
1653 " vendor_id: %d\n"
1654 " device_id: %d\n"
1655 " device_rev_id: %d\n"
1656 " sdvo_version_major: %d\n"
1657 " sdvo_version_minor: %d\n"
1658 " sdvo_inputs_mask: %d\n"
1659 " smooth_scaling: %d\n"
1660 " sharp_scaling: %d\n"
1661 " up_scaling: %d\n"
1662 " down_scaling: %d\n"
1663 " stall_support: %d\n"
1664 " output_flags: %d\n",
1665 caps->vendor_id,
1666 caps->device_id,
1667 caps->device_rev_id,
1668 caps->sdvo_version_major,
1669 caps->sdvo_version_minor,
1670 caps->sdvo_inputs_mask,
1671 caps->smooth_scaling,
1672 caps->sharp_scaling,
1673 caps->up_scaling,
1674 caps->down_scaling,
1675 caps->stall_support,
1676 caps->output_flags);
1677
1678 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08001679}
1680
Jani Nikula5fa7ac92012-08-29 16:43:58 +03001681static uint16_t intel_sdvo_get_hotplug_support(struct intel_sdvo *intel_sdvo)
Jesse Barnes79e53942008-11-07 14:24:08 -08001682{
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001683 struct drm_i915_private *dev_priv = to_i915(intel_sdvo->base.base.dev);
Jani Nikula5fa7ac92012-08-29 16:43:58 +03001684 uint16_t hotplug;
Jesse Barnes79e53942008-11-07 14:24:08 -08001685
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001686 if (!I915_HAS_HOTPLUG(dev_priv))
Ville Syrjälä1d83d952015-01-09 14:21:15 +02001687 return 0;
1688
Chris Wilsonc16336b2018-02-14 09:09:05 +00001689 /*
1690 * HW Erratum: SDVO Hotplug is broken on all i945G chips, there's noise
1691 * on the line.
1692 */
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +01001693 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
Jani Nikula5fa7ac92012-08-29 16:43:58 +03001694 return 0;
Daniel Vetter768b1072012-05-04 11:29:56 +02001695
Jani Nikula5fa7ac92012-08-29 16:43:58 +03001696 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_HOT_PLUG_SUPPORT,
1697 &hotplug, sizeof(hotplug)))
1698 return 0;
1699
1700 return hotplug;
Jesse Barnes79e53942008-11-07 14:24:08 -08001701}
1702
Simon Farnsworthcc68c812011-09-21 17:13:30 +01001703static void intel_sdvo_enable_hotplug(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08001704{
Daniel Vetter8aca63a2013-07-21 21:37:01 +02001705 struct intel_sdvo *intel_sdvo = to_sdvo(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08001706
Jani Nikula5fa7ac92012-08-29 16:43:58 +03001707 intel_sdvo_write_cmd(intel_sdvo, SDVO_CMD_SET_ACTIVE_HOT_PLUG,
Ville Syrjälä1b2cb022018-01-17 21:21:45 +02001708 &intel_sdvo->hotplug_active, 2);
1709}
1710
1711static bool intel_sdvo_hotplug(struct intel_encoder *encoder,
1712 struct intel_connector *connector)
1713{
1714 intel_sdvo_enable_hotplug(encoder);
1715
1716 return intel_encoder_hotplug(encoder, connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001717}
1718
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001719static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01001720intel_sdvo_multifunc_encoder(struct intel_sdvo *intel_sdvo)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001721{
Chris Wilsonbc652122011-01-25 13:28:29 +00001722 /* Is there more than one type of output? */
Adam Jackson22944882011-06-16 16:36:24 -04001723 return hweight16(intel_sdvo->caps.output_flags) > 1;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001724}
1725
Chris Wilsonf899fc62010-07-20 15:44:45 -07001726static struct edid *
Chris Wilsone957d772010-09-24 12:52:03 +01001727intel_sdvo_get_edid(struct drm_connector *connector)
Chris Wilsonf899fc62010-07-20 15:44:45 -07001728{
Chris Wilsone957d772010-09-24 12:52:03 +01001729 struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
1730 return drm_get_edid(connector, &sdvo->ddc);
Chris Wilsonf899fc62010-07-20 15:44:45 -07001731}
1732
Chris Wilsonff482d82010-09-15 10:40:38 +01001733/* Mac mini hack -- use the same DDC as the analog connector */
1734static struct edid *
1735intel_sdvo_get_analog_edid(struct drm_connector *connector)
1736{
Chris Wilsonfac5e232016-07-04 11:34:36 +01001737 struct drm_i915_private *dev_priv = to_i915(connector->dev);
Chris Wilsonff482d82010-09-15 10:40:38 +01001738
Chris Wilson0c1dab82010-11-23 22:37:01 +00001739 return drm_get_edid(connector,
Daniel Kurtz3bd7d902012-03-28 02:36:14 +08001740 intel_gmbus_get_adapter(dev_priv,
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03001741 dev_priv->vbt.crt_ddc_pin));
Chris Wilsonff482d82010-09-15 10:40:38 +01001742}
1743
Ben Widawskyc43b5632012-04-16 14:07:40 -07001744static enum drm_connector_status
Adam Jackson8bf38482011-06-16 16:36:25 -04001745intel_sdvo_tmds_sink_detect(struct drm_connector *connector)
Ma Ling9dff6af2009-04-02 13:13:26 +08001746{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001747 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilson9d1a9032010-09-14 17:58:19 +01001748 enum drm_connector_status status;
1749 struct edid *edid;
Ma Ling9dff6af2009-04-02 13:13:26 +08001750
Chris Wilsone957d772010-09-24 12:52:03 +01001751 edid = intel_sdvo_get_edid(connector);
Keith Packard57cdaf92009-09-04 13:07:54 +08001752
Chris Wilsonea5b2132010-08-04 13:50:23 +01001753 if (edid == NULL && intel_sdvo_multifunc_encoder(intel_sdvo)) {
Chris Wilsone957d772010-09-24 12:52:03 +01001754 u8 ddc, saved_ddc = intel_sdvo->ddc_bus;
Chris Wilson9d1a9032010-09-14 17:58:19 +01001755
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001756 /*
1757 * Don't use the 1 as the argument of DDC bus switch to get
1758 * the EDID. It is used for SDVO SPD ROM.
1759 */
Chris Wilson9d1a9032010-09-14 17:58:19 +01001760 for (ddc = intel_sdvo->ddc_bus >> 1; ddc > 1; ddc >>= 1) {
Chris Wilsone957d772010-09-24 12:52:03 +01001761 intel_sdvo->ddc_bus = ddc;
1762 edid = intel_sdvo_get_edid(connector);
1763 if (edid)
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001764 break;
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001765 }
Chris Wilsone957d772010-09-24 12:52:03 +01001766 /*
1767 * If we found the EDID on the other bus,
1768 * assume that is the correct DDC bus.
1769 */
1770 if (edid == NULL)
1771 intel_sdvo->ddc_bus = saved_ddc;
Zhao Yakui7c3f0a22010-01-08 10:58:20 +08001772 }
Chris Wilson9d1a9032010-09-14 17:58:19 +01001773
1774 /*
1775 * When there is no edid and no monitor is connected with VGA
1776 * port, try to use the CRT ddc to read the EDID for DVI-connector.
Keith Packard57cdaf92009-09-04 13:07:54 +08001777 */
Chris Wilsonff482d82010-09-15 10:40:38 +01001778 if (edid == NULL)
1779 edid = intel_sdvo_get_analog_edid(connector);
Adam Jackson149c36a2010-04-29 14:05:18 -04001780
Chris Wilson2f551c82010-09-15 10:42:50 +01001781 status = connector_status_unknown;
Ma Ling9dff6af2009-04-02 13:13:26 +08001782 if (edid != NULL) {
Adam Jackson149c36a2010-04-29 14:05:18 -04001783 /* DDC bus is shared, match EDID to connector type */
Chris Wilson9d1a9032010-09-14 17:58:19 +01001784 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
1785 status = connector_status_connected;
Chris Wilsonda79de92010-11-22 11:12:46 +00001786 if (intel_sdvo->is_hdmi) {
1787 intel_sdvo->has_hdmi_monitor = drm_detect_hdmi_monitor(edid);
1788 intel_sdvo->has_hdmi_audio = drm_detect_monitor_audio(edid);
Ville Syrjäläabedc072013-01-17 16:31:31 +02001789 intel_sdvo->rgb_quant_range_selectable =
1790 drm_rgb_quant_range_selectable(edid);
Chris Wilsonda79de92010-11-22 11:12:46 +00001791 }
Chris Wilson139467432011-02-09 20:01:16 +00001792 } else
1793 status = connector_status_disconnected;
Chris Wilson9d1a9032010-09-14 17:58:19 +01001794 kfree(edid);
1795 }
Chris Wilson7f36e7e2010-09-19 09:29:33 +01001796
ling.ma@intel.com2b8d33f72009-07-29 11:31:18 +08001797 return status;
Ma Ling9dff6af2009-04-02 13:13:26 +08001798}
1799
Chris Wilson52220082011-06-20 14:45:50 +01001800static bool
1801intel_sdvo_connector_matches_edid(struct intel_sdvo_connector *sdvo,
1802 struct edid *edid)
1803{
1804 bool monitor_is_digital = !!(edid->input & DRM_EDID_INPUT_DIGITAL);
1805 bool connector_is_digital = !!IS_DIGITAL(sdvo);
1806
1807 DRM_DEBUG_KMS("connector_is_digital? %d, monitor_is_digital? %d\n",
1808 connector_is_digital, monitor_is_digital);
1809 return connector_is_digital == monitor_is_digital;
1810}
1811
Chris Wilson7b334fc2010-09-09 23:51:02 +01001812static enum drm_connector_status
Chris Wilson930a9e22010-09-14 11:07:23 +01001813intel_sdvo_detect(struct drm_connector *connector, bool force)
Jesse Barnes79e53942008-11-07 14:24:08 -08001814{
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001815 uint16_t response;
Chris Wilsondf0e9242010-09-09 16:20:55 +01001816 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilson615fb932010-08-04 13:50:24 +01001817 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Zhenyu Wang14571b42010-03-30 14:06:33 +08001818 enum drm_connector_status ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001819
Chris Wilson164c8592013-07-20 20:27:08 +01001820 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001821 connector->base.id, connector->name);
Chris Wilson164c8592013-07-20 20:27:08 +01001822
Chris Wilsonfc373812012-11-23 11:57:56 +00001823 if (!intel_sdvo_get_value(intel_sdvo,
1824 SDVO_CMD_GET_ATTACHED_DISPLAYS,
1825 &response, 2))
Chris Wilson32aad862010-08-04 13:50:25 +01001826 return connector_status_unknown;
Jesse Barnes79e53942008-11-07 14:24:08 -08001827
Chris Wilsone957d772010-09-24 12:52:03 +01001828 DRM_DEBUG_KMS("SDVO response %d %d [%x]\n",
1829 response & 0xff, response >> 8,
1830 intel_sdvo_connector->output_flag);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001831
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001832 if (response == 0)
Jesse Barnes79e53942008-11-07 14:24:08 -08001833 return connector_status_disconnected;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001834
Chris Wilsonea5b2132010-08-04 13:50:23 +01001835 intel_sdvo->attached_output = response;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001836
Chris Wilson97aaf912011-01-04 20:10:52 +00001837 intel_sdvo->has_hdmi_monitor = false;
1838 intel_sdvo->has_hdmi_audio = false;
Ville Syrjäläabedc072013-01-17 16:31:31 +02001839 intel_sdvo->rgb_quant_range_selectable = false;
Chris Wilson97aaf912011-01-04 20:10:52 +00001840
Chris Wilson615fb932010-08-04 13:50:24 +01001841 if ((intel_sdvo_connector->output_flag & response) == 0)
Zhenyu Wang14571b42010-03-30 14:06:33 +08001842 ret = connector_status_disconnected;
Chris Wilson139467432011-02-09 20:01:16 +00001843 else if (IS_TMDS(intel_sdvo_connector))
Adam Jackson8bf38482011-06-16 16:36:25 -04001844 ret = intel_sdvo_tmds_sink_detect(connector);
Chris Wilson139467432011-02-09 20:01:16 +00001845 else {
1846 struct edid *edid;
1847
1848 /* if we have an edid check it matches the connection */
1849 edid = intel_sdvo_get_edid(connector);
1850 if (edid == NULL)
1851 edid = intel_sdvo_get_analog_edid(connector);
1852 if (edid != NULL) {
Chris Wilson52220082011-06-20 14:45:50 +01001853 if (intel_sdvo_connector_matches_edid(intel_sdvo_connector,
1854 edid))
Chris Wilson139467432011-02-09 20:01:16 +00001855 ret = connector_status_connected;
Chris Wilson52220082011-06-20 14:45:50 +01001856 else
1857 ret = connector_status_disconnected;
1858
Chris Wilson139467432011-02-09 20:01:16 +00001859 kfree(edid);
1860 } else
1861 ret = connector_status_connected;
1862 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08001863
1864 /* May update encoder flag for like clock for SDVO TV, etc.*/
1865 if (ret == connector_status_connected) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01001866 intel_sdvo->is_tv = false;
1867 intel_sdvo->is_lvds = false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001868
Daniel Vetter09ede542013-04-30 14:01:45 +02001869 if (response & SDVO_TV_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01001870 intel_sdvo->is_tv = true;
Zhenyu Wang14571b42010-03-30 14:06:33 +08001871 if (response & SDVO_LVDS_MASK)
Chris Wilson85454232010-08-08 14:28:23 +01001872 intel_sdvo->is_lvds = intel_sdvo->sdvo_lvds_fixed_mode != NULL;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08001873 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08001874
1875 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001876}
1877
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001878static void intel_sdvo_get_ddc_modes(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08001879{
Chris Wilsonff482d82010-09-15 10:40:38 +01001880 struct edid *edid;
Jesse Barnes79e53942008-11-07 14:24:08 -08001881
Chris Wilson46a3f4a2013-09-24 12:55:40 +01001882 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001883 connector->base.id, connector->name);
Chris Wilson46a3f4a2013-09-24 12:55:40 +01001884
Jesse Barnes79e53942008-11-07 14:24:08 -08001885 /* set the bus switch and get the modes */
Chris Wilsone957d772010-09-24 12:52:03 +01001886 edid = intel_sdvo_get_edid(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08001887
Keith Packard57cdaf92009-09-04 13:07:54 +08001888 /*
1889 * Mac mini hack. On this device, the DVI-I connector shares one DDC
1890 * link between analog and digital outputs. So, if the regular SDVO
1891 * DDC fails, check to see if the analog output is disconnected, in
1892 * which case we'll look there for the digital DDC data.
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001893 */
Chris Wilsonf899fc62010-07-20 15:44:45 -07001894 if (edid == NULL)
1895 edid = intel_sdvo_get_analog_edid(connector);
1896
Chris Wilsonff482d82010-09-15 10:40:38 +01001897 if (edid != NULL) {
Chris Wilson52220082011-06-20 14:45:50 +01001898 if (intel_sdvo_connector_matches_edid(to_intel_sdvo_connector(connector),
1899 edid)) {
Chris Wilson0c1dab82010-11-23 22:37:01 +00001900 drm_mode_connector_update_edid_property(connector, edid);
1901 drm_add_edid_modes(connector, edid);
1902 }
Chris Wilson139467432011-02-09 20:01:16 +00001903
Chris Wilsonff482d82010-09-15 10:40:38 +01001904 kfree(edid);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001905 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001906}
1907
1908/*
1909 * Set of SDVO TV modes.
1910 * Note! This is in reply order (see loop in get_tv_modes).
1911 * XXX: all 60Hz refresh?
1912 */
Chris Wilsonb1f559e2011-01-26 09:49:47 +00001913static const struct drm_display_mode sdvo_tv_modes[] = {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001914 { DRM_MODE("320x200", DRM_MODE_TYPE_DRIVER, 5815, 320, 321, 384,
1915 416, 0, 200, 201, 232, 233, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001916 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001917 { DRM_MODE("320x240", DRM_MODE_TYPE_DRIVER, 6814, 320, 321, 384,
1918 416, 0, 240, 241, 272, 273, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001919 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001920 { DRM_MODE("400x300", DRM_MODE_TYPE_DRIVER, 9910, 400, 401, 464,
1921 496, 0, 300, 301, 332, 333, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001922 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001923 { DRM_MODE("640x350", DRM_MODE_TYPE_DRIVER, 16913, 640, 641, 704,
1924 736, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001925 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001926 { DRM_MODE("640x400", DRM_MODE_TYPE_DRIVER, 19121, 640, 641, 704,
1927 736, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001928 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001929 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 22654, 640, 641, 704,
1930 736, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001931 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001932 { DRM_MODE("704x480", DRM_MODE_TYPE_DRIVER, 24624, 704, 705, 768,
1933 800, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001934 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001935 { DRM_MODE("704x576", DRM_MODE_TYPE_DRIVER, 29232, 704, 705, 768,
1936 800, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001937 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001938 { DRM_MODE("720x350", DRM_MODE_TYPE_DRIVER, 18751, 720, 721, 784,
1939 816, 0, 350, 351, 382, 383, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001940 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001941 { DRM_MODE("720x400", DRM_MODE_TYPE_DRIVER, 21199, 720, 721, 784,
1942 816, 0, 400, 401, 432, 433, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001943 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001944 { DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 25116, 720, 721, 784,
1945 816, 0, 480, 481, 512, 513, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001946 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001947 { DRM_MODE("720x540", DRM_MODE_TYPE_DRIVER, 28054, 720, 721, 784,
1948 816, 0, 540, 541, 572, 573, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001949 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001950 { DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 29816, 720, 721, 784,
1951 816, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001952 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001953 { DRM_MODE("768x576", DRM_MODE_TYPE_DRIVER, 31570, 768, 769, 832,
1954 864, 0, 576, 577, 608, 609, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001955 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001956 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 34030, 800, 801, 864,
1957 896, 0, 600, 601, 632, 633, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001958 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001959 { DRM_MODE("832x624", DRM_MODE_TYPE_DRIVER, 36581, 832, 833, 896,
1960 928, 0, 624, 625, 656, 657, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001961 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001962 { DRM_MODE("920x766", DRM_MODE_TYPE_DRIVER, 48707, 920, 921, 984,
1963 1016, 0, 766, 767, 798, 799, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001964 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001965 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 53827, 1024, 1025, 1088,
1966 1120, 0, 768, 769, 800, 801, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001967 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001968 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 87265, 1280, 1281, 1344,
1969 1376, 0, 1024, 1025, 1056, 1057, 0,
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001970 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1971};
1972
1973static void intel_sdvo_get_tv_modes(struct drm_connector *connector)
1974{
Chris Wilsondf0e9242010-09-09 16:20:55 +01001975 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001976 const struct drm_connector_state *conn_state = connector->state;
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08001977 struct intel_sdvo_sdtv_resolution_request tv_res;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001978 uint32_t reply = 0, format_map = 0;
1979 int i;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001980
Chris Wilson46a3f4a2013-09-24 12:55:40 +01001981 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03001982 connector->base.id, connector->name);
Chris Wilson46a3f4a2013-09-24 12:55:40 +01001983
Chris Wilsonc16336b2018-02-14 09:09:05 +00001984 /*
1985 * Read the list of supported input resolutions for the selected TV
Jesse Barnese2f0ba92009-02-02 15:11:52 -08001986 * format.
1987 */
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02001988 format_map = 1 << conn_state->tv.mode;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001989 memcpy(&tv_res, &format_map,
Chris Wilson32aad862010-08-04 13:50:25 +01001990 min(sizeof(format_map), sizeof(struct intel_sdvo_sdtv_resolution_request)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08001991
Chris Wilson32aad862010-08-04 13:50:25 +01001992 if (!intel_sdvo_set_target_output(intel_sdvo, intel_sdvo->attached_output))
1993 return;
Zhao Yakuice6feab2009-08-24 13:50:26 +08001994
Chris Wilson32aad862010-08-04 13:50:25 +01001995 BUILD_BUG_ON(sizeof(tv_res) != 3);
Chris Wilsone957d772010-09-24 12:52:03 +01001996 if (!intel_sdvo_write_cmd(intel_sdvo,
1997 SDVO_CMD_GET_SDTV_RESOLUTION_SUPPORT,
Chris Wilson32aad862010-08-04 13:50:25 +01001998 &tv_res, sizeof(tv_res)))
1999 return;
2000 if (!intel_sdvo_read_response(intel_sdvo, &reply, 3))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002001 return;
2002
2003 for (i = 0; i < ARRAY_SIZE(sdvo_tv_modes); i++)
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08002004 if (reply & (1 << i)) {
2005 struct drm_display_mode *nmode;
2006 nmode = drm_mode_duplicate(connector->dev,
Chris Wilson32aad862010-08-04 13:50:25 +01002007 &sdvo_tv_modes[i]);
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08002008 if (nmode)
2009 drm_mode_probed_add(connector, nmode);
2010 }
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002011}
2012
Ma Ling7086c872009-05-13 11:20:06 +08002013static void intel_sdvo_get_lvds_modes(struct drm_connector *connector)
2014{
Chris Wilsondf0e9242010-09-09 16:20:55 +01002015 struct intel_sdvo *intel_sdvo = intel_attached_sdvo(connector);
Chris Wilsonfac5e232016-07-04 11:34:36 +01002016 struct drm_i915_private *dev_priv = to_i915(connector->dev);
ling.ma@intel.com12682a92009-06-30 11:35:35 +08002017 struct drm_display_mode *newmode;
Ma Ling7086c872009-05-13 11:20:06 +08002018
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002019 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03002020 connector->base.id, connector->name);
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002021
Ma Ling7086c872009-05-13 11:20:06 +08002022 /*
Daniel Vetterc3456fb2013-06-10 09:47:58 +02002023 * Fetch modes from VBT. For SDVO prefer the VBT mode since some
Dave Airlie4300a0f2013-06-27 20:40:44 +10002024 * SDVO->LVDS transcoders can't cope with the EDID mode.
Ma Ling7086c872009-05-13 11:20:06 +08002025 */
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03002026 if (dev_priv->vbt.sdvo_lvds_vbt_mode != NULL) {
Ma Ling7086c872009-05-13 11:20:06 +08002027 newmode = drm_mode_duplicate(connector->dev,
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03002028 dev_priv->vbt.sdvo_lvds_vbt_mode);
Ma Ling7086c872009-05-13 11:20:06 +08002029 if (newmode != NULL) {
2030 /* Guarantee the mode is preferred */
2031 newmode->type = (DRM_MODE_TYPE_PREFERRED |
2032 DRM_MODE_TYPE_DRIVER);
2033 drm_mode_probed_add(connector, newmode);
2034 }
2035 }
ling.ma@intel.com12682a92009-06-30 11:35:35 +08002036
Dave Airlie4300a0f2013-06-27 20:40:44 +10002037 /*
2038 * Attempt to get the mode list from DDC.
2039 * Assume that the preferred modes are
2040 * arranged in priority order.
2041 */
2042 intel_ddc_get_modes(connector, &intel_sdvo->ddc);
2043
ling.ma@intel.com12682a92009-06-30 11:35:35 +08002044 list_for_each_entry(newmode, &connector->probed_modes, head) {
2045 if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002046 intel_sdvo->sdvo_lvds_fixed_mode =
ling.ma@intel.com12682a92009-06-30 11:35:35 +08002047 drm_mode_duplicate(connector->dev, newmode);
Chris Wilson6c9547f2010-08-25 10:05:17 +01002048
Chris Wilson85454232010-08-08 14:28:23 +01002049 intel_sdvo->is_lvds = true;
ling.ma@intel.com12682a92009-06-30 11:35:35 +08002050 break;
2051 }
2052 }
Ma Ling7086c872009-05-13 11:20:06 +08002053}
2054
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002055static int intel_sdvo_get_modes(struct drm_connector *connector)
2056{
Chris Wilson615fb932010-08-04 13:50:24 +01002057 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002058
Chris Wilson615fb932010-08-04 13:50:24 +01002059 if (IS_TV(intel_sdvo_connector))
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002060 intel_sdvo_get_tv_modes(connector);
Chris Wilson615fb932010-08-04 13:50:24 +01002061 else if (IS_LVDS(intel_sdvo_connector))
Ma Ling7086c872009-05-13 11:20:06 +08002062 intel_sdvo_get_lvds_modes(connector);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002063 else
2064 intel_sdvo_get_ddc_modes(connector);
2065
Chris Wilson32aad862010-08-04 13:50:25 +01002066 return !list_empty(&connector->probed_modes);
Jesse Barnes79e53942008-11-07 14:24:08 -08002067}
2068
2069static void intel_sdvo_destroy(struct drm_connector *connector)
2070{
Chris Wilson615fb932010-08-04 13:50:24 +01002071 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08002072
Jesse Barnes79e53942008-11-07 14:24:08 -08002073 drm_connector_cleanup(connector);
Jani Nikula4b745b12012-11-12 18:31:36 +02002074 kfree(intel_sdvo_connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08002075}
2076
Zhao Yakuice6feab2009-08-24 13:50:26 +08002077static int
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002078intel_sdvo_connector_atomic_get_property(struct drm_connector *connector,
2079 const struct drm_connector_state *state,
2080 struct drm_property *property,
2081 uint64_t *val)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002082{
Chris Wilson615fb932010-08-04 13:50:24 +01002083 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002084 const struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state((void *)state);
Chris Wilsonc5521702010-08-04 13:50:28 +01002085
2086 if (property == intel_sdvo_connector->tv_format) {
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002087 int i;
Chris Wilson32aad862010-08-04 13:50:25 +01002088
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002089 for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
2090 if (state->tv.mode == intel_sdvo_connector->tv_format_supported[i]) {
2091 *val = i;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002092
Chris Wilson32aad862010-08-04 13:50:25 +01002093 return 0;
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002094 }
Zhao Yakuib9219c52009-09-10 15:45:46 +08002095
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002096 WARN_ON(1);
2097 *val = 0;
2098 } else if (property == intel_sdvo_connector->top ||
2099 property == intel_sdvo_connector->bottom)
2100 *val = intel_sdvo_connector->max_vscan - sdvo_state->tv.overscan_v;
2101 else if (property == intel_sdvo_connector->left ||
2102 property == intel_sdvo_connector->right)
2103 *val = intel_sdvo_connector->max_hscan - sdvo_state->tv.overscan_h;
2104 else if (property == intel_sdvo_connector->hpos)
2105 *val = sdvo_state->tv.hpos;
2106 else if (property == intel_sdvo_connector->vpos)
2107 *val = sdvo_state->tv.vpos;
2108 else if (property == intel_sdvo_connector->saturation)
2109 *val = state->tv.saturation;
2110 else if (property == intel_sdvo_connector->contrast)
2111 *val = state->tv.contrast;
2112 else if (property == intel_sdvo_connector->hue)
2113 *val = state->tv.hue;
2114 else if (property == intel_sdvo_connector->brightness)
2115 *val = state->tv.brightness;
2116 else if (property == intel_sdvo_connector->sharpness)
2117 *val = sdvo_state->tv.sharpness;
2118 else if (property == intel_sdvo_connector->flicker_filter)
2119 *val = sdvo_state->tv.flicker_filter;
2120 else if (property == intel_sdvo_connector->flicker_filter_2d)
2121 *val = sdvo_state->tv.flicker_filter_2d;
2122 else if (property == intel_sdvo_connector->flicker_filter_adaptive)
2123 *val = sdvo_state->tv.flicker_filter_adaptive;
2124 else if (property == intel_sdvo_connector->tv_chroma_filter)
2125 *val = sdvo_state->tv.chroma_filter;
2126 else if (property == intel_sdvo_connector->tv_luma_filter)
2127 *val = sdvo_state->tv.luma_filter;
2128 else if (property == intel_sdvo_connector->dot_crawl)
2129 *val = sdvo_state->tv.dot_crawl;
2130 else
2131 return intel_digital_connector_atomic_get_property(connector, state, property, val);
Chris Wilsonc5521702010-08-04 13:50:28 +01002132
Chris Wilson32aad862010-08-04 13:50:25 +01002133 return 0;
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002134}
2135
2136static int
2137intel_sdvo_connector_atomic_set_property(struct drm_connector *connector,
2138 struct drm_connector_state *state,
2139 struct drm_property *property,
2140 uint64_t val)
2141{
2142 struct intel_sdvo_connector *intel_sdvo_connector = to_intel_sdvo_connector(connector);
2143 struct intel_sdvo_connector_state *sdvo_state = to_intel_sdvo_connector_state(state);
2144
2145 if (property == intel_sdvo_connector->tv_format) {
2146 state->tv.mode = intel_sdvo_connector->tv_format_supported[val];
2147
2148 if (state->crtc) {
2149 struct drm_crtc_state *crtc_state =
2150 drm_atomic_get_new_crtc_state(state->state, state->crtc);
2151
2152 crtc_state->connectors_changed = true;
2153 }
2154 } else if (property == intel_sdvo_connector->top ||
2155 property == intel_sdvo_connector->bottom)
2156 /* Cannot set these independent from each other */
2157 sdvo_state->tv.overscan_v = intel_sdvo_connector->max_vscan - val;
2158 else if (property == intel_sdvo_connector->left ||
2159 property == intel_sdvo_connector->right)
2160 /* Cannot set these independent from each other */
2161 sdvo_state->tv.overscan_h = intel_sdvo_connector->max_hscan - val;
2162 else if (property == intel_sdvo_connector->hpos)
2163 sdvo_state->tv.hpos = val;
2164 else if (property == intel_sdvo_connector->vpos)
2165 sdvo_state->tv.vpos = val;
2166 else if (property == intel_sdvo_connector->saturation)
2167 state->tv.saturation = val;
2168 else if (property == intel_sdvo_connector->contrast)
2169 state->tv.contrast = val;
2170 else if (property == intel_sdvo_connector->hue)
2171 state->tv.hue = val;
2172 else if (property == intel_sdvo_connector->brightness)
2173 state->tv.brightness = val;
2174 else if (property == intel_sdvo_connector->sharpness)
2175 sdvo_state->tv.sharpness = val;
2176 else if (property == intel_sdvo_connector->flicker_filter)
2177 sdvo_state->tv.flicker_filter = val;
2178 else if (property == intel_sdvo_connector->flicker_filter_2d)
2179 sdvo_state->tv.flicker_filter_2d = val;
2180 else if (property == intel_sdvo_connector->flicker_filter_adaptive)
2181 sdvo_state->tv.flicker_filter_adaptive = val;
2182 else if (property == intel_sdvo_connector->tv_chroma_filter)
2183 sdvo_state->tv.chroma_filter = val;
2184 else if (property == intel_sdvo_connector->tv_luma_filter)
2185 sdvo_state->tv.luma_filter = val;
2186 else if (property == intel_sdvo_connector->dot_crawl)
2187 sdvo_state->tv.dot_crawl = val;
2188 else
2189 return intel_digital_connector_atomic_set_property(connector, state, property, val);
2190
2191 return 0;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002192}
2193
Chris Wilson7a418e32016-06-24 14:00:14 +01002194static int
2195intel_sdvo_connector_register(struct drm_connector *connector)
2196{
2197 struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
Chris Wilson1ebaa0b2016-06-24 14:00:15 +01002198 int ret;
2199
2200 ret = intel_connector_register(connector);
2201 if (ret)
2202 return ret;
Chris Wilson7a418e32016-06-24 14:00:14 +01002203
2204 return sysfs_create_link(&connector->kdev->kobj,
2205 &sdvo->ddc.dev.kobj,
2206 sdvo->ddc.dev.kobj.name);
2207}
2208
Chris Wilsonc191eca2016-06-17 11:40:33 +01002209static void
2210intel_sdvo_connector_unregister(struct drm_connector *connector)
2211{
2212 struct intel_sdvo *sdvo = intel_attached_sdvo(connector);
2213
2214 sysfs_remove_link(&connector->kdev->kobj,
2215 sdvo->ddc.dev.kobj.name);
2216 intel_connector_unregister(connector);
2217}
2218
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002219static struct drm_connector_state *
2220intel_sdvo_connector_duplicate_state(struct drm_connector *connector)
2221{
2222 struct intel_sdvo_connector_state *state;
2223
2224 state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
2225 if (!state)
2226 return NULL;
2227
2228 __drm_atomic_helper_connector_duplicate_state(connector, &state->base.base);
2229 return &state->base.base;
2230}
2231
Jesse Barnes79e53942008-11-07 14:24:08 -08002232static const struct drm_connector_funcs intel_sdvo_connector_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08002233 .detect = intel_sdvo_detect,
2234 .fill_modes = drm_helper_probe_single_connector_modes,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002235 .atomic_get_property = intel_sdvo_connector_atomic_get_property,
2236 .atomic_set_property = intel_sdvo_connector_atomic_set_property,
Chris Wilson7a418e32016-06-24 14:00:14 +01002237 .late_register = intel_sdvo_connector_register,
Chris Wilsonc191eca2016-06-17 11:40:33 +01002238 .early_unregister = intel_sdvo_connector_unregister,
Jesse Barnes79e53942008-11-07 14:24:08 -08002239 .destroy = intel_sdvo_destroy,
Matt Roperc6f95f22015-01-22 16:50:32 -08002240 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002241 .atomic_duplicate_state = intel_sdvo_connector_duplicate_state,
Jesse Barnes79e53942008-11-07 14:24:08 -08002242};
2243
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002244static int intel_sdvo_atomic_check(struct drm_connector *conn,
2245 struct drm_connector_state *new_conn_state)
2246{
2247 struct drm_atomic_state *state = new_conn_state->state;
2248 struct drm_connector_state *old_conn_state =
2249 drm_atomic_get_old_connector_state(state, conn);
2250 struct intel_sdvo_connector_state *old_state =
2251 to_intel_sdvo_connector_state(old_conn_state);
2252 struct intel_sdvo_connector_state *new_state =
2253 to_intel_sdvo_connector_state(new_conn_state);
2254
2255 if (new_conn_state->crtc &&
2256 (memcmp(&old_state->tv, &new_state->tv, sizeof(old_state->tv)) ||
2257 memcmp(&old_conn_state->tv, &new_conn_state->tv, sizeof(old_conn_state->tv)))) {
2258 struct drm_crtc_state *crtc_state =
2259 drm_atomic_get_new_crtc_state(new_conn_state->state,
2260 new_conn_state->crtc);
2261
2262 crtc_state->connectors_changed = true;
2263 }
2264
2265 return intel_digital_connector_atomic_check(conn, new_conn_state);
2266}
2267
Jesse Barnes79e53942008-11-07 14:24:08 -08002268static const struct drm_connector_helper_funcs intel_sdvo_connector_helper_funcs = {
2269 .get_modes = intel_sdvo_get_modes,
2270 .mode_valid = intel_sdvo_mode_valid,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002271 .atomic_check = intel_sdvo_atomic_check,
Jesse Barnes79e53942008-11-07 14:24:08 -08002272};
2273
Hannes Ederb358d0a2008-12-18 21:18:47 +01002274static void intel_sdvo_enc_destroy(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08002275{
Daniel Vetter8aca63a2013-07-21 21:37:01 +02002276 struct intel_sdvo *intel_sdvo = to_sdvo(to_intel_encoder(encoder));
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002277
Chris Wilsonea5b2132010-08-04 13:50:23 +01002278 if (intel_sdvo->sdvo_lvds_fixed_mode != NULL)
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002279 drm_mode_destroy(encoder->dev,
Chris Wilsonea5b2132010-08-04 13:50:23 +01002280 intel_sdvo->sdvo_lvds_fixed_mode);
Zhenyu Wangd2a82a62010-03-29 21:22:55 +08002281
Chris Wilsone957d772010-09-24 12:52:03 +01002282 i2c_del_adapter(&intel_sdvo->ddc);
Chris Wilsonea5b2132010-08-04 13:50:23 +01002283 intel_encoder_destroy(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08002284}
2285
2286static const struct drm_encoder_funcs intel_sdvo_enc_funcs = {
2287 .destroy = intel_sdvo_enc_destroy,
2288};
2289
Chris Wilsonb66d8422010-08-12 15:26:41 +01002290static void
2291intel_sdvo_guess_ddc_bus(struct intel_sdvo *sdvo)
2292{
2293 uint16_t mask = 0;
2294 unsigned int num_bits;
2295
Chris Wilsonc16336b2018-02-14 09:09:05 +00002296 /*
2297 * Make a mask of outputs less than or equal to our own priority in the
Chris Wilsonb66d8422010-08-12 15:26:41 +01002298 * list.
2299 */
2300 switch (sdvo->controlled_output) {
2301 case SDVO_OUTPUT_LVDS1:
2302 mask |= SDVO_OUTPUT_LVDS1;
2303 case SDVO_OUTPUT_LVDS0:
2304 mask |= SDVO_OUTPUT_LVDS0;
2305 case SDVO_OUTPUT_TMDS1:
2306 mask |= SDVO_OUTPUT_TMDS1;
2307 case SDVO_OUTPUT_TMDS0:
2308 mask |= SDVO_OUTPUT_TMDS0;
2309 case SDVO_OUTPUT_RGB1:
2310 mask |= SDVO_OUTPUT_RGB1;
2311 case SDVO_OUTPUT_RGB0:
2312 mask |= SDVO_OUTPUT_RGB0;
2313 break;
2314 }
2315
2316 /* Count bits to find what number we are in the priority list. */
2317 mask &= sdvo->caps.output_flags;
2318 num_bits = hweight16(mask);
2319 /* If more than 3 outputs, default to DDC bus 3 for now. */
2320 if (num_bits > 3)
2321 num_bits = 3;
2322
2323 /* Corresponds to SDVO_CONTROL_BUS_DDCx */
2324 sdvo->ddc_bus = 1 << num_bits;
2325}
Jesse Barnes79e53942008-11-07 14:24:08 -08002326
Chris Wilsonc16336b2018-02-14 09:09:05 +00002327/*
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002328 * Choose the appropriate DDC bus for control bus switch command for this
2329 * SDVO output based on the controlled output.
2330 *
2331 * DDC bus number assignment is in a priority order of RGB outputs, then TMDS
2332 * outputs, then LVDS outputs.
2333 */
2334static void
Adam Jacksonb1083332010-04-23 16:07:40 -04002335intel_sdvo_select_ddc_bus(struct drm_i915_private *dev_priv,
Ville Syrjälä8bd864b2015-09-18 20:03:14 +03002336 struct intel_sdvo *sdvo)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002337{
Adam Jacksonb1083332010-04-23 16:07:40 -04002338 struct sdvo_device_mapping *mapping;
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002339
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02002340 if (sdvo->port == PORT_B)
Jani Nikula9d6c8752016-03-24 17:50:22 +02002341 mapping = &dev_priv->vbt.sdvo_mappings[0];
Adam Jacksonb1083332010-04-23 16:07:40 -04002342 else
Jani Nikula9d6c8752016-03-24 17:50:22 +02002343 mapping = &dev_priv->vbt.sdvo_mappings[1];
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002344
Chris Wilsonb66d8422010-08-12 15:26:41 +01002345 if (mapping->initialized)
2346 sdvo->ddc_bus = 1 << ((mapping->ddc_pin & 0xf0) >> 4);
2347 else
2348 intel_sdvo_guess_ddc_bus(sdvo);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002349}
2350
Chris Wilsone957d772010-09-24 12:52:03 +01002351static void
2352intel_sdvo_select_i2c_bus(struct drm_i915_private *dev_priv,
Ville Syrjälä8bd864b2015-09-18 20:03:14 +03002353 struct intel_sdvo *sdvo)
Chris Wilsone957d772010-09-24 12:52:03 +01002354{
2355 struct sdvo_device_mapping *mapping;
Adam Jackson46eb3032011-06-16 16:36:23 -04002356 u8 pin;
Chris Wilsone957d772010-09-24 12:52:03 +01002357
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02002358 if (sdvo->port == PORT_B)
Jani Nikula9d6c8752016-03-24 17:50:22 +02002359 mapping = &dev_priv->vbt.sdvo_mappings[0];
Chris Wilsone957d772010-09-24 12:52:03 +01002360 else
Jani Nikula9d6c8752016-03-24 17:50:22 +02002361 mapping = &dev_priv->vbt.sdvo_mappings[1];
Chris Wilsone957d772010-09-24 12:52:03 +01002362
Jani Nikula88ac7932015-03-27 00:20:22 +02002363 if (mapping->initialized &&
2364 intel_gmbus_is_valid_pin(dev_priv, mapping->i2c_pin))
Chris Wilsone957d772010-09-24 12:52:03 +01002365 pin = mapping->i2c_pin;
Jani Nikula6cb16122012-10-22 16:12:17 +03002366 else
Jani Nikula988c7012015-03-27 00:20:19 +02002367 pin = GMBUS_PIN_DPB;
Chris Wilsone957d772010-09-24 12:52:03 +01002368
Jani Nikula6cb16122012-10-22 16:12:17 +03002369 sdvo->i2c = intel_gmbus_get_adapter(dev_priv, pin);
2370
Chris Wilsonc16336b2018-02-14 09:09:05 +00002371 /*
2372 * With gmbus we should be able to drive sdvo i2c at 2MHz, but somehow
Jani Nikula6cb16122012-10-22 16:12:17 +03002373 * our code totally fails once we start using gmbus. Hence fall back to
Chris Wilsonc16336b2018-02-14 09:09:05 +00002374 * bit banging for now.
2375 */
Jani Nikula6cb16122012-10-22 16:12:17 +03002376 intel_gmbus_force_bit(sdvo->i2c, true);
Chris Wilsone957d772010-09-24 12:52:03 +01002377}
2378
Jani Nikulafbfcc4f2012-10-22 16:12:18 +03002379/* undo any changes intel_sdvo_select_i2c_bus() did to sdvo->i2c */
2380static void
2381intel_sdvo_unselect_i2c_bus(struct intel_sdvo *sdvo)
2382{
2383 intel_gmbus_force_bit(sdvo->i2c, false);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002384}
2385
2386static bool
Chris Wilsone27d8532010-10-22 09:15:22 +01002387intel_sdvo_is_hdmi_connector(struct intel_sdvo *intel_sdvo, int device)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002388{
Chris Wilson97aaf912011-01-04 20:10:52 +00002389 return intel_sdvo_check_supp_encode(intel_sdvo);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08002390}
2391
yakui_zhao714605e2009-05-31 17:18:07 +08002392static u8
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02002393intel_sdvo_get_slave_addr(struct drm_i915_private *dev_priv,
2394 struct intel_sdvo *sdvo)
yakui_zhao714605e2009-05-31 17:18:07 +08002395{
yakui_zhao714605e2009-05-31 17:18:07 +08002396 struct sdvo_device_mapping *my_mapping, *other_mapping;
2397
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02002398 if (sdvo->port == PORT_B) {
Jani Nikula9d6c8752016-03-24 17:50:22 +02002399 my_mapping = &dev_priv->vbt.sdvo_mappings[0];
2400 other_mapping = &dev_priv->vbt.sdvo_mappings[1];
yakui_zhao714605e2009-05-31 17:18:07 +08002401 } else {
Jani Nikula9d6c8752016-03-24 17:50:22 +02002402 my_mapping = &dev_priv->vbt.sdvo_mappings[1];
2403 other_mapping = &dev_priv->vbt.sdvo_mappings[0];
yakui_zhao714605e2009-05-31 17:18:07 +08002404 }
2405
2406 /* If the BIOS described our SDVO device, take advantage of it. */
2407 if (my_mapping->slave_addr)
2408 return my_mapping->slave_addr;
2409
Chris Wilsonc16336b2018-02-14 09:09:05 +00002410 /*
2411 * If the BIOS only described a different SDVO device, use the
yakui_zhao714605e2009-05-31 17:18:07 +08002412 * address that it isn't using.
2413 */
2414 if (other_mapping->slave_addr) {
2415 if (other_mapping->slave_addr == 0x70)
2416 return 0x72;
2417 else
2418 return 0x70;
2419 }
2420
Chris Wilsonc16336b2018-02-14 09:09:05 +00002421 /*
2422 * No SDVO device info is found for another DVO port,
yakui_zhao714605e2009-05-31 17:18:07 +08002423 * so use mapping assumption we had before BIOS parsing.
2424 */
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02002425 if (sdvo->port == PORT_B)
yakui_zhao714605e2009-05-31 17:18:07 +08002426 return 0x70;
2427 else
2428 return 0x72;
2429}
2430
Imre Deakc3934542014-02-11 17:12:50 +02002431static int
Chris Wilsondf0e9242010-09-09 16:20:55 +01002432intel_sdvo_connector_init(struct intel_sdvo_connector *connector,
2433 struct intel_sdvo *encoder)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002434{
Imre Deakc3934542014-02-11 17:12:50 +02002435 struct drm_connector *drm_connector;
2436 int ret;
2437
2438 drm_connector = &connector->base.base;
2439 ret = drm_connector_init(encoder->base.base.dev,
2440 drm_connector,
Chris Wilsondf0e9242010-09-09 16:20:55 +01002441 &intel_sdvo_connector_funcs,
2442 connector->base.base.connector_type);
Imre Deakc3934542014-02-11 17:12:50 +02002443 if (ret < 0)
2444 return ret;
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002445
Imre Deakc3934542014-02-11 17:12:50 +02002446 drm_connector_helper_add(drm_connector,
Chris Wilsondf0e9242010-09-09 16:20:55 +01002447 &intel_sdvo_connector_helper_funcs);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002448
Peter Ross8f4839e2012-01-28 14:49:25 +01002449 connector->base.base.interlace_allowed = 1;
Chris Wilsondf0e9242010-09-09 16:20:55 +01002450 connector->base.base.doublescan_allowed = 0;
2451 connector->base.base.display_info.subpixel_order = SubPixelHorizontalRGB;
Daniel Vetter4ac41f42012-07-02 14:54:00 +02002452 connector->base.get_hw_state = intel_sdvo_connector_get_hw_state;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002453
Chris Wilsondf0e9242010-09-09 16:20:55 +01002454 intel_connector_attach_encoder(&connector->base, &encoder->base);
Imre Deakc3934542014-02-11 17:12:50 +02002455
2456 return 0;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002457}
2458
Chris Wilson7f36e7e2010-09-19 09:29:33 +01002459static void
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02002460intel_sdvo_add_hdmi_properties(struct intel_sdvo *intel_sdvo,
2461 struct intel_sdvo_connector *connector)
Chris Wilson7f36e7e2010-09-19 09:29:33 +01002462{
Ville Syrjälä646d5772016-10-31 22:37:14 +02002463 struct drm_i915_private *dev_priv = to_i915(connector->base.base.dev);
Chris Wilson7f36e7e2010-09-19 09:29:33 +01002464
Chris Wilson3f43c482011-05-12 22:17:24 +01002465 intel_attach_force_audio_property(&connector->base.base);
Ville Syrjälä646d5772016-10-31 22:37:14 +02002466 if (INTEL_GEN(dev_priv) >= 4 && IS_MOBILE(dev_priv)) {
Chris Wilsone953fd72011-02-21 22:23:52 +00002467 intel_attach_broadcast_rgb_property(&connector->base.base);
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02002468 }
Ville Syrjälä7949dd42015-09-25 16:39:30 +03002469 intel_attach_aspect_ratio_property(&connector->base.base);
Maarten Lankhorst0e9f25d2017-05-01 15:37:53 +02002470 connector->base.base.state->picture_aspect_ratio = HDMI_PICTURE_ASPECT_NONE;
Chris Wilson7f36e7e2010-09-19 09:29:33 +01002471}
2472
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002473static struct intel_sdvo_connector *intel_sdvo_connector_alloc(void)
2474{
2475 struct intel_sdvo_connector *sdvo_connector;
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002476 struct intel_sdvo_connector_state *conn_state;
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002477
2478 sdvo_connector = kzalloc(sizeof(*sdvo_connector), GFP_KERNEL);
2479 if (!sdvo_connector)
2480 return NULL;
2481
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002482 conn_state = kzalloc(sizeof(*conn_state), GFP_KERNEL);
2483 if (!conn_state) {
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002484 kfree(sdvo_connector);
2485 return NULL;
2486 }
2487
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002488 __drm_atomic_helper_connector_reset(&sdvo_connector->base.base,
2489 &conn_state->base.base);
2490
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002491 return sdvo_connector;
2492}
2493
Zhenyu Wang14571b42010-03-30 14:06:33 +08002494static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002495intel_sdvo_dvi_init(struct intel_sdvo *intel_sdvo, int device)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002496{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002497 struct drm_encoder *encoder = &intel_sdvo->base.base;
Daniel Vetterde44e252017-07-26 21:32:51 +02002498 struct drm_i915_private *dev_priv = to_i915(encoder->dev);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002499 struct drm_connector *connector;
Simon Farnsworthcc68c812011-09-21 17:13:30 +01002500 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002501 struct intel_connector *intel_connector;
Chris Wilson615fb932010-08-04 13:50:24 +01002502 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002503
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002504 DRM_DEBUG_KMS("initialising DVI device %d\n", device);
2505
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002506 intel_sdvo_connector = intel_sdvo_connector_alloc();
Chris Wilson615fb932010-08-04 13:50:24 +01002507 if (!intel_sdvo_connector)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002508 return false;
2509
Zhenyu Wang14571b42010-03-30 14:06:33 +08002510 if (device == 0) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002511 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS0;
Chris Wilson615fb932010-08-04 13:50:24 +01002512 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS0;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002513 } else if (device == 1) {
Chris Wilsonea5b2132010-08-04 13:50:23 +01002514 intel_sdvo->controlled_output |= SDVO_OUTPUT_TMDS1;
Chris Wilson615fb932010-08-04 13:50:24 +01002515 intel_sdvo_connector->output_flag = SDVO_OUTPUT_TMDS1;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002516 }
2517
Chris Wilson615fb932010-08-04 13:50:24 +01002518 intel_connector = &intel_sdvo_connector->base;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002519 connector = &intel_connector->base;
Jani Nikula5fa7ac92012-08-29 16:43:58 +03002520 if (intel_sdvo_get_hotplug_support(intel_sdvo) &
2521 intel_sdvo_connector->output_flag) {
Jani Nikula5fa7ac92012-08-29 16:43:58 +03002522 intel_sdvo->hotplug_active |= intel_sdvo_connector->output_flag;
Chris Wilsonc16336b2018-02-14 09:09:05 +00002523 /*
2524 * Some SDVO devices have one-shot hotplug interrupts.
Simon Farnsworthcc68c812011-09-21 17:13:30 +01002525 * Ensure that they get re-enabled when an interrupt happens.
2526 */
Ville Syrjälä1b2cb022018-01-17 21:21:45 +02002527 intel_encoder->hotplug = intel_sdvo_hotplug;
Daniel Vetter3a2fb2c2015-10-08 21:51:57 +02002528 intel_sdvo_enable_hotplug(intel_encoder);
Jani Nikula5fa7ac92012-08-29 16:43:58 +03002529 } else {
Egbert Eich821450c2013-04-16 13:36:55 +02002530 intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
Jani Nikula5fa7ac92012-08-29 16:43:58 +03002531 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002532 encoder->encoder_type = DRM_MODE_ENCODER_TMDS;
2533 connector->connector_type = DRM_MODE_CONNECTOR_DVID;
2534
Daniel Vetterde44e252017-07-26 21:32:51 +02002535 /* gen3 doesn't do the hdmi bits in the SDVO register */
2536 if (INTEL_GEN(dev_priv) >= 4 &&
2537 intel_sdvo_is_hdmi_connector(intel_sdvo, device)) {
Zhenyu Wang14571b42010-03-30 14:06:33 +08002538 connector->connector_type = DRM_MODE_CONNECTOR_HDMIA;
Chris Wilsone27d8532010-10-22 09:15:22 +01002539 intel_sdvo->is_hdmi = true;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002540 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002541
Imre Deakc3934542014-02-11 17:12:50 +02002542 if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2543 kfree(intel_sdvo_connector);
2544 return false;
2545 }
2546
Chris Wilsonf797d222010-12-23 09:43:48 +00002547 if (intel_sdvo->is_hdmi)
Ville Syrjälä55bc60d2013-01-17 16:31:29 +02002548 intel_sdvo_add_hdmi_properties(intel_sdvo, intel_sdvo_connector);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002549
2550 return true;
2551}
2552
2553static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002554intel_sdvo_tv_init(struct intel_sdvo *intel_sdvo, int type)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002555{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002556 struct drm_encoder *encoder = &intel_sdvo->base.base;
2557 struct drm_connector *connector;
2558 struct intel_connector *intel_connector;
2559 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002560
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002561 DRM_DEBUG_KMS("initialising TV type %d\n", type);
2562
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002563 intel_sdvo_connector = intel_sdvo_connector_alloc();
Chris Wilson615fb932010-08-04 13:50:24 +01002564 if (!intel_sdvo_connector)
2565 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002566
Chris Wilson615fb932010-08-04 13:50:24 +01002567 intel_connector = &intel_sdvo_connector->base;
Chris Wilson4ef69c72010-09-09 15:14:28 +01002568 connector = &intel_connector->base;
2569 encoder->encoder_type = DRM_MODE_ENCODER_TVDAC;
2570 connector->connector_type = DRM_MODE_CONNECTOR_SVIDEO;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002571
Chris Wilson4ef69c72010-09-09 15:14:28 +01002572 intel_sdvo->controlled_output |= type;
2573 intel_sdvo_connector->output_flag = type;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002574
Chris Wilson4ef69c72010-09-09 15:14:28 +01002575 intel_sdvo->is_tv = true;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002576
Imre Deakc3934542014-02-11 17:12:50 +02002577 if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2578 kfree(intel_sdvo_connector);
2579 return false;
2580 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002581
Chris Wilson4ef69c72010-09-09 15:14:28 +01002582 if (!intel_sdvo_tv_create_property(intel_sdvo, intel_sdvo_connector, type))
Chris Wilson32aad862010-08-04 13:50:25 +01002583 goto err;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002584
Chris Wilson4ef69c72010-09-09 15:14:28 +01002585 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
Chris Wilson32aad862010-08-04 13:50:25 +01002586 goto err;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002587
Chris Wilson4ef69c72010-09-09 15:14:28 +01002588 return true;
Chris Wilson32aad862010-08-04 13:50:25 +01002589
2590err:
Chris Wilson123d5c02010-09-23 16:15:21 +01002591 intel_sdvo_destroy(connector);
Chris Wilson32aad862010-08-04 13:50:25 +01002592 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002593}
2594
2595static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002596intel_sdvo_analog_init(struct intel_sdvo *intel_sdvo, int device)
Zhenyu Wang14571b42010-03-30 14:06:33 +08002597{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002598 struct drm_encoder *encoder = &intel_sdvo->base.base;
2599 struct drm_connector *connector;
2600 struct intel_connector *intel_connector;
2601 struct intel_sdvo_connector *intel_sdvo_connector;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002602
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002603 DRM_DEBUG_KMS("initialising analog device %d\n", device);
2604
Ander Conselvan de Oliveira8ce7da42015-06-08 11:26:30 +03002605 intel_sdvo_connector = intel_sdvo_connector_alloc();
Chris Wilson615fb932010-08-04 13:50:24 +01002606 if (!intel_sdvo_connector)
2607 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002608
Chris Wilson615fb932010-08-04 13:50:24 +01002609 intel_connector = &intel_sdvo_connector->base;
2610 connector = &intel_connector->base;
Egbert Eich821450c2013-04-16 13:36:55 +02002611 intel_connector->polled = DRM_CONNECTOR_POLL_CONNECT;
Chris Wilson4ef69c72010-09-09 15:14:28 +01002612 encoder->encoder_type = DRM_MODE_ENCODER_DAC;
2613 connector->connector_type = DRM_MODE_CONNECTOR_VGA;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002614
Chris Wilson4ef69c72010-09-09 15:14:28 +01002615 if (device == 0) {
2616 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB0;
2617 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB0;
2618 } else if (device == 1) {
2619 intel_sdvo->controlled_output |= SDVO_OUTPUT_RGB1;
2620 intel_sdvo_connector->output_flag = SDVO_OUTPUT_RGB1;
2621 }
Zhenyu Wang14571b42010-03-30 14:06:33 +08002622
Imre Deakc3934542014-02-11 17:12:50 +02002623 if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2624 kfree(intel_sdvo_connector);
2625 return false;
2626 }
2627
Chris Wilson4ef69c72010-09-09 15:14:28 +01002628 return true;
2629}
2630
2631static bool
2632intel_sdvo_lvds_init(struct intel_sdvo *intel_sdvo, int device)
2633{
2634 struct drm_encoder *encoder = &intel_sdvo->base.base;
2635 struct drm_connector *connector;
2636 struct intel_connector *intel_connector;
2637 struct intel_sdvo_connector *intel_sdvo_connector;
2638
Chris Wilson46a3f4a2013-09-24 12:55:40 +01002639 DRM_DEBUG_KMS("initialising LVDS device %d\n", device);
2640
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03002641 intel_sdvo_connector = intel_sdvo_connector_alloc();
Chris Wilson4ef69c72010-09-09 15:14:28 +01002642 if (!intel_sdvo_connector)
2643 return false;
2644
2645 intel_connector = &intel_sdvo_connector->base;
2646 connector = &intel_connector->base;
2647 encoder->encoder_type = DRM_MODE_ENCODER_LVDS;
2648 connector->connector_type = DRM_MODE_CONNECTOR_LVDS;
2649
2650 if (device == 0) {
2651 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS0;
2652 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS0;
2653 } else if (device == 1) {
2654 intel_sdvo->controlled_output |= SDVO_OUTPUT_LVDS1;
2655 intel_sdvo_connector->output_flag = SDVO_OUTPUT_LVDS1;
2656 }
2657
Imre Deakc3934542014-02-11 17:12:50 +02002658 if (intel_sdvo_connector_init(intel_sdvo_connector, intel_sdvo) < 0) {
2659 kfree(intel_sdvo_connector);
2660 return false;
2661 }
2662
Chris Wilson4ef69c72010-09-09 15:14:28 +01002663 if (!intel_sdvo_create_enhance_property(intel_sdvo, intel_sdvo_connector))
Chris Wilson32aad862010-08-04 13:50:25 +01002664 goto err;
2665
2666 return true;
2667
2668err:
Chris Wilson123d5c02010-09-23 16:15:21 +01002669 intel_sdvo_destroy(connector);
Chris Wilson32aad862010-08-04 13:50:25 +01002670 return false;
Zhenyu Wang14571b42010-03-30 14:06:33 +08002671}
Zhao Yakui6070a4a2010-02-08 21:35:12 +08002672
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002673static bool
Chris Wilsonea5b2132010-08-04 13:50:23 +01002674intel_sdvo_output_setup(struct intel_sdvo *intel_sdvo, uint16_t flags)
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002675{
Chris Wilsonea5b2132010-08-04 13:50:23 +01002676 intel_sdvo->is_tv = false;
Chris Wilsonea5b2132010-08-04 13:50:23 +01002677 intel_sdvo->is_lvds = false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002678
Zhenyu Wang14571b42010-03-30 14:06:33 +08002679 /* SDVO requires XXX1 function may not exist unless it has XXX0 function.*/
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002680
Zhenyu Wang14571b42010-03-30 14:06:33 +08002681 if (flags & SDVO_OUTPUT_TMDS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002682 if (!intel_sdvo_dvi_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002683 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002684
Zhenyu Wang14571b42010-03-30 14:06:33 +08002685 if ((flags & SDVO_TMDS_MASK) == SDVO_TMDS_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002686 if (!intel_sdvo_dvi_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002687 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002688
Zhenyu Wang14571b42010-03-30 14:06:33 +08002689 /* TV has no XXX1 function block */
Zhenyu Wanga1f4b7ff2010-03-29 23:16:13 +08002690 if (flags & SDVO_OUTPUT_SVID0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002691 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_SVID0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002692 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002693
Zhenyu Wang14571b42010-03-30 14:06:33 +08002694 if (flags & SDVO_OUTPUT_CVBS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002695 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_CVBS0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002696 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002697
Chris Wilsona0b1c7a2011-09-30 22:56:41 +01002698 if (flags & SDVO_OUTPUT_YPRPB0)
2699 if (!intel_sdvo_tv_init(intel_sdvo, SDVO_OUTPUT_YPRPB0))
2700 return false;
2701
Zhenyu Wang14571b42010-03-30 14:06:33 +08002702 if (flags & SDVO_OUTPUT_RGB0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002703 if (!intel_sdvo_analog_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002704 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002705
Zhenyu Wang14571b42010-03-30 14:06:33 +08002706 if ((flags & SDVO_RGB_MASK) == SDVO_RGB_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002707 if (!intel_sdvo_analog_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002708 return false;
Zhao Yakui2dd87382010-01-27 16:32:46 +08002709
Zhenyu Wang14571b42010-03-30 14:06:33 +08002710 if (flags & SDVO_OUTPUT_LVDS0)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002711 if (!intel_sdvo_lvds_init(intel_sdvo, 0))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002712 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002713
Zhenyu Wang14571b42010-03-30 14:06:33 +08002714 if ((flags & SDVO_LVDS_MASK) == SDVO_LVDS_MASK)
Chris Wilsonea5b2132010-08-04 13:50:23 +01002715 if (!intel_sdvo_lvds_init(intel_sdvo, 1))
Zhenyu Wang14571b42010-03-30 14:06:33 +08002716 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002717
Zhenyu Wang14571b42010-03-30 14:06:33 +08002718 if ((flags & SDVO_OUTPUT_MASK) == 0) {
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002719 unsigned char bytes[2];
2720
Chris Wilsonea5b2132010-08-04 13:50:23 +01002721 intel_sdvo->controlled_output = 0;
2722 memcpy(bytes, &intel_sdvo->caps.output_flags, 2);
Dave Airlie51c8b402009-08-20 13:38:04 +10002723 DRM_DEBUG_KMS("%s: Unknown SDVO output type (0x%02x%02x)\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +01002724 SDVO_NAME(intel_sdvo),
Dave Airlie51c8b402009-08-20 13:38:04 +10002725 bytes[0], bytes[1]);
Zhenyu Wang14571b42010-03-30 14:06:33 +08002726 return false;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002727 }
Jesse Barnes27f82272011-09-02 12:54:37 -07002728 intel_sdvo->base.crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002729
Zhenyu Wang14571b42010-03-30 14:06:33 +08002730 return true;
ling.ma@intel.comfb7a46f2009-07-23 17:11:34 +08002731}
2732
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02002733static void intel_sdvo_output_cleanup(struct intel_sdvo *intel_sdvo)
2734{
2735 struct drm_device *dev = intel_sdvo->base.base.dev;
2736 struct drm_connector *connector, *tmp;
2737
2738 list_for_each_entry_safe(connector, tmp,
2739 &dev->mode_config.connector_list, head) {
Paulo Zanonid9255d52013-09-26 20:05:59 -03002740 if (intel_attached_encoder(connector) == &intel_sdvo->base) {
Thomas Wood34ea3d32014-05-29 16:57:41 +01002741 drm_connector_unregister(connector);
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02002742 intel_sdvo_destroy(connector);
Paulo Zanonid9255d52013-09-26 20:05:59 -03002743 }
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02002744 }
2745}
2746
Chris Wilson32aad862010-08-04 13:50:25 +01002747static bool intel_sdvo_tv_create_property(struct intel_sdvo *intel_sdvo,
2748 struct intel_sdvo_connector *intel_sdvo_connector,
2749 int type)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002750{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002751 struct drm_device *dev = intel_sdvo->base.base.dev;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002752 struct intel_sdvo_tv_format format;
2753 uint32_t format_map, i;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002754
Chris Wilson32aad862010-08-04 13:50:25 +01002755 if (!intel_sdvo_set_target_output(intel_sdvo, type))
2756 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002757
Chris Wilson1a3665c2011-01-25 13:59:37 +00002758 BUILD_BUG_ON(sizeof(format) != 6);
Chris Wilson32aad862010-08-04 13:50:25 +01002759 if (!intel_sdvo_get_value(intel_sdvo,
2760 SDVO_CMD_GET_SUPPORTED_TV_FORMATS,
2761 &format, sizeof(format)))
2762 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002763
Chris Wilson32aad862010-08-04 13:50:25 +01002764 memcpy(&format_map, &format, min(sizeof(format_map), sizeof(format)));
Zhao Yakuice6feab2009-08-24 13:50:26 +08002765
2766 if (format_map == 0)
Chris Wilson32aad862010-08-04 13:50:25 +01002767 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002768
Chris Wilson615fb932010-08-04 13:50:24 +01002769 intel_sdvo_connector->format_supported_num = 0;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002770 for (i = 0 ; i < TV_FORMAT_NUM; i++)
Chris Wilson40039752010-08-04 13:50:26 +01002771 if (format_map & (1 << i))
2772 intel_sdvo_connector->tv_format_supported[intel_sdvo_connector->format_supported_num++] = i;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002773
2774
Chris Wilsonc5521702010-08-04 13:50:28 +01002775 intel_sdvo_connector->tv_format =
Chris Wilson32aad862010-08-04 13:50:25 +01002776 drm_property_create(dev, DRM_MODE_PROP_ENUM,
2777 "mode", intel_sdvo_connector->format_supported_num);
Chris Wilsonc5521702010-08-04 13:50:28 +01002778 if (!intel_sdvo_connector->tv_format)
Chris Wilsonfcc8d672010-08-04 13:50:27 +01002779 return false;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002780
Chris Wilson615fb932010-08-04 13:50:24 +01002781 for (i = 0; i < intel_sdvo_connector->format_supported_num; i++)
Zhao Yakuice6feab2009-08-24 13:50:26 +08002782 drm_property_add_enum(
Chris Wilsonc5521702010-08-04 13:50:28 +01002783 intel_sdvo_connector->tv_format, i,
Chris Wilson40039752010-08-04 13:50:26 +01002784 i, tv_format_names[intel_sdvo_connector->tv_format_supported[i]]);
Zhao Yakuice6feab2009-08-24 13:50:26 +08002785
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002786 intel_sdvo_connector->base.base.state->tv.mode = intel_sdvo_connector->tv_format_supported[0];
Ville Syrjälä10223df2017-06-15 20:23:08 +03002787 drm_object_attach_property(&intel_sdvo_connector->base.base.base,
2788 intel_sdvo_connector->tv_format, 0);
Chris Wilson32aad862010-08-04 13:50:25 +01002789 return true;
Zhao Yakuice6feab2009-08-24 13:50:26 +08002790
2791}
2792
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002793#define _ENHANCEMENT(state_assignment, name, NAME) do { \
Chris Wilsonc5521702010-08-04 13:50:28 +01002794 if (enhancements.name) { \
2795 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_MAX_##NAME, &data_value, 4) || \
2796 !intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_##NAME, &response, 2)) \
2797 return false; \
Chris Wilsonc5521702010-08-04 13:50:28 +01002798 intel_sdvo_connector->name = \
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002799 drm_property_create_range(dev, 0, #name, 0, data_value[0]); \
Chris Wilsonc5521702010-08-04 13:50:28 +01002800 if (!intel_sdvo_connector->name) return false; \
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002801 state_assignment = response; \
Rob Clark662595d2012-10-11 20:36:04 -05002802 drm_object_attach_property(&connector->base, \
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002803 intel_sdvo_connector->name, 0); \
Chris Wilsonc5521702010-08-04 13:50:28 +01002804 DRM_DEBUG_KMS(#name ": max %d, default %d, current %d\n", \
2805 data_value[0], data_value[1], response); \
2806 } \
Akshay Joshi0206e352011-08-16 15:34:10 -04002807} while (0)
Chris Wilsonc5521702010-08-04 13:50:28 +01002808
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002809#define ENHANCEMENT(state, name, NAME) _ENHANCEMENT((state)->name, name, NAME)
2810
Chris Wilsonc5521702010-08-04 13:50:28 +01002811static bool
2812intel_sdvo_create_enhance_property_tv(struct intel_sdvo *intel_sdvo,
2813 struct intel_sdvo_connector *intel_sdvo_connector,
2814 struct intel_sdvo_enhancements_reply enhancements)
Zhao Yakuib9219c52009-09-10 15:45:46 +08002815{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002816 struct drm_device *dev = intel_sdvo->base.base.dev;
Chris Wilson32aad862010-08-04 13:50:25 +01002817 struct drm_connector *connector = &intel_sdvo_connector->base.base;
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002818 struct drm_connector_state *conn_state = connector->state;
2819 struct intel_sdvo_connector_state *sdvo_state =
2820 to_intel_sdvo_connector_state(conn_state);
Zhao Yakuib9219c52009-09-10 15:45:46 +08002821 uint16_t response, data_value[2];
2822
Chris Wilsonc16336b2018-02-14 09:09:05 +00002823 /* when horizontal overscan is supported, Add the left/right property */
Chris Wilsonc5521702010-08-04 13:50:28 +01002824 if (enhancements.overscan_h) {
2825 if (!intel_sdvo_get_value(intel_sdvo,
2826 SDVO_CMD_GET_MAX_OVERSCAN_H,
2827 &data_value, 4))
2828 return false;
2829
2830 if (!intel_sdvo_get_value(intel_sdvo,
2831 SDVO_CMD_GET_OVERSCAN_H,
2832 &response, 2))
2833 return false;
2834
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002835 sdvo_state->tv.overscan_h = response;
2836
Chris Wilsonc5521702010-08-04 13:50:28 +01002837 intel_sdvo_connector->max_hscan = data_value[0];
Chris Wilsonc5521702010-08-04 13:50:28 +01002838 intel_sdvo_connector->left =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002839 drm_property_create_range(dev, 0, "left_margin", 0, data_value[0]);
Chris Wilsonc5521702010-08-04 13:50:28 +01002840 if (!intel_sdvo_connector->left)
2841 return false;
2842
Rob Clark662595d2012-10-11 20:36:04 -05002843 drm_object_attach_property(&connector->base,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002844 intel_sdvo_connector->left, 0);
Chris Wilsonc5521702010-08-04 13:50:28 +01002845
2846 intel_sdvo_connector->right =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002847 drm_property_create_range(dev, 0, "right_margin", 0, data_value[0]);
Chris Wilsonc5521702010-08-04 13:50:28 +01002848 if (!intel_sdvo_connector->right)
2849 return false;
2850
Rob Clark662595d2012-10-11 20:36:04 -05002851 drm_object_attach_property(&connector->base,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002852 intel_sdvo_connector->right, 0);
Chris Wilsonc5521702010-08-04 13:50:28 +01002853 DRM_DEBUG_KMS("h_overscan: max %d, "
2854 "default %d, current %d\n",
2855 data_value[0], data_value[1], response);
2856 }
2857
2858 if (enhancements.overscan_v) {
2859 if (!intel_sdvo_get_value(intel_sdvo,
2860 SDVO_CMD_GET_MAX_OVERSCAN_V,
2861 &data_value, 4))
2862 return false;
2863
2864 if (!intel_sdvo_get_value(intel_sdvo,
2865 SDVO_CMD_GET_OVERSCAN_V,
2866 &response, 2))
2867 return false;
2868
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002869 sdvo_state->tv.overscan_v = response;
2870
Chris Wilsonc5521702010-08-04 13:50:28 +01002871 intel_sdvo_connector->max_vscan = data_value[0];
Chris Wilsonc5521702010-08-04 13:50:28 +01002872 intel_sdvo_connector->top =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002873 drm_property_create_range(dev, 0,
2874 "top_margin", 0, data_value[0]);
Chris Wilsonc5521702010-08-04 13:50:28 +01002875 if (!intel_sdvo_connector->top)
2876 return false;
2877
Rob Clark662595d2012-10-11 20:36:04 -05002878 drm_object_attach_property(&connector->base,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002879 intel_sdvo_connector->top, 0);
Chris Wilsonc5521702010-08-04 13:50:28 +01002880
2881 intel_sdvo_connector->bottom =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002882 drm_property_create_range(dev, 0,
2883 "bottom_margin", 0, data_value[0]);
Chris Wilsonc5521702010-08-04 13:50:28 +01002884 if (!intel_sdvo_connector->bottom)
2885 return false;
2886
Rob Clark662595d2012-10-11 20:36:04 -05002887 drm_object_attach_property(&connector->base,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002888 intel_sdvo_connector->bottom, 0);
Chris Wilsonc5521702010-08-04 13:50:28 +01002889 DRM_DEBUG_KMS("v_overscan: max %d, "
2890 "default %d, current %d\n",
2891 data_value[0], data_value[1], response);
2892 }
2893
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002894 ENHANCEMENT(&sdvo_state->tv, hpos, HPOS);
2895 ENHANCEMENT(&sdvo_state->tv, vpos, VPOS);
2896 ENHANCEMENT(&conn_state->tv, saturation, SATURATION);
2897 ENHANCEMENT(&conn_state->tv, contrast, CONTRAST);
2898 ENHANCEMENT(&conn_state->tv, hue, HUE);
2899 ENHANCEMENT(&conn_state->tv, brightness, BRIGHTNESS);
2900 ENHANCEMENT(&sdvo_state->tv, sharpness, SHARPNESS);
2901 ENHANCEMENT(&sdvo_state->tv, flicker_filter, FLICKER_FILTER);
2902 ENHANCEMENT(&sdvo_state->tv, flicker_filter_adaptive, FLICKER_FILTER_ADAPTIVE);
2903 ENHANCEMENT(&sdvo_state->tv, flicker_filter_2d, FLICKER_FILTER_2D);
2904 _ENHANCEMENT(sdvo_state->tv.chroma_filter, tv_chroma_filter, TV_CHROMA_FILTER);
2905 _ENHANCEMENT(sdvo_state->tv.luma_filter, tv_luma_filter, TV_LUMA_FILTER);
Chris Wilsonc5521702010-08-04 13:50:28 +01002906
Chris Wilsone0442182010-08-04 13:50:29 +01002907 if (enhancements.dot_crawl) {
2908 if (!intel_sdvo_get_value(intel_sdvo, SDVO_CMD_GET_DOT_CRAWL, &response, 2))
2909 return false;
2910
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002911 sdvo_state->tv.dot_crawl = response & 0x1;
Chris Wilsone0442182010-08-04 13:50:29 +01002912 intel_sdvo_connector->dot_crawl =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002913 drm_property_create_range(dev, 0, "dot_crawl", 0, 1);
Chris Wilsone0442182010-08-04 13:50:29 +01002914 if (!intel_sdvo_connector->dot_crawl)
2915 return false;
2916
Rob Clark662595d2012-10-11 20:36:04 -05002917 drm_object_attach_property(&connector->base,
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002918 intel_sdvo_connector->dot_crawl, 0);
Chris Wilsone0442182010-08-04 13:50:29 +01002919 DRM_DEBUG_KMS("dot crawl: current %d\n", response);
2920 }
2921
Chris Wilsonc5521702010-08-04 13:50:28 +01002922 return true;
2923}
2924
2925static bool
2926intel_sdvo_create_enhance_property_lvds(struct intel_sdvo *intel_sdvo,
2927 struct intel_sdvo_connector *intel_sdvo_connector,
2928 struct intel_sdvo_enhancements_reply enhancements)
2929{
Chris Wilson4ef69c72010-09-09 15:14:28 +01002930 struct drm_device *dev = intel_sdvo->base.base.dev;
Chris Wilsonc5521702010-08-04 13:50:28 +01002931 struct drm_connector *connector = &intel_sdvo_connector->base.base;
2932 uint16_t response, data_value[2];
2933
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002934 ENHANCEMENT(&connector->state->tv, brightness, BRIGHTNESS);
Chris Wilsonc5521702010-08-04 13:50:28 +01002935
2936 return true;
2937}
2938#undef ENHANCEMENT
Maarten Lankhorst630d30a2017-05-01 15:38:04 +02002939#undef _ENHANCEMENT
Chris Wilsonc5521702010-08-04 13:50:28 +01002940
2941static bool intel_sdvo_create_enhance_property(struct intel_sdvo *intel_sdvo,
2942 struct intel_sdvo_connector *intel_sdvo_connector)
2943{
2944 union {
2945 struct intel_sdvo_enhancements_reply reply;
2946 uint16_t response;
2947 } enhancements;
2948
Chris Wilson1a3665c2011-01-25 13:59:37 +00002949 BUILD_BUG_ON(sizeof(enhancements) != 2);
2950
Imre Deak99016642017-05-10 12:21:49 +03002951 if (!intel_sdvo_get_value(intel_sdvo,
2952 SDVO_CMD_GET_SUPPORTED_ENHANCEMENTS,
2953 &enhancements, sizeof(enhancements)) ||
2954 enhancements.response == 0) {
Zhao Yakuib9219c52009-09-10 15:45:46 +08002955 DRM_DEBUG_KMS("No enhancement is supported\n");
Chris Wilson32aad862010-08-04 13:50:25 +01002956 return true;
Zhao Yakuib9219c52009-09-10 15:45:46 +08002957 }
Chris Wilson32aad862010-08-04 13:50:25 +01002958
Chris Wilsonc5521702010-08-04 13:50:28 +01002959 if (IS_TV(intel_sdvo_connector))
2960 return intel_sdvo_create_enhance_property_tv(intel_sdvo, intel_sdvo_connector, enhancements.reply);
Akshay Joshi0206e352011-08-16 15:34:10 -04002961 else if (IS_LVDS(intel_sdvo_connector))
Chris Wilsonc5521702010-08-04 13:50:28 +01002962 return intel_sdvo_create_enhance_property_lvds(intel_sdvo, intel_sdvo_connector, enhancements.reply);
2963 else
2964 return true;
Chris Wilsone957d772010-09-24 12:52:03 +01002965}
Chris Wilson32aad862010-08-04 13:50:25 +01002966
Chris Wilsone957d772010-09-24 12:52:03 +01002967static int intel_sdvo_ddc_proxy_xfer(struct i2c_adapter *adapter,
2968 struct i2c_msg *msgs,
2969 int num)
2970{
2971 struct intel_sdvo *sdvo = adapter->algo_data;
2972
Daniel Vettera85066842017-07-26 15:26:47 +02002973 if (!__intel_sdvo_set_control_bus_switch(sdvo, sdvo->ddc_bus))
Chris Wilsone957d772010-09-24 12:52:03 +01002974 return -EIO;
2975
2976 return sdvo->i2c->algo->master_xfer(sdvo->i2c, msgs, num);
2977}
2978
2979static u32 intel_sdvo_ddc_proxy_func(struct i2c_adapter *adapter)
2980{
2981 struct intel_sdvo *sdvo = adapter->algo_data;
2982 return sdvo->i2c->algo->functionality(sdvo->i2c);
2983}
2984
2985static const struct i2c_algorithm intel_sdvo_ddc_proxy = {
2986 .master_xfer = intel_sdvo_ddc_proxy_xfer,
2987 .functionality = intel_sdvo_ddc_proxy_func
2988};
2989
Daniel Vettera85066842017-07-26 15:26:47 +02002990static void proxy_lock_bus(struct i2c_adapter *adapter,
2991 unsigned int flags)
2992{
2993 struct intel_sdvo *sdvo = adapter->algo_data;
2994 sdvo->i2c->lock_ops->lock_bus(sdvo->i2c, flags);
2995}
2996
2997static int proxy_trylock_bus(struct i2c_adapter *adapter,
2998 unsigned int flags)
2999{
3000 struct intel_sdvo *sdvo = adapter->algo_data;
3001 return sdvo->i2c->lock_ops->trylock_bus(sdvo->i2c, flags);
3002}
3003
3004static void proxy_unlock_bus(struct i2c_adapter *adapter,
3005 unsigned int flags)
3006{
3007 struct intel_sdvo *sdvo = adapter->algo_data;
3008 sdvo->i2c->lock_ops->unlock_bus(sdvo->i2c, flags);
3009}
3010
Ville Syrjälä0db1aa42017-09-01 17:31:22 +03003011static const struct i2c_lock_operations proxy_lock_ops = {
Daniel Vettera85066842017-07-26 15:26:47 +02003012 .lock_bus = proxy_lock_bus,
3013 .trylock_bus = proxy_trylock_bus,
3014 .unlock_bus = proxy_unlock_bus,
3015};
3016
Chris Wilsone957d772010-09-24 12:52:03 +01003017static bool
3018intel_sdvo_init_ddc_proxy(struct intel_sdvo *sdvo,
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003019 struct drm_i915_private *dev_priv)
Chris Wilsone957d772010-09-24 12:52:03 +01003020{
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003021 struct pci_dev *pdev = dev_priv->drm.pdev;
David Weinehall52a05c32016-08-22 13:32:44 +03003022
Chris Wilsone957d772010-09-24 12:52:03 +01003023 sdvo->ddc.owner = THIS_MODULE;
3024 sdvo->ddc.class = I2C_CLASS_DDC;
3025 snprintf(sdvo->ddc.name, I2C_NAME_SIZE, "SDVO DDC proxy");
David Weinehall52a05c32016-08-22 13:32:44 +03003026 sdvo->ddc.dev.parent = &pdev->dev;
Chris Wilsone957d772010-09-24 12:52:03 +01003027 sdvo->ddc.algo_data = sdvo;
3028 sdvo->ddc.algo = &intel_sdvo_ddc_proxy;
Daniel Vettera85066842017-07-26 15:26:47 +02003029 sdvo->ddc.lock_ops = &proxy_lock_ops;
Chris Wilsone957d772010-09-24 12:52:03 +01003030
3031 return i2c_add_adapter(&sdvo->ddc) == 0;
Zhao Yakuib9219c52009-09-10 15:45:46 +08003032}
3033
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02003034static void assert_sdvo_port_valid(const struct drm_i915_private *dev_priv,
3035 enum port port)
3036{
3037 if (HAS_PCH_SPLIT(dev_priv))
3038 WARN_ON(port != PORT_B);
3039 else
3040 WARN_ON(port != PORT_B && port != PORT_C);
3041}
3042
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003043bool intel_sdvo_init(struct drm_i915_private *dev_priv,
Ville Syrjäläf0f59a02015-11-18 15:33:26 +02003044 i915_reg_t sdvo_reg, enum port port)
Jesse Barnes79e53942008-11-07 14:24:08 -08003045{
Eric Anholt21d40d32010-03-25 11:11:14 -07003046 struct intel_encoder *intel_encoder;
Chris Wilsonea5b2132010-08-04 13:50:23 +01003047 struct intel_sdvo *intel_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -08003048 int i;
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02003049
3050 assert_sdvo_port_valid(dev_priv, port);
3051
Daniel Vetterb14c5672013-09-19 12:18:32 +02003052 intel_sdvo = kzalloc(sizeof(*intel_sdvo), GFP_KERNEL);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003053 if (!intel_sdvo)
Eric Anholt7d573822009-01-02 13:33:00 -08003054 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08003055
Chris Wilson56184e32011-05-17 14:03:50 +01003056 intel_sdvo->sdvo_reg = sdvo_reg;
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02003057 intel_sdvo->port = port;
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003058 intel_sdvo->slave_addr =
3059 intel_sdvo_get_slave_addr(dev_priv, intel_sdvo) >> 1;
Ville Syrjälä8bd864b2015-09-18 20:03:14 +03003060 intel_sdvo_select_i2c_bus(dev_priv, intel_sdvo);
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003061 if (!intel_sdvo_init_ddc_proxy(intel_sdvo, dev_priv))
Jani Nikulafbfcc4f2012-10-22 16:12:18 +03003062 goto err_i2c_bus;
Chris Wilsone957d772010-09-24 12:52:03 +01003063
Chris Wilson56184e32011-05-17 14:03:50 +01003064 /* encoder type will be decided later */
Chris Wilsonea5b2132010-08-04 13:50:23 +01003065 intel_encoder = &intel_sdvo->base;
Eric Anholt21d40d32010-03-25 11:11:14 -07003066 intel_encoder->type = INTEL_OUTPUT_SDVO;
Ander Conselvan de Oliveira79f255a2017-02-22 08:34:27 +02003067 intel_encoder->power_domain = POWER_DOMAIN_PORT_OTHER;
Pandiyan, Dhinakaran03cdc1d2016-09-19 18:24:38 -07003068 intel_encoder->port = port;
Ander Conselvan de Oliveirac39055b2016-11-23 16:21:44 +02003069 drm_encoder_init(&dev_priv->drm, &intel_encoder->base,
3070 &intel_sdvo_enc_funcs, 0,
Ville Syrjälä580d8ed2016-05-27 20:59:24 +03003071 "SDVO %c", port_name(port));
Jesse Barnes79e53942008-11-07 14:24:08 -08003072
Jesse Barnes79e53942008-11-07 14:24:08 -08003073 /* Read the regs to test if we can talk to the device */
3074 for (i = 0; i < 0x40; i++) {
Chris Wilsonf899fc62010-07-20 15:44:45 -07003075 u8 byte;
3076
3077 if (!intel_sdvo_read_byte(intel_sdvo, i, &byte)) {
Daniel Vettereef4eac2012-03-23 23:43:35 +01003078 DRM_DEBUG_KMS("No SDVO device found on %s\n",
3079 SDVO_NAME(intel_sdvo));
Chris Wilsonf899fc62010-07-20 15:44:45 -07003080 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -08003081 }
3082 }
3083
Daniel Vetter6cc5f342013-03-27 00:44:53 +01003084 intel_encoder->compute_config = intel_sdvo_compute_config;
Tvrtko Ursulin6e266952016-10-13 11:02:53 +01003085 if (HAS_PCH_SPLIT(dev_priv)) {
Ville Syrjälä3c65d1d2015-05-05 17:17:36 +03003086 intel_encoder->disable = pch_disable_sdvo;
3087 intel_encoder->post_disable = pch_post_disable_sdvo;
3088 } else {
3089 intel_encoder->disable = intel_disable_sdvo;
3090 }
Daniel Vetter192d47a2014-04-24 23:54:45 +02003091 intel_encoder->pre_enable = intel_sdvo_pre_enable;
Daniel Vetterce22c322012-07-01 15:31:04 +02003092 intel_encoder->enable = intel_enable_sdvo;
Daniel Vetter4ac41f42012-07-02 14:54:00 +02003093 intel_encoder->get_hw_state = intel_sdvo_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07003094 intel_encoder->get_config = intel_sdvo_get_config;
Daniel Vetterce22c322012-07-01 15:31:04 +02003095
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003096 /* In default case sdvo lvds is false */
Chris Wilson32aad862010-08-04 13:50:25 +01003097 if (!intel_sdvo_get_capabilities(intel_sdvo, &intel_sdvo->caps))
Chris Wilsonf899fc62010-07-20 15:44:45 -07003098 goto err;
Jesse Barnes79e53942008-11-07 14:24:08 -08003099
Chris Wilsonea5b2132010-08-04 13:50:23 +01003100 if (intel_sdvo_output_setup(intel_sdvo,
3101 intel_sdvo->caps.output_flags) != true) {
Daniel Vettereef4eac2012-03-23 23:43:35 +01003102 DRM_DEBUG_KMS("SDVO output failed to setup on %s\n",
3103 SDVO_NAME(intel_sdvo));
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02003104 /* Output_setup can leave behind connectors! */
3105 goto err_output;
Jesse Barnes79e53942008-11-07 14:24:08 -08003106 }
3107
Chris Wilsonc16336b2018-02-14 09:09:05 +00003108 /*
3109 * Only enable the hotplug irq if we need it, to work around noisy
Chris Wilson7ba220c2013-06-09 16:02:04 +01003110 * hotplug lines.
3111 */
3112 if (intel_sdvo->hotplug_active) {
Ville Syrjälä2a5c0832015-11-06 21:29:59 +02003113 if (intel_sdvo->port == PORT_B)
3114 intel_encoder->hpd_pin = HPD_SDVO_B;
3115 else
3116 intel_encoder->hpd_pin = HPD_SDVO_C;
Chris Wilson7ba220c2013-06-09 16:02:04 +01003117 }
3118
Daniel Vettere506d6f2012-11-13 17:24:43 +01003119 /*
3120 * Cloning SDVO with anything is often impossible, since the SDVO
3121 * encoder can request a special input timing mode. And even if that's
3122 * not the case we have evidence that cloning a plain unscaled mode with
3123 * VGA doesn't really work. Furthermore the cloning flags are way too
3124 * simplistic anyway to express such constraints, so just give up on
3125 * cloning for SDVO encoders.
3126 */
Ville Syrjäläbc079e82014-03-03 16:15:28 +02003127 intel_sdvo->base.cloneable = 0;
Daniel Vettere506d6f2012-11-13 17:24:43 +01003128
Ville Syrjälä8bd864b2015-09-18 20:03:14 +03003129 intel_sdvo_select_ddc_bus(dev_priv, intel_sdvo);
Jesse Barnese2f0ba92009-02-02 15:11:52 -08003130
Jesse Barnes79e53942008-11-07 14:24:08 -08003131 /* Set the input timing to the screen. Assume always input 0. */
Chris Wilson32aad862010-08-04 13:50:25 +01003132 if (!intel_sdvo_set_target_input(intel_sdvo))
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02003133 goto err_output;
Jesse Barnes79e53942008-11-07 14:24:08 -08003134
Chris Wilson32aad862010-08-04 13:50:25 +01003135 if (!intel_sdvo_get_input_pixel_clock_range(intel_sdvo,
3136 &intel_sdvo->pixel_clock_min,
3137 &intel_sdvo->pixel_clock_max))
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02003138 goto err_output;
Jesse Barnes79e53942008-11-07 14:24:08 -08003139
Zhao Yakui8a4c47f2009-07-20 13:48:04 +08003140 DRM_DEBUG_KMS("%s device VID/DID: %02X:%02X.%02X, "
yakui_zhao342dc382009-06-02 14:12:00 +08003141 "clock range %dMHz - %dMHz, "
3142 "input 1: %c, input 2: %c, "
3143 "output 1: %c, output 2: %c\n",
Chris Wilsonea5b2132010-08-04 13:50:23 +01003144 SDVO_NAME(intel_sdvo),
3145 intel_sdvo->caps.vendor_id, intel_sdvo->caps.device_id,
3146 intel_sdvo->caps.device_rev_id,
3147 intel_sdvo->pixel_clock_min / 1000,
3148 intel_sdvo->pixel_clock_max / 1000,
3149 (intel_sdvo->caps.sdvo_inputs_mask & 0x1) ? 'Y' : 'N',
3150 (intel_sdvo->caps.sdvo_inputs_mask & 0x2) ? 'Y' : 'N',
yakui_zhao342dc382009-06-02 14:12:00 +08003151 /* check currently supported outputs */
Chris Wilsonea5b2132010-08-04 13:50:23 +01003152 intel_sdvo->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08003153 (SDVO_OUTPUT_TMDS0 | SDVO_OUTPUT_RGB0) ? 'Y' : 'N',
Chris Wilsonea5b2132010-08-04 13:50:23 +01003154 intel_sdvo->caps.output_flags &
Jesse Barnes79e53942008-11-07 14:24:08 -08003155 (SDVO_OUTPUT_TMDS1 | SDVO_OUTPUT_RGB1) ? 'Y' : 'N');
Eric Anholt7d573822009-01-02 13:33:00 -08003156 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08003157
Jani Nikulad0ddfbd2012-11-12 18:31:35 +02003158err_output:
3159 intel_sdvo_output_cleanup(intel_sdvo);
3160
Chris Wilsonf899fc62010-07-20 15:44:45 -07003161err:
Chris Wilson373a3cf2010-09-15 12:03:59 +01003162 drm_encoder_cleanup(&intel_encoder->base);
Chris Wilsone957d772010-09-24 12:52:03 +01003163 i2c_del_adapter(&intel_sdvo->ddc);
Jani Nikulafbfcc4f2012-10-22 16:12:18 +03003164err_i2c_bus:
3165 intel_sdvo_unselect_i2c_bus(intel_sdvo);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003166 kfree(intel_sdvo);
Jesse Barnes79e53942008-11-07 14:24:08 -08003167
Eric Anholt7d573822009-01-02 13:33:00 -08003168 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08003169}