xref: /linux/drivers/gpu/drm/amd/display/dc/dce/dce_link_encoder.c (revision d4a292c5f8e65d2784b703c67179f4f7d0c7846c)
1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25 
26 #include "reg_helper.h"
27 
28 #include "core_types.h"
29 #include "link_encoder.h"
30 #include "dce_link_encoder.h"
31 #include "stream_encoder.h"
32 #include "dc_bios_types.h"
33 
34 #include "gpio_service_interface.h"
35 
36 #include "dce/dce_11_0_d.h"
37 #include "dce/dce_11_0_sh_mask.h"
38 #include "dce/dce_11_0_enum.h"
39 
40 #ifndef DMU_MEM_PWR_CNTL__DMCU_IRAM_MEM_PWR_STATE__SHIFT
41 #define DMU_MEM_PWR_CNTL__DMCU_IRAM_MEM_PWR_STATE__SHIFT 0xa
42 #endif
43 
44 #ifndef DMU_MEM_PWR_CNTL__DMCU_IRAM_MEM_PWR_STATE_MASK
45 #define DMU_MEM_PWR_CNTL__DMCU_IRAM_MEM_PWR_STATE_MASK 0x00000400L
46 #endif
47 
48 #ifndef HPD0_DC_HPD_CONTROL__DC_HPD_EN_MASK
49 #define HPD0_DC_HPD_CONTROL__DC_HPD_EN_MASK  0x10000000L
50 #endif
51 
52 #ifndef HPD0_DC_HPD_CONTROL__DC_HPD_EN__SHIFT
53 #define HPD0_DC_HPD_CONTROL__DC_HPD_EN__SHIFT  0x1c
54 #endif
55 
56 #define CTX \
57 	enc110->base.ctx
58 #define DC_LOGGER \
59 	enc110->base.ctx->logger
60 
61 #define REG(reg)\
62 	(enc110->link_regs->reg)
63 
64 #define AUX_REG(reg)\
65 	(enc110->aux_regs->reg)
66 
67 #define HPD_REG(reg)\
68 	(enc110->hpd_regs->reg)
69 
70 #define DEFAULT_AUX_MAX_DATA_SIZE 16
71 #define AUX_MAX_DEFER_WRITE_RETRY 20
72 /*
73  * @brief
74  * Trigger Source Select
75  * ASIC-dependent, actual values for register programming
76  */
77 #define DCE110_DIG_FE_SOURCE_SELECT_INVALID 0x0
78 #define DCE110_DIG_FE_SOURCE_SELECT_DIGA 0x1
79 #define DCE110_DIG_FE_SOURCE_SELECT_DIGB 0x2
80 #define DCE110_DIG_FE_SOURCE_SELECT_DIGC 0x4
81 #define DCE110_DIG_FE_SOURCE_SELECT_DIGD 0x08
82 #define DCE110_DIG_FE_SOURCE_SELECT_DIGE 0x10
83 #define DCE110_DIG_FE_SOURCE_SELECT_DIGF 0x20
84 #define DCE110_DIG_FE_SOURCE_SELECT_DIGG 0x40
85 
86 enum {
87 	DP_MST_UPDATE_MAX_RETRY = 50
88 };
89 
90 #define DIG_REG(reg)\
91 	(reg + enc110->offsets.dig)
92 
93 #define DP_REG(reg)\
94 	(reg + enc110->offsets.dp)
95 
96 static const struct link_encoder_funcs dce110_lnk_enc_funcs = {
97 	.validate_output_with_stream =
98 		dce110_link_encoder_validate_output_with_stream,
99 	.hw_init = dce110_link_encoder_hw_init,
100 	.setup = dce110_link_encoder_setup,
101 	.enable_tmds_output = dce110_link_encoder_enable_tmds_output,
102 	.enable_dp_output = dce110_link_encoder_enable_dp_output,
103 	.enable_dp_mst_output = dce110_link_encoder_enable_dp_mst_output,
104 	.enable_lvds_output = dce110_link_encoder_enable_lvds_output,
105 	.enable_analog_output = dce110_link_encoder_enable_analog_output,
106 	.disable_output = dce110_link_encoder_disable_output,
107 	.dp_set_lane_settings = dce110_link_encoder_dp_set_lane_settings,
108 	.dp_set_phy_pattern = dce110_link_encoder_dp_set_phy_pattern,
109 	.update_mst_stream_allocation_table =
110 		dce110_link_encoder_update_mst_stream_allocation_table,
111 	.psr_program_dp_dphy_fast_training =
112 			dce110_psr_program_dp_dphy_fast_training,
113 	.psr_program_secondary_packet = dce110_psr_program_secondary_packet,
114 	.connect_dig_be_to_fe = dce110_link_encoder_connect_dig_be_to_fe,
115 	.enable_hpd = dce110_link_encoder_enable_hpd,
116 	.disable_hpd = dce110_link_encoder_disable_hpd,
117 	.is_dig_enabled = dce110_is_dig_enabled,
118 	.destroy = dce110_link_encoder_destroy,
119 	.get_max_link_cap = dce110_link_encoder_get_max_link_cap,
120 	.get_dig_frontend = dce110_get_dig_frontend,
121 	.get_hpd_state = dce110_get_hpd_state,
122 	.program_hpd_filter = dce110_program_hpd_filter,
123 };
124 
link_transmitter_control(struct dce110_link_encoder * enc110,struct bp_transmitter_control * cntl)125 static enum bp_result link_transmitter_control(
126 	struct dce110_link_encoder *enc110,
127 	struct bp_transmitter_control *cntl)
128 {
129 	enum bp_result result;
130 	struct dc_bios *bp = enc110->base.ctx->dc_bios;
131 
132 	result = bp->funcs->transmitter_control(bp, cntl);
133 
134 	return result;
135 }
136 
link_dac_encoder_control(struct dce110_link_encoder * link_enc,enum bp_encoder_control_action action,uint32_t pix_clk_100hz)137 static enum bp_result link_dac_encoder_control(
138 	struct dce110_link_encoder *link_enc,
139 	enum bp_encoder_control_action action,
140 	uint32_t pix_clk_100hz)
141 {
142 	struct dc_bios *bios = link_enc->base.ctx->dc_bios;
143 	struct bp_encoder_control encoder_control = {0};
144 
145 	encoder_control.action = action;
146 	encoder_control.engine_id = link_enc->base.analog_engine;
147 	encoder_control.pixel_clock = pix_clk_100hz / 10;
148 
149 	return bios->funcs->encoder_control(bios, &encoder_control);
150 }
151 
enable_phy_bypass_mode(struct dce110_link_encoder * enc110,bool enable)152 static void enable_phy_bypass_mode(
153 	struct dce110_link_encoder *enc110,
154 	bool enable)
155 {
156 	/* This register resides in DP back end block;
157 	 * transmitter is used for the offset */
158 
159 	REG_UPDATE(DP_DPHY_CNTL, DPHY_BYPASS, enable);
160 
161 }
162 
disable_prbs_symbols(struct dce110_link_encoder * enc110,bool disable)163 static void disable_prbs_symbols(
164 	struct dce110_link_encoder *enc110,
165 	bool disable)
166 {
167 	/* This register resides in DP back end block;
168 	 * transmitter is used for the offset */
169 
170 	REG_UPDATE_4(DP_DPHY_CNTL,
171 			DPHY_ATEST_SEL_LANE0, disable,
172 			DPHY_ATEST_SEL_LANE1, disable,
173 			DPHY_ATEST_SEL_LANE2, disable,
174 			DPHY_ATEST_SEL_LANE3, disable);
175 }
176 
disable_prbs_mode(struct dce110_link_encoder * enc110)177 static void disable_prbs_mode(
178 	struct dce110_link_encoder *enc110)
179 {
180 	REG_UPDATE(DP_DPHY_PRBS_CNTL, DPHY_PRBS_EN, 0);
181 }
182 
program_pattern_symbols(struct dce110_link_encoder * enc110,uint16_t pattern_symbols[8])183 static void program_pattern_symbols(
184 	struct dce110_link_encoder *enc110,
185 	uint16_t pattern_symbols[8])
186 {
187 	/* This register resides in DP back end block;
188 	 * transmitter is used for the offset */
189 
190 	REG_SET_3(DP_DPHY_SYM0, 0,
191 			DPHY_SYM1, pattern_symbols[0],
192 			DPHY_SYM2, pattern_symbols[1],
193 			DPHY_SYM3, pattern_symbols[2]);
194 
195 	/* This register resides in DP back end block;
196 	 * transmitter is used for the offset */
197 
198 	REG_SET_3(DP_DPHY_SYM1, 0,
199 			DPHY_SYM4, pattern_symbols[3],
200 			DPHY_SYM5, pattern_symbols[4],
201 			DPHY_SYM6, pattern_symbols[5]);
202 
203 	/* This register resides in DP back end block;
204 	 * transmitter is used for the offset */
205 
206 	REG_SET_2(DP_DPHY_SYM2, 0,
207 			DPHY_SYM7, pattern_symbols[6],
208 			DPHY_SYM8, pattern_symbols[7]);
209 }
210 
set_dp_phy_pattern_d102(struct dce110_link_encoder * enc110)211 static void set_dp_phy_pattern_d102(
212 	struct dce110_link_encoder *enc110)
213 {
214 	/* Disable PHY Bypass mode to setup the test pattern */
215 	enable_phy_bypass_mode(enc110, false);
216 
217 	/* For 10-bit PRBS or debug symbols
218 	 * please use the following sequence: */
219 
220 	/* Enable debug symbols on the lanes */
221 
222 	disable_prbs_symbols(enc110, true);
223 
224 	/* Disable PRBS mode */
225 	disable_prbs_mode(enc110);
226 
227 	/* Program debug symbols to be output */
228 	{
229 		uint16_t pattern_symbols[8] = {
230 			0x2AA, 0x2AA, 0x2AA, 0x2AA,
231 			0x2AA, 0x2AA, 0x2AA, 0x2AA
232 		};
233 
234 		program_pattern_symbols(enc110, pattern_symbols);
235 	}
236 
237 	/* Enable phy bypass mode to enable the test pattern */
238 
239 	enable_phy_bypass_mode(enc110, true);
240 }
241 
set_link_training_complete(struct dce110_link_encoder * enc110,bool complete)242 static void set_link_training_complete(
243 	struct dce110_link_encoder *enc110,
244 	bool complete)
245 {
246 	/* This register resides in DP back end block;
247 	 * transmitter is used for the offset */
248 
249 	REG_UPDATE(DP_LINK_CNTL, DP_LINK_TRAINING_COMPLETE, complete);
250 
251 }
252 
dce110_get_dig_frontend(struct link_encoder * enc)253 unsigned int dce110_get_dig_frontend(struct link_encoder *enc)
254 {
255 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
256 	u32 value;
257 	enum engine_id result;
258 
259 	REG_GET(DIG_BE_CNTL, DIG_FE_SOURCE_SELECT, &value);
260 
261 	switch (value) {
262 	case DCE110_DIG_FE_SOURCE_SELECT_DIGA:
263 		result = ENGINE_ID_DIGA;
264 		break;
265 	case DCE110_DIG_FE_SOURCE_SELECT_DIGB:
266 		result = ENGINE_ID_DIGB;
267 		break;
268 	case DCE110_DIG_FE_SOURCE_SELECT_DIGC:
269 		result = ENGINE_ID_DIGC;
270 		break;
271 	case DCE110_DIG_FE_SOURCE_SELECT_DIGD:
272 		result = ENGINE_ID_DIGD;
273 		break;
274 	case DCE110_DIG_FE_SOURCE_SELECT_DIGE:
275 		result = ENGINE_ID_DIGE;
276 		break;
277 	case DCE110_DIG_FE_SOURCE_SELECT_DIGF:
278 		result = ENGINE_ID_DIGF;
279 		break;
280 	case DCE110_DIG_FE_SOURCE_SELECT_DIGG:
281 		result = ENGINE_ID_DIGG;
282 		break;
283 	default:
284 		// invalid source select DIG
285 		result = ENGINE_ID_UNKNOWN;
286 	}
287 
288 	return result;
289 }
290 
dce110_link_encoder_set_dp_phy_pattern_training_pattern(struct link_encoder * enc,uint32_t index)291 void dce110_link_encoder_set_dp_phy_pattern_training_pattern(
292 	struct link_encoder *enc,
293 	uint32_t index)
294 {
295 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
296 	/* Write Training Pattern */
297 
298 	REG_WRITE(DP_DPHY_TRAINING_PATTERN_SEL, index);
299 
300 	/* Set HW Register Training Complete to false */
301 
302 	set_link_training_complete(enc110, false);
303 
304 	/* Disable PHY Bypass mode to output Training Pattern */
305 
306 	enable_phy_bypass_mode(enc110, false);
307 
308 	/* Disable PRBS mode */
309 	disable_prbs_mode(enc110);
310 }
311 
setup_panel_mode(struct dce110_link_encoder * enc110,enum dp_panel_mode panel_mode)312 static void setup_panel_mode(
313 	struct dce110_link_encoder *enc110,
314 	enum dp_panel_mode panel_mode)
315 {
316 	uint32_t value;
317 	struct dc_context *ctx = enc110->base.ctx;
318 
319 	/* if psp set panel mode, dal should be program it */
320 	if (ctx->dc->caps.psp_setup_panel_mode)
321 		return;
322 
323 	/* The code below is only applicable to encoders with a digital transmitter. */
324 	if (enc110->base.transmitter == TRANSMITTER_UNKNOWN)
325 		return;
326 
327 	ASSERT(REG(DP_DPHY_INTERNAL_CTRL));
328 	value = REG_READ(DP_DPHY_INTERNAL_CTRL);
329 
330 	switch (panel_mode) {
331 	case DP_PANEL_MODE_EDP:
332 		value = 0x1;
333 		break;
334 	case DP_PANEL_MODE_SPECIAL:
335 		value = 0x11;
336 		break;
337 	default:
338 		value = 0x0;
339 		break;
340 	}
341 
342 	REG_WRITE(DP_DPHY_INTERNAL_CTRL, value);
343 }
344 
set_dp_phy_pattern_symbol_error(struct dce110_link_encoder * enc110)345 static void set_dp_phy_pattern_symbol_error(
346 	struct dce110_link_encoder *enc110)
347 {
348 	/* Disable PHY Bypass mode to setup the test pattern */
349 	enable_phy_bypass_mode(enc110, false);
350 
351 	/* program correct panel mode*/
352 	setup_panel_mode(enc110, DP_PANEL_MODE_DEFAULT);
353 
354 	/* A PRBS23 pattern is used for most DP electrical measurements. */
355 
356 	/* Enable PRBS symbols on the lanes */
357 	disable_prbs_symbols(enc110, false);
358 
359 	/* For PRBS23 Set bit DPHY_PRBS_SEL=1 and Set bit DPHY_PRBS_EN=1 */
360 	REG_UPDATE_2(DP_DPHY_PRBS_CNTL,
361 			DPHY_PRBS_SEL, 1,
362 			DPHY_PRBS_EN, 1);
363 
364 	/* Enable phy bypass mode to enable the test pattern */
365 	enable_phy_bypass_mode(enc110, true);
366 }
367 
set_dp_phy_pattern_prbs7(struct dce110_link_encoder * enc110)368 static void set_dp_phy_pattern_prbs7(
369 	struct dce110_link_encoder *enc110)
370 {
371 	/* Disable PHY Bypass mode to setup the test pattern */
372 	enable_phy_bypass_mode(enc110, false);
373 
374 	/* A PRBS7 pattern is used for most DP electrical measurements. */
375 
376 	/* Enable PRBS symbols on the lanes */
377 	disable_prbs_symbols(enc110, false);
378 
379 	/* For PRBS7 Set bit DPHY_PRBS_SEL=0 and Set bit DPHY_PRBS_EN=1 */
380 	REG_UPDATE_2(DP_DPHY_PRBS_CNTL,
381 			DPHY_PRBS_SEL, 0,
382 			DPHY_PRBS_EN, 1);
383 
384 	/* Enable phy bypass mode to enable the test pattern */
385 	enable_phy_bypass_mode(enc110, true);
386 }
387 
set_dp_phy_pattern_80bit_custom(struct dce110_link_encoder * enc110,const uint8_t * pattern)388 static void set_dp_phy_pattern_80bit_custom(
389 	struct dce110_link_encoder *enc110,
390 	const uint8_t *pattern)
391 {
392 	/* Disable PHY Bypass mode to setup the test pattern */
393 	enable_phy_bypass_mode(enc110, false);
394 
395 	/* Enable debug symbols on the lanes */
396 
397 	disable_prbs_symbols(enc110, true);
398 
399 	/* Enable PHY bypass mode to enable the test pattern */
400 	/* TODO is it really needed ? */
401 
402 	enable_phy_bypass_mode(enc110, true);
403 
404 	/* Program 80 bit custom pattern */
405 	{
406 		uint16_t pattern_symbols[8];
407 
408 		pattern_symbols[0] =
409 			((pattern[1] & 0x03) << 8) | pattern[0];
410 		pattern_symbols[1] =
411 			((pattern[2] & 0x0f) << 6) | ((pattern[1] >> 2) & 0x3f);
412 		pattern_symbols[2] =
413 			((pattern[3] & 0x3f) << 4) | ((pattern[2] >> 4) & 0x0f);
414 		pattern_symbols[3] =
415 			(pattern[4] << 2) | ((pattern[3] >> 6) & 0x03);
416 		pattern_symbols[4] =
417 			((pattern[6] & 0x03) << 8) | pattern[5];
418 		pattern_symbols[5] =
419 			((pattern[7] & 0x0f) << 6) | ((pattern[6] >> 2) & 0x3f);
420 		pattern_symbols[6] =
421 			((pattern[8] & 0x3f) << 4) | ((pattern[7] >> 4) & 0x0f);
422 		pattern_symbols[7] =
423 			(pattern[9] << 2) | ((pattern[8] >> 6) & 0x03);
424 
425 		program_pattern_symbols(enc110, pattern_symbols);
426 	}
427 
428 	/* Enable phy bypass mode to enable the test pattern */
429 
430 	enable_phy_bypass_mode(enc110, true);
431 }
432 
set_dp_phy_pattern_hbr2_compliance_cp2520_2(struct dce110_link_encoder * enc110,unsigned int cp2520_pattern)433 static void set_dp_phy_pattern_hbr2_compliance_cp2520_2(
434 	struct dce110_link_encoder *enc110,
435 	unsigned int cp2520_pattern)
436 {
437 
438 	/* previously there is a register DP_HBR2_EYE_PATTERN
439 	 * that is enabled to get the pattern.
440 	 * But it does not work with the latest spec change,
441 	 * so we are programming the following registers manually.
442 	 *
443 	 * The following settings have been confirmed
444 	 * by Nick Chorney and Sandra Liu */
445 
446 	/* Disable PHY Bypass mode to setup the test pattern */
447 
448 	enable_phy_bypass_mode(enc110, false);
449 
450 	/* Setup DIG encoder in DP SST mode */
451 	enc110->base.funcs->setup(&enc110->base, SIGNAL_TYPE_DISPLAY_PORT);
452 
453 	/* ensure normal panel mode. */
454 	setup_panel_mode(enc110, DP_PANEL_MODE_DEFAULT);
455 
456 	/* no vbid after BS (SR)
457 	 * DP_LINK_FRAMING_CNTL changed history Sandra Liu
458 	 * 11000260 / 11000104 / 110000FC */
459 	REG_UPDATE_3(DP_LINK_FRAMING_CNTL,
460 			DP_IDLE_BS_INTERVAL, 0xFC,
461 			DP_VBID_DISABLE, 1,
462 			DP_VID_ENHANCED_FRAME_MODE, 1);
463 
464 	/* swap every BS with SR */
465 	REG_UPDATE(DP_DPHY_SCRAM_CNTL, DPHY_SCRAMBLER_BS_COUNT, 0);
466 
467 	/* select cp2520 patterns */
468 	if (REG(DP_DPHY_HBR2_PATTERN_CONTROL))
469 		REG_UPDATE(DP_DPHY_HBR2_PATTERN_CONTROL,
470 				DP_DPHY_HBR2_PATTERN_CONTROL, cp2520_pattern);
471 	else
472 		/* pre-DCE11 can only generate CP2520 pattern 2 */
473 		ASSERT(cp2520_pattern == 2);
474 
475 	/* set link training complete */
476 	set_link_training_complete(enc110, true);
477 
478 	/* disable video stream */
479 	REG_UPDATE(DP_VID_STREAM_CNTL, DP_VID_STREAM_ENABLE, 0);
480 
481 	/* Disable PHY Bypass mode to setup the test pattern */
482 	enable_phy_bypass_mode(enc110, false);
483 }
484 
485 #if defined(CONFIG_DRM_AMD_DC_SI)
dce60_set_dp_phy_pattern_hbr2_compliance_cp2520_2(struct dce110_link_encoder * enc110,unsigned int cp2520_pattern)486 static void dce60_set_dp_phy_pattern_hbr2_compliance_cp2520_2(
487 	struct dce110_link_encoder *enc110,
488 	unsigned int cp2520_pattern)
489 {
490 
491 	/* previously there is a register DP_HBR2_EYE_PATTERN
492 	 * that is enabled to get the pattern.
493 	 * But it does not work with the latest spec change,
494 	 * so we are programming the following registers manually.
495 	 *
496 	 * The following settings have been confirmed
497 	 * by Nick Chorney and Sandra Liu */
498 
499 	/* Disable PHY Bypass mode to setup the test pattern */
500 
501 	enable_phy_bypass_mode(enc110, false);
502 
503 	/* Setup DIG encoder in DP SST mode */
504 	enc110->base.funcs->setup(&enc110->base, SIGNAL_TYPE_DISPLAY_PORT);
505 
506 	/* ensure normal panel mode. */
507 	setup_panel_mode(enc110, DP_PANEL_MODE_DEFAULT);
508 
509 	/* no vbid after BS (SR)
510 	 * DP_LINK_FRAMING_CNTL changed history Sandra Liu
511 	 * 11000260 / 11000104 / 110000FC */
512 	REG_UPDATE_3(DP_LINK_FRAMING_CNTL,
513 			DP_IDLE_BS_INTERVAL, 0xFC,
514 			DP_VBID_DISABLE, 1,
515 			DP_VID_ENHANCED_FRAME_MODE, 1);
516 
517 	/* DCE6 has no DP_DPHY_SCRAM_CNTL register, skip swap BS with SR */
518 
519 	/* select cp2520 patterns */
520 	if (REG(DP_DPHY_HBR2_PATTERN_CONTROL))
521 		REG_UPDATE(DP_DPHY_HBR2_PATTERN_CONTROL,
522 				DP_DPHY_HBR2_PATTERN_CONTROL, cp2520_pattern);
523 	else
524 		/* pre-DCE11 can only generate CP2520 pattern 2 */
525 		ASSERT(cp2520_pattern == 2);
526 
527 	/* set link training complete */
528 	set_link_training_complete(enc110, true);
529 
530 	/* disable video stream */
531 	REG_UPDATE(DP_VID_STREAM_CNTL, DP_VID_STREAM_ENABLE, 0);
532 
533 	/* Disable PHY Bypass mode to setup the test pattern */
534 	enable_phy_bypass_mode(enc110, false);
535 }
536 #endif
537 
set_dp_phy_pattern_passthrough_mode(struct dce110_link_encoder * enc110,enum dp_panel_mode panel_mode)538 static void set_dp_phy_pattern_passthrough_mode(
539 	struct dce110_link_encoder *enc110,
540 	enum dp_panel_mode panel_mode)
541 {
542 	/* program correct panel mode */
543 	setup_panel_mode(enc110, panel_mode);
544 
545 	/* restore LINK_FRAMING_CNTL and DPHY_SCRAMBLER_BS_COUNT
546 	 * in case we were doing HBR2 compliance pattern before
547 	 */
548 	REG_UPDATE_3(DP_LINK_FRAMING_CNTL,
549 			DP_IDLE_BS_INTERVAL, 0x2000,
550 			DP_VBID_DISABLE, 0,
551 			DP_VID_ENHANCED_FRAME_MODE, 1);
552 
553 	REG_UPDATE(DP_DPHY_SCRAM_CNTL, DPHY_SCRAMBLER_BS_COUNT, 0x1FF);
554 
555 	/* set link training complete */
556 	set_link_training_complete(enc110, true);
557 
558 	/* Disable PHY Bypass mode to setup the test pattern */
559 	enable_phy_bypass_mode(enc110, false);
560 
561 	/* Disable PRBS mode */
562 	disable_prbs_mode(enc110);
563 }
564 
565 #if defined(CONFIG_DRM_AMD_DC_SI)
dce60_set_dp_phy_pattern_passthrough_mode(struct dce110_link_encoder * enc110,enum dp_panel_mode panel_mode)566 static void dce60_set_dp_phy_pattern_passthrough_mode(
567 	struct dce110_link_encoder *enc110,
568 	enum dp_panel_mode panel_mode)
569 {
570 	/* program correct panel mode */
571 	setup_panel_mode(enc110, panel_mode);
572 
573 	/* restore LINK_FRAMING_CNTL
574 	 * in case we were doing HBR2 compliance pattern before
575 	 */
576 	REG_UPDATE_3(DP_LINK_FRAMING_CNTL,
577 			DP_IDLE_BS_INTERVAL, 0x2000,
578 			DP_VBID_DISABLE, 0,
579 			DP_VID_ENHANCED_FRAME_MODE, 1);
580 
581 	/* DCE6 has no DP_DPHY_SCRAM_CNTL register, skip DPHY_SCRAMBLER_BS_COUNT restore */
582 
583 	/* set link training complete */
584 	set_link_training_complete(enc110, true);
585 
586 	/* Disable PHY Bypass mode to setup the test pattern */
587 	enable_phy_bypass_mode(enc110, false);
588 
589 	/* Disable PRBS mode */
590 	disable_prbs_mode(enc110);
591 }
592 #endif
593 
594 /* return value is bit-vector */
get_frontend_source(enum engine_id engine)595 static uint8_t get_frontend_source(
596 	enum engine_id engine)
597 {
598 	switch (engine) {
599 	case ENGINE_ID_DIGA:
600 		return DCE110_DIG_FE_SOURCE_SELECT_DIGA;
601 	case ENGINE_ID_DIGB:
602 		return DCE110_DIG_FE_SOURCE_SELECT_DIGB;
603 	case ENGINE_ID_DIGC:
604 		return DCE110_DIG_FE_SOURCE_SELECT_DIGC;
605 	case ENGINE_ID_DIGD:
606 		return DCE110_DIG_FE_SOURCE_SELECT_DIGD;
607 	case ENGINE_ID_DIGE:
608 		return DCE110_DIG_FE_SOURCE_SELECT_DIGE;
609 	case ENGINE_ID_DIGF:
610 		return DCE110_DIG_FE_SOURCE_SELECT_DIGF;
611 	case ENGINE_ID_DIGG:
612 		return DCE110_DIG_FE_SOURCE_SELECT_DIGG;
613 	default:
614 		ASSERT_CRITICAL(false);
615 		return DCE110_DIG_FE_SOURCE_SELECT_INVALID;
616 	}
617 }
618 
configure_encoder(struct dce110_link_encoder * enc110,const struct dc_link_settings * link_settings)619 static void configure_encoder(
620 	struct dce110_link_encoder *enc110,
621 	const struct dc_link_settings *link_settings)
622 {
623 	/* set number of lanes */
624 
625 	REG_SET(DP_CONFIG, 0,
626 			DP_UDI_LANES, link_settings->lane_count - LANE_COUNT_ONE);
627 
628 	/* setup scrambler */
629 	REG_UPDATE(DP_DPHY_SCRAM_CNTL, DPHY_SCRAMBLER_ADVANCE, 1);
630 }
631 
632 #if defined(CONFIG_DRM_AMD_DC_SI)
dce60_configure_encoder(struct dce110_link_encoder * enc110,const struct dc_link_settings * link_settings)633 static void dce60_configure_encoder(
634 	struct dce110_link_encoder *enc110,
635 	const struct dc_link_settings *link_settings)
636 {
637 	/* set number of lanes */
638 
639 	REG_SET(DP_CONFIG, 0,
640 			DP_UDI_LANES, link_settings->lane_count - LANE_COUNT_ONE);
641 
642 	/* DCE6 has no DP_DPHY_SCRAM_CNTL register, skip setup scrambler */
643 }
644 #endif
645 
aux_initialize(struct dce110_link_encoder * enc110)646 static void aux_initialize(
647 	struct dce110_link_encoder *enc110)
648 {
649 	struct dc_context *ctx = enc110->base.ctx;
650 	enum hpd_source_id hpd_source = enc110->base.hpd_source;
651 	uint32_t addr = AUX_REG(AUX_CONTROL);
652 	uint32_t value = dm_read_reg(ctx, addr);
653 
654 	set_reg_field_value(value, hpd_source, AUX_CONTROL, AUX_HPD_SEL);
655 	set_reg_field_value(value, 0, AUX_CONTROL, AUX_LS_READ_EN);
656 	dm_write_reg(ctx, addr, value);
657 
658 	addr = AUX_REG(AUX_DPHY_RX_CONTROL0);
659 	value = dm_read_reg(ctx, addr);
660 
661 	/* 1/4 window (the maximum allowed) */
662 	set_reg_field_value(value, 1,
663 			AUX_DPHY_RX_CONTROL0, AUX_RX_RECEIVE_WINDOW);
664 	dm_write_reg(ctx, addr, value);
665 
666 }
667 
dce110_psr_program_dp_dphy_fast_training(struct link_encoder * enc,bool exit_link_training_required)668 void dce110_psr_program_dp_dphy_fast_training(struct link_encoder *enc,
669 			bool exit_link_training_required)
670 {
671 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
672 
673 	if (exit_link_training_required)
674 		REG_UPDATE(DP_DPHY_FAST_TRAINING,
675 				DPHY_RX_FAST_TRAINING_CAPABLE, 1);
676 	else {
677 		REG_UPDATE(DP_DPHY_FAST_TRAINING,
678 				DPHY_RX_FAST_TRAINING_CAPABLE, 0);
679 		/*In DCE 11, we are able to pre-program a Force SR register
680 		 * to be able to trigger SR symbol after 5 idle patterns
681 		 * transmitted. Upon PSR Exit, DMCU can trigger
682 		 * DPHY_LOAD_BS_COUNT_START = 1. Upon writing 1 to
683 		 * DPHY_LOAD_BS_COUNT_START and the internal counter
684 		 * reaches DPHY_LOAD_BS_COUNT, the next BS symbol will be
685 		 * replaced by SR symbol once.
686 		 */
687 
688 		REG_UPDATE(DP_DPHY_BS_SR_SWAP_CNTL, DPHY_LOAD_BS_COUNT, 0x5);
689 	}
690 }
691 
dce110_psr_program_secondary_packet(struct link_encoder * enc,unsigned int sdp_transmit_line_num_deadline)692 void dce110_psr_program_secondary_packet(struct link_encoder *enc,
693 			unsigned int sdp_transmit_line_num_deadline)
694 {
695 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
696 
697 	REG_UPDATE_2(DP_SEC_CNTL1,
698 		DP_SEC_GSP0_LINE_NUM, sdp_transmit_line_num_deadline,
699 		DP_SEC_GSP0_PRIORITY, 1);
700 }
701 
dce110_is_dig_enabled(struct link_encoder * enc)702 bool dce110_is_dig_enabled(struct link_encoder *enc)
703 {
704 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
705 	uint32_t value;
706 
707 	REG_GET(DIG_BE_EN_CNTL, DIG_ENABLE, &value);
708 	return value;
709 }
710 
link_encoder_disable(struct dce110_link_encoder * enc110)711 static void link_encoder_disable(struct dce110_link_encoder *enc110)
712 {
713 	/* reset training pattern */
714 	REG_SET(DP_DPHY_TRAINING_PATTERN_SEL, 0,
715 			DPHY_TRAINING_PATTERN_SEL, 0);
716 
717 	/* reset training complete */
718 	REG_UPDATE(DP_LINK_CNTL, DP_LINK_TRAINING_COMPLETE, 0);
719 
720 	/* reset panel mode */
721 	setup_panel_mode(enc110, DP_PANEL_MODE_DEFAULT);
722 }
723 
hpd_initialize(struct dce110_link_encoder * enc110)724 static void hpd_initialize(
725 	struct dce110_link_encoder *enc110)
726 {
727 	/* Associate HPD with DIG_BE */
728 	enum hpd_source_id hpd_source = enc110->base.hpd_source;
729 
730 	REG_UPDATE(DIG_BE_CNTL, DIG_HPD_SELECT, hpd_source);
731 }
732 
dce110_link_encoder_validate_dvi_output(const struct dce110_link_encoder * enc110,enum signal_type connector_signal,enum signal_type signal,const struct dc_crtc_timing * crtc_timing)733 bool dce110_link_encoder_validate_dvi_output(
734 	const struct dce110_link_encoder *enc110,
735 	enum signal_type connector_signal,
736 	enum signal_type signal,
737 	const struct dc_crtc_timing *crtc_timing)
738 {
739 	uint32_t max_pixel_clock = TMDS_MAX_PIXEL_CLOCK;
740 
741 	if (signal == SIGNAL_TYPE_DVI_DUAL_LINK)
742 		max_pixel_clock *= 2;
743 
744 	/* This handles the case of HDMI downgrade to DVI we don't want to
745 	 * we don't want to cap the pixel clock if the DDI is not DVI.
746 	 */
747 	if (connector_signal != SIGNAL_TYPE_DVI_DUAL_LINK &&
748 			connector_signal != SIGNAL_TYPE_DVI_SINGLE_LINK)
749 		max_pixel_clock = enc110->base.features.max_hdmi_pixel_clock;
750 
751 	/* DVI only support RGB pixel encoding */
752 	if (crtc_timing->pixel_encoding != PIXEL_ENCODING_RGB)
753 		return false;
754 
755 	/*connect DVI via adpater's HDMI connector*/
756 	if ((connector_signal == SIGNAL_TYPE_DVI_SINGLE_LINK ||
757 		connector_signal == SIGNAL_TYPE_HDMI_TYPE_A) &&
758 		signal != SIGNAL_TYPE_HDMI_TYPE_A &&
759 		crtc_timing->pix_clk_100hz > (TMDS_MAX_PIXEL_CLOCK * 10))
760 		return false;
761 	if (crtc_timing->pix_clk_100hz < (TMDS_MIN_PIXEL_CLOCK * 10))
762 		return false;
763 
764 	if (crtc_timing->pix_clk_100hz > (max_pixel_clock * 10))
765 		return false;
766 
767 	/* DVI supports 6/8bpp single-link and 10/16bpp dual-link */
768 	switch (crtc_timing->display_color_depth) {
769 	case COLOR_DEPTH_666:
770 	case COLOR_DEPTH_888:
771 	break;
772 	case COLOR_DEPTH_101010:
773 	case COLOR_DEPTH_161616:
774 		if (signal != SIGNAL_TYPE_DVI_DUAL_LINK)
775 			return false;
776 	break;
777 	default:
778 		return false;
779 	}
780 
781 	return true;
782 }
783 
dce110_link_encoder_validate_hdmi_output(const struct dce110_link_encoder * enc110,const struct dc_crtc_timing * crtc_timing,int adjusted_pix_clk_khz)784 static bool dce110_link_encoder_validate_hdmi_output(
785 	const struct dce110_link_encoder *enc110,
786 	const struct dc_crtc_timing *crtc_timing,
787 	int adjusted_pix_clk_khz)
788 {
789 	enum dc_color_depth max_deep_color =
790 			enc110->base.features.max_hdmi_deep_color;
791 
792 	if (max_deep_color < crtc_timing->display_color_depth)
793 		return false;
794 
795 	if (crtc_timing->display_color_depth < COLOR_DEPTH_888)
796 		return false;
797 	if (adjusted_pix_clk_khz < TMDS_MIN_PIXEL_CLOCK)
798 		return false;
799 
800 	if ((adjusted_pix_clk_khz == 0) ||
801 		(adjusted_pix_clk_khz > enc110->base.features.max_hdmi_pixel_clock))
802 		return false;
803 
804 	/* DCE11 HW does not support 420 */
805 	if (!enc110->base.features.hdmi_ycbcr420_supported &&
806 			crtc_timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
807 		return false;
808 
809 	if ((!enc110->base.features.flags.bits.HDMI_6GB_EN ||
810 			enc110->base.ctx->dc->debug.hdmi20_disable) &&
811 			adjusted_pix_clk_khz >= 300000)
812 		return false;
813 	if (enc110->base.ctx->dc->debug.hdmi20_disable &&
814 		crtc_timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
815 		return false;
816 	return true;
817 }
818 
dce110_link_encoder_validate_dp_output(const struct dce110_link_encoder * enc110,const struct dc_crtc_timing * crtc_timing)819 bool dce110_link_encoder_validate_dp_output(
820 	const struct dce110_link_encoder *enc110,
821 	const struct dc_crtc_timing *crtc_timing)
822 {
823 	if (crtc_timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
824 		return false;
825 
826 	return true;
827 }
828 
dce110_link_encoder_validate_rgb_output(const struct dce110_link_encoder * enc110,const struct dc_crtc_timing * crtc_timing)829 static bool dce110_link_encoder_validate_rgb_output(
830 	const struct dce110_link_encoder *enc110,
831 	const struct dc_crtc_timing *crtc_timing)
832 {
833 	/* When the VBIOS doesn't specify any limits, use 400 MHz.
834 	 * The value comes from amdgpu_atombios_get_clock_info.
835 	 */
836 	uint32_t max_pixel_clock_khz = 400000;
837 
838 	if (enc110->base.ctx->dc_bios->fw_info_valid &&
839 	    enc110->base.ctx->dc_bios->fw_info.max_pixel_clock) {
840 		max_pixel_clock_khz =
841 			enc110->base.ctx->dc_bios->fw_info.max_pixel_clock;
842 	}
843 
844 	if (crtc_timing->pix_clk_100hz > max_pixel_clock_khz * 10)
845 		return false;
846 
847 	if (crtc_timing->display_color_depth != COLOR_DEPTH_888)
848 		return false;
849 
850 	if (crtc_timing->pixel_encoding != PIXEL_ENCODING_RGB)
851 		return false;
852 
853 	return true;
854 }
855 
dce110_link_encoder_construct(struct dce110_link_encoder * enc110,const struct encoder_init_data * init_data,const struct encoder_feature_support * enc_features,const struct dce110_link_enc_registers * link_regs,const struct dce110_link_enc_aux_registers * aux_regs,const struct dce110_link_enc_hpd_registers * hpd_regs)856 void dce110_link_encoder_construct(
857 	struct dce110_link_encoder *enc110,
858 	const struct encoder_init_data *init_data,
859 	const struct encoder_feature_support *enc_features,
860 	const struct dce110_link_enc_registers *link_regs,
861 	const struct dce110_link_enc_aux_registers *aux_regs,
862 	const struct dce110_link_enc_hpd_registers *hpd_regs)
863 {
864 	struct bp_encoder_cap_info bp_cap_info = {0};
865 	const struct dc_vbios_funcs *bp_funcs = init_data->ctx->dc_bios->funcs;
866 	enum bp_result result = BP_RESULT_OK;
867 
868 	enc110->base.funcs = &dce110_lnk_enc_funcs;
869 	enc110->base.ctx = init_data->ctx;
870 	enc110->base.id = init_data->encoder;
871 	enc110->base.analog_id = init_data->analog_encoder;
872 
873 	enc110->base.hpd_gpio = init_data->hpd_gpio;
874 	enc110->base.hpd_source = init_data->hpd_source;
875 	enc110->base.connector = init_data->connector;
876 
877 	enc110->base.preferred_engine = ENGINE_ID_UNKNOWN;
878 	enc110->base.analog_engine = init_data->analog_engine;
879 
880 	enc110->base.features = *enc_features;
881 
882 	enc110->base.transmitter = init_data->transmitter;
883 
884 	/* set the flag to indicate whether driver poll the I2C data pin
885 	 * while doing the DP sink detect
886 	 */
887 
888 /*	if (dal_adapter_service_is_feature_supported(as,
889 		FEATURE_DP_SINK_DETECT_POLL_DATA_PIN))
890 		enc110->base.features.flags.bits.
891 			DP_SINK_DETECT_POLL_DATA_PIN = true;*/
892 
893 	enc110->base.output_signals =
894 		SIGNAL_TYPE_DVI_SINGLE_LINK |
895 		SIGNAL_TYPE_DVI_DUAL_LINK |
896 		SIGNAL_TYPE_LVDS |
897 		SIGNAL_TYPE_DISPLAY_PORT |
898 		SIGNAL_TYPE_DISPLAY_PORT_MST |
899 		SIGNAL_TYPE_EDP |
900 		SIGNAL_TYPE_HDMI_TYPE_A;
901 
902 	if ((enc110->base.connector.id == CONNECTOR_ID_DUAL_LINK_DVII ||
903 	     enc110->base.connector.id == CONNECTOR_ID_SINGLE_LINK_DVII) &&
904 		enc110->base.analog_engine != ENGINE_ID_UNKNOWN)
905 		enc110->base.output_signals |= SIGNAL_TYPE_RGB;
906 
907 	/* For DCE 8.0 and 8.1, by design, UNIPHY is hardwired to DIG_BE.
908 	 * SW always assign DIG_FE 1:1 mapped to DIG_FE for non-MST UNIPHY.
909 	 * SW assign DIG_FE to non-MST UNIPHY first and MST last. So prefer
910 	 * DIG is per UNIPHY and used by SST DP, eDP, HDMI, DVI and LVDS.
911 	 * Prefer DIG assignment is decided by board design.
912 	 * For DCE 8.0, there are only max 6 UNIPHYs, we assume board design
913 	 * and VBIOS will filter out 7 UNIPHY for DCE 8.0.
914 	 * By this, adding DIGG should not hurt DCE 8.0.
915 	 * This will let DCE 8.1 share DCE 8.0 as much as possible
916 	 */
917 
918 	enc110->link_regs = link_regs;
919 	enc110->aux_regs = aux_regs;
920 	enc110->hpd_regs = hpd_regs;
921 
922 	switch (enc110->base.transmitter) {
923 	case TRANSMITTER_UNIPHY_A:
924 		enc110->base.preferred_engine = ENGINE_ID_DIGA;
925 	break;
926 	case TRANSMITTER_UNIPHY_B:
927 		enc110->base.preferred_engine = ENGINE_ID_DIGB;
928 	break;
929 	case TRANSMITTER_UNIPHY_C:
930 		enc110->base.preferred_engine = ENGINE_ID_DIGC;
931 	break;
932 	case TRANSMITTER_UNIPHY_D:
933 		enc110->base.preferred_engine = ENGINE_ID_DIGD;
934 	break;
935 	case TRANSMITTER_UNIPHY_E:
936 		enc110->base.preferred_engine = ENGINE_ID_DIGE;
937 	break;
938 	case TRANSMITTER_UNIPHY_F:
939 		enc110->base.preferred_engine = ENGINE_ID_DIGF;
940 	break;
941 	case TRANSMITTER_UNIPHY_G:
942 		enc110->base.preferred_engine = ENGINE_ID_DIGG;
943 	break;
944 	default:
945 		if (init_data->analog_engine != ENGINE_ID_UNKNOWN) {
946 			/* The connector is analog-only, ie. VGA */
947 			enc110->base.preferred_engine = init_data->analog_engine;
948 			enc110->base.output_signals = SIGNAL_TYPE_RGB;
949 			enc110->base.transmitter = TRANSMITTER_UNKNOWN;
950 			break;
951 		}
952 		ASSERT_CRITICAL(false);
953 		enc110->base.preferred_engine = ENGINE_ID_UNKNOWN;
954 	}
955 
956 	/* default to one to mirror Windows behavior */
957 	enc110->base.features.flags.bits.HDMI_6GB_EN = 1;
958 
959 	result = bp_funcs->get_encoder_cap_info(enc110->base.ctx->dc_bios,
960 						enc110->base.id, &bp_cap_info);
961 
962 	/* Override features with DCE-specific values */
963 	if (result == BP_RESULT_OK) {
964 		enc110->base.features.flags.bits.IS_HBR2_CAPABLE =
965 				bp_cap_info.DP_HBR2_EN;
966 		enc110->base.features.flags.bits.IS_HBR3_CAPABLE =
967 				bp_cap_info.DP_HBR3_EN;
968 		enc110->base.features.flags.bits.HDMI_6GB_EN = bp_cap_info.HDMI_6GB_EN;
969 	} else if (result != BP_RESULT_NORECORD) {
970 		DC_LOG_WARNING("%s: Failed to get encoder_cap_info from VBIOS with error code %d!\n",
971 				__func__,
972 				result);
973 	}
974 	if (enc110->base.ctx->dc->debug.hdmi20_disable) {
975 		enc110->base.features.flags.bits.HDMI_6GB_EN = 0;
976 	}
977 }
978 
dce110_link_encoder_validate_output_with_stream(struct link_encoder * enc,const struct dc_stream_state * stream)979 bool dce110_link_encoder_validate_output_with_stream(
980 	struct link_encoder *enc,
981 	const struct dc_stream_state *stream)
982 {
983 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
984 	bool is_valid;
985 
986 	switch (stream->signal) {
987 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
988 	case SIGNAL_TYPE_DVI_DUAL_LINK:
989 		is_valid = dce110_link_encoder_validate_dvi_output(
990 			enc110,
991 			stream->link->connector_signal,
992 			stream->signal,
993 			&stream->timing);
994 	break;
995 	case SIGNAL_TYPE_HDMI_TYPE_A:
996 		is_valid = dce110_link_encoder_validate_hdmi_output(
997 				enc110,
998 				&stream->timing,
999 				stream->phy_pix_clk);
1000 	break;
1001 	case SIGNAL_TYPE_DISPLAY_PORT:
1002 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
1003 		is_valid = dce110_link_encoder_validate_dp_output(
1004 					enc110, &stream->timing);
1005 	break;
1006 	case SIGNAL_TYPE_RGB:
1007 		is_valid = dce110_link_encoder_validate_rgb_output(
1008 					enc110, &stream->timing);
1009 	break;
1010 	case SIGNAL_TYPE_EDP:
1011 	case SIGNAL_TYPE_LVDS:
1012 		is_valid = stream->timing.pixel_encoding == PIXEL_ENCODING_RGB;
1013 	break;
1014 	case SIGNAL_TYPE_VIRTUAL:
1015 		is_valid = true;
1016 		break;
1017 	default:
1018 		is_valid = false;
1019 	break;
1020 	}
1021 
1022 	return is_valid;
1023 }
1024 
dce110_link_encoder_hw_init(struct link_encoder * enc)1025 void dce110_link_encoder_hw_init(
1026 	struct link_encoder *enc)
1027 {
1028 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1029 	struct bp_transmitter_control cntl = { 0 };
1030 	enum bp_result result;
1031 
1032 	cntl.action = TRANSMITTER_CONTROL_INIT;
1033 	cntl.engine_id = ENGINE_ID_UNKNOWN;
1034 	cntl.transmitter = enc110->base.transmitter;
1035 	cntl.connector_obj_id = enc110->base.connector;
1036 	cntl.lanes_number = LANE_COUNT_FOUR;
1037 	cntl.coherent = false;
1038 	cntl.hpd_sel = enc110->base.hpd_source;
1039 
1040 	if (enc110->base.analog_engine != ENGINE_ID_UNKNOWN) {
1041 		result = link_dac_encoder_control(enc110, ENCODER_CONTROL_INIT, 0);
1042 		if (result != BP_RESULT_OK) {
1043 			DC_LOG_ERROR("%s: Failed to execute VBIOS command table for DAC!\n",
1044 				__func__);
1045 			BREAK_TO_DEBUGGER();
1046 			return;
1047 		}
1048 	}
1049 
1050 	/* The code below is only applicable to encoders with a digital transmitter. */
1051 	if (enc110->base.transmitter == TRANSMITTER_UNKNOWN)
1052 		return;
1053 
1054 	if (enc110->base.connector.id == CONNECTOR_ID_EDP)
1055 		cntl.signal = SIGNAL_TYPE_EDP;
1056 
1057 	result = link_transmitter_control(enc110, &cntl);
1058 
1059 	if (result != BP_RESULT_OK) {
1060 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1061 			__func__);
1062 		BREAK_TO_DEBUGGER();
1063 		return;
1064 	}
1065 
1066 	if (enc110->base.connector.id == CONNECTOR_ID_LVDS) {
1067 		cntl.action = TRANSMITTER_CONTROL_BACKLIGHT_BRIGHTNESS;
1068 
1069 		result = link_transmitter_control(enc110, &cntl);
1070 
1071 		ASSERT(result == BP_RESULT_OK);
1072 
1073 	}
1074 	aux_initialize(enc110);
1075 
1076 	/* reinitialize HPD.
1077 	 * hpd_initialize() will pass DIG_FE id to HW context.
1078 	 * All other routine within HW context will use fe_engine_offset
1079 	 * as DIG_FE id even caller pass DIG_FE id.
1080 	 * So this routine must be called first. */
1081 	hpd_initialize(enc110);
1082 }
1083 
dce110_link_encoder_destroy(struct link_encoder ** enc)1084 void dce110_link_encoder_destroy(struct link_encoder **enc)
1085 {
1086 	if ((*enc)->hpd_gpio) {
1087 		dal_gpio_destroy_irq(&(*enc)->hpd_gpio);
1088 		(*enc)->hpd_gpio = NULL;
1089 	}
1090 
1091 	kfree(TO_DCE110_LINK_ENC(*enc));
1092 	*enc = NULL;
1093 }
1094 
dce110_link_encoder_setup(struct link_encoder * enc,enum signal_type signal)1095 void dce110_link_encoder_setup(
1096 	struct link_encoder *enc,
1097 	enum signal_type signal)
1098 {
1099 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1100 
1101 	switch (signal) {
1102 	case SIGNAL_TYPE_EDP:
1103 	case SIGNAL_TYPE_DISPLAY_PORT:
1104 		/* DP SST */
1105 		REG_UPDATE(DIG_BE_CNTL, DIG_MODE, 0);
1106 		break;
1107 	case SIGNAL_TYPE_LVDS:
1108 		/* LVDS */
1109 		REG_UPDATE(DIG_BE_CNTL, DIG_MODE, 1);
1110 		break;
1111 	case SIGNAL_TYPE_DVI_SINGLE_LINK:
1112 	case SIGNAL_TYPE_DVI_DUAL_LINK:
1113 		/* TMDS-DVI */
1114 		REG_UPDATE(DIG_BE_CNTL, DIG_MODE, 2);
1115 		break;
1116 	case SIGNAL_TYPE_HDMI_TYPE_A:
1117 		/* TMDS-HDMI */
1118 		REG_UPDATE(DIG_BE_CNTL, DIG_MODE, 3);
1119 		break;
1120 	case SIGNAL_TYPE_DISPLAY_PORT_MST:
1121 		/* DP MST */
1122 		REG_UPDATE(DIG_BE_CNTL, DIG_MODE, 5);
1123 		break;
1124 	case SIGNAL_TYPE_RGB:
1125 		break;
1126 	default:
1127 		ASSERT_CRITICAL(false);
1128 		/* invalid mode ! */
1129 		break;
1130 	}
1131 
1132 }
1133 
1134 /* TODO: still need depth or just pass in adjusted pixel clock? */
dce110_link_encoder_enable_tmds_output(struct link_encoder * enc,enum clock_source_id clock_source,enum dc_color_depth color_depth,enum signal_type signal,uint32_t pixel_clock)1135 void dce110_link_encoder_enable_tmds_output(
1136 	struct link_encoder *enc,
1137 	enum clock_source_id clock_source,
1138 	enum dc_color_depth color_depth,
1139 	enum signal_type signal,
1140 	uint32_t pixel_clock)
1141 {
1142 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1143 	struct bp_transmitter_control cntl = { 0 };
1144 	enum bp_result result;
1145 
1146 	/* Enable the PHY */
1147 	cntl.connector_obj_id = enc110->base.connector;
1148 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1149 	cntl.engine_id = enc->preferred_engine;
1150 	cntl.transmitter = enc110->base.transmitter;
1151 	cntl.pll_id = clock_source;
1152 	cntl.signal = signal;
1153 	if (cntl.signal == SIGNAL_TYPE_DVI_DUAL_LINK)
1154 		cntl.lanes_number = 8;
1155 	else
1156 		cntl.lanes_number = 4;
1157 
1158 	cntl.hpd_sel = enc110->base.hpd_source;
1159 
1160 	cntl.pixel_clock = pixel_clock;
1161 	cntl.color_depth = color_depth;
1162 
1163 	result = link_transmitter_control(enc110, &cntl);
1164 
1165 	if (result != BP_RESULT_OK) {
1166 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1167 			__func__);
1168 		BREAK_TO_DEBUGGER();
1169 	}
1170 }
1171 
1172 /* TODO: still need depth or just pass in adjusted pixel clock? */
dce110_link_encoder_enable_lvds_output(struct link_encoder * enc,enum clock_source_id clock_source,uint32_t pixel_clock)1173 void dce110_link_encoder_enable_lvds_output(
1174 	struct link_encoder *enc,
1175 	enum clock_source_id clock_source,
1176 	uint32_t pixel_clock)
1177 {
1178 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1179 	struct bp_transmitter_control cntl = { 0 };
1180 	enum bp_result result;
1181 
1182 	/* Enable the PHY */
1183 	cntl.connector_obj_id = enc110->base.connector;
1184 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1185 	cntl.engine_id = enc->preferred_engine;
1186 	cntl.transmitter = enc110->base.transmitter;
1187 	cntl.pll_id = clock_source;
1188 	cntl.signal = SIGNAL_TYPE_LVDS;
1189 	cntl.lanes_number = 4;
1190 
1191 	cntl.hpd_sel = enc110->base.hpd_source;
1192 
1193 	cntl.pixel_clock = pixel_clock;
1194 
1195 	result = link_transmitter_control(enc110, &cntl);
1196 
1197 	if (result != BP_RESULT_OK) {
1198 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1199 			__func__);
1200 		BREAK_TO_DEBUGGER();
1201 	}
1202 }
1203 
dce110_link_encoder_enable_analog_output(struct link_encoder * enc,uint32_t pixel_clock)1204 void dce110_link_encoder_enable_analog_output(
1205 	struct link_encoder *enc,
1206 	uint32_t pixel_clock)
1207 {
1208 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1209 	enum bp_result result;
1210 
1211 	result = link_dac_encoder_control(enc110, ENCODER_CONTROL_ENABLE, pixel_clock);
1212 
1213 	if (result != BP_RESULT_OK) {
1214 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1215 			__func__);
1216 		BREAK_TO_DEBUGGER();
1217 	}
1218 }
1219 
1220 /* enables DP PHY output */
dce110_link_encoder_enable_dp_output(struct link_encoder * enc,const struct dc_link_settings * link_settings,enum clock_source_id clock_source)1221 void dce110_link_encoder_enable_dp_output(
1222 	struct link_encoder *enc,
1223 	const struct dc_link_settings *link_settings,
1224 	enum clock_source_id clock_source)
1225 {
1226 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1227 	struct bp_transmitter_control cntl = { 0 };
1228 	enum bp_result result;
1229 
1230 	/* Enable the PHY */
1231 
1232 	/* number_of_lanes is used for pixel clock adjust,
1233 	 * but it's not passed to asic_control.
1234 	 * We need to set number of lanes manually.
1235 	 */
1236 	configure_encoder(enc110, link_settings);
1237 	cntl.connector_obj_id = enc110->base.connector;
1238 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1239 	cntl.engine_id = enc->preferred_engine;
1240 	cntl.transmitter = enc110->base.transmitter;
1241 	cntl.pll_id = clock_source;
1242 	cntl.signal = SIGNAL_TYPE_DISPLAY_PORT;
1243 	cntl.lanes_number = link_settings->lane_count;
1244 	cntl.hpd_sel = enc110->base.hpd_source;
1245 	cntl.pixel_clock = link_settings->link_rate
1246 						* LINK_RATE_REF_FREQ_IN_KHZ;
1247 	/* TODO: check if undefined works */
1248 	cntl.color_depth = COLOR_DEPTH_UNDEFINED;
1249 
1250 	result = link_transmitter_control(enc110, &cntl);
1251 
1252 	if (result != BP_RESULT_OK) {
1253 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1254 			__func__);
1255 		BREAK_TO_DEBUGGER();
1256 	}
1257 }
1258 
1259 /* enables DP PHY output in MST mode */
dce110_link_encoder_enable_dp_mst_output(struct link_encoder * enc,const struct dc_link_settings * link_settings,enum clock_source_id clock_source)1260 void dce110_link_encoder_enable_dp_mst_output(
1261 	struct link_encoder *enc,
1262 	const struct dc_link_settings *link_settings,
1263 	enum clock_source_id clock_source)
1264 {
1265 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1266 	struct bp_transmitter_control cntl = { 0 };
1267 	enum bp_result result;
1268 
1269 	/* Enable the PHY */
1270 
1271 	/* number_of_lanes is used for pixel clock adjust,
1272 	 * but it's not passed to asic_control.
1273 	 * We need to set number of lanes manually.
1274 	 */
1275 	configure_encoder(enc110, link_settings);
1276 
1277 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1278 	cntl.engine_id = ENGINE_ID_UNKNOWN;
1279 	cntl.transmitter = enc110->base.transmitter;
1280 	cntl.pll_id = clock_source;
1281 	cntl.signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
1282 	cntl.lanes_number = link_settings->lane_count;
1283 	cntl.hpd_sel = enc110->base.hpd_source;
1284 	cntl.pixel_clock = link_settings->link_rate
1285 						* LINK_RATE_REF_FREQ_IN_KHZ;
1286 	/* TODO: check if undefined works */
1287 	cntl.color_depth = COLOR_DEPTH_UNDEFINED;
1288 
1289 	result = link_transmitter_control(enc110, &cntl);
1290 
1291 	if (result != BP_RESULT_OK) {
1292 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1293 			__func__);
1294 		BREAK_TO_DEBUGGER();
1295 	}
1296 }
1297 
1298 #if defined(CONFIG_DRM_AMD_DC_SI)
1299 /* enables DP PHY output */
dce60_link_encoder_enable_dp_output(struct link_encoder * enc,const struct dc_link_settings * link_settings,enum clock_source_id clock_source)1300 static void dce60_link_encoder_enable_dp_output(
1301 	struct link_encoder *enc,
1302 	const struct dc_link_settings *link_settings,
1303 	enum clock_source_id clock_source)
1304 {
1305 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1306 	struct bp_transmitter_control cntl = { 0 };
1307 	enum bp_result result;
1308 
1309 	/* Enable the PHY */
1310 
1311 	/* number_of_lanes is used for pixel clock adjust,
1312 	 * but it's not passed to asic_control.
1313 	 * We need to set number of lanes manually.
1314 	 */
1315 	dce60_configure_encoder(enc110, link_settings);
1316 	cntl.connector_obj_id = enc110->base.connector;
1317 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1318 	cntl.engine_id = enc->preferred_engine;
1319 	cntl.transmitter = enc110->base.transmitter;
1320 	cntl.pll_id = clock_source;
1321 	cntl.signal = SIGNAL_TYPE_DISPLAY_PORT;
1322 	cntl.lanes_number = link_settings->lane_count;
1323 	cntl.hpd_sel = enc110->base.hpd_source;
1324 	cntl.pixel_clock = link_settings->link_rate
1325 						* LINK_RATE_REF_FREQ_IN_KHZ;
1326 	/* TODO: check if undefined works */
1327 	cntl.color_depth = COLOR_DEPTH_UNDEFINED;
1328 
1329 	result = link_transmitter_control(enc110, &cntl);
1330 
1331 	if (result != BP_RESULT_OK) {
1332 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1333 			__func__);
1334 		BREAK_TO_DEBUGGER();
1335 	}
1336 }
1337 
1338 /* enables DP PHY output in MST mode */
dce60_link_encoder_enable_dp_mst_output(struct link_encoder * enc,const struct dc_link_settings * link_settings,enum clock_source_id clock_source)1339 static void dce60_link_encoder_enable_dp_mst_output(
1340 	struct link_encoder *enc,
1341 	const struct dc_link_settings *link_settings,
1342 	enum clock_source_id clock_source)
1343 {
1344 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1345 	struct bp_transmitter_control cntl = { 0 };
1346 	enum bp_result result;
1347 
1348 	/* Enable the PHY */
1349 
1350 	/* number_of_lanes is used for pixel clock adjust,
1351 	 * but it's not passed to asic_control.
1352 	 * We need to set number of lanes manually.
1353 	 */
1354 	dce60_configure_encoder(enc110, link_settings);
1355 
1356 	cntl.action = TRANSMITTER_CONTROL_ENABLE;
1357 	cntl.engine_id = ENGINE_ID_UNKNOWN;
1358 	cntl.transmitter = enc110->base.transmitter;
1359 	cntl.pll_id = clock_source;
1360 	cntl.signal = SIGNAL_TYPE_DISPLAY_PORT_MST;
1361 	cntl.lanes_number = link_settings->lane_count;
1362 	cntl.hpd_sel = enc110->base.hpd_source;
1363 	cntl.pixel_clock = link_settings->link_rate
1364 						* LINK_RATE_REF_FREQ_IN_KHZ;
1365 	/* TODO: check if undefined works */
1366 	cntl.color_depth = COLOR_DEPTH_UNDEFINED;
1367 
1368 	result = link_transmitter_control(enc110, &cntl);
1369 
1370 	if (result != BP_RESULT_OK) {
1371 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1372 			__func__);
1373 		BREAK_TO_DEBUGGER();
1374 	}
1375 }
1376 #endif
1377 
1378 /*
1379  * @brief
1380  * Disable transmitter and its encoder
1381  */
dce110_link_encoder_disable_output(struct link_encoder * enc,enum signal_type signal)1382 void dce110_link_encoder_disable_output(
1383 	struct link_encoder *enc,
1384 	enum signal_type signal)
1385 {
1386 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1387 	struct bp_transmitter_control cntl = { 0 };
1388 	enum bp_result result;
1389 
1390 	if (enc->analog_engine != ENGINE_ID_UNKNOWN)
1391 		link_dac_encoder_control(enc110, ENCODER_CONTROL_DISABLE, 0);
1392 
1393 	/* The code below only applies to connectors that support digital signals. */
1394 	if (enc->transmitter == TRANSMITTER_UNKNOWN)
1395 		return;
1396 
1397 	if (!dce110_is_dig_enabled(enc)) {
1398 		/* OF_SKIP_POWER_DOWN_INACTIVE_ENCODER */
1399 		return;
1400 	}
1401 	/* Power-down RX and disable GPU PHY should be paired.
1402 	 * Disabling PHY without powering down RX may cause
1403 	 * symbol lock loss, on which we will get DP Sink interrupt. */
1404 
1405 	/* There is a case for the DP active dongles
1406 	 * where we want to disable the PHY but keep RX powered,
1407 	 * for those we need to ignore DP Sink interrupt
1408 	 * by checking lane count that has been set
1409 	 * on the last do_enable_output(). */
1410 
1411 	/* disable transmitter */
1412 	cntl.action = TRANSMITTER_CONTROL_DISABLE;
1413 	cntl.transmitter = enc110->base.transmitter;
1414 	cntl.hpd_sel = enc110->base.hpd_source;
1415 	cntl.signal = signal;
1416 	cntl.connector_obj_id = enc110->base.connector;
1417 
1418 	result = link_transmitter_control(enc110, &cntl);
1419 
1420 	if (result != BP_RESULT_OK) {
1421 		DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n",
1422 			__func__);
1423 		BREAK_TO_DEBUGGER();
1424 		return;
1425 	}
1426 
1427 	/* disable encoder */
1428 	if (dc_is_dp_signal(signal))
1429 		link_encoder_disable(enc110);
1430 }
1431 
dce110_link_encoder_dp_set_lane_settings(struct link_encoder * enc,const struct dc_link_settings * link_settings,const struct dc_lane_settings lane_settings[LANE_COUNT_DP_MAX])1432 void dce110_link_encoder_dp_set_lane_settings(
1433 	struct link_encoder *enc,
1434 	const struct dc_link_settings *link_settings,
1435 	const struct dc_lane_settings lane_settings[LANE_COUNT_DP_MAX])
1436 {
1437 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1438 	union dpcd_training_lane_set training_lane_set = { { 0 } };
1439 	int32_t lane = 0;
1440 	struct bp_transmitter_control cntl = { 0 };
1441 
1442 	if (!link_settings) {
1443 		BREAK_TO_DEBUGGER();
1444 		return;
1445 	}
1446 
1447 	cntl.action = TRANSMITTER_CONTROL_SET_VOLTAGE_AND_PREEMPASIS;
1448 	cntl.transmitter = enc110->base.transmitter;
1449 	cntl.connector_obj_id = enc110->base.connector;
1450 	cntl.lanes_number = link_settings->lane_count;
1451 	cntl.hpd_sel = enc110->base.hpd_source;
1452 	cntl.pixel_clock = link_settings->link_rate *
1453 						LINK_RATE_REF_FREQ_IN_KHZ;
1454 
1455 	for (lane = 0; lane < link_settings->lane_count; lane++) {
1456 		/* translate lane settings */
1457 
1458 		training_lane_set.bits.VOLTAGE_SWING_SET =
1459 				lane_settings[lane].VOLTAGE_SWING;
1460 		training_lane_set.bits.PRE_EMPHASIS_SET =
1461 				lane_settings[lane].PRE_EMPHASIS;
1462 
1463 		/* post cursor 2 setting only applies to HBR2 link rate */
1464 		if (link_settings->link_rate == LINK_RATE_HIGH2) {
1465 			/* this is passed to VBIOS
1466 			 * to program post cursor 2 level */
1467 
1468 			training_lane_set.bits.POST_CURSOR2_SET =
1469 					lane_settings[lane].POST_CURSOR2;
1470 		}
1471 
1472 		cntl.lane_select = lane;
1473 		cntl.lane_settings = training_lane_set.raw;
1474 
1475 		/* call VBIOS table to set voltage swing and pre-emphasis */
1476 		if (link_transmitter_control(enc110, &cntl) != BP_RESULT_OK) {
1477 			DC_LOG_ERROR("%s: Failed to execute VBIOS command table!\n", __func__);
1478 			BREAK_TO_DEBUGGER();
1479 		}
1480 	}
1481 }
1482 
1483 /* set DP PHY test and training patterns */
dce110_link_encoder_dp_set_phy_pattern(struct link_encoder * enc,const struct encoder_set_dp_phy_pattern_param * param)1484 void dce110_link_encoder_dp_set_phy_pattern(
1485 	struct link_encoder *enc,
1486 	const struct encoder_set_dp_phy_pattern_param *param)
1487 {
1488 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1489 
1490 	switch (param->dp_phy_pattern) {
1491 	case DP_TEST_PATTERN_TRAINING_PATTERN1:
1492 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 0);
1493 		break;
1494 	case DP_TEST_PATTERN_TRAINING_PATTERN2:
1495 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 1);
1496 		break;
1497 	case DP_TEST_PATTERN_TRAINING_PATTERN3:
1498 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 2);
1499 		break;
1500 	case DP_TEST_PATTERN_TRAINING_PATTERN4:
1501 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 3);
1502 		break;
1503 	case DP_TEST_PATTERN_D102:
1504 		set_dp_phy_pattern_d102(enc110);
1505 		break;
1506 	case DP_TEST_PATTERN_SYMBOL_ERROR:
1507 		set_dp_phy_pattern_symbol_error(enc110);
1508 		break;
1509 	case DP_TEST_PATTERN_PRBS7:
1510 		set_dp_phy_pattern_prbs7(enc110);
1511 		break;
1512 	case DP_TEST_PATTERN_80BIT_CUSTOM:
1513 		set_dp_phy_pattern_80bit_custom(
1514 			enc110, param->custom_pattern);
1515 		break;
1516 	case DP_TEST_PATTERN_CP2520_1:
1517 		set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 1);
1518 		break;
1519 	case DP_TEST_PATTERN_CP2520_2:
1520 		set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 2);
1521 		break;
1522 	case DP_TEST_PATTERN_CP2520_3:
1523 		set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 3);
1524 		break;
1525 	case DP_TEST_PATTERN_VIDEO_MODE: {
1526 		set_dp_phy_pattern_passthrough_mode(
1527 			enc110, param->dp_panel_mode);
1528 		break;
1529 	}
1530 
1531 	default:
1532 		/* invalid phy pattern */
1533 		ASSERT_CRITICAL(false);
1534 		break;
1535 	}
1536 }
1537 
1538 #if defined(CONFIG_DRM_AMD_DC_SI)
1539 /* set DP PHY test and training patterns */
dce60_link_encoder_dp_set_phy_pattern(struct link_encoder * enc,const struct encoder_set_dp_phy_pattern_param * param)1540 static void dce60_link_encoder_dp_set_phy_pattern(
1541 	struct link_encoder *enc,
1542 	const struct encoder_set_dp_phy_pattern_param *param)
1543 {
1544 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1545 
1546 	switch (param->dp_phy_pattern) {
1547 	case DP_TEST_PATTERN_TRAINING_PATTERN1:
1548 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 0);
1549 		break;
1550 	case DP_TEST_PATTERN_TRAINING_PATTERN2:
1551 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 1);
1552 		break;
1553 	case DP_TEST_PATTERN_TRAINING_PATTERN3:
1554 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 2);
1555 		break;
1556 	case DP_TEST_PATTERN_TRAINING_PATTERN4:
1557 		dce110_link_encoder_set_dp_phy_pattern_training_pattern(enc, 3);
1558 		break;
1559 	case DP_TEST_PATTERN_D102:
1560 		set_dp_phy_pattern_d102(enc110);
1561 		break;
1562 	case DP_TEST_PATTERN_SYMBOL_ERROR:
1563 		set_dp_phy_pattern_symbol_error(enc110);
1564 		break;
1565 	case DP_TEST_PATTERN_PRBS7:
1566 		set_dp_phy_pattern_prbs7(enc110);
1567 		break;
1568 	case DP_TEST_PATTERN_80BIT_CUSTOM:
1569 		set_dp_phy_pattern_80bit_custom(
1570 			enc110, param->custom_pattern);
1571 		break;
1572 	case DP_TEST_PATTERN_CP2520_1:
1573 		dce60_set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 1);
1574 		break;
1575 	case DP_TEST_PATTERN_CP2520_2:
1576 		dce60_set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 2);
1577 		break;
1578 	case DP_TEST_PATTERN_CP2520_3:
1579 		dce60_set_dp_phy_pattern_hbr2_compliance_cp2520_2(enc110, 3);
1580 		break;
1581 	case DP_TEST_PATTERN_VIDEO_MODE: {
1582 		dce60_set_dp_phy_pattern_passthrough_mode(
1583 			enc110, param->dp_panel_mode);
1584 		break;
1585 	}
1586 
1587 	default:
1588 		/* invalid phy pattern */
1589 		ASSERT_CRITICAL(false);
1590 		break;
1591 	}
1592 }
1593 #endif
1594 
fill_stream_allocation_row_info(const struct link_mst_stream_allocation * stream_allocation,uint32_t * src,uint32_t * slots)1595 static void fill_stream_allocation_row_info(
1596 	const struct link_mst_stream_allocation *stream_allocation,
1597 	uint32_t *src,
1598 	uint32_t *slots)
1599 {
1600 	const struct stream_encoder *stream_enc = stream_allocation->stream_enc;
1601 
1602 	if (stream_enc) {
1603 		*src = stream_enc->id;
1604 		*slots = stream_allocation->slot_count;
1605 	} else {
1606 		*src = 0;
1607 		*slots = 0;
1608 	}
1609 }
1610 
1611 /* programs DP MST VC payload allocation */
dce110_link_encoder_update_mst_stream_allocation_table(struct link_encoder * enc,const struct link_mst_stream_allocation_table * table)1612 void dce110_link_encoder_update_mst_stream_allocation_table(
1613 	struct link_encoder *enc,
1614 	const struct link_mst_stream_allocation_table *table)
1615 {
1616 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1617 	uint32_t value1 = 0;
1618 	uint32_t value2 = 0;
1619 	uint32_t slots = 0;
1620 	uint32_t src = 0;
1621 	uint32_t retries = 0;
1622 
1623 	/* For CZ, there are only 3 pipes. So Virtual channel is up 3.*/
1624 
1625 	/* --- Set MSE Stream Attribute -
1626 	 * Setup VC Payload Table on Tx Side,
1627 	 * Issue allocation change trigger
1628 	 * to commit payload on both tx and rx side */
1629 
1630 	/* we should clean-up table each time */
1631 
1632 	if (table->stream_count >= 1) {
1633 		fill_stream_allocation_row_info(
1634 			&table->stream_allocations[0],
1635 			&src,
1636 			&slots);
1637 	} else {
1638 		src = 0;
1639 		slots = 0;
1640 	}
1641 
1642 	REG_UPDATE_2(DP_MSE_SAT0,
1643 			DP_MSE_SAT_SRC0, src,
1644 			DP_MSE_SAT_SLOT_COUNT0, slots);
1645 
1646 	if (table->stream_count >= 2) {
1647 		fill_stream_allocation_row_info(
1648 			&table->stream_allocations[1],
1649 			&src,
1650 			&slots);
1651 	} else {
1652 		src = 0;
1653 		slots = 0;
1654 	}
1655 
1656 	REG_UPDATE_2(DP_MSE_SAT0,
1657 			DP_MSE_SAT_SRC1, src,
1658 			DP_MSE_SAT_SLOT_COUNT1, slots);
1659 
1660 	if (table->stream_count >= 3) {
1661 		fill_stream_allocation_row_info(
1662 			&table->stream_allocations[2],
1663 			&src,
1664 			&slots);
1665 	} else {
1666 		src = 0;
1667 		slots = 0;
1668 	}
1669 
1670 	REG_UPDATE_2(DP_MSE_SAT1,
1671 			DP_MSE_SAT_SRC2, src,
1672 			DP_MSE_SAT_SLOT_COUNT2, slots);
1673 
1674 	if (table->stream_count >= 4) {
1675 		fill_stream_allocation_row_info(
1676 			&table->stream_allocations[3],
1677 			&src,
1678 			&slots);
1679 	} else {
1680 		src = 0;
1681 		slots = 0;
1682 	}
1683 
1684 	REG_UPDATE_2(DP_MSE_SAT1,
1685 			DP_MSE_SAT_SRC3, src,
1686 			DP_MSE_SAT_SLOT_COUNT3, slots);
1687 
1688 	/* --- wait for transaction finish */
1689 
1690 	/* send allocation change trigger (ACT) ?
1691 	 * this step first sends the ACT,
1692 	 * then double buffers the SAT into the hardware
1693 	 * making the new allocation active on the DP MST mode link */
1694 
1695 
1696 	/* DP_MSE_SAT_UPDATE:
1697 	 * 0 - No Action
1698 	 * 1 - Update SAT with trigger
1699 	 * 2 - Update SAT without trigger */
1700 
1701 	REG_UPDATE(DP_MSE_SAT_UPDATE,
1702 			DP_MSE_SAT_UPDATE, 1);
1703 
1704 	/* wait for update to complete
1705 	 * (i.e. DP_MSE_SAT_UPDATE field is reset to 0)
1706 	 * then wait for the transmission
1707 	 * of at least 16 MTP headers on immediate local link.
1708 	 * i.e. DP_MSE_16_MTP_KEEPOUT field (read only) is reset to 0
1709 	 * a value of 1 indicates that DP MST mode
1710 	 * is in the 16 MTP keepout region after a VC has been added.
1711 	 * MST stream bandwidth (VC rate) can be configured
1712 	 * after this bit is cleared */
1713 
1714 	do {
1715 		udelay(10);
1716 
1717 		REG_READ(DP_MSE_SAT_UPDATE);
1718 
1719 		REG_GET(DP_MSE_SAT_UPDATE,
1720 				DP_MSE_SAT_UPDATE, &value1);
1721 
1722 		REG_GET(DP_MSE_SAT_UPDATE,
1723 				DP_MSE_16_MTP_KEEPOUT, &value2);
1724 
1725 		/* bit field DP_MSE_SAT_UPDATE is set to 1 already */
1726 		if (!value1 && !value2)
1727 			break;
1728 		++retries;
1729 	} while (retries < DP_MST_UPDATE_MAX_RETRY);
1730 }
1731 
dce110_link_encoder_connect_dig_be_to_fe(struct link_encoder * enc,enum engine_id engine,bool connect)1732 void dce110_link_encoder_connect_dig_be_to_fe(
1733 	struct link_encoder *enc,
1734 	enum engine_id engine,
1735 	bool connect)
1736 {
1737 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1738 	uint32_t field;
1739 
1740 	if (engine != ENGINE_ID_UNKNOWN) {
1741 
1742 		REG_GET(DIG_BE_CNTL, DIG_FE_SOURCE_SELECT, &field);
1743 
1744 		if (connect)
1745 			field |= get_frontend_source(engine);
1746 		else
1747 			field &= ~get_frontend_source(engine);
1748 
1749 		REG_UPDATE(DIG_BE_CNTL, DIG_FE_SOURCE_SELECT, field);
1750 	}
1751 }
1752 
dce110_link_encoder_enable_hpd(struct link_encoder * enc)1753 void dce110_link_encoder_enable_hpd(struct link_encoder *enc)
1754 {
1755 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1756 	struct dc_context *ctx = enc110->base.ctx;
1757 	uint32_t addr = HPD_REG(DC_HPD_CONTROL);
1758 	uint32_t hpd_enable = 0;
1759 	uint32_t value = dm_read_reg(ctx, addr);
1760 
1761 	hpd_enable = get_reg_field_value(hpd_enable, DC_HPD_CONTROL, DC_HPD_EN);
1762 
1763 	if (hpd_enable == 0)
1764 		set_reg_field_value(value, 1, DC_HPD_CONTROL, DC_HPD_EN);
1765 }
1766 
dce110_link_encoder_disable_hpd(struct link_encoder * enc)1767 void dce110_link_encoder_disable_hpd(struct link_encoder *enc)
1768 {
1769 	struct dce110_link_encoder *enc110 = TO_DCE110_LINK_ENC(enc);
1770 	struct dc_context *ctx = enc110->base.ctx;
1771 	uint32_t addr = HPD_REG(DC_HPD_CONTROL);
1772 	uint32_t value = dm_read_reg(ctx, addr);
1773 
1774 	set_reg_field_value(value, 0, DC_HPD_CONTROL, DC_HPD_EN);
1775 }
1776 
dce110_link_encoder_get_max_link_cap(struct link_encoder * enc,struct dc_link_settings * link_settings)1777 void dce110_link_encoder_get_max_link_cap(struct link_encoder *enc,
1778 	struct dc_link_settings *link_settings)
1779 {
1780 	/* Set Default link settings */
1781 	struct dc_link_settings max_link_cap = {LANE_COUNT_FOUR, LINK_RATE_HIGH,
1782 			LINK_SPREAD_05_DOWNSPREAD_30KHZ, false, 0};
1783 
1784 	/* Higher link settings based on feature supported */
1785 	if (enc->features.flags.bits.IS_HBR2_CAPABLE)
1786 		max_link_cap.link_rate = LINK_RATE_HIGH2;
1787 
1788 	if (enc->features.flags.bits.IS_HBR3_CAPABLE)
1789 		max_link_cap.link_rate = LINK_RATE_HIGH3;
1790 
1791 	*link_settings = max_link_cap;
1792 }
1793 
dce110_get_hpd_state(struct link_encoder * enc)1794 bool dce110_get_hpd_state(struct link_encoder *enc)
1795 {
1796 	uint32_t state = 0;
1797 
1798 	if (!enc->hpd_gpio)
1799 		return false;
1800 
1801 	dal_gpio_lock_pin(enc->hpd_gpio);
1802 	dal_gpio_get_value(enc->hpd_gpio, &state);
1803 	dal_gpio_unlock_pin(enc->hpd_gpio);
1804 
1805 	return state;
1806 }
1807 
dce110_program_hpd_filter(struct link_encoder * enc,int delay_on_connect_in_ms,int delay_on_disconnect_in_ms)1808 bool dce110_program_hpd_filter(struct link_encoder *enc, int delay_on_connect_in_ms, int delay_on_disconnect_in_ms)
1809 {
1810 	/* Setup HPD filtering */
1811 	if (enc->hpd_gpio && dal_gpio_lock_pin(enc->hpd_gpio) == GPIO_RESULT_OK) {
1812 		struct gpio_hpd_config config;
1813 
1814 		config.delay_on_connect = delay_on_connect_in_ms;
1815 		config.delay_on_disconnect = delay_on_disconnect_in_ms;
1816 
1817 		dal_irq_setup_hpd_filter(enc->hpd_gpio, &config);
1818 
1819 		dal_gpio_unlock_pin(enc->hpd_gpio);
1820 
1821 		return true;
1822 	} else {
1823 		ASSERT(0);
1824 		return false;
1825 	}
1826 }
1827 
1828 #if defined(CONFIG_DRM_AMD_DC_SI)
1829 static const struct link_encoder_funcs dce60_lnk_enc_funcs = {
1830 	.validate_output_with_stream =
1831 		dce110_link_encoder_validate_output_with_stream,
1832 	.hw_init = dce110_link_encoder_hw_init,
1833 	.setup = dce110_link_encoder_setup,
1834 	.enable_tmds_output = dce110_link_encoder_enable_tmds_output,
1835 	.enable_dp_output = dce60_link_encoder_enable_dp_output,
1836 	.enable_dp_mst_output = dce60_link_encoder_enable_dp_mst_output,
1837 	.enable_lvds_output = dce110_link_encoder_enable_lvds_output,
1838 	.enable_analog_output = dce110_link_encoder_enable_analog_output,
1839 	.disable_output = dce110_link_encoder_disable_output,
1840 	.dp_set_lane_settings = dce110_link_encoder_dp_set_lane_settings,
1841 	.dp_set_phy_pattern = dce60_link_encoder_dp_set_phy_pattern,
1842 	.update_mst_stream_allocation_table =
1843 		dce110_link_encoder_update_mst_stream_allocation_table,
1844 	.psr_program_dp_dphy_fast_training =
1845 			dce110_psr_program_dp_dphy_fast_training,
1846 	.psr_program_secondary_packet = dce110_psr_program_secondary_packet,
1847 	.connect_dig_be_to_fe = dce110_link_encoder_connect_dig_be_to_fe,
1848 	.enable_hpd = dce110_link_encoder_enable_hpd,
1849 	.disable_hpd = dce110_link_encoder_disable_hpd,
1850 	.is_dig_enabled = dce110_is_dig_enabled,
1851 	.destroy = dce110_link_encoder_destroy,
1852 	.get_max_link_cap = dce110_link_encoder_get_max_link_cap,
1853 	.get_dig_frontend = dce110_get_dig_frontend,
1854 	.get_hpd_state = dce110_get_hpd_state,
1855 	.program_hpd_filter = dce110_program_hpd_filter,
1856 };
1857 
dce60_link_encoder_construct(struct dce110_link_encoder * enc110,const struct encoder_init_data * init_data,const struct encoder_feature_support * enc_features,const struct dce110_link_enc_registers * link_regs,const struct dce110_link_enc_aux_registers * aux_regs,const struct dce110_link_enc_hpd_registers * hpd_regs)1858 void dce60_link_encoder_construct(
1859 	struct dce110_link_encoder *enc110,
1860 	const struct encoder_init_data *init_data,
1861 	const struct encoder_feature_support *enc_features,
1862 	const struct dce110_link_enc_registers *link_regs,
1863 	const struct dce110_link_enc_aux_registers *aux_regs,
1864 	const struct dce110_link_enc_hpd_registers *hpd_regs)
1865 {
1866 	struct bp_encoder_cap_info bp_cap_info = {0};
1867 	const struct dc_vbios_funcs *bp_funcs = init_data->ctx->dc_bios->funcs;
1868 	enum bp_result result = BP_RESULT_OK;
1869 
1870 	enc110->base.funcs = &dce60_lnk_enc_funcs;
1871 	enc110->base.ctx = init_data->ctx;
1872 	enc110->base.id = init_data->encoder;
1873 	enc110->base.analog_id = init_data->analog_encoder;
1874 
1875 	enc110->base.hpd_gpio = init_data->hpd_gpio;
1876 	enc110->base.hpd_source = init_data->hpd_source;
1877 	enc110->base.connector = init_data->connector;
1878 
1879 	enc110->base.preferred_engine = ENGINE_ID_UNKNOWN;
1880 	enc110->base.analog_engine = init_data->analog_engine;
1881 
1882 	enc110->base.features = *enc_features;
1883 
1884 	enc110->base.transmitter = init_data->transmitter;
1885 
1886 	/* set the flag to indicate whether driver poll the I2C data pin
1887 	 * while doing the DP sink detect
1888 	 */
1889 
1890 /*	if (dal_adapter_service_is_feature_supported(as,
1891 		FEATURE_DP_SINK_DETECT_POLL_DATA_PIN))
1892 		enc110->base.features.flags.bits.
1893 			DP_SINK_DETECT_POLL_DATA_PIN = true;*/
1894 
1895 	enc110->base.output_signals =
1896 		SIGNAL_TYPE_DVI_SINGLE_LINK |
1897 		SIGNAL_TYPE_DVI_DUAL_LINK |
1898 		SIGNAL_TYPE_LVDS |
1899 		SIGNAL_TYPE_DISPLAY_PORT |
1900 		SIGNAL_TYPE_DISPLAY_PORT_MST |
1901 		SIGNAL_TYPE_EDP |
1902 		SIGNAL_TYPE_HDMI_TYPE_A;
1903 
1904 	if ((enc110->base.connector.id == CONNECTOR_ID_DUAL_LINK_DVII ||
1905 	     enc110->base.connector.id == CONNECTOR_ID_SINGLE_LINK_DVII) &&
1906 		enc110->base.analog_engine != ENGINE_ID_UNKNOWN)
1907 		enc110->base.output_signals |= SIGNAL_TYPE_RGB;
1908 
1909 	/* For DCE 8.0 and 8.1, by design, UNIPHY is hardwired to DIG_BE.
1910 	 * SW always assign DIG_FE 1:1 mapped to DIG_FE for non-MST UNIPHY.
1911 	 * SW assign DIG_FE to non-MST UNIPHY first and MST last. So prefer
1912 	 * DIG is per UNIPHY and used by SST DP, eDP, HDMI, DVI and LVDS.
1913 	 * Prefer DIG assignment is decided by board design.
1914 	 * For DCE 8.0, there are only max 6 UNIPHYs, we assume board design
1915 	 * and VBIOS will filter out 7 UNIPHY for DCE 8.0.
1916 	 * By this, adding DIGG should not hurt DCE 8.0.
1917 	 * This will let DCE 8.1 share DCE 8.0 as much as possible
1918 	 */
1919 
1920 	enc110->link_regs = link_regs;
1921 	enc110->aux_regs = aux_regs;
1922 	enc110->hpd_regs = hpd_regs;
1923 
1924 	switch (enc110->base.transmitter) {
1925 	case TRANSMITTER_UNIPHY_A:
1926 		enc110->base.preferred_engine = ENGINE_ID_DIGA;
1927 	break;
1928 	case TRANSMITTER_UNIPHY_B:
1929 		enc110->base.preferred_engine = ENGINE_ID_DIGB;
1930 	break;
1931 	case TRANSMITTER_UNIPHY_C:
1932 		enc110->base.preferred_engine = ENGINE_ID_DIGC;
1933 	break;
1934 	case TRANSMITTER_UNIPHY_D:
1935 		enc110->base.preferred_engine = ENGINE_ID_DIGD;
1936 	break;
1937 	case TRANSMITTER_UNIPHY_E:
1938 		enc110->base.preferred_engine = ENGINE_ID_DIGE;
1939 	break;
1940 	case TRANSMITTER_UNIPHY_F:
1941 		enc110->base.preferred_engine = ENGINE_ID_DIGF;
1942 	break;
1943 	case TRANSMITTER_UNIPHY_G:
1944 		enc110->base.preferred_engine = ENGINE_ID_DIGG;
1945 	break;
1946 	default:
1947 		if (init_data->analog_engine != ENGINE_ID_UNKNOWN) {
1948 			/* The connector is analog-only, ie. VGA */
1949 			enc110->base.preferred_engine = init_data->analog_engine;
1950 			enc110->base.output_signals = SIGNAL_TYPE_RGB;
1951 			enc110->base.transmitter = TRANSMITTER_UNKNOWN;
1952 			break;
1953 		}
1954 		ASSERT_CRITICAL(false);
1955 		enc110->base.preferred_engine = ENGINE_ID_UNKNOWN;
1956 	}
1957 
1958 	/* default to one to mirror Windows behavior */
1959 	enc110->base.features.flags.bits.HDMI_6GB_EN = 1;
1960 
1961 	result = bp_funcs->get_encoder_cap_info(enc110->base.ctx->dc_bios,
1962 						enc110->base.id, &bp_cap_info);
1963 
1964 	/* Override features with DCE-specific values */
1965 	if (result == BP_RESULT_OK) {
1966 		enc110->base.features.flags.bits.IS_HBR2_CAPABLE =
1967 				bp_cap_info.DP_HBR2_EN;
1968 		enc110->base.features.flags.bits.IS_HBR3_CAPABLE =
1969 				bp_cap_info.DP_HBR3_EN;
1970 		enc110->base.features.flags.bits.HDMI_6GB_EN = bp_cap_info.HDMI_6GB_EN;
1971 	} else if (result != BP_RESULT_NORECORD) {
1972 		DC_LOG_WARNING("%s: Failed to get encoder_cap_info from VBIOS with error code %d!\n",
1973 				__func__,
1974 				result);
1975 	}
1976 	if (enc110->base.ctx->dc->debug.hdmi20_disable) {
1977 		enc110->base.features.flags.bits.HDMI_6GB_EN = 0;
1978 	}
1979 }
1980 #endif
1981