xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision a8143150faa73c424c4d63fbff774a9c06a98ddc)
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 
709379bc100SJani Nikula static const struct ddi_buf_trans *
710*a8143150SJosé Roberto de Souza bdw_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
711379bc100SJani Nikula {
712*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
713*a8143150SJosé Roberto de Souza 
714379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
715379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
716379bc100SJani Nikula 		return bdw_ddi_translations_edp;
717379bc100SJani Nikula 	} else {
718379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
719379bc100SJani Nikula 		return bdw_ddi_translations_dp;
720379bc100SJani Nikula 	}
721379bc100SJani Nikula }
722379bc100SJani Nikula 
723379bc100SJani Nikula static const struct ddi_buf_trans *
724*a8143150SJosé Roberto de Souza skl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
725379bc100SJani Nikula {
726*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
727*a8143150SJosé Roberto de Souza 
728379bc100SJani Nikula 	if (IS_SKL_ULX(dev_priv)) {
729379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_dp);
730379bc100SJani Nikula 		return skl_y_ddi_translations_dp;
731379bc100SJani Nikula 	} else if (IS_SKL_ULT(dev_priv)) {
732379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_u_ddi_translations_dp);
733379bc100SJani Nikula 		return skl_u_ddi_translations_dp;
734379bc100SJani Nikula 	} else {
735379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_dp);
736379bc100SJani Nikula 		return skl_ddi_translations_dp;
737379bc100SJani Nikula 	}
738379bc100SJani Nikula }
739379bc100SJani Nikula 
740379bc100SJani Nikula static const struct ddi_buf_trans *
741*a8143150SJosé Roberto de Souza kbl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
742379bc100SJani Nikula {
743*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
744*a8143150SJosé Roberto de Souza 
7455f4ae270SChris Wilson 	if (IS_KBL_ULX(dev_priv) ||
7465f4ae270SChris Wilson 	    IS_CFL_ULX(dev_priv) ||
7475f4ae270SChris Wilson 	    IS_CML_ULX(dev_priv)) {
748379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_y_ddi_translations_dp);
749379bc100SJani Nikula 		return kbl_y_ddi_translations_dp;
7505f4ae270SChris Wilson 	} else if (IS_KBL_ULT(dev_priv) ||
7515f4ae270SChris Wilson 		   IS_CFL_ULT(dev_priv) ||
7525f4ae270SChris Wilson 		   IS_CML_ULT(dev_priv)) {
753379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_u_ddi_translations_dp);
754379bc100SJani Nikula 		return kbl_u_ddi_translations_dp;
755379bc100SJani Nikula 	} else {
756379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_ddi_translations_dp);
757379bc100SJani Nikula 		return kbl_ddi_translations_dp;
758379bc100SJani Nikula 	}
759379bc100SJani Nikula }
760379bc100SJani Nikula 
761379bc100SJani Nikula static const struct ddi_buf_trans *
762*a8143150SJosé Roberto de Souza skl_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
763379bc100SJani Nikula {
764*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
765*a8143150SJosé Roberto de Souza 
766379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
7675f4ae270SChris Wilson 		if (IS_SKL_ULX(dev_priv) ||
7685f4ae270SChris Wilson 		    IS_KBL_ULX(dev_priv) ||
7695f4ae270SChris Wilson 		    IS_CFL_ULX(dev_priv) ||
7705f4ae270SChris Wilson 		    IS_CML_ULX(dev_priv)) {
771379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_y_ddi_translations_edp);
772379bc100SJani Nikula 			return skl_y_ddi_translations_edp;
7735f4ae270SChris Wilson 		} else if (IS_SKL_ULT(dev_priv) ||
7745f4ae270SChris Wilson 			   IS_KBL_ULT(dev_priv) ||
7755f4ae270SChris Wilson 			   IS_CFL_ULT(dev_priv) ||
7765f4ae270SChris Wilson 			   IS_CML_ULT(dev_priv)) {
777379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_u_ddi_translations_edp);
778379bc100SJani Nikula 			return skl_u_ddi_translations_edp;
779379bc100SJani Nikula 		} else {
780379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_ddi_translations_edp);
781379bc100SJani Nikula 			return skl_ddi_translations_edp;
782379bc100SJani Nikula 		}
783379bc100SJani Nikula 	}
784379bc100SJani Nikula 
7855f4ae270SChris Wilson 	if (IS_KABYLAKE(dev_priv) ||
7865f4ae270SChris Wilson 	    IS_COFFEELAKE(dev_priv) ||
7875f4ae270SChris Wilson 	    IS_COMETLAKE(dev_priv))
788*a8143150SJosé Roberto de Souza 		return kbl_get_buf_trans_dp(encoder, n_entries);
789379bc100SJani Nikula 	else
790*a8143150SJosé Roberto de Souza 		return skl_get_buf_trans_dp(encoder, n_entries);
791379bc100SJani Nikula }
792379bc100SJani Nikula 
793379bc100SJani Nikula static const struct ddi_buf_trans *
794379bc100SJani Nikula skl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
795379bc100SJani Nikula {
7965f4ae270SChris Wilson 	if (IS_SKL_ULX(dev_priv) ||
7975f4ae270SChris Wilson 	    IS_KBL_ULX(dev_priv) ||
7985f4ae270SChris Wilson 	    IS_CFL_ULX(dev_priv) ||
7995f4ae270SChris Wilson 	    IS_CML_ULX(dev_priv)) {
800379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_hdmi);
801379bc100SJani Nikula 		return skl_y_ddi_translations_hdmi;
802379bc100SJani Nikula 	} else {
803379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
804379bc100SJani Nikula 		return skl_ddi_translations_hdmi;
805379bc100SJani Nikula 	}
806379bc100SJani Nikula }
807379bc100SJani Nikula 
808379bc100SJani Nikula static int skl_buf_trans_num_entries(enum port port, int n_entries)
809379bc100SJani Nikula {
810379bc100SJani Nikula 	/* Only DDIA and DDIE can select the 10th register with DP */
811379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E)
812379bc100SJani Nikula 		return min(n_entries, 10);
813379bc100SJani Nikula 	else
814379bc100SJani Nikula 		return min(n_entries, 9);
815379bc100SJani Nikula }
816379bc100SJani Nikula 
817379bc100SJani Nikula static const struct ddi_buf_trans *
818*a8143150SJosé Roberto de Souza intel_ddi_get_buf_trans_dp(struct intel_encoder *encoder,
819379bc100SJani Nikula 			   enum port port, int *n_entries)
820379bc100SJani Nikula {
821*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
822*a8143150SJosé Roberto de Souza 
8235f4ae270SChris Wilson 	if (IS_KABYLAKE(dev_priv) ||
8245f4ae270SChris Wilson 	    IS_COFFEELAKE(dev_priv) ||
8255f4ae270SChris Wilson 	    IS_COMETLAKE(dev_priv)) {
826379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
827*a8143150SJosé Roberto de Souza 			kbl_get_buf_trans_dp(encoder, n_entries);
828379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
829379bc100SJani Nikula 		return ddi_translations;
830379bc100SJani Nikula 	} else if (IS_SKYLAKE(dev_priv)) {
831379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
832*a8143150SJosé Roberto de Souza 			skl_get_buf_trans_dp(encoder, n_entries);
833379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
834379bc100SJani Nikula 		return ddi_translations;
835379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
836379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
837379bc100SJani Nikula 		return  bdw_ddi_translations_dp;
838379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
839379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
840379bc100SJani Nikula 		return hsw_ddi_translations_dp;
841379bc100SJani Nikula 	}
842379bc100SJani Nikula 
843379bc100SJani Nikula 	*n_entries = 0;
844379bc100SJani Nikula 	return NULL;
845379bc100SJani Nikula }
846379bc100SJani Nikula 
847379bc100SJani Nikula static const struct ddi_buf_trans *
848*a8143150SJosé Roberto de Souza intel_ddi_get_buf_trans_edp(struct intel_encoder *encoder,
849379bc100SJani Nikula 			    enum port port, int *n_entries)
850379bc100SJani Nikula {
851*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
852*a8143150SJosé Roberto de Souza 
853379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
854379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
855*a8143150SJosé Roberto de Souza 			skl_get_buf_trans_edp(encoder, n_entries);
856379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
857379bc100SJani Nikula 		return ddi_translations;
858379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
859*a8143150SJosé Roberto de Souza 		return bdw_get_buf_trans_edp(encoder, n_entries);
860379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
861379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
862379bc100SJani Nikula 		return hsw_ddi_translations_dp;
863379bc100SJani Nikula 	}
864379bc100SJani Nikula 
865379bc100SJani Nikula 	*n_entries = 0;
866379bc100SJani Nikula 	return NULL;
867379bc100SJani Nikula }
868379bc100SJani Nikula 
869379bc100SJani Nikula static const struct ddi_buf_trans *
870379bc100SJani Nikula intel_ddi_get_buf_trans_fdi(struct drm_i915_private *dev_priv,
871379bc100SJani Nikula 			    int *n_entries)
872379bc100SJani Nikula {
873379bc100SJani Nikula 	if (IS_BROADWELL(dev_priv)) {
874379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_fdi);
875379bc100SJani Nikula 		return bdw_ddi_translations_fdi;
876379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
877379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
878379bc100SJani Nikula 		return hsw_ddi_translations_fdi;
879379bc100SJani Nikula 	}
880379bc100SJani Nikula 
881379bc100SJani Nikula 	*n_entries = 0;
882379bc100SJani Nikula 	return NULL;
883379bc100SJani Nikula }
884379bc100SJani Nikula 
885379bc100SJani Nikula static const struct ddi_buf_trans *
886*a8143150SJosé Roberto de Souza intel_ddi_get_buf_trans_hdmi(struct intel_encoder *encoder,
887379bc100SJani Nikula 			     int *n_entries)
888379bc100SJani Nikula {
889*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
890*a8143150SJosé Roberto de Souza 
891379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
892379bc100SJani Nikula 		return skl_get_buf_trans_hdmi(dev_priv, n_entries);
893379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
894379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
895379bc100SJani Nikula 		return bdw_ddi_translations_hdmi;
896379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
897379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
898379bc100SJani Nikula 		return hsw_ddi_translations_hdmi;
899379bc100SJani Nikula 	}
900379bc100SJani Nikula 
901379bc100SJani Nikula 	*n_entries = 0;
902379bc100SJani Nikula 	return NULL;
903379bc100SJani Nikula }
904379bc100SJani Nikula 
905379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
906*a8143150SJosé Roberto de Souza bxt_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
907379bc100SJani Nikula {
908379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_dp);
909379bc100SJani Nikula 	return bxt_ddi_translations_dp;
910379bc100SJani Nikula }
911379bc100SJani Nikula 
912379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
913*a8143150SJosé Roberto de Souza bxt_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
914379bc100SJani Nikula {
915*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
916*a8143150SJosé Roberto de Souza 
917379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
918379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bxt_ddi_translations_edp);
919379bc100SJani Nikula 		return bxt_ddi_translations_edp;
920379bc100SJani Nikula 	}
921379bc100SJani Nikula 
922*a8143150SJosé Roberto de Souza 	return bxt_get_buf_trans_dp(encoder, n_entries);
923379bc100SJani Nikula }
924379bc100SJani Nikula 
925379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
926*a8143150SJosé Roberto de Souza bxt_get_buf_trans_hdmi(struct intel_encoder *encoder, int *n_entries)
927379bc100SJani Nikula {
928379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_hdmi);
929379bc100SJani Nikula 	return bxt_ddi_translations_hdmi;
930379bc100SJani Nikula }
931379bc100SJani Nikula 
932379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
933*a8143150SJosé Roberto de Souza cnl_get_buf_trans_hdmi(struct intel_encoder *encoder, int *n_entries)
934379bc100SJani Nikula {
935*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
936f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
937379bc100SJani Nikula 
938379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
939379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_85V);
940379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_85V;
941379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
942379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_95V);
943379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_95V;
944379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
945379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_1_05V);
946379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_1_05V;
947379bc100SJani Nikula 	} else {
948379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
949379bc100SJani Nikula 		MISSING_CASE(voltage);
950379bc100SJani Nikula 	}
951379bc100SJani Nikula 	return NULL;
952379bc100SJani Nikula }
953379bc100SJani Nikula 
954379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
955*a8143150SJosé Roberto de Souza cnl_get_buf_trans_dp(struct intel_encoder *encoder, int *n_entries)
956379bc100SJani Nikula {
957*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
958f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
959379bc100SJani Nikula 
960379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
961379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_85V);
962379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_85V;
963379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
964379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_95V);
965379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_95V;
966379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
967379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_1_05V);
968379bc100SJani Nikula 		return cnl_ddi_translations_dp_1_05V;
969379bc100SJani Nikula 	} else {
970379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
971379bc100SJani Nikula 		MISSING_CASE(voltage);
972379bc100SJani Nikula 	}
973379bc100SJani Nikula 	return NULL;
974379bc100SJani Nikula }
975379bc100SJani Nikula 
976379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
977*a8143150SJosé Roberto de Souza cnl_get_buf_trans_edp(struct intel_encoder *encoder, int *n_entries)
978379bc100SJani Nikula {
979*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
980f7960e7fSJani Nikula 	u32 voltage = intel_de_read(dev_priv, CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
981379bc100SJani Nikula 
982379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
983379bc100SJani Nikula 		if (voltage == VOLTAGE_INFO_0_85V) {
984379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_85V);
985379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_85V;
986379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_0_95V) {
987379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_95V);
988379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_95V;
989379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_1_05V) {
990379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_1_05V);
991379bc100SJani Nikula 			return cnl_ddi_translations_edp_1_05V;
992379bc100SJani Nikula 		} else {
993379bc100SJani Nikula 			*n_entries = 1; /* shut up gcc */
994379bc100SJani Nikula 			MISSING_CASE(voltage);
995379bc100SJani Nikula 		}
996379bc100SJani Nikula 		return NULL;
997379bc100SJani Nikula 	} else {
998*a8143150SJosé Roberto de Souza 		return cnl_get_buf_trans_dp(encoder, n_entries);
999379bc100SJani Nikula 	}
1000379bc100SJani Nikula }
1001379bc100SJani Nikula 
1002379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
1003*a8143150SJosé Roberto de Souza icl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
10044a8134d5SMatt Roper 			int *n_entries)
1005379bc100SJani Nikula {
1006*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1007*a8143150SJosé Roberto de Souza 
1008379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
1009379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_hdmi);
1010379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_hdmi;
1011379bc100SJani Nikula 	} else if (rate > 540000 && type == INTEL_OUTPUT_EDP) {
1012379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr3);
1013379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr3;
1014379bc100SJani Nikula 	} else if (type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.low_vswing) {
1015379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr2);
1016379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr2;
1017379bc100SJani Nikula 	}
1018379bc100SJani Nikula 
1019379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_dp_hbr2);
1020379bc100SJani Nikula 	return icl_combo_phy_ddi_translations_dp_hbr2;
1021379bc100SJani Nikula }
1022379bc100SJani Nikula 
10239f7ffa29SJosé Roberto de Souza static const struct icl_mg_phy_ddi_buf_trans *
1024*a8143150SJosé Roberto de Souza icl_get_mg_buf_trans(struct intel_encoder *encoder, int type, int rate,
10259f7ffa29SJosé Roberto de Souza 		     int *n_entries)
10269f7ffa29SJosé Roberto de Souza {
10279f7ffa29SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI) {
10289f7ffa29SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_hdmi);
10299f7ffa29SJosé Roberto de Souza 		return icl_mg_phy_ddi_translations_hdmi;
10309f7ffa29SJosé Roberto de Souza 	} else if (rate > 270000) {
10319f7ffa29SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_hbr2_hbr3);
10329f7ffa29SJosé Roberto de Souza 		return icl_mg_phy_ddi_translations_hbr2_hbr3;
10339f7ffa29SJosé Roberto de Souza 	}
10349f7ffa29SJosé Roberto de Souza 
10359f7ffa29SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations_rbr_hbr);
10369f7ffa29SJosé Roberto de Souza 	return icl_mg_phy_ddi_translations_rbr_hbr;
10379f7ffa29SJosé Roberto de Souza }
10389f7ffa29SJosé Roberto de Souza 
1039bd3cf6f7SJosé Roberto de Souza static const struct cnl_ddi_buf_trans *
1040*a8143150SJosé Roberto de Souza ehl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
1041b42d5a67SJosé Roberto de Souza 			int *n_entries)
1042b42d5a67SJosé Roberto de Souza {
1043a2ae2010SJosé Roberto de Souza 	if (type != INTEL_OUTPUT_HDMI && type != INTEL_OUTPUT_EDP) {
1044a2ae2010SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(ehl_combo_phy_ddi_translations_dp);
1045a2ae2010SJosé Roberto de Souza 		return ehl_combo_phy_ddi_translations_dp;
1046b42d5a67SJosé Roberto de Souza 	}
1047b42d5a67SJosé Roberto de Souza 
1048*a8143150SJosé Roberto de Souza 	return icl_get_combo_buf_trans(encoder, type, rate, n_entries);
1049b42d5a67SJosé Roberto de Souza }
1050b42d5a67SJosé Roberto de Souza 
1051b42d5a67SJosé Roberto de Souza static const struct cnl_ddi_buf_trans *
1052*a8143150SJosé Roberto de Souza tgl_get_combo_buf_trans(struct intel_encoder *encoder, int type, int rate,
1053bd3cf6f7SJosé Roberto de Souza 			int *n_entries)
1054bd3cf6f7SJosé Roberto de Souza {
105570988115SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_EDP) {
1056*a8143150SJosé Roberto de Souza 		return icl_get_combo_buf_trans(encoder, type, rate, n_entries);
1057bd3cf6f7SJosé Roberto de Souza 	} else if (rate > 270000) {
1058bd3cf6f7SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_combo_phy_ddi_translations_dp_hbr2);
1059bd3cf6f7SJosé Roberto de Souza 		return tgl_combo_phy_ddi_translations_dp_hbr2;
1060bd3cf6f7SJosé Roberto de Souza 	}
1061bd3cf6f7SJosé Roberto de Souza 
1062bd3cf6f7SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(tgl_combo_phy_ddi_translations_dp_hbr);
1063bd3cf6f7SJosé Roberto de Souza 	return tgl_combo_phy_ddi_translations_dp_hbr;
1064bd3cf6f7SJosé Roberto de Souza }
1065bd3cf6f7SJosé Roberto de Souza 
10669fa67699SJosé Roberto de Souza static const struct tgl_dkl_phy_ddi_buf_trans *
1067*a8143150SJosé Roberto de Souza tgl_get_dkl_buf_trans(struct intel_encoder *encoder, int type, int rate,
10689fa67699SJosé Roberto de Souza 		      int *n_entries)
10699fa67699SJosé Roberto de Souza {
10709fa67699SJosé Roberto de Souza 	if (type == INTEL_OUTPUT_HDMI) {
10719fa67699SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_dkl_phy_hdmi_ddi_trans);
10729fa67699SJosé Roberto de Souza 		return tgl_dkl_phy_hdmi_ddi_trans;
10739fa67699SJosé Roberto de Souza 	} else if (rate > 270000) {
10749fa67699SJosé Roberto de Souza 		*n_entries = ARRAY_SIZE(tgl_dkl_phy_dp_ddi_trans_hbr2);
10759fa67699SJosé Roberto de Souza 		return tgl_dkl_phy_dp_ddi_trans_hbr2;
10769fa67699SJosé Roberto de Souza 	}
10779fa67699SJosé Roberto de Souza 
10789fa67699SJosé Roberto de Souza 	*n_entries = ARRAY_SIZE(tgl_dkl_phy_dp_ddi_trans);
10799fa67699SJosé Roberto de Souza 	return tgl_dkl_phy_dp_ddi_trans;
10809fa67699SJosé Roberto de Souza }
10819fa67699SJosé Roberto de Souza 
10820aed3bdeSJani Nikula static int intel_ddi_hdmi_level(struct intel_encoder *encoder)
1083379bc100SJani Nikula {
10840aed3bdeSJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1085379bc100SJani Nikula 	int n_entries, level, default_entry;
10860aed3bdeSJani Nikula 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1087379bc100SJani Nikula 
1088978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
1089978c3e53SClinton A Taylor 		if (intel_phy_is_combo(dev_priv, phy))
1090*a8143150SJosé Roberto de Souza 			tgl_get_combo_buf_trans(encoder, INTEL_OUTPUT_HDMI,
1091978c3e53SClinton A Taylor 						0, &n_entries);
1092978c3e53SClinton A Taylor 		else
1093*a8143150SJosé Roberto de Souza 			tgl_get_dkl_buf_trans(encoder, INTEL_OUTPUT_HDMI, 0,
10949fa67699SJosé Roberto de Souza 					      &n_entries);
1095978c3e53SClinton A Taylor 		default_entry = n_entries - 1;
1096978c3e53SClinton A Taylor 	} else if (INTEL_GEN(dev_priv) == 11) {
1097d8fe2ab6SMatt Roper 		if (intel_phy_is_combo(dev_priv, phy))
1098*a8143150SJosé Roberto de Souza 			icl_get_combo_buf_trans(encoder, INTEL_OUTPUT_HDMI,
1099379bc100SJani Nikula 						0, &n_entries);
1100379bc100SJani Nikula 		else
1101*a8143150SJosé Roberto de Souza 			icl_get_mg_buf_trans(encoder, INTEL_OUTPUT_HDMI, 0,
11029f7ffa29SJosé Roberto de Souza 					     &n_entries);
1103379bc100SJani Nikula 		default_entry = n_entries - 1;
1104379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
1105*a8143150SJosé Roberto de Souza 		cnl_get_buf_trans_hdmi(encoder, &n_entries);
1106379bc100SJani Nikula 		default_entry = n_entries - 1;
1107379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
1108*a8143150SJosé Roberto de Souza 		bxt_get_buf_trans_hdmi(encoder, &n_entries);
1109379bc100SJani Nikula 		default_entry = n_entries - 1;
1110379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
1111*a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1112379bc100SJani Nikula 		default_entry = 8;
1113379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
1114*a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1115379bc100SJani Nikula 		default_entry = 7;
1116379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
1117*a8143150SJosé Roberto de Souza 		intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1118379bc100SJani Nikula 		default_entry = 6;
1119379bc100SJani Nikula 	} else {
11201de143ccSPankaj Bharadiya 		drm_WARN(&dev_priv->drm, 1, "ddi translation table missing\n");
1121379bc100SJani Nikula 		return 0;
1122379bc100SJani Nikula 	}
1123379bc100SJani Nikula 
11241de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, n_entries == 0))
1125379bc100SJani Nikula 		return 0;
11267a0073d6SJani Nikula 
11270aed3bdeSJani Nikula 	level = intel_bios_hdmi_level_shift(encoder);
11280aed3bdeSJani Nikula 	if (level < 0)
11297a0073d6SJani Nikula 		level = default_entry;
11307a0073d6SJani Nikula 
11311de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1132379bc100SJani Nikula 		level = n_entries - 1;
1133379bc100SJani Nikula 
1134379bc100SJani Nikula 	return level;
1135379bc100SJani Nikula }
1136379bc100SJani Nikula 
1137379bc100SJani Nikula /*
1138379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
1139379bc100SJani Nikula  * values in advance. This function programs the correct values for
1140379bc100SJani Nikula  * DP/eDP/FDI use cases.
1141379bc100SJani Nikula  */
1142379bc100SJani Nikula static void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
1143379bc100SJani Nikula 					 const struct intel_crtc_state *crtc_state)
1144379bc100SJani Nikula {
1145379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1146379bc100SJani Nikula 	u32 iboost_bit = 0;
1147379bc100SJani Nikula 	int i, n_entries;
1148379bc100SJani Nikula 	enum port port = encoder->port;
1149379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
1150379bc100SJani Nikula 
1151379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
1152379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
1153379bc100SJani Nikula 							       &n_entries);
1154379bc100SJani Nikula 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
1155*a8143150SJosé Roberto de Souza 		ddi_translations = intel_ddi_get_buf_trans_edp(encoder, port,
1156379bc100SJani Nikula 							       &n_entries);
1157379bc100SJani Nikula 	else
1158*a8143150SJosé Roberto de Souza 		ddi_translations = intel_ddi_get_buf_trans_dp(encoder, port,
1159379bc100SJani Nikula 							      &n_entries);
1160379bc100SJani Nikula 
1161379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
1162605a1872SJani Nikula 	if (IS_GEN9_BC(dev_priv) && intel_bios_dp_boost_level(encoder))
1163379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
1164379bc100SJani Nikula 
1165379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
1166f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
1167379bc100SJani Nikula 			       ddi_translations[i].trans1 | iboost_bit);
1168f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
1169379bc100SJani Nikula 			       ddi_translations[i].trans2);
1170379bc100SJani Nikula 	}
1171379bc100SJani Nikula }
1172379bc100SJani Nikula 
1173379bc100SJani Nikula /*
1174379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
1175379bc100SJani Nikula  * values in advance. This function programs the correct values for
1176379bc100SJani Nikula  * HDMI/DVI use cases.
1177379bc100SJani Nikula  */
1178379bc100SJani Nikula static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
1179379bc100SJani Nikula 					   int level)
1180379bc100SJani Nikula {
1181379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1182379bc100SJani Nikula 	u32 iboost_bit = 0;
1183379bc100SJani Nikula 	int n_entries;
1184379bc100SJani Nikula 	enum port port = encoder->port;
1185379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
1186379bc100SJani Nikula 
1187*a8143150SJosé Roberto de Souza 	ddi_translations = intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
1188379bc100SJani Nikula 
11891de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
1190379bc100SJani Nikula 		return;
11911de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1192379bc100SJani Nikula 		level = n_entries - 1;
1193379bc100SJani Nikula 
1194379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
119501a60883SJani Nikula 	if (IS_GEN9_BC(dev_priv) && intel_bios_hdmi_boost_level(encoder))
1196379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
1197379bc100SJani Nikula 
1198379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
1199f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
1200379bc100SJani Nikula 		       ddi_translations[level].trans1 | iboost_bit);
1201f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
1202379bc100SJani Nikula 		       ddi_translations[level].trans2);
1203379bc100SJani Nikula }
1204379bc100SJani Nikula 
1205379bc100SJani Nikula static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
1206379bc100SJani Nikula 				    enum port port)
1207379bc100SJani Nikula {
12085a2ad99bSManasi Navare 	if (IS_BROXTON(dev_priv)) {
12095a2ad99bSManasi Navare 		udelay(16);
1210379bc100SJani Nikula 		return;
1211379bc100SJani Nikula 	}
12125a2ad99bSManasi Navare 
12135a2ad99bSManasi Navare 	if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
12145a2ad99bSManasi Navare 			 DDI_BUF_IS_IDLE), 8))
12155a2ad99bSManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
121647bdb1caSJani Nikula 			port_name(port));
1217379bc100SJani Nikula }
1218379bc100SJani Nikula 
1219e828da30SManasi Navare static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
1220e828da30SManasi Navare 				      enum port port)
1221e828da30SManasi Navare {
1222e828da30SManasi Navare 	/* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
1223e828da30SManasi Navare 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEMINILAKE(dev_priv)) {
1224e828da30SManasi Navare 		usleep_range(518, 1000);
1225e828da30SManasi Navare 		return;
1226e828da30SManasi Navare 	}
1227e828da30SManasi Navare 
1228e828da30SManasi Navare 	if (wait_for_us(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1229e828da30SManasi Navare 			  DDI_BUF_IS_IDLE), 500))
1230e828da30SManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
1231e828da30SManasi Navare 			port_name(port));
1232e828da30SManasi Navare }
1233e828da30SManasi Navare 
1234379bc100SJani Nikula static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
1235379bc100SJani Nikula {
1236379bc100SJani Nikula 	switch (pll->info->id) {
1237379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
1238379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
1239379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
1240379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
1241379bc100SJani Nikula 	case DPLL_ID_SPLL:
1242379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
1243379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
1244379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
1245379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
1246379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
1247379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
1248379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
1249379bc100SJani Nikula 	default:
1250379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
1251379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
1252379bc100SJani Nikula 	}
1253379bc100SJani Nikula }
1254379bc100SJani Nikula 
1255379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
1256379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
1257379bc100SJani Nikula {
1258379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1259379bc100SJani Nikula 	int clock = crtc_state->port_clock;
1260379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
1261379bc100SJani Nikula 
1262379bc100SJani Nikula 	switch (id) {
1263379bc100SJani Nikula 	default:
1264379bc100SJani Nikula 		/*
1265379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
1266379bc100SJani Nikula 		 * here, so do warn if this get passed in
1267379bc100SJani Nikula 		 */
1268379bc100SJani Nikula 		MISSING_CASE(id);
1269379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
1270379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
1271379bc100SJani Nikula 		switch (clock) {
1272379bc100SJani Nikula 		case 162000:
1273379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
1274379bc100SJani Nikula 		case 270000:
1275379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
1276379bc100SJani Nikula 		case 540000:
1277379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
1278379bc100SJani Nikula 		case 810000:
1279379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
1280379bc100SJani Nikula 		default:
1281379bc100SJani Nikula 			MISSING_CASE(clock);
1282379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
1283379bc100SJani Nikula 		}
1284379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
1285379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
1286379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
1287379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
12886677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
12896677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
1290379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
1291379bc100SJani Nikula 	}
1292379bc100SJani Nikula }
1293379bc100SJani Nikula 
1294379bc100SJani Nikula /* Starting with Haswell, different DDI ports can work in FDI mode for
1295379bc100SJani Nikula  * connection to the PCH-located connectors. For this, it is necessary to train
1296379bc100SJani Nikula  * both the DDI port and PCH receiver for the desired DDI buffer settings.
1297379bc100SJani Nikula  *
1298379bc100SJani Nikula  * The recommended port to work in FDI mode is DDI E, which we use here. Also,
1299379bc100SJani Nikula  * please note that when FDI mode is active on DDI E, it shares 2 lines with
1300379bc100SJani Nikula  * DDI A (which is used for eDP)
1301379bc100SJani Nikula  */
1302379bc100SJani Nikula 
13036a6d79deSVille Syrjälä void hsw_fdi_link_train(struct intel_encoder *encoder,
1304379bc100SJani Nikula 			const struct intel_crtc_state *crtc_state)
1305379bc100SJani Nikula {
13066a6d79deSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
13076a6d79deSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1308379bc100SJani Nikula 	u32 temp, i, rx_ctl_val, ddi_pll_sel;
1309379bc100SJani Nikula 
1310379bc100SJani Nikula 	intel_prepare_dp_ddi_buffers(encoder, crtc_state);
1311379bc100SJani Nikula 
1312379bc100SJani Nikula 	/* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
1313379bc100SJani Nikula 	 * mode set "sequence for CRT port" document:
1314379bc100SJani Nikula 	 * - TP1 to TP2 time with the default value
1315379bc100SJani Nikula 	 * - FDI delay to 90h
1316379bc100SJani Nikula 	 *
1317379bc100SJani Nikula 	 * WaFDIAutoLinkSetTimingOverrride:hsw
1318379bc100SJani Nikula 	 */
1319f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A),
1320f7960e7fSJani Nikula 		       FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2) | FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
1321379bc100SJani Nikula 
1322379bc100SJani Nikula 	/* Enable the PCH Receiver FDI PLL */
1323379bc100SJani Nikula 	rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
1324379bc100SJani Nikula 		     FDI_RX_PLL_ENABLE |
1325379bc100SJani Nikula 		     FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
1326f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1327f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, FDI_RX_CTL(PIPE_A));
1328379bc100SJani Nikula 	udelay(220);
1329379bc100SJani Nikula 
1330379bc100SJani Nikula 	/* Switch from Rawclk to PCDclk */
1331379bc100SJani Nikula 	rx_ctl_val |= FDI_PCDCLK;
1332f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1333379bc100SJani Nikula 
1334379bc100SJani Nikula 	/* Configure Port Clock Select */
1335379bc100SJani Nikula 	ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
1336f7960e7fSJani Nikula 	intel_de_write(dev_priv, PORT_CLK_SEL(PORT_E), ddi_pll_sel);
13371de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, ddi_pll_sel != PORT_CLK_SEL_SPLL);
1338379bc100SJani Nikula 
1339379bc100SJani Nikula 	/* Start the training iterating through available voltages and emphasis,
1340379bc100SJani Nikula 	 * testing each value twice. */
1341379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
1342379bc100SJani Nikula 		/* Configure DP_TP_CTL with auto-training */
1343f7960e7fSJani Nikula 		intel_de_write(dev_priv, DP_TP_CTL(PORT_E),
13447db8736dSVille Syrjälä 			       DP_TP_CTL_FDI_AUTOTRAIN |
13457db8736dSVille Syrjälä 			       DP_TP_CTL_ENHANCED_FRAME_ENABLE |
13467db8736dSVille Syrjälä 			       DP_TP_CTL_LINK_TRAIN_PAT1 |
13477db8736dSVille Syrjälä 			       DP_TP_CTL_ENABLE);
1348379bc100SJani Nikula 
1349379bc100SJani Nikula 		/* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
1350379bc100SJani Nikula 		 * DDI E does not support port reversal, the functionality is
1351379bc100SJani Nikula 		 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
1352379bc100SJani Nikula 		 * port reversal bit */
1353f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(PORT_E),
1354f7960e7fSJani Nikula 			       DDI_BUF_CTL_ENABLE | ((crtc_state->fdi_lanes - 1) << 1) | DDI_BUF_TRANS_SELECT(i / 2));
1355f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DDI_BUF_CTL(PORT_E));
1356379bc100SJani Nikula 
1357379bc100SJani Nikula 		udelay(600);
1358379bc100SJani Nikula 
1359379bc100SJani Nikula 		/* Program PCH FDI Receiver TU */
1360f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
1361379bc100SJani Nikula 
1362379bc100SJani Nikula 		/* Enable PCH FDI Receiver with auto-training */
1363379bc100SJani Nikula 		rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
1364f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), rx_ctl_val);
1365f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_CTL(PIPE_A));
1366379bc100SJani Nikula 
1367379bc100SJani Nikula 		/* Wait for FDI receiver lane calibration */
1368379bc100SJani Nikula 		udelay(30);
1369379bc100SJani Nikula 
1370379bc100SJani Nikula 		/* Unset FDI_RX_MISC pwrdn lanes */
1371f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
1372379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1373f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), temp);
1374f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_MISC(PIPE_A));
1375379bc100SJani Nikula 
1376379bc100SJani Nikula 		/* Wait for FDI auto training time */
1377379bc100SJani Nikula 		udelay(5);
1378379bc100SJani Nikula 
1379f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DP_TP_STATUS(PORT_E));
1380379bc100SJani Nikula 		if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
138147bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
138247bdb1caSJani Nikula 				    "FDI link training done on step %d\n", i);
1383379bc100SJani Nikula 			break;
1384379bc100SJani Nikula 		}
1385379bc100SJani Nikula 
1386379bc100SJani Nikula 		/*
1387379bc100SJani Nikula 		 * Leave things enabled even if we failed to train FDI.
1388379bc100SJani Nikula 		 * Results in less fireworks from the state checker.
1389379bc100SJani Nikula 		 */
1390379bc100SJani Nikula 		if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
139147bdb1caSJani Nikula 			drm_err(&dev_priv->drm, "FDI link training failed!\n");
1392379bc100SJani Nikula 			break;
1393379bc100SJani Nikula 		}
1394379bc100SJani Nikula 
1395379bc100SJani Nikula 		rx_ctl_val &= ~FDI_RX_ENABLE;
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 
1399f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_E));
1400379bc100SJani Nikula 		temp &= ~DDI_BUF_CTL_ENABLE;
1401f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(PORT_E), temp);
1402f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DDI_BUF_CTL(PORT_E));
1403379bc100SJani Nikula 
1404379bc100SJani Nikula 		/* Disable DP_TP_CTL and FDI_RX_CTL and retry */
1405f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, DP_TP_CTL(PORT_E));
1406379bc100SJani Nikula 		temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1407379bc100SJani Nikula 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1408f7960e7fSJani Nikula 		intel_de_write(dev_priv, DP_TP_CTL(PORT_E), temp);
1409f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, DP_TP_CTL(PORT_E));
1410379bc100SJani Nikula 
1411379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, PORT_E);
1412379bc100SJani Nikula 
1413379bc100SJani Nikula 		/* Reset FDI_RX_MISC pwrdn lanes */
1414f7960e7fSJani Nikula 		temp = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
1415379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1416379bc100SJani Nikula 		temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1417f7960e7fSJani Nikula 		intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), temp);
1418f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, FDI_RX_MISC(PIPE_A));
1419379bc100SJani Nikula 	}
1420379bc100SJani Nikula 
1421379bc100SJani Nikula 	/* Enable normal pixel sending for FDI */
1422f7960e7fSJani Nikula 	intel_de_write(dev_priv, DP_TP_CTL(PORT_E),
14237db8736dSVille Syrjälä 		       DP_TP_CTL_FDI_AUTOTRAIN |
14247db8736dSVille Syrjälä 		       DP_TP_CTL_LINK_TRAIN_NORMAL |
14257db8736dSVille Syrjälä 		       DP_TP_CTL_ENHANCED_FRAME_ENABLE |
14267db8736dSVille Syrjälä 		       DP_TP_CTL_ENABLE);
1427379bc100SJani Nikula }
1428379bc100SJani Nikula 
1429379bc100SJani Nikula static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
1430379bc100SJani Nikula {
1431b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
14327801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1433379bc100SJani Nikula 
14347801f3b7SLucas De Marchi 	intel_dp->DP = dig_port->saved_port_bits |
1435379bc100SJani Nikula 		DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
1436379bc100SJani Nikula 	intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
1437379bc100SJani Nikula }
1438379bc100SJani Nikula 
1439379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
1440379bc100SJani Nikula 				 enum port port)
1441379bc100SJani Nikula {
1442f7960e7fSJani Nikula 	u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
1443379bc100SJani Nikula 
1444379bc100SJani Nikula 	switch (val) {
1445379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
1446379bc100SJani Nikula 		return 0;
1447379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
1448379bc100SJani Nikula 		return 162000;
1449379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
1450379bc100SJani Nikula 		return 270000;
1451379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
1452379bc100SJani Nikula 		return 540000;
1453379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
1454379bc100SJani Nikula 		return 810000;
1455379bc100SJani Nikula 	default:
1456379bc100SJani Nikula 		MISSING_CASE(val);
1457379bc100SJani Nikula 		return 0;
1458379bc100SJani Nikula 	}
1459379bc100SJani Nikula }
1460379bc100SJani Nikula 
1461379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
1462379bc100SJani Nikula {
1463379bc100SJani Nikula 	int dotclock;
1464379bc100SJani Nikula 
1465379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
1466379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1467379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
1468379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
1469379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1470379bc100SJani Nikula 						    &pipe_config->dp_m_n);
14712969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
14722969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
1473379bc100SJani Nikula 	else
1474379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
1475379bc100SJani Nikula 
1476379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
1477379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
1478379bc100SJani Nikula 		dotclock *= 2;
1479379bc100SJani Nikula 
1480379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
1481379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
1482379bc100SJani Nikula 
14831326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
1484379bc100SJani Nikula }
1485379bc100SJani Nikula 
1486379bc100SJani Nikula static void intel_ddi_clock_get(struct intel_encoder *encoder,
1487379bc100SJani Nikula 				struct intel_crtc_state *pipe_config)
1488379bc100SJani Nikula {
1489379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
149056ed441aSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1491379bc100SJani Nikula 
149256ed441aSMatt Roper 	if (intel_phy_is_tc(dev_priv, phy) &&
149345e4728bSImre Deak 	    intel_get_shared_dpll_id(dev_priv, pipe_config->shared_dpll) ==
149445e4728bSImre Deak 	    DPLL_ID_ICL_TBTPLL)
149545e4728bSImre Deak 		pipe_config->port_clock = icl_calc_tbt_pll_link(dev_priv,
149645e4728bSImre Deak 								encoder->port);
149745e4728bSImre Deak 	else
1498b953eb21SImre Deak 		pipe_config->port_clock =
1499b953eb21SImre Deak 			intel_dpll_get_freq(dev_priv, pipe_config->shared_dpll);
150045e4728bSImre Deak 
150145e4728bSImre Deak 	ddi_dotclock_get(pipe_config);
1502379bc100SJani Nikula }
1503379bc100SJani Nikula 
15040c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
15050c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
1506379bc100SJani Nikula {
15072225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1508379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1509379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1510379bc100SJani Nikula 	u32 temp;
1511379bc100SJani Nikula 
1512379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
1513379bc100SJani Nikula 		return;
1514379bc100SJani Nikula 
15151de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
1516379bc100SJani Nikula 
15173e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
1518379bc100SJani Nikula 
1519379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1520379bc100SJani Nikula 	case 18:
15213e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
1522379bc100SJani Nikula 		break;
1523379bc100SJani Nikula 	case 24:
15243e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
1525379bc100SJani Nikula 		break;
1526379bc100SJani Nikula 	case 30:
15273e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
1528379bc100SJani Nikula 		break;
1529379bc100SJani Nikula 	case 36:
15303e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
1531379bc100SJani Nikula 		break;
1532379bc100SJani Nikula 	default:
1533379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
1534379bc100SJani Nikula 		break;
1535379bc100SJani Nikula 	}
1536379bc100SJani Nikula 
1537cae154fcSVille Syrjälä 	/* nonsense combination */
15381de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
1539cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
1540cae154fcSVille Syrjälä 
1541cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
15423e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
1543cae154fcSVille Syrjälä 
1544379bc100SJani Nikula 	/*
1545379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
1546379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
1547646d3dc8SVille Syrjälä 	 * colorspace information.
1548379bc100SJani Nikula 	 */
1549379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
15503e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
1551646d3dc8SVille Syrjälä 
1552379bc100SJani Nikula 	/*
1553379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
1554379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
15550c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
15560c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
1557379bc100SJani Nikula 	 */
1558bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
15593e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
15600c06fa15SGwan-gyeong Mun 
1561f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
1562379bc100SJani Nikula }
1563379bc100SJani Nikula 
1564dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
1565dc5b8ed5SVille Syrjälä {
1566dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
1567dc5b8ed5SVille Syrjälä 		return 0;
1568dc5b8ed5SVille Syrjälä 	else
1569dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
1570dc5b8ed5SVille Syrjälä }
1571dc5b8ed5SVille Syrjälä 
157299389390SJosé Roberto de Souza /*
157399389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
157499389390SJosé Roberto de Souza  *
157599389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
157699389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
157799389390SJosé Roberto de Souza  */
157899389390SJosé Roberto de Souza static u32
1579eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
1580eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
1581379bc100SJani Nikula {
15822225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1583379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1584379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
1585379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1586379bc100SJani Nikula 	enum port port = encoder->port;
1587379bc100SJani Nikula 	u32 temp;
1588379bc100SJani Nikula 
1589379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1590379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
1591df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12)
1592df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
1593df16b636SMahesh Kumar 	else
1594379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
1595379bc100SJani Nikula 
1596379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1597379bc100SJani Nikula 	case 18:
1598379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
1599379bc100SJani Nikula 		break;
1600379bc100SJani Nikula 	case 24:
1601379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
1602379bc100SJani Nikula 		break;
1603379bc100SJani Nikula 	case 30:
1604379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
1605379bc100SJani Nikula 		break;
1606379bc100SJani Nikula 	case 36:
1607379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
1608379bc100SJani Nikula 		break;
1609379bc100SJani Nikula 	default:
1610379bc100SJani Nikula 		BUG();
1611379bc100SJani Nikula 	}
1612379bc100SJani Nikula 
16131326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
1614379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
16151326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
1616379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
1617379bc100SJani Nikula 
1618379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
1619379bc100SJani Nikula 		switch (pipe) {
1620379bc100SJani Nikula 		case PIPE_A:
1621379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
1622379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
1623379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
1624379bc100SJani Nikula 			 * support). */
1625379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
1626379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1627379bc100SJani Nikula 			else
1628379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
1629379bc100SJani Nikula 			break;
1630379bc100SJani Nikula 		case PIPE_B:
1631379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1632379bc100SJani Nikula 			break;
1633379bc100SJani Nikula 		case PIPE_C:
1634379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1635379bc100SJani Nikula 			break;
1636379bc100SJani Nikula 		default:
1637379bc100SJani Nikula 			BUG();
1638379bc100SJani Nikula 			break;
1639379bc100SJani Nikula 		}
1640379bc100SJani Nikula 	}
1641379bc100SJani Nikula 
1642379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1643379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
1644379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
1645379bc100SJani Nikula 		else
1646379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
1647379bc100SJani Nikula 
1648379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
1649379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
1650379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
1651379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
1652379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1653379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI;
1654379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
1655379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
1656379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1657379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1658b3545e08SLucas De Marchi 
16596671c367SJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12) {
16606671c367SJosé Roberto de Souza 			enum transcoder master;
16616671c367SJosé Roberto de Souza 
16626671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
16631de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
16641de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
16656671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
16666671c367SJosé Roberto de Souza 		}
1667379bc100SJani Nikula 	} else {
1668379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1669379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1670379bc100SJani Nikula 	}
1671379bc100SJani Nikula 
1672dc5b8ed5SVille Syrjälä 	if (IS_GEN_RANGE(dev_priv, 8, 10) &&
1673dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
1674dc5b8ed5SVille Syrjälä 		u8 master_select =
1675dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
1676dc5b8ed5SVille Syrjälä 
1677dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
1678dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
1679dc5b8ed5SVille Syrjälä 	}
1680dc5b8ed5SVille Syrjälä 
168199389390SJosé Roberto de Souza 	return temp;
168299389390SJosé Roberto de Souza }
168399389390SJosé Roberto de Souza 
1684eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
1685eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
168699389390SJosé Roberto de Souza {
16872225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
168899389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
168999389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
169099389390SJosé Roberto de Souza 
1691589a4cd6SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
1692589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
1693589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
1694589a4cd6SVille Syrjälä 
1695589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
1696dc5b8ed5SVille Syrjälä 			u8 master_select =
1697dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
1698589a4cd6SVille Syrjälä 
1699589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
1700d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
1701589a4cd6SVille Syrjälä 		}
1702589a4cd6SVille Syrjälä 
1703589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
1704589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
1705589a4cd6SVille Syrjälä 	}
1706589a4cd6SVille Syrjälä 
1707580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
1708580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
1709580fbdc5SImre Deak 							     crtc_state));
171099389390SJosé Roberto de Souza }
171199389390SJosé Roberto de Souza 
171299389390SJosé Roberto de Souza /*
171399389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
171499389390SJosé Roberto de Souza  * bit.
171599389390SJosé Roberto de Souza  */
171699389390SJosé Roberto de Souza static void
1717eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
1718eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
171999389390SJosé Roberto de Souza {
17202225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
172199389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
172299389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1723589a4cd6SVille Syrjälä 	u32 ctl;
172499389390SJosé Roberto de Souza 
1725eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
1726589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
1727589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
1728379bc100SJani Nikula }
1729379bc100SJani Nikula 
1730379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
1731379bc100SJani Nikula {
17322225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1733379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1734379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1735589a4cd6SVille Syrjälä 	u32 ctl;
1736c59053dcSJosé Roberto de Souza 
1737589a4cd6SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11)
1738589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
1739589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
1740589a4cd6SVille Syrjälä 
1741589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
1742dc5b8ed5SVille Syrjälä 
1743589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
1744379bc100SJani Nikula 
1745dc5b8ed5SVille Syrjälä 	if (IS_GEN_RANGE(dev_priv, 8, 10))
1746dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
1747dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
1748dc5b8ed5SVille Syrjälä 
1749df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12) {
1750919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
1751589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
1752919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
1753919e4f07SJosé Roberto de Souza 		}
1754df16b636SMahesh Kumar 	} else {
1755589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
1756df16b636SMahesh Kumar 	}
1757dc5b8ed5SVille Syrjälä 
1758589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
1759379bc100SJani Nikula 
1760379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
1761379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
176247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
176347bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
1764379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
1765379bc100SJani Nikula 		msleep(100);
1766379bc100SJani Nikula 	}
1767379bc100SJani Nikula }
1768379bc100SJani Nikula 
1769379bc100SJani Nikula int intel_ddi_toggle_hdcp_signalling(struct intel_encoder *intel_encoder,
1770379bc100SJani Nikula 				     bool enable)
1771379bc100SJani Nikula {
1772379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
1773379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1774379bc100SJani Nikula 	intel_wakeref_t wakeref;
1775379bc100SJani Nikula 	enum pipe pipe = 0;
1776379bc100SJani Nikula 	int ret = 0;
1777379bc100SJani Nikula 	u32 tmp;
1778379bc100SJani Nikula 
1779379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1780379bc100SJani Nikula 						     intel_encoder->power_domain);
17811de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
1782379bc100SJani Nikula 		return -ENXIO;
1783379bc100SJani Nikula 
17841de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev,
17851de143ccSPankaj Bharadiya 			!intel_encoder->get_hw_state(intel_encoder, &pipe))) {
1786379bc100SJani Nikula 		ret = -EIO;
1787379bc100SJani Nikula 		goto out;
1788379bc100SJani Nikula 	}
1789379bc100SJani Nikula 
1790f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(pipe));
1791379bc100SJani Nikula 	if (enable)
1792379bc100SJani Nikula 		tmp |= TRANS_DDI_HDCP_SIGNALLING;
1793379bc100SJani Nikula 	else
1794379bc100SJani Nikula 		tmp &= ~TRANS_DDI_HDCP_SIGNALLING;
1795f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(pipe), tmp);
1796379bc100SJani Nikula out:
1797379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
1798379bc100SJani Nikula 	return ret;
1799379bc100SJani Nikula }
1800379bc100SJani Nikula 
1801379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1802379bc100SJani Nikula {
1803379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
1804379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1805fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
1806379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
1807379bc100SJani Nikula 	enum port port = encoder->port;
1808379bc100SJani Nikula 	enum transcoder cpu_transcoder;
1809379bc100SJani Nikula 	intel_wakeref_t wakeref;
1810379bc100SJani Nikula 	enum pipe pipe = 0;
1811379bc100SJani Nikula 	u32 tmp;
1812379bc100SJani Nikula 	bool ret;
1813379bc100SJani Nikula 
1814379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1815379bc100SJani Nikula 						     encoder->power_domain);
1816379bc100SJani Nikula 	if (!wakeref)
1817379bc100SJani Nikula 		return false;
1818379bc100SJani Nikula 
1819379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
1820379bc100SJani Nikula 		ret = false;
1821379bc100SJani Nikula 		goto out;
1822379bc100SJani Nikula 	}
1823379bc100SJani Nikula 
182410cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
1825379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
1826379bc100SJani Nikula 	else
1827379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
1828379bc100SJani Nikula 
1829f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
1830379bc100SJani Nikula 
1831379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1832379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
1833379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
1834379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
1835379bc100SJani Nikula 		break;
1836379bc100SJani Nikula 
1837379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
1838379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
1839379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
1840379bc100SJani Nikula 		break;
1841379bc100SJani Nikula 
1842379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
1843379bc100SJani Nikula 		/* if the transcoder is in MST state then
1844379bc100SJani Nikula 		 * connector isn't connected */
1845379bc100SJani Nikula 		ret = false;
1846379bc100SJani Nikula 		break;
1847379bc100SJani Nikula 
1848379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
1849379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_VGA;
1850379bc100SJani Nikula 		break;
1851379bc100SJani Nikula 
1852379bc100SJani Nikula 	default:
1853379bc100SJani Nikula 		ret = false;
1854379bc100SJani Nikula 		break;
1855379bc100SJani Nikula 	}
1856379bc100SJani Nikula 
1857379bc100SJani Nikula out:
1858379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1859379bc100SJani Nikula 
1860379bc100SJani Nikula 	return ret;
1861379bc100SJani Nikula }
1862379bc100SJani Nikula 
1863379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
1864379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
1865379bc100SJani Nikula {
1866379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
1867379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1868379bc100SJani Nikula 	enum port port = encoder->port;
1869379bc100SJani Nikula 	intel_wakeref_t wakeref;
1870379bc100SJani Nikula 	enum pipe p;
1871379bc100SJani Nikula 	u32 tmp;
1872379bc100SJani Nikula 	u8 mst_pipe_mask;
1873379bc100SJani Nikula 
1874379bc100SJani Nikula 	*pipe_mask = 0;
1875379bc100SJani Nikula 	*is_dp_mst = false;
1876379bc100SJani Nikula 
1877379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1878379bc100SJani Nikula 						     encoder->power_domain);
1879379bc100SJani Nikula 	if (!wakeref)
1880379bc100SJani Nikula 		return;
1881379bc100SJani Nikula 
1882f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
1883379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
1884379bc100SJani Nikula 		goto out;
1885379bc100SJani Nikula 
188610cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
1887f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
1888f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
1889379bc100SJani Nikula 
1890379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1891379bc100SJani Nikula 		default:
1892379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
1893379bc100SJani Nikula 			/* fallthrough */
1894379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
1895379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
1896379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
1897379bc100SJani Nikula 			break;
1898379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
1899379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
1900379bc100SJani Nikula 			break;
1901379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
1902379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
1903379bc100SJani Nikula 			break;
1904379bc100SJani Nikula 		}
1905379bc100SJani Nikula 
1906379bc100SJani Nikula 		goto out;
1907379bc100SJani Nikula 	}
1908379bc100SJani Nikula 
1909379bc100SJani Nikula 	mst_pipe_mask = 0;
1910379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
1911379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
1912df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
19136aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
19146aa3bef1SJosé Roberto de Souza 
19156aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
19166aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
19176aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
19186aa3bef1SJosé Roberto de Souza 			continue;
1919df16b636SMahesh Kumar 
1920df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12) {
1921df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
1922df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
1923df16b636SMahesh Kumar 		} else {
1924df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
1925df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
1926df16b636SMahesh Kumar 		}
1927379bc100SJani Nikula 
1928f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
1929f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
19306aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
19316aa3bef1SJosé Roberto de Souza 					trans_wakeref);
1932379bc100SJani Nikula 
1933df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
1934379bc100SJani Nikula 			continue;
1935379bc100SJani Nikula 
1936379bc100SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
1937379bc100SJani Nikula 		    TRANS_DDI_MODE_SELECT_DP_MST)
1938379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
1939379bc100SJani Nikula 
1940379bc100SJani Nikula 		*pipe_mask |= BIT(p);
1941379bc100SJani Nikula 	}
1942379bc100SJani Nikula 
1943379bc100SJani Nikula 	if (!*pipe_mask)
194447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
194547bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
194666a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
1947379bc100SJani Nikula 
1948379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
194947bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
195047bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
195166a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
195266a990ddSVille Syrjälä 			    *pipe_mask);
1953379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
1954379bc100SJani Nikula 	}
1955379bc100SJani Nikula 
1956379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
195747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
195847bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
195966a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
196066a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
1961379bc100SJani Nikula 	else
1962379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
1963379bc100SJani Nikula 
1964379bc100SJani Nikula out:
1965379bc100SJani Nikula 	if (*pipe_mask && IS_GEN9_LP(dev_priv)) {
1966f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
1967379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
1968379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
1969379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
197047bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
197147bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
197247bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
1973379bc100SJani Nikula 	}
1974379bc100SJani Nikula 
1975379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1976379bc100SJani Nikula }
1977379bc100SJani Nikula 
1978379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1979379bc100SJani Nikula 			    enum pipe *pipe)
1980379bc100SJani Nikula {
1981379bc100SJani Nikula 	u8 pipe_mask;
1982379bc100SJani Nikula 	bool is_mst;
1983379bc100SJani Nikula 
1984379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
1985379bc100SJani Nikula 
1986379bc100SJani Nikula 	if (is_mst || !pipe_mask)
1987379bc100SJani Nikula 		return false;
1988379bc100SJani Nikula 
1989379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
1990379bc100SJani Nikula 
1991379bc100SJani Nikula 	return true;
1992379bc100SJani Nikula }
1993379bc100SJani Nikula 
199481b55ef1SJani Nikula static enum intel_display_power_domain
1995379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
1996379bc100SJani Nikula {
1997379bc100SJani Nikula 	/* CNL+ HW requires corresponding AUX IOs to be powered up for PSR with
1998379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
1999379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
2000379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
2001379bc100SJani Nikula 	 * states enabled.
2002379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
2003379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
2004379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
2005379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
2006379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
2007379bc100SJani Nikula 	 * returns the correct domain for other ports too.
2008379bc100SJani Nikula 	 */
2009379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
2010379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
2011379bc100SJani Nikula }
2012379bc100SJani Nikula 
2013379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
2014379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
2015379bc100SJani Nikula {
2016379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2017379bc100SJani Nikula 	struct intel_digital_port *dig_port;
2018d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2019379bc100SJani Nikula 
2020379bc100SJani Nikula 	/*
2021379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
2022379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
2023379bc100SJani Nikula 	 * hook.
2024379bc100SJani Nikula 	 */
20251de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
20261de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
2027379bc100SJani Nikula 		return;
2028379bc100SJani Nikula 
2029b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
2030f77a2db2SImre Deak 
2031f77a2db2SImre Deak 	if (!intel_phy_is_tc(dev_priv, phy) ||
2032f77a2db2SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
2033f77a2db2SImre Deak 		intel_display_power_get(dev_priv,
2034f77a2db2SImre Deak 					dig_port->ddi_io_power_domain);
2035379bc100SJani Nikula 
2036379bc100SJani Nikula 	/*
2037379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
2038379bc100SJani Nikula 	 * ports.
2039379bc100SJani Nikula 	 */
2040379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
2041d8fe2ab6SMatt Roper 	    intel_phy_is_tc(dev_priv, phy))
2042379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2043379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
2044379bc100SJani Nikula 
2045379bc100SJani Nikula 	/*
2046379bc100SJani Nikula 	 * VDSC power is needed when DSC is enabled
2047379bc100SJani Nikula 	 */
2048010663a6SJani Nikula 	if (crtc_state->dsc.compression_enable)
2049379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2050379bc100SJani Nikula 					intel_dsc_power_domain(crtc_state));
2051379bc100SJani Nikula }
2052379bc100SJani Nikula 
205302a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
205402a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
2055379bc100SJani Nikula {
20562225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2057379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2058379bc100SJani Nikula 	enum port port = encoder->port;
2059379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2060379bc100SJani Nikula 
2061df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2062df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2063f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2064f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2065df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_PORT(port));
2066df16b636SMahesh Kumar 		else
2067f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2068f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2069379bc100SJani Nikula 				       TRANS_CLK_SEL_PORT(port));
2070379bc100SJani Nikula 	}
2071df16b636SMahesh Kumar }
2072379bc100SJani Nikula 
2073379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
2074379bc100SJani Nikula {
20752225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2076379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2077379bc100SJani Nikula 
2078df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2079df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2080f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2081f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2082df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
2083df16b636SMahesh Kumar 		else
2084f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2085f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
2086379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
2087379bc100SJani Nikula 	}
2088df16b636SMahesh Kumar }
2089379bc100SJani Nikula 
2090379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
2091379bc100SJani Nikula 				enum port port, u8 iboost)
2092379bc100SJani Nikula {
2093379bc100SJani Nikula 	u32 tmp;
2094379bc100SJani Nikula 
2095f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
2096379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
2097379bc100SJani Nikula 	if (iboost)
2098379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
2099379bc100SJani Nikula 	else
2100379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
2101f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
2102379bc100SJani Nikula }
2103379bc100SJani Nikula 
2104379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
2105379bc100SJani Nikula 			       int level, enum intel_output_type type)
2106379bc100SJani Nikula {
21077801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2108379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2109379bc100SJani Nikula 	enum port port = encoder->port;
2110379bc100SJani Nikula 	u8 iboost;
2111379bc100SJani Nikula 
2112379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
211301a60883SJani Nikula 		iboost = intel_bios_hdmi_boost_level(encoder);
2114379bc100SJani Nikula 	else
2115605a1872SJani Nikula 		iboost = intel_bios_dp_boost_level(encoder);
2116379bc100SJani Nikula 
2117379bc100SJani Nikula 	if (iboost == 0) {
2118379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations;
2119379bc100SJani Nikula 		int n_entries;
2120379bc100SJani Nikula 
2121379bc100SJani Nikula 		if (type == INTEL_OUTPUT_HDMI)
2122*a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_hdmi(encoder, &n_entries);
2123379bc100SJani Nikula 		else if (type == INTEL_OUTPUT_EDP)
2124*a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_edp(encoder,
2125*a8143150SJosé Roberto de Souza 								       port,
2126*a8143150SJosé Roberto de Souza 								       &n_entries);
2127379bc100SJani Nikula 		else
2128*a8143150SJosé Roberto de Souza 			ddi_translations = intel_ddi_get_buf_trans_dp(encoder,
2129*a8143150SJosé Roberto de Souza 								      port,
2130*a8143150SJosé Roberto de Souza 								      &n_entries);
2131379bc100SJani Nikula 
21321de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2133379bc100SJani Nikula 			return;
21341de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2135379bc100SJani Nikula 			level = n_entries - 1;
2136379bc100SJani Nikula 
2137379bc100SJani Nikula 		iboost = ddi_translations[level].i_boost;
2138379bc100SJani Nikula 	}
2139379bc100SJani Nikula 
2140379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
2141379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
214247bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
2143379bc100SJani Nikula 		return;
2144379bc100SJani Nikula 	}
2145379bc100SJani Nikula 
2146379bc100SJani Nikula 	_skl_ddi_set_iboost(dev_priv, port, iboost);
2147379bc100SJani Nikula 
21487801f3b7SLucas De Marchi 	if (port == PORT_A && dig_port->max_lanes == 4)
2149379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
2150379bc100SJani Nikula }
2151379bc100SJani Nikula 
2152379bc100SJani Nikula static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
2153379bc100SJani Nikula 				    int level, enum intel_output_type type)
2154379bc100SJani Nikula {
2155379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2156379bc100SJani Nikula 	const struct bxt_ddi_buf_trans *ddi_translations;
2157379bc100SJani Nikula 	enum port port = encoder->port;
2158379bc100SJani Nikula 	int n_entries;
2159379bc100SJani Nikula 
2160379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2161*a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_hdmi(encoder, &n_entries);
2162379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2163*a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_edp(encoder, &n_entries);
2164379bc100SJani Nikula 	else
2165*a8143150SJosé Roberto de Souza 		ddi_translations = bxt_get_buf_trans_dp(encoder, &n_entries);
2166379bc100SJani Nikula 
21671de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2168379bc100SJani Nikula 		return;
21691de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2170379bc100SJani Nikula 		level = n_entries - 1;
2171379bc100SJani Nikula 
2172379bc100SJani Nikula 	bxt_ddi_phy_set_signal_level(dev_priv, port,
2173379bc100SJani Nikula 				     ddi_translations[level].margin,
2174379bc100SJani Nikula 				     ddi_translations[level].scale,
2175379bc100SJani Nikula 				     ddi_translations[level].enable,
2176379bc100SJani Nikula 				     ddi_translations[level].deemphasis);
2177379bc100SJani Nikula }
2178379bc100SJani Nikula 
217953de0a20SVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp)
2180379bc100SJani Nikula {
218153de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2182379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2183379bc100SJani Nikula 	enum port port = encoder->port;
2184d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2185379bc100SJani Nikula 	int n_entries;
2186379bc100SJani Nikula 
2187978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
2188978c3e53SClinton A Taylor 		if (intel_phy_is_combo(dev_priv, phy))
2189*a8143150SJosé Roberto de Souza 			tgl_get_combo_buf_trans(encoder, encoder->type,
2190978c3e53SClinton A Taylor 						intel_dp->link_rate, &n_entries);
2191978c3e53SClinton A Taylor 		else
2192*a8143150SJosé Roberto de Souza 			tgl_get_dkl_buf_trans(encoder, encoder->type,
21939fa67699SJosé Roberto de Souza 					      intel_dp->link_rate, &n_entries);
2194978c3e53SClinton A Taylor 	} else if (INTEL_GEN(dev_priv) == 11) {
2195b42d5a67SJosé Roberto de Souza 		if (IS_ELKHARTLAKE(dev_priv))
2196*a8143150SJosé Roberto de Souza 			ehl_get_combo_buf_trans(encoder, encoder->type,
2197b42d5a67SJosé Roberto de Souza 						intel_dp->link_rate, &n_entries);
2198b42d5a67SJosé Roberto de Souza 		else if (intel_phy_is_combo(dev_priv, phy))
2199*a8143150SJosé Roberto de Souza 			icl_get_combo_buf_trans(encoder, encoder->type,
2200379bc100SJani Nikula 						intel_dp->link_rate, &n_entries);
2201379bc100SJani Nikula 		else
2202*a8143150SJosé Roberto de Souza 			icl_get_mg_buf_trans(encoder, encoder->type,
22039f7ffa29SJosé Roberto de Souza 					     intel_dp->link_rate, &n_entries);
2204379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2205379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2206*a8143150SJosé Roberto de Souza 			cnl_get_buf_trans_edp(encoder, &n_entries);
2207379bc100SJani Nikula 		else
2208*a8143150SJosé Roberto de Souza 			cnl_get_buf_trans_dp(encoder, &n_entries);
2209379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
2210379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2211*a8143150SJosé Roberto de Souza 			bxt_get_buf_trans_edp(encoder, &n_entries);
2212379bc100SJani Nikula 		else
2213*a8143150SJosé Roberto de Souza 			bxt_get_buf_trans_dp(encoder, &n_entries);
2214379bc100SJani Nikula 	} else {
2215379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2216*a8143150SJosé Roberto de Souza 			intel_ddi_get_buf_trans_edp(encoder, port, &n_entries);
2217379bc100SJani Nikula 		else
2218*a8143150SJosé Roberto de Souza 			intel_ddi_get_buf_trans_dp(encoder, port, &n_entries);
2219379bc100SJani Nikula 	}
2220379bc100SJani Nikula 
22211de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
2222379bc100SJani Nikula 		n_entries = 1;
22231de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
22241de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
2225379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
2226379bc100SJani Nikula 
2227379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
2228379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
2229379bc100SJani Nikula }
2230379bc100SJani Nikula 
2231379bc100SJani Nikula /*
2232379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
2233379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
2234379bc100SJani Nikula  * rethink this code.
2235379bc100SJani Nikula  */
223653de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
2237379bc100SJani Nikula {
2238379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
2239379bc100SJani Nikula }
2240379bc100SJani Nikula 
2241379bc100SJani Nikula static void cnl_ddi_vswing_program(struct intel_encoder *encoder,
2242379bc100SJani Nikula 				   int level, enum intel_output_type type)
2243379bc100SJani Nikula {
2244379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2245379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations;
2246379bc100SJani Nikula 	enum port port = encoder->port;
2247379bc100SJani Nikula 	int n_entries, ln;
2248379bc100SJani Nikula 	u32 val;
2249379bc100SJani Nikula 
2250379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2251*a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_hdmi(encoder, &n_entries);
2252379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2253*a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_edp(encoder, &n_entries);
2254379bc100SJani Nikula 	else
2255*a8143150SJosé Roberto de Souza 		ddi_translations = cnl_get_buf_trans_dp(encoder, &n_entries);
2256379bc100SJani Nikula 
22571de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
2258379bc100SJani Nikula 		return;
22591de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
2260379bc100SJani Nikula 		level = n_entries - 1;
2261379bc100SJani Nikula 
2262379bc100SJani Nikula 	/* Set PORT_TX_DW5 Scaling Mode Sel to 010b. */
2263f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2264379bc100SJani Nikula 	val &= ~SCALING_MODE_SEL_MASK;
2265379bc100SJani Nikula 	val |= SCALING_MODE_SEL(2);
2266f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2267379bc100SJani Nikula 
2268379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2269f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW2_LN0(port));
2270379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2271379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2272379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2273379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2274379bc100SJani Nikula 	/* Rcomp scalar is fixed as 0x98 for every table entry */
2275379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2276f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW2_GRP(port), val);
2277379bc100SJani Nikula 
2278379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2279379bc100SJani Nikula 	/* We cannot write to GRP. It would overrite individual loadgen */
2280379bc100SJani Nikula 	for (ln = 0; ln < 4; ln++) {
2281f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, CNL_PORT_TX_DW4_LN(ln, port));
2282379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2283379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2284379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2285379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2286379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2287f7960e7fSJani Nikula 		intel_de_write(dev_priv, CNL_PORT_TX_DW4_LN(ln, port), val);
2288379bc100SJani Nikula 	}
2289379bc100SJani Nikula 
2290379bc100SJani Nikula 	/* Program PORT_TX_DW5 */
2291379bc100SJani Nikula 	/* All DW5 values are fixed for every table entry */
2292f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2293379bc100SJani Nikula 	val &= ~RTERM_SELECT_MASK;
2294379bc100SJani Nikula 	val |= RTERM_SELECT(6);
2295379bc100SJani Nikula 	val |= TAP3_DISABLE;
2296f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2297379bc100SJani Nikula 
2298379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2299f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW7_LN0(port));
2300379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2301379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2302f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW7_GRP(port), val);
2303379bc100SJani Nikula }
2304379bc100SJani Nikula 
2305379bc100SJani Nikula static void cnl_ddi_vswing_sequence(struct intel_encoder *encoder,
2306379bc100SJani Nikula 				    int level, enum intel_output_type type)
2307379bc100SJani Nikula {
2308379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2309379bc100SJani Nikula 	enum port port = encoder->port;
2310379bc100SJani Nikula 	int width, rate, ln;
2311379bc100SJani Nikula 	u32 val;
2312379bc100SJani Nikula 
2313379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2314379bc100SJani Nikula 		width = 4;
2315379bc100SJani Nikula 		rate = 0; /* Rate is always < than 6GHz for HDMI */
2316379bc100SJani Nikula 	} else {
2317b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2318379bc100SJani Nikula 
2319379bc100SJani Nikula 		width = intel_dp->lane_count;
2320379bc100SJani Nikula 		rate = intel_dp->link_rate;
2321379bc100SJani Nikula 	}
2322379bc100SJani Nikula 
2323379bc100SJani Nikula 	/*
2324379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2325379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2326379bc100SJani Nikula 	 * else clear to 0b.
2327379bc100SJani Nikula 	 */
2328f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_PCS_DW1_LN0(port));
2329379bc100SJani Nikula 	if (type != INTEL_OUTPUT_HDMI)
2330379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2331379bc100SJani Nikula 	else
2332379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2333f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_PCS_DW1_GRP(port), val);
2334379bc100SJani Nikula 
2335379bc100SJani Nikula 	/* 2. Program loadgen select */
2336379bc100SJani Nikula 	/*
2337379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2338379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2339379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2340379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2341379bc100SJani Nikula 	 */
2342379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2343f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, CNL_PORT_TX_DW4_LN(ln, port));
2344379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2345379bc100SJani Nikula 
2346379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1)  ||
2347379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2348379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2349379bc100SJani Nikula 		}
2350f7960e7fSJani Nikula 		intel_de_write(dev_priv, CNL_PORT_TX_DW4_LN(ln, port), val);
2351379bc100SJani Nikula 	}
2352379bc100SJani Nikula 
2353379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2354f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_CL1CM_DW5);
2355379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2356f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_CL1CM_DW5, val);
2357379bc100SJani Nikula 
2358379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2359f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2360379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2361f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2362379bc100SJani Nikula 
2363379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2364379bc100SJani Nikula 	cnl_ddi_vswing_program(encoder, level, type);
2365379bc100SJani Nikula 
2366379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2367f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, CNL_PORT_TX_DW5_LN0(port));
2368379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2369f7960e7fSJani Nikula 	intel_de_write(dev_priv, CNL_PORT_TX_DW5_GRP(port), val);
2370379bc100SJani Nikula }
2371379bc100SJani Nikula 
2372*a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
2373dc867bc7SMatt Roper 					 u32 level, enum phy phy, int type,
2374379bc100SJani Nikula 					 int rate)
2375379bc100SJani Nikula {
2376*a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2377379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations = NULL;
2378379bc100SJani Nikula 	u32 n_entries, val;
2379379bc100SJani Nikula 	int ln;
2380379bc100SJani Nikula 
2381bd3cf6f7SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
2382*a8143150SJosé Roberto de Souza 		ddi_translations = tgl_get_combo_buf_trans(encoder, type, rate,
2383bd3cf6f7SJosé Roberto de Souza 							   &n_entries);
2384b42d5a67SJosé Roberto de Souza 	else if (IS_ELKHARTLAKE(dev_priv))
2385*a8143150SJosé Roberto de Souza 		ddi_translations = ehl_get_combo_buf_trans(encoder, type, rate,
2386b42d5a67SJosé Roberto de Souza 							   &n_entries);
2387bd3cf6f7SJosé Roberto de Souza 	else
2388*a8143150SJosé Roberto de Souza 		ddi_translations = icl_get_combo_buf_trans(encoder, type, rate,
23894a8134d5SMatt Roper 							   &n_entries);
2390379bc100SJani Nikula 	if (!ddi_translations)
2391379bc100SJani Nikula 		return;
2392379bc100SJani Nikula 
2393379bc100SJani Nikula 	if (level >= n_entries) {
239447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
239547bdb1caSJani Nikula 			    "DDI translation not found for level %d. Using %d instead.",
239647bdb1caSJani Nikula 			    level, n_entries - 1);
2397379bc100SJani Nikula 		level = n_entries - 1;
2398379bc100SJani Nikula 	}
2399379bc100SJani Nikula 
2400379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
2401f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2402379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
2403379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
2404379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
2405379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
2406379bc100SJani Nikula 	val |= TAP3_DISABLE;
2407f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2408379bc100SJani Nikula 
2409379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2410f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN0(phy));
2411379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2412379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2413379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2414379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2415379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
2416379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2417f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), val);
2418379bc100SJani Nikula 
2419379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2420379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
2421379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2422f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
2423379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2424379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2425379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2426379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2427379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2428f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
2429379bc100SJani Nikula 	}
2430379bc100SJani Nikula 
2431379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2432f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN0(phy));
2433379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2434379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2435f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW7_GRP(phy), val);
2436379bc100SJani Nikula }
2437379bc100SJani Nikula 
2438379bc100SJani Nikula static void icl_combo_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2439379bc100SJani Nikula 					      u32 level,
2440379bc100SJani Nikula 					      enum intel_output_type type)
2441379bc100SJani Nikula {
2442379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2443dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2444379bc100SJani Nikula 	int width = 0;
2445379bc100SJani Nikula 	int rate = 0;
2446379bc100SJani Nikula 	u32 val;
2447379bc100SJani Nikula 	int ln = 0;
2448379bc100SJani Nikula 
2449379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2450379bc100SJani Nikula 		width = 4;
2451379bc100SJani Nikula 		/* Rate is always < than 6GHz for HDMI */
2452379bc100SJani Nikula 	} else {
2453b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2454379bc100SJani Nikula 
2455379bc100SJani Nikula 		width = intel_dp->lane_count;
2456379bc100SJani Nikula 		rate = intel_dp->link_rate;
2457379bc100SJani Nikula 	}
2458379bc100SJani Nikula 
2459379bc100SJani Nikula 	/*
2460379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2461379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2462379bc100SJani Nikula 	 * else clear to 0b.
2463379bc100SJani Nikula 	 */
2464f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN0(phy));
2465379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2466379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2467379bc100SJani Nikula 	else
2468379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2469f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
2470379bc100SJani Nikula 
2471379bc100SJani Nikula 	/* 2. Program loadgen select */
2472379bc100SJani Nikula 	/*
2473379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2474379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2475379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2476379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2477379bc100SJani Nikula 	 */
2478379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2479f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
2480379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2481379bc100SJani Nikula 
2482379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
2483379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2484379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2485379bc100SJani Nikula 		}
2486f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
2487379bc100SJani Nikula 	}
2488379bc100SJani Nikula 
2489379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2490f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
2491379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2492f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
2493379bc100SJani Nikula 
2494379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2495f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2496379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2497f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2498379bc100SJani Nikula 
2499379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2500*a8143150SJosé Roberto de Souza 	icl_ddi_combo_vswing_program(encoder, level, phy, type, rate);
2501379bc100SJani Nikula 
2502379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2503f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
2504379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2505f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
2506379bc100SJani Nikula }
2507379bc100SJani Nikula 
2508379bc100SJani Nikula static void icl_mg_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
25099f7ffa29SJosé Roberto de Souza 					   int link_clock, u32 level,
25109f7ffa29SJosé Roberto de Souza 					   enum intel_output_type type)
2511379bc100SJani Nikula {
2512379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2513f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
2514379bc100SJani Nikula 	const struct icl_mg_phy_ddi_buf_trans *ddi_translations;
2515379bc100SJani Nikula 	u32 n_entries, val;
25169f7ffa29SJosé Roberto de Souza 	int ln, rate = 0;
2517379bc100SJani Nikula 
25189f7ffa29SJosé Roberto de Souza 	if (type != INTEL_OUTPUT_HDMI) {
25199f7ffa29SJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
25209f7ffa29SJosé Roberto de Souza 
25219f7ffa29SJosé Roberto de Souza 		rate = intel_dp->link_rate;
25229f7ffa29SJosé Roberto de Souza 	}
25239f7ffa29SJosé Roberto de Souza 
2524*a8143150SJosé Roberto de Souza 	ddi_translations = icl_get_mg_buf_trans(encoder, type, rate,
25259f7ffa29SJosé Roberto de Souza 						&n_entries);
2526379bc100SJani Nikula 	/* The table does not have values for level 3 and level 9. */
2527379bc100SJani Nikula 	if (level >= n_entries || level == 3 || level == 9) {
252847bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
252947bdb1caSJani Nikula 			    "DDI translation not found for level %d. Using %d instead.",
2530379bc100SJani Nikula 			    level, n_entries - 2);
2531379bc100SJani Nikula 		level = n_entries - 2;
2532379bc100SJani Nikula 	}
2533379bc100SJani Nikula 
2534379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
2535379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2536f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
2537379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2538f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
2539379bc100SJani Nikula 
2540f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
2541379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2542f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
2543379bc100SJani Nikula 	}
2544379bc100SJani Nikula 
2545379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
2546379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2547f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
2548379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2549379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2550379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2551f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
2552379bc100SJani Nikula 
2553f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port));
2554379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2555379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2556379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2557f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port), val);
2558379bc100SJani Nikula 	}
2559379bc100SJani Nikula 
2560379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
2561379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2562f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
2563379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2564379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2565379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2566379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2567379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2568379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2569379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2570f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
2571379bc100SJani Nikula 
2572f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
2573379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2574379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2575379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2576379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2577379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2578379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2579379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2580f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
2581379bc100SJani Nikula 
2582379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
2583379bc100SJani Nikula 	}
2584379bc100SJani Nikula 
2585379bc100SJani Nikula 	/*
2586379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
2587379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
2588379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
2589379bc100SJani Nikula 	 */
2590379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2591f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
2592379bc100SJani Nikula 		if (link_clock < 300000)
2593379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
2594379bc100SJani Nikula 		else
2595379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
2596f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
2597379bc100SJani Nikula 	}
2598379bc100SJani Nikula 
2599379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
2600379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2601f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
2602379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2603379bc100SJani Nikula 		if (link_clock <= 500000) {
2604379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2605379bc100SJani Nikula 		} else {
2606379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2607379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2608379bc100SJani Nikula 		}
2609f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
2610379bc100SJani Nikula 
2611f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
2612379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2613379bc100SJani Nikula 		if (link_clock <= 500000) {
2614379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2615379bc100SJani Nikula 		} else {
2616379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2617379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2618379bc100SJani Nikula 		}
2619f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
2620379bc100SJani Nikula 	}
2621379bc100SJani Nikula 
2622379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
2623379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2624f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
2625f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
2626379bc100SJani Nikula 		val |= CRI_CALCINIT;
2627f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
2628f7960e7fSJani Nikula 			       val);
2629379bc100SJani Nikula 
2630f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
2631f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
2632379bc100SJani Nikula 		val |= CRI_CALCINIT;
2633f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
2634f7960e7fSJani Nikula 			       val);
2635379bc100SJani Nikula 	}
2636379bc100SJani Nikula }
2637379bc100SJani Nikula 
2638379bc100SJani Nikula static void icl_ddi_vswing_sequence(struct intel_encoder *encoder,
2639379bc100SJani Nikula 				    int link_clock,
2640379bc100SJani Nikula 				    u32 level,
2641379bc100SJani Nikula 				    enum intel_output_type type)
2642379bc100SJani Nikula {
2643379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2644d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2645379bc100SJani Nikula 
2646d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy))
2647379bc100SJani Nikula 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2648379bc100SJani Nikula 	else
26499f7ffa29SJosé Roberto de Souza 		icl_mg_phy_ddi_vswing_sequence(encoder, link_clock, level,
26509f7ffa29SJosé Roberto de Souza 					       type);
2651379bc100SJani Nikula }
2652379bc100SJani Nikula 
2653978c3e53SClinton A Taylor static void
2654978c3e53SClinton A Taylor tgl_dkl_phy_ddi_vswing_sequence(struct intel_encoder *encoder, int link_clock,
265594641eb6SVandita Kulkarni 				u32 level, enum intel_output_type type)
2656978c3e53SClinton A Taylor {
2657978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2658978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
2659978c3e53SClinton A Taylor 	const struct tgl_dkl_phy_ddi_buf_trans *ddi_translations;
2660978c3e53SClinton A Taylor 	u32 n_entries, val, ln, dpcnt_mask, dpcnt_val;
26619fa67699SJosé Roberto de Souza 	int rate = 0;
2662978c3e53SClinton A Taylor 
266394641eb6SVandita Kulkarni 	if (type == INTEL_OUTPUT_HDMI) {
26649fa67699SJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
26659fa67699SJosé Roberto de Souza 
26669fa67699SJosé Roberto de Souza 		rate = intel_dp->link_rate;
2667362bfb99SMatt Roper 	}
2668978c3e53SClinton A Taylor 
2669*a8143150SJosé Roberto de Souza 	ddi_translations = tgl_get_dkl_buf_trans(encoder, encoder->type, rate,
26709fa67699SJosé Roberto de Souza 						 &n_entries);
26719fa67699SJosé Roberto de Souza 
2672978c3e53SClinton A Taylor 	if (level >= n_entries)
2673978c3e53SClinton A Taylor 		level = n_entries - 1;
2674978c3e53SClinton A Taylor 
2675978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
2676978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
2677978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
2678978c3e53SClinton A Taylor 	dpcnt_val = DKL_TX_VSWING_CONTROL(ddi_translations[level].dkl_vswing_control);
2679978c3e53SClinton A Taylor 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(ddi_translations[level].dkl_de_emphasis_control);
2680978c3e53SClinton A Taylor 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(ddi_translations[level].dkl_preshoot_control);
2681978c3e53SClinton A Taylor 
2682978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
2683f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2684f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
2685978c3e53SClinton A Taylor 
2686f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
26872d69c42eSJosé Roberto de Souza 
2688978c3e53SClinton A Taylor 		/* All the registers are RMW */
2689f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
2690978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
2691978c3e53SClinton A Taylor 		val |= dpcnt_val;
2692f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
2693978c3e53SClinton A Taylor 
2694f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
2695978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
2696978c3e53SClinton A Taylor 		val |= dpcnt_val;
2697f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
2698978c3e53SClinton A Taylor 
2699f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
2700978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
2701f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
2702978c3e53SClinton A Taylor 	}
2703978c3e53SClinton A Taylor }
2704978c3e53SClinton A Taylor 
2705978c3e53SClinton A Taylor static void tgl_ddi_vswing_sequence(struct intel_encoder *encoder,
2706978c3e53SClinton A Taylor 				    int link_clock,
2707978c3e53SClinton A Taylor 				    u32 level,
2708978c3e53SClinton A Taylor 				    enum intel_output_type type)
2709978c3e53SClinton A Taylor {
2710978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2711978c3e53SClinton A Taylor 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2712978c3e53SClinton A Taylor 
2713978c3e53SClinton A Taylor 	if (intel_phy_is_combo(dev_priv, phy))
2714978c3e53SClinton A Taylor 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2715978c3e53SClinton A Taylor 	else
271694641eb6SVandita Kulkarni 		tgl_dkl_phy_ddi_vswing_sequence(encoder, link_clock, level, type);
2717978c3e53SClinton A Taylor }
2718978c3e53SClinton A Taylor 
27198b4f2137SPankaj Bharadiya static u32 translate_signal_level(struct intel_dp *intel_dp, int signal_levels)
2720379bc100SJani Nikula {
27218b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
2722379bc100SJani Nikula 	int i;
2723379bc100SJani Nikula 
2724379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
2725379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
2726379bc100SJani Nikula 			return i;
2727379bc100SJani Nikula 	}
2728379bc100SJani Nikula 
27298b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
27308b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
2731379bc100SJani Nikula 		 signal_levels);
2732379bc100SJani Nikula 
2733379bc100SJani Nikula 	return 0;
2734379bc100SJani Nikula }
2735379bc100SJani Nikula 
2736379bc100SJani Nikula static u32 intel_ddi_dp_level(struct intel_dp *intel_dp)
2737379bc100SJani Nikula {
2738379bc100SJani Nikula 	u8 train_set = intel_dp->train_set[0];
2739379bc100SJani Nikula 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2740379bc100SJani Nikula 					 DP_TRAIN_PRE_EMPHASIS_MASK);
2741379bc100SJani Nikula 
27428b4f2137SPankaj Bharadiya 	return translate_signal_level(intel_dp, signal_levels);
2743379bc100SJani Nikula }
2744379bc100SJani Nikula 
2745fb83f72cSVille Syrjälä static void
2746fb83f72cSVille Syrjälä tgl_set_signal_levels(struct intel_dp *intel_dp)
2747379bc100SJani Nikula {
2748fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2749379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2750379bc100SJani Nikula 
2751978c3e53SClinton A Taylor 	tgl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2752978c3e53SClinton A Taylor 				level, encoder->type);
2753379bc100SJani Nikula }
2754379bc100SJani Nikula 
2755fb83f72cSVille Syrjälä static void
2756fb83f72cSVille Syrjälä icl_set_signal_levels(struct intel_dp *intel_dp)
2757379bc100SJani Nikula {
2758fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2759379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2760379bc100SJani Nikula 
2761fb83f72cSVille Syrjälä 	icl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2762fb83f72cSVille Syrjälä 				level, encoder->type);
2763fb83f72cSVille Syrjälä }
2764fb83f72cSVille Syrjälä 
2765fb83f72cSVille Syrjälä static void
2766fb83f72cSVille Syrjälä cnl_set_signal_levels(struct intel_dp *intel_dp)
2767fb83f72cSVille Syrjälä {
2768fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2769fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2770fb83f72cSVille Syrjälä 
2771fb83f72cSVille Syrjälä 	cnl_ddi_vswing_sequence(encoder, level, encoder->type);
2772fb83f72cSVille Syrjälä }
2773fb83f72cSVille Syrjälä 
2774fb83f72cSVille Syrjälä static void
2775fb83f72cSVille Syrjälä bxt_set_signal_levels(struct intel_dp *intel_dp)
2776fb83f72cSVille Syrjälä {
2777fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2778fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2779fb83f72cSVille Syrjälä 
2780fb83f72cSVille Syrjälä 	bxt_ddi_vswing_sequence(encoder, level, encoder->type);
2781fb83f72cSVille Syrjälä }
2782fb83f72cSVille Syrjälä 
2783fb83f72cSVille Syrjälä static void
2784fb83f72cSVille Syrjälä hsw_set_signal_levels(struct intel_dp *intel_dp)
2785fb83f72cSVille Syrjälä {
2786fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
2787fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2788fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
2789fb83f72cSVille Syrjälä 	enum port port = encoder->port;
2790fb83f72cSVille Syrjälä 	u32 signal_levels;
2791fb83f72cSVille Syrjälä 
2792fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
2793fb83f72cSVille Syrjälä 
2794fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
2795fb83f72cSVille Syrjälä 		    signal_levels);
2796fb83f72cSVille Syrjälä 
2797fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
2798fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
2799fb83f72cSVille Syrjälä 
2800379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
2801379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, encoder->type);
2802379bc100SJani Nikula 
2803fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
2804fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
2805379bc100SJani Nikula }
2806379bc100SJani Nikula 
280781b55ef1SJani Nikula static u32 icl_dpclka_cfgcr0_clk_off(struct drm_i915_private *dev_priv,
2808befa372bSMatt Roper 				     enum phy phy)
2809379bc100SJani Nikula {
2810befa372bSMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
2811befa372bSMatt Roper 		return ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
2812befa372bSMatt Roper 	} else if (intel_phy_is_tc(dev_priv, phy)) {
2813befa372bSMatt Roper 		enum tc_port tc_port = intel_port_to_tc(dev_priv,
2814befa372bSMatt Roper 							(enum port)phy);
2815379bc100SJani Nikula 
2816379bc100SJani Nikula 		return ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port);
2817379bc100SJani Nikula 	}
2818379bc100SJani Nikula 
2819379bc100SJani Nikula 	return 0;
2820379bc100SJani Nikula }
2821379bc100SJani Nikula 
2822379bc100SJani Nikula static void icl_map_plls_to_ports(struct intel_encoder *encoder,
2823379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2824379bc100SJani Nikula {
2825379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2826379bc100SJani Nikula 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2827befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2828379bc100SJani Nikula 	u32 val;
2829379bc100SJani Nikula 
2830353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
2831379bc100SJani Nikula 
2832f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
28331de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
28341de143ccSPankaj Bharadiya 		    (val & icl_dpclka_cfgcr0_clk_off(dev_priv, phy)) == 0);
2835379bc100SJani Nikula 
2836befa372bSMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
2837befa372bSMatt Roper 		/*
2838befa372bSMatt Roper 		 * Even though this register references DDIs, note that we
2839befa372bSMatt Roper 		 * want to pass the PHY rather than the port (DDI).  For
2840befa372bSMatt Roper 		 * ICL, port=phy in all cases so it doesn't matter, but for
2841befa372bSMatt Roper 		 * EHL the bspec notes the following:
2842befa372bSMatt Roper 		 *
2843befa372bSMatt Roper 		 *   "DDID clock tied to DDIA clock, so DPCLKA_CFGCR0 DDIA
2844befa372bSMatt Roper 		 *   Clock Select chooses the PLL for both DDIA and DDID and
2845befa372bSMatt Roper 		 *   drives port A in all cases."
2846befa372bSMatt Roper 		 */
2847befa372bSMatt Roper 		val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
2848befa372bSMatt Roper 		val |= ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
2849f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2850f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, ICL_DPCLKA_CFGCR0);
2851379bc100SJani Nikula 	}
2852379bc100SJani Nikula 
2853befa372bSMatt Roper 	val &= ~icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2854f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2855379bc100SJani Nikula 
2856353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
2857379bc100SJani Nikula }
2858379bc100SJani Nikula 
2859379bc100SJani Nikula static void icl_unmap_plls_to_ports(struct intel_encoder *encoder)
2860379bc100SJani Nikula {
2861379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2862befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2863379bc100SJani Nikula 	u32 val;
2864379bc100SJani Nikula 
2865353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
2866379bc100SJani Nikula 
2867f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
2868befa372bSMatt Roper 	val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2869f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
2870379bc100SJani Nikula 
2871353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
2872379bc100SJani Nikula }
2873379bc100SJani Nikula 
28745956f440SLucas De Marchi static void icl_sanitize_port_clk_off(struct drm_i915_private *dev_priv,
28755956f440SLucas De Marchi 				      u32 port_mask, bool ddi_clk_needed)
28765956f440SLucas De Marchi {
28775956f440SLucas De Marchi 	enum port port;
28785956f440SLucas De Marchi 	u32 val;
28795956f440SLucas De Marchi 
2880f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
28815956f440SLucas De Marchi 	for_each_port_masked(port, port_mask) {
28825956f440SLucas De Marchi 		enum phy phy = intel_port_to_phy(dev_priv, port);
288341ba19fcSLucas De Marchi 		bool ddi_clk_off = val & icl_dpclka_cfgcr0_clk_off(dev_priv,
288441ba19fcSLucas De Marchi 								   phy);
28855956f440SLucas De Marchi 
288641ba19fcSLucas De Marchi 		if (ddi_clk_needed == !ddi_clk_off)
28875956f440SLucas De Marchi 			continue;
28885956f440SLucas De Marchi 
28895956f440SLucas De Marchi 		/*
28905956f440SLucas De Marchi 		 * Punt on the case now where clock is gated, but it would
28915956f440SLucas De Marchi 		 * be needed by the port. Something else is really broken then.
28925956f440SLucas De Marchi 		 */
28931de143ccSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm, ddi_clk_needed))
28945956f440SLucas De Marchi 			continue;
28955956f440SLucas De Marchi 
289647bdb1caSJani Nikula 		drm_notice(&dev_priv->drm,
289747bdb1caSJani Nikula 			   "PHY %c is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2898d6f970f0SLucas De Marchi 			   phy_name(phy));
28995956f440SLucas De Marchi 		val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2900f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
29015956f440SLucas De Marchi 	}
29025956f440SLucas De Marchi }
29035956f440SLucas De Marchi 
2904379bc100SJani Nikula void icl_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
2905379bc100SJani Nikula {
2906379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2907379bc100SJani Nikula 	u32 port_mask;
2908379bc100SJani Nikula 	bool ddi_clk_needed;
2909379bc100SJani Nikula 
2910379bc100SJani Nikula 	/*
2911379bc100SJani Nikula 	 * In case of DP MST, we sanitize the primary encoder only, not the
2912379bc100SJani Nikula 	 * virtual ones.
2913379bc100SJani Nikula 	 */
2914379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DP_MST)
2915379bc100SJani Nikula 		return;
2916379bc100SJani Nikula 
2917379bc100SJani Nikula 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2918379bc100SJani Nikula 		u8 pipe_mask;
2919379bc100SJani Nikula 		bool is_mst;
2920379bc100SJani Nikula 
2921379bc100SJani Nikula 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2922379bc100SJani Nikula 		/*
2923379bc100SJani Nikula 		 * In the unlikely case that BIOS enables DP in MST mode, just
2924379bc100SJani Nikula 		 * warn since our MST HW readout is incomplete.
2925379bc100SJani Nikula 		 */
29261de143ccSPankaj Bharadiya 		if (drm_WARN_ON(&dev_priv->drm, is_mst))
2927379bc100SJani Nikula 			return;
2928379bc100SJani Nikula 	}
2929379bc100SJani Nikula 
2930379bc100SJani Nikula 	port_mask = BIT(encoder->port);
2931379bc100SJani Nikula 	ddi_clk_needed = encoder->base.crtc;
2932379bc100SJani Nikula 
2933379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DSI) {
2934379bc100SJani Nikula 		struct intel_encoder *other_encoder;
2935379bc100SJani Nikula 
2936379bc100SJani Nikula 		port_mask = intel_dsi_encoder_ports(encoder);
2937379bc100SJani Nikula 		/*
2938379bc100SJani Nikula 		 * Sanity check that we haven't incorrectly registered another
2939379bc100SJani Nikula 		 * encoder using any of the ports of this DSI encoder.
2940379bc100SJani Nikula 		 */
2941379bc100SJani Nikula 		for_each_intel_encoder(&dev_priv->drm, other_encoder) {
2942379bc100SJani Nikula 			if (other_encoder == encoder)
2943379bc100SJani Nikula 				continue;
2944379bc100SJani Nikula 
29451de143ccSPankaj Bharadiya 			if (drm_WARN_ON(&dev_priv->drm,
29461de143ccSPankaj Bharadiya 					port_mask & BIT(other_encoder->port)))
2947379bc100SJani Nikula 				return;
2948379bc100SJani Nikula 		}
2949379bc100SJani Nikula 		/*
2950379bc100SJani Nikula 		 * For DSI we keep the ddi clocks gated
2951379bc100SJani Nikula 		 * except during enable/disable sequence.
2952379bc100SJani Nikula 		 */
2953379bc100SJani Nikula 		ddi_clk_needed = false;
2954379bc100SJani Nikula 	}
2955379bc100SJani Nikula 
29565956f440SLucas De Marchi 	icl_sanitize_port_clk_off(dev_priv, port_mask, ddi_clk_needed);
2957379bc100SJani Nikula }
2958379bc100SJani Nikula 
2959379bc100SJani Nikula static void intel_ddi_clk_select(struct intel_encoder *encoder,
2960379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2961379bc100SJani Nikula {
2962379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2963379bc100SJani Nikula 	enum port port = encoder->port;
2964d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2965379bc100SJani Nikula 	u32 val;
2966379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2967379bc100SJani Nikula 
29681de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, !pll))
2969379bc100SJani Nikula 		return;
2970379bc100SJani Nikula 
2971353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
2972379bc100SJani Nikula 
2973379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2974d8fe2ab6SMatt Roper 		if (!intel_phy_is_combo(dev_priv, phy))
2975f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
2976379bc100SJani Nikula 				       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
2977c2052d6eSJosé Roberto de Souza 		else if (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C)
2978c2052d6eSJosé Roberto de Souza 			/*
2979c2052d6eSJosé Roberto de Souza 			 * MG does not exist but the programming is required
2980c2052d6eSJosé Roberto de Souza 			 * to ungate DDIC and DDID
2981c2052d6eSJosé Roberto de Souza 			 */
2982f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
2983f7960e7fSJani Nikula 				       DDI_CLK_SEL_MG);
2984379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2985379bc100SJani Nikula 		/* Configure DPCLKA_CFGCR0 to map the DPLL to the DDI. */
2986f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPCLKA_CFGCR0);
2987379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
2988379bc100SJani Nikula 		val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, port);
2989f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0, val);
2990379bc100SJani Nikula 
2991379bc100SJani Nikula 		/*
2992379bc100SJani Nikula 		 * Configure DPCLKA_CFGCR0 to turn on the clock for the DDI.
2993379bc100SJani Nikula 		 * This step and the step before must be done with separate
2994379bc100SJani Nikula 		 * register writes.
2995379bc100SJani Nikula 		 */
2996f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPCLKA_CFGCR0);
2997379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_OFF(port);
2998f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0, val);
2999379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3000379bc100SJani Nikula 		/* DDI -> PLL mapping  */
3001f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DPLL_CTRL2);
3002379bc100SJani Nikula 
3003379bc100SJani Nikula 		val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
3004379bc100SJani Nikula 			 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
3005379bc100SJani Nikula 		val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
3006379bc100SJani Nikula 			DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
3007379bc100SJani Nikula 
3008f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPLL_CTRL2, val);
3009379bc100SJani Nikula 
3010379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3011f7960e7fSJani Nikula 		intel_de_write(dev_priv, PORT_CLK_SEL(port),
3012f7960e7fSJani Nikula 			       hsw_pll_to_ddi_pll_sel(pll));
3013379bc100SJani Nikula 	}
3014379bc100SJani Nikula 
3015353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
3016379bc100SJani Nikula }
3017379bc100SJani Nikula 
3018379bc100SJani Nikula static void intel_ddi_clk_disable(struct intel_encoder *encoder)
3019379bc100SJani Nikula {
3020379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3021379bc100SJani Nikula 	enum port port = encoder->port;
3022d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3023379bc100SJani Nikula 
3024379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
3025c2052d6eSJosé Roberto de Souza 		if (!intel_phy_is_combo(dev_priv, phy) ||
3026c2052d6eSJosé Roberto de Souza 		    (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C))
3027f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_CLK_SEL(port),
3028f7960e7fSJani Nikula 				       DDI_CLK_SEL_NONE);
3029379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
3030f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPCLKA_CFGCR0,
3031f7960e7fSJani Nikula 			       intel_de_read(dev_priv, DPCLKA_CFGCR0) | DPCLKA_CFGCR0_DDI_CLK_OFF(port));
3032379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3033f7960e7fSJani Nikula 		intel_de_write(dev_priv, DPLL_CTRL2,
3034f7960e7fSJani Nikula 			       intel_de_read(dev_priv, DPLL_CTRL2) | DPLL_CTRL2_DDI_CLK_OFF(port));
3035379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3036f7960e7fSJani Nikula 		intel_de_write(dev_priv, PORT_CLK_SEL(port),
3037f7960e7fSJani Nikula 			       PORT_CLK_SEL_NONE);
3038379bc100SJani Nikula 	}
3039379bc100SJani Nikula }
3040379bc100SJani Nikula 
30418aaf5cbdSJosé Roberto de Souza static void
30427801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
30433b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
3044379bc100SJani Nikula {
30457801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
30467801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
30473b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
30483b51be4eSClinton A Taylor 	u8 width;
3049379bc100SJani Nikula 
30507801f3b7SLucas De Marchi 	if (dig_port->tc_mode == TC_PORT_TBT_ALT)
3051379bc100SJani Nikula 		return;
3052379bc100SJani Nikula 
3053978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
3054f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3055f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
3056f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
3057f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3058f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
3059f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
3060978c3e53SClinton A Taylor 	} else {
3061f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
3062f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
3063978c3e53SClinton A Taylor 	}
3064379bc100SJani Nikula 
30654f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3066379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3067379bc100SJani Nikula 
30683b51be4eSClinton A Taylor 	/* DPPATC */
30697801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
30703b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
3071379bc100SJani Nikula 
30723b51be4eSClinton A Taylor 	switch (pin_assignment) {
30733b51be4eSClinton A Taylor 	case 0x0:
30741de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
30757801f3b7SLucas De Marchi 			    dig_port->tc_mode != TC_PORT_LEGACY);
30763b51be4eSClinton A Taylor 		if (width == 1) {
3077379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
30783b51be4eSClinton A Taylor 		} else {
30793b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
30803b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3081379bc100SJani Nikula 		}
3082379bc100SJani Nikula 		break;
30833b51be4eSClinton A Taylor 	case 0x1:
30843b51be4eSClinton A Taylor 		if (width == 4) {
30853b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
30863b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
30873b51be4eSClinton A Taylor 		}
3088379bc100SJani Nikula 		break;
30893b51be4eSClinton A Taylor 	case 0x2:
30903b51be4eSClinton A Taylor 		if (width == 2) {
30913b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
30923b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
30933b51be4eSClinton A Taylor 		}
30943b51be4eSClinton A Taylor 		break;
30953b51be4eSClinton A Taylor 	case 0x3:
30963b51be4eSClinton A Taylor 	case 0x5:
30973b51be4eSClinton A Taylor 		if (width == 1) {
30983b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
30993b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
31003b51be4eSClinton A Taylor 		} else {
31013b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31023b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31033b51be4eSClinton A Taylor 		}
31043b51be4eSClinton A Taylor 		break;
31053b51be4eSClinton A Taylor 	case 0x4:
31063b51be4eSClinton A Taylor 	case 0x6:
31073b51be4eSClinton A Taylor 		if (width == 1) {
31083b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
31093b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
31103b51be4eSClinton A Taylor 		} else {
31113b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
31123b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
31133b51be4eSClinton A Taylor 		}
31143b51be4eSClinton A Taylor 		break;
3115379bc100SJani Nikula 	default:
31163b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
3117379bc100SJani Nikula 	}
3118379bc100SJani Nikula 
3119978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12) {
3120f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3121f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
3122f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
3123f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
3124f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
3125f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
3126978c3e53SClinton A Taylor 	} else {
3127f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
3128f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
3129379bc100SJani Nikula 	}
3130978c3e53SClinton A Taylor }
3131379bc100SJani Nikula 
3132379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
3133379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3134379bc100SJani Nikula {
313547bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
313647bdb1caSJani Nikula 
3137379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3138379bc100SJani Nikula 		return;
3139379bc100SJani Nikula 
3140379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
314147bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
314247bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
3143379bc100SJani Nikula }
3144379bc100SJani Nikula 
3145379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
3146379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
3147379bc100SJani Nikula {
3148379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
31494444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3150379bc100SJani Nikula 	u32 val;
3151379bc100SJani Nikula 
3152379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3153379bc100SJani Nikula 		return;
3154379bc100SJani Nikula 
3155b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
3156f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3157379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
3158f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3159379bc100SJani Nikula 
31604444df6eSLucas De Marchi 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
31614cb3b44dSDaniele Ceraolo Spurio 				  DP_TP_STATUS_FEC_ENABLE_LIVE, 1))
316247bdb1caSJani Nikula 		drm_err(&dev_priv->drm,
316347bdb1caSJani Nikula 			"Timed out waiting for FEC Enable Status\n");
3164379bc100SJani Nikula }
3165379bc100SJani Nikula 
3166379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
3167379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3168379bc100SJani Nikula {
3169379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
31704444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3171379bc100SJani Nikula 	u32 val;
3172379bc100SJani Nikula 
3173379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3174379bc100SJani Nikula 		return;
3175379bc100SJani Nikula 
3176b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
3177f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3178379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
3179f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3180f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3181379bc100SJani Nikula }
3182379bc100SJani Nikula 
3183ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
3184ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
318599389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
318699389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
318799389390SJosé Roberto de Souza {
3188b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
318999389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
319099389390SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3191b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
319299389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
319399389390SJosé Roberto de Souza 	int level = intel_ddi_dp_level(intel_dp);
31944444df6eSLucas De Marchi 	enum transcoder transcoder = crtc_state->cpu_transcoder;
319599389390SJosé Roberto de Souza 
319699389390SJosé Roberto de Souza 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
319799389390SJosé Roberto de Souza 				 crtc_state->lane_count, is_mst);
319899389390SJosé Roberto de Souza 
31994444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_ctl = TGL_DP_TP_CTL(transcoder);
32004444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_status = TGL_DP_TP_STATUS(transcoder);
32014444df6eSLucas De Marchi 
32025e19c0b0SMatt Roper 	/*
32035e19c0b0SMatt Roper 	 * 1. Enable Power Wells
32045e19c0b0SMatt Roper 	 *
32055e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
32065e19c0b0SMatt Roper 	 * before we called down into this function.
32075e19c0b0SMatt Roper 	 */
320899389390SJosé Roberto de Souza 
32095e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
321099389390SJosé Roberto de Souza 	intel_edp_panel_on(intel_dp);
321199389390SJosé Roberto de Souza 
321299389390SJosé Roberto de Souza 	/*
32135e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
32145e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
32155e19c0b0SMatt Roper 	 *
32165e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
32171e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
321899389390SJosé Roberto de Souza 	 */
321999389390SJosé Roberto de Souza 
32205e19c0b0SMatt Roper 	/*
32215e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
32225e19c0b0SMatt Roper 	 *
32235e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
32241e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
32255e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
32265e19c0b0SMatt Roper 	 */
32276171e58bSClinton A Taylor 	intel_ddi_clk_select(encoder, crtc_state);
32286171e58bSClinton A Taylor 
32295e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
323099389390SJosé Roberto de Souza 	if (!intel_phy_is_tc(dev_priv, phy) ||
323199389390SJosé Roberto de Souza 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
323299389390SJosé Roberto de Souza 		intel_display_power_get(dev_priv,
323399389390SJosé Roberto de Souza 					dig_port->ddi_io_power_domain);
323499389390SJosé Roberto de Souza 
32355e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
32363b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
323799389390SJosé Roberto de Souza 
323899389390SJosé Roberto de Souza 	/*
32395e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
32405e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
32415e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
32425e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
32435e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
32445e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
32455e19c0b0SMatt Roper 	 * unconditionally here.
32465e19c0b0SMatt Roper 	 */
32475e19c0b0SMatt Roper 
32485e19c0b0SMatt Roper 	/*
32495e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
32505e19c0b0SMatt Roper 	 * Transcoder.
325199389390SJosé Roberto de Souza 	 */
325202a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
325399389390SJosé Roberto de Souza 
32545e19c0b0SMatt Roper 	/*
32555e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
32565e19c0b0SMatt Roper 	 * Transport Select
32575e19c0b0SMatt Roper 	 */
3258eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
325999389390SJosé Roberto de Souza 
32605e19c0b0SMatt Roper 	/*
32615e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
32625e19c0b0SMatt Roper 	 * selected
32635e19c0b0SMatt Roper 	 *
32645e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
32655e19c0b0SMatt Roper 	 * down this function.
32665e19c0b0SMatt Roper 	 */
32675e19c0b0SMatt Roper 
32685e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
3269978c3e53SClinton A Taylor 	tgl_ddi_vswing_sequence(encoder, crtc_state->port_clock, level,
327099389390SJosé Roberto de Souza 				encoder->type);
327199389390SJosé Roberto de Souza 
32725e19c0b0SMatt Roper 	/*
32735e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
32745e19c0b0SMatt Roper 	 * the used lanes of the DDI.
32755e19c0b0SMatt Roper 	 */
327699389390SJosé Roberto de Souza 	if (intel_phy_is_combo(dev_priv, phy)) {
327799389390SJosé Roberto de Souza 		bool lane_reversal =
327899389390SJosé Roberto de Souza 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
327999389390SJosé Roberto de Souza 
328099389390SJosé Roberto de Souza 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
328199389390SJosé Roberto de Souza 					       crtc_state->lane_count,
328299389390SJosé Roberto de Souza 					       lane_reversal);
328399389390SJosé Roberto de Souza 	}
328499389390SJosé Roberto de Souza 
32855e19c0b0SMatt Roper 	/*
32865e19c0b0SMatt Roper 	 * 7.g Configure and enable DDI_BUF_CTL
32875e19c0b0SMatt Roper 	 * 7.h Wait for DDI_BUF_CTL DDI Idle Status = 0b (Not Idle), timeout
32885e19c0b0SMatt Roper 	 *     after 500 us.
32895e19c0b0SMatt Roper 	 *
32905e19c0b0SMatt Roper 	 * We only configure what the register value will be here.  Actual
32915e19c0b0SMatt Roper 	 * enabling happens during link training farther down.
32925e19c0b0SMatt Roper 	 */
329399389390SJosé Roberto de Souza 	intel_ddi_init_dp_buf_reg(encoder);
329499389390SJosé Roberto de Souza 
329599389390SJosé Roberto de Souza 	if (!is_mst)
329699389390SJosé Roberto de Souza 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
329799389390SJosé Roberto de Souza 
329899389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
329999389390SJosé Roberto de Souza 	/*
330099389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
330199389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
330299389390SJosé Roberto de Souza 	 * training
330399389390SJosé Roberto de Souza 	 */
330499389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
33055e19c0b0SMatt Roper 
33065e19c0b0SMatt Roper 	/*
33075e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
33085e19c0b0SMatt Roper 	 *     failure handling)
33095e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
33105e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
33115e19c0b0SMatt Roper 	 *     (timeout after 800 us)
33125e19c0b0SMatt Roper 	 */
331399389390SJosé Roberto de Souza 	intel_dp_start_link_train(intel_dp);
331499389390SJosé Roberto de Souza 
33155e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
3316eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
331799389390SJosé Roberto de Souza 		intel_dp_stop_link_train(intel_dp);
331899389390SJosé Roberto de Souza 
33195e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
332099389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
332199389390SJosé Roberto de Souza 	intel_dsc_enable(encoder, crtc_state);
332299389390SJosé Roberto de Souza }
332399389390SJosé Roberto de Souza 
3324ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
3325ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3326379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3327379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3328379bc100SJani Nikula {
3329b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3330379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3331379bc100SJani Nikula 	enum port port = encoder->port;
3332dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3333b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3334379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3335379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
3336379bc100SJani Nikula 
3337542dfab5SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) < 11)
33381de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
33391de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
3340542dfab5SJosé Roberto de Souza 	else
33411de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
3342379bc100SJani Nikula 
3343379bc100SJani Nikula 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
3344379bc100SJani Nikula 				 crtc_state->lane_count, is_mst);
3345379bc100SJani Nikula 
3346379bc100SJani Nikula 	intel_edp_panel_on(intel_dp);
3347379bc100SJani Nikula 
3348379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3349379bc100SJani Nikula 
3350d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
33513b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
33523b2ed431SImre Deak 		intel_display_power_get(dev_priv,
33533b2ed431SImre Deak 					dig_port->ddi_io_power_domain);
3354379bc100SJani Nikula 
33553b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
3356379bc100SJani Nikula 
3357379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3358379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3359379bc100SJani Nikula 					level, encoder->type);
3360379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3361379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
3362379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3363379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
3364379bc100SJani Nikula 	else
3365379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
3366379bc100SJani Nikula 
3367d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
3368379bc100SJani Nikula 		bool lane_reversal =
3369379bc100SJani Nikula 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
3370379bc100SJani Nikula 
3371dc867bc7SMatt Roper 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
3372379bc100SJani Nikula 					       crtc_state->lane_count,
3373379bc100SJani Nikula 					       lane_reversal);
3374379bc100SJani Nikula 	}
3375379bc100SJani Nikula 
3376379bc100SJani Nikula 	intel_ddi_init_dp_buf_reg(encoder);
3377379bc100SJani Nikula 	if (!is_mst)
3378379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
3379379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
3380379bc100SJani Nikula 					      true);
3381379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
3382379bc100SJani Nikula 	intel_dp_start_link_train(intel_dp);
3383eadf6f91SManasi Navare 	if ((port != PORT_A || INTEL_GEN(dev_priv) >= 9) &&
3384eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
3385379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3386379bc100SJani Nikula 
3387379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
3388379bc100SJani Nikula 
3389379bc100SJani Nikula 	if (!is_mst)
339002a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
3391379bc100SJani Nikula 
3392379bc100SJani Nikula 	intel_dsc_enable(encoder, crtc_state);
3393379bc100SJani Nikula }
3394379bc100SJani Nikula 
3395ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
3396ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
339799389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
339899389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
339999389390SJosé Roberto de Souza {
340099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
340199389390SJosé Roberto de Souza 
340299389390SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
3403ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
340499389390SJosé Roberto de Souza 	else
3405ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
34060c06fa15SGwan-gyeong Mun 
3407bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
3408bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
3409bd8c9ccaSGwan-gyeong Mun 	 */
34101fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
34110c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
34121c9d2eb2SJani Nikula 
34131c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
341499389390SJosé Roberto de Souza 	}
34151fc1e8d4SJosé Roberto de Souza }
341699389390SJosé Roberto de Souza 
3417ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
3418ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
3419379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
3420379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
3421379bc100SJani Nikula {
34220ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
34230ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3424379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
34250aed3bdeSJani Nikula 	int level = intel_ddi_hdmi_level(encoder);
3426379bc100SJani Nikula 
3427379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
3428379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3429379bc100SJani Nikula 
3430379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
3431379bc100SJani Nikula 
34323b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
3433379bc100SJani Nikula 
3434978c3e53SClinton A Taylor 	if (INTEL_GEN(dev_priv) >= 12)
3435978c3e53SClinton A Taylor 		tgl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3436978c3e53SClinton A Taylor 					level, INTEL_OUTPUT_HDMI);
3437978c3e53SClinton A Taylor 	else if (INTEL_GEN(dev_priv) == 11)
3438379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3439379bc100SJani Nikula 					level, INTEL_OUTPUT_HDMI);
3440379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3441379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3442379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3443379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3444379bc100SJani Nikula 	else
3445379bc100SJani Nikula 		intel_prepare_hdmi_ddi_buffers(encoder, level);
3446379bc100SJani Nikula 
3447379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
3448379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, INTEL_OUTPUT_HDMI);
3449379bc100SJani Nikula 
345002a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
3451379bc100SJani Nikula 
34520ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
3453379bc100SJani Nikula 				 crtc_state->has_infoframe,
3454379bc100SJani Nikula 				 crtc_state, conn_state);
3455379bc100SJani Nikula }
3456379bc100SJani Nikula 
3457ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
3458ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3459379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
3460379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
3461379bc100SJani Nikula {
34622225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3463379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3464379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
3465379bc100SJani Nikula 
3466379bc100SJani Nikula 	/*
3467379bc100SJani Nikula 	 * When called from DP MST code:
3468379bc100SJani Nikula 	 * - conn_state will be NULL
3469379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3470379bc100SJani Nikula 	 * - the main connector associated with this port
3471379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3472379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
3473379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
3474379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
3475379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3476379bc100SJani Nikula 	 *   the DP link parameteres
3477379bc100SJani Nikula 	 */
3478379bc100SJani Nikula 
34791de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
3480379bc100SJani Nikula 
3481379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3482379bc100SJani Nikula 		icl_map_plls_to_ports(encoder, crtc_state);
3483379bc100SJani Nikula 
3484379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3485379bc100SJani Nikula 
3486379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
3487ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
3488ede9771dSVille Syrjälä 					  conn_state);
3489379bc100SJani Nikula 	} else {
3490379bc100SJani Nikula 		struct intel_lspcon *lspcon =
3491b7d02c3aSVille Syrjälä 				enc_to_intel_lspcon(encoder);
3492379bc100SJani Nikula 
3493ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
3494ede9771dSVille Syrjälä 					conn_state);
3495379bc100SJani Nikula 		if (lspcon->active) {
3496379bc100SJani Nikula 			struct intel_digital_port *dig_port =
3497b7d02c3aSVille Syrjälä 					enc_to_dig_port(encoder);
3498379bc100SJani Nikula 
3499379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
3500379bc100SJani Nikula 						 crtc_state->has_infoframe,
3501379bc100SJani Nikula 						 crtc_state, conn_state);
3502379bc100SJani Nikula 		}
3503379bc100SJani Nikula 	}
3504379bc100SJani Nikula }
3505379bc100SJani Nikula 
3506379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
3507379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
3508379bc100SJani Nikula {
3509379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3510379bc100SJani Nikula 	enum port port = encoder->port;
3511379bc100SJani Nikula 	bool wait = false;
3512379bc100SJani Nikula 	u32 val;
3513379bc100SJani Nikula 
3514f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
3515379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
3516379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
3517f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
3518379bc100SJani Nikula 		wait = true;
3519379bc100SJani Nikula 	}
3520379bc100SJani Nikula 
3521e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
3522b7d02c3aSVille Syrjälä 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
35234444df6eSLucas De Marchi 
3524f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
3525379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3526379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
3527f7960e7fSJani Nikula 		intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
3528e468ff06SLucas De Marchi 	}
3529379bc100SJani Nikula 
3530379bc100SJani Nikula 	/* Disable FEC in DP Sink */
3531379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
3532379bc100SJani Nikula 
3533379bc100SJani Nikula 	if (wait)
3534379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
3535379bc100SJani Nikula }
3536379bc100SJani Nikula 
3537ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
3538ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
3539379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
3540379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
3541379bc100SJani Nikula {
3542379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3543b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3544379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
3545379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
3546379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
3547d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3548379bc100SJani Nikula 
3549c980216dSImre Deak 	if (!is_mst)
3550c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
3551c980216dSImre Deak 					old_crtc_state, old_conn_state);
3552fa37a213SGwan-gyeong Mun 
3553379bc100SJani Nikula 	/*
3554379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
3555379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
3556379bc100SJani Nikula 	 */
3557379bc100SJani Nikula 	intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
355878eaaba3SJosé Roberto de Souza 
3559c59053dcSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
3560c59053dcSJosé Roberto de Souza 		if (is_mst) {
3561c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
3562c59053dcSJosé Roberto de Souza 			u32 val;
3563c59053dcSJosé Roberto de Souza 
3564f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
3565f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
3566919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
3567919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
3568f7960e7fSJani Nikula 			intel_de_write(dev_priv,
3569f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
3570f7960e7fSJani Nikula 				       val);
3571c59053dcSJosé Roberto de Souza 		}
3572c59053dcSJosé Roberto de Souza 	} else {
3573c59053dcSJosé Roberto de Souza 		if (!is_mst)
357450a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
3575c59053dcSJosé Roberto de Souza 	}
3576379bc100SJani Nikula 
3577379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3578379bc100SJani Nikula 
35793ca8f191SJosé Roberto de Souza 	/*
35803ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
35813ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
35823ca8f191SJosé Roberto de Souza 	 * transcoder"
35833ca8f191SJosé Roberto de Souza 	 */
35843ca8f191SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
35853ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
35863ca8f191SJosé Roberto de Souza 
3587379bc100SJani Nikula 	intel_edp_panel_vdd_on(intel_dp);
3588379bc100SJani Nikula 	intel_edp_panel_off(intel_dp);
3589379bc100SJani Nikula 
3590d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
35913b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
3592379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3593379bc100SJani Nikula 						  dig_port->ddi_io_power_domain);
3594379bc100SJani Nikula 
3595379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3596379bc100SJani Nikula }
3597379bc100SJani Nikula 
3598ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
3599ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
3600379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
3601379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
3602379bc100SJani Nikula {
3603379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3604b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3605379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3606379bc100SJani Nikula 
3607379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
3608379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
3609379bc100SJani Nikula 
3610379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
3611379bc100SJani Nikula 
3612379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3613379bc100SJani Nikula 
3614379bc100SJani Nikula 	intel_display_power_put_unchecked(dev_priv,
3615379bc100SJani Nikula 					  dig_port->ddi_io_power_domain);
3616379bc100SJani Nikula 
3617379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3618379bc100SJani Nikula 
3619379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
3620379bc100SJani Nikula }
3621379bc100SJani Nikula 
3622ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
3623ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3624379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3625379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3626379bc100SJani Nikula {
3627379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3628b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
362917bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
363017bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3631379bc100SJani Nikula 
36327829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
3633773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
3634773b4b54SVille Syrjälä 
3635773b4b54SVille Syrjälä 		intel_disable_pipe(old_crtc_state);
3636773b4b54SVille Syrjälä 
3637773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
3638773b4b54SVille Syrjälä 
3639773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
3640773b4b54SVille Syrjälä 
3641773b4b54SVille Syrjälä 		if (INTEL_GEN(dev_priv) >= 9)
3642f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
3643773b4b54SVille Syrjälä 		else
36449eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
36457829c92bSVille Syrjälä 	}
3646773b4b54SVille Syrjälä 
3647379bc100SJani Nikula 	/*
3648379bc100SJani Nikula 	 * When called from DP MST code:
3649379bc100SJani Nikula 	 * - old_conn_state will be NULL
3650379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3651379bc100SJani Nikula 	 * - the main connector associated with this port
3652379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3653379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
3654379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
3655379bc100SJani Nikula 	 *   stream that was activated last, but each stream
3656379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3657379bc100SJani Nikula 	 *   the DP link parameteres
3658379bc100SJani Nikula 	 */
3659379bc100SJani Nikula 
3660379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3661ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
3662ede9771dSVille Syrjälä 					    old_conn_state);
3663379bc100SJani Nikula 	else
3664ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
3665ede9771dSVille Syrjälä 					  old_conn_state);
3666379bc100SJani Nikula 
3667379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3668379bc100SJani Nikula 		icl_unmap_plls_to_ports(encoder);
366917bef9baSVille Syrjälä 
367017bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
367117bef9baSVille Syrjälä 		intel_display_power_put_unchecked(dev_priv,
367217bef9baSVille Syrjälä 						  intel_ddi_main_link_aux_domain(dig_port));
367317bef9baSVille Syrjälä 
367417bef9baSVille Syrjälä 	if (is_tc_port)
367517bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
3676379bc100SJani Nikula }
3677379bc100SJani Nikula 
3678ede9771dSVille Syrjälä void intel_ddi_fdi_post_disable(struct intel_atomic_state *state,
3679ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
3680379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
3681379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
3682379bc100SJani Nikula {
3683379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3684379bc100SJani Nikula 	u32 val;
3685379bc100SJani Nikula 
3686379bc100SJani Nikula 	/*
3687379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
3688379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
3689379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
3690379bc100SJani Nikula 	 * originally before the BUN.
3691379bc100SJani Nikula 	 */
3692f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3693379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
3694f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3695379bc100SJani Nikula 
3696379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3697379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3698379bc100SJani Nikula 
3699f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
3700379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
3701379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
3702f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), val);
3703379bc100SJani Nikula 
3704f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3705379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
3706f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3707379bc100SJani Nikula 
3708f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
3709379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
3710f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
3711379bc100SJani Nikula }
3712379bc100SJani Nikula 
3713d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
3714d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
3715d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
3716d82a855aSVille Syrjälä {
3717d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
3718d82a855aSVille Syrjälä 	struct drm_connector *conn;
3719d82a855aSVille Syrjälä 	int i;
3720d82a855aSVille Syrjälä 
3721d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
3722d82a855aSVille Syrjälä 		return;
3723d82a855aSVille Syrjälä 
3724d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3725d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
3726d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
3727d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
3728d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
3729d82a855aSVille Syrjälä 
3730d82a855aSVille Syrjälä 		if (!slave_crtc)
3731d82a855aSVille Syrjälä 			continue;
3732d82a855aSVille Syrjälä 
3733d82a855aSVille Syrjälä 		slave_crtc_state =
3734d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
3735d82a855aSVille Syrjälä 
3736d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
3737d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
3738d82a855aSVille Syrjälä 			continue;
3739d82a855aSVille Syrjälä 
3740d82a855aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder));
3741d82a855aSVille Syrjälä 	}
3742d82a855aSVille Syrjälä 
3743d82a855aSVille Syrjälä 	usleep_range(200, 400);
3744d82a855aSVille Syrjälä 
3745d82a855aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder));
3746d82a855aSVille Syrjälä }
3747d82a855aSVille Syrjälä 
3748ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
3749ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
3750379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
3751379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
3752379bc100SJani Nikula {
3753379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3754b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3755379bc100SJani Nikula 	enum port port = encoder->port;
3756379bc100SJani Nikula 
3757379bc100SJani Nikula 	if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
3758379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3759379bc100SJani Nikula 
3760379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
37617a00e68bSGwan-gyeong Mun 	intel_psr_enable(intel_dp, crtc_state, conn_state);
37621bf3657cSGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3763379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3764379bc100SJani Nikula 
3765379bc100SJani Nikula 	if (crtc_state->has_audio)
3766379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3767d82a855aSVille Syrjälä 
3768d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
3769379bc100SJani Nikula }
3770379bc100SJani Nikula 
3771379bc100SJani Nikula static i915_reg_t
3772379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3773379bc100SJani Nikula 			       enum port port)
3774379bc100SJani Nikula {
377512c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
377612c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
377712c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
377812c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
377912c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
378012c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
3781379bc100SJani Nikula 	};
3782379bc100SJani Nikula 
37831de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, INTEL_GEN(dev_priv) < 9);
3784379bc100SJani Nikula 
37851de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
3786379bc100SJani Nikula 		port = PORT_A;
3787379bc100SJani Nikula 
378812c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
3789379bc100SJani Nikula }
3790379bc100SJani Nikula 
3791ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
3792ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3793379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3794379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3795379bc100SJani Nikula {
3796379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3797b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3798379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
3799379bc100SJani Nikula 	enum port port = encoder->port;
3800379bc100SJani Nikula 
3801379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3802379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3803379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
380447bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
380547bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3806379bc100SJani Nikula 			    connector->base.id, connector->name);
3807379bc100SJani Nikula 
3808379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
3809379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
3810379bc100SJani Nikula 		/*
3811379bc100SJani Nikula 		 * For some reason these chicken bits have been
3812379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3813379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3814379bc100SJani Nikula 		 * a specific transcoder.
3815379bc100SJani Nikula 		 */
3816379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3817379bc100SJani Nikula 		u32 val;
3818379bc100SJani Nikula 
3819f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
3820379bc100SJani Nikula 
3821379bc100SJani Nikula 		if (port == PORT_E)
3822379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3823379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3824379bc100SJani Nikula 		else
3825379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3826379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3827379bc100SJani Nikula 
3828f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3829f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3830379bc100SJani Nikula 
3831379bc100SJani Nikula 		udelay(1);
3832379bc100SJani Nikula 
3833379bc100SJani Nikula 		if (port == PORT_E)
3834379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3835379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3836379bc100SJani Nikula 		else
3837379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3838379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3839379bc100SJani Nikula 
3840f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3841379bc100SJani Nikula 	}
3842379bc100SJani Nikula 
3843379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3844379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3845379bc100SJani Nikula 	 * enabling the port.
3846379bc100SJani Nikula 	 */
3847f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3848379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3849379bc100SJani Nikula 
3850379bc100SJani Nikula 	if (crtc_state->has_audio)
3851379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3852379bc100SJani Nikula }
3853379bc100SJani Nikula 
3854ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3855ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3856379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3857379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3858379bc100SJani Nikula {
38598b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
386021fd23acSJani Nikula 
3861eed22a46SVille Syrjälä 	intel_ddi_enable_transcoder_func(encoder, crtc_state);
38627c2fedd7SVille Syrjälä 
386321fd23acSJani Nikula 	intel_enable_pipe(crtc_state);
386421fd23acSJani Nikula 
386521fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
386621fd23acSJani Nikula 
3867379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3868ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3869379bc100SJani Nikula 	else
3870ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3871379bc100SJani Nikula 
3872379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3873379bc100SJani Nikula 	if (conn_state->content_protection ==
3874379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3875d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
387667e1d5edSVille Syrjälä 				  crtc_state->cpu_transcoder,
3877d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3878379bc100SJani Nikula }
3879379bc100SJani Nikula 
3880ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3881ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3882379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3883379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3884379bc100SJani Nikula {
3885b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3886379bc100SJani Nikula 
3887379bc100SJani Nikula 	intel_dp->link_trained = false;
3888379bc100SJani Nikula 
3889379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3890379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3891379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3892379bc100SJani Nikula 
3893379bc100SJani Nikula 	intel_edp_drrs_disable(intel_dp, old_crtc_state);
3894379bc100SJani Nikula 	intel_psr_disable(intel_dp, old_crtc_state);
3895379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3896379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3897379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3898379bc100SJani Nikula 					      false);
3899379bc100SJani Nikula }
3900379bc100SJani Nikula 
3901ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3902ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3903379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3904379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3905379bc100SJani Nikula {
390647bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3907379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3908379bc100SJani Nikula 
3909379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3910379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3911379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3912379bc100SJani Nikula 
3913379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3914379bc100SJani Nikula 					       false, false))
391547bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
391647bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3917379bc100SJani Nikula 			    connector->base.id, connector->name);
3918379bc100SJani Nikula }
3919379bc100SJani Nikula 
3920ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3921ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3922379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3923379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3924379bc100SJani Nikula {
3925379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3926379bc100SJani Nikula 
3927379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3928ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3929ede9771dSVille Syrjälä 				       old_conn_state);
3930379bc100SJani Nikula 	else
3931ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3932ede9771dSVille Syrjälä 				     old_conn_state);
3933379bc100SJani Nikula }
3934379bc100SJani Nikula 
3935ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3936ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3937379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3938379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3939379bc100SJani Nikula {
3940b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3941379bc100SJani Nikula 
39420c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3943379bc100SJani Nikula 
39447a00e68bSGwan-gyeong Mun 	intel_psr_update(intel_dp, crtc_state, conn_state);
394576d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3946379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3947379bc100SJani Nikula 
3948ede9771dSVille Syrjälä 	intel_panel_update_backlight(state, encoder, crtc_state, conn_state);
3949379bc100SJani Nikula }
3950379bc100SJani Nikula 
3951ede9771dSVille Syrjälä static void intel_ddi_update_pipe(struct intel_atomic_state *state,
3952ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3953379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3954379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3955379bc100SJani Nikula {
3956d456512cSRamalingam C 
3957379bc100SJani Nikula 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3958ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3959ede9771dSVille Syrjälä 					 conn_state);
3960379bc100SJani Nikula 
3961ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3962379bc100SJani Nikula }
3963379bc100SJani Nikula 
3964379bc100SJani Nikula static void
396524a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
396624a7bfe0SImre Deak 			 struct intel_encoder *encoder,
396724a7bfe0SImre Deak 			 struct intel_crtc *crtc)
396824a7bfe0SImre Deak {
396924a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
397024a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
397124a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
397224a7bfe0SImre Deak 
39738b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
397424a7bfe0SImre Deak 
3975b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
3976b7d02c3aSVille Syrjälä 		               required_lanes);
39771326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
397824a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
397924a7bfe0SImre Deak }
398024a7bfe0SImre Deak 
398124a7bfe0SImre Deak static void
398224a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
398324a7bfe0SImre Deak 			  struct intel_encoder *encoder,
398424a7bfe0SImre Deak 			  struct intel_crtc *crtc)
398524a7bfe0SImre Deak {
3986b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
398724a7bfe0SImre Deak }
398824a7bfe0SImre Deak 
398924a7bfe0SImre Deak static void
3990ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3991ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
3992379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3993379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3994379bc100SJani Nikula {
3995379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3996b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3997d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3998d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3999379bc100SJani Nikula 
400024a7bfe0SImre Deak 	if (is_tc_port)
400124a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
400224a7bfe0SImre Deak 
400324a7bfe0SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port)
4004379bc100SJani Nikula 		intel_display_power_get(dev_priv,
4005379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
4006379bc100SJani Nikula 
40079d44dcb9SLucas De Marchi 	if (is_tc_port && dig_port->tc_mode != TC_PORT_TBT_ALT)
40089d44dcb9SLucas De Marchi 		/*
40099d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
40109d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
40119d44dcb9SLucas De Marchi 		 */
40129d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
40139d44dcb9SLucas De Marchi 	else if (IS_GEN9_LP(dev_priv))
4014379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
4015379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
4016379bc100SJani Nikula }
4017379bc100SJani Nikula 
4018379bc100SJani Nikula static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
4019379bc100SJani Nikula {
40207801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
40217801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
40227801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
402335ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
4024379bc100SJani Nikula 	bool wait = false;
4025379bc100SJani Nikula 
4026f7960e7fSJani Nikula 	dp_tp_ctl = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
402735ac28a8SLucas De Marchi 
402835ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
4029f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
403035ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
4031f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
403235ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
4033379bc100SJani Nikula 			wait = true;
4034379bc100SJani Nikula 		}
4035379bc100SJani Nikula 
403635ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
403735ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
4038f7960e7fSJani Nikula 		intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, dp_tp_ctl);
4039f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4040379bc100SJani Nikula 
4041379bc100SJani Nikula 		if (wait)
4042379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
4043379bc100SJani Nikula 	}
4044379bc100SJani Nikula 
404535ac28a8SLucas De Marchi 	dp_tp_ctl = DP_TP_CTL_ENABLE |
4046379bc100SJani Nikula 		    DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
4047379bc100SJani Nikula 	if (intel_dp->link_mst)
404835ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
4049379bc100SJani Nikula 	else {
405035ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
4051379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
405235ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
4053379bc100SJani Nikula 	}
4054f7960e7fSJani Nikula 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, dp_tp_ctl);
4055f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4056379bc100SJani Nikula 
4057379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
4058f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
4059f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
4060379bc100SJani Nikula 
4061e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
4062379bc100SJani Nikula }
4063379bc100SJani Nikula 
4064eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
4065eee3f911SVille Syrjälä 				     u8 dp_train_pat)
4066eee3f911SVille Syrjälä {
4067eee3f911SVille Syrjälä 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
4068eee3f911SVille Syrjälä 	u8 train_pat_mask = drm_dp_training_pattern_mask(intel_dp->dpcd);
4069eee3f911SVille Syrjälä 	enum port port = dp_to_dig_port(intel_dp)->base.port;
4070eee3f911SVille Syrjälä 	u32 temp;
4071eee3f911SVille Syrjälä 
4072eee3f911SVille Syrjälä 	temp = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
4073eee3f911SVille Syrjälä 
4074eee3f911SVille Syrjälä 	if (dp_train_pat & DP_LINK_SCRAMBLING_DISABLE)
4075eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_SCRAMBLE_DISABLE;
4076eee3f911SVille Syrjälä 	else
4077eee3f911SVille Syrjälä 		temp &= ~DP_TP_CTL_SCRAMBLE_DISABLE;
4078eee3f911SVille Syrjälä 
4079eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
4080eee3f911SVille Syrjälä 	switch (dp_train_pat & train_pat_mask) {
4081eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
4082eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
4083eee3f911SVille Syrjälä 		break;
4084eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
4085eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
4086eee3f911SVille Syrjälä 		break;
4087eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
4088eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
4089eee3f911SVille Syrjälä 		break;
4090eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
4091eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
4092eee3f911SVille Syrjälä 		break;
4093eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
4094eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
4095eee3f911SVille Syrjälä 		break;
4096eee3f911SVille Syrjälä 	}
4097eee3f911SVille Syrjälä 
4098eee3f911SVille Syrjälä 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, temp);
4099eee3f911SVille Syrjälä 
4100eee3f911SVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
4101eee3f911SVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
4102eee3f911SVille Syrjälä }
4103eee3f911SVille Syrjälä 
41048fdda385SVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp)
41058fdda385SVille Syrjälä {
41068fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
41078fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
41088fdda385SVille Syrjälä 	enum port port = encoder->port;
41098fdda385SVille Syrjälä 	u32 val;
41108fdda385SVille Syrjälä 
41118fdda385SVille Syrjälä 	val = intel_de_read(dev_priv, intel_dp->regs.dp_tp_ctl);
41128fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
41138fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
41148fdda385SVille Syrjälä 	intel_de_write(dev_priv, intel_dp->regs.dp_tp_ctl, val);
41158fdda385SVille Syrjälä 
41168fdda385SVille Syrjälä 	/*
41178fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
41188fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
41198fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
41208fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
41218fdda385SVille Syrjälä 	 * idle patterns to be sent.
41228fdda385SVille Syrjälä 	 */
41238fdda385SVille Syrjälä 	if (port == PORT_A && INTEL_GEN(dev_priv) < 12)
41248fdda385SVille Syrjälä 		return;
41258fdda385SVille Syrjälä 
41268fdda385SVille Syrjälä 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
41278fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
41288fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
41298fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
41308fdda385SVille Syrjälä }
41318fdda385SVille Syrjälä 
4132379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
4133379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
4134379bc100SJani Nikula {
4135379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
4136379bc100SJani Nikula 		return false;
4137379bc100SJani Nikula 
4138379bc100SJani Nikula 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO))
4139379bc100SJani Nikula 		return false;
4140379bc100SJani Nikula 
4141f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
4142379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
4143379bc100SJani Nikula }
4144379bc100SJani Nikula 
4145379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
4146379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
4147379bc100SJani Nikula {
41480fde0b1dSMatt Roper 	if (INTEL_GEN(dev_priv) >= 12 && crtc_state->port_clock > 594000)
41490fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
41500fde0b1dSMatt Roper 	else if (IS_ELKHARTLAKE(dev_priv) && crtc_state->port_clock > 594000)
41519d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
41529d5fd37eSMatt Roper 	else if (INTEL_GEN(dev_priv) >= 11 && crtc_state->port_clock > 594000)
4153379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
4154379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv) && crtc_state->port_clock > 594000)
4155379bc100SJani Nikula 		crtc_state->min_voltage_level = 2;
4156379bc100SJani Nikula }
4157379bc100SJani Nikula 
4158dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
415902d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
416002d8ea47SVille Syrjälä {
4161dc5b8ed5SVille Syrjälä 	u32 master_select;
416202d8ea47SVille Syrjälä 
4163dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
4164dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
416502d8ea47SVille Syrjälä 
416602d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
416702d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
416802d8ea47SVille Syrjälä 
4169d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
4170dc5b8ed5SVille Syrjälä 	} else {
4171dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
4172dc5b8ed5SVille Syrjälä 
4173dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
4174dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
4175dc5b8ed5SVille Syrjälä 
4176dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
4177dc5b8ed5SVille Syrjälä 	}
417802d8ea47SVille Syrjälä 
417902d8ea47SVille Syrjälä 	if (master_select == 0)
418002d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
418102d8ea47SVille Syrjälä 	else
418202d8ea47SVille Syrjälä 		return master_select - 1;
418302d8ea47SVille Syrjälä }
418402d8ea47SVille Syrjälä 
4185dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
418602d8ea47SVille Syrjälä {
418702d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
418802d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
418902d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
419002d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
419102d8ea47SVille Syrjälä 
419202d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
4193dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
419402d8ea47SVille Syrjälä 
419502d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
419602d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
419702d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
419802d8ea47SVille Syrjälä 
419902d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
420002d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
420102d8ea47SVille Syrjälä 								   power_domain);
420202d8ea47SVille Syrjälä 
420302d8ea47SVille Syrjälä 		if (!trans_wakeref)
420402d8ea47SVille Syrjälä 			continue;
420502d8ea47SVille Syrjälä 
4206dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
420702d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
420802d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
420902d8ea47SVille Syrjälä 
421002d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
421102d8ea47SVille Syrjälä 	}
421202d8ea47SVille Syrjälä 
421302d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
421402d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
421502d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
421602d8ea47SVille Syrjälä }
421702d8ea47SVille Syrjälä 
4218379bc100SJani Nikula void intel_ddi_get_config(struct intel_encoder *encoder,
4219379bc100SJani Nikula 			  struct intel_crtc_state *pipe_config)
4220379bc100SJani Nikula {
4221379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
42222225f3c6SMaarten Lankhorst 	struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->uapi.crtc);
4223379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
4224edcb9028SJosé Roberto de Souza 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4225379bc100SJani Nikula 	u32 temp, flags = 0;
4226379bc100SJani Nikula 
4227379bc100SJani Nikula 	/* XXX: DSI transcoder paranoia */
42281de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
4229379bc100SJani Nikula 		return;
4230379bc100SJani Nikula 
4231fbacb15eSJani Nikula 	intel_dsc_get_config(encoder, pipe_config);
4232fbacb15eSJani Nikula 
4233f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
4234379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
4235379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
4236379bc100SJani Nikula 	else
4237379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
4238379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
4239379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
4240379bc100SJani Nikula 	else
4241379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
4242379bc100SJani Nikula 
42431326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
4244379bc100SJani Nikula 
4245379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
4246379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
4247379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
4248379bc100SJani Nikula 		break;
4249379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
4250379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
4251379bc100SJani Nikula 		break;
4252379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
4253379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
4254379bc100SJani Nikula 		break;
4255379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
4256379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
4257379bc100SJani Nikula 		break;
4258379bc100SJani Nikula 	default:
4259379bc100SJani Nikula 		break;
4260379bc100SJani Nikula 	}
4261379bc100SJani Nikula 
4262379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
4263379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
4264379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
4265379bc100SJani Nikula 
4266379bc100SJani Nikula 		pipe_config->infoframes.enable |=
4267379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4268379bc100SJani Nikula 
4269379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
4270379bc100SJani Nikula 			pipe_config->has_infoframe = true;
4271379bc100SJani Nikula 
4272379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
4273379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
4274379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
4275379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
4276379bc100SJani Nikula 		/* fall through */
4277379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
4278379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
4279379bc100SJani Nikula 		pipe_config->lane_count = 4;
4280379bc100SJani Nikula 		break;
4281379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
4282379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
4283379bc100SJani Nikula 		break;
4284379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
4285379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
4286379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
4287379bc100SJani Nikula 		else
4288379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
4289379bc100SJani Nikula 		pipe_config->lane_count =
4290379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
4291379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
42928aa940c8SMaarten Lankhorst 
42938aa940c8SMaarten Lankhorst 		if (INTEL_GEN(dev_priv) >= 11) {
42948aa940c8SMaarten Lankhorst 			i915_reg_t dp_tp_ctl;
42958aa940c8SMaarten Lankhorst 
42968aa940c8SMaarten Lankhorst 			if (IS_GEN(dev_priv, 11))
42978aa940c8SMaarten Lankhorst 				dp_tp_ctl = DP_TP_CTL(encoder->port);
42988aa940c8SMaarten Lankhorst 			else
42998aa940c8SMaarten Lankhorst 				dp_tp_ctl = TGL_DP_TP_CTL(pipe_config->cpu_transcoder);
43008aa940c8SMaarten Lankhorst 
43018aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
4302f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
43038aa940c8SMaarten Lankhorst 
430447bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
430547bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
43068aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
43078aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
43088aa940c8SMaarten Lankhorst 		}
43098aa940c8SMaarten Lankhorst 
4310dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
4311dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4312dee66f3eSGwan-gyeong Mun 
4313379bc100SJani Nikula 		break;
4314379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
4315379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
4316379bc100SJani Nikula 		pipe_config->lane_count =
4317379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
43186671c367SJosé Roberto de Souza 
43196671c367SJosé Roberto de Souza 		if (INTEL_GEN(dev_priv) >= 12)
43206671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
43216671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
43226671c367SJosé Roberto de Souza 
4323379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
4324dee66f3eSGwan-gyeong Mun 
4325dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
4326dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4327379bc100SJani Nikula 		break;
4328379bc100SJani Nikula 	default:
4329379bc100SJani Nikula 		break;
4330379bc100SJani Nikula 	}
4331379bc100SJani Nikula 
4332f153478dSImre Deak 	if (INTEL_GEN(dev_priv) >= 12) {
4333f153478dSImre Deak 		enum transcoder transcoder =
4334f153478dSImre Deak 			intel_dp_mst_is_slave_trans(pipe_config) ?
4335f153478dSImre Deak 			pipe_config->mst_master_transcoder :
4336f153478dSImre Deak 			pipe_config->cpu_transcoder;
4337f153478dSImre Deak 
4338f153478dSImre Deak 		intel_dp->regs.dp_tp_ctl = TGL_DP_TP_CTL(transcoder);
4339f153478dSImre Deak 		intel_dp->regs.dp_tp_status = TGL_DP_TP_STATUS(transcoder);
4340f153478dSImre Deak 	}
4341f153478dSImre Deak 
4342379bc100SJani Nikula 	pipe_config->has_audio =
4343379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
4344379bc100SJani Nikula 
4345379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
4346379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
4347379bc100SJani Nikula 		/*
4348379bc100SJani Nikula 		 * This is a big fat ugly hack.
4349379bc100SJani Nikula 		 *
4350379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
4351379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
4352379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
4353379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
4354379bc100SJani Nikula 		 * max, not what it tells us to use.
4355379bc100SJani Nikula 		 *
4356379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
4357379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
4358379bc100SJani Nikula 		 * load.
4359379bc100SJani Nikula 		 */
436047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
436147bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
4362379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
4363379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
4364379bc100SJani Nikula 	}
4365379bc100SJani Nikula 
4366379bc100SJani Nikula 	intel_ddi_clock_get(encoder, pipe_config);
4367379bc100SJani Nikula 
4368379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4369379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4370379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
4371379bc100SJani Nikula 
4372379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4373379bc100SJani Nikula 
4374379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
4375379bc100SJani Nikula 
4376379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4377379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
4378379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
4379379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4380379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
4381379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
4382379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4383379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
4384379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
4385379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4386379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
4387379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
438802d8ea47SVille Syrjälä 
4389dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 8)
4390dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
4391dee66f3eSGwan-gyeong Mun 
4392dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
43932c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
4394379bc100SJani Nikula }
4395379bc100SJani Nikula 
4396379bc100SJani Nikula static enum intel_output_type
4397379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
4398379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
4399379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
4400379bc100SJani Nikula {
4401379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
4402379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
4403379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
4404379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
4405379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
4406379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
4407379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
4408379bc100SJani Nikula 	default:
4409379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
4410379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
4411379bc100SJani Nikula 	}
4412379bc100SJani Nikula }
4413379bc100SJani Nikula 
4414379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
4415379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
4416379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
4417379bc100SJani Nikula {
44182225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
4419379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4420379bc100SJani Nikula 	enum port port = encoder->port;
4421379bc100SJani Nikula 	int ret;
4422379bc100SJani Nikula 
442310cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
4424379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
4425379bc100SJani Nikula 
4426bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
4427379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
4428bdacf087SAnshuman Gupta 	} else {
4429379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
4430bdacf087SAnshuman Gupta 	}
4431bdacf087SAnshuman Gupta 
4432379bc100SJani Nikula 	if (ret)
4433379bc100SJani Nikula 		return ret;
4434379bc100SJani Nikula 
4435379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
4436379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
4437379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
4438379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
4439379bc100SJani Nikula 			pipe_config->crc_enabled;
4440379bc100SJani Nikula 
4441379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4442379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4443379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
4444379bc100SJani Nikula 
4445379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4446379bc100SJani Nikula 
4447379bc100SJani Nikula 	return 0;
4448379bc100SJani Nikula }
4449379bc100SJani Nikula 
4450b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
4451b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
4452b50a1aa6SManasi Navare {
4453b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
4454b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
4455b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
4456b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
4457b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
4458b50a1aa6SManasi Navare }
4459b50a1aa6SManasi Navare 
4460b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
4461b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
4462b50a1aa6SManasi Navare {
4463b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
4464b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
4465b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
4466b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
4467b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
4468b50a1aa6SManasi Navare }
4469b50a1aa6SManasi Navare 
4470b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
4471b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
4472b50a1aa6SManasi Navare {
4473b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
4474b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
4475b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
4476b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
4477b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
4478b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
4479b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
4480b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
4481b50a1aa6SManasi Navare }
4482b50a1aa6SManasi Navare 
4483b50a1aa6SManasi Navare static u8
4484b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
4485b50a1aa6SManasi Navare 				int tile_group_id)
4486b50a1aa6SManasi Navare {
4487b50a1aa6SManasi Navare 	struct drm_connector *connector;
4488b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
4489b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
4490b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
4491b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
4492b50a1aa6SManasi Navare 	u8 transcoders = 0;
4493b50a1aa6SManasi Navare 	int i;
4494b50a1aa6SManasi Navare 
4495dc5b8ed5SVille Syrjälä 	/*
4496dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
4497dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
4498dc5b8ed5SVille Syrjälä 	 */
4499dc5b8ed5SVille Syrjälä 	if (INTEL_GEN(dev_priv) < 9)
4500b50a1aa6SManasi Navare 		return 0;
4501b50a1aa6SManasi Navare 
4502b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
4503b50a1aa6SManasi Navare 		return 0;
4504b50a1aa6SManasi Navare 
4505b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
4506b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
4507b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
4508b50a1aa6SManasi Navare 
4509b50a1aa6SManasi Navare 		if (!crtc)
4510b50a1aa6SManasi Navare 			continue;
4511b50a1aa6SManasi Navare 
4512b50a1aa6SManasi Navare 		if (!connector->has_tile ||
4513b50a1aa6SManasi Navare 		    connector->tile_group->id !=
4514b50a1aa6SManasi Navare 		    tile_group_id)
4515b50a1aa6SManasi Navare 			continue;
4516b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
4517b50a1aa6SManasi Navare 							     crtc);
4518b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
4519b50a1aa6SManasi Navare 						crtc_state))
4520b50a1aa6SManasi Navare 			continue;
4521b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
4522b50a1aa6SManasi Navare 	}
4523b50a1aa6SManasi Navare 
4524b50a1aa6SManasi Navare 	return transcoders;
4525b50a1aa6SManasi Navare }
4526b50a1aa6SManasi Navare 
4527b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
4528b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
4529b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
4530b50a1aa6SManasi Navare {
453147bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4532b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
4533b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
4534b50a1aa6SManasi Navare 
453547bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
4536b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
4537b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
4538b50a1aa6SManasi Navare 
4539b50a1aa6SManasi Navare 	if (connector->has_tile)
4540b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
4541b50a1aa6SManasi Navare 									connector->tile_group->id);
4542b50a1aa6SManasi Navare 
4543b50a1aa6SManasi Navare 	/*
4544b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
4545b50a1aa6SManasi Navare 	 * make them a master always when present
4546b50a1aa6SManasi Navare 	 */
4547b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
4548b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
4549b50a1aa6SManasi Navare 	else
4550b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
4551b50a1aa6SManasi Navare 
4552b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
4553b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
4554b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
4555b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
4556b50a1aa6SManasi Navare 	}
4557b50a1aa6SManasi Navare 
4558b50a1aa6SManasi Navare 	return 0;
4559b50a1aa6SManasi Navare }
4560b50a1aa6SManasi Navare 
4561379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
4562379bc100SJani Nikula {
4563b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
4564379bc100SJani Nikula 
4565379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
4566379bc100SJani Nikula 
4567379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
4568379bc100SJani Nikula 	kfree(dig_port);
4569379bc100SJani Nikula }
4570379bc100SJani Nikula 
4571379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
457232691b58SImre Deak 	.reset = intel_dp_encoder_reset,
4573379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
4574379bc100SJani Nikula };
4575379bc100SJani Nikula 
4576379bc100SJani Nikula static struct intel_connector *
45777801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
4578379bc100SJani Nikula {
45797801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4580379bc100SJani Nikula 	struct intel_connector *connector;
45817801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4582379bc100SJani Nikula 
4583379bc100SJani Nikula 	connector = intel_connector_alloc();
4584379bc100SJani Nikula 	if (!connector)
4585379bc100SJani Nikula 		return NULL;
4586379bc100SJani Nikula 
45877801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
45887801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
45897801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
45907801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
4591eee3f911SVille Syrjälä 
4592fb83f72cSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 12)
45937801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = tgl_set_signal_levels;
4594fb83f72cSVille Syrjälä 	else if (INTEL_GEN(dev_priv) >= 11)
45957801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = icl_set_signal_levels;
4596fb83f72cSVille Syrjälä 	else if (IS_CANNONLAKE(dev_priv))
45977801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = cnl_set_signal_levels;
4598fb83f72cSVille Syrjälä 	else if (IS_GEN9_LP(dev_priv))
45997801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = bxt_set_signal_levels;
4600fb83f72cSVille Syrjälä 	else
46017801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = hsw_set_signal_levels;
4602fb83f72cSVille Syrjälä 
46037801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
46047801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
460553de0a20SVille Syrjälä 
4606edcb9028SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) < 12) {
46077801f3b7SLucas De Marchi 		dig_port->dp.regs.dp_tp_ctl = DP_TP_CTL(port);
46087801f3b7SLucas De Marchi 		dig_port->dp.regs.dp_tp_status = DP_TP_STATUS(port);
4609edcb9028SJosé Roberto de Souza 	}
4610379bc100SJani Nikula 
46117801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
4612379bc100SJani Nikula 		kfree(connector);
4613379bc100SJani Nikula 		return NULL;
4614379bc100SJani Nikula 	}
4615379bc100SJani Nikula 
4616379bc100SJani Nikula 	return connector;
4617379bc100SJani Nikula }
4618379bc100SJani Nikula 
4619379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
4620379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
4621379bc100SJani Nikula {
4622379bc100SJani Nikula 	struct drm_atomic_state *state;
4623379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
4624379bc100SJani Nikula 	int ret;
4625379bc100SJani Nikula 
4626379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
4627379bc100SJani Nikula 	if (!state)
4628379bc100SJani Nikula 		return -ENOMEM;
4629379bc100SJani Nikula 
4630379bc100SJani Nikula 	state->acquire_ctx = ctx;
4631379bc100SJani Nikula 
4632379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
4633379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
4634379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
4635379bc100SJani Nikula 		goto out;
4636379bc100SJani Nikula 	}
4637379bc100SJani Nikula 
4638379bc100SJani Nikula 	crtc_state->connectors_changed = true;
4639379bc100SJani Nikula 
4640379bc100SJani Nikula 	ret = drm_atomic_commit(state);
4641379bc100SJani Nikula out:
4642379bc100SJani Nikula 	drm_atomic_state_put(state);
4643379bc100SJani Nikula 
4644379bc100SJani Nikula 	return ret;
4645379bc100SJani Nikula }
4646379bc100SJani Nikula 
4647379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4648379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4649379bc100SJani Nikula {
4650379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4651b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
4652379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4653379bc100SJani Nikula 	struct i2c_adapter *adapter =
4654379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4655379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4656379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4657379bc100SJani Nikula 	struct intel_crtc *crtc;
4658379bc100SJani Nikula 	u8 config;
4659379bc100SJani Nikula 	int ret;
4660379bc100SJani Nikula 
4661379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4662379bc100SJani Nikula 		return 0;
4663379bc100SJani Nikula 
4664379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4665379bc100SJani Nikula 			       ctx);
4666379bc100SJani Nikula 	if (ret)
4667379bc100SJani Nikula 		return ret;
4668379bc100SJani Nikula 
4669379bc100SJani Nikula 	conn_state = connector->base.state;
4670379bc100SJani Nikula 
4671379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4672379bc100SJani Nikula 	if (!crtc)
4673379bc100SJani Nikula 		return 0;
4674379bc100SJani Nikula 
4675379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4676379bc100SJani Nikula 	if (ret)
4677379bc100SJani Nikula 		return ret;
4678379bc100SJani Nikula 
4679379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4680379bc100SJani Nikula 
46811de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
46821de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4683379bc100SJani Nikula 
46841326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4685379bc100SJani Nikula 		return 0;
4686379bc100SJani Nikula 
4687379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4688379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4689379bc100SJani Nikula 		return 0;
4690379bc100SJani Nikula 
4691379bc100SJani Nikula 	if (conn_state->commit &&
4692379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4693379bc100SJani Nikula 		return 0;
4694379bc100SJani Nikula 
4695379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4696379bc100SJani Nikula 	if (ret < 0) {
469747bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
469847bdb1caSJani Nikula 			ret);
4699379bc100SJani Nikula 		return 0;
4700379bc100SJani Nikula 	}
4701379bc100SJani Nikula 
4702379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4703379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4704379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4705379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4706379bc100SJani Nikula 		return 0;
4707379bc100SJani Nikula 
4708379bc100SJani Nikula 	/*
4709379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4710379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4711379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4712379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4713379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4714379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4715379bc100SJani Nikula 	 * the SCDC settings on the fly.
4716379bc100SJani Nikula 	 */
4717379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4718379bc100SJani Nikula }
4719379bc100SJani Nikula 
47203944709dSImre Deak static enum intel_hotplug_state
47213944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
47228c8919c7SImre Deak 		  struct intel_connector *connector)
4723379bc100SJani Nikula {
4724b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4725b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4726b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4727b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4728379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
47293944709dSImre Deak 	enum intel_hotplug_state state;
4730379bc100SJani Nikula 	int ret;
4731379bc100SJani Nikula 
47328c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4733379bc100SJani Nikula 
4734379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4735379bc100SJani Nikula 
4736379bc100SJani Nikula 	for (;;) {
4737379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4738379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4739379bc100SJani Nikula 		else
4740379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4741379bc100SJani Nikula 
4742379bc100SJani Nikula 		if (ret == -EDEADLK) {
4743379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4744379bc100SJani Nikula 			continue;
4745379bc100SJani Nikula 		}
4746379bc100SJani Nikula 
4747379bc100SJani Nikula 		break;
4748379bc100SJani Nikula 	}
4749379bc100SJani Nikula 
4750379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4751379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
47523a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
47533a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4754379bc100SJani Nikula 
4755bb80c925SJosé Roberto de Souza 	/*
4756bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4757bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4758bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4759bb80c925SJosé Roberto de Souza 	 *
4760bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4761bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4762bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4763bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4764bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4765bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4766bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4767bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4768bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4769bb80c925SJosé Roberto de Souza 	 * status.
4770b4df5405SImre Deak 	 *
4771b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4772b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4773b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4774b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4775b4df5405SImre Deak 	 * connectors to account for this delay.
4776bb80c925SJosé Roberto de Souza 	 */
4777b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4778b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4779bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4780bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4781bb80c925SJosé Roberto de Souza 
47823944709dSImre Deak 	return state;
4783379bc100SJani Nikula }
4784379bc100SJani Nikula 
4785edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4786edc0e09cSVille Syrjälä {
4787edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4788c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4789edc0e09cSVille Syrjälä 
4790edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4791edc0e09cSVille Syrjälä }
4792edc0e09cSVille Syrjälä 
4793edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4794edc0e09cSVille Syrjälä {
4795edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4796c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4797edc0e09cSVille Syrjälä 
4798c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4799edc0e09cSVille Syrjälä }
4800edc0e09cSVille Syrjälä 
4801edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4802edc0e09cSVille Syrjälä {
4803edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4804c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4805edc0e09cSVille Syrjälä 
4806edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4807edc0e09cSVille Syrjälä }
4808edc0e09cSVille Syrjälä 
4809379bc100SJani Nikula static struct intel_connector *
48107801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4811379bc100SJani Nikula {
4812379bc100SJani Nikula 	struct intel_connector *connector;
48137801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4814379bc100SJani Nikula 
4815379bc100SJani Nikula 	connector = intel_connector_alloc();
4816379bc100SJani Nikula 	if (!connector)
4817379bc100SJani Nikula 		return NULL;
4818379bc100SJani Nikula 
48197801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
48207801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4821379bc100SJani Nikula 
4822379bc100SJani Nikula 	return connector;
4823379bc100SJani Nikula }
4824379bc100SJani Nikula 
48257801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4826379bc100SJani Nikula {
48277801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4828379bc100SJani Nikula 
48297801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4830379bc100SJani Nikula 		return false;
4831379bc100SJani Nikula 
48327801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4833379bc100SJani Nikula 		return false;
4834379bc100SJani Nikula 
4835379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4836379bc100SJani Nikula 	 *                     supported configuration
4837379bc100SJani Nikula 	 */
4838379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4839379bc100SJani Nikula 		return true;
4840379bc100SJani Nikula 
4841379bc100SJani Nikula 	/* Cannonlake: Most of SKUs don't support DDI_E, and the only
4842379bc100SJani Nikula 	 *             one who does also have a full A/E split called
4843379bc100SJani Nikula 	 *             DDI_F what makes DDI_E useless. However for this
4844379bc100SJani Nikula 	 *             case let's trust VBT info.
4845379bc100SJani Nikula 	 */
4846379bc100SJani Nikula 	if (IS_CANNONLAKE(dev_priv) &&
4847379bc100SJani Nikula 	    !intel_bios_is_port_present(dev_priv, PORT_E))
4848379bc100SJani Nikula 		return true;
4849379bc100SJani Nikula 
4850379bc100SJani Nikula 	return false;
4851379bc100SJani Nikula }
4852379bc100SJani Nikula 
4853379bc100SJani Nikula static int
48547801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4855379bc100SJani Nikula {
48567801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
48577801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4858379bc100SJani Nikula 	int max_lanes = 4;
4859379bc100SJani Nikula 
4860379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4861379bc100SJani Nikula 		return max_lanes;
4862379bc100SJani Nikula 
4863379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4864f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4865379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4866379bc100SJani Nikula 		else
4867379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4868379bc100SJani Nikula 			max_lanes = 2;
4869379bc100SJani Nikula 	}
4870379bc100SJani Nikula 
4871379bc100SJani Nikula 	/*
4872379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4873379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4874379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4875379bc100SJani Nikula 	 */
48767801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
487747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
487847bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
48797801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4880379bc100SJani Nikula 		max_lanes = 4;
4881379bc100SJani Nikula 	}
4882379bc100SJani Nikula 
4883379bc100SJani Nikula 	return max_lanes;
4884379bc100SJani Nikula }
4885379bc100SJani Nikula 
4886379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4887379bc100SJani Nikula {
48887801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
488970dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
4890379bc100SJani Nikula 	bool init_hdmi, init_dp, init_lspcon = false;
4891d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4892379bc100SJani Nikula 
4893c5faae5aSJani Nikula 	init_hdmi = intel_bios_port_supports_dvi(dev_priv, port) ||
4894c5faae5aSJani Nikula 		intel_bios_port_supports_hdmi(dev_priv, port);
4895c5faae5aSJani Nikula 	init_dp = intel_bios_port_supports_dp(dev_priv, port);
4896379bc100SJani Nikula 
4897379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4898379bc100SJani Nikula 		/*
4899379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4900379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4901379bc100SJani Nikula 		 * is initialized before lspcon.
4902379bc100SJani Nikula 		 */
4903379bc100SJani Nikula 		init_dp = true;
4904379bc100SJani Nikula 		init_lspcon = true;
4905379bc100SJani Nikula 		init_hdmi = false;
490647bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
490747bdb1caSJani Nikula 			    port_name(port));
4908379bc100SJani Nikula 	}
4909379bc100SJani Nikula 
4910379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
491147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
491247bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4913379bc100SJani Nikula 			    port_name(port));
4914379bc100SJani Nikula 		return;
4915379bc100SJani Nikula 	}
4916379bc100SJani Nikula 
49177801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
49187801f3b7SLucas De Marchi 	if (!dig_port)
4919379bc100SJani Nikula 		return;
4920379bc100SJani Nikula 
49217801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4922379bc100SJani Nikula 
492370dfbc29SLucas De Marchi 	drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4924379bc100SJani Nikula 			 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
4925379bc100SJani Nikula 
492670dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
492770dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
492870dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4929b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
493070dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
493170dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
493270dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
493370dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
493470dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
493570dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
493670dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
493770dfbc29SLucas De Marchi 	encoder->get_config = intel_ddi_get_config;
493870dfbc29SLucas De Marchi 	encoder->suspend = intel_dp_encoder_suspend;
493970dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
494070dfbc29SLucas De Marchi 
494170dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
494270dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
494370dfbc29SLucas De Marchi 	encoder->port = port;
494470dfbc29SLucas De Marchi 	encoder->cloneable = 0;
494570dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4946379bc100SJani Nikula 
4947379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
49487801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
49497801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
49507801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4951379bc100SJani Nikula 	else
49527801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
49537801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
49547801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
495570dfbc29SLucas De Marchi 
49567801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
49577801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
49587801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4959379bc100SJani Nikula 
4960d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4961c5faae5aSJani Nikula 		bool is_legacy =
4962c5faae5aSJani Nikula 			!intel_bios_port_supports_typec_usb(dev_priv, port) &&
4963c5faae5aSJani Nikula 			!intel_bios_port_supports_tbt(dev_priv, port);
4964379bc100SJani Nikula 
49657801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
496624a7bfe0SImre Deak 
496770dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
496870dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
4969ab7bc4e1SImre Deak 	}
4970ab7bc4e1SImre Deak 
49711de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
49727801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
4973327f8d8cSLucas De Marchi 					      port - PORT_A;
4974379bc100SJani Nikula 
4975379bc100SJani Nikula 	if (init_dp) {
49767801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
4977379bc100SJani Nikula 			goto err;
4978379bc100SJani Nikula 
49797801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
4980379bc100SJani Nikula 	}
4981379bc100SJani Nikula 
4982379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4983379bc100SJani Nikula 	 * case we have some really bad VBTs... */
498470dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
49857801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
4986379bc100SJani Nikula 			goto err;
4987379bc100SJani Nikula 	}
4988379bc100SJani Nikula 
4989379bc100SJani Nikula 	if (init_lspcon) {
49907801f3b7SLucas De Marchi 		if (lspcon_init(dig_port))
4991379bc100SJani Nikula 			/* TODO: handle hdmi info frame part */
499247bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
499347bdb1caSJani Nikula 				    "LSPCON init success on port %c\n",
4994379bc100SJani Nikula 				    port_name(port));
4995379bc100SJani Nikula 		else
4996379bc100SJani Nikula 			/*
4997379bc100SJani Nikula 			 * LSPCON init faied, but DP init was success, so
4998379bc100SJani Nikula 			 * lets try to drive as DP++ port.
4999379bc100SJani Nikula 			 */
500047bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
500147bdb1caSJani Nikula 				"LSPCON init failed on port %c\n",
5002379bc100SJani Nikula 				port_name(port));
5003379bc100SJani Nikula 	}
5004379bc100SJani Nikula 
5005edc0e09cSVille Syrjälä 	if (INTEL_GEN(dev_priv) >= 11) {
5006edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
50077801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
5008edc0e09cSVille Syrjälä 		else
50097801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5010c7e8a3d6SVille Syrjälä 	} else if (INTEL_GEN(dev_priv) >= 8) {
5011c7e8a3d6SVille Syrjälä 		if (port == PORT_A || IS_GEN9_LP(dev_priv))
50127801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
5013edc0e09cSVille Syrjälä 		else
50147801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5015edc0e09cSVille Syrjälä 	} else {
5016c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
50177801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
5018edc0e09cSVille Syrjälä 		else
50197801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
5020edc0e09cSVille Syrjälä 	}
5021edc0e09cSVille Syrjälä 
50227801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
5023379bc100SJani Nikula 
5024379bc100SJani Nikula 	return;
5025379bc100SJani Nikula 
5026379bc100SJani Nikula err:
502770dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
50287801f3b7SLucas De Marchi 	kfree(dig_port);
5029379bc100SJani Nikula }
5030