xref: /linux/drivers/gpu/drm/i915/display/g4x_hdmi.c (revision fbf5df34a4dbcd09d433dd4f0916bf9b2ddb16de)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  *
5  * HDMI support for G4x,ILK,SNB,IVB,VLV,CHV (HSW+ handled by the DDI code).
6  */
7 
8 #include <drm/drm_print.h>
9 
10 #include "g4x_hdmi.h"
11 #include "intel_atomic.h"
12 #include "intel_audio.h"
13 #include "intel_connector.h"
14 #include "intel_crtc.h"
15 #include "intel_de.h"
16 #include "intel_display_power.h"
17 #include "intel_display_regs.h"
18 #include "intel_display_types.h"
19 #include "intel_dp_aux.h"
20 #include "intel_dpio_phy.h"
21 #include "intel_encoder.h"
22 #include "intel_fifo_underrun.h"
23 #include "intel_hdmi.h"
24 #include "intel_hotplug.h"
25 #include "intel_sdvo.h"
26 
27 static void intel_hdmi_prepare(struct intel_encoder *encoder,
28 			       const struct intel_crtc_state *crtc_state)
29 {
30 	struct intel_display *display = to_intel_display(encoder);
31 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
32 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
33 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
34 	u32 hdmi_val;
35 
36 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
37 
38 	hdmi_val = SDVO_ENCODING_HDMI;
39 	if (!HAS_PCH_SPLIT(display) && crtc_state->limited_color_range)
40 		hdmi_val |= HDMI_COLOR_RANGE_16_235;
41 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
42 		hdmi_val |= SDVO_VSYNC_ACTIVE_HIGH;
43 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
44 		hdmi_val |= SDVO_HSYNC_ACTIVE_HIGH;
45 
46 	if (crtc_state->pipe_bpp > 24)
47 		hdmi_val |= HDMI_COLOR_FORMAT_12bpc;
48 	else
49 		hdmi_val |= SDVO_COLOR_FORMAT_8bpc;
50 
51 	if (crtc_state->has_hdmi_sink)
52 		hdmi_val |= HDMI_MODE_SELECT_HDMI;
53 
54 	if (HAS_PCH_CPT(display))
55 		hdmi_val |= SDVO_PIPE_SEL_CPT(crtc->pipe);
56 	else if (display->platform.cherryview)
57 		hdmi_val |= SDVO_PIPE_SEL_CHV(crtc->pipe);
58 	else
59 		hdmi_val |= SDVO_PIPE_SEL(crtc->pipe);
60 
61 	intel_de_write(display, intel_hdmi->hdmi_reg, hdmi_val);
62 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
63 }
64 
65 static bool intel_hdmi_get_hw_state(struct intel_encoder *encoder,
66 				    enum pipe *pipe)
67 {
68 	struct intel_display *display = to_intel_display(encoder);
69 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
70 	struct ref_tracker *wakeref;
71 	bool ret;
72 
73 	wakeref = intel_display_power_get_if_enabled(display,
74 						     encoder->power_domain);
75 	if (!wakeref)
76 		return false;
77 
78 	ret = intel_sdvo_port_enabled(display, intel_hdmi->hdmi_reg, pipe);
79 
80 	intel_display_power_put(display, encoder->power_domain, wakeref);
81 
82 	return ret;
83 }
84 
85 static bool connector_is_hdmi(struct drm_connector *connector)
86 {
87 	struct intel_encoder *encoder =
88 		intel_attached_encoder(to_intel_connector(connector));
89 
90 	return encoder && encoder->type == INTEL_OUTPUT_HDMI;
91 }
92 
93 static bool g4x_compute_has_hdmi_sink(struct intel_atomic_state *state,
94 				      struct intel_crtc *this_crtc)
95 {
96 	const struct drm_connector_state *conn_state;
97 	struct drm_connector *connector;
98 	int i;
99 
100 	/*
101 	 * On g4x only one HDMI port can transmit infoframes/audio at
102 	 * any given time. Select the first suitable port for this duty.
103 	 *
104 	 * See also g4x_hdmi_connector_atomic_check().
105 	 */
106 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
107 		struct intel_encoder *encoder = to_intel_encoder(conn_state->best_encoder);
108 		const struct intel_crtc_state *crtc_state;
109 		struct intel_crtc *crtc;
110 
111 		if (!connector_is_hdmi(connector))
112 			continue;
113 
114 		crtc = to_intel_crtc(conn_state->crtc);
115 		if (!crtc)
116 			continue;
117 
118 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
119 
120 		if (!intel_hdmi_compute_has_hdmi_sink(encoder, crtc_state, conn_state))
121 			continue;
122 
123 		return crtc == this_crtc;
124 	}
125 
126 	return false;
127 }
128 
129 static int g4x_hdmi_compute_config(struct intel_encoder *encoder,
130 				   struct intel_crtc_state *crtc_state,
131 				   struct drm_connector_state *conn_state)
132 {
133 	struct intel_display *display = to_intel_display(encoder);
134 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
135 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
136 
137 	if (HAS_PCH_SPLIT(display))
138 		crtc_state->has_pch_encoder = true;
139 
140 	if (display->platform.g4x)
141 		crtc_state->has_hdmi_sink = g4x_compute_has_hdmi_sink(state, crtc);
142 	else
143 		crtc_state->has_hdmi_sink =
144 			intel_hdmi_compute_has_hdmi_sink(encoder, crtc_state, conn_state);
145 
146 	return intel_hdmi_compute_config(encoder, crtc_state, conn_state);
147 }
148 
149 static void intel_hdmi_get_config(struct intel_encoder *encoder,
150 				  struct intel_crtc_state *pipe_config)
151 {
152 	struct intel_display *display = to_intel_display(encoder);
153 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
154 	u32 tmp, flags = 0;
155 	int dotclock;
156 
157 	pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
158 
159 	tmp = intel_de_read(display, intel_hdmi->hdmi_reg);
160 
161 	if (tmp & SDVO_HSYNC_ACTIVE_HIGH)
162 		flags |= DRM_MODE_FLAG_PHSYNC;
163 	else
164 		flags |= DRM_MODE_FLAG_NHSYNC;
165 
166 	if (tmp & SDVO_VSYNC_ACTIVE_HIGH)
167 		flags |= DRM_MODE_FLAG_PVSYNC;
168 	else
169 		flags |= DRM_MODE_FLAG_NVSYNC;
170 
171 	if (tmp & HDMI_MODE_SELECT_HDMI)
172 		pipe_config->has_hdmi_sink = true;
173 
174 	pipe_config->infoframes.enable |=
175 		intel_hdmi_infoframes_enabled(encoder, pipe_config);
176 
177 	if (pipe_config->infoframes.enable)
178 		pipe_config->has_infoframe = true;
179 
180 	if (tmp & HDMI_AUDIO_ENABLE)
181 		pipe_config->has_audio = true;
182 
183 	if (!HAS_PCH_SPLIT(display) &&
184 	    tmp & HDMI_COLOR_RANGE_16_235)
185 		pipe_config->limited_color_range = true;
186 
187 	pipe_config->hw.adjusted_mode.flags |= flags;
188 
189 	if ((tmp & SDVO_COLOR_FORMAT_MASK) == HDMI_COLOR_FORMAT_12bpc)
190 		dotclock = DIV_ROUND_CLOSEST(pipe_config->port_clock * 2, 3);
191 	else
192 		dotclock = pipe_config->port_clock;
193 
194 	if (pipe_config->pixel_multiplier)
195 		dotclock /= pipe_config->pixel_multiplier;
196 
197 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
198 
199 	pipe_config->lane_count = 4;
200 
201 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
202 
203 	intel_read_infoframe(encoder, pipe_config,
204 			     HDMI_INFOFRAME_TYPE_AVI,
205 			     &pipe_config->infoframes.avi);
206 	intel_read_infoframe(encoder, pipe_config,
207 			     HDMI_INFOFRAME_TYPE_SPD,
208 			     &pipe_config->infoframes.spd);
209 	intel_read_infoframe(encoder, pipe_config,
210 			     HDMI_INFOFRAME_TYPE_VENDOR,
211 			     &pipe_config->infoframes.hdmi);
212 
213 	intel_audio_codec_get_config(encoder, pipe_config);
214 }
215 
216 static void g4x_hdmi_enable_port(struct intel_encoder *encoder,
217 				 const struct intel_crtc_state *pipe_config)
218 {
219 	struct intel_display *display = to_intel_display(encoder);
220 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
221 	u32 temp;
222 
223 	temp = intel_de_read(display, intel_hdmi->hdmi_reg);
224 
225 	temp |= SDVO_ENABLE;
226 
227 	intel_de_write(display, intel_hdmi->hdmi_reg, temp);
228 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
229 }
230 
231 static void g4x_hdmi_audio_enable(struct intel_encoder *encoder,
232 				  const struct intel_crtc_state *crtc_state,
233 				  const struct drm_connector_state *conn_state)
234 {
235 	struct intel_display *display = to_intel_display(encoder);
236 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
237 
238 	if (!crtc_state->has_audio)
239 		return;
240 
241 	drm_WARN_ON(display->drm, !crtc_state->has_hdmi_sink);
242 
243 	/* Enable audio presence detect */
244 	intel_de_rmw(display, hdmi->hdmi_reg, 0, HDMI_AUDIO_ENABLE);
245 
246 	intel_audio_codec_enable(encoder, crtc_state, conn_state);
247 }
248 
249 static void g4x_hdmi_audio_disable(struct intel_encoder *encoder,
250 				   const struct intel_crtc_state *old_crtc_state,
251 				   const struct drm_connector_state *old_conn_state)
252 {
253 	struct intel_display *display = to_intel_display(encoder);
254 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
255 
256 	if (!old_crtc_state->has_audio)
257 		return;
258 
259 	intel_audio_codec_disable(encoder, old_crtc_state, old_conn_state);
260 
261 	/* Disable audio presence detect */
262 	intel_de_rmw(display, hdmi->hdmi_reg, HDMI_AUDIO_ENABLE, 0);
263 }
264 
265 static void g4x_enable_hdmi(struct intel_atomic_state *state,
266 			    struct intel_encoder *encoder,
267 			    const struct intel_crtc_state *pipe_config,
268 			    const struct drm_connector_state *conn_state)
269 {
270 	g4x_hdmi_enable_port(encoder, pipe_config);
271 }
272 
273 static void ibx_enable_hdmi(struct intel_atomic_state *state,
274 			    struct intel_encoder *encoder,
275 			    const struct intel_crtc_state *pipe_config,
276 			    const struct drm_connector_state *conn_state)
277 {
278 	struct intel_display *display = to_intel_display(encoder);
279 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
280 	u32 temp;
281 
282 	temp = intel_de_read(display, intel_hdmi->hdmi_reg);
283 
284 	temp |= SDVO_ENABLE;
285 
286 	/*
287 	 * HW workaround, need to write this twice for issue
288 	 * that may result in first write getting masked.
289 	 */
290 	intel_de_write(display, intel_hdmi->hdmi_reg, temp);
291 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
292 	intel_de_write(display, intel_hdmi->hdmi_reg, temp);
293 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
294 
295 	/*
296 	 * HW workaround, need to toggle enable bit off and on
297 	 * for 12bpc with pixel repeat.
298 	 *
299 	 * FIXME: BSpec says this should be done at the end of
300 	 * the modeset sequence, so not sure if this isn't too soon.
301 	 */
302 	if (pipe_config->pipe_bpp > 24 &&
303 	    pipe_config->pixel_multiplier > 1) {
304 		intel_de_write(display, intel_hdmi->hdmi_reg,
305 			       temp & ~SDVO_ENABLE);
306 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
307 
308 		/*
309 		 * HW workaround, need to write this twice for issue
310 		 * that may result in first write getting masked.
311 		 */
312 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
313 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
314 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
315 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
316 	}
317 }
318 
319 static void cpt_enable_hdmi(struct intel_atomic_state *state,
320 			    struct intel_encoder *encoder,
321 			    const struct intel_crtc_state *pipe_config,
322 			    const struct drm_connector_state *conn_state)
323 {
324 	struct intel_display *display = to_intel_display(encoder);
325 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
326 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
327 	enum pipe pipe = crtc->pipe;
328 	u32 temp;
329 
330 	temp = intel_de_read(display, intel_hdmi->hdmi_reg);
331 
332 	temp |= SDVO_ENABLE;
333 
334 	/*
335 	 * WaEnableHDMI8bpcBefore12bpc:snb,ivb
336 	 *
337 	 * The procedure for 12bpc is as follows:
338 	 * 1. disable HDMI clock gating
339 	 * 2. enable HDMI with 8bpc
340 	 * 3. enable HDMI with 12bpc
341 	 * 4. enable HDMI clock gating
342 	 */
343 
344 	if (pipe_config->pipe_bpp > 24) {
345 		intel_de_rmw(display, TRANS_CHICKEN1(pipe),
346 			     0, TRANS_CHICKEN1_HDMIUNIT_GC_DISABLE);
347 
348 		temp &= ~SDVO_COLOR_FORMAT_MASK;
349 		temp |= SDVO_COLOR_FORMAT_8bpc;
350 	}
351 
352 	intel_de_write(display, intel_hdmi->hdmi_reg, temp);
353 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
354 
355 	if (pipe_config->pipe_bpp > 24) {
356 		temp &= ~SDVO_COLOR_FORMAT_MASK;
357 		temp |= HDMI_COLOR_FORMAT_12bpc;
358 
359 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
360 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
361 
362 		intel_de_rmw(display, TRANS_CHICKEN1(pipe),
363 			     TRANS_CHICKEN1_HDMIUNIT_GC_DISABLE, 0);
364 	}
365 }
366 
367 static void vlv_enable_hdmi(struct intel_atomic_state *state,
368 			    struct intel_encoder *encoder,
369 			    const struct intel_crtc_state *pipe_config,
370 			    const struct drm_connector_state *conn_state)
371 {
372 }
373 
374 static void intel_disable_hdmi(struct intel_atomic_state *state,
375 			       struct intel_encoder *encoder,
376 			       const struct intel_crtc_state *old_crtc_state,
377 			       const struct drm_connector_state *old_conn_state)
378 {
379 	struct intel_display *display = to_intel_display(encoder);
380 	struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
381 	struct intel_digital_port *dig_port =
382 		hdmi_to_dig_port(intel_hdmi);
383 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
384 	u32 temp;
385 
386 	temp = intel_de_read(display, intel_hdmi->hdmi_reg);
387 
388 	temp &= ~SDVO_ENABLE;
389 	intel_de_write(display, intel_hdmi->hdmi_reg, temp);
390 	intel_de_posting_read(display, intel_hdmi->hdmi_reg);
391 
392 	/*
393 	 * HW workaround for IBX, we need to move the port
394 	 * to transcoder A after disabling it to allow the
395 	 * matching DP port to be enabled on transcoder A.
396 	 */
397 	if (HAS_PCH_IBX(display) && crtc->pipe == PIPE_B) {
398 		/*
399 		 * We get CPU/PCH FIFO underruns on the other pipe when
400 		 * doing the workaround. Sweep them under the rug.
401 		 */
402 		intel_set_cpu_fifo_underrun_reporting(display, PIPE_A, false);
403 		intel_set_pch_fifo_underrun_reporting(display, PIPE_A, false);
404 
405 		temp &= ~SDVO_PIPE_SEL_MASK;
406 		temp |= SDVO_ENABLE | SDVO_PIPE_SEL(PIPE_A);
407 		/*
408 		 * HW workaround, need to write this twice for issue
409 		 * that may result in first write getting masked.
410 		 */
411 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
412 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
413 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
414 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
415 
416 		temp &= ~SDVO_ENABLE;
417 		intel_de_write(display, intel_hdmi->hdmi_reg, temp);
418 		intel_de_posting_read(display, intel_hdmi->hdmi_reg);
419 
420 		intel_wait_for_vblank_if_active(display, PIPE_A);
421 		intel_set_cpu_fifo_underrun_reporting(display, PIPE_A, true);
422 		intel_set_pch_fifo_underrun_reporting(display, PIPE_A, true);
423 	}
424 
425 	dig_port->set_infoframes(encoder,
426 				       false,
427 				       old_crtc_state, old_conn_state);
428 
429 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
430 }
431 
432 static void g4x_disable_hdmi(struct intel_atomic_state *state,
433 			     struct intel_encoder *encoder,
434 			     const struct intel_crtc_state *old_crtc_state,
435 			     const struct drm_connector_state *old_conn_state)
436 {
437 	intel_disable_hdmi(state, encoder, old_crtc_state, old_conn_state);
438 }
439 
440 static void pch_disable_hdmi(struct intel_atomic_state *state,
441 			     struct intel_encoder *encoder,
442 			     const struct intel_crtc_state *old_crtc_state,
443 			     const struct drm_connector_state *old_conn_state)
444 {
445 }
446 
447 static void pch_post_disable_hdmi(struct intel_atomic_state *state,
448 				  struct intel_encoder *encoder,
449 				  const struct intel_crtc_state *old_crtc_state,
450 				  const struct drm_connector_state *old_conn_state)
451 {
452 	intel_disable_hdmi(state, encoder, old_crtc_state, old_conn_state);
453 }
454 
455 static void intel_hdmi_pre_enable(struct intel_atomic_state *state,
456 				  struct intel_encoder *encoder,
457 				  const struct intel_crtc_state *pipe_config,
458 				  const struct drm_connector_state *conn_state)
459 {
460 	struct intel_digital_port *dig_port =
461 		enc_to_dig_port(encoder);
462 
463 	intel_hdmi_prepare(encoder, pipe_config);
464 
465 	dig_port->set_infoframes(encoder,
466 				       pipe_config->has_infoframe,
467 				       pipe_config, conn_state);
468 }
469 
470 static void vlv_hdmi_pre_enable(struct intel_atomic_state *state,
471 				struct intel_encoder *encoder,
472 				const struct intel_crtc_state *pipe_config,
473 				const struct drm_connector_state *conn_state)
474 {
475 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
476 
477 	vlv_phy_pre_encoder_enable(encoder, pipe_config);
478 
479 	/* HDMI 1.0V-2dB */
480 	vlv_set_phy_signal_level(encoder, pipe_config,
481 				 0x2b245f5f, 0x00002000,
482 				 0x5578b83a, 0x2b247878);
483 
484 	dig_port->set_infoframes(encoder,
485 			      pipe_config->has_infoframe,
486 			      pipe_config, conn_state);
487 
488 	g4x_hdmi_enable_port(encoder, pipe_config);
489 
490 	vlv_wait_port_ready(encoder, 0x0);
491 }
492 
493 static void vlv_hdmi_pre_pll_enable(struct intel_atomic_state *state,
494 				    struct intel_encoder *encoder,
495 				    const struct intel_crtc_state *pipe_config,
496 				    const struct drm_connector_state *conn_state)
497 {
498 	intel_hdmi_prepare(encoder, pipe_config);
499 
500 	vlv_phy_pre_pll_enable(encoder, pipe_config);
501 }
502 
503 static void chv_hdmi_pre_pll_enable(struct intel_atomic_state *state,
504 				    struct intel_encoder *encoder,
505 				    const struct intel_crtc_state *pipe_config,
506 				    const struct drm_connector_state *conn_state)
507 {
508 	intel_hdmi_prepare(encoder, pipe_config);
509 
510 	chv_phy_pre_pll_enable(encoder, pipe_config);
511 }
512 
513 static void chv_hdmi_post_pll_disable(struct intel_atomic_state *state,
514 				      struct intel_encoder *encoder,
515 				      const struct intel_crtc_state *old_crtc_state,
516 				      const struct drm_connector_state *old_conn_state)
517 {
518 	chv_phy_post_pll_disable(encoder, old_crtc_state);
519 }
520 
521 static void vlv_hdmi_post_disable(struct intel_atomic_state *state,
522 				  struct intel_encoder *encoder,
523 				  const struct intel_crtc_state *old_crtc_state,
524 				  const struct drm_connector_state *old_conn_state)
525 {
526 	/* Reset lanes to avoid HDMI flicker (VLV w/a) */
527 	vlv_phy_reset_lanes(encoder, old_crtc_state);
528 }
529 
530 static void chv_hdmi_post_disable(struct intel_atomic_state *state,
531 				  struct intel_encoder *encoder,
532 				  const struct intel_crtc_state *old_crtc_state,
533 				  const struct drm_connector_state *old_conn_state)
534 {
535 	/* Assert data lane reset */
536 	chv_data_lane_soft_reset(encoder, old_crtc_state, true);
537 }
538 
539 static void chv_hdmi_pre_enable(struct intel_atomic_state *state,
540 				struct intel_encoder *encoder,
541 				const struct intel_crtc_state *pipe_config,
542 				const struct drm_connector_state *conn_state)
543 {
544 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
545 
546 	chv_phy_pre_encoder_enable(encoder, pipe_config);
547 
548 	/* FIXME: Program the support xxx V-dB */
549 	/* Use 800mV-0dB */
550 	chv_set_phy_signal_level(encoder, pipe_config, 128, 102, false);
551 
552 	dig_port->set_infoframes(encoder,
553 			      pipe_config->has_infoframe,
554 			      pipe_config, conn_state);
555 
556 	g4x_hdmi_enable_port(encoder, pipe_config);
557 
558 	vlv_wait_port_ready(encoder, 0x0);
559 
560 	/* Second common lane will stay alive on its own now */
561 	chv_phy_release_cl2_override(encoder);
562 }
563 
564 static const struct drm_encoder_funcs intel_hdmi_enc_funcs = {
565 	.destroy = intel_encoder_destroy,
566 };
567 
568 static enum intel_hotplug_state
569 intel_hdmi_hotplug(struct intel_encoder *encoder,
570 		   struct intel_connector *connector)
571 {
572 	enum intel_hotplug_state state;
573 
574 	state = intel_encoder_hotplug(encoder, connector);
575 
576 	/*
577 	 * On many platforms the HDMI live state signal is known to be
578 	 * unreliable, so we can't use it to detect if a sink is connected or
579 	 * not. Instead we detect if it's connected based on whether we can
580 	 * read the EDID or not. That in turn has a problem during disconnect,
581 	 * since the HPD interrupt may be raised before the DDC lines get
582 	 * disconnected (due to how the required length of DDC vs. HPD
583 	 * connector pins are specified) and so we'll still be able to get a
584 	 * valid EDID. To solve this schedule another detection cycle if this
585 	 * time around we didn't detect any change in the sink's connection
586 	 * status.
587 	 */
588 	if (state == INTEL_HOTPLUG_UNCHANGED && !connector->hotplug_retries)
589 		state = INTEL_HOTPLUG_RETRY;
590 
591 	return state;
592 }
593 
594 int g4x_hdmi_connector_atomic_check(struct drm_connector *connector,
595 				    struct drm_atomic_state *state)
596 {
597 	struct intel_display *display = to_intel_display(connector->dev);
598 	struct drm_connector_list_iter conn_iter;
599 	struct drm_connector *conn;
600 	int ret;
601 
602 	ret = intel_digital_connector_atomic_check(connector, state);
603 	if (ret)
604 		return ret;
605 
606 	if (!display->platform.g4x)
607 		return 0;
608 
609 	if (!intel_connector_needs_modeset(to_intel_atomic_state(state), connector))
610 		return 0;
611 
612 	/*
613 	 * On g4x only one HDMI port can transmit infoframes/audio
614 	 * at any given time. Make sure all enabled HDMI ports are
615 	 * included in the state so that it's possible to select
616 	 * one of them for this duty.
617 	 *
618 	 * See also g4x_compute_has_hdmi_sink().
619 	 */
620 	drm_connector_list_iter_begin(display->drm, &conn_iter);
621 	drm_for_each_connector_iter(conn, &conn_iter) {
622 		struct drm_connector_state *conn_state;
623 		struct drm_crtc_state *crtc_state;
624 		struct drm_crtc *crtc;
625 
626 		if (!connector_is_hdmi(conn))
627 			continue;
628 
629 		drm_dbg_kms(display->drm, "Adding [CONNECTOR:%d:%s]\n",
630 			    conn->base.id, conn->name);
631 
632 		conn_state = drm_atomic_get_connector_state(state, conn);
633 		if (IS_ERR(conn_state)) {
634 			ret = PTR_ERR(conn_state);
635 			break;
636 		}
637 
638 		crtc = conn_state->crtc;
639 		if (!crtc)
640 			continue;
641 
642 		crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
643 		crtc_state->mode_changed = true;
644 
645 		ret = drm_atomic_add_affected_planes(state, crtc);
646 		if (ret)
647 			break;
648 	}
649 	drm_connector_list_iter_end(&conn_iter);
650 
651 	return ret;
652 }
653 
654 static bool is_hdmi_port_valid(struct intel_display *display, enum port port)
655 {
656 	if (display->platform.g4x || display->platform.valleyview)
657 		return port == PORT_B || port == PORT_C;
658 	else
659 		return port == PORT_B || port == PORT_C || port == PORT_D;
660 }
661 
662 static bool assert_hdmi_port_valid(struct intel_display *display, enum port port)
663 {
664 	return !drm_WARN(display->drm, !is_hdmi_port_valid(display, port),
665 			 "Platform does not support HDMI %c\n", port_name(port));
666 }
667 
668 bool g4x_hdmi_init(struct intel_display *display,
669 		   i915_reg_t hdmi_reg, enum port port)
670 {
671 	const struct intel_bios_encoder_data *devdata;
672 	struct intel_digital_port *dig_port;
673 	struct intel_encoder *intel_encoder;
674 	struct intel_connector *intel_connector;
675 
676 	if (!assert_port_valid(display, port))
677 		return false;
678 
679 	if (!assert_hdmi_port_valid(display, port))
680 		return false;
681 
682 	devdata = intel_bios_encoder_data_lookup(display, port);
683 
684 	/* FIXME bail? */
685 	if (!devdata)
686 		drm_dbg_kms(display->drm, "No VBT child device for HDMI-%c\n",
687 			    port_name(port));
688 
689 	dig_port = intel_dig_port_alloc();
690 	if (!dig_port)
691 		return false;
692 
693 	intel_connector = intel_connector_alloc();
694 	if (!intel_connector)
695 		goto err_connector_alloc;
696 
697 	intel_encoder = &dig_port->base;
698 
699 	intel_encoder->devdata = devdata;
700 
701 	if (drm_encoder_init(display->drm, &intel_encoder->base,
702 			     &intel_hdmi_enc_funcs, DRM_MODE_ENCODER_TMDS,
703 			     "HDMI %c", port_name(port)))
704 		goto err_encoder_init;
705 
706 	intel_encoder->hotplug = intel_hdmi_hotplug;
707 	intel_encoder->compute_config = g4x_hdmi_compute_config;
708 	if (HAS_PCH_SPLIT(display)) {
709 		intel_encoder->disable = pch_disable_hdmi;
710 		intel_encoder->post_disable = pch_post_disable_hdmi;
711 	} else {
712 		intel_encoder->disable = g4x_disable_hdmi;
713 	}
714 	intel_encoder->get_hw_state = intel_hdmi_get_hw_state;
715 	intel_encoder->get_config = intel_hdmi_get_config;
716 	if (display->platform.cherryview) {
717 		intel_encoder->pre_pll_enable = chv_hdmi_pre_pll_enable;
718 		intel_encoder->pre_enable = chv_hdmi_pre_enable;
719 		intel_encoder->enable = vlv_enable_hdmi;
720 		intel_encoder->post_disable = chv_hdmi_post_disable;
721 		intel_encoder->post_pll_disable = chv_hdmi_post_pll_disable;
722 	} else if (display->platform.valleyview) {
723 		intel_encoder->pre_pll_enable = vlv_hdmi_pre_pll_enable;
724 		intel_encoder->pre_enable = vlv_hdmi_pre_enable;
725 		intel_encoder->enable = vlv_enable_hdmi;
726 		intel_encoder->post_disable = vlv_hdmi_post_disable;
727 	} else {
728 		intel_encoder->pre_enable = intel_hdmi_pre_enable;
729 		if (HAS_PCH_CPT(display))
730 			intel_encoder->enable = cpt_enable_hdmi;
731 		else if (HAS_PCH_IBX(display))
732 			intel_encoder->enable = ibx_enable_hdmi;
733 		else
734 			intel_encoder->enable = g4x_enable_hdmi;
735 	}
736 	intel_encoder->audio_enable = g4x_hdmi_audio_enable;
737 	intel_encoder->audio_disable = g4x_hdmi_audio_disable;
738 	intel_encoder->shutdown = intel_hdmi_encoder_shutdown;
739 
740 	intel_encoder->type = INTEL_OUTPUT_HDMI;
741 	intel_encoder->power_domain = intel_display_power_ddi_lanes_domain(display, port);
742 	intel_encoder->port = port;
743 	if (display->platform.cherryview) {
744 		if (port == PORT_D)
745 			intel_encoder->pipe_mask = BIT(PIPE_C);
746 		else
747 			intel_encoder->pipe_mask = BIT(PIPE_A) | BIT(PIPE_B);
748 	} else {
749 		intel_encoder->pipe_mask = ~0;
750 	}
751 	intel_encoder->cloneable = BIT(INTEL_OUTPUT_ANALOG);
752 	intel_encoder->hpd_pin = intel_hpd_pin_default(port);
753 	/*
754 	 * BSpec is unclear about HDMI+HDMI cloning on g4x, but it seems
755 	 * to work on real hardware. And since g4x can send infoframes to
756 	 * only one port anyway, nothing is lost by allowing it.
757 	 */
758 	if (display->platform.g4x)
759 		intel_encoder->cloneable |= BIT(INTEL_OUTPUT_HDMI);
760 
761 	dig_port->hdmi.hdmi_reg = hdmi_reg;
762 
763 	intel_infoframe_init(dig_port);
764 
765 	if (!intel_hdmi_init_connector(dig_port, intel_connector))
766 		goto err_init_connector;
767 
768 	return true;
769 
770 err_init_connector:
771 	drm_encoder_cleanup(&intel_encoder->base);
772 err_encoder_init:
773 	kfree(intel_connector);
774 err_connector_alloc:
775 	kfree(dig_port);
776 
777 	return false;
778 }
779