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