xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision cd803bb4f8a8d7b2c27c464ae32af14005cce7a1)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2012 Intel Corporation
3379bc100SJani Nikula  *
4379bc100SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5379bc100SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6379bc100SJani Nikula  * to deal in the Software without restriction, including without limitation
7379bc100SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8379bc100SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9379bc100SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10379bc100SJani Nikula  *
11379bc100SJani Nikula  * The above copyright notice and this permission notice (including the next
12379bc100SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13379bc100SJani Nikula  * Software.
14379bc100SJani Nikula  *
15379bc100SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16379bc100SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17379bc100SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18379bc100SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19379bc100SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20379bc100SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21379bc100SJani Nikula  * IN THE SOFTWARE.
22379bc100SJani Nikula  *
23379bc100SJani Nikula  * Authors:
24379bc100SJani Nikula  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25379bc100SJani Nikula  *
26379bc100SJani Nikula  */
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_scdc_helper.h>
29379bc100SJani Nikula 
30379bc100SJani Nikula #include "i915_drv.h"
31379bc100SJani Nikula #include "intel_audio.h"
32379bc100SJani Nikula #include "intel_combo_phy.h"
33379bc100SJani Nikula #include "intel_connector.h"
34379bc100SJani Nikula #include "intel_ddi.h"
351d455f8dSJani Nikula #include "intel_display_types.h"
36379bc100SJani Nikula #include "intel_dp.h"
37c59053dcSJosé Roberto de Souza #include "intel_dp_mst.h"
38379bc100SJani Nikula #include "intel_dp_link_training.h"
39379bc100SJani Nikula #include "intel_dpio_phy.h"
40379bc100SJani Nikula #include "intel_dsi.h"
41379bc100SJani Nikula #include "intel_fifo_underrun.h"
42379bc100SJani Nikula #include "intel_gmbus.h"
43379bc100SJani Nikula #include "intel_hdcp.h"
44379bc100SJani Nikula #include "intel_hdmi.h"
45379bc100SJani Nikula #include "intel_hotplug.h"
46379bc100SJani Nikula #include "intel_lspcon.h"
47379bc100SJani Nikula #include "intel_panel.h"
48379bc100SJani Nikula #include "intel_psr.h"
49bdacf087SAnshuman Gupta #include "intel_sprite.h"
50bc85328fSImre Deak #include "intel_tc.h"
51379bc100SJani Nikula #include "intel_vdsc.h"
52379bc100SJani Nikula 
53379bc100SJani Nikula struct ddi_buf_trans {
54379bc100SJani Nikula 	u32 trans1;	/* balance leg enable, de-emph level */
55379bc100SJani Nikula 	u32 trans2;	/* vref sel, vswing */
56379bc100SJani Nikula 	u8 i_boost;	/* SKL: I_boost; valid: 0x0, 0x1, 0x3, 0x7 */
57379bc100SJani Nikula };
58379bc100SJani Nikula 
59379bc100SJani Nikula static const u8 index_to_dp_signal_levels[] = {
60379bc100SJani Nikula 	[0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
61379bc100SJani Nikula 	[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
62379bc100SJani Nikula 	[2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
63379bc100SJani Nikula 	[3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
64379bc100SJani Nikula 	[4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
65379bc100SJani Nikula 	[5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
66379bc100SJani Nikula 	[6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
67379bc100SJani Nikula 	[7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
68379bc100SJani Nikula 	[8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
69379bc100SJani Nikula 	[9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
70379bc100SJani Nikula };
71379bc100SJani Nikula 
72379bc100SJani Nikula /* HDMI/DVI modes ignore everything but the last 2 items. So we share
73379bc100SJani Nikula  * them for both DP and FDI transports, allowing those ports to
74379bc100SJani Nikula  * automatically adapt to HDMI connections as well
75379bc100SJani Nikula  */
76379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_dp[] = {
77379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0006000E, 0x0 },
78379bc100SJani Nikula 	{ 0x00D75FFF, 0x0005000A, 0x0 },
79379bc100SJani Nikula 	{ 0x00C30FFF, 0x00040006, 0x0 },
80379bc100SJani Nikula 	{ 0x80AAAFFF, 0x000B0000, 0x0 },
81379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000A, 0x0 },
82379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0004, 0x0 },
83379bc100SJani Nikula 	{ 0x80C30FFF, 0x000B0000, 0x0 },
84379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00040006, 0x0 },
85379bc100SJani Nikula 	{ 0x80D75FFF, 0x000B0000, 0x0 },
86379bc100SJani Nikula };
87379bc100SJani Nikula 
88379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_fdi[] = {
89379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },
90379bc100SJani Nikula 	{ 0x00D75FFF, 0x000F000A, 0x0 },
91379bc100SJani Nikula 	{ 0x00C30FFF, 0x00060006, 0x0 },
92379bc100SJani Nikula 	{ 0x00AAAFFF, 0x001E0000, 0x0 },
93379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000F000A, 0x0 },
94379bc100SJani Nikula 	{ 0x00D75FFF, 0x00160004, 0x0 },
95379bc100SJani Nikula 	{ 0x00C30FFF, 0x001E0000, 0x0 },
96379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00060006, 0x0 },
97379bc100SJani Nikula 	{ 0x00D75FFF, 0x001E0000, 0x0 },
98379bc100SJani Nikula };
99379bc100SJani Nikula 
100379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_hdmi[] = {
101379bc100SJani Nikula 					/* Idx	NT mV d	T mV d	db	*/
102379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0006000E, 0x0 },/* 0:	400	400	0	*/
103379bc100SJani Nikula 	{ 0x00E79FFF, 0x000E000C, 0x0 },/* 1:	400	500	2	*/
104379bc100SJani Nikula 	{ 0x00D75FFF, 0x0005000A, 0x0 },/* 2:	400	600	3.5	*/
105379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000A, 0x0 },/* 3:	600	600	0	*/
106379bc100SJani Nikula 	{ 0x00E79FFF, 0x001D0007, 0x0 },/* 4:	600	750	2	*/
107379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0004, 0x0 },/* 5:	600	900	3.5	*/
108379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00040006, 0x0 },/* 6:	800	800	0	*/
109379bc100SJani Nikula 	{ 0x80E79FFF, 0x00030002, 0x0 },/* 7:	800	1000	2	*/
110379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00140005, 0x0 },/* 8:	850	850	0	*/
111379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000C0004, 0x0 },/* 9:	900	900	0	*/
112379bc100SJani Nikula 	{ 0x00FFFFFF, 0x001C0003, 0x0 },/* 10:	950	950	0	*/
113379bc100SJani Nikula 	{ 0x80FFFFFF, 0x00030002, 0x0 },/* 11:	1000	1000	0	*/
114379bc100SJani Nikula };
115379bc100SJani Nikula 
116379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_edp[] = {
117379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00000012, 0x0 },
118379bc100SJani Nikula 	{ 0x00EBAFFF, 0x00020011, 0x0 },
119379bc100SJani Nikula 	{ 0x00C71FFF, 0x0006000F, 0x0 },
120379bc100SJani Nikula 	{ 0x00AAAFFF, 0x000E000A, 0x0 },
121379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00020011, 0x0 },
122379bc100SJani Nikula 	{ 0x00DB6FFF, 0x0005000F, 0x0 },
123379bc100SJani Nikula 	{ 0x00BEEFFF, 0x000A000C, 0x0 },
124379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000F, 0x0 },
125379bc100SJani Nikula 	{ 0x00DB6FFF, 0x000A000C, 0x0 },
126379bc100SJani Nikula };
127379bc100SJani Nikula 
128379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_dp[] = {
129379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },
130379bc100SJani Nikula 	{ 0x00D75FFF, 0x000E000A, 0x0 },
131379bc100SJani Nikula 	{ 0x00BEFFFF, 0x00140006, 0x0 },
132379bc100SJani Nikula 	{ 0x80B2CFFF, 0x001B0002, 0x0 },
133379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000E000A, 0x0 },
134379bc100SJani Nikula 	{ 0x00DB6FFF, 0x00160005, 0x0 },
135379bc100SJani Nikula 	{ 0x80C71FFF, 0x001A0002, 0x0 },
136379bc100SJani Nikula 	{ 0x00F7DFFF, 0x00180004, 0x0 },
137379bc100SJani Nikula 	{ 0x80D75FFF, 0x001B0002, 0x0 },
138379bc100SJani Nikula };
139379bc100SJani Nikula 
140379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_fdi[] = {
141379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0001000E, 0x0 },
142379bc100SJani Nikula 	{ 0x00D75FFF, 0x0004000A, 0x0 },
143379bc100SJani Nikula 	{ 0x00C30FFF, 0x00070006, 0x0 },
144379bc100SJani Nikula 	{ 0x00AAAFFF, 0x000C0000, 0x0 },
145379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0004000A, 0x0 },
146379bc100SJani Nikula 	{ 0x00D75FFF, 0x00090004, 0x0 },
147379bc100SJani Nikula 	{ 0x00C30FFF, 0x000C0000, 0x0 },
148379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00070006, 0x0 },
149379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0000, 0x0 },
150379bc100SJani Nikula };
151379bc100SJani Nikula 
152379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_hdmi[] = {
153379bc100SJani Nikula 					/* Idx	NT mV d	T mV df	db	*/
154379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },/* 0:	400	400	0	*/
155379bc100SJani Nikula 	{ 0x00D75FFF, 0x000E000A, 0x0 },/* 1:	400	600	3.5	*/
156379bc100SJani Nikula 	{ 0x00BEFFFF, 0x00140006, 0x0 },/* 2:	400	800	6	*/
157379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0009000D, 0x0 },/* 3:	450	450	0	*/
158379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000E000A, 0x0 },/* 4:	600	600	0	*/
159379bc100SJani Nikula 	{ 0x00D7FFFF, 0x00140006, 0x0 },/* 5:	600	800	2.5	*/
160379bc100SJani Nikula 	{ 0x80CB2FFF, 0x001B0002, 0x0 },/* 6:	600	1000	4.5	*/
161379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00140006, 0x0 },/* 7:	800	800	0	*/
162379bc100SJani Nikula 	{ 0x80E79FFF, 0x001B0002, 0x0 },/* 8:	800	1000	2	*/
163379bc100SJani Nikula 	{ 0x80FFFFFF, 0x001B0002, 0x0 },/* 9:	1000	1000	0	*/
164379bc100SJani Nikula };
165379bc100SJani Nikula 
166379bc100SJani Nikula /* Skylake H and S */
167379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_dp[] = {
168379bc100SJani Nikula 	{ 0x00002016, 0x000000A0, 0x0 },
169379bc100SJani Nikula 	{ 0x00005012, 0x0000009B, 0x0 },
170379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
171379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
172379bc100SJani Nikula 	{ 0x00002016, 0x0000009B, 0x0 },
173379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
174379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
175379bc100SJani Nikula 	{ 0x00002016, 0x000000DF, 0x0 },
176379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
177379bc100SJani Nikula };
178379bc100SJani Nikula 
179379bc100SJani Nikula /* Skylake U */
180379bc100SJani Nikula static const struct ddi_buf_trans skl_u_ddi_translations_dp[] = {
181379bc100SJani Nikula 	{ 0x0000201B, 0x000000A2, 0x0 },
182379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
183379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x1 },
184379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
185379bc100SJani Nikula 	{ 0x0000201B, 0x0000009D, 0x0 },
186379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
187379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
188379bc100SJani Nikula 	{ 0x00002016, 0x00000088, 0x0 },
189379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
190379bc100SJani Nikula };
191379bc100SJani Nikula 
192379bc100SJani Nikula /* Skylake Y */
193379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_dp[] = {
194379bc100SJani Nikula 	{ 0x00000018, 0x000000A2, 0x0 },
195379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
196379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
197379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x3 },
198379bc100SJani Nikula 	{ 0x00000018, 0x0000009D, 0x0 },
199379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
200379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
201379bc100SJani Nikula 	{ 0x00000018, 0x00000088, 0x0 },
202379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
203379bc100SJani Nikula };
204379bc100SJani Nikula 
205379bc100SJani Nikula /* Kabylake H and S */
206379bc100SJani Nikula static const struct ddi_buf_trans kbl_ddi_translations_dp[] = {
207379bc100SJani Nikula 	{ 0x00002016, 0x000000A0, 0x0 },
208379bc100SJani Nikula 	{ 0x00005012, 0x0000009B, 0x0 },
209379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
210379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
211379bc100SJani Nikula 	{ 0x00002016, 0x0000009B, 0x0 },
212379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
213379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
214379bc100SJani Nikula 	{ 0x00002016, 0x00000097, 0x0 },
215379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
216379bc100SJani Nikula };
217379bc100SJani Nikula 
218379bc100SJani Nikula /* Kabylake U */
219379bc100SJani Nikula static const struct ddi_buf_trans kbl_u_ddi_translations_dp[] = {
220379bc100SJani Nikula 	{ 0x0000201B, 0x000000A1, 0x0 },
221379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
222379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
223379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x3 },
224379bc100SJani Nikula 	{ 0x0000201B, 0x0000009D, 0x0 },
225379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
226379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
227379bc100SJani Nikula 	{ 0x00002016, 0x0000004F, 0x0 },
228379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
229379bc100SJani Nikula };
230379bc100SJani Nikula 
231379bc100SJani Nikula /* Kabylake Y */
232379bc100SJani Nikula static const struct ddi_buf_trans kbl_y_ddi_translations_dp[] = {
233379bc100SJani Nikula 	{ 0x00001017, 0x000000A1, 0x0 },
234379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
235379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
236379bc100SJani Nikula 	{ 0x8000800F, 0x000000C0, 0x3 },
237379bc100SJani Nikula 	{ 0x00001017, 0x0000009D, 0x0 },
238379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
239379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
240379bc100SJani Nikula 	{ 0x00001017, 0x0000004C, 0x0 },
241379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
242379bc100SJani Nikula };
243379bc100SJani Nikula 
244379bc100SJani Nikula /*
245379bc100SJani Nikula  * Skylake/Kabylake H and S
246379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
247379bc100SJani Nikula  */
248379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_edp[] = {
249379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
250379bc100SJani Nikula 	{ 0x00004013, 0x000000A9, 0x0 },
251379bc100SJani Nikula 	{ 0x00007011, 0x000000A2, 0x0 },
252379bc100SJani Nikula 	{ 0x00009010, 0x0000009C, 0x0 },
253379bc100SJani Nikula 	{ 0x00000018, 0x000000A9, 0x0 },
254379bc100SJani Nikula 	{ 0x00006013, 0x000000A2, 0x0 },
255379bc100SJani Nikula 	{ 0x00007011, 0x000000A6, 0x0 },
256379bc100SJani Nikula 	{ 0x00000018, 0x000000AB, 0x0 },
257379bc100SJani Nikula 	{ 0x00007013, 0x0000009F, 0x0 },
258379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
259379bc100SJani Nikula };
260379bc100SJani Nikula 
261379bc100SJani Nikula /*
262379bc100SJani Nikula  * Skylake/Kabylake U
263379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
264379bc100SJani Nikula  */
265379bc100SJani Nikula static const struct ddi_buf_trans skl_u_ddi_translations_edp[] = {
266379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
267379bc100SJani Nikula 	{ 0x00004013, 0x000000A9, 0x0 },
268379bc100SJani Nikula 	{ 0x00007011, 0x000000A2, 0x0 },
269379bc100SJani Nikula 	{ 0x00009010, 0x0000009C, 0x0 },
270379bc100SJani Nikula 	{ 0x00000018, 0x000000A9, 0x0 },
271379bc100SJani Nikula 	{ 0x00006013, 0x000000A2, 0x0 },
272379bc100SJani Nikula 	{ 0x00007011, 0x000000A6, 0x0 },
273379bc100SJani Nikula 	{ 0x00002016, 0x000000AB, 0x0 },
274379bc100SJani Nikula 	{ 0x00005013, 0x0000009F, 0x0 },
275379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
276379bc100SJani Nikula };
277379bc100SJani Nikula 
278379bc100SJani Nikula /*
279379bc100SJani Nikula  * Skylake/Kabylake Y
280379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
281379bc100SJani Nikula  */
282379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_edp[] = {
283379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
284379bc100SJani Nikula 	{ 0x00004013, 0x000000AB, 0x0 },
285379bc100SJani Nikula 	{ 0x00007011, 0x000000A4, 0x0 },
286379bc100SJani Nikula 	{ 0x00009010, 0x000000DF, 0x0 },
287379bc100SJani Nikula 	{ 0x00000018, 0x000000AA, 0x0 },
288379bc100SJani Nikula 	{ 0x00006013, 0x000000A4, 0x0 },
289379bc100SJani Nikula 	{ 0x00007011, 0x0000009D, 0x0 },
290379bc100SJani Nikula 	{ 0x00000018, 0x000000A0, 0x0 },
291379bc100SJani Nikula 	{ 0x00006012, 0x000000DF, 0x0 },
292379bc100SJani Nikula 	{ 0x00000018, 0x0000008A, 0x0 },
293379bc100SJani Nikula };
294379bc100SJani Nikula 
295379bc100SJani Nikula /* Skylake/Kabylake U, H and S */
296379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_hdmi[] = {
297379bc100SJani Nikula 	{ 0x00000018, 0x000000AC, 0x0 },
298379bc100SJani Nikula 	{ 0x00005012, 0x0000009D, 0x0 },
299379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
300379bc100SJani Nikula 	{ 0x00000018, 0x000000A1, 0x0 },
301379bc100SJani Nikula 	{ 0x00000018, 0x00000098, 0x0 },
302379bc100SJani Nikula 	{ 0x00004013, 0x00000088, 0x0 },
303379bc100SJani Nikula 	{ 0x80006012, 0x000000CD, 0x1 },
304379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
305379bc100SJani Nikula 	{ 0x80003015, 0x000000CD, 0x1 },	/* Default */
306379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x1 },
307379bc100SJani Nikula 	{ 0x80000018, 0x000000C0, 0x1 },
308379bc100SJani Nikula };
309379bc100SJani Nikula 
310379bc100SJani Nikula /* Skylake/Kabylake Y */
311379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_hdmi[] = {
312379bc100SJani Nikula 	{ 0x00000018, 0x000000A1, 0x0 },
313379bc100SJani Nikula 	{ 0x00005012, 0x000000DF, 0x0 },
314379bc100SJani Nikula 	{ 0x80007011, 0x000000CB, 0x3 },
315379bc100SJani Nikula 	{ 0x00000018, 0x000000A4, 0x0 },
316379bc100SJani Nikula 	{ 0x00000018, 0x0000009D, 0x0 },
317379bc100SJani Nikula 	{ 0x00004013, 0x00000080, 0x0 },
318379bc100SJani Nikula 	{ 0x80006013, 0x000000C0, 0x3 },
319379bc100SJani Nikula 	{ 0x00000018, 0x0000008A, 0x0 },
320379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x3 },	/* Default */
321379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x3 },
322379bc100SJani Nikula 	{ 0x80000018, 0x000000C0, 0x3 },
323379bc100SJani Nikula };
324379bc100SJani Nikula 
325379bc100SJani Nikula struct bxt_ddi_buf_trans {
326379bc100SJani Nikula 	u8 margin;	/* swing value */
327379bc100SJani Nikula 	u8 scale;	/* scale value */
328379bc100SJani Nikula 	u8 enable;	/* scale enable */
329379bc100SJani Nikula 	u8 deemphasis;
330379bc100SJani Nikula };
331379bc100SJani Nikula 
332379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_dp[] = {
333379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
334379bc100SJani Nikula 	{ 52,  0x9A, 0, 128, },	/* 0:	400		0   */
335379bc100SJani Nikula 	{ 78,  0x9A, 0, 85,  },	/* 1:	400		3.5 */
336379bc100SJani Nikula 	{ 104, 0x9A, 0, 64,  },	/* 2:	400		6   */
337379bc100SJani Nikula 	{ 154, 0x9A, 0, 43,  },	/* 3:	400		9.5 */
338379bc100SJani Nikula 	{ 77,  0x9A, 0, 128, },	/* 4:	600		0   */
339379bc100SJani Nikula 	{ 116, 0x9A, 0, 85,  },	/* 5:	600		3.5 */
340379bc100SJani Nikula 	{ 154, 0x9A, 0, 64,  },	/* 6:	600		6   */
341379bc100SJani Nikula 	{ 102, 0x9A, 0, 128, },	/* 7:	800		0   */
342379bc100SJani Nikula 	{ 154, 0x9A, 0, 85,  },	/* 8:	800		3.5 */
343379bc100SJani Nikula 	{ 154, 0x9A, 1, 128, },	/* 9:	1200		0   */
344379bc100SJani Nikula };
345379bc100SJani Nikula 
346379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_edp[] = {
347379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
348379bc100SJani Nikula 	{ 26, 0, 0, 128, },	/* 0:	200		0   */
349379bc100SJani Nikula 	{ 38, 0, 0, 112, },	/* 1:	200		1.5 */
350379bc100SJani Nikula 	{ 48, 0, 0, 96,  },	/* 2:	200		4   */
351379bc100SJani Nikula 	{ 54, 0, 0, 69,  },	/* 3:	200		6   */
352379bc100SJani Nikula 	{ 32, 0, 0, 128, },	/* 4:	250		0   */
353379bc100SJani Nikula 	{ 48, 0, 0, 104, },	/* 5:	250		1.5 */
354379bc100SJani Nikula 	{ 54, 0, 0, 85,  },	/* 6:	250		4   */
355379bc100SJani Nikula 	{ 43, 0, 0, 128, },	/* 7:	300		0   */
356379bc100SJani Nikula 	{ 54, 0, 0, 101, },	/* 8:	300		1.5 */
357379bc100SJani Nikula 	{ 48, 0, 0, 128, },	/* 9:	300		0   */
358379bc100SJani Nikula };
359379bc100SJani Nikula 
360379bc100SJani Nikula /* BSpec has 2 recommended values - entries 0 and 8.
361379bc100SJani Nikula  * Using the entry with higher vswing.
362379bc100SJani Nikula  */
363379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_hdmi[] = {
364379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
365379bc100SJani Nikula 	{ 52,  0x9A, 0, 128, },	/* 0:	400		0   */
366379bc100SJani Nikula 	{ 52,  0x9A, 0, 85,  },	/* 1:	400		3.5 */
367379bc100SJani Nikula 	{ 52,  0x9A, 0, 64,  },	/* 2:	400		6   */
368379bc100SJani Nikula 	{ 42,  0x9A, 0, 43,  },	/* 3:	400		9.5 */
369379bc100SJani Nikula 	{ 77,  0x9A, 0, 128, },	/* 4:	600		0   */
370379bc100SJani Nikula 	{ 77,  0x9A, 0, 85,  },	/* 5:	600		3.5 */
371379bc100SJani Nikula 	{ 77,  0x9A, 0, 64,  },	/* 6:	600		6   */
372379bc100SJani Nikula 	{ 102, 0x9A, 0, 128, },	/* 7:	800		0   */
373379bc100SJani Nikula 	{ 102, 0x9A, 0, 85,  },	/* 8:	800		3.5 */
374379bc100SJani Nikula 	{ 154, 0x9A, 1, 128, },	/* 9:	1200		0   */
375379bc100SJani Nikula };
376379bc100SJani Nikula 
377379bc100SJani Nikula struct cnl_ddi_buf_trans {
378379bc100SJani Nikula 	u8 dw2_swing_sel;
379379bc100SJani Nikula 	u8 dw7_n_scalar;
380379bc100SJani Nikula 	u8 dw4_cursor_coeff;
381379bc100SJani Nikula 	u8 dw4_post_cursor_2;
382379bc100SJani Nikula 	u8 dw4_post_cursor_1;
383379bc100SJani Nikula };
384379bc100SJani Nikula 
385379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for DP */
386379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_85V[] = {
387379bc100SJani Nikula 						/* NT mV Trans mV db    */
388379bc100SJani Nikula 	{ 0xA, 0x5D, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
389379bc100SJani Nikula 	{ 0xA, 0x6A, 0x38, 0x00, 0x07 },	/* 350   500      3.1   */
390379bc100SJani Nikula 	{ 0xB, 0x7A, 0x32, 0x00, 0x0D },	/* 350   700      6.0   */
391379bc100SJani Nikula 	{ 0x6, 0x7C, 0x2D, 0x00, 0x12 },	/* 350   900      8.2   */
392379bc100SJani Nikula 	{ 0xA, 0x69, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
393379bc100SJani Nikula 	{ 0xB, 0x7A, 0x36, 0x00, 0x09 },	/* 500   700      2.9   */
394379bc100SJani Nikula 	{ 0x6, 0x7C, 0x30, 0x00, 0x0F },	/* 500   900      5.1   */
395379bc100SJani Nikula 	{ 0xB, 0x7D, 0x3C, 0x00, 0x03 },	/* 650   725      0.9   */
396379bc100SJani Nikula 	{ 0x6, 0x7C, 0x34, 0x00, 0x0B },	/* 600   900      3.5   */
397379bc100SJani Nikula 	{ 0x6, 0x7B, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
398379bc100SJani Nikula };
399379bc100SJani Nikula 
400379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for HDMI */
401379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_85V[] = {
402379bc100SJani Nikula 						/* NT mV Trans mV db    */
403379bc100SJani Nikula 	{ 0xA, 0x60, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
404379bc100SJani Nikula 	{ 0xB, 0x73, 0x36, 0x00, 0x09 },	/* 450   650      3.2   */
405379bc100SJani Nikula 	{ 0x6, 0x7F, 0x31, 0x00, 0x0E },	/* 450   850      5.5   */
406379bc100SJani Nikula 	{ 0xB, 0x73, 0x3F, 0x00, 0x00 },	/* 650   650      0.0   */
407379bc100SJani Nikula 	{ 0x6, 0x7F, 0x37, 0x00, 0x08 },	/* 650   850      2.3   */
408379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 850   850      0.0   */
409379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
410379bc100SJani Nikula };
411379bc100SJani Nikula 
412379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for eDP */
413379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_85V[] = {
414379bc100SJani Nikula 						/* NT mV Trans mV db    */
415379bc100SJani Nikula 	{ 0xA, 0x66, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
416379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
417379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
418379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
419379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
420379bc100SJani Nikula 	{ 0xA, 0x66, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
421379bc100SJani Nikula 	{ 0xB, 0x70, 0x3C, 0x00, 0x03 },	/* 460   600      2.3   */
422379bc100SJani Nikula 	{ 0xC, 0x75, 0x3C, 0x00, 0x03 },	/* 537   700      2.3   */
423379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
424379bc100SJani Nikula };
425379bc100SJani Nikula 
426379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for DP */
427379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_95V[] = {
428379bc100SJani Nikula 						/* NT mV Trans mV db    */
429379bc100SJani Nikula 	{ 0xA, 0x5D, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
430379bc100SJani Nikula 	{ 0xA, 0x6A, 0x38, 0x00, 0x07 },	/* 350   500      3.1   */
431379bc100SJani Nikula 	{ 0xB, 0x7A, 0x32, 0x00, 0x0D },	/* 350   700      6.0   */
432379bc100SJani Nikula 	{ 0x6, 0x7C, 0x2D, 0x00, 0x12 },	/* 350   900      8.2   */
433379bc100SJani Nikula 	{ 0xA, 0x69, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
434379bc100SJani Nikula 	{ 0xB, 0x7A, 0x36, 0x00, 0x09 },	/* 500   700      2.9   */
435379bc100SJani Nikula 	{ 0x6, 0x7C, 0x30, 0x00, 0x0F },	/* 500   900      5.1   */
436379bc100SJani Nikula 	{ 0xB, 0x7D, 0x3C, 0x00, 0x03 },	/* 650   725      0.9   */
437379bc100SJani Nikula 	{ 0x6, 0x7C, 0x34, 0x00, 0x0B },	/* 600   900      3.5   */
438379bc100SJani Nikula 	{ 0x6, 0x7B, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
439379bc100SJani Nikula };
440379bc100SJani Nikula 
441379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for HDMI */
442379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_95V[] = {
443379bc100SJani Nikula 						/* NT mV Trans mV db    */
444379bc100SJani Nikula 	{ 0xA, 0x5C, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
445379bc100SJani Nikula 	{ 0xB, 0x69, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
446379bc100SJani Nikula 	{ 0x5, 0x76, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
447379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
448379bc100SJani Nikula 	{ 0xB, 0x69, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
449379bc100SJani Nikula 	{ 0xB, 0x79, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
450379bc100SJani Nikula 	{ 0x6, 0x7D, 0x32, 0x00, 0x0D },	/* 600   1000     4.4   */
451379bc100SJani Nikula 	{ 0x5, 0x76, 0x3F, 0x00, 0x00 },	/* 800   800      0.0   */
452379bc100SJani Nikula 	{ 0x6, 0x7D, 0x39, 0x00, 0x06 },	/* 800   1000     1.9   */
453379bc100SJani Nikula 	{ 0x6, 0x7F, 0x39, 0x00, 0x06 },	/* 850   1050     1.8   */
454379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
455379bc100SJani Nikula };
456379bc100SJani Nikula 
457379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for eDP */
458379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_95V[] = {
459379bc100SJani Nikula 						/* NT mV Trans mV db    */
460379bc100SJani Nikula 	{ 0xA, 0x61, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
461379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
462379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
463379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
464379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
465379bc100SJani Nikula 	{ 0xA, 0x61, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
466379bc100SJani Nikula 	{ 0xB, 0x68, 0x39, 0x00, 0x06 },	/* 460   600      2.3   */
467379bc100SJani Nikula 	{ 0xC, 0x6E, 0x39, 0x00, 0x06 },	/* 537   700      2.3   */
468379bc100SJani Nikula 	{ 0x4, 0x7F, 0x3A, 0x00, 0x05 },	/* 460   600      2.3   */
469379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
470379bc100SJani Nikula };
471379bc100SJani Nikula 
472379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for DP */
473379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_1_05V[] = {
474379bc100SJani Nikula 						/* NT mV Trans mV db    */
475379bc100SJani Nikula 	{ 0xA, 0x58, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
476379bc100SJani Nikula 	{ 0xB, 0x64, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
477379bc100SJani Nikula 	{ 0x5, 0x70, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
478379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2C, 0x00, 0x13 },	/* 400   1050     8.4   */
479379bc100SJani Nikula 	{ 0xB, 0x64, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
480379bc100SJani Nikula 	{ 0x5, 0x73, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
481379bc100SJani Nikula 	{ 0x6, 0x7F, 0x30, 0x00, 0x0F },	/* 550   1050     5.6   */
482379bc100SJani Nikula 	{ 0x5, 0x76, 0x3E, 0x00, 0x01 },	/* 850   900      0.5   */
483379bc100SJani Nikula 	{ 0x6, 0x7F, 0x36, 0x00, 0x09 },	/* 750   1050     2.9   */
484379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
485379bc100SJani Nikula };
486379bc100SJani Nikula 
487379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for HDMI */
488379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_1_05V[] = {
489379bc100SJani Nikula 						/* NT mV Trans mV db    */
490379bc100SJani Nikula 	{ 0xA, 0x58, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
491379bc100SJani Nikula 	{ 0xB, 0x64, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
492379bc100SJani Nikula 	{ 0x5, 0x70, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
493379bc100SJani Nikula 	{ 0xA, 0x5B, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
494379bc100SJani Nikula 	{ 0xB, 0x64, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
495379bc100SJani Nikula 	{ 0x5, 0x73, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
496379bc100SJani Nikula 	{ 0x6, 0x7C, 0x32, 0x00, 0x0D },	/* 600   1000     4.4   */
497379bc100SJani Nikula 	{ 0x5, 0x70, 0x3F, 0x00, 0x00 },	/* 800   800      0.0   */
498379bc100SJani Nikula 	{ 0x6, 0x7C, 0x39, 0x00, 0x06 },	/* 800   1000     1.9   */
499379bc100SJani Nikula 	{ 0x6, 0x7F, 0x39, 0x00, 0x06 },	/* 850   1050     1.8   */
500379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
501379bc100SJani Nikula };
502379bc100SJani Nikula 
503379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for eDP */
504379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_1_05V[] = {
505379bc100SJani Nikula 						/* NT mV Trans mV db    */
506379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
507379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
508379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
509379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
510379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
511379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
512379bc100SJani Nikula 	{ 0xB, 0x64, 0x39, 0x00, 0x06 },	/* 460   600      2.3   */
513379bc100SJani Nikula 	{ 0xE, 0x6A, 0x39, 0x00, 0x06 },	/* 537   700      2.3   */
514379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
515379bc100SJani Nikula };
516379bc100SJani Nikula 
517379bc100SJani Nikula /* icl_combo_phy_ddi_translations */
518379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_dp_hbr2[] = {
519379bc100SJani Nikula 						/* NT mV Trans mV db    */
520379bc100SJani Nikula 	{ 0xA, 0x35, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
521379bc100SJani Nikula 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
522379bc100SJani Nikula 	{ 0xC, 0x71, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
523379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
524379bc100SJani Nikula 	{ 0xA, 0x4C, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
525379bc100SJani Nikula 	{ 0xC, 0x73, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
526379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
527379bc100SJani Nikula 	{ 0xC, 0x6C, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
528379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
529379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
530379bc100SJani Nikula };
531379bc100SJani Nikula 
532379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_edp_hbr2[] = {
533379bc100SJani Nikula 						/* NT mV Trans mV db    */
534379bc100SJani Nikula 	{ 0x0, 0x7F, 0x3F, 0x00, 0x00 },	/* 200   200      0.0   */
535379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 200   250      1.9   */
536379bc100SJani Nikula 	{ 0x1, 0x7F, 0x33, 0x00, 0x0C },	/* 200   300      3.5   */
537379bc100SJani Nikula 	{ 0x9, 0x7F, 0x31, 0x00, 0x0E },	/* 200   350      4.9   */
538379bc100SJani Nikula 	{ 0x8, 0x7F, 0x3F, 0x00, 0x00 },	/* 250   250      0.0   */
539379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 250   300      1.6   */
540379bc100SJani Nikula 	{ 0x9, 0x7F, 0x35, 0x00, 0x0A },	/* 250   350      2.9   */
541379bc100SJani Nikula 	{ 0x1, 0x7F, 0x3F, 0x00, 0x00 },	/* 300   300      0.0   */
542379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 300   350      1.3   */
543379bc100SJani Nikula 	{ 0x9, 0x7F, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
544379bc100SJani Nikula };
545379bc100SJani Nikula 
546379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_edp_hbr3[] = {
547379bc100SJani Nikula 						/* NT mV Trans mV db    */
548379bc100SJani Nikula 	{ 0xA, 0x35, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
549379bc100SJani Nikula 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
550379bc100SJani Nikula 	{ 0xC, 0x71, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
551379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
552379bc100SJani Nikula 	{ 0xA, 0x4C, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
553379bc100SJani Nikula 	{ 0xC, 0x73, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
554379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
555379bc100SJani Nikula 	{ 0xC, 0x6C, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
556379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
557379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
558379bc100SJani Nikula };
559379bc100SJani Nikula 
560379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_hdmi[] = {
561379bc100SJani Nikula 						/* NT mV Trans mV db    */
562379bc100SJani Nikula 	{ 0xA, 0x60, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
563379bc100SJani Nikula 	{ 0xB, 0x73, 0x36, 0x00, 0x09 },	/* 450   650      3.2   */
564379bc100SJani Nikula 	{ 0x6, 0x7F, 0x31, 0x00, 0x0E },	/* 450   850      5.5   */
565379bc100SJani Nikula 	{ 0xB, 0x73, 0x3F, 0x00, 0x00 },	/* 650   650      0.0   ALS */
566379bc100SJani Nikula 	{ 0x6, 0x7F, 0x37, 0x00, 0x08 },	/* 650   850      2.3   */
567379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 850   850      0.0   */
568379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
569379bc100SJani Nikula };
570379bc100SJani Nikula 
571a2ae2010SJosé Roberto de Souza static const struct cnl_ddi_buf_trans ehl_combo_phy_ddi_translations_dp[] = {
572b42d5a67SJosé Roberto de Souza 						/* NT mV Trans mV db    */
573b42d5a67SJosé Roberto de Souza 	{ 0xA, 0x33, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
574b42d5a67SJosé Roberto de Souza 	{ 0xA, 0x47, 0x36, 0x00, 0x09 },	/* 350   500      3.1   */
575b42d5a67SJosé Roberto de Souza 	{ 0xC, 0x64, 0x30, 0x00, 0x0F },	/* 350   700      6.0   */
576b42d5a67SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x2C, 0x00, 0x13 },	/* 350   900      8.2   */
577b42d5a67SJosé Roberto de Souza 	{ 0xA, 0x46, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
578b42d5a67SJosé Roberto de Souza 	{ 0xC, 0x64, 0x36, 0x00, 0x09 },	/* 500   700      2.9   */
579b42d5a67SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x30, 0x00, 0x0F },	/* 500   900      5.1   */
580b42d5a67SJosé Roberto de Souza 	{ 0xC, 0x61, 0x3F, 0x00, 0x00 },	/* 650   700      0.6   */
581b42d5a67SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x37, 0x00, 0x08 },	/* 600   900      3.5   */
582b42d5a67SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
583b42d5a67SJosé Roberto de Souza };
584b42d5a67SJosé Roberto de Souza 
585379bc100SJani Nikula struct icl_mg_phy_ddi_buf_trans {
586379bc100SJani Nikula 	u32 cri_txdeemph_override_11_6;
5879f7ffa29SJosé Roberto de Souza 	u32 cri_txdeemph_override_5_0;
588379bc100SJani Nikula 	u32 cri_txdeemph_override_17_12;
589379bc100SJani Nikula };
590379bc100SJani Nikula 
5919f7ffa29SJosé Roberto de Souza static const struct icl_mg_phy_ddi_buf_trans icl_mg_phy_ddi_translations_rbr_hbr[] = {
592379bc100SJani Nikula 				/* Voltage swing  pre-emphasis */
5939f7ffa29SJosé Roberto de Souza 	{ 0x18, 0x00, 0x00 },	/* 0              0   */
5949f7ffa29SJosé Roberto de Souza 	{ 0x1D, 0x00, 0x05 },	/* 0              1   */
5959f7ffa29SJosé Roberto de Souza 	{ 0x24, 0x00, 0x0C },	/* 0              2   */
5969f7ffa29SJosé Roberto de Souza 	{ 0x2B, 0x00, 0x14 },	/* 0              3   */
5979f7ffa29SJosé Roberto de Souza 	{ 0x21, 0x00, 0x00 },	/* 1              0   */
5989f7ffa29SJosé Roberto de Souza 	{ 0x2B, 0x00, 0x08 },	/* 1              1   */
5999f7ffa29SJosé Roberto de Souza 	{ 0x30, 0x00, 0x0F },	/* 1              2   */
6009f7ffa29SJosé Roberto de Souza 	{ 0x31, 0x00, 0x03 },	/* 2              0   */
6019f7ffa29SJosé Roberto de Souza 	{ 0x34, 0x00, 0x0B },	/* 2              1   */
6029f7ffa29SJosé Roberto de Souza 	{ 0x3F, 0x00, 0x00 },	/* 3              0   */
6039f7ffa29SJosé Roberto de Souza };
6049f7ffa29SJosé Roberto de Souza 
6059f7ffa29SJosé Roberto de Souza static const struct icl_mg_phy_ddi_buf_trans icl_mg_phy_ddi_translations_hbr2_hbr3[] = {
6069f7ffa29SJosé Roberto de Souza 				/* Voltage swing  pre-emphasis */
6079f7ffa29SJosé Roberto de Souza 	{ 0x18, 0x00, 0x00 },	/* 0              0   */
6089f7ffa29SJosé Roberto de Souza 	{ 0x1D, 0x00, 0x05 },	/* 0              1   */
6099f7ffa29SJosé Roberto de Souza 	{ 0x24, 0x00, 0x0C },	/* 0              2   */
6109f7ffa29SJosé Roberto de Souza 	{ 0x2B, 0x00, 0x14 },	/* 0              3   */
6119f7ffa29SJosé Roberto de Souza 	{ 0x26, 0x00, 0x00 },	/* 1              0   */
6129f7ffa29SJosé Roberto de Souza 	{ 0x2C, 0x00, 0x07 },	/* 1              1   */
6139f7ffa29SJosé Roberto de Souza 	{ 0x33, 0x00, 0x0C },	/* 1              2   */
6149f7ffa29SJosé Roberto de Souza 	{ 0x2E, 0x00, 0x00 },	/* 2              0   */
6159f7ffa29SJosé Roberto de Souza 	{ 0x36, 0x00, 0x09 },	/* 2              1   */
6169f7ffa29SJosé Roberto de Souza 	{ 0x3F, 0x00, 0x00 },	/* 3              0   */
6179f7ffa29SJosé Roberto de Souza };
6189f7ffa29SJosé Roberto de Souza 
6199f7ffa29SJosé Roberto de Souza static const struct icl_mg_phy_ddi_buf_trans icl_mg_phy_ddi_translations_hdmi[] = {
6209f7ffa29SJosé Roberto de Souza 				/* HDMI Preset	VS	Pre-emph */
6219f7ffa29SJosé Roberto de Souza 	{ 0x1A, 0x0, 0x0 },	/* 1		400mV	0dB */
6229f7ffa29SJosé Roberto de Souza 	{ 0x20, 0x0, 0x0 },	/* 2		500mV	0dB */
6239f7ffa29SJosé Roberto de Souza 	{ 0x29, 0x0, 0x0 },	/* 3		650mV	0dB */
6249f7ffa29SJosé Roberto de Souza 	{ 0x32, 0x0, 0x0 },	/* 4		800mV	0dB */
6259f7ffa29SJosé Roberto de Souza 	{ 0x3F, 0x0, 0x0 },	/* 5		1000mV	0dB */
6269f7ffa29SJosé Roberto de Souza 	{ 0x3A, 0x0, 0x5 },	/* 6		Full	-1.5 dB */
6279f7ffa29SJosé Roberto de Souza 	{ 0x39, 0x0, 0x6 },	/* 7		Full	-1.8 dB */
6289f7ffa29SJosé Roberto de Souza 	{ 0x38, 0x0, 0x7 },	/* 8		Full	-2 dB */
6299f7ffa29SJosé Roberto de Souza 	{ 0x37, 0x0, 0x8 },	/* 9		Full	-2.5 dB */
6309f7ffa29SJosé Roberto de Souza 	{ 0x36, 0x0, 0x9 },	/* 10		Full	-3 dB */
631379bc100SJani Nikula };
632379bc100SJani Nikula 
633978c3e53SClinton A Taylor struct tgl_dkl_phy_ddi_buf_trans {
634978c3e53SClinton A Taylor 	u32 dkl_vswing_control;
635978c3e53SClinton A Taylor 	u32 dkl_preshoot_control;
636978c3e53SClinton A Taylor 	u32 dkl_de_emphasis_control;
637978c3e53SClinton A Taylor };
638978c3e53SClinton A Taylor 
639362bfb99SMatt Roper static const struct tgl_dkl_phy_ddi_buf_trans tgl_dkl_phy_dp_ddi_trans[] = {
640978c3e53SClinton A Taylor 				/* VS	pre-emp	Non-trans mV	Pre-emph dB */
641978c3e53SClinton A Taylor 	{ 0x7, 0x0, 0x00 },	/* 0	0	400mV		0 dB */
642250a353cSJosé Roberto de Souza 	{ 0x5, 0x0, 0x05 },	/* 0	1	400mV		3.5 dB */
643250a353cSJosé Roberto de Souza 	{ 0x2, 0x0, 0x0B },	/* 0	2	400mV		6 dB */
6449fa67699SJosé Roberto de Souza 	{ 0x0, 0x0, 0x18 },	/* 0	3	400mV		9.5 dB */
6459fa67699SJosé Roberto de Souza 	{ 0x5, 0x0, 0x00 },	/* 1	0	600mV		0 dB */
6469fa67699SJosé Roberto de Souza 	{ 0x2, 0x0, 0x08 },	/* 1	1	600mV		3.5 dB */
6479fa67699SJosé Roberto de Souza 	{ 0x0, 0x0, 0x14 },	/* 1	2	600mV		6 dB */
6489fa67699SJosé Roberto de Souza 	{ 0x2, 0x0, 0x00 },	/* 2	0	800mV		0 dB */
6499fa67699SJosé Roberto de Souza 	{ 0x0, 0x0, 0x0B },	/* 2	1	800mV		3.5 dB */
6509fa67699SJosé Roberto de Souza 	{ 0x0, 0x0, 0x00 },	/* 3	0	1200mV		0 dB HDMI default */
6519fa67699SJosé Roberto de Souza };
6529fa67699SJosé Roberto de Souza 
6539fa67699SJosé Roberto de Souza static const struct tgl_dkl_phy_ddi_buf_trans tgl_dkl_phy_dp_ddi_trans_hbr2[] = {
6549fa67699SJosé Roberto de Souza 				/* VS	pre-emp	Non-trans mV	Pre-emph dB */
6559fa67699SJosé Roberto de Souza 	{ 0x7, 0x0, 0x00 },	/* 0	0	400mV		0 dB */
6569fa67699SJosé Roberto de Souza 	{ 0x5, 0x0, 0x05 },	/* 0	1	400mV		3.5 dB */
6579fa67699SJosé Roberto de Souza 	{ 0x2, 0x0, 0x0B },	/* 0	2	400mV		6 dB */
658978c3e53SClinton A Taylor 	{ 0x0, 0x0, 0x19 },	/* 0	3	400mV		9.5 dB */
659978c3e53SClinton A Taylor 	{ 0x5, 0x0, 0x00 },	/* 1	0	600mV		0 dB */
660250a353cSJosé Roberto de Souza 	{ 0x2, 0x0, 0x08 },	/* 1	1	600mV		3.5 dB */
661978c3e53SClinton A Taylor 	{ 0x0, 0x0, 0x14 },	/* 1	2	600mV		6 dB */
662978c3e53SClinton A Taylor 	{ 0x2, 0x0, 0x00 },	/* 2	0	800mV		0 dB */
663978c3e53SClinton A Taylor 	{ 0x0, 0x0, 0x0B },	/* 2	1	800mV		3.5 dB */
664978c3e53SClinton A Taylor 	{ 0x0, 0x0, 0x00 },	/* 3	0	1200mV		0 dB HDMI default */
665978c3e53SClinton A Taylor };
666978c3e53SClinton A Taylor 
667362bfb99SMatt Roper static const struct tgl_dkl_phy_ddi_buf_trans tgl_dkl_phy_hdmi_ddi_trans[] = {
668362bfb99SMatt Roper 				/* HDMI Preset	VS	Pre-emph */
669362bfb99SMatt Roper 	{ 0x7, 0x0, 0x0 },	/* 1		400mV	0dB */
670362bfb99SMatt Roper 	{ 0x6, 0x0, 0x0 },	/* 2		500mV	0dB */
671362bfb99SMatt Roper 	{ 0x4, 0x0, 0x0 },	/* 3		650mV	0dB */
672362bfb99SMatt Roper 	{ 0x2, 0x0, 0x0 },	/* 4		800mV	0dB */
673362bfb99SMatt Roper 	{ 0x0, 0x0, 0x0 },	/* 5		1000mV	0dB */
674362bfb99SMatt Roper 	{ 0x0, 0x0, 0x5 },	/* 6		Full	-1.5 dB */
675362bfb99SMatt Roper 	{ 0x0, 0x0, 0x6 },	/* 7		Full	-1.8 dB */
676362bfb99SMatt Roper 	{ 0x0, 0x0, 0x7 },	/* 8		Full	-2 dB */
677362bfb99SMatt Roper 	{ 0x0, 0x0, 0x8 },	/* 9		Full	-2.5 dB */
678362bfb99SMatt Roper 	{ 0x0, 0x0, 0xA },	/* 10		Full	-3 dB */
679362bfb99SMatt Roper };
680362bfb99SMatt Roper 
681bd3cf6f7SJosé Roberto de Souza static const struct cnl_ddi_buf_trans tgl_combo_phy_ddi_translations_dp_hbr[] = {
682bd3cf6f7SJosé Roberto de Souza 						/* NT mV Trans mV db    */
683bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x32, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
684bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
685bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x71, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
686bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7D, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
687bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x4C, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
688bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x73, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
689bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
690bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x6C, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
691bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
692bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
693bd3cf6f7SJosé Roberto de Souza };
694bd3cf6f7SJosé Roberto de Souza 
695bd3cf6f7SJosé Roberto de Souza static const struct cnl_ddi_buf_trans tgl_combo_phy_ddi_translations_dp_hbr2[] = {
696bd3cf6f7SJosé Roberto de Souza 						/* NT mV Trans mV db    */
697bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x35, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
698bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
699bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x63, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
700bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
701bd3cf6f7SJosé Roberto de Souza 	{ 0xA, 0x47, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
702bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x63, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
703bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
704bd3cf6f7SJosé Roberto de Souza 	{ 0xC, 0x61, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
705bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7B, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
706bd3cf6f7SJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
707bd3cf6f7SJosé Roberto de Souza };
708bd3cf6f7SJosé Roberto de Souza 
70981619f4aSJosé Roberto de Souza /*
71081619f4aSJosé Roberto de Souza  * Cloned the HOBL entry to comply with the voltage and pre-emphasis entries
71181619f4aSJosé Roberto de Souza  * that DisplayPort specification requires
71281619f4aSJosé Roberto de Souza  */
71381619f4aSJosé Roberto de Souza static const struct cnl_ddi_buf_trans tgl_combo_phy_ddi_translations_edp_hbr2_hobl[] = {
71481619f4aSJosé Roberto de Souza 						/* VS	pre-emp	*/
71581619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 0	0	*/
71681619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 0	1	*/
71781619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 0	2	*/
71881619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 0	3	*/
71981619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1	0	*/
72081619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1	1	*/
72181619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1	2	*/
72281619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 2	0	*/
72381619f4aSJosé Roberto de Souza 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 2	1	*/
72481619f4aSJosé Roberto de Souza };
72581619f4aSJosé Roberto de Souza 
72681619f4aSJosé Roberto de Souza static bool is_hobl_buf_trans(const struct cnl_ddi_buf_trans *table)
72781619f4aSJosé Roberto de Souza {
72881619f4aSJosé Roberto de Souza 	return table == tgl_combo_phy_ddi_translations_edp_hbr2_hobl;
72981619f4aSJosé Roberto de Souza }
73081619f4aSJosé Roberto de Souza 
731379bc100SJani Nikula static const struct ddi_buf_trans *
732a8143150SJosé Roberto de Souza bdw_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
733379bc100SJani Nikula {
734a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
735a8143150SJosé Roberto de Souza 
736379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
737379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
738379bc100SJani Nikula 		return bdw_ddi_translations_edp;
739379bc100SJani Nikula 	} else {
740379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
741379bc100SJani Nikula 		return bdw_ddi_translations_dp;
742379bc100SJani Nikula 	}
743379bc100SJani Nikula }
744379bc100SJani Nikula 
745379bc100SJani Nikula static const struct ddi_buf_trans *
746a8143150SJosé Roberto de Souza skl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
747379bc100SJani Nikula {
748a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
749a8143150SJosé Roberto de Souza 
750379bc100SJani Nikula 	if (IS_SKL_ULX(dev_priv)) {
751379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_dp);
752379bc100SJani Nikula 		return skl_y_ddi_translations_dp;
753379bc100SJani Nikula 	} else if (IS_SKL_ULT(dev_priv)) {
754379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_u_ddi_translations_dp);
755379bc100SJani Nikula 		return skl_u_ddi_translations_dp;
756379bc100SJani Nikula 	} else {
757379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_dp);
758379bc100SJani Nikula 		return skl_ddi_translations_dp;
759379bc100SJani Nikula 	}
760379bc100SJani Nikula }
761379bc100SJani Nikula 
762379bc100SJani Nikula static const struct ddi_buf_trans *
763a8143150SJosé Roberto de Souza kbl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
764379bc100SJani Nikula {
765a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
766a8143150SJosé Roberto de Souza 
7675f4ae270SChris Wilson 	if (IS_KBL_ULX(dev_priv) ||
7685f4ae270SChris Wilson 	    IS_CFL_ULX(dev_priv) ||
7695f4ae270SChris Wilson 	    IS_CML_ULX(dev_priv)) {
770379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_y_ddi_translations_dp);
771379bc100SJani Nikula 		return kbl_y_ddi_translations_dp;
7725f4ae270SChris Wilson 	} else if (IS_KBL_ULT(dev_priv) ||
7735f4ae270SChris Wilson 		   IS_CFL_ULT(dev_priv) ||
7745f4ae270SChris Wilson 		   IS_CML_ULT(dev_priv)) {
775379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_u_ddi_translations_dp);
776379bc100SJani Nikula 		return kbl_u_ddi_translations_dp;
777379bc100SJani Nikula 	} else {
778379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_ddi_translations_dp);
779379bc100SJani Nikula 		return kbl_ddi_translations_dp;
780379bc100SJani Nikula 	}
781379bc100SJani Nikula }
782379bc100SJani Nikula 
783379bc100SJani Nikula static const struct ddi_buf_trans *
784a8143150SJosé Roberto de Souza skl_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
785379bc100SJani Nikula {
786a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
787a8143150SJosé Roberto de Souza 
788379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
7895f4ae270SChris Wilson 		if (IS_SKL_ULX(dev_priv) ||
7905f4ae270SChris Wilson 		    IS_KBL_ULX(dev_priv) ||
7915f4ae270SChris Wilson 		    IS_CFL_ULX(dev_priv) ||
7925f4ae270SChris Wilson 		    IS_CML_ULX(dev_priv)) {
793379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_y_ddi_translations_edp);
794379bc100SJani Nikula 			return skl_y_ddi_translations_edp;
7955f4ae270SChris Wilson 		} else if (IS_SKL_ULT(dev_priv) ||
7965f4ae270SChris Wilson 			   IS_KBL_ULT(dev_priv) ||
7975f4ae270SChris Wilson 			   IS_CFL_ULT(dev_priv) ||
7985f4ae270SChris Wilson 			   IS_CML_ULT(dev_priv)) {
799379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_u_ddi_translations_edp);
800379bc100SJani Nikula 			return skl_u_ddi_translations_edp;
801379bc100SJani Nikula 		} else {
802379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_ddi_translations_edp);
803379bc100SJani Nikula 			return skl_ddi_translations_edp;
804379bc100SJani Nikula 		}
805379bc100SJani Nikula 	}
806379bc100SJani Nikula 
8075f4ae270SChris Wilson 	if (IS_KABYLAKE(dev_priv) ||
8085f4ae270SChris Wilson 	    IS_COFFEELAKE(dev_priv) ||
8095f4ae270SChris Wilson 	    IS_COMETLAKE(dev_priv))
810a8143150SJosé Roberto de Souza 		return kbl_get_buf_trans_dp(encoder, n_entries);
811379bc100SJani Nikula 	else
812a8143150SJosé Roberto de Souza 		return skl_get_buf_trans_dp(encoder, n_entries);
813379bc100SJani Nikula }
814379bc100SJani Nikula 
815379bc100SJani Nikula static const struct ddi_buf_trans *
816379bc100SJani Nikula skl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
817379bc100SJani Nikula {
8185f4ae270SChris Wilson 	if (IS_SKL_ULX(dev_priv) ||
8195f4ae270SChris Wilson 	    IS_KBL_ULX(dev_priv) ||
8205f4ae270SChris Wilson 	    IS_CFL_ULX(dev_priv) ||
8215f4ae270SChris Wilson 	    IS_CML_ULX(dev_priv)) {
822379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_hdmi);
823379bc100SJani Nikula 		return skl_y_ddi_translations_hdmi;
824379bc100SJani Nikula 	} else {
825379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
826379bc100SJani Nikula 		return skl_ddi_translations_hdmi;
827379bc100SJani Nikula 	}
828379bc100SJani Nikula }
829379bc100SJani Nikula 
830379bc100SJani Nikula static int skl_buf_trans_num_entries(enum port port, int n_entries)
831379bc100SJani Nikula {
832379bc100SJani Nikula 	/* Only DDIA and DDIE can select the 10th register with DP */
833379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E)
834379bc100SJani Nikula 		return min(n_entries, 10);
835379bc100SJani Nikula 	else
836379bc100SJani Nikula 		return min(n_entries, 9);
837379bc100SJani Nikula }
838379bc100SJani Nikula 
839379bc100SJani Nikula static const struct ddi_buf_trans *
840f0e86e05SJosé Roberto de Souza intel_ddi_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
841379bc100SJani Nikula {
842a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
843a8143150SJosé Roberto de Souza 
8445f4ae270SChris Wilson 	if (IS_KABYLAKE(dev_priv) ||
8455f4ae270SChris Wilson 	    IS_COFFEELAKE(dev_priv) ||
8465f4ae270SChris Wilson 	    IS_COMETLAKE(dev_priv)) {
847379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
848a8143150SJosé Roberto de Souza 			kbl_get_buf_trans_dp(encoder, n_entries);
849f0e86e05SJosé Roberto de Souza 		*n_entries = skl_buf_trans_num_entries(encoder->port, *n_entries);
850379bc100SJani Nikula 		return ddi_translations;
851379bc100SJani Nikula 	} else if (IS_SKYLAKE(dev_priv)) {
852379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
853a8143150SJosé Roberto de Souza 			skl_get_buf_trans_dp(encoder, n_entries);
854f0e86e05SJosé Roberto de Souza 		*n_entries = skl_buf_trans_num_entries(encoder->port, *n_entries);
855379bc100SJani Nikula 		return ddi_translations;
856379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
857379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
858379bc100SJani Nikula 		return  bdw_ddi_translations_dp;
859379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
860379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
861379bc100SJani Nikula 		return hsw_ddi_translations_dp;
862379bc100SJani Nikula 	}
863379bc100SJani Nikula 
864379bc100SJani Nikula 	*n_entries = 0;
865379bc100SJani Nikula 	return NULL;
866379bc100SJani Nikula }
867379bc100SJani Nikula 
868379bc100SJani Nikula static const struct ddi_buf_trans *
869f0e86e05SJosé Roberto de Souza intel_ddi_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
870379bc100SJani Nikula {
871a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
872a8143150SJosé Roberto de Souza 
873379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
874379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
875a8143150SJosé Roberto de Souza 			skl_get_buf_trans_edp(encoder, n_entries);
876f0e86e05SJosé Roberto de Souza 		*n_entries = skl_buf_trans_num_entries(encoder->port, *n_entries);
877379bc100SJani Nikula 		return ddi_translations;
878379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
879a8143150SJosé Roberto de Souza 		return bdw_get_buf_trans_edp(encoder, n_entries);
880379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
881379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
882379bc100SJani Nikula 		return hsw_ddi_translations_dp;
883379bc100SJani Nikula 	}
884379bc100SJani Nikula 
885379bc100SJani Nikula 	*n_entries = 0;
886379bc100SJani Nikula 	return NULL;
887379bc100SJani Nikula }
888379bc100SJani Nikula 
889379bc100SJani Nikula static const struct ddi_buf_trans *
890379bc100SJani Nikula intel_ddi_get_buf_trans_fdi(struct drm_i915_private *dev_priv,
891379bc100SJani Nikula 			    int *n_entries)
892379bc100SJani Nikula {
893379bc100SJani Nikula 	if (IS_BROADWELL(dev_priv)) {
894379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_fdi);
895379bc100SJani Nikula 		return bdw_ddi_translations_fdi;
896379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
897379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
898379bc100SJani Nikula 		return hsw_ddi_translations_fdi;
899379bc100SJani Nikula 	}
900379bc100SJani Nikula 
901379bc100SJani Nikula 	*n_entries = 0;
902379bc100SJani Nikula 	return NULL;
903379bc100SJani Nikula }
904379bc100SJani Nikula 
905379bc100SJani Nikula static const struct ddi_buf_trans *
906a8143150SJosé Roberto de Souza intel_ddi_get_buf_trans_hdmi(struct intel_encoder *encoder,
907379bc100SJani Nikula 			     int *n_entries)
908379bc100SJani Nikula {
909a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
910a8143150SJosé Roberto de Souza 
911379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
912379bc100SJani Nikula 		return skl_get_buf_trans_hdmi(dev_priv, n_entries);
913379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
914379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
915379bc100SJani Nikula 		return bdw_ddi_translations_hdmi;
916379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
917379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
918379bc100SJani Nikula 		return hsw_ddi_translations_hdmi;
919379bc100SJani Nikula 	}
920379bc100SJani Nikula 
921379bc100SJani Nikula 	*n_entries = 0;
922379bc100SJani Nikula 	return NULL;
923379bc100SJani Nikula }
924379bc100SJani Nikula 
925379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
926a8143150SJosé Roberto de Souza bxt_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
927379bc100SJani Nikula {
928379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_dp);
929379bc100SJani Nikula 	return bxt_ddi_translations_dp;
930379bc100SJani Nikula }
931379bc100SJani Nikula 
932379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
933a8143150SJosé Roberto de Souza bxt_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
934379bc100SJani Nikula {
935a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
936a8143150SJosé Roberto de Souza 
937379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
938379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bxt_ddi_translations_edp);
939379bc100SJani Nikula 		return bxt_ddi_translations_edp;
940379bc100SJani Nikula 	}
941379bc100SJani Nikula 
942a8143150SJosé Roberto de Souza 	return bxt_get_buf_trans_dp(encoder, n_entries);
943379bc100SJani Nikula }
944379bc100SJani Nikula 
945379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
946a8143150SJosé Roberto de Souza bxt_get_buf_trans_hdmi(struct intel_encoder *encoder, int *n_entries)
947379bc100SJani Nikula {
948379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_hdmi);
949379bc100SJani Nikula 	return bxt_ddi_translations_hdmi;
950379bc100SJani Nikula }
951379bc100SJani Nikula 
952379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
953a8143150SJosé Roberto de Souza cnl_get_buf_trans_hdmi(struct intel_encoder *encoder, int *n_entries)
954379bc100SJani Nikula {
955a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
956f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
957379bc100SJani Nikula 
958379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
959379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_85V);
960379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_85V;
961379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
962379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_95V);
963379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_95V;
964379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
965379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_1_05V);
966379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_1_05V;
967379bc100SJani Nikula 	} else {
968379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
969379bc100SJani Nikula 		MISSING_CASE(voltage);
970379bc100SJani Nikula 	}
971379bc100SJani Nikula 	return NULL;
972379bc100SJani Nikula }
973379bc100SJani Nikula 
974379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
975a8143150SJosé Roberto de Souza cnl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
976379bc100SJani Nikula {
977a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
978f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
979379bc100SJani Nikula 
980379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
981379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_85V);
982379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_85V;
983379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
984379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_95V);
985379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_95V;
986379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
987379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_1_05V);
988379bc100SJani Nikula 		return cnl_ddi_translations_dp_1_05V;
989379bc100SJani Nikula 	} else {
990379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
991379bc100SJani Nikula 		MISSING_CASE(voltage);
992379bc100SJani Nikula 	}
993379bc100SJani Nikula 	return NULL;
994379bc100SJani Nikula }
995379bc100SJani Nikula 
996379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
997a8143150SJosé Roberto de Souza cnl_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
998379bc100SJani Nikula {
999a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1000f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
1001379bc100SJani Nikula 
1002379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
1003379bc100SJani Nikula 		if (voltage == VOLTAGE_INFO_0_85V) {
1004379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_85V);
1005379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_85V;
1006379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_0_95V) {
1007379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_95V);
1008379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_95V;
1009379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_1_05V) {
1010379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_1_05V);
1011379bc100SJani Nikula 			return cnl_ddi_translations_edp_1_05V;
1012379bc100SJani Nikula 		} else {
1013379bc100SJani Nikula 			*n_entries = 1; /* shut up gcc */
1014379bc100SJani Nikula 			MISSING_CASE(voltage);
1015379bc100SJani Nikula 		}
1016379bc100SJani Nikula 		return NULL;
1017379bc100SJani Nikula 	} else {
1018a8143150SJosé Roberto de Souza 		return cnl_get_buf_trans_dp(encoder, n_entries);
1019379bc100SJani Nikula 	}
1020379bc100SJani Nikula }
1021379bc100SJani Nikula 
1022379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
1023a8143150SJosé Roberto de Souza icl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
10244a8134d5SMatt Roper 			int *n_entries)
1025379bc100SJani Nikula {
1026a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1027a8143150SJosé Roberto de Souza 
1028379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
1029379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_hdmi);
1030379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_hdmi;
1031379bc100SJani Nikula 	} else if (rate > 540000 && type == INTEL_OUTPUT_EDP) {
1032379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr3);
1033379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr3;
1034379bc100SJani Nikula 	} else if (type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.low_vswing) {
1035379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr2);
1036379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr2;
1037379bc100SJani Nikula 	}
1038379bc100SJani Nikula 
1039379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_dp_hbr2);
1040379bc100SJani Nikula 	return icl_combo_phy_ddi_translations_dp_hbr2;
1041379bc100SJani Nikula }
1042379bc100SJani Nikula 
10439f7ffa29SJosé Roberto de Souza static const struct icl_mg_phy_ddi_buf_trans *
1044a8143150SJosé Roberto de Souza icl_get_mg_buf_trans(struct intel_encoder *encoder, int type, int rate,
10459f7ffa29SJosé Roberto de Souza 		     int *n_entries)
10469f7ffa29SJosé Roberto de Souza {
10479f7ffa29SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI) {
10489f7ffa29SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_hdmi);
10499f7ffa29SJosé Roberto de Souza 		return icl_mg_phy_ddi_translations_hdmi;
10509f7ffa29SJosé Roberto de Souza 	} else if (rate > 270000) {
10519f7ffa29SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_hbr2_hbr3);
10529f7ffa29SJosé Roberto de Souza 		return icl_mg_phy_ddi_translations_hbr2_hbr3;
10539f7ffa29SJosé Roberto de Souza 	}
10549f7ffa29SJosé Roberto de Souza 
10559f7ffa29SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_rbr_hbr);
10569f7ffa29SJosé Roberto de Souza 	return icl_mg_phy_ddi_translations_rbr_hbr;
10579f7ffa29SJosé Roberto de Souza }
10589f7ffa29SJosé Roberto de Souza 
1059bd3cf6f7SJosé Roberto de Souza static const struct cnl_ddi_buf_trans *
1060a8143150SJosé Roberto de Souza ehl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
1061b42d5a67SJosé Roberto de Souza 			int *n_entries)
1062b42d5a67SJosé Roberto de Souza {
1063a2ae2010SJosé Roberto de Souza 	if (type != INTEL_OUTPUT_HDMI && type != INTEL_OUTPUT_EDP) {
1064a2ae2010SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(ehl_combo_phy_ddi_translations_dp);
1065a2ae2010SJosé Roberto de Souza 		return ehl_combo_phy_ddi_translations_dp;
1066b42d5a67SJosé Roberto de Souza 	}
1067b42d5a67SJosé Roberto de Souza 
1068a8143150SJosé Roberto de Souza 	return icl_get_combo_buf_trans(encoder, type, rate, n_entries);
1069b42d5a67SJosé Roberto de Souza }
1070b42d5a67SJosé Roberto de Souza 
1071b42d5a67SJosé Roberto de Souza static const struct cnl_ddi_buf_trans *
1072a8143150SJosé Roberto de Souza tgl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
1073bd3cf6f7SJosé Roberto de Souza 			int *n_entries)
1074bd3cf6f7SJosé Roberto de Souza {
107581619f4aSJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
107681619f4aSJosé Roberto de Souza 
107781619f4aSJosé Roberto de Souza 	if (type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.hobl) {
107881619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
107981619f4aSJosé Roberto de Souza 
108081619f4aSJosé Roberto de Souza 		if (!intel_dp->hobl_failed && rate <= 540000) {
108181619f4aSJosé Roberto de Souza 			/* Same table applies to TGL, RKL and DG1 */
108281619f4aSJosé Roberto de Souza 			*n_entries = ARRAY_SIZE(tgl_combo_phy_ddi_translations_edp_hbr2_hobl);
108381619f4aSJosé Roberto de Souza 			return tgl_combo_phy_ddi_translations_edp_hbr2_hobl;
108481619f4aSJosé Roberto de Souza 		}
108581619f4aSJosé Roberto de Souza 	}
108681619f4aSJosé Roberto de Souza 
108770988115SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_EDP) {
1088a8143150SJosé Roberto de Souza 		return icl_get_combo_buf_trans(encoder, type, rate, n_entries);
1089bd3cf6f7SJosé Roberto de Souza 	} else if (rate > 270000) {
1090bd3cf6f7SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_combo_phy_ddi_translations_dp_hbr2);
1091bd3cf6f7SJosé Roberto de Souza 		return tgl_combo_phy_ddi_translations_dp_hbr2;
1092bd3cf6f7SJosé Roberto de Souza 	}
1093bd3cf6f7SJosé Roberto de Souza 
1094bd3cf6f7SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(tgl_combo_phy_ddi_translations_dp_hbr);
1095bd3cf6f7SJosé Roberto de Souza 	return tgl_combo_phy_ddi_translations_dp_hbr;
1096bd3cf6f7SJosé Roberto de Souza }
1097bd3cf6f7SJosé Roberto de Souza 
10989fa67699SJosé Roberto de Souza static const struct tgl_dkl_phy_ddi_buf_trans *
1099a8143150SJosé Roberto de Souza tgl_get_dkl_buf_trans(struct intel_encoder *encoder, int type, int rate,
11009fa67699SJosé Roberto de Souza 		      int *n_entries)
11019fa67699SJosé Roberto de Souza {
11029fa67699SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI) {
11039fa67699SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_dkl_phy_hdmi_ddi_trans);
11049fa67699SJosé Roberto de Souza 		return tgl_dkl_phy_hdmi_ddi_trans;
11059fa67699SJosé Roberto de Souza 	} else if (rate > 270000) {
11069fa67699SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_dkl_phy_dp_ddi_trans_hbr2);
11079fa67699SJosé Roberto de Souza 		return tgl_dkl_phy_dp_ddi_trans_hbr2;
11089fa67699SJosé Roberto de Souza 	}
11099fa67699SJosé Roberto de Souza 
11109fa67699SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(tgl_dkl_phy_dp_ddi_trans);
11119fa67699SJosé Roberto de Souza 	return tgl_dkl_phy_dp_ddi_trans;
11129fa67699SJosé Roberto de Souza }
11139fa67699SJosé Roberto de Souza 
11140aed3bdeSJani Nikula static int intel_ddi_hdmi_level(struct intel_encoder *encoder)
1115379bc100SJani Nikula {
11160aed3bdeSJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1117379bc100SJani Nikula 	int n_entries, level, default_entry;
11180aed3bdeSJani Nikula 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1119379bc100SJani Nikula 
1120978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
1121978c3e53SClinton A Taylor 		if (intel_phy_is_combo(dev_priv, phy))
1122a8143150SJosé Roberto de Souza 			tgl_get_combo_buf_trans(encoder, INTEL_OUTPUT_HDMI,
1123978c3e53SClinton A Taylor 						0, &n_entries);
1124978c3e53SClinton A Taylor 		else
1125a8143150SJosé Roberto de Souza 			tgl_get_dkl_buf_trans(encoder, INTEL_OUTPUT_HDMI, 0,
11269fa67699SJosé Roberto de Souza 					      &n_entries);
1127978c3e53SClinton A Taylor 		default_entry = n_entries - 1;
1128978c3e53SClinton A Taylor 	} else if (INTEL_GEN(dev_priv) == 11) {
1129d8fe2ab6SMatt Roper 		if (intel_phy_is_combo(dev_priv, phy))
1130a8143150SJosé Roberto de Souza 			icl_get_combo_buf_trans(encoder, INTEL_OUTPUT_HDMI,
1131379bc100SJani Nikula 						0, &n_entries);
1132379bc100SJani Nikula 		else
1133a8143150SJosé Roberto de Souza 			icl_get_mg_buf_trans(encoder, INTEL_OUTPUT_HDMI, 0,
11349f7ffa29SJosé Roberto de Souza 					     &n_entries);
1135379bc100SJani Nikula 		default_entry = n_entries - 1;
1136379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
1137a8143150SJosé Roberto de Souza 		cnl_get_buf_trans_hdmi(encoder, &n_entries);
1138379bc100SJani Nikula 		default_entry = n_entries - 1;
1139379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
1140a8143150SJosé Roberto de Souza 		bxt_get_buf_trans_hdmi(encoder, &n_entries);
1141379bc100SJani Nikula 		default_entry = n_entries - 1;
1142379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
1143a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1144379bc100SJani Nikula 		default_entry = 8;
1145379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
1146a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1147379bc100SJani Nikula 		default_entry = 7;
1148379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
1149a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1150379bc100SJani Nikula 		default_entry = 6;
1151379bc100SJani Nikula 	} else {
11521de143ccSPankaj Bharadiya 		drm_WARN(&dev_priv->drm, 1, "ddi translation table missing\n");
1153379bc100SJani Nikula 		return 0;
1154379bc100SJani Nikula 	}
1155379bc100SJani Nikula 
11561de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, n_entries == 0))
1157379bc100SJani Nikula 		return 0;
11587a0073d6SJani Nikula 
11590aed3bdeSJani Nikula 	level = intel_bios_hdmi_level_shift(encoder);
11600aed3bdeSJani Nikula 	if (level < 0)
11617a0073d6SJani Nikula 		level = default_entry;
11627a0073d6SJani Nikula 
11631de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1164379bc100SJani Nikula 		level = n_entries - 1;
1165379bc100SJani Nikula 
1166379bc100SJani Nikula 	return level;
1167379bc100SJani Nikula }
1168379bc100SJani Nikula 
1169379bc100SJani Nikula /*
1170379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
1171379bc100SJani Nikula  * values in advance. This function programs the correct values for
1172379bc100SJani Nikula  * DP/eDP/FDI use cases.
1173379bc100SJani Nikula  */
1174379bc100SJani Nikula static void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
1175379bc100SJani Nikula 					 const struct intel_crtc_state *crtc_state)
1176379bc100SJani Nikula {
1177379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1178379bc100SJani Nikula 	u32 iboost_bit = 0;
1179379bc100SJani Nikula 	int i, n_entries;
1180379bc100SJani Nikula 	enum port port = encoder->port;
1181379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
1182379bc100SJani Nikula 
1183379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1184379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
1185379bc100SJani Nikula 							       &n_entries);
1186379bc100SJani Nikula 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
1187f0e86e05SJosé Roberto de Souza 		ddi_translations = intel_ddi_get_buf_trans_edp(encoder,
1188379bc100SJani Nikula 							       &n_entries);
1189379bc100SJani Nikula 	else
1190f0e86e05SJosé Roberto de Souza 		ddi_translations = intel_ddi_get_buf_trans_dp(encoder,
1191379bc100SJani Nikula 							      &n_entries);
1192379bc100SJani Nikula 
1193379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
1194605a1872SJani Nikula 	if (IS_GEN9_BC(dev_priv) && intel_bios_dp_boost_level(encoder))
1195379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
1196379bc100SJani Nikula 
1197379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
1198f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
1199379bc100SJani Nikula 			       ddi_translations[i].trans1 | iboost_bit);
1200f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
1201379bc100SJani Nikula 			       ddi_translations[i].trans2);
1202379bc100SJani Nikula 	}
1203379bc100SJani Nikula }
1204379bc100SJani Nikula 
1205379bc100SJani Nikula /*
1206379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
1207379bc100SJani Nikula  * values in advance. This function programs the correct values for
1208379bc100SJani Nikula  * HDMI/DVI use cases.
1209379bc100SJani Nikula  */
1210379bc100SJani Nikula static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
1211379bc100SJani Nikula 					   int level)
1212379bc100SJani Nikula {
1213379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1214379bc100SJani Nikula 	u32 iboost_bit = 0;
1215379bc100SJani Nikula 	int n_entries;
1216379bc100SJani Nikula 	enum port port = encoder->port;
1217379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
1218379bc100SJani Nikula 
1219a8143150SJosé Roberto de Souza 	ddi_translations = intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1220379bc100SJani Nikula 
12211de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
1222379bc100SJani Nikula 		return;
12231de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1224379bc100SJani Nikula 		level = n_entries - 1;
1225379bc100SJani Nikula 
1226379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
122701a60883SJani Nikula 	if (IS_GEN9_BC(dev_priv) && intel_bios_hdmi_boost_level(encoder))
1228379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
1229379bc100SJani Nikula 
1230379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
1231f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
1232379bc100SJani Nikula 		       ddi_translations[level].trans1 | iboost_bit);
1233f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
1234379bc100SJani Nikula 		       ddi_translations[level].trans2);
1235379bc100SJani Nikula }
1236379bc100SJani Nikula 
1237379bc100SJani Nikula static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
1238379bc100SJani Nikula 				    enum port port)
1239379bc100SJani Nikula {
12405a2ad99bSManasi Navare 	if (IS_BROXTON(dev_priv)) {
12415a2ad99bSManasi Navare 		udelay(16);
1242379bc100SJani Nikula 		return;
1243379bc100SJani Nikula 	}
12445a2ad99bSManasi Navare 
12455a2ad99bSManasi Navare 	if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
12465a2ad99bSManasi Navare 			 DDI_BUF_IS_IDLE), 8))
12475a2ad99bSManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
124847bdb1caSJani Nikula 			port_name(port));
1249379bc100SJani Nikula }
1250379bc100SJani Nikula 
1251e828da30SManasi Navare static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
1252e828da30SManasi Navare 				      enum port port)
1253e828da30SManasi Navare {
1254e828da30SManasi Navare 	/* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
1255e828da30SManasi Navare 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
1256e828da30SManasi Navare 		usleep_range(518, 1000);
1257e828da30SManasi Navare 		return;
1258e828da30SManasi Navare 	}
1259e828da30SManasi Navare 
1260e828da30SManasi Navare 	if (wait_for_us(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1261e828da30SManasi Navare 			  DDI_BUF_IS_IDLE), 500))
1262e828da30SManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
1263e828da30SManasi Navare 			port_name(port));
1264e828da30SManasi Navare }
1265e828da30SManasi Navare 
1266379bc100SJani Nikula static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
1267379bc100SJani Nikula {
1268379bc100SJani Nikula 	switch (pll->info->id) {
1269379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
1270379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
1271379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
1272379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
1273379bc100SJani Nikula 	case DPLL_ID_SPLL:
1274379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
1275379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
1276379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
1277379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
1278379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
1279379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
1280379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
1281379bc100SJani Nikula 	default:
1282379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
1283379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
1284379bc100SJani Nikula 	}
1285379bc100SJani Nikula }
1286379bc100SJani Nikula 
1287379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
1288379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
1289379bc100SJani Nikula {
1290379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1291379bc100SJani Nikula 	int clock = crtc_state->port_clock;
1292379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
1293379bc100SJani Nikula 
1294379bc100SJani Nikula 	switch (id) {
1295379bc100SJani Nikula 	default:
1296379bc100SJani Nikula 		/*
1297379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
1298379bc100SJani Nikula 		 * here, so do warn if this get passed in
1299379bc100SJani Nikula 		 */
1300379bc100SJani Nikula 		MISSING_CASE(id);
1301379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
1302379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
1303379bc100SJani Nikula 		switch (clock) {
1304379bc100SJani Nikula 		case 162000:
1305379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
1306379bc100SJani Nikula 		case 270000:
1307379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
1308379bc100SJani Nikula 		case 540000:
1309379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
1310379bc100SJani Nikula 		case 810000:
1311379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
1312379bc100SJani Nikula 		default:
1313379bc100SJani Nikula 			MISSING_CASE(clock);
1314379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
1315379bc100SJani Nikula 		}
1316379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
1317379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
1318379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
1319379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
13206677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
13216677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
1322379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
1323379bc100SJani Nikula 	}
1324379bc100SJani Nikula }
1325379bc100SJani Nikula 
1326379bc100SJani Nikula /* Starting with Haswell, different DDI ports can work in FDI mode for
1327379bc100SJani Nikula  * connection to the PCH-located connectors. For this, it is necessary to train
1328379bc100SJani Nikula  * both the DDI port and PCH receiver for the desired DDI buffer settings.
1329379bc100SJani Nikula  *
1330379bc100SJani Nikula  * The recommended port to work in FDI mode is DDI E, which we use here. Also,
1331379bc100SJani Nikula  * please note that when FDI mode is active on DDI E, it shares 2 lines with
1332379bc100SJani Nikula  * DDI A (which is used for eDP)
1333379bc100SJani Nikula  */
1334379bc100SJani Nikula 
13356a6d79deSVille Syrjälä void hsw_fdi_link_train(struct intel_encoder *encoder,
1336379bc100SJani Nikula 			const struct intel_crtc_state *crtc_state)
1337379bc100SJani Nikula {
13386a6d79deSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13396a6d79deSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1340379bc100SJani Nikula 	u32 temp, i, rx_ctl_val, ddi_pll_sel;
1341379bc100SJani Nikula 
1342379bc100SJani Nikula 	intel_prepare_dp_ddi_buffers(encoder, crtc_state);
1343379bc100SJani Nikula 
1344379bc100SJani Nikula 	/* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
1345379bc100SJani Nikula 	 * mode set "sequence for CRT port" document:
1346379bc100SJani Nikula 	 * - TP1 to TP2 time with the default value
1347379bc100SJani Nikula 	 * - FDI delay to 90h
1348379bc100SJani Nikula 	 *
1349379bc100SJani Nikula 	 * WaFDIAutoLinkSetTimingOverrride:hsw
1350379bc100SJani Nikula 	 */
1351f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A),
1352f7960e7fSJani Nikula 		       FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2) | FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
1353379bc100SJani Nikula 
1354379bc100SJani Nikula 	/* Enable the PCH Receiver FDI PLL */
1355379bc100SJani Nikula 	rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
1356379bc100SJani Nikula 		     FDI_RX_PLL_ENABLE |
1357379bc100SJani Nikula 		     FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
1358f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1359f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, FDI_RX_CTL(PIPE_A));
1360379bc100SJani Nikula 	udelay(220);
1361379bc100SJani Nikula 
1362379bc100SJani Nikula 	/* Switch from Rawclk to PCDclk */
1363379bc100SJani Nikula 	rx_ctl_val |= FDI_PCDCLK;
1364f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1365379bc100SJani Nikula 
1366379bc100SJani Nikula 	/* Configure Port Clock Select */
1367379bc100SJani Nikula 	ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
1368f7960e7fSJani Nikula 	intel_de_write(dev_priv, PORT_CLK_SEL(PORT_E), ddi_pll_sel);
13691de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ddi_pll_sel != PORT_CLK_SEL_SPLL);
1370379bc100SJani Nikula 
1371379bc100SJani Nikula 	/* Start the training iterating through available voltages and emphasis,
1372379bc100SJani Nikula 	 * testing each value twice. */
1373379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
1374379bc100SJani Nikula 		/* Configure DP_TP_CTL with auto-training */
1375f7960e7fSJani Nikula 		intel_de_write(dev_priv, DP_TP_CTL(PORT_E),
13767db8736dSVille Syrjälä 			       DP_TP_CTL_FDI_AUTOTRAIN |
13777db8736dSVille Syrjälä 			       DP_TP_CTL_ENHANCED_FRAME_ENABLE |
13787db8736dSVille Syrjälä 			       DP_TP_CTL_LINK_TRAIN_PAT1 |
13797db8736dSVille Syrjälä 			       DP_TP_CTL_ENABLE);
1380379bc100SJani Nikula 
1381379bc100SJani Nikula 		/* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
1382379bc100SJani Nikula 		 * DDI E does not support port reversal, the functionality is
1383379bc100SJani Nikula 		 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
1384379bc100SJani Nikula 		 * port reversal bit */
1385f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(PORT_E),
1386f7960e7fSJani Nikula 			       DDI_BUF_CTL_ENABLE | ((crtc_state->fdi_lanes - 1) << 1) | DDI_BUF_TRANS_SELECT(i / 2));
1387f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DDI_BUF_CTL(PORT_E));
1388379bc100SJani Nikula 
1389379bc100SJani Nikula 		udelay(600);
1390379bc100SJani Nikula 
1391379bc100SJani Nikula 		/* Program PCH FDI Receiver TU */
1392f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
1393379bc100SJani Nikula 
1394379bc100SJani Nikula 		/* Enable PCH FDI Receiver with auto-training */
1395379bc100SJani Nikula 		rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
1396f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1397f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_CTL(PIPE_A));
1398379bc100SJani Nikula 
1399379bc100SJani Nikula 		/* Wait for FDI receiver lane calibration */
1400379bc100SJani Nikula 		udelay(30);
1401379bc100SJani Nikula 
1402379bc100SJani Nikula 		/* Unset FDI_RX_MISC pwrdn lanes */
1403f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
1404379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1405f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), temp);
1406f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_MISC(PIPE_A));
1407379bc100SJani Nikula 
1408379bc100SJani Nikula 		/* Wait for FDI auto training time */
1409379bc100SJani Nikula 		udelay(5);
1410379bc100SJani Nikula 
1411f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DP_TP_STATUS(PORT_E));
1412379bc100SJani Nikula 		if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
141347bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
141447bdb1caSJani Nikula 				    "FDI link training done on step %d\n", i);
1415379bc100SJani Nikula 			break;
1416379bc100SJani Nikula 		}
1417379bc100SJani Nikula 
1418379bc100SJani Nikula 		/*
1419379bc100SJani Nikula 		 * Leave things enabled even if we failed to train FDI.
1420379bc100SJani Nikula 		 * Results in less fireworks from the state checker.
1421379bc100SJani Nikula 		 */
1422379bc100SJani Nikula 		if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
142347bdb1caSJani Nikula 			drm_err(&dev_priv->drm, "FDI link training failed!\n");
1424379bc100SJani Nikula 			break;
1425379bc100SJani Nikula 		}
1426379bc100SJani Nikula 
1427379bc100SJani Nikula 		rx_ctl_val &= ~FDI_RX_ENABLE;
1428f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1429f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_CTL(PIPE_A));
1430379bc100SJani Nikula 
1431f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_E));
1432379bc100SJani Nikula 		temp &= ~DDI_BUF_CTL_ENABLE;
1433f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(PORT_E), temp);
1434f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DDI_BUF_CTL(PORT_E));
1435379bc100SJani Nikula 
1436379bc100SJani Nikula 		/* Disable DP_TP_CTL and FDI_RX_CTL and retry */
1437f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DP_TP_CTL(PORT_E));
1438379bc100SJani Nikula 		temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1439379bc100SJani Nikula 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1440f7960e7fSJani Nikula 		intel_de_write(dev_priv, DP_TP_CTL(PORT_E), temp);
1441f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DP_TP_CTL(PORT_E));
1442379bc100SJani Nikula 
1443379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, PORT_E);
1444379bc100SJani Nikula 
1445379bc100SJani Nikula 		/* Reset FDI_RX_MISC pwrdn lanes */
1446f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
1447379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1448379bc100SJani Nikula 		temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1449f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), temp);
1450f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_MISC(PIPE_A));
1451379bc100SJani Nikula 	}
1452379bc100SJani Nikula 
1453379bc100SJani Nikula 	/* Enable normal pixel sending for FDI */
1454f7960e7fSJani Nikula 	intel_de_write(dev_priv, DP_TP_CTL(PORT_E),
14557db8736dSVille Syrjälä 		       DP_TP_CTL_FDI_AUTOTRAIN |
14567db8736dSVille Syrjälä 		       DP_TP_CTL_LINK_TRAIN_NORMAL |
14577db8736dSVille Syrjälä 		       DP_TP_CTL_ENHANCED_FRAME_ENABLE |
14587db8736dSVille Syrjälä 		       DP_TP_CTL_ENABLE);
1459379bc100SJani Nikula }
1460379bc100SJani Nikula 
1461379bc100SJani Nikula static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
1462379bc100SJani Nikula {
1463b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
14647801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1465379bc100SJani Nikula 
14667801f3b7SLucas De Marchi 	intel_dp->DP = dig_port->saved_port_bits |
1467379bc100SJani Nikula 		DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
1468379bc100SJani Nikula 	intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
1469379bc100SJani Nikula }
1470379bc100SJani Nikula 
1471379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
1472379bc100SJani Nikula 				 enum port port)
1473379bc100SJani Nikula {
1474f7960e7fSJani Nikula 	u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
1475379bc100SJani Nikula 
1476379bc100SJani Nikula 	switch (val) {
1477379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
1478379bc100SJani Nikula 		return 0;
1479379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
1480379bc100SJani Nikula 		return 162000;
1481379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
1482379bc100SJani Nikula 		return 270000;
1483379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
1484379bc100SJani Nikula 		return 540000;
1485379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
1486379bc100SJani Nikula 		return 810000;
1487379bc100SJani Nikula 	default:
1488379bc100SJani Nikula 		MISSING_CASE(val);
1489379bc100SJani Nikula 		return 0;
1490379bc100SJani Nikula 	}
1491379bc100SJani Nikula }
1492379bc100SJani Nikula 
1493379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
1494379bc100SJani Nikula {
1495379bc100SJani Nikula 	int dotclock;
1496379bc100SJani Nikula 
1497379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
1498379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1499379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
1500379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
1501379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1502379bc100SJani Nikula 						    &pipe_config->dp_m_n);
15032969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
15042969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
1505379bc100SJani Nikula 	else
1506379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
1507379bc100SJani Nikula 
1508379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
1509379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
1510379bc100SJani Nikula 		dotclock *= 2;
1511379bc100SJani Nikula 
1512379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
1513379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
1514379bc100SJani Nikula 
15151326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
1516379bc100SJani Nikula }
1517379bc100SJani Nikula 
1518379bc100SJani Nikula static void intel_ddi_clock_get(struct intel_encoder *encoder,
1519379bc100SJani Nikula 				struct intel_crtc_state *pipe_config)
1520379bc100SJani Nikula {
1521379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
152256ed441aSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1523379bc100SJani Nikula 
152456ed441aSMatt Roper 	if (intel_phy_is_tc(dev_priv, phy) &&
152545e4728bSImre Deak 	    intel_get_shared_dpll_id(dev_priv, pipe_config->shared_dpll) ==
152645e4728bSImre Deak 	    DPLL_ID_ICL_TBTPLL)
152745e4728bSImre Deak 		pipe_config->port_clock = icl_calc_tbt_pll_link(dev_priv,
152845e4728bSImre Deak 								encoder->port);
152945e4728bSImre Deak 	else
1530b953eb21SImre Deak 		pipe_config->port_clock =
1531b953eb21SImre Deak 			intel_dpll_get_freq(dev_priv, pipe_config->shared_dpll);
153245e4728bSImre Deak 
153345e4728bSImre Deak 	ddi_dotclock_get(pipe_config);
1534379bc100SJani Nikula }
1535379bc100SJani Nikula 
15360c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
15370c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
1538379bc100SJani Nikula {
15392225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1540379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1541379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1542379bc100SJani Nikula 	u32 temp;
1543379bc100SJani Nikula 
1544379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
1545379bc100SJani Nikula 		return;
1546379bc100SJani Nikula 
15471de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
1548379bc100SJani Nikula 
15493e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
1550379bc100SJani Nikula 
1551379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1552379bc100SJani Nikula 	case 18:
15533e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
1554379bc100SJani Nikula 		break;
1555379bc100SJani Nikula 	case 24:
15563e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
1557379bc100SJani Nikula 		break;
1558379bc100SJani Nikula 	case 30:
15593e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
1560379bc100SJani Nikula 		break;
1561379bc100SJani Nikula 	case 36:
15623e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
1563379bc100SJani Nikula 		break;
1564379bc100SJani Nikula 	default:
1565379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
1566379bc100SJani Nikula 		break;
1567379bc100SJani Nikula 	}
1568379bc100SJani Nikula 
1569cae154fcSVille Syrjälä 	/* nonsense combination */
15701de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
1571cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
1572cae154fcSVille Syrjälä 
1573cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
15743e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
1575cae154fcSVille Syrjälä 
1576379bc100SJani Nikula 	/*
1577379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
1578379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
1579646d3dc8SVille Syrjälä 	 * colorspace information.
1580379bc100SJani Nikula 	 */
1581379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
15823e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
1583646d3dc8SVille Syrjälä 
1584379bc100SJani Nikula 	/*
1585379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
1586379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
15870c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
15880c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
1589379bc100SJani Nikula 	 */
1590bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
15913e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
15920c06fa15SGwan-gyeong Mun 
1593f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
1594379bc100SJani Nikula }
1595379bc100SJani Nikula 
1596dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
1597dc5b8ed5SVille Syrjälä {
1598dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
1599dc5b8ed5SVille Syrjälä 		return 0;
1600dc5b8ed5SVille Syrjälä 	else
1601dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
1602dc5b8ed5SVille Syrjälä }
1603dc5b8ed5SVille Syrjälä 
160499389390SJosé Roberto de Souza /*
160599389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
160699389390SJosé Roberto de Souza  *
160799389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
160899389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
160999389390SJosé Roberto de Souza  */
161099389390SJosé Roberto de Souza static u32
1611eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
1612eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
1613379bc100SJani Nikula {
16142225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1615379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1616379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
1617379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1618379bc100SJani Nikula 	enum port port = encoder->port;
1619379bc100SJani Nikula 	u32 temp;
1620379bc100SJani Nikula 
1621379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1622379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
1623df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12)
1624df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
1625df16b636SMahesh Kumar 	else
1626379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
1627379bc100SJani Nikula 
1628379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1629379bc100SJani Nikula 	case 18:
1630379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
1631379bc100SJani Nikula 		break;
1632379bc100SJani Nikula 	case 24:
1633379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
1634379bc100SJani Nikula 		break;
1635379bc100SJani Nikula 	case 30:
1636379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
1637379bc100SJani Nikula 		break;
1638379bc100SJani Nikula 	case 36:
1639379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
1640379bc100SJani Nikula 		break;
1641379bc100SJani Nikula 	default:
1642379bc100SJani Nikula 		BUG();
1643379bc100SJani Nikula 	}
1644379bc100SJani Nikula 
16451326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
1646379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
16471326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
1648379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
1649379bc100SJani Nikula 
1650379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
1651379bc100SJani Nikula 		switch (pipe) {
1652379bc100SJani Nikula 		case PIPE_A:
1653379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
1654379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
1655379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
1656379bc100SJani Nikula 			 * support). */
1657379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
1658379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1659379bc100SJani Nikula 			else
1660379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
1661379bc100SJani Nikula 			break;
1662379bc100SJani Nikula 		case PIPE_B:
1663379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1664379bc100SJani Nikula 			break;
1665379bc100SJani Nikula 		case PIPE_C:
1666379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1667379bc100SJani Nikula 			break;
1668379bc100SJani Nikula 		default:
1669379bc100SJani Nikula 			BUG();
1670379bc100SJani Nikula 			break;
1671379bc100SJani Nikula 		}
1672379bc100SJani Nikula 	}
1673379bc100SJani Nikula 
1674379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1675379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
1676379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
1677379bc100SJani Nikula 		else
1678379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
1679379bc100SJani Nikula 
1680379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
1681379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
1682379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
1683379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
1684379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1685379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI;
1686379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
1687379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
1688379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1689379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1690b3545e08SLucas De Marchi 
16916671c367SJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12) {
16926671c367SJosé Roberto de Souza 			enum transcoder master;
16936671c367SJosé Roberto de Souza 
16946671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
16951de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
16961de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
16976671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
16986671c367SJosé Roberto de Souza 		}
1699379bc100SJani Nikula 	} else {
1700379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1701379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1702379bc100SJani Nikula 	}
1703379bc100SJani Nikula 
1704dc5b8ed5SVille Syrjälä 	if (IS_GEN_RANGE(dev_priv, 8, 10) &&
1705dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
1706dc5b8ed5SVille Syrjälä 		u8 master_select =
1707dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
1708dc5b8ed5SVille Syrjälä 
1709dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
1710dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
1711dc5b8ed5SVille Syrjälä 	}
1712dc5b8ed5SVille Syrjälä 
171399389390SJosé Roberto de Souza 	return temp;
171499389390SJosé Roberto de Souza }
171599389390SJosé Roberto de Souza 
1716eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
1717eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
171899389390SJosé Roberto de Souza {
17192225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
172099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
172199389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
172299389390SJosé Roberto de Souza 
1723589a4cd6SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
1724589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
1725589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
1726589a4cd6SVille Syrjälä 
1727589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
1728dc5b8ed5SVille Syrjälä 			u8 master_select =
1729dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
1730589a4cd6SVille Syrjälä 
1731589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
1732d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
1733589a4cd6SVille Syrjälä 		}
1734589a4cd6SVille Syrjälä 
1735589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
1736589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
1737589a4cd6SVille Syrjälä 	}
1738589a4cd6SVille Syrjälä 
1739580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
1740580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
1741580fbdc5SImre Deak 							     crtc_state));
174299389390SJosé Roberto de Souza }
174399389390SJosé Roberto de Souza 
174499389390SJosé Roberto de Souza /*
174599389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
174699389390SJosé Roberto de Souza  * bit.
174799389390SJosé Roberto de Souza  */
174899389390SJosé Roberto de Souza static void
1749eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
1750eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
175199389390SJosé Roberto de Souza {
17522225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
175399389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
175499389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1755589a4cd6SVille Syrjälä 	u32 ctl;
175699389390SJosé Roberto de Souza 
1757eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
1758589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
1759589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
1760379bc100SJani Nikula }
1761379bc100SJani Nikula 
1762379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
1763379bc100SJani Nikula {
17642225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1765379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1766379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1767589a4cd6SVille Syrjälä 	u32 ctl;
1768c59053dcSJosé Roberto de Souza 
1769589a4cd6SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11)
1770589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
1771589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
1772589a4cd6SVille Syrjälä 
1773589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
1774dc5b8ed5SVille Syrjälä 
1775589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
1776379bc100SJani Nikula 
1777dc5b8ed5SVille Syrjälä 	if (IS_GEN_RANGE(dev_priv, 8, 10))
1778dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
1779dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
1780dc5b8ed5SVille Syrjälä 
1781df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12) {
1782919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
1783589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
1784919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
1785919e4f07SJosé Roberto de Souza 		}
1786df16b636SMahesh Kumar 	} else {
1787589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
1788df16b636SMahesh Kumar 	}
1789dc5b8ed5SVille Syrjälä 
1790589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
1791379bc100SJani Nikula 
1792379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
1793379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
179447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
179547bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
1796379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
1797379bc100SJani Nikula 		msleep(100);
1798379bc100SJani Nikula 	}
1799379bc100SJani Nikula }
1800379bc100SJani Nikula 
1801379bc100SJani Nikula int intel_ddi_toggle_hdcp_signalling(struct intel_encoder *intel_encoder,
1802379bc100SJani Nikula 				     bool enable)
1803379bc100SJani Nikula {
1804379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
1805379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1806379bc100SJani Nikula 	intel_wakeref_t wakeref;
1807379bc100SJani Nikula 	enum pipe pipe = 0;
1808379bc100SJani Nikula 	int ret = 0;
1809379bc100SJani Nikula 	u32 tmp;
1810379bc100SJani Nikula 
1811379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1812379bc100SJani Nikula 						     intel_encoder->power_domain);
18131de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
1814379bc100SJani Nikula 		return -ENXIO;
1815379bc100SJani Nikula 
18161de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev,
18171de143ccSPankaj Bharadiya 			!intel_encoder->get_hw_state(intel_encoder, &pipe))) {
1818379bc100SJani Nikula 		ret = -EIO;
1819379bc100SJani Nikula 		goto out;
1820379bc100SJani Nikula 	}
1821379bc100SJani Nikula 
1822f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(pipe));
1823379bc100SJani Nikula 	if (enable)
1824379bc100SJani Nikula 		tmp |= TRANS_DDI_HDCP_SIGNALLING;
1825379bc100SJani Nikula 	else
1826379bc100SJani Nikula 		tmp &= ~TRANS_DDI_HDCP_SIGNALLING;
1827f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe), tmp);
1828379bc100SJani Nikula out:
1829379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
1830379bc100SJani Nikula 	return ret;
1831379bc100SJani Nikula }
1832379bc100SJani Nikula 
1833379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1834379bc100SJani Nikula {
1835379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
1836379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1837fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
1838379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
1839379bc100SJani Nikula 	enum port port = encoder->port;
1840379bc100SJani Nikula 	enum transcoder cpu_transcoder;
1841379bc100SJani Nikula 	intel_wakeref_t wakeref;
1842379bc100SJani Nikula 	enum pipe pipe = 0;
1843379bc100SJani Nikula 	u32 tmp;
1844379bc100SJani Nikula 	bool ret;
1845379bc100SJani Nikula 
1846379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1847379bc100SJani Nikula 						     encoder->power_domain);
1848379bc100SJani Nikula 	if (!wakeref)
1849379bc100SJani Nikula 		return false;
1850379bc100SJani Nikula 
1851379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
1852379bc100SJani Nikula 		ret = false;
1853379bc100SJani Nikula 		goto out;
1854379bc100SJani Nikula 	}
1855379bc100SJani Nikula 
185610cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
1857379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
1858379bc100SJani Nikula 	else
1859379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
1860379bc100SJani Nikula 
1861f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
1862379bc100SJani Nikula 
1863379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1864379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
1865379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
1866379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
1867379bc100SJani Nikula 		break;
1868379bc100SJani Nikula 
1869379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
1870379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
1871379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
1872379bc100SJani Nikula 		break;
1873379bc100SJani Nikula 
1874379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
1875379bc100SJani Nikula 		/* if the transcoder is in MST state then
1876379bc100SJani Nikula 		 * connector isn't connected */
1877379bc100SJani Nikula 		ret = false;
1878379bc100SJani Nikula 		break;
1879379bc100SJani Nikula 
1880379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
1881379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_VGA;
1882379bc100SJani Nikula 		break;
1883379bc100SJani Nikula 
1884379bc100SJani Nikula 	default:
1885379bc100SJani Nikula 		ret = false;
1886379bc100SJani Nikula 		break;
1887379bc100SJani Nikula 	}
1888379bc100SJani Nikula 
1889379bc100SJani Nikula out:
1890379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1891379bc100SJani Nikula 
1892379bc100SJani Nikula 	return ret;
1893379bc100SJani Nikula }
1894379bc100SJani Nikula 
1895379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
1896379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
1897379bc100SJani Nikula {
1898379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
1899379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1900379bc100SJani Nikula 	enum port port = encoder->port;
1901379bc100SJani Nikula 	intel_wakeref_t wakeref;
1902379bc100SJani Nikula 	enum pipe p;
1903379bc100SJani Nikula 	u32 tmp;
1904379bc100SJani Nikula 	u8 mst_pipe_mask;
1905379bc100SJani Nikula 
1906379bc100SJani Nikula 	*pipe_mask = 0;
1907379bc100SJani Nikula 	*is_dp_mst = false;
1908379bc100SJani Nikula 
1909379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1910379bc100SJani Nikula 						     encoder->power_domain);
1911379bc100SJani Nikula 	if (!wakeref)
1912379bc100SJani Nikula 		return;
1913379bc100SJani Nikula 
1914f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
1915379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
1916379bc100SJani Nikula 		goto out;
1917379bc100SJani Nikula 
191810cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
1919f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
1920f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
1921379bc100SJani Nikula 
1922379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1923379bc100SJani Nikula 		default:
1924379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
1925379bc100SJani Nikula 			/* fallthrough */
1926379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
1927379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
1928379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
1929379bc100SJani Nikula 			break;
1930379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
1931379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
1932379bc100SJani Nikula 			break;
1933379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
1934379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
1935379bc100SJani Nikula 			break;
1936379bc100SJani Nikula 		}
1937379bc100SJani Nikula 
1938379bc100SJani Nikula 		goto out;
1939379bc100SJani Nikula 	}
1940379bc100SJani Nikula 
1941379bc100SJani Nikula 	mst_pipe_mask = 0;
1942379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
1943379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
1944df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
19456aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
19466aa3bef1SJosé Roberto de Souza 
19476aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
19486aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
19496aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
19506aa3bef1SJosé Roberto de Souza 			continue;
1951df16b636SMahesh Kumar 
1952df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12) {
1953df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
1954df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
1955df16b636SMahesh Kumar 		} else {
1956df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
1957df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
1958df16b636SMahesh Kumar 		}
1959379bc100SJani Nikula 
1960f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
1961f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
19626aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
19636aa3bef1SJosé Roberto de Souza 					trans_wakeref);
1964379bc100SJani Nikula 
1965df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
1966379bc100SJani Nikula 			continue;
1967379bc100SJani Nikula 
1968379bc100SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
1969379bc100SJani Nikula 		    TRANS_DDI_MODE_SELECT_DP_MST)
1970379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
1971379bc100SJani Nikula 
1972379bc100SJani Nikula 		*pipe_mask |= BIT(p);
1973379bc100SJani Nikula 	}
1974379bc100SJani Nikula 
1975379bc100SJani Nikula 	if (!*pipe_mask)
197647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
197747bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
197866a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
1979379bc100SJani Nikula 
1980379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
198147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
198247bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
198366a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
198466a990ddSVille Syrjälä 			    *pipe_mask);
1985379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
1986379bc100SJani Nikula 	}
1987379bc100SJani Nikula 
1988379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
198947bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
199047bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
199166a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
199266a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
1993379bc100SJani Nikula 	else
1994379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
1995379bc100SJani Nikula 
1996379bc100SJani Nikula out:
1997379bc100SJani Nikula 	if (*pipe_mask && IS_GEN9_LP(dev_priv)) {
1998f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
1999379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
2000379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
2001379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
200247bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
200347bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
200447bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
2005379bc100SJani Nikula 	}
2006379bc100SJani Nikula 
2007379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
2008379bc100SJani Nikula }
2009379bc100SJani Nikula 
2010379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
2011379bc100SJani Nikula 			    enum pipe *pipe)
2012379bc100SJani Nikula {
2013379bc100SJani Nikula 	u8 pipe_mask;
2014379bc100SJani Nikula 	bool is_mst;
2015379bc100SJani Nikula 
2016379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2017379bc100SJani Nikula 
2018379bc100SJani Nikula 	if (is_mst || !pipe_mask)
2019379bc100SJani Nikula 		return false;
2020379bc100SJani Nikula 
2021379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
2022379bc100SJani Nikula 
2023379bc100SJani Nikula 	return true;
2024379bc100SJani Nikula }
2025379bc100SJani Nikula 
202681b55ef1SJani Nikula static enum intel_display_power_domain
2027379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
2028379bc100SJani Nikula {
2029379bc100SJani Nikula 	/* CNL+ HW requires corresponding AUX IOs to be powered up for PSR with
2030379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
2031379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
2032379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
2033379bc100SJani Nikula 	 * states enabled.
2034379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
2035379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
2036379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
2037379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
2038379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
2039379bc100SJani Nikula 	 * returns the correct domain for other ports too.
2040379bc100SJani Nikula 	 */
2041379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
2042379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
2043379bc100SJani Nikula }
2044379bc100SJani Nikula 
2045379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
2046379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
2047379bc100SJani Nikula {
2048379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2049379bc100SJani Nikula 	struct intel_digital_port *dig_port;
2050d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2051379bc100SJani Nikula 
2052379bc100SJani Nikula 	/*
2053379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
2054379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
2055379bc100SJani Nikula 	 * hook.
2056379bc100SJani Nikula 	 */
20571de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
20581de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
2059379bc100SJani Nikula 		return;
2060379bc100SJani Nikula 
2061b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
2062f77a2db2SImre Deak 
2063f77a2db2SImre Deak 	if (!intel_phy_is_tc(dev_priv, phy) ||
2064f77a2db2SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
2065f77a2db2SImre Deak 		intel_display_power_get(dev_priv,
2066f77a2db2SImre Deak 					dig_port->ddi_io_power_domain);
2067379bc100SJani Nikula 
2068379bc100SJani Nikula 	/*
2069379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
2070379bc100SJani Nikula 	 * ports.
2071379bc100SJani Nikula 	 */
2072379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
2073d8fe2ab6SMatt Roper 	    intel_phy_is_tc(dev_priv, phy))
2074379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2075379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
2076379bc100SJani Nikula 
2077379bc100SJani Nikula 	/*
2078379bc100SJani Nikula 	 * VDSC power is needed when DSC is enabled
2079379bc100SJani Nikula 	 */
2080010663a6SJani Nikula 	if (crtc_state->dsc.compression_enable)
2081379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2082379bc100SJani Nikula 					intel_dsc_power_domain(crtc_state));
2083379bc100SJani Nikula }
2084379bc100SJani Nikula 
208502a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
208602a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
2087379bc100SJani Nikula {
20882225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2089379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2090379bc100SJani Nikula 	enum port port = encoder->port;
2091379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2092379bc100SJani Nikula 
2093df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2094df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2095f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2096f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2097df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_PORT(port));
2098df16b636SMahesh Kumar 		else
2099f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2100f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2101379bc100SJani Nikula 				       TRANS_CLK_SEL_PORT(port));
2102379bc100SJani Nikula 	}
2103df16b636SMahesh Kumar }
2104379bc100SJani Nikula 
2105379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
2106379bc100SJani Nikula {
21072225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2108379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2109379bc100SJani Nikula 
2110df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2111df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2112f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2113f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2114df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
2115df16b636SMahesh Kumar 		else
2116f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2117f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2118379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
2119379bc100SJani Nikula 	}
2120df16b636SMahesh Kumar }
2121379bc100SJani Nikula 
2122379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
2123379bc100SJani Nikula 				enum port port, u8 iboost)
2124379bc100SJani Nikula {
2125379bc100SJani Nikula 	u32 tmp;
2126379bc100SJani Nikula 
2127f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
2128379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
2129379bc100SJani Nikula 	if (iboost)
2130379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
2131379bc100SJani Nikula 	else
2132379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
2133f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
2134379bc100SJani Nikula }
2135379bc100SJani Nikula 
2136379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
2137379bc100SJani Nikula 			       int level, enum intel_output_type type)
2138379bc100SJani Nikula {
21397801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2140379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2141379bc100SJani Nikula 	u8 iboost;
2142379bc100SJani Nikula 
2143379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
214401a60883SJani Nikula 		iboost = intel_bios_hdmi_boost_level(encoder);
2145379bc100SJani Nikula 	else
2146605a1872SJani Nikula 		iboost = intel_bios_dp_boost_level(encoder);
2147379bc100SJani Nikula 
2148379bc100SJani Nikula 	if (iboost == 0) {
2149379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations;
2150379bc100SJani Nikula 		int n_entries;
2151379bc100SJani Nikula 
2152379bc100SJani Nikula 		if (type == INTEL_OUTPUT_HDMI)
2153a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
2154379bc100SJani Nikula 		else if (type == INTEL_OUTPUT_EDP)
2155a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_edp(encoder,
2156a8143150SJosé Roberto de Souza 								       &n_entries);
2157379bc100SJani Nikula 		else
2158a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_dp(encoder,
2159a8143150SJosé Roberto de Souza 								      &n_entries);
2160379bc100SJani Nikula 
21611de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2162379bc100SJani Nikula 			return;
21631de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2164379bc100SJani Nikula 			level = n_entries - 1;
2165379bc100SJani Nikula 
2166379bc100SJani Nikula 		iboost = ddi_translations[level].i_boost;
2167379bc100SJani Nikula 	}
2168379bc100SJani Nikula 
2169379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
2170379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
217147bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
2172379bc100SJani Nikula 		return;
2173379bc100SJani Nikula 	}
2174379bc100SJani Nikula 
2175f0e86e05SJosé Roberto de Souza 	_skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
2176379bc100SJani Nikula 
2177f0e86e05SJosé Roberto de Souza 	if (encoder->port == PORT_A && dig_port->max_lanes == 4)
2178379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
2179379bc100SJani Nikula }
2180379bc100SJani Nikula 
2181379bc100SJani Nikula static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
2182379bc100SJani Nikula 				    int level, enum intel_output_type type)
2183379bc100SJani Nikula {
2184379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2185379bc100SJani Nikula 	const struct bxt_ddi_buf_trans *ddi_translations;
2186379bc100SJani Nikula 	enum port port = encoder->port;
2187379bc100SJani Nikula 	int n_entries;
2188379bc100SJani Nikula 
2189379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2190a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_hdmi(encoder, &n_entries);
2191379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2192a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_edp(encoder, &n_entries);
2193379bc100SJani Nikula 	else
2194a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_dp(encoder, &n_entries);
2195379bc100SJani Nikula 
21961de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2197379bc100SJani Nikula 		return;
21981de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2199379bc100SJani Nikula 		level = n_entries - 1;
2200379bc100SJani Nikula 
2201379bc100SJani Nikula 	bxt_ddi_phy_set_signal_level(dev_priv, port,
2202379bc100SJani Nikula 				     ddi_translations[level].margin,
2203379bc100SJani Nikula 				     ddi_translations[level].scale,
2204379bc100SJani Nikula 				     ddi_translations[level].enable,
2205379bc100SJani Nikula 				     ddi_translations[level].deemphasis);
2206379bc100SJani Nikula }
2207379bc100SJani Nikula 
220853de0a20SVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp)
2209379bc100SJani Nikula {
221053de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2211379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2212379bc100SJani Nikula 	enum port port = encoder->port;
2213d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2214379bc100SJani Nikula 	int n_entries;
2215379bc100SJani Nikula 
2216978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
2217978c3e53SClinton A Taylor 		if (intel_phy_is_combo(dev_priv, phy))
2218a8143150SJosé Roberto de Souza 			tgl_get_combo_buf_trans(encoder, encoder->type,
2219978c3e53SClinton A Taylor 						intel_dp->link_rate, &n_entries);
2220978c3e53SClinton A Taylor 		else
2221a8143150SJosé Roberto de Souza 			tgl_get_dkl_buf_trans(encoder, encoder->type,
22229fa67699SJosé Roberto de Souza 					      intel_dp->link_rate, &n_entries);
2223978c3e53SClinton A Taylor 	} else if (INTEL_GEN(dev_priv) == 11) {
2224b42d5a67SJosé Roberto de Souza 		if (IS_ELKHARTLAKE(dev_priv))
2225a8143150SJosé Roberto de Souza 			ehl_get_combo_buf_trans(encoder, encoder->type,
2226b42d5a67SJosé Roberto de Souza 						intel_dp->link_rate, &n_entries);
2227b42d5a67SJosé Roberto de Souza 		else if (intel_phy_is_combo(dev_priv, phy))
2228a8143150SJosé Roberto de Souza 			icl_get_combo_buf_trans(encoder, encoder->type,
2229379bc100SJani Nikula 						intel_dp->link_rate, &n_entries);
2230379bc100SJani Nikula 		else
2231a8143150SJosé Roberto de Souza 			icl_get_mg_buf_trans(encoder, encoder->type,
22329f7ffa29SJosé Roberto de Souza 					     intel_dp->link_rate, &n_entries);
2233379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2234379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2235a8143150SJosé Roberto de Souza 			cnl_get_buf_trans_edp(encoder, &n_entries);
2236379bc100SJani Nikula 		else
2237a8143150SJosé Roberto de Souza 			cnl_get_buf_trans_dp(encoder, &n_entries);
2238379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
2239379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2240a8143150SJosé Roberto de Souza 			bxt_get_buf_trans_edp(encoder, &n_entries);
2241379bc100SJani Nikula 		else
2242a8143150SJosé Roberto de Souza 			bxt_get_buf_trans_dp(encoder, &n_entries);
2243379bc100SJani Nikula 	} else {
2244379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2245f0e86e05SJosé Roberto de Souza 			intel_ddi_get_buf_trans_edp(encoder, &n_entries);
2246379bc100SJani Nikula 		else
2247f0e86e05SJosé Roberto de Souza 			intel_ddi_get_buf_trans_dp(encoder, &n_entries);
2248379bc100SJani Nikula 	}
2249379bc100SJani Nikula 
22501de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
2251379bc100SJani Nikula 		n_entries = 1;
22521de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
22531de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
2254379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
2255379bc100SJani Nikula 
2256379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
2257379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
2258379bc100SJani Nikula }
2259379bc100SJani Nikula 
2260379bc100SJani Nikula /*
2261379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
2262379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
2263379bc100SJani Nikula  * rethink this code.
2264379bc100SJani Nikula  */
226553de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
2266379bc100SJani Nikula {
2267379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
2268379bc100SJani Nikula }
2269379bc100SJani Nikula 
2270379bc100SJani Nikula static void cnl_ddi_vswing_program(struct intel_encoder *encoder,
2271379bc100SJani Nikula 				   int level, enum intel_output_type type)
2272379bc100SJani Nikula {
2273379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2274379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations;
2275379bc100SJani Nikula 	enum port port = encoder->port;
2276379bc100SJani Nikula 	int n_entries, ln;
2277379bc100SJani Nikula 	u32 val;
2278379bc100SJani Nikula 
2279379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2280a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_hdmi(encoder, &n_entries);
2281379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2282a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_edp(encoder, &n_entries);
2283379bc100SJani Nikula 	else
2284a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_dp(encoder, &n_entries);
2285379bc100SJani Nikula 
22861de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2287379bc100SJani Nikula 		return;
22881de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2289379bc100SJani Nikula 		level = n_entries - 1;
2290379bc100SJani Nikula 
2291379bc100SJani Nikula 	/* Set PORT_TX_DW5 Scaling Mode Sel to 010b. */
2292f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2293379bc100SJani Nikula 	val &= ~SCALING_MODE_SEL_MASK;
2294379bc100SJani Nikula 	val |= SCALING_MODE_SEL(2);
2295f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2296379bc100SJani Nikula 
2297379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2298f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW2_LN0(port));
2299379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2300379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2301379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2302379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2303379bc100SJani Nikula 	/* Rcomp scalar is fixed as 0x98 for every table entry */
2304379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2305f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW2_GRP(port), val);
2306379bc100SJani Nikula 
2307379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2308379bc100SJani Nikula 	/* We cannot write to GRP. It would overrite individual loadgen */
2309379bc100SJani Nikula 	for (ln = 0; ln < 4; ln++) {
2310f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, CNL_PORT_TX_DW4_LN(ln, port));
2311379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2312379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2313379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2314379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2315379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2316f7960e7fSJani Nikula 		intel_de_write(dev_priv, CNL_PORT_TX_DW4_LN(ln, port), val);
2317379bc100SJani Nikula 	}
2318379bc100SJani Nikula 
2319379bc100SJani Nikula 	/* Program PORT_TX_DW5 */
2320379bc100SJani Nikula 	/* All DW5 values are fixed for every table entry */
2321f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2322379bc100SJani Nikula 	val &= ~RTERM_SELECT_MASK;
2323379bc100SJani Nikula 	val |= RTERM_SELECT(6);
2324379bc100SJani Nikula 	val |= TAP3_DISABLE;
2325f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2326379bc100SJani Nikula 
2327379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2328f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW7_LN0(port));
2329379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2330379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2331f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW7_GRP(port), val);
2332379bc100SJani Nikula }
2333379bc100SJani Nikula 
2334379bc100SJani Nikula static void cnl_ddi_vswing_sequence(struct intel_encoder *encoder,
2335379bc100SJani Nikula 				    int level, enum intel_output_type type)
2336379bc100SJani Nikula {
2337379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2338379bc100SJani Nikula 	enum port port = encoder->port;
2339379bc100SJani Nikula 	int width, rate, ln;
2340379bc100SJani Nikula 	u32 val;
2341379bc100SJani Nikula 
2342379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2343379bc100SJani Nikula 		width = 4;
2344379bc100SJani Nikula 		rate = 0; /* Rate is always < than 6GHz for HDMI */
2345379bc100SJani Nikula 	} else {
2346b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2347379bc100SJani Nikula 
2348379bc100SJani Nikula 		width = intel_dp->lane_count;
2349379bc100SJani Nikula 		rate = intel_dp->link_rate;
2350379bc100SJani Nikula 	}
2351379bc100SJani Nikula 
2352379bc100SJani Nikula 	/*
2353379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2354379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2355379bc100SJani Nikula 	 * else clear to 0b.
2356379bc100SJani Nikula 	 */
2357f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_PCS_DW1_LN0(port));
2358379bc100SJani Nikula 	if (type != INTEL_OUTPUT_HDMI)
2359379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2360379bc100SJani Nikula 	else
2361379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2362f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_PCS_DW1_GRP(port), val);
2363379bc100SJani Nikula 
2364379bc100SJani Nikula 	/* 2. Program loadgen select */
2365379bc100SJani Nikula 	/*
2366379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2367379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2368379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2369379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2370379bc100SJani Nikula 	 */
2371379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2372f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, CNL_PORT_TX_DW4_LN(ln, port));
2373379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2374379bc100SJani Nikula 
2375379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1)  ||
2376379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2377379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2378379bc100SJani Nikula 		}
2379f7960e7fSJani Nikula 		intel_de_write(dev_priv, CNL_PORT_TX_DW4_LN(ln, port), val);
2380379bc100SJani Nikula 	}
2381379bc100SJani Nikula 
2382379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2383f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_CL1CM_DW5);
2384379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2385f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_CL1CM_DW5, val);
2386379bc100SJani Nikula 
2387379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2388f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2389379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2390f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2391379bc100SJani Nikula 
2392379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2393379bc100SJani Nikula 	cnl_ddi_vswing_program(encoder, level, type);
2394379bc100SJani Nikula 
2395379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2396f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2397379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2398f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2399379bc100SJani Nikula }
2400379bc100SJani Nikula 
2401a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
2402f0e86e05SJosé Roberto de Souza 					 u32 level, int type, int rate)
2403379bc100SJani Nikula {
2404a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2405f0e86e05SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2406379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations = NULL;
2407379bc100SJani Nikula 	u32 n_entries, val;
2408379bc100SJani Nikula 	int ln;
2409379bc100SJani Nikula 
2410bd3cf6f7SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
2411a8143150SJosé Roberto de Souza 		ddi_translations = tgl_get_combo_buf_trans(encoder, type, rate,
2412bd3cf6f7SJosé Roberto de Souza 							   &n_entries);
2413b42d5a67SJosé Roberto de Souza 	else if (IS_ELKHARTLAKE(dev_priv))
2414a8143150SJosé Roberto de Souza 		ddi_translations = ehl_get_combo_buf_trans(encoder, type, rate,
2415b42d5a67SJosé Roberto de Souza 							   &n_entries);
2416bd3cf6f7SJosé Roberto de Souza 	else
2417a8143150SJosé Roberto de Souza 		ddi_translations = icl_get_combo_buf_trans(encoder, type, rate,
24184a8134d5SMatt Roper 							   &n_entries);
2419379bc100SJani Nikula 	if (!ddi_translations)
2420379bc100SJani Nikula 		return;
2421379bc100SJani Nikula 
2422379bc100SJani Nikula 	if (level >= n_entries) {
242347bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
242447bdb1caSJani Nikula 			    "DDI translation not found for level %d. Using %d instead.",
242547bdb1caSJani Nikula 			    level, n_entries - 1);
2426379bc100SJani Nikula 		level = n_entries - 1;
2427379bc100SJani Nikula 	}
2428379bc100SJani Nikula 
242981619f4aSJosé Roberto de Souza 	if (type == INTEL_OUTPUT_EDP) {
243081619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
243181619f4aSJosé Roberto de Souza 
243281619f4aSJosé Roberto de Souza 		val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
243381619f4aSJosé Roberto de Souza 		intel_dp->hobl_active = is_hobl_buf_trans(ddi_translations);
243481619f4aSJosé Roberto de Souza 		intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
243581619f4aSJosé Roberto de Souza 			     intel_dp->hobl_active ? val : 0);
243681619f4aSJosé Roberto de Souza 	}
243781619f4aSJosé Roberto de Souza 
2438379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
2439f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2440379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
2441379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
2442379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
2443379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
2444379bc100SJani Nikula 	val |= TAP3_DISABLE;
2445f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2446379bc100SJani Nikula 
2447379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2448f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN0(phy));
2449379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2450379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2451379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2452379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2453379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
2454379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2455f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), val);
2456379bc100SJani Nikula 
2457379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2458379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
2459379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2460f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
2461379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2462379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2463379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2464379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2465379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2466f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
2467379bc100SJani Nikula 	}
2468379bc100SJani Nikula 
2469379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2470f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN0(phy));
2471379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2472379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2473f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW7_GRP(phy), val);
2474379bc100SJani Nikula }
2475379bc100SJani Nikula 
2476379bc100SJani Nikula static void icl_combo_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2477379bc100SJani Nikula 					      u32 level,
2478379bc100SJani Nikula 					      enum intel_output_type type)
2479379bc100SJani Nikula {
2480379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2481dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2482379bc100SJani Nikula 	int width = 0;
2483379bc100SJani Nikula 	int rate = 0;
2484379bc100SJani Nikula 	u32 val;
2485379bc100SJani Nikula 	int ln = 0;
2486379bc100SJani Nikula 
2487379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2488379bc100SJani Nikula 		width = 4;
2489379bc100SJani Nikula 		/* Rate is always < than 6GHz for HDMI */
2490379bc100SJani Nikula 	} else {
2491b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2492379bc100SJani Nikula 
2493379bc100SJani Nikula 		width = intel_dp->lane_count;
2494379bc100SJani Nikula 		rate = intel_dp->link_rate;
2495379bc100SJani Nikula 	}
2496379bc100SJani Nikula 
2497379bc100SJani Nikula 	/*
2498379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2499379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2500379bc100SJani Nikula 	 * else clear to 0b.
2501379bc100SJani Nikula 	 */
2502f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN0(phy));
2503379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2504379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2505379bc100SJani Nikula 	else
2506379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2507f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
2508379bc100SJani Nikula 
2509379bc100SJani Nikula 	/* 2. Program loadgen select */
2510379bc100SJani Nikula 	/*
2511379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2512379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2513379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2514379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2515379bc100SJani Nikula 	 */
2516379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2517f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
2518379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2519379bc100SJani Nikula 
2520379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
2521379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2522379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2523379bc100SJani Nikula 		}
2524f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
2525379bc100SJani Nikula 	}
2526379bc100SJani Nikula 
2527379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2528f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
2529379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2530f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
2531379bc100SJani Nikula 
2532379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2533f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2534379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2535f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2536379bc100SJani Nikula 
2537379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2538f0e86e05SJosé Roberto de Souza 	icl_ddi_combo_vswing_program(encoder, level, type, rate);
2539379bc100SJani Nikula 
2540379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2541f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2542379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2543f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2544379bc100SJani Nikula }
2545379bc100SJani Nikula 
2546379bc100SJani Nikula static void icl_mg_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
25479f7ffa29SJosé Roberto de Souza 					   int link_clock, u32 level,
25489f7ffa29SJosé Roberto de Souza 					   enum intel_output_type type)
2549379bc100SJani Nikula {
2550379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2551f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
2552379bc100SJani Nikula 	const struct icl_mg_phy_ddi_buf_trans *ddi_translations;
2553379bc100SJani Nikula 	u32 n_entries, val;
25549f7ffa29SJosé Roberto de Souza 	int ln, rate = 0;
2555379bc100SJani Nikula 
25569f7ffa29SJosé Roberto de Souza 	if (type != INTEL_OUTPUT_HDMI) {
25579f7ffa29SJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
25589f7ffa29SJosé Roberto de Souza 
25599f7ffa29SJosé Roberto de Souza 		rate = intel_dp->link_rate;
25609f7ffa29SJosé Roberto de Souza 	}
25619f7ffa29SJosé Roberto de Souza 
2562a8143150SJosé Roberto de Souza 	ddi_translations = icl_get_mg_buf_trans(encoder, type, rate,
25639f7ffa29SJosé Roberto de Souza 						&n_entries);
2564379bc100SJani Nikula 	/* The table does not have values for level 3 and level 9. */
2565379bc100SJani Nikula 	if (level >= n_entries || level == 3 || level == 9) {
256647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
256747bdb1caSJani Nikula 			    "DDI translation not found for level %d. Using %d instead.",
2568379bc100SJani Nikula 			    level, n_entries - 2);
2569379bc100SJani Nikula 		level = n_entries - 2;
2570379bc100SJani Nikula 	}
2571379bc100SJani Nikula 
2572379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
2573379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2574f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
2575379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2576f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
2577379bc100SJani Nikula 
2578f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
2579379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2580f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
2581379bc100SJani Nikula 	}
2582379bc100SJani Nikula 
2583379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
2584379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2585f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
2586379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2587379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2588379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2589f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
2590379bc100SJani Nikula 
2591f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port));
2592379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2593379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2594379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2595f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port), val);
2596379bc100SJani Nikula 	}
2597379bc100SJani Nikula 
2598379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
2599379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2600f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
2601379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2602379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2603379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2604379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2605379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2606379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2607379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2608f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
2609379bc100SJani Nikula 
2610f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
2611379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2612379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2613379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2614379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2615379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2616379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2617379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2618f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
2619379bc100SJani Nikula 
2620379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
2621379bc100SJani Nikula 	}
2622379bc100SJani Nikula 
2623379bc100SJani Nikula 	/*
2624379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
2625379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
2626379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
2627379bc100SJani Nikula 	 */
2628379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2629f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
2630379bc100SJani Nikula 		if (link_clock < 300000)
2631379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
2632379bc100SJani Nikula 		else
2633379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
2634f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
2635379bc100SJani Nikula 	}
2636379bc100SJani Nikula 
2637379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
2638379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2639f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
2640379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2641379bc100SJani Nikula 		if (link_clock <= 500000) {
2642379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2643379bc100SJani Nikula 		} else {
2644379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2645379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2646379bc100SJani Nikula 		}
2647f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
2648379bc100SJani Nikula 
2649f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
2650379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2651379bc100SJani Nikula 		if (link_clock <= 500000) {
2652379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2653379bc100SJani Nikula 		} else {
2654379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2655379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2656379bc100SJani Nikula 		}
2657f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
2658379bc100SJani Nikula 	}
2659379bc100SJani Nikula 
2660379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
2661379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2662f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
2663f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
2664379bc100SJani Nikula 		val |= CRI_CALCINIT;
2665f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
2666f7960e7fSJani Nikula 			       val);
2667379bc100SJani Nikula 
2668f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
2669f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
2670379bc100SJani Nikula 		val |= CRI_CALCINIT;
2671f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
2672f7960e7fSJani Nikula 			       val);
2673379bc100SJani Nikula 	}
2674379bc100SJani Nikula }
2675379bc100SJani Nikula 
2676379bc100SJani Nikula static void icl_ddi_vswing_sequence(struct intel_encoder *encoder,
2677379bc100SJani Nikula 				    int link_clock,
2678379bc100SJani Nikula 				    u32 level,
2679379bc100SJani Nikula 				    enum intel_output_type type)
2680379bc100SJani Nikula {
2681379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2682d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2683379bc100SJani Nikula 
2684d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy))
2685379bc100SJani Nikula 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2686379bc100SJani Nikula 	else
26879f7ffa29SJosé Roberto de Souza 		icl_mg_phy_ddi_vswing_sequence(encoder, link_clock, level,
26889f7ffa29SJosé Roberto de Souza 					       type);
2689379bc100SJani Nikula }
2690379bc100SJani Nikula 
2691978c3e53SClinton A Taylor static void
2692978c3e53SClinton A Taylor tgl_dkl_phy_ddi_vswing_sequence(struct intel_encoder *encoder, int link_clock,
269394641eb6SVandita Kulkarni 				u32 level, enum intel_output_type type)
2694978c3e53SClinton A Taylor {
2695978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2696978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
2697978c3e53SClinton A Taylor 	const struct tgl_dkl_phy_ddi_buf_trans *ddi_translations;
2698978c3e53SClinton A Taylor 	u32 n_entries, val, ln, dpcnt_mask, dpcnt_val;
26999fa67699SJosé Roberto de Souza 	int rate = 0;
2700978c3e53SClinton A Taylor 
270194641eb6SVandita Kulkarni 	if (type == INTEL_OUTPUT_HDMI) {
27029fa67699SJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
27039fa67699SJosé Roberto de Souza 
27049fa67699SJosé Roberto de Souza 		rate = intel_dp->link_rate;
2705362bfb99SMatt Roper 	}
2706978c3e53SClinton A Taylor 
2707a8143150SJosé Roberto de Souza 	ddi_translations = tgl_get_dkl_buf_trans(encoder, encoder->type, rate,
27089fa67699SJosé Roberto de Souza 						 &n_entries);
27099fa67699SJosé Roberto de Souza 
2710978c3e53SClinton A Taylor 	if (level >= n_entries)
2711978c3e53SClinton A Taylor 		level = n_entries - 1;
2712978c3e53SClinton A Taylor 
2713978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
2714978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
2715978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
2716978c3e53SClinton A Taylor 	dpcnt_val = DKL_TX_VSWING_CONTROL(ddi_translations[level].dkl_vswing_control);
2717978c3e53SClinton A Taylor 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(ddi_translations[level].dkl_de_emphasis_control);
2718978c3e53SClinton A Taylor 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(ddi_translations[level].dkl_preshoot_control);
2719978c3e53SClinton A Taylor 
2720978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
2721f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2722f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
2723978c3e53SClinton A Taylor 
2724f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
27252d69c42eSJosé Roberto de Souza 
2726978c3e53SClinton A Taylor 		/* All the registers are RMW */
2727f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
2728978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
2729978c3e53SClinton A Taylor 		val |= dpcnt_val;
2730f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
2731978c3e53SClinton A Taylor 
2732f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
2733978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
2734978c3e53SClinton A Taylor 		val |= dpcnt_val;
2735f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
2736978c3e53SClinton A Taylor 
2737f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
2738978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
2739f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
2740978c3e53SClinton A Taylor 	}
2741978c3e53SClinton A Taylor }
2742978c3e53SClinton A Taylor 
2743978c3e53SClinton A Taylor static void tgl_ddi_vswing_sequence(struct intel_encoder *encoder,
2744978c3e53SClinton A Taylor 				    int link_clock,
2745978c3e53SClinton A Taylor 				    u32 level,
2746978c3e53SClinton A Taylor 				    enum intel_output_type type)
2747978c3e53SClinton A Taylor {
2748978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2749978c3e53SClinton A Taylor 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2750978c3e53SClinton A Taylor 
2751978c3e53SClinton A Taylor 	if (intel_phy_is_combo(dev_priv, phy))
2752978c3e53SClinton A Taylor 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2753978c3e53SClinton A Taylor 	else
275494641eb6SVandita Kulkarni 		tgl_dkl_phy_ddi_vswing_sequence(encoder, link_clock, level, type);
2755978c3e53SClinton A Taylor }
2756978c3e53SClinton A Taylor 
27578b4f2137SPankaj Bharadiya static u32 translate_signal_level(struct intel_dp *intel_dp, int signal_levels)
2758379bc100SJani Nikula {
27598b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2760379bc100SJani Nikula 	int i;
2761379bc100SJani Nikula 
2762379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
2763379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
2764379bc100SJani Nikula 			return i;
2765379bc100SJani Nikula 	}
2766379bc100SJani Nikula 
27678b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
27688b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
2769379bc100SJani Nikula 		 signal_levels);
2770379bc100SJani Nikula 
2771379bc100SJani Nikula 	return 0;
2772379bc100SJani Nikula }
2773379bc100SJani Nikula 
2774379bc100SJani Nikula static u32 intel_ddi_dp_level(struct intel_dp *intel_dp)
2775379bc100SJani Nikula {
2776379bc100SJani Nikula 	u8 train_set = intel_dp->train_set[0];
2777379bc100SJani Nikula 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2778379bc100SJani Nikula 					 DP_TRAIN_PRE_EMPHASIS_MASK);
2779379bc100SJani Nikula 
27808b4f2137SPankaj Bharadiya 	return translate_signal_level(intel_dp, signal_levels);
2781379bc100SJani Nikula }
2782379bc100SJani Nikula 
2783fb83f72cSVille Syrjälä static void
2784fb83f72cSVille Syrjälä tgl_set_signal_levels(struct intel_dp *intel_dp)
2785379bc100SJani Nikula {
2786fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2787379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2788379bc100SJani Nikula 
2789978c3e53SClinton A Taylor 	tgl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2790978c3e53SClinton A Taylor 				level, encoder->type);
2791379bc100SJani Nikula }
2792379bc100SJani Nikula 
2793fb83f72cSVille Syrjälä static void
2794fb83f72cSVille Syrjälä icl_set_signal_levels(struct intel_dp *intel_dp)
2795379bc100SJani Nikula {
2796fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2797379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2798379bc100SJani Nikula 
2799fb83f72cSVille Syrjälä 	icl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2800fb83f72cSVille Syrjälä 				level, encoder->type);
2801fb83f72cSVille Syrjälä }
2802fb83f72cSVille Syrjälä 
2803fb83f72cSVille Syrjälä static void
2804fb83f72cSVille Syrjälä cnl_set_signal_levels(struct intel_dp *intel_dp)
2805fb83f72cSVille Syrjälä {
2806fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2807fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2808fb83f72cSVille Syrjälä 
2809fb83f72cSVille Syrjälä 	cnl_ddi_vswing_sequence(encoder, level, encoder->type);
2810fb83f72cSVille Syrjälä }
2811fb83f72cSVille Syrjälä 
2812fb83f72cSVille Syrjälä static void
2813fb83f72cSVille Syrjälä bxt_set_signal_levels(struct intel_dp *intel_dp)
2814fb83f72cSVille Syrjälä {
2815fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2816fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2817fb83f72cSVille Syrjälä 
2818fb83f72cSVille Syrjälä 	bxt_ddi_vswing_sequence(encoder, level, encoder->type);
2819fb83f72cSVille Syrjälä }
2820fb83f72cSVille Syrjälä 
2821fb83f72cSVille Syrjälä static void
2822fb83f72cSVille Syrjälä hsw_set_signal_levels(struct intel_dp *intel_dp)
2823fb83f72cSVille Syrjälä {
2824fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2825fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2826fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2827fb83f72cSVille Syrjälä 	enum port port = encoder->port;
2828fb83f72cSVille Syrjälä 	u32 signal_levels;
2829fb83f72cSVille Syrjälä 
2830fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
2831fb83f72cSVille Syrjälä 
2832fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
2833fb83f72cSVille Syrjälä 		    signal_levels);
2834fb83f72cSVille Syrjälä 
2835fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
2836fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
2837fb83f72cSVille Syrjälä 
2838379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
2839379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, encoder->type);
2840379bc100SJani Nikula 
2841fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
2842fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
2843379bc100SJani Nikula }
2844379bc100SJani Nikula 
284581b55ef1SJani Nikula static u32 icl_dpclka_cfgcr0_clk_off(struct drm_i915_private *dev_priv,
2846befa372bSMatt Roper 				     enum phy phy)
2847379bc100SJani Nikula {
2848*cd803bb4SMatt Roper 	if (IS_ROCKETLAKE(dev_priv)) {
2849*cd803bb4SMatt Roper 		return RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
2850*cd803bb4SMatt Roper 	} else if (intel_phy_is_combo(dev_priv, phy)) {
2851befa372bSMatt Roper 		return ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
2852befa372bSMatt Roper 	} else if (intel_phy_is_tc(dev_priv, phy)) {
2853befa372bSMatt Roper 		enum tc_port tc_port = intel_port_to_tc(dev_priv,
2854befa372bSMatt Roper 							(enum port)phy);
2855379bc100SJani Nikula 
2856379bc100SJani Nikula 		return ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port);
2857379bc100SJani Nikula 	}
2858379bc100SJani Nikula 
2859379bc100SJani Nikula 	return 0;
2860379bc100SJani Nikula }
2861379bc100SJani Nikula 
2862379bc100SJani Nikula static void icl_map_plls_to_ports(struct intel_encoder *encoder,
2863379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2864379bc100SJani Nikula {
2865379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2866379bc100SJani Nikula 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2867befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2868379bc100SJani Nikula 	u32 val;
2869379bc100SJani Nikula 
2870353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
2871379bc100SJani Nikula 
2872f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
28731de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
28741de143ccSPankaj Bharadiya 		    (val & icl_dpclka_cfgcr0_clk_off(dev_priv, phy)) == 0);
2875379bc100SJani Nikula 
2876befa372bSMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
2877*cd803bb4SMatt Roper 		u32 mask, sel;
2878*cd803bb4SMatt Roper 
2879*cd803bb4SMatt Roper 		if (IS_ROCKETLAKE(dev_priv)) {
2880*cd803bb4SMatt Roper 			mask = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
2881*cd803bb4SMatt Roper 			sel = RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
2882*cd803bb4SMatt Roper 		} else {
2883*cd803bb4SMatt Roper 			mask = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
2884*cd803bb4SMatt Roper 			sel = ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
2885*cd803bb4SMatt Roper 		}
2886*cd803bb4SMatt Roper 
2887befa372bSMatt Roper 		/*
2888befa372bSMatt Roper 		 * Even though this register references DDIs, note that we
2889befa372bSMatt Roper 		 * want to pass the PHY rather than the port (DDI).  For
2890befa372bSMatt Roper 		 * ICL, port=phy in all cases so it doesn't matter, but for
2891befa372bSMatt Roper 		 * EHL the bspec notes the following:
2892befa372bSMatt Roper 		 *
2893befa372bSMatt Roper 		 *   "DDID clock tied to DDIA clock, so DPCLKA_CFGCR0 DDIA
2894befa372bSMatt Roper 		 *   Clock Select chooses the PLL for both DDIA and DDID and
2895befa372bSMatt Roper 		 *   drives port A in all cases."
2896befa372bSMatt Roper 		 */
2897*cd803bb4SMatt Roper 		val &= ~mask;
2898*cd803bb4SMatt Roper 		val |= sel;
2899f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2900f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, ICL_DPCLKA_CFGCR0);
2901379bc100SJani Nikula 	}
2902379bc100SJani Nikula 
2903befa372bSMatt Roper 	val &= ~icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2904f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2905379bc100SJani Nikula 
2906353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
2907379bc100SJani Nikula }
2908379bc100SJani Nikula 
2909379bc100SJani Nikula static void icl_unmap_plls_to_ports(struct intel_encoder *encoder)
2910379bc100SJani Nikula {
2911379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2912befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2913379bc100SJani Nikula 	u32 val;
2914379bc100SJani Nikula 
2915353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
2916379bc100SJani Nikula 
2917f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
2918befa372bSMatt Roper 	val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2919f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2920379bc100SJani Nikula 
2921353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
2922379bc100SJani Nikula }
2923379bc100SJani Nikula 
29245956f440SLucas De Marchi static void icl_sanitize_port_clk_off(struct drm_i915_private *dev_priv,
29255956f440SLucas De Marchi 				      u32 port_mask, bool ddi_clk_needed)
29265956f440SLucas De Marchi {
29275956f440SLucas De Marchi 	enum port port;
29285956f440SLucas De Marchi 	u32 val;
29295956f440SLucas De Marchi 
2930f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
29315956f440SLucas De Marchi 	for_each_port_masked(port, port_mask) {
29325956f440SLucas De Marchi 		enum phy phy = intel_port_to_phy(dev_priv, port);
293341ba19fcSLucas De Marchi 		bool ddi_clk_off = val & icl_dpclka_cfgcr0_clk_off(dev_priv,
293441ba19fcSLucas De Marchi 								   phy);
29355956f440SLucas De Marchi 
293641ba19fcSLucas De Marchi 		if (ddi_clk_needed == !ddi_clk_off)
29375956f440SLucas De Marchi 			continue;
29385956f440SLucas De Marchi 
29395956f440SLucas De Marchi 		/*
29405956f440SLucas De Marchi 		 * Punt on the case now where clock is gated, but it would
29415956f440SLucas De Marchi 		 * be needed by the port. Something else is really broken then.
29425956f440SLucas De Marchi 		 */
29431de143ccSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm, ddi_clk_needed))
29445956f440SLucas De Marchi 			continue;
29455956f440SLucas De Marchi 
294647bdb1caSJani Nikula 		drm_notice(&dev_priv->drm,
294747bdb1caSJani Nikula 			   "PHY %c is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2948d6f970f0SLucas De Marchi 			   phy_name(phy));
29495956f440SLucas De Marchi 		val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2950f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
29515956f440SLucas De Marchi 	}
29525956f440SLucas De Marchi }
29535956f440SLucas De Marchi 
2954379bc100SJani Nikula void icl_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
2955379bc100SJani Nikula {
2956379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2957379bc100SJani Nikula 	u32 port_mask;
2958379bc100SJani Nikula 	bool ddi_clk_needed;
2959379bc100SJani Nikula 
2960379bc100SJani Nikula 	/*
2961379bc100SJani Nikula 	 * In case of DP MST, we sanitize the primary encoder only, not the
2962379bc100SJani Nikula 	 * virtual ones.
2963379bc100SJani Nikula 	 */
2964379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DP_MST)
2965379bc100SJani Nikula 		return;
2966379bc100SJani Nikula 
2967379bc100SJani Nikula 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2968379bc100SJani Nikula 		u8 pipe_mask;
2969379bc100SJani Nikula 		bool is_mst;
2970379bc100SJani Nikula 
2971379bc100SJani Nikula 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2972379bc100SJani Nikula 		/*
2973379bc100SJani Nikula 		 * In the unlikely case that BIOS enables DP in MST mode, just
2974379bc100SJani Nikula 		 * warn since our MST HW readout is incomplete.
2975379bc100SJani Nikula 		 */
29761de143ccSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm, is_mst))
2977379bc100SJani Nikula 			return;
2978379bc100SJani Nikula 	}
2979379bc100SJani Nikula 
2980379bc100SJani Nikula 	port_mask = BIT(encoder->port);
2981379bc100SJani Nikula 	ddi_clk_needed = encoder->base.crtc;
2982379bc100SJani Nikula 
2983379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DSI) {
2984379bc100SJani Nikula 		struct intel_encoder *other_encoder;
2985379bc100SJani Nikula 
2986379bc100SJani Nikula 		port_mask = intel_dsi_encoder_ports(encoder);
2987379bc100SJani Nikula 		/*
2988379bc100SJani Nikula 		 * Sanity check that we haven't incorrectly registered another
2989379bc100SJani Nikula 		 * encoder using any of the ports of this DSI encoder.
2990379bc100SJani Nikula 		 */
2991379bc100SJani Nikula 		for_each_intel_encoder(&dev_priv->drm, other_encoder) {
2992379bc100SJani Nikula 			if (other_encoder == encoder)
2993379bc100SJani Nikula 				continue;
2994379bc100SJani Nikula 
29951de143ccSPankaj Bharadiya 			if (drm_WARN_ON(&dev_priv->drm,
29961de143ccSPankaj Bharadiya 					port_mask & BIT(other_encoder->port)))
2997379bc100SJani Nikula 				return;
2998379bc100SJani Nikula 		}
2999379bc100SJani Nikula 		/*
3000379bc100SJani Nikula 		 * For DSI we keep the ddi clocks gated
3001379bc100SJani Nikula 		 * except during enable/disable sequence.
3002379bc100SJani Nikula 		 */
3003379bc100SJani Nikula 		ddi_clk_needed = false;
3004379bc100SJani Nikula 	}
3005379bc100SJani Nikula 
30065956f440SLucas De Marchi 	icl_sanitize_port_clk_off(dev_priv, port_mask, ddi_clk_needed);
3007379bc100SJani Nikula }
3008379bc100SJani Nikula 
3009379bc100SJani Nikula static void intel_ddi_clk_select(struct intel_encoder *encoder,
3010379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
3011379bc100SJani Nikula {
3012379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3013379bc100SJani Nikula 	enum port port = encoder->port;
3014d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3015379bc100SJani Nikula 	u32 val;
3016379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
3017379bc100SJani Nikula 
30181de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !pll))
3019379bc100SJani Nikula 		return;
3020379bc100SJani Nikula 
3021353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
3022379bc100SJani Nikula 
3023379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
3024d8fe2ab6SMatt Roper 		if (!intel_phy_is_combo(dev_priv, phy))
3025f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
3026379bc100SJani Nikula 				       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
3027c2052d6eSJosé Roberto de Souza 		else if (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C)
3028c2052d6eSJosé Roberto de Souza 			/*
3029c2052d6eSJosé Roberto de Souza 			 * MG does not exist but the programming is required
3030c2052d6eSJosé Roberto de Souza 			 * to ungate DDIC and DDID
3031c2052d6eSJosé Roberto de Souza 			 */
3032f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
3033f7960e7fSJani Nikula 				       DDI_CLK_SEL_MG);
3034379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
3035379bc100SJani Nikula 		/* Configure DPCLKA_CFGCR0 to map the DPLL to the DDI. */
3036f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPCLKA_CFGCR0);
3037379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
3038379bc100SJani Nikula 		val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, port);
3039f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0, val);
3040379bc100SJani Nikula 
3041379bc100SJani Nikula 		/*
3042379bc100SJani Nikula 		 * Configure DPCLKA_CFGCR0 to turn on the clock for the DDI.
3043379bc100SJani Nikula 		 * This step and the step before must be done with separate
3044379bc100SJani Nikula 		 * register writes.
3045379bc100SJani Nikula 		 */
3046f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPCLKA_CFGCR0);
3047379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_OFF(port);
3048f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0, val);
3049379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3050379bc100SJani Nikula 		/* DDI -> PLL mapping  */
3051f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPLL_CTRL2);
3052379bc100SJani Nikula 
3053379bc100SJani Nikula 		val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
3054379bc100SJani Nikula 			 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
3055379bc100SJani Nikula 		val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
3056379bc100SJani Nikula 			DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
3057379bc100SJani Nikula 
3058f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPLL_CTRL2, val);
3059379bc100SJani Nikula 
3060379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3061f7960e7fSJani Nikula 		intel_de_write(dev_priv, PORT_CLK_SEL(port),
3062f7960e7fSJani Nikula 			       hsw_pll_to_ddi_pll_sel(pll));
3063379bc100SJani Nikula 	}
3064379bc100SJani Nikula 
3065353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
3066379bc100SJani Nikula }
3067379bc100SJani Nikula 
3068379bc100SJani Nikula static void intel_ddi_clk_disable(struct intel_encoder *encoder)
3069379bc100SJani Nikula {
3070379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3071379bc100SJani Nikula 	enum port port = encoder->port;
3072d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3073379bc100SJani Nikula 
3074379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
3075c2052d6eSJosé Roberto de Souza 		if (!intel_phy_is_combo(dev_priv, phy) ||
3076c2052d6eSJosé Roberto de Souza 		    (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C))
3077f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
3078f7960e7fSJani Nikula 				       DDI_CLK_SEL_NONE);
3079379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
3080f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0,
3081f7960e7fSJani Nikula 			       intel_de_read(dev_priv, DPCLKA_CFGCR0) | DPCLKA_CFGCR0_DDI_CLK_OFF(port));
3082379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3083f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPLL_CTRL2,
3084f7960e7fSJani Nikula 			       intel_de_read(dev_priv, DPLL_CTRL2) | DPLL_CTRL2_DDI_CLK_OFF(port));
3085379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3086f7960e7fSJani Nikula 		intel_de_write(dev_priv, PORT_CLK_SEL(port),
3087f7960e7fSJani Nikula 			       PORT_CLK_SEL_NONE);
3088379bc100SJani Nikula 	}
3089379bc100SJani Nikula }
3090379bc100SJani Nikula 
30918aaf5cbdSJosé Roberto de Souza static void
30927801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
30933b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
3094379bc100SJani Nikula {
30957801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
30967801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
30973b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
30983b51be4eSClinton A Taylor 	u8 width;
3099379bc100SJani Nikula 
31007801f3b7SLucas De Marchi 	if (dig_port->tc_mode == TC_PORT_TBT_ALT)
3101379bc100SJani Nikula 		return;
3102379bc100SJani Nikula 
3103978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
3104f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3105f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
3106f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
3107f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3108f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
3109f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
3110978c3e53SClinton A Taylor 	} else {
3111f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
3112f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
3113978c3e53SClinton A Taylor 	}
3114379bc100SJani Nikula 
31154f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3116379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3117379bc100SJani Nikula 
31183b51be4eSClinton A Taylor 	/* DPPATC */
31197801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
31203b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
3121379bc100SJani Nikula 
31223b51be4eSClinton A Taylor 	switch (pin_assignment) {
31233b51be4eSClinton A Taylor 	case 0x0:
31241de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
31257801f3b7SLucas De Marchi 			    dig_port->tc_mode != TC_PORT_LEGACY);
31263b51be4eSClinton A Taylor 		if (width == 1) {
3127379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
31283b51be4eSClinton A Taylor 		} else {
31293b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31303b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3131379bc100SJani Nikula 		}
3132379bc100SJani Nikula 		break;
31333b51be4eSClinton A Taylor 	case 0x1:
31343b51be4eSClinton A Taylor 		if (width == 4) {
31353b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31363b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31373b51be4eSClinton A Taylor 		}
3138379bc100SJani Nikula 		break;
31393b51be4eSClinton A Taylor 	case 0x2:
31403b51be4eSClinton A Taylor 		if (width == 2) {
31413b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31423b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31433b51be4eSClinton A Taylor 		}
31443b51be4eSClinton A Taylor 		break;
31453b51be4eSClinton A Taylor 	case 0x3:
31463b51be4eSClinton A Taylor 	case 0x5:
31473b51be4eSClinton A Taylor 		if (width == 1) {
31483b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
31493b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
31503b51be4eSClinton A Taylor 		} else {
31513b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31523b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31533b51be4eSClinton A Taylor 		}
31543b51be4eSClinton A Taylor 		break;
31553b51be4eSClinton A Taylor 	case 0x4:
31563b51be4eSClinton A Taylor 	case 0x6:
31573b51be4eSClinton A Taylor 		if (width == 1) {
31583b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
31593b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
31603b51be4eSClinton A Taylor 		} else {
31613b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31623b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31633b51be4eSClinton A Taylor 		}
31643b51be4eSClinton A Taylor 		break;
3165379bc100SJani Nikula 	default:
31663b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
3167379bc100SJani Nikula 	}
3168379bc100SJani Nikula 
3169978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
3170f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3171f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
3172f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
3173f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3174f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
3175f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
3176978c3e53SClinton A Taylor 	} else {
3177f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
3178f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
3179379bc100SJani Nikula 	}
3180978c3e53SClinton A Taylor }
3181379bc100SJani Nikula 
3182379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
3183379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3184379bc100SJani Nikula {
318547bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
318647bdb1caSJani Nikula 
3187379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3188379bc100SJani Nikula 		return;
3189379bc100SJani Nikula 
3190379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
319147bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
319247bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
3193379bc100SJani Nikula }
3194379bc100SJani Nikula 
3195379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
3196379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
3197379bc100SJani Nikula {
3198379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
31994444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3200379bc100SJani Nikula 	u32 val;
3201379bc100SJani Nikula 
3202379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3203379bc100SJani Nikula 		return;
3204379bc100SJani Nikula 
3205b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
3206f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3207379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
3208f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3209379bc100SJani Nikula 
32104444df6eSLucas De Marchi 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
32114cb3b44dSDaniele Ceraolo Spurio 				  DP_TP_STATUS_FEC_ENABLE_LIVE, 1))
321247bdb1caSJani Nikula 		drm_err(&dev_priv->drm,
321347bdb1caSJani Nikula 			"Timed out waiting for FEC Enable Status\n");
3214379bc100SJani Nikula }
3215379bc100SJani Nikula 
3216379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
3217379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3218379bc100SJani Nikula {
3219379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
32204444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3221379bc100SJani Nikula 	u32 val;
3222379bc100SJani Nikula 
3223379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3224379bc100SJani Nikula 		return;
3225379bc100SJani Nikula 
3226b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
3227f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3228379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
3229f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3230f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3231379bc100SJani Nikula }
3232379bc100SJani Nikula 
3233ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
3234ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
323599389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
323699389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
323799389390SJosé Roberto de Souza {
3238b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
323999389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
324099389390SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3241b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
324299389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
324399389390SJosé Roberto de Souza 	int level = intel_ddi_dp_level(intel_dp);
32444444df6eSLucas De Marchi 	enum transcoder transcoder = crtc_state->cpu_transcoder;
324599389390SJosé Roberto de Souza 
324699389390SJosé Roberto de Souza 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
324799389390SJosé Roberto de Souza 				 crtc_state->lane_count, is_mst);
324899389390SJosé Roberto de Souza 
32494444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_ctl = TGL_DP_TP_CTL(transcoder);
32504444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_status = TGL_DP_TP_STATUS(transcoder);
32514444df6eSLucas De Marchi 
32525e19c0b0SMatt Roper 	/*
32535e19c0b0SMatt Roper 	 * 1. Enable Power Wells
32545e19c0b0SMatt Roper 	 *
32555e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
32565e19c0b0SMatt Roper 	 * before we called down into this function.
32575e19c0b0SMatt Roper 	 */
325899389390SJosé Roberto de Souza 
32595e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
326099389390SJosé Roberto de Souza 	intel_edp_panel_on(intel_dp);
326199389390SJosé Roberto de Souza 
326299389390SJosé Roberto de Souza 	/*
32635e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
32645e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
32655e19c0b0SMatt Roper 	 *
32665e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
32671e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
326899389390SJosé Roberto de Souza 	 */
326999389390SJosé Roberto de Souza 
32705e19c0b0SMatt Roper 	/*
32715e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
32725e19c0b0SMatt Roper 	 *
32735e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
32741e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
32755e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
32765e19c0b0SMatt Roper 	 */
32776171e58bSClinton A Taylor 	intel_ddi_clk_select(encoder, crtc_state);
32786171e58bSClinton A Taylor 
32795e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
328099389390SJosé Roberto de Souza 	if (!intel_phy_is_tc(dev_priv, phy) ||
328199389390SJosé Roberto de Souza 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
328299389390SJosé Roberto de Souza 		intel_display_power_get(dev_priv,
328399389390SJosé Roberto de Souza 					dig_port->ddi_io_power_domain);
328499389390SJosé Roberto de Souza 
32855e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
32863b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
328799389390SJosé Roberto de Souza 
328899389390SJosé Roberto de Souza 	/*
32895e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
32905e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
32915e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
32925e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
32935e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
32945e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
32955e19c0b0SMatt Roper 	 * unconditionally here.
32965e19c0b0SMatt Roper 	 */
32975e19c0b0SMatt Roper 
32985e19c0b0SMatt Roper 	/*
32995e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
33005e19c0b0SMatt Roper 	 * Transcoder.
330199389390SJosé Roberto de Souza 	 */
330202a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
330399389390SJosé Roberto de Souza 
33045e19c0b0SMatt Roper 	/*
33055e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
33065e19c0b0SMatt Roper 	 * Transport Select
33075e19c0b0SMatt Roper 	 */
3308eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
330999389390SJosé Roberto de Souza 
33105e19c0b0SMatt Roper 	/*
33115e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
33125e19c0b0SMatt Roper 	 * selected
33135e19c0b0SMatt Roper 	 *
33145e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
33155e19c0b0SMatt Roper 	 * down this function.
33165e19c0b0SMatt Roper 	 */
33175e19c0b0SMatt Roper 
33185e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
3319978c3e53SClinton A Taylor 	tgl_ddi_vswing_sequence(encoder, crtc_state->port_clock, level,
332099389390SJosé Roberto de Souza 				encoder->type);
332199389390SJosé Roberto de Souza 
33225e19c0b0SMatt Roper 	/*
33235e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
33245e19c0b0SMatt Roper 	 * the used lanes of the DDI.
33255e19c0b0SMatt Roper 	 */
332699389390SJosé Roberto de Souza 	if (intel_phy_is_combo(dev_priv, phy)) {
332799389390SJosé Roberto de Souza 		bool lane_reversal =
332899389390SJosé Roberto de Souza 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
332999389390SJosé Roberto de Souza 
333099389390SJosé Roberto de Souza 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
333199389390SJosé Roberto de Souza 					       crtc_state->lane_count,
333299389390SJosé Roberto de Souza 					       lane_reversal);
333399389390SJosé Roberto de Souza 	}
333499389390SJosé Roberto de Souza 
33355e19c0b0SMatt Roper 	/*
33365e19c0b0SMatt Roper 	 * 7.g Configure and enable DDI_BUF_CTL
33375e19c0b0SMatt Roper 	 * 7.h Wait for DDI_BUF_CTL DDI Idle Status = 0b (Not Idle), timeout
33385e19c0b0SMatt Roper 	 *     after 500 us.
33395e19c0b0SMatt Roper 	 *
33405e19c0b0SMatt Roper 	 * We only configure what the register value will be here.  Actual
33415e19c0b0SMatt Roper 	 * enabling happens during link training farther down.
33425e19c0b0SMatt Roper 	 */
334399389390SJosé Roberto de Souza 	intel_ddi_init_dp_buf_reg(encoder);
334499389390SJosé Roberto de Souza 
334599389390SJosé Roberto de Souza 	if (!is_mst)
334699389390SJosé Roberto de Souza 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
334799389390SJosé Roberto de Souza 
334899389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
334999389390SJosé Roberto de Souza 	/*
335099389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
335199389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
335299389390SJosé Roberto de Souza 	 * training
335399389390SJosé Roberto de Souza 	 */
335499389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
33555e19c0b0SMatt Roper 
33565e19c0b0SMatt Roper 	/*
33575e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
33585e19c0b0SMatt Roper 	 *     failure handling)
33595e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
33605e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
33615e19c0b0SMatt Roper 	 *     (timeout after 800 us)
33625e19c0b0SMatt Roper 	 */
336399389390SJosé Roberto de Souza 	intel_dp_start_link_train(intel_dp);
336499389390SJosé Roberto de Souza 
33655e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
3366eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
336799389390SJosé Roberto de Souza 		intel_dp_stop_link_train(intel_dp);
336899389390SJosé Roberto de Souza 
33695e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
337099389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
337199389390SJosé Roberto de Souza 	intel_dsc_enable(encoder, crtc_state);
337299389390SJosé Roberto de Souza }
337399389390SJosé Roberto de Souza 
3374ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
3375ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3376379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3377379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3378379bc100SJani Nikula {
3379b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3380379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3381379bc100SJani Nikula 	enum port port = encoder->port;
3382dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3383b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3384379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3385379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
3386379bc100SJani Nikula 
3387542dfab5SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) < 11)
33881de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
33891de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
3390542dfab5SJosé Roberto de Souza 	else
33911de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
3392379bc100SJani Nikula 
3393379bc100SJani Nikula 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
3394379bc100SJani Nikula 				 crtc_state->lane_count, is_mst);
3395379bc100SJani Nikula 
3396379bc100SJani Nikula 	intel_edp_panel_on(intel_dp);
3397379bc100SJani Nikula 
3398379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3399379bc100SJani Nikula 
3400d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
34013b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
34023b2ed431SImre Deak 		intel_display_power_get(dev_priv,
34033b2ed431SImre Deak 					dig_port->ddi_io_power_domain);
3404379bc100SJani Nikula 
34053b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
3406379bc100SJani Nikula 
3407379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3408379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3409379bc100SJani Nikula 					level, encoder->type);
3410379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3411379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
3412379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3413379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
3414379bc100SJani Nikula 	else
3415379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
3416379bc100SJani Nikula 
3417d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
3418379bc100SJani Nikula 		bool lane_reversal =
3419379bc100SJani Nikula 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
3420379bc100SJani Nikula 
3421dc867bc7SMatt Roper 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
3422379bc100SJani Nikula 					       crtc_state->lane_count,
3423379bc100SJani Nikula 					       lane_reversal);
3424379bc100SJani Nikula 	}
3425379bc100SJani Nikula 
3426379bc100SJani Nikula 	intel_ddi_init_dp_buf_reg(encoder);
3427379bc100SJani Nikula 	if (!is_mst)
3428379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
3429379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
3430379bc100SJani Nikula 					      true);
3431379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
3432379bc100SJani Nikula 	intel_dp_start_link_train(intel_dp);
3433eadf6f91SManasi Navare 	if ((port != PORT_A || INTEL_GEN(dev_priv) >= 9) &&
3434eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
3435379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3436379bc100SJani Nikula 
3437379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
3438379bc100SJani Nikula 
3439379bc100SJani Nikula 	if (!is_mst)
344002a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
3441379bc100SJani Nikula 
3442379bc100SJani Nikula 	intel_dsc_enable(encoder, crtc_state);
3443379bc100SJani Nikula }
3444379bc100SJani Nikula 
3445ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
3446ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
344799389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
344899389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
344999389390SJosé Roberto de Souza {
345099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
345199389390SJosé Roberto de Souza 
345299389390SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
3453ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
345499389390SJosé Roberto de Souza 	else
3455ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
34560c06fa15SGwan-gyeong Mun 
3457bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
3458bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
3459bd8c9ccaSGwan-gyeong Mun 	 */
34601fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
34610c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
34621c9d2eb2SJani Nikula 
34631c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
346499389390SJosé Roberto de Souza 	}
34651fc1e8d4SJosé Roberto de Souza }
346699389390SJosé Roberto de Souza 
3467ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
3468ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
3469379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
3470379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
3471379bc100SJani Nikula {
34720ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
34730ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3474379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
34750aed3bdeSJani Nikula 	int level = intel_ddi_hdmi_level(encoder);
3476379bc100SJani Nikula 
3477379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
3478379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3479379bc100SJani Nikula 
3480379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
3481379bc100SJani Nikula 
34823b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
3483379bc100SJani Nikula 
3484978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12)
3485978c3e53SClinton A Taylor 		tgl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3486978c3e53SClinton A Taylor 					level, INTEL_OUTPUT_HDMI);
3487978c3e53SClinton A Taylor 	else if (INTEL_GEN(dev_priv) == 11)
3488379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3489379bc100SJani Nikula 					level, INTEL_OUTPUT_HDMI);
3490379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3491379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3492379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3493379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3494379bc100SJani Nikula 	else
3495379bc100SJani Nikula 		intel_prepare_hdmi_ddi_buffers(encoder, level);
3496379bc100SJani Nikula 
3497379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
3498379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, INTEL_OUTPUT_HDMI);
3499379bc100SJani Nikula 
350002a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
3501379bc100SJani Nikula 
35020ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
3503379bc100SJani Nikula 				 crtc_state->has_infoframe,
3504379bc100SJani Nikula 				 crtc_state, conn_state);
3505379bc100SJani Nikula }
3506379bc100SJani Nikula 
3507ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
3508ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3509379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
3510379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
3511379bc100SJani Nikula {
35122225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3513379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3514379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
3515379bc100SJani Nikula 
3516379bc100SJani Nikula 	/*
3517379bc100SJani Nikula 	 * When called from DP MST code:
3518379bc100SJani Nikula 	 * - conn_state will be NULL
3519379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3520379bc100SJani Nikula 	 * - the main connector associated with this port
3521379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3522379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
3523379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
3524379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
3525379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3526379bc100SJani Nikula 	 *   the DP link parameteres
3527379bc100SJani Nikula 	 */
3528379bc100SJani Nikula 
35291de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
3530379bc100SJani Nikula 
3531379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3532379bc100SJani Nikula 		icl_map_plls_to_ports(encoder, crtc_state);
3533379bc100SJani Nikula 
3534379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3535379bc100SJani Nikula 
3536379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
3537ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
3538ede9771dSVille Syrjälä 					  conn_state);
3539379bc100SJani Nikula 	} else {
3540379bc100SJani Nikula 		struct intel_lspcon *lspcon =
3541b7d02c3aSVille Syrjälä 				enc_to_intel_lspcon(encoder);
3542379bc100SJani Nikula 
3543ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
3544ede9771dSVille Syrjälä 					conn_state);
3545379bc100SJani Nikula 		if (lspcon->active) {
3546379bc100SJani Nikula 			struct intel_digital_port *dig_port =
3547b7d02c3aSVille Syrjälä 					enc_to_dig_port(encoder);
3548379bc100SJani Nikula 
3549379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
3550379bc100SJani Nikula 						 crtc_state->has_infoframe,
3551379bc100SJani Nikula 						 crtc_state, conn_state);
3552379bc100SJani Nikula 		}
3553379bc100SJani Nikula 	}
3554379bc100SJani Nikula }
3555379bc100SJani Nikula 
3556379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
3557379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
3558379bc100SJani Nikula {
3559379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3560379bc100SJani Nikula 	enum port port = encoder->port;
3561379bc100SJani Nikula 	bool wait = false;
3562379bc100SJani Nikula 	u32 val;
3563379bc100SJani Nikula 
3564f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
3565379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
3566379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
3567f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
3568379bc100SJani Nikula 		wait = true;
3569379bc100SJani Nikula 	}
3570379bc100SJani Nikula 
3571e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
3572b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
35734444df6eSLucas De Marchi 
3574f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3575379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3576379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
3577f7960e7fSJani Nikula 		intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3578e468ff06SLucas De Marchi 	}
3579379bc100SJani Nikula 
3580379bc100SJani Nikula 	/* Disable FEC in DP Sink */
3581379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
3582379bc100SJani Nikula 
3583379bc100SJani Nikula 	if (wait)
3584379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
3585379bc100SJani Nikula }
3586379bc100SJani Nikula 
3587ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
3588ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
3589379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
3590379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
3591379bc100SJani Nikula {
3592379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3593b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3594379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
3595379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
3596379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
3597d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3598379bc100SJani Nikula 
3599c980216dSImre Deak 	if (!is_mst)
3600c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
3601c980216dSImre Deak 					old_crtc_state, old_conn_state);
3602fa37a213SGwan-gyeong Mun 
3603379bc100SJani Nikula 	/*
3604379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
3605379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
3606379bc100SJani Nikula 	 */
3607379bc100SJani Nikula 	intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
360878eaaba3SJosé Roberto de Souza 
3609c59053dcSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
3610c59053dcSJosé Roberto de Souza 		if (is_mst) {
3611c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
3612c59053dcSJosé Roberto de Souza 			u32 val;
3613c59053dcSJosé Roberto de Souza 
3614f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
3615f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
3616919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
3617919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
3618f7960e7fSJani Nikula 			intel_de_write(dev_priv,
3619f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
3620f7960e7fSJani Nikula 				       val);
3621c59053dcSJosé Roberto de Souza 		}
3622c59053dcSJosé Roberto de Souza 	} else {
3623c59053dcSJosé Roberto de Souza 		if (!is_mst)
362450a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
3625c59053dcSJosé Roberto de Souza 	}
3626379bc100SJani Nikula 
3627379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3628379bc100SJani Nikula 
36293ca8f191SJosé Roberto de Souza 	/*
36303ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
36313ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
36323ca8f191SJosé Roberto de Souza 	 * transcoder"
36333ca8f191SJosé Roberto de Souza 	 */
36343ca8f191SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
36353ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
36363ca8f191SJosé Roberto de Souza 
3637379bc100SJani Nikula 	intel_edp_panel_vdd_on(intel_dp);
3638379bc100SJani Nikula 	intel_edp_panel_off(intel_dp);
3639379bc100SJani Nikula 
3640d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
36413b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
3642379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3643379bc100SJani Nikula 						  dig_port->ddi_io_power_domain);
3644379bc100SJani Nikula 
3645379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3646379bc100SJani Nikula }
3647379bc100SJani Nikula 
3648ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
3649ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
3650379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
3651379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
3652379bc100SJani Nikula {
3653379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3654b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3655379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3656379bc100SJani Nikula 
3657379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
3658379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
3659379bc100SJani Nikula 
3660379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
3661379bc100SJani Nikula 
3662379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3663379bc100SJani Nikula 
3664379bc100SJani Nikula 	intel_display_power_put_unchecked(dev_priv,
3665379bc100SJani Nikula 					  dig_port->ddi_io_power_domain);
3666379bc100SJani Nikula 
3667379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3668379bc100SJani Nikula 
3669379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
3670379bc100SJani Nikula }
3671379bc100SJani Nikula 
3672ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
3673ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3674379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3675379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3676379bc100SJani Nikula {
3677379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3678b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
367917bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
368017bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3681379bc100SJani Nikula 
36827829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
3683773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
3684773b4b54SVille Syrjälä 
3685773b4b54SVille Syrjälä 		intel_disable_pipe(old_crtc_state);
3686773b4b54SVille Syrjälä 
3687773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
3688773b4b54SVille Syrjälä 
3689773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
3690773b4b54SVille Syrjälä 
3691773b4b54SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 9)
3692f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
3693773b4b54SVille Syrjälä 		else
36949eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
36957829c92bSVille Syrjälä 	}
3696773b4b54SVille Syrjälä 
3697379bc100SJani Nikula 	/*
3698379bc100SJani Nikula 	 * When called from DP MST code:
3699379bc100SJani Nikula 	 * - old_conn_state will be NULL
3700379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3701379bc100SJani Nikula 	 * - the main connector associated with this port
3702379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3703379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
3704379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
3705379bc100SJani Nikula 	 *   stream that was activated last, but each stream
3706379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3707379bc100SJani Nikula 	 *   the DP link parameteres
3708379bc100SJani Nikula 	 */
3709379bc100SJani Nikula 
3710379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3711ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
3712ede9771dSVille Syrjälä 					    old_conn_state);
3713379bc100SJani Nikula 	else
3714ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
3715ede9771dSVille Syrjälä 					  old_conn_state);
3716379bc100SJani Nikula 
3717379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3718379bc100SJani Nikula 		icl_unmap_plls_to_ports(encoder);
371917bef9baSVille Syrjälä 
372017bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
372117bef9baSVille Syrjälä 		intel_display_power_put_unchecked(dev_priv,
372217bef9baSVille Syrjälä 						  intel_ddi_main_link_aux_domain(dig_port));
372317bef9baSVille Syrjälä 
372417bef9baSVille Syrjälä 	if (is_tc_port)
372517bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
3726379bc100SJani Nikula }
3727379bc100SJani Nikula 
3728ede9771dSVille Syrjälä void intel_ddi_fdi_post_disable(struct intel_atomic_state *state,
3729ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
3730379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
3731379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
3732379bc100SJani Nikula {
3733379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3734379bc100SJani Nikula 	u32 val;
3735379bc100SJani Nikula 
3736379bc100SJani Nikula 	/*
3737379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
3738379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
3739379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
3740379bc100SJani Nikula 	 * originally before the BUN.
3741379bc100SJani Nikula 	 */
3742f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3743379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
3744f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3745379bc100SJani Nikula 
3746379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3747379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3748379bc100SJani Nikula 
3749f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
3750379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
3751379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
3752f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), val);
3753379bc100SJani Nikula 
3754f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3755379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
3756f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3757379bc100SJani Nikula 
3758f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3759379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
3760f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3761379bc100SJani Nikula }
3762379bc100SJani Nikula 
3763d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
3764d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
3765d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
3766d82a855aSVille Syrjälä {
3767d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
3768d82a855aSVille Syrjälä 	struct drm_connector *conn;
3769d82a855aSVille Syrjälä 	int i;
3770d82a855aSVille Syrjälä 
3771d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
3772d82a855aSVille Syrjälä 		return;
3773d82a855aSVille Syrjälä 
3774d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3775d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
3776d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
3777d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
3778d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
3779d82a855aSVille Syrjälä 
3780d82a855aSVille Syrjälä 		if (!slave_crtc)
3781d82a855aSVille Syrjälä 			continue;
3782d82a855aSVille Syrjälä 
3783d82a855aSVille Syrjälä 		slave_crtc_state =
3784d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
3785d82a855aSVille Syrjälä 
3786d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
3787d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
3788d82a855aSVille Syrjälä 			continue;
3789d82a855aSVille Syrjälä 
3790d82a855aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder));
3791d82a855aSVille Syrjälä 	}
3792d82a855aSVille Syrjälä 
3793d82a855aSVille Syrjälä 	usleep_range(200, 400);
3794d82a855aSVille Syrjälä 
3795d82a855aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder));
3796d82a855aSVille Syrjälä }
3797d82a855aSVille Syrjälä 
3798ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
3799ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
3800379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
3801379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
3802379bc100SJani Nikula {
3803379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3804b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3805379bc100SJani Nikula 	enum port port = encoder->port;
3806379bc100SJani Nikula 
3807379bc100SJani Nikula 	if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
3808379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3809379bc100SJani Nikula 
3810379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
38117a00e68bSGwan-gyeong Mun 	intel_psr_enable(intel_dp, crtc_state, conn_state);
38121bf3657cSGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3813379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3814379bc100SJani Nikula 
3815379bc100SJani Nikula 	if (crtc_state->has_audio)
3816379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3817d82a855aSVille Syrjälä 
3818d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
3819379bc100SJani Nikula }
3820379bc100SJani Nikula 
3821379bc100SJani Nikula static i915_reg_t
3822379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3823379bc100SJani Nikula 			       enum port port)
3824379bc100SJani Nikula {
382512c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
382612c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
382712c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
382812c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
382912c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
383012c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
3831379bc100SJani Nikula 	};
3832379bc100SJani Nikula 
38331de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, INTEL_GEN(dev_priv) < 9);
3834379bc100SJani Nikula 
38351de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
3836379bc100SJani Nikula 		port = PORT_A;
3837379bc100SJani Nikula 
383812c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
3839379bc100SJani Nikula }
3840379bc100SJani Nikula 
3841ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
3842ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3843379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3844379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3845379bc100SJani Nikula {
3846379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3847b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3848379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
3849379bc100SJani Nikula 	enum port port = encoder->port;
3850379bc100SJani Nikula 
3851379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3852379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3853379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
385447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
385547bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3856379bc100SJani Nikula 			    connector->base.id, connector->name);
3857379bc100SJani Nikula 
3858379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
3859379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
3860379bc100SJani Nikula 		/*
3861379bc100SJani Nikula 		 * For some reason these chicken bits have been
3862379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3863379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3864379bc100SJani Nikula 		 * a specific transcoder.
3865379bc100SJani Nikula 		 */
3866379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3867379bc100SJani Nikula 		u32 val;
3868379bc100SJani Nikula 
3869f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
3870379bc100SJani Nikula 
3871379bc100SJani Nikula 		if (port == PORT_E)
3872379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3873379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3874379bc100SJani Nikula 		else
3875379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3876379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3877379bc100SJani Nikula 
3878f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3879f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3880379bc100SJani Nikula 
3881379bc100SJani Nikula 		udelay(1);
3882379bc100SJani Nikula 
3883379bc100SJani Nikula 		if (port == PORT_E)
3884379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3885379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3886379bc100SJani Nikula 		else
3887379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3888379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3889379bc100SJani Nikula 
3890f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3891379bc100SJani Nikula 	}
3892379bc100SJani Nikula 
3893379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3894379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3895379bc100SJani Nikula 	 * enabling the port.
3896379bc100SJani Nikula 	 */
3897f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3898379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3899379bc100SJani Nikula 
3900379bc100SJani Nikula 	if (crtc_state->has_audio)
3901379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3902379bc100SJani Nikula }
3903379bc100SJani Nikula 
3904ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3905ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3906379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3907379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3908379bc100SJani Nikula {
39098b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
391021fd23acSJani Nikula 
3911eed22a46SVille Syrjälä 	intel_ddi_enable_transcoder_func(encoder, crtc_state);
39127c2fedd7SVille Syrjälä 
391321fd23acSJani Nikula 	intel_enable_pipe(crtc_state);
391421fd23acSJani Nikula 
391521fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
391621fd23acSJani Nikula 
3917379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3918ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3919379bc100SJani Nikula 	else
3920ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3921379bc100SJani Nikula 
3922379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3923379bc100SJani Nikula 	if (conn_state->content_protection ==
3924379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3925d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
392667e1d5edSVille Syrjälä 				  crtc_state->cpu_transcoder,
3927d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3928379bc100SJani Nikula }
3929379bc100SJani Nikula 
3930ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3931ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3932379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3933379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3934379bc100SJani Nikula {
3935b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3936379bc100SJani Nikula 
3937379bc100SJani Nikula 	intel_dp->link_trained = false;
3938379bc100SJani Nikula 
3939379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3940379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3941379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3942379bc100SJani Nikula 
3943379bc100SJani Nikula 	intel_edp_drrs_disable(intel_dp, old_crtc_state);
3944379bc100SJani Nikula 	intel_psr_disable(intel_dp, old_crtc_state);
3945379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3946379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3947379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3948379bc100SJani Nikula 					      false);
3949379bc100SJani Nikula }
3950379bc100SJani Nikula 
3951ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3952ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3953379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3954379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3955379bc100SJani Nikula {
395647bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3957379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3958379bc100SJani Nikula 
3959379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3960379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3961379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3962379bc100SJani Nikula 
3963379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3964379bc100SJani Nikula 					       false, false))
396547bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
396647bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3967379bc100SJani Nikula 			    connector->base.id, connector->name);
3968379bc100SJani Nikula }
3969379bc100SJani Nikula 
3970ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3971ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3972379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3973379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3974379bc100SJani Nikula {
3975379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3976379bc100SJani Nikula 
3977379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3978ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3979ede9771dSVille Syrjälä 				       old_conn_state);
3980379bc100SJani Nikula 	else
3981ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3982ede9771dSVille Syrjälä 				     old_conn_state);
3983379bc100SJani Nikula }
3984379bc100SJani Nikula 
3985ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3986ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3987379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3988379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3989379bc100SJani Nikula {
3990b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3991379bc100SJani Nikula 
39920c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3993379bc100SJani Nikula 
39947a00e68bSGwan-gyeong Mun 	intel_psr_update(intel_dp, crtc_state, conn_state);
399576d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3996379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3997379bc100SJani Nikula 
3998ede9771dSVille Syrjälä 	intel_panel_update_backlight(state, encoder, crtc_state, conn_state);
3999379bc100SJani Nikula }
4000379bc100SJani Nikula 
4001ede9771dSVille Syrjälä static void intel_ddi_update_pipe(struct intel_atomic_state *state,
4002ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
4003379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
4004379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
4005379bc100SJani Nikula {
4006d456512cSRamalingam C 
4007379bc100SJani Nikula 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
4008ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
4009ede9771dSVille Syrjälä 					 conn_state);
4010379bc100SJani Nikula 
4011ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
4012379bc100SJani Nikula }
4013379bc100SJani Nikula 
4014379bc100SJani Nikula static void
401524a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
401624a7bfe0SImre Deak 			 struct intel_encoder *encoder,
401724a7bfe0SImre Deak 			 struct intel_crtc *crtc)
401824a7bfe0SImre Deak {
401924a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
402024a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
402124a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
402224a7bfe0SImre Deak 
40238b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
402424a7bfe0SImre Deak 
4025b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
4026b7d02c3aSVille Syrjälä 		               required_lanes);
40271326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
402824a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
402924a7bfe0SImre Deak }
403024a7bfe0SImre Deak 
403124a7bfe0SImre Deak static void
403224a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
403324a7bfe0SImre Deak 			  struct intel_encoder *encoder,
403424a7bfe0SImre Deak 			  struct intel_crtc *crtc)
403524a7bfe0SImre Deak {
4036b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
403724a7bfe0SImre Deak }
403824a7bfe0SImre Deak 
403924a7bfe0SImre Deak static void
4040ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
4041ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
4042379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
4043379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
4044379bc100SJani Nikula {
4045379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4046b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4047d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
4048d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
4049379bc100SJani Nikula 
405024a7bfe0SImre Deak 	if (is_tc_port)
405124a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
405224a7bfe0SImre Deak 
405324a7bfe0SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port)
4054379bc100SJani Nikula 		intel_display_power_get(dev_priv,
4055379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
4056379bc100SJani Nikula 
40579d44dcb9SLucas De Marchi 	if (is_tc_port && dig_port->tc_mode != TC_PORT_TBT_ALT)
40589d44dcb9SLucas De Marchi 		/*
40599d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
40609d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
40619d44dcb9SLucas De Marchi 		 */
40629d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
40639d44dcb9SLucas De Marchi 	else if (IS_GEN9_LP(dev_priv))
4064379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
4065379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
4066379bc100SJani Nikula }
4067379bc100SJani Nikula 
4068379bc100SJani Nikula static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
4069379bc100SJani Nikula {
40707801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
40717801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
40727801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
407335ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
4074379bc100SJani Nikula 	bool wait = false;
4075379bc100SJani Nikula 
4076f7960e7fSJani Nikula 	dp_tp_ctl = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
407735ac28a8SLucas De Marchi 
407835ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
4079f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
408035ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
4081f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
408235ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
4083379bc100SJani Nikula 			wait = true;
4084379bc100SJani Nikula 		}
4085379bc100SJani Nikula 
408635ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
408735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
4088f7960e7fSJani Nikula 		intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, dp_tp_ctl);
4089f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4090379bc100SJani Nikula 
4091379bc100SJani Nikula 		if (wait)
4092379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
4093379bc100SJani Nikula 	}
4094379bc100SJani Nikula 
4095963501bdSImre Deak 	dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
4096379bc100SJani Nikula 	if (intel_dp->link_mst)
409735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
4098379bc100SJani Nikula 	else {
409935ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
4100379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
410135ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
4102379bc100SJani Nikula 	}
4103f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, dp_tp_ctl);
4104f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4105379bc100SJani Nikula 
4106379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
4107f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
4108f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
4109379bc100SJani Nikula 
4110e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
4111379bc100SJani Nikula }
4112379bc100SJani Nikula 
4113eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
4114eee3f911SVille Syrjälä 				     u8 dp_train_pat)
4115eee3f911SVille Syrjälä {
4116eee3f911SVille Syrjälä 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4117eee3f911SVille Syrjälä 	u8 train_pat_mask = drm_dp_training_pattern_mask(intel_dp->dpcd);
4118eee3f911SVille Syrjälä 	u32 temp;
4119eee3f911SVille Syrjälä 
4120eee3f911SVille Syrjälä 	temp = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4121eee3f911SVille Syrjälä 
4122eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
4123eee3f911SVille Syrjälä 	switch (dp_train_pat & train_pat_mask) {
4124eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
4125eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
4126eee3f911SVille Syrjälä 		break;
4127eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
4128eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
4129eee3f911SVille Syrjälä 		break;
4130eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
4131eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
4132eee3f911SVille Syrjälä 		break;
4133eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
4134eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
4135eee3f911SVille Syrjälä 		break;
4136eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
4137eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
4138eee3f911SVille Syrjälä 		break;
4139eee3f911SVille Syrjälä 	}
4140eee3f911SVille Syrjälä 
4141eee3f911SVille Syrjälä 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, temp);
4142eee3f911SVille Syrjälä }
4143eee3f911SVille Syrjälä 
41448fdda385SVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp)
41458fdda385SVille Syrjälä {
41468fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
41478fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
41488fdda385SVille Syrjälä 	enum port port = encoder->port;
41498fdda385SVille Syrjälä 	u32 val;
41508fdda385SVille Syrjälä 
41518fdda385SVille Syrjälä 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
41528fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
41538fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
41548fdda385SVille Syrjälä 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
41558fdda385SVille Syrjälä 
41568fdda385SVille Syrjälä 	/*
41578fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
41588fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
41598fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
41608fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
41618fdda385SVille Syrjälä 	 * idle patterns to be sent.
41628fdda385SVille Syrjälä 	 */
41638fdda385SVille Syrjälä 	if (port == PORT_A && INTEL_GEN(dev_priv) < 12)
41648fdda385SVille Syrjälä 		return;
41658fdda385SVille Syrjälä 
41668fdda385SVille Syrjälä 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
41678fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
41688fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
41698fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
41708fdda385SVille Syrjälä }
41718fdda385SVille Syrjälä 
4172379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
4173379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
4174379bc100SJani Nikula {
4175379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
4176379bc100SJani Nikula 		return false;
4177379bc100SJani Nikula 
4178379bc100SJani Nikula 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO))
4179379bc100SJani Nikula 		return false;
4180379bc100SJani Nikula 
4181f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
4182379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
4183379bc100SJani Nikula }
4184379bc100SJani Nikula 
4185379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
4186379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
4187379bc100SJani Nikula {
41880fde0b1dSMatt Roper 	if (INTEL_GEN(dev_priv) >= 12 && crtc_state->port_clock > 594000)
41890fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
41900fde0b1dSMatt Roper 	else if (IS_ELKHARTLAKE(dev_priv) && crtc_state->port_clock > 594000)
41919d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
41929d5fd37eSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 11 && crtc_state->port_clock > 594000)
4193379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
4194379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv) && crtc_state->port_clock > 594000)
4195379bc100SJani Nikula 		crtc_state->min_voltage_level = 2;
4196379bc100SJani Nikula }
4197379bc100SJani Nikula 
4198dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
419902d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
420002d8ea47SVille Syrjälä {
4201dc5b8ed5SVille Syrjälä 	u32 master_select;
420202d8ea47SVille Syrjälä 
4203dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
4204dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
420502d8ea47SVille Syrjälä 
420602d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
420702d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
420802d8ea47SVille Syrjälä 
4209d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
4210dc5b8ed5SVille Syrjälä 	} else {
4211dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
4212dc5b8ed5SVille Syrjälä 
4213dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
4214dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
4215dc5b8ed5SVille Syrjälä 
4216dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
4217dc5b8ed5SVille Syrjälä 	}
421802d8ea47SVille Syrjälä 
421902d8ea47SVille Syrjälä 	if (master_select == 0)
422002d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
422102d8ea47SVille Syrjälä 	else
422202d8ea47SVille Syrjälä 		return master_select - 1;
422302d8ea47SVille Syrjälä }
422402d8ea47SVille Syrjälä 
4225dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
422602d8ea47SVille Syrjälä {
422702d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
422802d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
422902d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
423002d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
423102d8ea47SVille Syrjälä 
423202d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
4233dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
423402d8ea47SVille Syrjälä 
423502d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
423602d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
423702d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
423802d8ea47SVille Syrjälä 
423902d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
424002d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
424102d8ea47SVille Syrjälä 								   power_domain);
424202d8ea47SVille Syrjälä 
424302d8ea47SVille Syrjälä 		if (!trans_wakeref)
424402d8ea47SVille Syrjälä 			continue;
424502d8ea47SVille Syrjälä 
4246dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
424702d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
424802d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
424902d8ea47SVille Syrjälä 
425002d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
425102d8ea47SVille Syrjälä 	}
425202d8ea47SVille Syrjälä 
425302d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
425402d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
425502d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
425602d8ea47SVille Syrjälä }
425702d8ea47SVille Syrjälä 
4258379bc100SJani Nikula void intel_ddi_get_config(struct intel_encoder *encoder,
4259379bc100SJani Nikula 			  struct intel_crtc_state *pipe_config)
4260379bc100SJani Nikula {
4261379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
42622225f3c6SMaarten Lankhorst 	struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->uapi.crtc);
4263379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4264edcb9028SJosé Roberto de Souza 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4265379bc100SJani Nikula 	u32 temp, flags = 0;
4266379bc100SJani Nikula 
4267379bc100SJani Nikula 	/* XXX: DSI transcoder paranoia */
42681de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
4269379bc100SJani Nikula 		return;
4270379bc100SJani Nikula 
4271fbacb15eSJani Nikula 	intel_dsc_get_config(encoder, pipe_config);
4272fbacb15eSJani Nikula 
4273f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
4274379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
4275379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
4276379bc100SJani Nikula 	else
4277379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
4278379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
4279379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
4280379bc100SJani Nikula 	else
4281379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
4282379bc100SJani Nikula 
42831326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
4284379bc100SJani Nikula 
4285379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
4286379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
4287379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
4288379bc100SJani Nikula 		break;
4289379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
4290379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
4291379bc100SJani Nikula 		break;
4292379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
4293379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
4294379bc100SJani Nikula 		break;
4295379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
4296379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
4297379bc100SJani Nikula 		break;
4298379bc100SJani Nikula 	default:
4299379bc100SJani Nikula 		break;
4300379bc100SJani Nikula 	}
4301379bc100SJani Nikula 
4302379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
4303379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
4304379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
4305379bc100SJani Nikula 
4306379bc100SJani Nikula 		pipe_config->infoframes.enable |=
4307379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4308379bc100SJani Nikula 
4309379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
4310379bc100SJani Nikula 			pipe_config->has_infoframe = true;
4311379bc100SJani Nikula 
4312379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
4313379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
4314379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
4315379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
4316379bc100SJani Nikula 		/* fall through */
4317379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
4318379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
4319379bc100SJani Nikula 		pipe_config->lane_count = 4;
4320379bc100SJani Nikula 		break;
4321379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
4322379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
4323379bc100SJani Nikula 		break;
4324379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
4325379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
4326379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
4327379bc100SJani Nikula 		else
4328379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
4329379bc100SJani Nikula 		pipe_config->lane_count =
4330379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
4331379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
43328aa940c8SMaarten Lankhorst 
43338aa940c8SMaarten Lankhorst 		if (INTEL_GEN(dev_priv) >= 11) {
43348aa940c8SMaarten Lankhorst 			i915_reg_t dp_tp_ctl;
43358aa940c8SMaarten Lankhorst 
43368aa940c8SMaarten Lankhorst 			if (IS_GEN(dev_priv, 11))
43378aa940c8SMaarten Lankhorst 				dp_tp_ctl = DP_TP_CTL(encoder->port);
43388aa940c8SMaarten Lankhorst 			else
43398aa940c8SMaarten Lankhorst 				dp_tp_ctl = TGL_DP_TP_CTL(pipe_config->cpu_transcoder);
43408aa940c8SMaarten Lankhorst 
43418aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
4342f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
43438aa940c8SMaarten Lankhorst 
434447bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
434547bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
43468aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
43478aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
43488aa940c8SMaarten Lankhorst 		}
43498aa940c8SMaarten Lankhorst 
4350dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
4351dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4352dee66f3eSGwan-gyeong Mun 
4353379bc100SJani Nikula 		break;
4354379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
4355379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
4356379bc100SJani Nikula 		pipe_config->lane_count =
4357379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
43586671c367SJosé Roberto de Souza 
43596671c367SJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
43606671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
43616671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
43626671c367SJosé Roberto de Souza 
4363379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
4364dee66f3eSGwan-gyeong Mun 
4365dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
4366dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4367379bc100SJani Nikula 		break;
4368379bc100SJani Nikula 	default:
4369379bc100SJani Nikula 		break;
4370379bc100SJani Nikula 	}
4371379bc100SJani Nikula 
4372f153478dSImre Deak 	if (INTEL_GEN(dev_priv) >= 12) {
4373f153478dSImre Deak 		enum transcoder transcoder =
4374f153478dSImre Deak 			intel_dp_mst_is_slave_trans(pipe_config) ?
4375f153478dSImre Deak 			pipe_config->mst_master_transcoder :
4376f153478dSImre Deak 			pipe_config->cpu_transcoder;
4377f153478dSImre Deak 
4378f153478dSImre Deak 		intel_dp->regs.dp_tp_ctl = TGL_DP_TP_CTL(transcoder);
4379f153478dSImre Deak 		intel_dp->regs.dp_tp_status = TGL_DP_TP_STATUS(transcoder);
4380f153478dSImre Deak 	}
4381f153478dSImre Deak 
4382379bc100SJani Nikula 	pipe_config->has_audio =
4383379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
4384379bc100SJani Nikula 
4385379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
4386379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
4387379bc100SJani Nikula 		/*
4388379bc100SJani Nikula 		 * This is a big fat ugly hack.
4389379bc100SJani Nikula 		 *
4390379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
4391379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
4392379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
4393379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
4394379bc100SJani Nikula 		 * max, not what it tells us to use.
4395379bc100SJani Nikula 		 *
4396379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
4397379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
4398379bc100SJani Nikula 		 * load.
4399379bc100SJani Nikula 		 */
440047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
440147bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
4402379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
4403379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
4404379bc100SJani Nikula 	}
4405379bc100SJani Nikula 
4406379bc100SJani Nikula 	intel_ddi_clock_get(encoder, pipe_config);
4407379bc100SJani Nikula 
4408379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4409379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4410379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
4411379bc100SJani Nikula 
4412379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4413379bc100SJani Nikula 
4414379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
4415379bc100SJani Nikula 
4416379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4417379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
4418379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
4419379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4420379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
4421379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
4422379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4423379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
4424379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
4425379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4426379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
4427379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
442802d8ea47SVille Syrjälä 
4429dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 8)
4430dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
4431dee66f3eSGwan-gyeong Mun 
4432dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
44332c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
4434379bc100SJani Nikula }
4435379bc100SJani Nikula 
4436379bc100SJani Nikula static enum intel_output_type
4437379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
4438379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
4439379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
4440379bc100SJani Nikula {
4441379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
4442379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
4443379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
4444379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
4445379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
4446379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
4447379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
4448379bc100SJani Nikula 	default:
4449379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
4450379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
4451379bc100SJani Nikula 	}
4452379bc100SJani Nikula }
4453379bc100SJani Nikula 
4454379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
4455379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
4456379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
4457379bc100SJani Nikula {
44582225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
4459379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4460379bc100SJani Nikula 	enum port port = encoder->port;
4461379bc100SJani Nikula 	int ret;
4462379bc100SJani Nikula 
446310cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
4464379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
4465379bc100SJani Nikula 
4466bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
4467379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
4468bdacf087SAnshuman Gupta 	} else {
4469379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
4470bdacf087SAnshuman Gupta 	}
4471bdacf087SAnshuman Gupta 
4472379bc100SJani Nikula 	if (ret)
4473379bc100SJani Nikula 		return ret;
4474379bc100SJani Nikula 
4475379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
4476379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
4477379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
4478379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
4479379bc100SJani Nikula 			pipe_config->crc_enabled;
4480379bc100SJani Nikula 
4481379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4482379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4483379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
4484379bc100SJani Nikula 
4485379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4486379bc100SJani Nikula 
4487379bc100SJani Nikula 	return 0;
4488379bc100SJani Nikula }
4489379bc100SJani Nikula 
4490b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
4491b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
4492b50a1aa6SManasi Navare {
4493b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
4494b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
4495b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
4496b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
4497b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
4498b50a1aa6SManasi Navare }
4499b50a1aa6SManasi Navare 
4500b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
4501b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
4502b50a1aa6SManasi Navare {
4503b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
4504b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
4505b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
4506b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
4507b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
4508b50a1aa6SManasi Navare }
4509b50a1aa6SManasi Navare 
4510b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
4511b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
4512b50a1aa6SManasi Navare {
4513b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
4514b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
4515b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
4516b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
4517b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
4518b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
4519b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
4520b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
4521b50a1aa6SManasi Navare }
4522b50a1aa6SManasi Navare 
4523b50a1aa6SManasi Navare static u8
4524b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
4525b50a1aa6SManasi Navare 				int tile_group_id)
4526b50a1aa6SManasi Navare {
4527b50a1aa6SManasi Navare 	struct drm_connector *connector;
4528b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
4529b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
4530b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
4531b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
4532b50a1aa6SManasi Navare 	u8 transcoders = 0;
4533b50a1aa6SManasi Navare 	int i;
4534b50a1aa6SManasi Navare 
4535dc5b8ed5SVille Syrjälä 	/*
4536dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
4537dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
4538dc5b8ed5SVille Syrjälä 	 */
4539dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 9)
4540b50a1aa6SManasi Navare 		return 0;
4541b50a1aa6SManasi Navare 
4542b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
4543b50a1aa6SManasi Navare 		return 0;
4544b50a1aa6SManasi Navare 
4545b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
4546b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
4547b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
4548b50a1aa6SManasi Navare 
4549b50a1aa6SManasi Navare 		if (!crtc)
4550b50a1aa6SManasi Navare 			continue;
4551b50a1aa6SManasi Navare 
4552b50a1aa6SManasi Navare 		if (!connector->has_tile ||
4553b50a1aa6SManasi Navare 		    connector->tile_group->id !=
4554b50a1aa6SManasi Navare 		    tile_group_id)
4555b50a1aa6SManasi Navare 			continue;
4556b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
4557b50a1aa6SManasi Navare 							     crtc);
4558b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
4559b50a1aa6SManasi Navare 						crtc_state))
4560b50a1aa6SManasi Navare 			continue;
4561b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
4562b50a1aa6SManasi Navare 	}
4563b50a1aa6SManasi Navare 
4564b50a1aa6SManasi Navare 	return transcoders;
4565b50a1aa6SManasi Navare }
4566b50a1aa6SManasi Navare 
4567b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
4568b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
4569b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
4570b50a1aa6SManasi Navare {
457147bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4572b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
4573b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
4574b50a1aa6SManasi Navare 
457547bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
4576b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
4577b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
4578b50a1aa6SManasi Navare 
4579b50a1aa6SManasi Navare 	if (connector->has_tile)
4580b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
4581b50a1aa6SManasi Navare 									connector->tile_group->id);
4582b50a1aa6SManasi Navare 
4583b50a1aa6SManasi Navare 	/*
4584b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
4585b50a1aa6SManasi Navare 	 * make them a master always when present
4586b50a1aa6SManasi Navare 	 */
4587b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
4588b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
4589b50a1aa6SManasi Navare 	else
4590b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
4591b50a1aa6SManasi Navare 
4592b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
4593b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
4594b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
4595b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
4596b50a1aa6SManasi Navare 	}
4597b50a1aa6SManasi Navare 
4598b50a1aa6SManasi Navare 	return 0;
4599b50a1aa6SManasi Navare }
4600b50a1aa6SManasi Navare 
4601379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
4602379bc100SJani Nikula {
4603b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4604379bc100SJani Nikula 
4605379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
4606379bc100SJani Nikula 
4607379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
4608379bc100SJani Nikula 	kfree(dig_port);
4609379bc100SJani Nikula }
4610379bc100SJani Nikula 
4611379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
461232691b58SImre Deak 	.reset = intel_dp_encoder_reset,
4613379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
4614379bc100SJani Nikula };
4615379bc100SJani Nikula 
4616379bc100SJani Nikula static struct intel_connector *
46177801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
4618379bc100SJani Nikula {
46197801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4620379bc100SJani Nikula 	struct intel_connector *connector;
46217801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4622379bc100SJani Nikula 
4623379bc100SJani Nikula 	connector = intel_connector_alloc();
4624379bc100SJani Nikula 	if (!connector)
4625379bc100SJani Nikula 		return NULL;
4626379bc100SJani Nikula 
46277801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
46287801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
46297801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
46307801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
4631eee3f911SVille Syrjälä 
4632fb83f72cSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 12)
46337801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = tgl_set_signal_levels;
4634fb83f72cSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 11)
46357801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = icl_set_signal_levels;
4636fb83f72cSVille Syrjälä 	else if (IS_CANNONLAKE(dev_priv))
46377801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = cnl_set_signal_levels;
4638fb83f72cSVille Syrjälä 	else if (IS_GEN9_LP(dev_priv))
46397801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = bxt_set_signal_levels;
4640fb83f72cSVille Syrjälä 	else
46417801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = hsw_set_signal_levels;
4642fb83f72cSVille Syrjälä 
46437801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
46447801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
464553de0a20SVille Syrjälä 
4646edcb9028SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) < 12) {
46477801f3b7SLucas De Marchi 		dig_port->dp.regs.dp_tp_ctl = DP_TP_CTL(port);
46487801f3b7SLucas De Marchi 		dig_port->dp.regs.dp_tp_status = DP_TP_STATUS(port);
4649edcb9028SJosé Roberto de Souza 	}
4650379bc100SJani Nikula 
46517801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
4652379bc100SJani Nikula 		kfree(connector);
4653379bc100SJani Nikula 		return NULL;
4654379bc100SJani Nikula 	}
4655379bc100SJani Nikula 
4656379bc100SJani Nikula 	return connector;
4657379bc100SJani Nikula }
4658379bc100SJani Nikula 
4659379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
4660379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
4661379bc100SJani Nikula {
4662379bc100SJani Nikula 	struct drm_atomic_state *state;
4663379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
4664379bc100SJani Nikula 	int ret;
4665379bc100SJani Nikula 
4666379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
4667379bc100SJani Nikula 	if (!state)
4668379bc100SJani Nikula 		return -ENOMEM;
4669379bc100SJani Nikula 
4670379bc100SJani Nikula 	state->acquire_ctx = ctx;
4671379bc100SJani Nikula 
4672379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
4673379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
4674379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
4675379bc100SJani Nikula 		goto out;
4676379bc100SJani Nikula 	}
4677379bc100SJani Nikula 
4678379bc100SJani Nikula 	crtc_state->connectors_changed = true;
4679379bc100SJani Nikula 
4680379bc100SJani Nikula 	ret = drm_atomic_commit(state);
4681379bc100SJani Nikula out:
4682379bc100SJani Nikula 	drm_atomic_state_put(state);
4683379bc100SJani Nikula 
4684379bc100SJani Nikula 	return ret;
4685379bc100SJani Nikula }
4686379bc100SJani Nikula 
4687379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4688379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4689379bc100SJani Nikula {
4690379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4691b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
4692379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4693379bc100SJani Nikula 	struct i2c_adapter *adapter =
4694379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4695379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4696379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4697379bc100SJani Nikula 	struct intel_crtc *crtc;
4698379bc100SJani Nikula 	u8 config;
4699379bc100SJani Nikula 	int ret;
4700379bc100SJani Nikula 
4701379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4702379bc100SJani Nikula 		return 0;
4703379bc100SJani Nikula 
4704379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4705379bc100SJani Nikula 			       ctx);
4706379bc100SJani Nikula 	if (ret)
4707379bc100SJani Nikula 		return ret;
4708379bc100SJani Nikula 
4709379bc100SJani Nikula 	conn_state = connector->base.state;
4710379bc100SJani Nikula 
4711379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4712379bc100SJani Nikula 	if (!crtc)
4713379bc100SJani Nikula 		return 0;
4714379bc100SJani Nikula 
4715379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4716379bc100SJani Nikula 	if (ret)
4717379bc100SJani Nikula 		return ret;
4718379bc100SJani Nikula 
4719379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4720379bc100SJani Nikula 
47211de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
47221de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4723379bc100SJani Nikula 
47241326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4725379bc100SJani Nikula 		return 0;
4726379bc100SJani Nikula 
4727379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4728379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4729379bc100SJani Nikula 		return 0;
4730379bc100SJani Nikula 
4731379bc100SJani Nikula 	if (conn_state->commit &&
4732379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4733379bc100SJani Nikula 		return 0;
4734379bc100SJani Nikula 
4735379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4736379bc100SJani Nikula 	if (ret < 0) {
473747bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
473847bdb1caSJani Nikula 			ret);
4739379bc100SJani Nikula 		return 0;
4740379bc100SJani Nikula 	}
4741379bc100SJani Nikula 
4742379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4743379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4744379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4745379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4746379bc100SJani Nikula 		return 0;
4747379bc100SJani Nikula 
4748379bc100SJani Nikula 	/*
4749379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4750379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4751379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4752379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4753379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4754379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4755379bc100SJani Nikula 	 * the SCDC settings on the fly.
4756379bc100SJani Nikula 	 */
4757379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4758379bc100SJani Nikula }
4759379bc100SJani Nikula 
47603944709dSImre Deak static enum intel_hotplug_state
47613944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
47628c8919c7SImre Deak 		  struct intel_connector *connector)
4763379bc100SJani Nikula {
4764b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4765b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4766b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4767b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4768379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
47693944709dSImre Deak 	enum intel_hotplug_state state;
4770379bc100SJani Nikula 	int ret;
4771379bc100SJani Nikula 
47728c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4773379bc100SJani Nikula 
4774379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4775379bc100SJani Nikula 
4776379bc100SJani Nikula 	for (;;) {
4777379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4778379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4779379bc100SJani Nikula 		else
4780379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4781379bc100SJani Nikula 
4782379bc100SJani Nikula 		if (ret == -EDEADLK) {
4783379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4784379bc100SJani Nikula 			continue;
4785379bc100SJani Nikula 		}
4786379bc100SJani Nikula 
4787379bc100SJani Nikula 		break;
4788379bc100SJani Nikula 	}
4789379bc100SJani Nikula 
4790379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4791379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
47923a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
47933a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4794379bc100SJani Nikula 
4795bb80c925SJosé Roberto de Souza 	/*
4796bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4797bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4798bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4799bb80c925SJosé Roberto de Souza 	 *
4800bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4801bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4802bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4803bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4804bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4805bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4806bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4807bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4808bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4809bb80c925SJosé Roberto de Souza 	 * status.
4810b4df5405SImre Deak 	 *
4811b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4812b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4813b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4814b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4815b4df5405SImre Deak 	 * connectors to account for this delay.
4816bb80c925SJosé Roberto de Souza 	 */
4817b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4818b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4819bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4820bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4821bb80c925SJosé Roberto de Souza 
48223944709dSImre Deak 	return state;
4823379bc100SJani Nikula }
4824379bc100SJani Nikula 
4825edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4826edc0e09cSVille Syrjälä {
4827edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4828c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4829edc0e09cSVille Syrjälä 
4830edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4831edc0e09cSVille Syrjälä }
4832edc0e09cSVille Syrjälä 
4833edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4834edc0e09cSVille Syrjälä {
4835edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4836c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4837edc0e09cSVille Syrjälä 
4838c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4839edc0e09cSVille Syrjälä }
4840edc0e09cSVille Syrjälä 
4841edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4842edc0e09cSVille Syrjälä {
4843edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4844c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4845edc0e09cSVille Syrjälä 
4846edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4847edc0e09cSVille Syrjälä }
4848edc0e09cSVille Syrjälä 
4849379bc100SJani Nikula static struct intel_connector *
48507801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4851379bc100SJani Nikula {
4852379bc100SJani Nikula 	struct intel_connector *connector;
48537801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4854379bc100SJani Nikula 
4855379bc100SJani Nikula 	connector = intel_connector_alloc();
4856379bc100SJani Nikula 	if (!connector)
4857379bc100SJani Nikula 		return NULL;
4858379bc100SJani Nikula 
48597801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
48607801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4861379bc100SJani Nikula 
4862379bc100SJani Nikula 	return connector;
4863379bc100SJani Nikula }
4864379bc100SJani Nikula 
48657801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4866379bc100SJani Nikula {
48677801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4868379bc100SJani Nikula 
48697801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4870379bc100SJani Nikula 		return false;
4871379bc100SJani Nikula 
48727801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4873379bc100SJani Nikula 		return false;
4874379bc100SJani Nikula 
4875379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4876379bc100SJani Nikula 	 *                     supported configuration
4877379bc100SJani Nikula 	 */
4878379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4879379bc100SJani Nikula 		return true;
4880379bc100SJani Nikula 
4881379bc100SJani Nikula 	/* Cannonlake: Most of SKUs don't support DDI_E, and the only
4882379bc100SJani Nikula 	 *             one who does also have a full A/E split called
4883379bc100SJani Nikula 	 *             DDI_F what makes DDI_E useless. However for this
4884379bc100SJani Nikula 	 *             case let's trust VBT info.
4885379bc100SJani Nikula 	 */
4886379bc100SJani Nikula 	if (IS_CANNONLAKE(dev_priv) &&
4887379bc100SJani Nikula 	    !intel_bios_is_port_present(dev_priv, PORT_E))
4888379bc100SJani Nikula 		return true;
4889379bc100SJani Nikula 
4890379bc100SJani Nikula 	return false;
4891379bc100SJani Nikula }
4892379bc100SJani Nikula 
4893379bc100SJani Nikula static int
48947801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4895379bc100SJani Nikula {
48967801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
48977801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4898379bc100SJani Nikula 	int max_lanes = 4;
4899379bc100SJani Nikula 
4900379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4901379bc100SJani Nikula 		return max_lanes;
4902379bc100SJani Nikula 
4903379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4904f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4905379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4906379bc100SJani Nikula 		else
4907379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4908379bc100SJani Nikula 			max_lanes = 2;
4909379bc100SJani Nikula 	}
4910379bc100SJani Nikula 
4911379bc100SJani Nikula 	/*
4912379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4913379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4914379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4915379bc100SJani Nikula 	 */
49167801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
491747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
491847bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
49197801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4920379bc100SJani Nikula 		max_lanes = 4;
4921379bc100SJani Nikula 	}
4922379bc100SJani Nikula 
4923379bc100SJani Nikula 	return max_lanes;
4924379bc100SJani Nikula }
4925379bc100SJani Nikula 
4926379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4927379bc100SJani Nikula {
49287801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
492970dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
4930379bc100SJani Nikula 	bool init_hdmi, init_dp, init_lspcon = false;
4931d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4932379bc100SJani Nikula 
4933c5faae5aSJani Nikula 	init_hdmi = intel_bios_port_supports_dvi(dev_priv, port) ||
4934c5faae5aSJani Nikula 		intel_bios_port_supports_hdmi(dev_priv, port);
4935c5faae5aSJani Nikula 	init_dp = intel_bios_port_supports_dp(dev_priv, port);
4936379bc100SJani Nikula 
4937379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4938379bc100SJani Nikula 		/*
4939379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4940379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4941379bc100SJani Nikula 		 * is initialized before lspcon.
4942379bc100SJani Nikula 		 */
4943379bc100SJani Nikula 		init_dp = true;
4944379bc100SJani Nikula 		init_lspcon = true;
4945379bc100SJani Nikula 		init_hdmi = false;
494647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
494747bdb1caSJani Nikula 			    port_name(port));
4948379bc100SJani Nikula 	}
4949379bc100SJani Nikula 
4950379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
495147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
495247bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4953379bc100SJani Nikula 			    port_name(port));
4954379bc100SJani Nikula 		return;
4955379bc100SJani Nikula 	}
4956379bc100SJani Nikula 
49577801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
49587801f3b7SLucas De Marchi 	if (!dig_port)
4959379bc100SJani Nikula 		return;
4960379bc100SJani Nikula 
49617801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4962379bc100SJani Nikula 
496370dfbc29SLucas De Marchi 	drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4964379bc100SJani Nikula 			 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
4965379bc100SJani Nikula 
496670dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
496770dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
496870dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4969b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
497070dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
497170dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
497270dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
497370dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
497470dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
497570dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
497670dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
497770dfbc29SLucas De Marchi 	encoder->get_config = intel_ddi_get_config;
497870dfbc29SLucas De Marchi 	encoder->suspend = intel_dp_encoder_suspend;
497970dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
498070dfbc29SLucas De Marchi 
498170dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
498270dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
498370dfbc29SLucas De Marchi 	encoder->port = port;
498470dfbc29SLucas De Marchi 	encoder->cloneable = 0;
498570dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4986379bc100SJani Nikula 
4987379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
49887801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
49897801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
49907801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4991379bc100SJani Nikula 	else
49927801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
49937801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
49947801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
499570dfbc29SLucas De Marchi 
49967801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
49977801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
49987801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4999379bc100SJani Nikula 
5000d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
5001c5faae5aSJani Nikula 		bool is_legacy =
5002c5faae5aSJani Nikula 			!intel_bios_port_supports_typec_usb(dev_priv, port) &&
5003c5faae5aSJani Nikula 			!intel_bios_port_supports_tbt(dev_priv, port);
5004379bc100SJani Nikula 
50057801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
500624a7bfe0SImre Deak 
500770dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
500870dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
5009ab7bc4e1SImre Deak 	}
5010ab7bc4e1SImre Deak 
50111de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
50127801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
5013327f8d8cSLucas De Marchi 					      port - PORT_A;
5014379bc100SJani Nikula 
5015379bc100SJani Nikula 	if (init_dp) {
50167801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
5017379bc100SJani Nikula 			goto err;
5018379bc100SJani Nikula 
50197801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
5020379bc100SJani Nikula 	}
5021379bc100SJani Nikula 
5022379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
5023379bc100SJani Nikula 	 * case we have some really bad VBTs... */
502470dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
50257801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
5026379bc100SJani Nikula 			goto err;
5027379bc100SJani Nikula 	}
5028379bc100SJani Nikula 
5029379bc100SJani Nikula 	if (init_lspcon) {
50307801f3b7SLucas De Marchi 		if (lspcon_init(dig_port))
5031379bc100SJani Nikula 			/* TODO: handle hdmi info frame part */
503247bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
503347bdb1caSJani Nikula 				    "LSPCON init success on port %c\n",
5034379bc100SJani Nikula 				    port_name(port));
5035379bc100SJani Nikula 		else
5036379bc100SJani Nikula 			/*
5037379bc100SJani Nikula 			 * LSPCON init faied, but DP init was success, so
5038379bc100SJani Nikula 			 * lets try to drive as DP++ port.
5039379bc100SJani Nikula 			 */
504047bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
504147bdb1caSJani Nikula 				"LSPCON init failed on port %c\n",
5042379bc100SJani Nikula 				port_name(port));
5043379bc100SJani Nikula 	}
5044379bc100SJani Nikula 
5045edc0e09cSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
5046edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
50477801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
5048edc0e09cSVille Syrjälä 		else
50497801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5050c7e8a3d6SVille Syrjälä 	} else if (INTEL_GEN(dev_priv) >= 8) {
5051c7e8a3d6SVille Syrjälä 		if (port == PORT_A || IS_GEN9_LP(dev_priv))
50527801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
5053edc0e09cSVille Syrjälä 		else
50547801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5055edc0e09cSVille Syrjälä 	} else {
5056c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
50577801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
5058edc0e09cSVille Syrjälä 		else
50597801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5060edc0e09cSVille Syrjälä 	}
5061edc0e09cSVille Syrjälä 
50627801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
5063379bc100SJani Nikula 
5064379bc100SJani Nikula 	return;
5065379bc100SJani Nikula 
5066379bc100SJani Nikula err:
506770dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
50687801f3b7SLucas De Marchi 	kfree(dig_port);
5069379bc100SJani Nikula }
5070