xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 32691b58d157584b18faabf25cce755c1e31c370)
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"
35379bc100SJani Nikula #include "intel_dp.h"
36379bc100SJani Nikula #include "intel_dp_link_training.h"
37379bc100SJani Nikula #include "intel_dpio_phy.h"
38379bc100SJani Nikula #include "intel_drv.h"
39379bc100SJani Nikula #include "intel_dsi.h"
40379bc100SJani Nikula #include "intel_fifo_underrun.h"
41379bc100SJani Nikula #include "intel_gmbus.h"
42379bc100SJani Nikula #include "intel_hdcp.h"
43379bc100SJani Nikula #include "intel_hdmi.h"
44379bc100SJani Nikula #include "intel_hotplug.h"
45379bc100SJani Nikula #include "intel_lspcon.h"
46379bc100SJani Nikula #include "intel_panel.h"
47379bc100SJani Nikula #include "intel_psr.h"
48bc85328fSImre Deak #include "intel_tc.h"
49379bc100SJani Nikula #include "intel_vdsc.h"
50379bc100SJani Nikula 
51379bc100SJani Nikula struct ddi_buf_trans {
52379bc100SJani Nikula 	u32 trans1;	/* balance leg enable, de-emph level */
53379bc100SJani Nikula 	u32 trans2;	/* vref sel, vswing */
54379bc100SJani Nikula 	u8 i_boost;	/* SKL: I_boost; valid: 0x0, 0x1, 0x3, 0x7 */
55379bc100SJani Nikula };
56379bc100SJani Nikula 
57379bc100SJani Nikula static const u8 index_to_dp_signal_levels[] = {
58379bc100SJani Nikula 	[0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
59379bc100SJani Nikula 	[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
60379bc100SJani Nikula 	[2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
61379bc100SJani Nikula 	[3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
62379bc100SJani Nikula 	[4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
63379bc100SJani Nikula 	[5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
64379bc100SJani Nikula 	[6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
65379bc100SJani Nikula 	[7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
66379bc100SJani Nikula 	[8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
67379bc100SJani Nikula 	[9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
68379bc100SJani Nikula };
69379bc100SJani Nikula 
70379bc100SJani Nikula /* HDMI/DVI modes ignore everything but the last 2 items. So we share
71379bc100SJani Nikula  * them for both DP and FDI transports, allowing those ports to
72379bc100SJani Nikula  * automatically adapt to HDMI connections as well
73379bc100SJani Nikula  */
74379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_dp[] = {
75379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0006000E, 0x0 },
76379bc100SJani Nikula 	{ 0x00D75FFF, 0x0005000A, 0x0 },
77379bc100SJani Nikula 	{ 0x00C30FFF, 0x00040006, 0x0 },
78379bc100SJani Nikula 	{ 0x80AAAFFF, 0x000B0000, 0x0 },
79379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000A, 0x0 },
80379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0004, 0x0 },
81379bc100SJani Nikula 	{ 0x80C30FFF, 0x000B0000, 0x0 },
82379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00040006, 0x0 },
83379bc100SJani Nikula 	{ 0x80D75FFF, 0x000B0000, 0x0 },
84379bc100SJani Nikula };
85379bc100SJani Nikula 
86379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_fdi[] = {
87379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },
88379bc100SJani Nikula 	{ 0x00D75FFF, 0x000F000A, 0x0 },
89379bc100SJani Nikula 	{ 0x00C30FFF, 0x00060006, 0x0 },
90379bc100SJani Nikula 	{ 0x00AAAFFF, 0x001E0000, 0x0 },
91379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000F000A, 0x0 },
92379bc100SJani Nikula 	{ 0x00D75FFF, 0x00160004, 0x0 },
93379bc100SJani Nikula 	{ 0x00C30FFF, 0x001E0000, 0x0 },
94379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00060006, 0x0 },
95379bc100SJani Nikula 	{ 0x00D75FFF, 0x001E0000, 0x0 },
96379bc100SJani Nikula };
97379bc100SJani Nikula 
98379bc100SJani Nikula static const struct ddi_buf_trans hsw_ddi_translations_hdmi[] = {
99379bc100SJani Nikula 					/* Idx	NT mV d	T mV d	db	*/
100379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0006000E, 0x0 },/* 0:	400	400	0	*/
101379bc100SJani Nikula 	{ 0x00E79FFF, 0x000E000C, 0x0 },/* 1:	400	500	2	*/
102379bc100SJani Nikula 	{ 0x00D75FFF, 0x0005000A, 0x0 },/* 2:	400	600	3.5	*/
103379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000A, 0x0 },/* 3:	600	600	0	*/
104379bc100SJani Nikula 	{ 0x00E79FFF, 0x001D0007, 0x0 },/* 4:	600	750	2	*/
105379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0004, 0x0 },/* 5:	600	900	3.5	*/
106379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00040006, 0x0 },/* 6:	800	800	0	*/
107379bc100SJani Nikula 	{ 0x80E79FFF, 0x00030002, 0x0 },/* 7:	800	1000	2	*/
108379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00140005, 0x0 },/* 8:	850	850	0	*/
109379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000C0004, 0x0 },/* 9:	900	900	0	*/
110379bc100SJani Nikula 	{ 0x00FFFFFF, 0x001C0003, 0x0 },/* 10:	950	950	0	*/
111379bc100SJani Nikula 	{ 0x80FFFFFF, 0x00030002, 0x0 },/* 11:	1000	1000	0	*/
112379bc100SJani Nikula };
113379bc100SJani Nikula 
114379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_edp[] = {
115379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00000012, 0x0 },
116379bc100SJani Nikula 	{ 0x00EBAFFF, 0x00020011, 0x0 },
117379bc100SJani Nikula 	{ 0x00C71FFF, 0x0006000F, 0x0 },
118379bc100SJani Nikula 	{ 0x00AAAFFF, 0x000E000A, 0x0 },
119379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00020011, 0x0 },
120379bc100SJani Nikula 	{ 0x00DB6FFF, 0x0005000F, 0x0 },
121379bc100SJani Nikula 	{ 0x00BEEFFF, 0x000A000C, 0x0 },
122379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0005000F, 0x0 },
123379bc100SJani Nikula 	{ 0x00DB6FFF, 0x000A000C, 0x0 },
124379bc100SJani Nikula };
125379bc100SJani Nikula 
126379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_dp[] = {
127379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },
128379bc100SJani Nikula 	{ 0x00D75FFF, 0x000E000A, 0x0 },
129379bc100SJani Nikula 	{ 0x00BEFFFF, 0x00140006, 0x0 },
130379bc100SJani Nikula 	{ 0x80B2CFFF, 0x001B0002, 0x0 },
131379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000E000A, 0x0 },
132379bc100SJani Nikula 	{ 0x00DB6FFF, 0x00160005, 0x0 },
133379bc100SJani Nikula 	{ 0x80C71FFF, 0x001A0002, 0x0 },
134379bc100SJani Nikula 	{ 0x00F7DFFF, 0x00180004, 0x0 },
135379bc100SJani Nikula 	{ 0x80D75FFF, 0x001B0002, 0x0 },
136379bc100SJani Nikula };
137379bc100SJani Nikula 
138379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_fdi[] = {
139379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0001000E, 0x0 },
140379bc100SJani Nikula 	{ 0x00D75FFF, 0x0004000A, 0x0 },
141379bc100SJani Nikula 	{ 0x00C30FFF, 0x00070006, 0x0 },
142379bc100SJani Nikula 	{ 0x00AAAFFF, 0x000C0000, 0x0 },
143379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0004000A, 0x0 },
144379bc100SJani Nikula 	{ 0x00D75FFF, 0x00090004, 0x0 },
145379bc100SJani Nikula 	{ 0x00C30FFF, 0x000C0000, 0x0 },
146379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00070006, 0x0 },
147379bc100SJani Nikula 	{ 0x00D75FFF, 0x000C0000, 0x0 },
148379bc100SJani Nikula };
149379bc100SJani Nikula 
150379bc100SJani Nikula static const struct ddi_buf_trans bdw_ddi_translations_hdmi[] = {
151379bc100SJani Nikula 					/* Idx	NT mV d	T mV df	db	*/
152379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0007000E, 0x0 },/* 0:	400	400	0	*/
153379bc100SJani Nikula 	{ 0x00D75FFF, 0x000E000A, 0x0 },/* 1:	400	600	3.5	*/
154379bc100SJani Nikula 	{ 0x00BEFFFF, 0x00140006, 0x0 },/* 2:	400	800	6	*/
155379bc100SJani Nikula 	{ 0x00FFFFFF, 0x0009000D, 0x0 },/* 3:	450	450	0	*/
156379bc100SJani Nikula 	{ 0x00FFFFFF, 0x000E000A, 0x0 },/* 4:	600	600	0	*/
157379bc100SJani Nikula 	{ 0x00D7FFFF, 0x00140006, 0x0 },/* 5:	600	800	2.5	*/
158379bc100SJani Nikula 	{ 0x80CB2FFF, 0x001B0002, 0x0 },/* 6:	600	1000	4.5	*/
159379bc100SJani Nikula 	{ 0x00FFFFFF, 0x00140006, 0x0 },/* 7:	800	800	0	*/
160379bc100SJani Nikula 	{ 0x80E79FFF, 0x001B0002, 0x0 },/* 8:	800	1000	2	*/
161379bc100SJani Nikula 	{ 0x80FFFFFF, 0x001B0002, 0x0 },/* 9:	1000	1000	0	*/
162379bc100SJani Nikula };
163379bc100SJani Nikula 
164379bc100SJani Nikula /* Skylake H and S */
165379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_dp[] = {
166379bc100SJani Nikula 	{ 0x00002016, 0x000000A0, 0x0 },
167379bc100SJani Nikula 	{ 0x00005012, 0x0000009B, 0x0 },
168379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
169379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
170379bc100SJani Nikula 	{ 0x00002016, 0x0000009B, 0x0 },
171379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
172379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
173379bc100SJani Nikula 	{ 0x00002016, 0x000000DF, 0x0 },
174379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
175379bc100SJani Nikula };
176379bc100SJani Nikula 
177379bc100SJani Nikula /* Skylake U */
178379bc100SJani Nikula static const struct ddi_buf_trans skl_u_ddi_translations_dp[] = {
179379bc100SJani Nikula 	{ 0x0000201B, 0x000000A2, 0x0 },
180379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
181379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x1 },
182379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
183379bc100SJani Nikula 	{ 0x0000201B, 0x0000009D, 0x0 },
184379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
185379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
186379bc100SJani Nikula 	{ 0x00002016, 0x00000088, 0x0 },
187379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
188379bc100SJani Nikula };
189379bc100SJani Nikula 
190379bc100SJani Nikula /* Skylake Y */
191379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_dp[] = {
192379bc100SJani Nikula 	{ 0x00000018, 0x000000A2, 0x0 },
193379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
194379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
195379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x3 },
196379bc100SJani Nikula 	{ 0x00000018, 0x0000009D, 0x0 },
197379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
198379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
199379bc100SJani Nikula 	{ 0x00000018, 0x00000088, 0x0 },
200379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
201379bc100SJani Nikula };
202379bc100SJani Nikula 
203379bc100SJani Nikula /* Kabylake H and S */
204379bc100SJani Nikula static const struct ddi_buf_trans kbl_ddi_translations_dp[] = {
205379bc100SJani Nikula 	{ 0x00002016, 0x000000A0, 0x0 },
206379bc100SJani Nikula 	{ 0x00005012, 0x0000009B, 0x0 },
207379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
208379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x1 },
209379bc100SJani Nikula 	{ 0x00002016, 0x0000009B, 0x0 },
210379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
211379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x1 },
212379bc100SJani Nikula 	{ 0x00002016, 0x00000097, 0x0 },
213379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x1 },
214379bc100SJani Nikula };
215379bc100SJani Nikula 
216379bc100SJani Nikula /* Kabylake U */
217379bc100SJani Nikula static const struct ddi_buf_trans kbl_u_ddi_translations_dp[] = {
218379bc100SJani Nikula 	{ 0x0000201B, 0x000000A1, 0x0 },
219379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
220379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
221379bc100SJani Nikula 	{ 0x80009010, 0x000000C0, 0x3 },
222379bc100SJani Nikula 	{ 0x0000201B, 0x0000009D, 0x0 },
223379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
224379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
225379bc100SJani Nikula 	{ 0x00002016, 0x0000004F, 0x0 },
226379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
227379bc100SJani Nikula };
228379bc100SJani Nikula 
229379bc100SJani Nikula /* Kabylake Y */
230379bc100SJani Nikula static const struct ddi_buf_trans kbl_y_ddi_translations_dp[] = {
231379bc100SJani Nikula 	{ 0x00001017, 0x000000A1, 0x0 },
232379bc100SJani Nikula 	{ 0x00005012, 0x00000088, 0x0 },
233379bc100SJani Nikula 	{ 0x80007011, 0x000000CD, 0x3 },
234379bc100SJani Nikula 	{ 0x8000800F, 0x000000C0, 0x3 },
235379bc100SJani Nikula 	{ 0x00001017, 0x0000009D, 0x0 },
236379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
237379bc100SJani Nikula 	{ 0x80007011, 0x000000C0, 0x3 },
238379bc100SJani Nikula 	{ 0x00001017, 0x0000004C, 0x0 },
239379bc100SJani Nikula 	{ 0x80005012, 0x000000C0, 0x3 },
240379bc100SJani Nikula };
241379bc100SJani Nikula 
242379bc100SJani Nikula /*
243379bc100SJani Nikula  * Skylake/Kabylake H and S
244379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
245379bc100SJani Nikula  */
246379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_edp[] = {
247379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
248379bc100SJani Nikula 	{ 0x00004013, 0x000000A9, 0x0 },
249379bc100SJani Nikula 	{ 0x00007011, 0x000000A2, 0x0 },
250379bc100SJani Nikula 	{ 0x00009010, 0x0000009C, 0x0 },
251379bc100SJani Nikula 	{ 0x00000018, 0x000000A9, 0x0 },
252379bc100SJani Nikula 	{ 0x00006013, 0x000000A2, 0x0 },
253379bc100SJani Nikula 	{ 0x00007011, 0x000000A6, 0x0 },
254379bc100SJani Nikula 	{ 0x00000018, 0x000000AB, 0x0 },
255379bc100SJani Nikula 	{ 0x00007013, 0x0000009F, 0x0 },
256379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
257379bc100SJani Nikula };
258379bc100SJani Nikula 
259379bc100SJani Nikula /*
260379bc100SJani Nikula  * Skylake/Kabylake U
261379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
262379bc100SJani Nikula  */
263379bc100SJani Nikula static const struct ddi_buf_trans skl_u_ddi_translations_edp[] = {
264379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
265379bc100SJani Nikula 	{ 0x00004013, 0x000000A9, 0x0 },
266379bc100SJani Nikula 	{ 0x00007011, 0x000000A2, 0x0 },
267379bc100SJani Nikula 	{ 0x00009010, 0x0000009C, 0x0 },
268379bc100SJani Nikula 	{ 0x00000018, 0x000000A9, 0x0 },
269379bc100SJani Nikula 	{ 0x00006013, 0x000000A2, 0x0 },
270379bc100SJani Nikula 	{ 0x00007011, 0x000000A6, 0x0 },
271379bc100SJani Nikula 	{ 0x00002016, 0x000000AB, 0x0 },
272379bc100SJani Nikula 	{ 0x00005013, 0x0000009F, 0x0 },
273379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
274379bc100SJani Nikula };
275379bc100SJani Nikula 
276379bc100SJani Nikula /*
277379bc100SJani Nikula  * Skylake/Kabylake Y
278379bc100SJani Nikula  * eDP 1.4 low vswing translation parameters
279379bc100SJani Nikula  */
280379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_edp[] = {
281379bc100SJani Nikula 	{ 0x00000018, 0x000000A8, 0x0 },
282379bc100SJani Nikula 	{ 0x00004013, 0x000000AB, 0x0 },
283379bc100SJani Nikula 	{ 0x00007011, 0x000000A4, 0x0 },
284379bc100SJani Nikula 	{ 0x00009010, 0x000000DF, 0x0 },
285379bc100SJani Nikula 	{ 0x00000018, 0x000000AA, 0x0 },
286379bc100SJani Nikula 	{ 0x00006013, 0x000000A4, 0x0 },
287379bc100SJani Nikula 	{ 0x00007011, 0x0000009D, 0x0 },
288379bc100SJani Nikula 	{ 0x00000018, 0x000000A0, 0x0 },
289379bc100SJani Nikula 	{ 0x00006012, 0x000000DF, 0x0 },
290379bc100SJani Nikula 	{ 0x00000018, 0x0000008A, 0x0 },
291379bc100SJani Nikula };
292379bc100SJani Nikula 
293379bc100SJani Nikula /* Skylake/Kabylake U, H and S */
294379bc100SJani Nikula static const struct ddi_buf_trans skl_ddi_translations_hdmi[] = {
295379bc100SJani Nikula 	{ 0x00000018, 0x000000AC, 0x0 },
296379bc100SJani Nikula 	{ 0x00005012, 0x0000009D, 0x0 },
297379bc100SJani Nikula 	{ 0x00007011, 0x00000088, 0x0 },
298379bc100SJani Nikula 	{ 0x00000018, 0x000000A1, 0x0 },
299379bc100SJani Nikula 	{ 0x00000018, 0x00000098, 0x0 },
300379bc100SJani Nikula 	{ 0x00004013, 0x00000088, 0x0 },
301379bc100SJani Nikula 	{ 0x80006012, 0x000000CD, 0x1 },
302379bc100SJani Nikula 	{ 0x00000018, 0x000000DF, 0x0 },
303379bc100SJani Nikula 	{ 0x80003015, 0x000000CD, 0x1 },	/* Default */
304379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x1 },
305379bc100SJani Nikula 	{ 0x80000018, 0x000000C0, 0x1 },
306379bc100SJani Nikula };
307379bc100SJani Nikula 
308379bc100SJani Nikula /* Skylake/Kabylake Y */
309379bc100SJani Nikula static const struct ddi_buf_trans skl_y_ddi_translations_hdmi[] = {
310379bc100SJani Nikula 	{ 0x00000018, 0x000000A1, 0x0 },
311379bc100SJani Nikula 	{ 0x00005012, 0x000000DF, 0x0 },
312379bc100SJani Nikula 	{ 0x80007011, 0x000000CB, 0x3 },
313379bc100SJani Nikula 	{ 0x00000018, 0x000000A4, 0x0 },
314379bc100SJani Nikula 	{ 0x00000018, 0x0000009D, 0x0 },
315379bc100SJani Nikula 	{ 0x00004013, 0x00000080, 0x0 },
316379bc100SJani Nikula 	{ 0x80006013, 0x000000C0, 0x3 },
317379bc100SJani Nikula 	{ 0x00000018, 0x0000008A, 0x0 },
318379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x3 },	/* Default */
319379bc100SJani Nikula 	{ 0x80003015, 0x000000C0, 0x3 },
320379bc100SJani Nikula 	{ 0x80000018, 0x000000C0, 0x3 },
321379bc100SJani Nikula };
322379bc100SJani Nikula 
323379bc100SJani Nikula struct bxt_ddi_buf_trans {
324379bc100SJani Nikula 	u8 margin;	/* swing value */
325379bc100SJani Nikula 	u8 scale;	/* scale value */
326379bc100SJani Nikula 	u8 enable;	/* scale enable */
327379bc100SJani Nikula 	u8 deemphasis;
328379bc100SJani Nikula };
329379bc100SJani Nikula 
330379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_dp[] = {
331379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
332379bc100SJani Nikula 	{ 52,  0x9A, 0, 128, },	/* 0:	400		0   */
333379bc100SJani Nikula 	{ 78,  0x9A, 0, 85,  },	/* 1:	400		3.5 */
334379bc100SJani Nikula 	{ 104, 0x9A, 0, 64,  },	/* 2:	400		6   */
335379bc100SJani Nikula 	{ 154, 0x9A, 0, 43,  },	/* 3:	400		9.5 */
336379bc100SJani Nikula 	{ 77,  0x9A, 0, 128, },	/* 4:	600		0   */
337379bc100SJani Nikula 	{ 116, 0x9A, 0, 85,  },	/* 5:	600		3.5 */
338379bc100SJani Nikula 	{ 154, 0x9A, 0, 64,  },	/* 6:	600		6   */
339379bc100SJani Nikula 	{ 102, 0x9A, 0, 128, },	/* 7:	800		0   */
340379bc100SJani Nikula 	{ 154, 0x9A, 0, 85,  },	/* 8:	800		3.5 */
341379bc100SJani Nikula 	{ 154, 0x9A, 1, 128, },	/* 9:	1200		0   */
342379bc100SJani Nikula };
343379bc100SJani Nikula 
344379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_edp[] = {
345379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
346379bc100SJani Nikula 	{ 26, 0, 0, 128, },	/* 0:	200		0   */
347379bc100SJani Nikula 	{ 38, 0, 0, 112, },	/* 1:	200		1.5 */
348379bc100SJani Nikula 	{ 48, 0, 0, 96,  },	/* 2:	200		4   */
349379bc100SJani Nikula 	{ 54, 0, 0, 69,  },	/* 3:	200		6   */
350379bc100SJani Nikula 	{ 32, 0, 0, 128, },	/* 4:	250		0   */
351379bc100SJani Nikula 	{ 48, 0, 0, 104, },	/* 5:	250		1.5 */
352379bc100SJani Nikula 	{ 54, 0, 0, 85,  },	/* 6:	250		4   */
353379bc100SJani Nikula 	{ 43, 0, 0, 128, },	/* 7:	300		0   */
354379bc100SJani Nikula 	{ 54, 0, 0, 101, },	/* 8:	300		1.5 */
355379bc100SJani Nikula 	{ 48, 0, 0, 128, },	/* 9:	300		0   */
356379bc100SJani Nikula };
357379bc100SJani Nikula 
358379bc100SJani Nikula /* BSpec has 2 recommended values - entries 0 and 8.
359379bc100SJani Nikula  * Using the entry with higher vswing.
360379bc100SJani Nikula  */
361379bc100SJani Nikula static const struct bxt_ddi_buf_trans bxt_ddi_translations_hdmi[] = {
362379bc100SJani Nikula 					/* Idx	NT mV diff	db  */
363379bc100SJani Nikula 	{ 52,  0x9A, 0, 128, },	/* 0:	400		0   */
364379bc100SJani Nikula 	{ 52,  0x9A, 0, 85,  },	/* 1:	400		3.5 */
365379bc100SJani Nikula 	{ 52,  0x9A, 0, 64,  },	/* 2:	400		6   */
366379bc100SJani Nikula 	{ 42,  0x9A, 0, 43,  },	/* 3:	400		9.5 */
367379bc100SJani Nikula 	{ 77,  0x9A, 0, 128, },	/* 4:	600		0   */
368379bc100SJani Nikula 	{ 77,  0x9A, 0, 85,  },	/* 5:	600		3.5 */
369379bc100SJani Nikula 	{ 77,  0x9A, 0, 64,  },	/* 6:	600		6   */
370379bc100SJani Nikula 	{ 102, 0x9A, 0, 128, },	/* 7:	800		0   */
371379bc100SJani Nikula 	{ 102, 0x9A, 0, 85,  },	/* 8:	800		3.5 */
372379bc100SJani Nikula 	{ 154, 0x9A, 1, 128, },	/* 9:	1200		0   */
373379bc100SJani Nikula };
374379bc100SJani Nikula 
375379bc100SJani Nikula struct cnl_ddi_buf_trans {
376379bc100SJani Nikula 	u8 dw2_swing_sel;
377379bc100SJani Nikula 	u8 dw7_n_scalar;
378379bc100SJani Nikula 	u8 dw4_cursor_coeff;
379379bc100SJani Nikula 	u8 dw4_post_cursor_2;
380379bc100SJani Nikula 	u8 dw4_post_cursor_1;
381379bc100SJani Nikula };
382379bc100SJani Nikula 
383379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for DP */
384379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_85V[] = {
385379bc100SJani Nikula 						/* NT mV Trans mV db    */
386379bc100SJani Nikula 	{ 0xA, 0x5D, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
387379bc100SJani Nikula 	{ 0xA, 0x6A, 0x38, 0x00, 0x07 },	/* 350   500      3.1   */
388379bc100SJani Nikula 	{ 0xB, 0x7A, 0x32, 0x00, 0x0D },	/* 350   700      6.0   */
389379bc100SJani Nikula 	{ 0x6, 0x7C, 0x2D, 0x00, 0x12 },	/* 350   900      8.2   */
390379bc100SJani Nikula 	{ 0xA, 0x69, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
391379bc100SJani Nikula 	{ 0xB, 0x7A, 0x36, 0x00, 0x09 },	/* 500   700      2.9   */
392379bc100SJani Nikula 	{ 0x6, 0x7C, 0x30, 0x00, 0x0F },	/* 500   900      5.1   */
393379bc100SJani Nikula 	{ 0xB, 0x7D, 0x3C, 0x00, 0x03 },	/* 650   725      0.9   */
394379bc100SJani Nikula 	{ 0x6, 0x7C, 0x34, 0x00, 0x0B },	/* 600   900      3.5   */
395379bc100SJani Nikula 	{ 0x6, 0x7B, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
396379bc100SJani Nikula };
397379bc100SJani Nikula 
398379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for HDMI */
399379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_85V[] = {
400379bc100SJani Nikula 						/* NT mV Trans mV db    */
401379bc100SJani Nikula 	{ 0xA, 0x60, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
402379bc100SJani Nikula 	{ 0xB, 0x73, 0x36, 0x00, 0x09 },	/* 450   650      3.2   */
403379bc100SJani Nikula 	{ 0x6, 0x7F, 0x31, 0x00, 0x0E },	/* 450   850      5.5   */
404379bc100SJani Nikula 	{ 0xB, 0x73, 0x3F, 0x00, 0x00 },	/* 650   650      0.0   */
405379bc100SJani Nikula 	{ 0x6, 0x7F, 0x37, 0x00, 0x08 },	/* 650   850      2.3   */
406379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 850   850      0.0   */
407379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
408379bc100SJani Nikula };
409379bc100SJani Nikula 
410379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.85V for eDP */
411379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_85V[] = {
412379bc100SJani Nikula 						/* NT mV Trans mV db    */
413379bc100SJani Nikula 	{ 0xA, 0x66, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
414379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
415379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
416379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
417379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
418379bc100SJani Nikula 	{ 0xA, 0x66, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
419379bc100SJani Nikula 	{ 0xB, 0x70, 0x3C, 0x00, 0x03 },	/* 460   600      2.3   */
420379bc100SJani Nikula 	{ 0xC, 0x75, 0x3C, 0x00, 0x03 },	/* 537   700      2.3   */
421379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
422379bc100SJani Nikula };
423379bc100SJani Nikula 
424379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for DP */
425379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_95V[] = {
426379bc100SJani Nikula 						/* NT mV Trans mV db    */
427379bc100SJani Nikula 	{ 0xA, 0x5D, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
428379bc100SJani Nikula 	{ 0xA, 0x6A, 0x38, 0x00, 0x07 },	/* 350   500      3.1   */
429379bc100SJani Nikula 	{ 0xB, 0x7A, 0x32, 0x00, 0x0D },	/* 350   700      6.0   */
430379bc100SJani Nikula 	{ 0x6, 0x7C, 0x2D, 0x00, 0x12 },	/* 350   900      8.2   */
431379bc100SJani Nikula 	{ 0xA, 0x69, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
432379bc100SJani Nikula 	{ 0xB, 0x7A, 0x36, 0x00, 0x09 },	/* 500   700      2.9   */
433379bc100SJani Nikula 	{ 0x6, 0x7C, 0x30, 0x00, 0x0F },	/* 500   900      5.1   */
434379bc100SJani Nikula 	{ 0xB, 0x7D, 0x3C, 0x00, 0x03 },	/* 650   725      0.9   */
435379bc100SJani Nikula 	{ 0x6, 0x7C, 0x34, 0x00, 0x0B },	/* 600   900      3.5   */
436379bc100SJani Nikula 	{ 0x6, 0x7B, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
437379bc100SJani Nikula };
438379bc100SJani Nikula 
439379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for HDMI */
440379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_95V[] = {
441379bc100SJani Nikula 						/* NT mV Trans mV db    */
442379bc100SJani Nikula 	{ 0xA, 0x5C, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
443379bc100SJani Nikula 	{ 0xB, 0x69, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
444379bc100SJani Nikula 	{ 0x5, 0x76, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
445379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
446379bc100SJani Nikula 	{ 0xB, 0x69, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
447379bc100SJani Nikula 	{ 0xB, 0x79, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
448379bc100SJani Nikula 	{ 0x6, 0x7D, 0x32, 0x00, 0x0D },	/* 600   1000     4.4   */
449379bc100SJani Nikula 	{ 0x5, 0x76, 0x3F, 0x00, 0x00 },	/* 800   800      0.0   */
450379bc100SJani Nikula 	{ 0x6, 0x7D, 0x39, 0x00, 0x06 },	/* 800   1000     1.9   */
451379bc100SJani Nikula 	{ 0x6, 0x7F, 0x39, 0x00, 0x06 },	/* 850   1050     1.8   */
452379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
453379bc100SJani Nikula };
454379bc100SJani Nikula 
455379bc100SJani Nikula /* Voltage Swing Programming for VccIO 0.95V for eDP */
456379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_95V[] = {
457379bc100SJani Nikula 						/* NT mV Trans mV db    */
458379bc100SJani Nikula 	{ 0xA, 0x61, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
459379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
460379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
461379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
462379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
463379bc100SJani Nikula 	{ 0xA, 0x61, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
464379bc100SJani Nikula 	{ 0xB, 0x68, 0x39, 0x00, 0x06 },	/* 460   600      2.3   */
465379bc100SJani Nikula 	{ 0xC, 0x6E, 0x39, 0x00, 0x06 },	/* 537   700      2.3   */
466379bc100SJani Nikula 	{ 0x4, 0x7F, 0x3A, 0x00, 0x05 },	/* 460   600      2.3   */
467379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
468379bc100SJani Nikula };
469379bc100SJani Nikula 
470379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for DP */
471379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_1_05V[] = {
472379bc100SJani Nikula 						/* NT mV Trans mV db    */
473379bc100SJani Nikula 	{ 0xA, 0x58, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
474379bc100SJani Nikula 	{ 0xB, 0x64, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
475379bc100SJani Nikula 	{ 0x5, 0x70, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
476379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2C, 0x00, 0x13 },	/* 400   1050     8.4   */
477379bc100SJani Nikula 	{ 0xB, 0x64, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
478379bc100SJani Nikula 	{ 0x5, 0x73, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
479379bc100SJani Nikula 	{ 0x6, 0x7F, 0x30, 0x00, 0x0F },	/* 550   1050     5.6   */
480379bc100SJani Nikula 	{ 0x5, 0x76, 0x3E, 0x00, 0x01 },	/* 850   900      0.5   */
481379bc100SJani Nikula 	{ 0x6, 0x7F, 0x36, 0x00, 0x09 },	/* 750   1050     2.9   */
482379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
483379bc100SJani Nikula };
484379bc100SJani Nikula 
485379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for HDMI */
486379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_1_05V[] = {
487379bc100SJani Nikula 						/* NT mV Trans mV db    */
488379bc100SJani Nikula 	{ 0xA, 0x58, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
489379bc100SJani Nikula 	{ 0xB, 0x64, 0x37, 0x00, 0x08 },	/* 400   600      3.5   */
490379bc100SJani Nikula 	{ 0x5, 0x70, 0x31, 0x00, 0x0E },	/* 400   800      6.0   */
491379bc100SJani Nikula 	{ 0xA, 0x5B, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
492379bc100SJani Nikula 	{ 0xB, 0x64, 0x3F, 0x00, 0x00 },	/* 600   600      0.0   */
493379bc100SJani Nikula 	{ 0x5, 0x73, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
494379bc100SJani Nikula 	{ 0x6, 0x7C, 0x32, 0x00, 0x0D },	/* 600   1000     4.4   */
495379bc100SJani Nikula 	{ 0x5, 0x70, 0x3F, 0x00, 0x00 },	/* 800   800      0.0   */
496379bc100SJani Nikula 	{ 0x6, 0x7C, 0x39, 0x00, 0x06 },	/* 800   1000     1.9   */
497379bc100SJani Nikula 	{ 0x6, 0x7F, 0x39, 0x00, 0x06 },	/* 850   1050     1.8   */
498379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 1050  1050     0.0   */
499379bc100SJani Nikula };
500379bc100SJani Nikula 
501379bc100SJani Nikula /* Voltage Swing Programming for VccIO 1.05V for eDP */
502379bc100SJani Nikula static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_1_05V[] = {
503379bc100SJani Nikula 						/* NT mV Trans mV db    */
504379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3A, 0x00, 0x05 },	/* 384   500      2.3   */
505379bc100SJani Nikula 	{ 0x0, 0x7F, 0x38, 0x00, 0x07 },	/* 153   200      2.3   */
506379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 192   250      2.3   */
507379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 230   300      2.3   */
508379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 269   350      2.3   */
509379bc100SJani Nikula 	{ 0xA, 0x5E, 0x3C, 0x00, 0x03 },	/* 446   500      1.0   */
510379bc100SJani Nikula 	{ 0xB, 0x64, 0x39, 0x00, 0x06 },	/* 460   600      2.3   */
511379bc100SJani Nikula 	{ 0xE, 0x6A, 0x39, 0x00, 0x06 },	/* 537   700      2.3   */
512379bc100SJani Nikula 	{ 0x2, 0x7F, 0x3F, 0x00, 0x00 },	/* 400   400      0.0   */
513379bc100SJani Nikula };
514379bc100SJani Nikula 
515379bc100SJani Nikula /* icl_combo_phy_ddi_translations */
516379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_dp_hbr2[] = {
517379bc100SJani Nikula 						/* NT mV Trans mV db    */
518379bc100SJani Nikula 	{ 0xA, 0x35, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
519379bc100SJani Nikula 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
520379bc100SJani Nikula 	{ 0xC, 0x71, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
521379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
522379bc100SJani Nikula 	{ 0xA, 0x4C, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
523379bc100SJani Nikula 	{ 0xC, 0x73, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
524379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
525379bc100SJani Nikula 	{ 0xC, 0x6C, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
526379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
527379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
528379bc100SJani Nikula };
529379bc100SJani Nikula 
530379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_edp_hbr2[] = {
531379bc100SJani Nikula 						/* NT mV Trans mV db    */
532379bc100SJani Nikula 	{ 0x0, 0x7F, 0x3F, 0x00, 0x00 },	/* 200   200      0.0   */
533379bc100SJani Nikula 	{ 0x8, 0x7F, 0x38, 0x00, 0x07 },	/* 200   250      1.9   */
534379bc100SJani Nikula 	{ 0x1, 0x7F, 0x33, 0x00, 0x0C },	/* 200   300      3.5   */
535379bc100SJani Nikula 	{ 0x9, 0x7F, 0x31, 0x00, 0x0E },	/* 200   350      4.9   */
536379bc100SJani Nikula 	{ 0x8, 0x7F, 0x3F, 0x00, 0x00 },	/* 250   250      0.0   */
537379bc100SJani Nikula 	{ 0x1, 0x7F, 0x38, 0x00, 0x07 },	/* 250   300      1.6   */
538379bc100SJani Nikula 	{ 0x9, 0x7F, 0x35, 0x00, 0x0A },	/* 250   350      2.9   */
539379bc100SJani Nikula 	{ 0x1, 0x7F, 0x3F, 0x00, 0x00 },	/* 300   300      0.0   */
540379bc100SJani Nikula 	{ 0x9, 0x7F, 0x38, 0x00, 0x07 },	/* 300   350      1.3   */
541379bc100SJani Nikula 	{ 0x9, 0x7F, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
542379bc100SJani Nikula };
543379bc100SJani Nikula 
544379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_edp_hbr3[] = {
545379bc100SJani Nikula 						/* NT mV Trans mV db    */
546379bc100SJani Nikula 	{ 0xA, 0x35, 0x3F, 0x00, 0x00 },	/* 350   350      0.0   */
547379bc100SJani Nikula 	{ 0xA, 0x4F, 0x37, 0x00, 0x08 },	/* 350   500      3.1   */
548379bc100SJani Nikula 	{ 0xC, 0x71, 0x2F, 0x00, 0x10 },	/* 350   700      6.0   */
549379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2B, 0x00, 0x14 },	/* 350   900      8.2   */
550379bc100SJani Nikula 	{ 0xA, 0x4C, 0x3F, 0x00, 0x00 },	/* 500   500      0.0   */
551379bc100SJani Nikula 	{ 0xC, 0x73, 0x34, 0x00, 0x0B },	/* 500   700      2.9   */
552379bc100SJani Nikula 	{ 0x6, 0x7F, 0x2F, 0x00, 0x10 },	/* 500   900      5.1   */
553379bc100SJani Nikula 	{ 0xC, 0x6C, 0x3C, 0x00, 0x03 },	/* 650   700      0.6   */
554379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   900      3.5   */
555379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 900   900      0.0   */
556379bc100SJani Nikula };
557379bc100SJani Nikula 
558379bc100SJani Nikula static const struct cnl_ddi_buf_trans icl_combo_phy_ddi_translations_hdmi[] = {
559379bc100SJani Nikula 						/* NT mV Trans mV db    */
560379bc100SJani Nikula 	{ 0xA, 0x60, 0x3F, 0x00, 0x00 },	/* 450   450      0.0   */
561379bc100SJani Nikula 	{ 0xB, 0x73, 0x36, 0x00, 0x09 },	/* 450   650      3.2   */
562379bc100SJani Nikula 	{ 0x6, 0x7F, 0x31, 0x00, 0x0E },	/* 450   850      5.5   */
563379bc100SJani Nikula 	{ 0xB, 0x73, 0x3F, 0x00, 0x00 },	/* 650   650      0.0   ALS */
564379bc100SJani Nikula 	{ 0x6, 0x7F, 0x37, 0x00, 0x08 },	/* 650   850      2.3   */
565379bc100SJani Nikula 	{ 0x6, 0x7F, 0x3F, 0x00, 0x00 },	/* 850   850      0.0   */
566379bc100SJani Nikula 	{ 0x6, 0x7F, 0x35, 0x00, 0x0A },	/* 600   850      3.0   */
567379bc100SJani Nikula };
568379bc100SJani Nikula 
569379bc100SJani Nikula struct icl_mg_phy_ddi_buf_trans {
570379bc100SJani Nikula 	u32 cri_txdeemph_override_5_0;
571379bc100SJani Nikula 	u32 cri_txdeemph_override_11_6;
572379bc100SJani Nikula 	u32 cri_txdeemph_override_17_12;
573379bc100SJani Nikula };
574379bc100SJani Nikula 
575379bc100SJani Nikula static const struct icl_mg_phy_ddi_buf_trans icl_mg_phy_ddi_translations[] = {
576379bc100SJani Nikula 				/* Voltage swing  pre-emphasis */
577379bc100SJani Nikula 	{ 0x0, 0x1B, 0x00 },	/* 0              0   */
578379bc100SJani Nikula 	{ 0x0, 0x23, 0x08 },	/* 0              1   */
579379bc100SJani Nikula 	{ 0x0, 0x2D, 0x12 },	/* 0              2   */
580379bc100SJani Nikula 	{ 0x0, 0x00, 0x00 },	/* 0              3   */
581379bc100SJani Nikula 	{ 0x0, 0x23, 0x00 },	/* 1              0   */
582379bc100SJani Nikula 	{ 0x0, 0x2B, 0x09 },	/* 1              1   */
583379bc100SJani Nikula 	{ 0x0, 0x2E, 0x11 },	/* 1              2   */
584379bc100SJani Nikula 	{ 0x0, 0x2F, 0x00 },	/* 2              0   */
585379bc100SJani Nikula 	{ 0x0, 0x33, 0x0C },	/* 2              1   */
586379bc100SJani Nikula 	{ 0x0, 0x00, 0x00 },	/* 3              0   */
587379bc100SJani Nikula };
588379bc100SJani Nikula 
589379bc100SJani Nikula static const struct ddi_buf_trans *
590379bc100SJani Nikula bdw_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
591379bc100SJani Nikula {
592379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
593379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
594379bc100SJani Nikula 		return bdw_ddi_translations_edp;
595379bc100SJani Nikula 	} else {
596379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
597379bc100SJani Nikula 		return bdw_ddi_translations_dp;
598379bc100SJani Nikula 	}
599379bc100SJani Nikula }
600379bc100SJani Nikula 
601379bc100SJani Nikula static const struct ddi_buf_trans *
602379bc100SJani Nikula skl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
603379bc100SJani Nikula {
604379bc100SJani Nikula 	if (IS_SKL_ULX(dev_priv)) {
605379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_dp);
606379bc100SJani Nikula 		return skl_y_ddi_translations_dp;
607379bc100SJani Nikula 	} else if (IS_SKL_ULT(dev_priv)) {
608379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_u_ddi_translations_dp);
609379bc100SJani Nikula 		return skl_u_ddi_translations_dp;
610379bc100SJani Nikula 	} else {
611379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_dp);
612379bc100SJani Nikula 		return skl_ddi_translations_dp;
613379bc100SJani Nikula 	}
614379bc100SJani Nikula }
615379bc100SJani Nikula 
616379bc100SJani Nikula static const struct ddi_buf_trans *
617379bc100SJani Nikula kbl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
618379bc100SJani Nikula {
619379bc100SJani Nikula 	if (IS_KBL_ULX(dev_priv) || IS_CFL_ULX(dev_priv)) {
620379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_y_ddi_translations_dp);
621379bc100SJani Nikula 		return kbl_y_ddi_translations_dp;
622379bc100SJani Nikula 	} else if (IS_KBL_ULT(dev_priv) || IS_CFL_ULT(dev_priv)) {
623379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_u_ddi_translations_dp);
624379bc100SJani Nikula 		return kbl_u_ddi_translations_dp;
625379bc100SJani Nikula 	} else {
626379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(kbl_ddi_translations_dp);
627379bc100SJani Nikula 		return kbl_ddi_translations_dp;
628379bc100SJani Nikula 	}
629379bc100SJani Nikula }
630379bc100SJani Nikula 
631379bc100SJani Nikula static const struct ddi_buf_trans *
632379bc100SJani Nikula skl_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
633379bc100SJani Nikula {
634379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
635379bc100SJani Nikula 		if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv) ||
636379bc100SJani Nikula 		    IS_CFL_ULX(dev_priv)) {
637379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_y_ddi_translations_edp);
638379bc100SJani Nikula 			return skl_y_ddi_translations_edp;
639379bc100SJani Nikula 		} else if (IS_SKL_ULT(dev_priv) || IS_KBL_ULT(dev_priv) ||
640379bc100SJani Nikula 			   IS_CFL_ULT(dev_priv)) {
641379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_u_ddi_translations_edp);
642379bc100SJani Nikula 			return skl_u_ddi_translations_edp;
643379bc100SJani Nikula 		} else {
644379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(skl_ddi_translations_edp);
645379bc100SJani Nikula 			return skl_ddi_translations_edp;
646379bc100SJani Nikula 		}
647379bc100SJani Nikula 	}
648379bc100SJani Nikula 
649379bc100SJani Nikula 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
650379bc100SJani Nikula 		return kbl_get_buf_trans_dp(dev_priv, n_entries);
651379bc100SJani Nikula 	else
652379bc100SJani Nikula 		return skl_get_buf_trans_dp(dev_priv, n_entries);
653379bc100SJani Nikula }
654379bc100SJani Nikula 
655379bc100SJani Nikula static const struct ddi_buf_trans *
656379bc100SJani Nikula skl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
657379bc100SJani Nikula {
658379bc100SJani Nikula 	if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv) ||
659379bc100SJani Nikula 	    IS_CFL_ULX(dev_priv)) {
660379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_y_ddi_translations_hdmi);
661379bc100SJani Nikula 		return skl_y_ddi_translations_hdmi;
662379bc100SJani Nikula 	} else {
663379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
664379bc100SJani Nikula 		return skl_ddi_translations_hdmi;
665379bc100SJani Nikula 	}
666379bc100SJani Nikula }
667379bc100SJani Nikula 
668379bc100SJani Nikula static int skl_buf_trans_num_entries(enum port port, int n_entries)
669379bc100SJani Nikula {
670379bc100SJani Nikula 	/* Only DDIA and DDIE can select the 10th register with DP */
671379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E)
672379bc100SJani Nikula 		return min(n_entries, 10);
673379bc100SJani Nikula 	else
674379bc100SJani Nikula 		return min(n_entries, 9);
675379bc100SJani Nikula }
676379bc100SJani Nikula 
677379bc100SJani Nikula static const struct ddi_buf_trans *
678379bc100SJani Nikula intel_ddi_get_buf_trans_dp(struct drm_i915_private *dev_priv,
679379bc100SJani Nikula 			   enum port port, int *n_entries)
680379bc100SJani Nikula {
681379bc100SJani Nikula 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
682379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
683379bc100SJani Nikula 			kbl_get_buf_trans_dp(dev_priv, n_entries);
684379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
685379bc100SJani Nikula 		return ddi_translations;
686379bc100SJani Nikula 	} else if (IS_SKYLAKE(dev_priv)) {
687379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
688379bc100SJani Nikula 			skl_get_buf_trans_dp(dev_priv, n_entries);
689379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
690379bc100SJani Nikula 		return ddi_translations;
691379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
692379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
693379bc100SJani Nikula 		return  bdw_ddi_translations_dp;
694379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
695379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
696379bc100SJani Nikula 		return hsw_ddi_translations_dp;
697379bc100SJani Nikula 	}
698379bc100SJani Nikula 
699379bc100SJani Nikula 	*n_entries = 0;
700379bc100SJani Nikula 	return NULL;
701379bc100SJani Nikula }
702379bc100SJani Nikula 
703379bc100SJani Nikula static const struct ddi_buf_trans *
704379bc100SJani Nikula intel_ddi_get_buf_trans_edp(struct drm_i915_private *dev_priv,
705379bc100SJani Nikula 			    enum port port, int *n_entries)
706379bc100SJani Nikula {
707379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
708379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations =
709379bc100SJani Nikula 			skl_get_buf_trans_edp(dev_priv, n_entries);
710379bc100SJani Nikula 		*n_entries = skl_buf_trans_num_entries(port, *n_entries);
711379bc100SJani Nikula 		return ddi_translations;
712379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
713379bc100SJani Nikula 		return bdw_get_buf_trans_edp(dev_priv, n_entries);
714379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
715379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
716379bc100SJani Nikula 		return hsw_ddi_translations_dp;
717379bc100SJani Nikula 	}
718379bc100SJani Nikula 
719379bc100SJani Nikula 	*n_entries = 0;
720379bc100SJani Nikula 	return NULL;
721379bc100SJani Nikula }
722379bc100SJani Nikula 
723379bc100SJani Nikula static const struct ddi_buf_trans *
724379bc100SJani Nikula intel_ddi_get_buf_trans_fdi(struct drm_i915_private *dev_priv,
725379bc100SJani Nikula 			    int *n_entries)
726379bc100SJani Nikula {
727379bc100SJani Nikula 	if (IS_BROADWELL(dev_priv)) {
728379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_fdi);
729379bc100SJani Nikula 		return bdw_ddi_translations_fdi;
730379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
731379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
732379bc100SJani Nikula 		return hsw_ddi_translations_fdi;
733379bc100SJani Nikula 	}
734379bc100SJani Nikula 
735379bc100SJani Nikula 	*n_entries = 0;
736379bc100SJani Nikula 	return NULL;
737379bc100SJani Nikula }
738379bc100SJani Nikula 
739379bc100SJani Nikula static const struct ddi_buf_trans *
740379bc100SJani Nikula intel_ddi_get_buf_trans_hdmi(struct drm_i915_private *dev_priv,
741379bc100SJani Nikula 			     int *n_entries)
742379bc100SJani Nikula {
743379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
744379bc100SJani Nikula 		return skl_get_buf_trans_hdmi(dev_priv, n_entries);
745379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
746379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
747379bc100SJani Nikula 		return bdw_ddi_translations_hdmi;
748379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
749379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
750379bc100SJani Nikula 		return hsw_ddi_translations_hdmi;
751379bc100SJani Nikula 	}
752379bc100SJani Nikula 
753379bc100SJani Nikula 	*n_entries = 0;
754379bc100SJani Nikula 	return NULL;
755379bc100SJani Nikula }
756379bc100SJani Nikula 
757379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
758379bc100SJani Nikula bxt_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
759379bc100SJani Nikula {
760379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_dp);
761379bc100SJani Nikula 	return bxt_ddi_translations_dp;
762379bc100SJani Nikula }
763379bc100SJani Nikula 
764379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
765379bc100SJani Nikula bxt_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
766379bc100SJani Nikula {
767379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
768379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(bxt_ddi_translations_edp);
769379bc100SJani Nikula 		return bxt_ddi_translations_edp;
770379bc100SJani Nikula 	}
771379bc100SJani Nikula 
772379bc100SJani Nikula 	return bxt_get_buf_trans_dp(dev_priv, n_entries);
773379bc100SJani Nikula }
774379bc100SJani Nikula 
775379bc100SJani Nikula static const struct bxt_ddi_buf_trans *
776379bc100SJani Nikula bxt_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
777379bc100SJani Nikula {
778379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(bxt_ddi_translations_hdmi);
779379bc100SJani Nikula 	return bxt_ddi_translations_hdmi;
780379bc100SJani Nikula }
781379bc100SJani Nikula 
782379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
783379bc100SJani Nikula cnl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
784379bc100SJani Nikula {
785379bc100SJani Nikula 	u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
786379bc100SJani Nikula 
787379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
788379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_85V);
789379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_85V;
790379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
791379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_95V);
792379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_0_95V;
793379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
794379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_1_05V);
795379bc100SJani Nikula 		return cnl_ddi_translations_hdmi_1_05V;
796379bc100SJani Nikula 	} else {
797379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
798379bc100SJani Nikula 		MISSING_CASE(voltage);
799379bc100SJani Nikula 	}
800379bc100SJani Nikula 	return NULL;
801379bc100SJani Nikula }
802379bc100SJani Nikula 
803379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
804379bc100SJani Nikula cnl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
805379bc100SJani Nikula {
806379bc100SJani Nikula 	u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
807379bc100SJani Nikula 
808379bc100SJani Nikula 	if (voltage == VOLTAGE_INFO_0_85V) {
809379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_85V);
810379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_85V;
811379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_0_95V) {
812379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_95V);
813379bc100SJani Nikula 		return cnl_ddi_translations_dp_0_95V;
814379bc100SJani Nikula 	} else if (voltage == VOLTAGE_INFO_1_05V) {
815379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_1_05V);
816379bc100SJani Nikula 		return cnl_ddi_translations_dp_1_05V;
817379bc100SJani Nikula 	} else {
818379bc100SJani Nikula 		*n_entries = 1; /* shut up gcc */
819379bc100SJani Nikula 		MISSING_CASE(voltage);
820379bc100SJani Nikula 	}
821379bc100SJani Nikula 	return NULL;
822379bc100SJani Nikula }
823379bc100SJani Nikula 
824379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
825379bc100SJani Nikula cnl_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
826379bc100SJani Nikula {
827379bc100SJani Nikula 	u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
828379bc100SJani Nikula 
829379bc100SJani Nikula 	if (dev_priv->vbt.edp.low_vswing) {
830379bc100SJani Nikula 		if (voltage == VOLTAGE_INFO_0_85V) {
831379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_85V);
832379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_85V;
833379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_0_95V) {
834379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_95V);
835379bc100SJani Nikula 			return cnl_ddi_translations_edp_0_95V;
836379bc100SJani Nikula 		} else if (voltage == VOLTAGE_INFO_1_05V) {
837379bc100SJani Nikula 			*n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_1_05V);
838379bc100SJani Nikula 			return cnl_ddi_translations_edp_1_05V;
839379bc100SJani Nikula 		} else {
840379bc100SJani Nikula 			*n_entries = 1; /* shut up gcc */
841379bc100SJani Nikula 			MISSING_CASE(voltage);
842379bc100SJani Nikula 		}
843379bc100SJani Nikula 		return NULL;
844379bc100SJani Nikula 	} else {
845379bc100SJani Nikula 		return cnl_get_buf_trans_dp(dev_priv, n_entries);
846379bc100SJani Nikula 	}
847379bc100SJani Nikula }
848379bc100SJani Nikula 
849379bc100SJani Nikula static const struct cnl_ddi_buf_trans *
850379bc100SJani Nikula icl_get_combo_buf_trans(struct drm_i915_private *dev_priv, enum port port,
851379bc100SJani Nikula 			int type, int rate, int *n_entries)
852379bc100SJani Nikula {
853379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
854379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_hdmi);
855379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_hdmi;
856379bc100SJani Nikula 	} else if (rate > 540000 && type == INTEL_OUTPUT_EDP) {
857379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr3);
858379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr3;
859379bc100SJani Nikula 	} else if (type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.low_vswing) {
860379bc100SJani Nikula 		*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_edp_hbr2);
861379bc100SJani Nikula 		return icl_combo_phy_ddi_translations_edp_hbr2;
862379bc100SJani Nikula 	}
863379bc100SJani Nikula 
864379bc100SJani Nikula 	*n_entries = ARRAY_SIZE(icl_combo_phy_ddi_translations_dp_hbr2);
865379bc100SJani Nikula 	return icl_combo_phy_ddi_translations_dp_hbr2;
866379bc100SJani Nikula }
867379bc100SJani Nikula 
868379bc100SJani Nikula static int intel_ddi_hdmi_level(struct drm_i915_private *dev_priv, enum port port)
869379bc100SJani Nikula {
870379bc100SJani Nikula 	int n_entries, level, default_entry;
871379bc100SJani Nikula 
872379bc100SJani Nikula 	level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
873379bc100SJani Nikula 
874379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
875379bc100SJani Nikula 		if (intel_port_is_combophy(dev_priv, port))
876379bc100SJani Nikula 			icl_get_combo_buf_trans(dev_priv, port, INTEL_OUTPUT_HDMI,
877379bc100SJani Nikula 						0, &n_entries);
878379bc100SJani Nikula 		else
879379bc100SJani Nikula 			n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
880379bc100SJani Nikula 		default_entry = n_entries - 1;
881379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
882379bc100SJani Nikula 		cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
883379bc100SJani Nikula 		default_entry = n_entries - 1;
884379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
885379bc100SJani Nikula 		bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
886379bc100SJani Nikula 		default_entry = n_entries - 1;
887379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
888379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
889379bc100SJani Nikula 		default_entry = 8;
890379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
891379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
892379bc100SJani Nikula 		default_entry = 7;
893379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
894379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
895379bc100SJani Nikula 		default_entry = 6;
896379bc100SJani Nikula 	} else {
897379bc100SJani Nikula 		WARN(1, "ddi translation table missing\n");
898379bc100SJani Nikula 		return 0;
899379bc100SJani Nikula 	}
900379bc100SJani Nikula 
901379bc100SJani Nikula 	/* Choose a good default if VBT is badly populated */
902379bc100SJani Nikula 	if (level == HDMI_LEVEL_SHIFT_UNKNOWN || level >= n_entries)
903379bc100SJani Nikula 		level = default_entry;
904379bc100SJani Nikula 
905379bc100SJani Nikula 	if (WARN_ON_ONCE(n_entries == 0))
906379bc100SJani Nikula 		return 0;
907379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
908379bc100SJani Nikula 		level = n_entries - 1;
909379bc100SJani Nikula 
910379bc100SJani Nikula 	return level;
911379bc100SJani Nikula }
912379bc100SJani Nikula 
913379bc100SJani Nikula /*
914379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
915379bc100SJani Nikula  * values in advance. This function programs the correct values for
916379bc100SJani Nikula  * DP/eDP/FDI use cases.
917379bc100SJani Nikula  */
918379bc100SJani Nikula static void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
919379bc100SJani Nikula 					 const struct intel_crtc_state *crtc_state)
920379bc100SJani Nikula {
921379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
922379bc100SJani Nikula 	u32 iboost_bit = 0;
923379bc100SJani Nikula 	int i, n_entries;
924379bc100SJani Nikula 	enum port port = encoder->port;
925379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
926379bc100SJani Nikula 
927379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
928379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
929379bc100SJani Nikula 							       &n_entries);
930379bc100SJani Nikula 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
931379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port,
932379bc100SJani Nikula 							       &n_entries);
933379bc100SJani Nikula 	else
934379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port,
935379bc100SJani Nikula 							      &n_entries);
936379bc100SJani Nikula 
937379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
938379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv) &&
939379bc100SJani Nikula 	    dev_priv->vbt.ddi_port_info[port].dp_boost_level)
940379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
941379bc100SJani Nikula 
942379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
943379bc100SJani Nikula 		I915_WRITE(DDI_BUF_TRANS_LO(port, i),
944379bc100SJani Nikula 			   ddi_translations[i].trans1 | iboost_bit);
945379bc100SJani Nikula 		I915_WRITE(DDI_BUF_TRANS_HI(port, i),
946379bc100SJani Nikula 			   ddi_translations[i].trans2);
947379bc100SJani Nikula 	}
948379bc100SJani Nikula }
949379bc100SJani Nikula 
950379bc100SJani Nikula /*
951379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
952379bc100SJani Nikula  * values in advance. This function programs the correct values for
953379bc100SJani Nikula  * HDMI/DVI use cases.
954379bc100SJani Nikula  */
955379bc100SJani Nikula static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
956379bc100SJani Nikula 					   int level)
957379bc100SJani Nikula {
958379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
959379bc100SJani Nikula 	u32 iboost_bit = 0;
960379bc100SJani Nikula 	int n_entries;
961379bc100SJani Nikula 	enum port port = encoder->port;
962379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
963379bc100SJani Nikula 
964379bc100SJani Nikula 	ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
965379bc100SJani Nikula 
966379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
967379bc100SJani Nikula 		return;
968379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
969379bc100SJani Nikula 		level = n_entries - 1;
970379bc100SJani Nikula 
971379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
972379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv) &&
973379bc100SJani Nikula 	    dev_priv->vbt.ddi_port_info[port].hdmi_boost_level)
974379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
975379bc100SJani Nikula 
976379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
977379bc100SJani Nikula 	I915_WRITE(DDI_BUF_TRANS_LO(port, 9),
978379bc100SJani Nikula 		   ddi_translations[level].trans1 | iboost_bit);
979379bc100SJani Nikula 	I915_WRITE(DDI_BUF_TRANS_HI(port, 9),
980379bc100SJani Nikula 		   ddi_translations[level].trans2);
981379bc100SJani Nikula }
982379bc100SJani Nikula 
983379bc100SJani Nikula static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
984379bc100SJani Nikula 				    enum port port)
985379bc100SJani Nikula {
986379bc100SJani Nikula 	i915_reg_t reg = DDI_BUF_CTL(port);
987379bc100SJani Nikula 	int i;
988379bc100SJani Nikula 
989379bc100SJani Nikula 	for (i = 0; i < 16; i++) {
990379bc100SJani Nikula 		udelay(1);
991379bc100SJani Nikula 		if (I915_READ(reg) & DDI_BUF_IS_IDLE)
992379bc100SJani Nikula 			return;
993379bc100SJani Nikula 	}
994379bc100SJani Nikula 	DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
995379bc100SJani Nikula }
996379bc100SJani Nikula 
997379bc100SJani Nikula static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
998379bc100SJani Nikula {
999379bc100SJani Nikula 	switch (pll->info->id) {
1000379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
1001379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
1002379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
1003379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
1004379bc100SJani Nikula 	case DPLL_ID_SPLL:
1005379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
1006379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
1007379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
1008379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
1009379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
1010379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
1011379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
1012379bc100SJani Nikula 	default:
1013379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
1014379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
1015379bc100SJani Nikula 	}
1016379bc100SJani Nikula }
1017379bc100SJani Nikula 
1018379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
1019379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
1020379bc100SJani Nikula {
1021379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1022379bc100SJani Nikula 	int clock = crtc_state->port_clock;
1023379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
1024379bc100SJani Nikula 
1025379bc100SJani Nikula 	switch (id) {
1026379bc100SJani Nikula 	default:
1027379bc100SJani Nikula 		/*
1028379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
1029379bc100SJani Nikula 		 * here, so do warn if this get passed in
1030379bc100SJani Nikula 		 */
1031379bc100SJani Nikula 		MISSING_CASE(id);
1032379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
1033379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
1034379bc100SJani Nikula 		switch (clock) {
1035379bc100SJani Nikula 		case 162000:
1036379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
1037379bc100SJani Nikula 		case 270000:
1038379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
1039379bc100SJani Nikula 		case 540000:
1040379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
1041379bc100SJani Nikula 		case 810000:
1042379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
1043379bc100SJani Nikula 		default:
1044379bc100SJani Nikula 			MISSING_CASE(clock);
1045379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
1046379bc100SJani Nikula 		}
1047379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
1048379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
1049379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
1050379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
1051379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
1052379bc100SJani Nikula 	}
1053379bc100SJani Nikula }
1054379bc100SJani Nikula 
1055379bc100SJani Nikula /* Starting with Haswell, different DDI ports can work in FDI mode for
1056379bc100SJani Nikula  * connection to the PCH-located connectors. For this, it is necessary to train
1057379bc100SJani Nikula  * both the DDI port and PCH receiver for the desired DDI buffer settings.
1058379bc100SJani Nikula  *
1059379bc100SJani Nikula  * The recommended port to work in FDI mode is DDI E, which we use here. Also,
1060379bc100SJani Nikula  * please note that when FDI mode is active on DDI E, it shares 2 lines with
1061379bc100SJani Nikula  * DDI A (which is used for eDP)
1062379bc100SJani Nikula  */
1063379bc100SJani Nikula 
1064379bc100SJani Nikula void hsw_fdi_link_train(struct intel_crtc *crtc,
1065379bc100SJani Nikula 			const struct intel_crtc_state *crtc_state)
1066379bc100SJani Nikula {
1067379bc100SJani Nikula 	struct drm_device *dev = crtc->base.dev;
1068379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1069379bc100SJani Nikula 	struct intel_encoder *encoder;
1070379bc100SJani Nikula 	u32 temp, i, rx_ctl_val, ddi_pll_sel;
1071379bc100SJani Nikula 
1072379bc100SJani Nikula 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
1073379bc100SJani Nikula 		WARN_ON(encoder->type != INTEL_OUTPUT_ANALOG);
1074379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
1075379bc100SJani Nikula 	}
1076379bc100SJani Nikula 
1077379bc100SJani Nikula 	/* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
1078379bc100SJani Nikula 	 * mode set "sequence for CRT port" document:
1079379bc100SJani Nikula 	 * - TP1 to TP2 time with the default value
1080379bc100SJani Nikula 	 * - FDI delay to 90h
1081379bc100SJani Nikula 	 *
1082379bc100SJani Nikula 	 * WaFDIAutoLinkSetTimingOverrride:hsw
1083379bc100SJani Nikula 	 */
1084379bc100SJani Nikula 	I915_WRITE(FDI_RX_MISC(PIPE_A), FDI_RX_PWRDN_LANE1_VAL(2) |
1085379bc100SJani Nikula 				  FDI_RX_PWRDN_LANE0_VAL(2) |
1086379bc100SJani Nikula 				  FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
1087379bc100SJani Nikula 
1088379bc100SJani Nikula 	/* Enable the PCH Receiver FDI PLL */
1089379bc100SJani Nikula 	rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
1090379bc100SJani Nikula 		     FDI_RX_PLL_ENABLE |
1091379bc100SJani Nikula 		     FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
1092379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1093379bc100SJani Nikula 	POSTING_READ(FDI_RX_CTL(PIPE_A));
1094379bc100SJani Nikula 	udelay(220);
1095379bc100SJani Nikula 
1096379bc100SJani Nikula 	/* Switch from Rawclk to PCDclk */
1097379bc100SJani Nikula 	rx_ctl_val |= FDI_PCDCLK;
1098379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1099379bc100SJani Nikula 
1100379bc100SJani Nikula 	/* Configure Port Clock Select */
1101379bc100SJani Nikula 	ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
1102379bc100SJani Nikula 	I915_WRITE(PORT_CLK_SEL(PORT_E), ddi_pll_sel);
1103379bc100SJani Nikula 	WARN_ON(ddi_pll_sel != PORT_CLK_SEL_SPLL);
1104379bc100SJani Nikula 
1105379bc100SJani Nikula 	/* Start the training iterating through available voltages and emphasis,
1106379bc100SJani Nikula 	 * testing each value twice. */
1107379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
1108379bc100SJani Nikula 		/* Configure DP_TP_CTL with auto-training */
1109379bc100SJani Nikula 		I915_WRITE(DP_TP_CTL(PORT_E),
1110379bc100SJani Nikula 					DP_TP_CTL_FDI_AUTOTRAIN |
1111379bc100SJani Nikula 					DP_TP_CTL_ENHANCED_FRAME_ENABLE |
1112379bc100SJani Nikula 					DP_TP_CTL_LINK_TRAIN_PAT1 |
1113379bc100SJani Nikula 					DP_TP_CTL_ENABLE);
1114379bc100SJani Nikula 
1115379bc100SJani Nikula 		/* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
1116379bc100SJani Nikula 		 * DDI E does not support port reversal, the functionality is
1117379bc100SJani Nikula 		 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
1118379bc100SJani Nikula 		 * port reversal bit */
1119379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(PORT_E),
1120379bc100SJani Nikula 			   DDI_BUF_CTL_ENABLE |
1121379bc100SJani Nikula 			   ((crtc_state->fdi_lanes - 1) << 1) |
1122379bc100SJani Nikula 			   DDI_BUF_TRANS_SELECT(i / 2));
1123379bc100SJani Nikula 		POSTING_READ(DDI_BUF_CTL(PORT_E));
1124379bc100SJani Nikula 
1125379bc100SJani Nikula 		udelay(600);
1126379bc100SJani Nikula 
1127379bc100SJani Nikula 		/* Program PCH FDI Receiver TU */
1128379bc100SJani Nikula 		I915_WRITE(FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
1129379bc100SJani Nikula 
1130379bc100SJani Nikula 		/* Enable PCH FDI Receiver with auto-training */
1131379bc100SJani Nikula 		rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
1132379bc100SJani Nikula 		I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1133379bc100SJani Nikula 		POSTING_READ(FDI_RX_CTL(PIPE_A));
1134379bc100SJani Nikula 
1135379bc100SJani Nikula 		/* Wait for FDI receiver lane calibration */
1136379bc100SJani Nikula 		udelay(30);
1137379bc100SJani Nikula 
1138379bc100SJani Nikula 		/* Unset FDI_RX_MISC pwrdn lanes */
1139379bc100SJani Nikula 		temp = I915_READ(FDI_RX_MISC(PIPE_A));
1140379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1141379bc100SJani Nikula 		I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1142379bc100SJani Nikula 		POSTING_READ(FDI_RX_MISC(PIPE_A));
1143379bc100SJani Nikula 
1144379bc100SJani Nikula 		/* Wait for FDI auto training time */
1145379bc100SJani Nikula 		udelay(5);
1146379bc100SJani Nikula 
1147379bc100SJani Nikula 		temp = I915_READ(DP_TP_STATUS(PORT_E));
1148379bc100SJani Nikula 		if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
1149379bc100SJani Nikula 			DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
1150379bc100SJani Nikula 			break;
1151379bc100SJani Nikula 		}
1152379bc100SJani Nikula 
1153379bc100SJani Nikula 		/*
1154379bc100SJani Nikula 		 * Leave things enabled even if we failed to train FDI.
1155379bc100SJani Nikula 		 * Results in less fireworks from the state checker.
1156379bc100SJani Nikula 		 */
1157379bc100SJani Nikula 		if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
1158379bc100SJani Nikula 			DRM_ERROR("FDI link training failed!\n");
1159379bc100SJani Nikula 			break;
1160379bc100SJani Nikula 		}
1161379bc100SJani Nikula 
1162379bc100SJani Nikula 		rx_ctl_val &= ~FDI_RX_ENABLE;
1163379bc100SJani Nikula 		I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1164379bc100SJani Nikula 		POSTING_READ(FDI_RX_CTL(PIPE_A));
1165379bc100SJani Nikula 
1166379bc100SJani Nikula 		temp = I915_READ(DDI_BUF_CTL(PORT_E));
1167379bc100SJani Nikula 		temp &= ~DDI_BUF_CTL_ENABLE;
1168379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
1169379bc100SJani Nikula 		POSTING_READ(DDI_BUF_CTL(PORT_E));
1170379bc100SJani Nikula 
1171379bc100SJani Nikula 		/* Disable DP_TP_CTL and FDI_RX_CTL and retry */
1172379bc100SJani Nikula 		temp = I915_READ(DP_TP_CTL(PORT_E));
1173379bc100SJani Nikula 		temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1174379bc100SJani Nikula 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1175379bc100SJani Nikula 		I915_WRITE(DP_TP_CTL(PORT_E), temp);
1176379bc100SJani Nikula 		POSTING_READ(DP_TP_CTL(PORT_E));
1177379bc100SJani Nikula 
1178379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, PORT_E);
1179379bc100SJani Nikula 
1180379bc100SJani Nikula 		/* Reset FDI_RX_MISC pwrdn lanes */
1181379bc100SJani Nikula 		temp = I915_READ(FDI_RX_MISC(PIPE_A));
1182379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1183379bc100SJani Nikula 		temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1184379bc100SJani Nikula 		I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1185379bc100SJani Nikula 		POSTING_READ(FDI_RX_MISC(PIPE_A));
1186379bc100SJani Nikula 	}
1187379bc100SJani Nikula 
1188379bc100SJani Nikula 	/* Enable normal pixel sending for FDI */
1189379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(PORT_E),
1190379bc100SJani Nikula 		   DP_TP_CTL_FDI_AUTOTRAIN |
1191379bc100SJani Nikula 		   DP_TP_CTL_LINK_TRAIN_NORMAL |
1192379bc100SJani Nikula 		   DP_TP_CTL_ENHANCED_FRAME_ENABLE |
1193379bc100SJani Nikula 		   DP_TP_CTL_ENABLE);
1194379bc100SJani Nikula }
1195379bc100SJani Nikula 
1196379bc100SJani Nikula static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
1197379bc100SJani Nikula {
1198379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1199379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port =
1200379bc100SJani Nikula 		enc_to_dig_port(&encoder->base);
1201379bc100SJani Nikula 
1202379bc100SJani Nikula 	intel_dp->DP = intel_dig_port->saved_port_bits |
1203379bc100SJani Nikula 		DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
1204379bc100SJani Nikula 	intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
1205379bc100SJani Nikula }
1206379bc100SJani Nikula 
1207379bc100SJani Nikula static struct intel_encoder *
1208379bc100SJani Nikula intel_ddi_get_crtc_encoder(struct intel_crtc *crtc)
1209379bc100SJani Nikula {
1210379bc100SJani Nikula 	struct drm_device *dev = crtc->base.dev;
1211379bc100SJani Nikula 	struct intel_encoder *encoder, *ret = NULL;
1212379bc100SJani Nikula 	int num_encoders = 0;
1213379bc100SJani Nikula 
1214379bc100SJani Nikula 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
1215379bc100SJani Nikula 		ret = encoder;
1216379bc100SJani Nikula 		num_encoders++;
1217379bc100SJani Nikula 	}
1218379bc100SJani Nikula 
1219379bc100SJani Nikula 	if (num_encoders != 1)
1220379bc100SJani Nikula 		WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
1221379bc100SJani Nikula 		     pipe_name(crtc->pipe));
1222379bc100SJani Nikula 
1223379bc100SJani Nikula 	BUG_ON(ret == NULL);
1224379bc100SJani Nikula 	return ret;
1225379bc100SJani Nikula }
1226379bc100SJani Nikula 
1227379bc100SJani Nikula static int hsw_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
1228379bc100SJani Nikula 				   i915_reg_t reg)
1229379bc100SJani Nikula {
1230379bc100SJani Nikula 	int refclk;
1231379bc100SJani Nikula 	int n, p, r;
1232379bc100SJani Nikula 	u32 wrpll;
1233379bc100SJani Nikula 
1234379bc100SJani Nikula 	wrpll = I915_READ(reg);
1235379bc100SJani Nikula 	switch (wrpll & WRPLL_REF_MASK) {
1236379bc100SJani Nikula 	case WRPLL_REF_SPECIAL_HSW:
1237379bc100SJani Nikula 		/*
1238379bc100SJani Nikula 		 * muxed-SSC for BDW.
1239379bc100SJani Nikula 		 * non-SSC for non-ULT HSW. Check FUSE_STRAP3
1240379bc100SJani Nikula 		 * for the non-SSC reference frequency.
1241379bc100SJani Nikula 		 */
1242379bc100SJani Nikula 		if (IS_HASWELL(dev_priv) && !IS_HSW_ULT(dev_priv)) {
1243379bc100SJani Nikula 			if (I915_READ(FUSE_STRAP3) & HSW_REF_CLK_SELECT)
1244379bc100SJani Nikula 				refclk = 24;
1245379bc100SJani Nikula 			else
1246379bc100SJani Nikula 				refclk = 135;
1247379bc100SJani Nikula 			break;
1248379bc100SJani Nikula 		}
1249379bc100SJani Nikula 		/* fall through */
1250379bc100SJani Nikula 	case WRPLL_REF_PCH_SSC:
1251379bc100SJani Nikula 		/*
1252379bc100SJani Nikula 		 * We could calculate spread here, but our checking
1253379bc100SJani Nikula 		 * code only cares about 5% accuracy, and spread is a max of
1254379bc100SJani Nikula 		 * 0.5% downspread.
1255379bc100SJani Nikula 		 */
1256379bc100SJani Nikula 		refclk = 135;
1257379bc100SJani Nikula 		break;
1258379bc100SJani Nikula 	case WRPLL_REF_LCPLL:
1259379bc100SJani Nikula 		refclk = 2700;
1260379bc100SJani Nikula 		break;
1261379bc100SJani Nikula 	default:
1262379bc100SJani Nikula 		MISSING_CASE(wrpll);
1263379bc100SJani Nikula 		return 0;
1264379bc100SJani Nikula 	}
1265379bc100SJani Nikula 
1266379bc100SJani Nikula 	r = wrpll & WRPLL_DIVIDER_REF_MASK;
1267379bc100SJani Nikula 	p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
1268379bc100SJani Nikula 	n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
1269379bc100SJani Nikula 
1270379bc100SJani Nikula 	/* Convert to KHz, p & r have a fixed point portion */
1271379bc100SJani Nikula 	return (refclk * n * 100) / (p * r);
1272379bc100SJani Nikula }
1273379bc100SJani Nikula 
1274379bc100SJani Nikula static int skl_calc_wrpll_link(const struct intel_dpll_hw_state *pll_state)
1275379bc100SJani Nikula {
1276379bc100SJani Nikula 	u32 p0, p1, p2, dco_freq;
1277379bc100SJani Nikula 
1278379bc100SJani Nikula 	p0 = pll_state->cfgcr2 & DPLL_CFGCR2_PDIV_MASK;
1279379bc100SJani Nikula 	p2 = pll_state->cfgcr2 & DPLL_CFGCR2_KDIV_MASK;
1280379bc100SJani Nikula 
1281379bc100SJani Nikula 	if (pll_state->cfgcr2 &  DPLL_CFGCR2_QDIV_MODE(1))
1282379bc100SJani Nikula 		p1 = (pll_state->cfgcr2 & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
1283379bc100SJani Nikula 	else
1284379bc100SJani Nikula 		p1 = 1;
1285379bc100SJani Nikula 
1286379bc100SJani Nikula 
1287379bc100SJani Nikula 	switch (p0) {
1288379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_1:
1289379bc100SJani Nikula 		p0 = 1;
1290379bc100SJani Nikula 		break;
1291379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_2:
1292379bc100SJani Nikula 		p0 = 2;
1293379bc100SJani Nikula 		break;
1294379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_3:
1295379bc100SJani Nikula 		p0 = 3;
1296379bc100SJani Nikula 		break;
1297379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_7:
1298379bc100SJani Nikula 		p0 = 7;
1299379bc100SJani Nikula 		break;
1300379bc100SJani Nikula 	}
1301379bc100SJani Nikula 
1302379bc100SJani Nikula 	switch (p2) {
1303379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_5:
1304379bc100SJani Nikula 		p2 = 5;
1305379bc100SJani Nikula 		break;
1306379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_2:
1307379bc100SJani Nikula 		p2 = 2;
1308379bc100SJani Nikula 		break;
1309379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_3:
1310379bc100SJani Nikula 		p2 = 3;
1311379bc100SJani Nikula 		break;
1312379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_1:
1313379bc100SJani Nikula 		p2 = 1;
1314379bc100SJani Nikula 		break;
1315379bc100SJani Nikula 	}
1316379bc100SJani Nikula 
1317379bc100SJani Nikula 	dco_freq = (pll_state->cfgcr1 & DPLL_CFGCR1_DCO_INTEGER_MASK)
1318379bc100SJani Nikula 		* 24 * 1000;
1319379bc100SJani Nikula 
1320379bc100SJani Nikula 	dco_freq += (((pll_state->cfgcr1 & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9)
1321379bc100SJani Nikula 		     * 24 * 1000) / 0x8000;
1322379bc100SJani Nikula 
1323379bc100SJani Nikula 	if (WARN_ON(p0 == 0 || p1 == 0 || p2 == 0))
1324379bc100SJani Nikula 		return 0;
1325379bc100SJani Nikula 
1326379bc100SJani Nikula 	return dco_freq / (p0 * p1 * p2 * 5);
1327379bc100SJani Nikula }
1328379bc100SJani Nikula 
1329379bc100SJani Nikula int cnl_calc_wrpll_link(struct drm_i915_private *dev_priv,
1330379bc100SJani Nikula 			struct intel_dpll_hw_state *pll_state)
1331379bc100SJani Nikula {
1332379bc100SJani Nikula 	u32 p0, p1, p2, dco_freq, ref_clock;
1333379bc100SJani Nikula 
1334379bc100SJani Nikula 	p0 = pll_state->cfgcr1 & DPLL_CFGCR1_PDIV_MASK;
1335379bc100SJani Nikula 	p2 = pll_state->cfgcr1 & DPLL_CFGCR1_KDIV_MASK;
1336379bc100SJani Nikula 
1337379bc100SJani Nikula 	if (pll_state->cfgcr1 & DPLL_CFGCR1_QDIV_MODE(1))
1338379bc100SJani Nikula 		p1 = (pll_state->cfgcr1 & DPLL_CFGCR1_QDIV_RATIO_MASK) >>
1339379bc100SJani Nikula 			DPLL_CFGCR1_QDIV_RATIO_SHIFT;
1340379bc100SJani Nikula 	else
1341379bc100SJani Nikula 		p1 = 1;
1342379bc100SJani Nikula 
1343379bc100SJani Nikula 
1344379bc100SJani Nikula 	switch (p0) {
1345379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_2:
1346379bc100SJani Nikula 		p0 = 2;
1347379bc100SJani Nikula 		break;
1348379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_3:
1349379bc100SJani Nikula 		p0 = 3;
1350379bc100SJani Nikula 		break;
1351379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_5:
1352379bc100SJani Nikula 		p0 = 5;
1353379bc100SJani Nikula 		break;
1354379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_7:
1355379bc100SJani Nikula 		p0 = 7;
1356379bc100SJani Nikula 		break;
1357379bc100SJani Nikula 	}
1358379bc100SJani Nikula 
1359379bc100SJani Nikula 	switch (p2) {
1360379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_1:
1361379bc100SJani Nikula 		p2 = 1;
1362379bc100SJani Nikula 		break;
1363379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_2:
1364379bc100SJani Nikula 		p2 = 2;
1365379bc100SJani Nikula 		break;
1366379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_3:
1367379bc100SJani Nikula 		p2 = 3;
1368379bc100SJani Nikula 		break;
1369379bc100SJani Nikula 	}
1370379bc100SJani Nikula 
1371379bc100SJani Nikula 	ref_clock = cnl_hdmi_pll_ref_clock(dev_priv);
1372379bc100SJani Nikula 
1373379bc100SJani Nikula 	dco_freq = (pll_state->cfgcr0 & DPLL_CFGCR0_DCO_INTEGER_MASK)
1374379bc100SJani Nikula 		* ref_clock;
1375379bc100SJani Nikula 
1376379bc100SJani Nikula 	dco_freq += (((pll_state->cfgcr0 & DPLL_CFGCR0_DCO_FRACTION_MASK) >>
1377379bc100SJani Nikula 		      DPLL_CFGCR0_DCO_FRACTION_SHIFT) * ref_clock) / 0x8000;
1378379bc100SJani Nikula 
1379379bc100SJani Nikula 	if (WARN_ON(p0 == 0 || p1 == 0 || p2 == 0))
1380379bc100SJani Nikula 		return 0;
1381379bc100SJani Nikula 
1382379bc100SJani Nikula 	return dco_freq / (p0 * p1 * p2 * 5);
1383379bc100SJani Nikula }
1384379bc100SJani Nikula 
1385379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
1386379bc100SJani Nikula 				 enum port port)
1387379bc100SJani Nikula {
1388379bc100SJani Nikula 	u32 val = I915_READ(DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
1389379bc100SJani Nikula 
1390379bc100SJani Nikula 	switch (val) {
1391379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
1392379bc100SJani Nikula 		return 0;
1393379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
1394379bc100SJani Nikula 		return 162000;
1395379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
1396379bc100SJani Nikula 		return 270000;
1397379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
1398379bc100SJani Nikula 		return 540000;
1399379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
1400379bc100SJani Nikula 		return 810000;
1401379bc100SJani Nikula 	default:
1402379bc100SJani Nikula 		MISSING_CASE(val);
1403379bc100SJani Nikula 		return 0;
1404379bc100SJani Nikula 	}
1405379bc100SJani Nikula }
1406379bc100SJani Nikula 
1407379bc100SJani Nikula static int icl_calc_mg_pll_link(struct drm_i915_private *dev_priv,
1408379bc100SJani Nikula 				const struct intel_dpll_hw_state *pll_state)
1409379bc100SJani Nikula {
1410379bc100SJani Nikula 	u32 m1, m2_int, m2_frac, div1, div2, ref_clock;
1411379bc100SJani Nikula 	u64 tmp;
1412379bc100SJani Nikula 
1413379bc100SJani Nikula 	ref_clock = dev_priv->cdclk.hw.ref;
1414379bc100SJani Nikula 
1415379bc100SJani Nikula 	m1 = pll_state->mg_pll_div1 & MG_PLL_DIV1_FBPREDIV_MASK;
1416379bc100SJani Nikula 	m2_int = pll_state->mg_pll_div0 & MG_PLL_DIV0_FBDIV_INT_MASK;
1417379bc100SJani Nikula 	m2_frac = (pll_state->mg_pll_div0 & MG_PLL_DIV0_FRACNEN_H) ?
1418379bc100SJani Nikula 		(pll_state->mg_pll_div0 & MG_PLL_DIV0_FBDIV_FRAC_MASK) >>
1419379bc100SJani Nikula 		MG_PLL_DIV0_FBDIV_FRAC_SHIFT : 0;
1420379bc100SJani Nikula 
1421379bc100SJani Nikula 	switch (pll_state->mg_clktop2_hsclkctl &
1422379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK) {
1423379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_2:
1424379bc100SJani Nikula 		div1 = 2;
1425379bc100SJani Nikula 		break;
1426379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_3:
1427379bc100SJani Nikula 		div1 = 3;
1428379bc100SJani Nikula 		break;
1429379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_5:
1430379bc100SJani Nikula 		div1 = 5;
1431379bc100SJani Nikula 		break;
1432379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_7:
1433379bc100SJani Nikula 		div1 = 7;
1434379bc100SJani Nikula 		break;
1435379bc100SJani Nikula 	default:
1436379bc100SJani Nikula 		MISSING_CASE(pll_state->mg_clktop2_hsclkctl);
1437379bc100SJani Nikula 		return 0;
1438379bc100SJani Nikula 	}
1439379bc100SJani Nikula 
1440379bc100SJani Nikula 	div2 = (pll_state->mg_clktop2_hsclkctl &
1441379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK) >>
1442379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_SHIFT;
1443379bc100SJani Nikula 
1444379bc100SJani Nikula 	/* div2 value of 0 is same as 1 means no div */
1445379bc100SJani Nikula 	if (div2 == 0)
1446379bc100SJani Nikula 		div2 = 1;
1447379bc100SJani Nikula 
1448379bc100SJani Nikula 	/*
1449379bc100SJani Nikula 	 * Adjust the original formula to delay the division by 2^22 in order to
1450379bc100SJani Nikula 	 * minimize possible rounding errors.
1451379bc100SJani Nikula 	 */
1452379bc100SJani Nikula 	tmp = (u64)m1 * m2_int * ref_clock +
1453379bc100SJani Nikula 	      (((u64)m1 * m2_frac * ref_clock) >> 22);
1454379bc100SJani Nikula 	tmp = div_u64(tmp, 5 * div1 * div2);
1455379bc100SJani Nikula 
1456379bc100SJani Nikula 	return tmp;
1457379bc100SJani Nikula }
1458379bc100SJani Nikula 
1459379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
1460379bc100SJani Nikula {
1461379bc100SJani Nikula 	int dotclock;
1462379bc100SJani Nikula 
1463379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
1464379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1465379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
1466379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
1467379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1468379bc100SJani Nikula 						    &pipe_config->dp_m_n);
1469379bc100SJani Nikula 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp == 36)
1470379bc100SJani Nikula 		dotclock = pipe_config->port_clock * 2 / 3;
1471379bc100SJani Nikula 	else
1472379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
1473379bc100SJani Nikula 
1474379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
1475379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
1476379bc100SJani Nikula 		dotclock *= 2;
1477379bc100SJani Nikula 
1478379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
1479379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
1480379bc100SJani Nikula 
1481379bc100SJani Nikula 	pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1482379bc100SJani Nikula }
1483379bc100SJani Nikula 
1484379bc100SJani Nikula static void icl_ddi_clock_get(struct intel_encoder *encoder,
1485379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1486379bc100SJani Nikula {
1487379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1488379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1489379bc100SJani Nikula 	enum port port = encoder->port;
1490379bc100SJani Nikula 	int link_clock;
1491379bc100SJani Nikula 
1492379bc100SJani Nikula 	if (intel_port_is_combophy(dev_priv, port)) {
1493379bc100SJani Nikula 		link_clock = cnl_calc_wrpll_link(dev_priv, pll_state);
1494379bc100SJani Nikula 	} else {
1495379bc100SJani Nikula 		enum intel_dpll_id pll_id = intel_get_shared_dpll_id(dev_priv,
1496379bc100SJani Nikula 						pipe_config->shared_dpll);
1497379bc100SJani Nikula 
1498379bc100SJani Nikula 		if (pll_id == DPLL_ID_ICL_TBTPLL)
1499379bc100SJani Nikula 			link_clock = icl_calc_tbt_pll_link(dev_priv, port);
1500379bc100SJani Nikula 		else
1501379bc100SJani Nikula 			link_clock = icl_calc_mg_pll_link(dev_priv, pll_state);
1502379bc100SJani Nikula 	}
1503379bc100SJani Nikula 
1504379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1505379bc100SJani Nikula 
1506379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1507379bc100SJani Nikula }
1508379bc100SJani Nikula 
1509379bc100SJani Nikula static void cnl_ddi_clock_get(struct intel_encoder *encoder,
1510379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1511379bc100SJani Nikula {
1512379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1513379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1514379bc100SJani Nikula 	int link_clock;
1515379bc100SJani Nikula 
1516379bc100SJani Nikula 	if (pll_state->cfgcr0 & DPLL_CFGCR0_HDMI_MODE) {
1517379bc100SJani Nikula 		link_clock = cnl_calc_wrpll_link(dev_priv, pll_state);
1518379bc100SJani Nikula 	} else {
1519379bc100SJani Nikula 		link_clock = pll_state->cfgcr0 & DPLL_CFGCR0_LINK_RATE_MASK;
1520379bc100SJani Nikula 
1521379bc100SJani Nikula 		switch (link_clock) {
1522379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_810:
1523379bc100SJani Nikula 			link_clock = 81000;
1524379bc100SJani Nikula 			break;
1525379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1080:
1526379bc100SJani Nikula 			link_clock = 108000;
1527379bc100SJani Nikula 			break;
1528379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1350:
1529379bc100SJani Nikula 			link_clock = 135000;
1530379bc100SJani Nikula 			break;
1531379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1620:
1532379bc100SJani Nikula 			link_clock = 162000;
1533379bc100SJani Nikula 			break;
1534379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_2160:
1535379bc100SJani Nikula 			link_clock = 216000;
1536379bc100SJani Nikula 			break;
1537379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_2700:
1538379bc100SJani Nikula 			link_clock = 270000;
1539379bc100SJani Nikula 			break;
1540379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_3240:
1541379bc100SJani Nikula 			link_clock = 324000;
1542379bc100SJani Nikula 			break;
1543379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_4050:
1544379bc100SJani Nikula 			link_clock = 405000;
1545379bc100SJani Nikula 			break;
1546379bc100SJani Nikula 		default:
1547379bc100SJani Nikula 			WARN(1, "Unsupported link rate\n");
1548379bc100SJani Nikula 			break;
1549379bc100SJani Nikula 		}
1550379bc100SJani Nikula 		link_clock *= 2;
1551379bc100SJani Nikula 	}
1552379bc100SJani Nikula 
1553379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1554379bc100SJani Nikula 
1555379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1556379bc100SJani Nikula }
1557379bc100SJani Nikula 
1558379bc100SJani Nikula static void skl_ddi_clock_get(struct intel_encoder *encoder,
1559379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1560379bc100SJani Nikula {
1561379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1562379bc100SJani Nikula 	int link_clock;
1563379bc100SJani Nikula 
1564379bc100SJani Nikula 	/*
1565379bc100SJani Nikula 	 * ctrl1 register is already shifted for each pll, just use 0 to get
1566379bc100SJani Nikula 	 * the internal shift for each field
1567379bc100SJani Nikula 	 */
1568379bc100SJani Nikula 	if (pll_state->ctrl1 & DPLL_CTRL1_HDMI_MODE(0)) {
1569379bc100SJani Nikula 		link_clock = skl_calc_wrpll_link(pll_state);
1570379bc100SJani Nikula 	} else {
1571379bc100SJani Nikula 		link_clock = pll_state->ctrl1 & DPLL_CTRL1_LINK_RATE_MASK(0);
1572379bc100SJani Nikula 		link_clock >>= DPLL_CTRL1_LINK_RATE_SHIFT(0);
1573379bc100SJani Nikula 
1574379bc100SJani Nikula 		switch (link_clock) {
1575379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_810:
1576379bc100SJani Nikula 			link_clock = 81000;
1577379bc100SJani Nikula 			break;
1578379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1080:
1579379bc100SJani Nikula 			link_clock = 108000;
1580379bc100SJani Nikula 			break;
1581379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1350:
1582379bc100SJani Nikula 			link_clock = 135000;
1583379bc100SJani Nikula 			break;
1584379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1620:
1585379bc100SJani Nikula 			link_clock = 162000;
1586379bc100SJani Nikula 			break;
1587379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_2160:
1588379bc100SJani Nikula 			link_clock = 216000;
1589379bc100SJani Nikula 			break;
1590379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_2700:
1591379bc100SJani Nikula 			link_clock = 270000;
1592379bc100SJani Nikula 			break;
1593379bc100SJani Nikula 		default:
1594379bc100SJani Nikula 			WARN(1, "Unsupported link rate\n");
1595379bc100SJani Nikula 			break;
1596379bc100SJani Nikula 		}
1597379bc100SJani Nikula 		link_clock *= 2;
1598379bc100SJani Nikula 	}
1599379bc100SJani Nikula 
1600379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1601379bc100SJani Nikula 
1602379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1603379bc100SJani Nikula }
1604379bc100SJani Nikula 
1605379bc100SJani Nikula static void hsw_ddi_clock_get(struct intel_encoder *encoder,
1606379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1607379bc100SJani Nikula {
1608379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1609379bc100SJani Nikula 	int link_clock = 0;
1610379bc100SJani Nikula 	u32 val, pll;
1611379bc100SJani Nikula 
1612379bc100SJani Nikula 	val = hsw_pll_to_ddi_pll_sel(pipe_config->shared_dpll);
1613379bc100SJani Nikula 	switch (val & PORT_CLK_SEL_MASK) {
1614379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_810:
1615379bc100SJani Nikula 		link_clock = 81000;
1616379bc100SJani Nikula 		break;
1617379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_1350:
1618379bc100SJani Nikula 		link_clock = 135000;
1619379bc100SJani Nikula 		break;
1620379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_2700:
1621379bc100SJani Nikula 		link_clock = 270000;
1622379bc100SJani Nikula 		break;
1623379bc100SJani Nikula 	case PORT_CLK_SEL_WRPLL1:
1624379bc100SJani Nikula 		link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(0));
1625379bc100SJani Nikula 		break;
1626379bc100SJani Nikula 	case PORT_CLK_SEL_WRPLL2:
1627379bc100SJani Nikula 		link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(1));
1628379bc100SJani Nikula 		break;
1629379bc100SJani Nikula 	case PORT_CLK_SEL_SPLL:
1630379bc100SJani Nikula 		pll = I915_READ(SPLL_CTL) & SPLL_FREQ_MASK;
1631379bc100SJani Nikula 		if (pll == SPLL_FREQ_810MHz)
1632379bc100SJani Nikula 			link_clock = 81000;
1633379bc100SJani Nikula 		else if (pll == SPLL_FREQ_1350MHz)
1634379bc100SJani Nikula 			link_clock = 135000;
1635379bc100SJani Nikula 		else if (pll == SPLL_FREQ_2700MHz)
1636379bc100SJani Nikula 			link_clock = 270000;
1637379bc100SJani Nikula 		else {
1638379bc100SJani Nikula 			WARN(1, "bad spll freq\n");
1639379bc100SJani Nikula 			return;
1640379bc100SJani Nikula 		}
1641379bc100SJani Nikula 		break;
1642379bc100SJani Nikula 	default:
1643379bc100SJani Nikula 		WARN(1, "bad port clock sel\n");
1644379bc100SJani Nikula 		return;
1645379bc100SJani Nikula 	}
1646379bc100SJani Nikula 
1647379bc100SJani Nikula 	pipe_config->port_clock = link_clock * 2;
1648379bc100SJani Nikula 
1649379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1650379bc100SJani Nikula }
1651379bc100SJani Nikula 
1652379bc100SJani Nikula static int bxt_calc_pll_link(const struct intel_dpll_hw_state *pll_state)
1653379bc100SJani Nikula {
1654379bc100SJani Nikula 	struct dpll clock;
1655379bc100SJani Nikula 
1656379bc100SJani Nikula 	clock.m1 = 2;
1657379bc100SJani Nikula 	clock.m2 = (pll_state->pll0 & PORT_PLL_M2_MASK) << 22;
1658379bc100SJani Nikula 	if (pll_state->pll3 & PORT_PLL_M2_FRAC_ENABLE)
1659379bc100SJani Nikula 		clock.m2 |= pll_state->pll2 & PORT_PLL_M2_FRAC_MASK;
1660379bc100SJani Nikula 	clock.n = (pll_state->pll1 & PORT_PLL_N_MASK) >> PORT_PLL_N_SHIFT;
1661379bc100SJani Nikula 	clock.p1 = (pll_state->ebb0 & PORT_PLL_P1_MASK) >> PORT_PLL_P1_SHIFT;
1662379bc100SJani Nikula 	clock.p2 = (pll_state->ebb0 & PORT_PLL_P2_MASK) >> PORT_PLL_P2_SHIFT;
1663379bc100SJani Nikula 
1664379bc100SJani Nikula 	return chv_calc_dpll_params(100000, &clock);
1665379bc100SJani Nikula }
1666379bc100SJani Nikula 
1667379bc100SJani Nikula static void bxt_ddi_clock_get(struct intel_encoder *encoder,
1668379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1669379bc100SJani Nikula {
1670379bc100SJani Nikula 	pipe_config->port_clock =
1671379bc100SJani Nikula 		bxt_calc_pll_link(&pipe_config->dpll_hw_state);
1672379bc100SJani Nikula 
1673379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1674379bc100SJani Nikula }
1675379bc100SJani Nikula 
1676379bc100SJani Nikula static void intel_ddi_clock_get(struct intel_encoder *encoder,
1677379bc100SJani Nikula 				struct intel_crtc_state *pipe_config)
1678379bc100SJani Nikula {
1679379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1680379bc100SJani Nikula 
1681379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
1682379bc100SJani Nikula 		icl_ddi_clock_get(encoder, pipe_config);
1683379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
1684379bc100SJani Nikula 		cnl_ddi_clock_get(encoder, pipe_config);
1685379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
1686379bc100SJani Nikula 		bxt_ddi_clock_get(encoder, pipe_config);
1687379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
1688379bc100SJani Nikula 		skl_ddi_clock_get(encoder, pipe_config);
1689379bc100SJani Nikula 	else if (INTEL_GEN(dev_priv) <= 8)
1690379bc100SJani Nikula 		hsw_ddi_clock_get(encoder, pipe_config);
1691379bc100SJani Nikula }
1692379bc100SJani Nikula 
1693379bc100SJani Nikula void intel_ddi_set_pipe_settings(const struct intel_crtc_state *crtc_state)
1694379bc100SJani Nikula {
1695379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1696379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1697379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1698379bc100SJani Nikula 	u32 temp;
1699379bc100SJani Nikula 
1700379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
1701379bc100SJani Nikula 		return;
1702379bc100SJani Nikula 
1703379bc100SJani Nikula 	WARN_ON(transcoder_is_dsi(cpu_transcoder));
1704379bc100SJani Nikula 
1705379bc100SJani Nikula 	temp = TRANS_MSA_SYNC_CLK;
1706379bc100SJani Nikula 
1707379bc100SJani Nikula 	if (crtc_state->limited_color_range)
1708379bc100SJani Nikula 		temp |= TRANS_MSA_CEA_RANGE;
1709379bc100SJani Nikula 
1710379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1711379bc100SJani Nikula 	case 18:
1712379bc100SJani Nikula 		temp |= TRANS_MSA_6_BPC;
1713379bc100SJani Nikula 		break;
1714379bc100SJani Nikula 	case 24:
1715379bc100SJani Nikula 		temp |= TRANS_MSA_8_BPC;
1716379bc100SJani Nikula 		break;
1717379bc100SJani Nikula 	case 30:
1718379bc100SJani Nikula 		temp |= TRANS_MSA_10_BPC;
1719379bc100SJani Nikula 		break;
1720379bc100SJani Nikula 	case 36:
1721379bc100SJani Nikula 		temp |= TRANS_MSA_12_BPC;
1722379bc100SJani Nikula 		break;
1723379bc100SJani Nikula 	default:
1724379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
1725379bc100SJani Nikula 		break;
1726379bc100SJani Nikula 	}
1727379bc100SJani Nikula 
1728379bc100SJani Nikula 	/*
1729379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
1730379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
1731379bc100SJani Nikula 	 * colorspace information. The output colorspace encoding is BT601.
1732379bc100SJani Nikula 	 */
1733379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
1734379bc100SJani Nikula 		temp |= TRANS_MSA_SAMPLING_444 | TRANS_MSA_CLRSP_YCBCR;
1735379bc100SJani Nikula 	/*
1736379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
1737379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
1738379bc100SJani Nikula 	 * YCBCR 420 signals we should program MSA MISC1 fields which
1739379bc100SJani Nikula 	 * indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
1740379bc100SJani Nikula 	 */
1741379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1742379bc100SJani Nikula 		temp |= TRANS_MSA_USE_VSC_SDP;
1743379bc100SJani Nikula 	I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
1744379bc100SJani Nikula }
1745379bc100SJani Nikula 
1746379bc100SJani Nikula void intel_ddi_set_vc_payload_alloc(const struct intel_crtc_state *crtc_state,
1747379bc100SJani Nikula 				    bool state)
1748379bc100SJani Nikula {
1749379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1750379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1751379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1752379bc100SJani Nikula 	u32 temp;
1753379bc100SJani Nikula 
1754379bc100SJani Nikula 	temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1755379bc100SJani Nikula 	if (state == true)
1756379bc100SJani Nikula 		temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1757379bc100SJani Nikula 	else
1758379bc100SJani Nikula 		temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1759379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1760379bc100SJani Nikula }
1761379bc100SJani Nikula 
1762379bc100SJani Nikula void intel_ddi_enable_transcoder_func(const struct intel_crtc_state *crtc_state)
1763379bc100SJani Nikula {
1764379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1765379bc100SJani Nikula 	struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
1766379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1767379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
1768379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1769379bc100SJani Nikula 	enum port port = encoder->port;
1770379bc100SJani Nikula 	u32 temp;
1771379bc100SJani Nikula 
1772379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1773379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
1774379bc100SJani Nikula 	temp |= TRANS_DDI_SELECT_PORT(port);
1775379bc100SJani Nikula 
1776379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1777379bc100SJani Nikula 	case 18:
1778379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
1779379bc100SJani Nikula 		break;
1780379bc100SJani Nikula 	case 24:
1781379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
1782379bc100SJani Nikula 		break;
1783379bc100SJani Nikula 	case 30:
1784379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
1785379bc100SJani Nikula 		break;
1786379bc100SJani Nikula 	case 36:
1787379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
1788379bc100SJani Nikula 		break;
1789379bc100SJani Nikula 	default:
1790379bc100SJani Nikula 		BUG();
1791379bc100SJani Nikula 	}
1792379bc100SJani Nikula 
1793379bc100SJani Nikula 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
1794379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
1795379bc100SJani Nikula 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
1796379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
1797379bc100SJani Nikula 
1798379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
1799379bc100SJani Nikula 		switch (pipe) {
1800379bc100SJani Nikula 		case PIPE_A:
1801379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
1802379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
1803379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
1804379bc100SJani Nikula 			 * support). */
1805379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
1806379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1807379bc100SJani Nikula 			else
1808379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
1809379bc100SJani Nikula 			break;
1810379bc100SJani Nikula 		case PIPE_B:
1811379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1812379bc100SJani Nikula 			break;
1813379bc100SJani Nikula 		case PIPE_C:
1814379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1815379bc100SJani Nikula 			break;
1816379bc100SJani Nikula 		default:
1817379bc100SJani Nikula 			BUG();
1818379bc100SJani Nikula 			break;
1819379bc100SJani Nikula 		}
1820379bc100SJani Nikula 	}
1821379bc100SJani Nikula 
1822379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1823379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
1824379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
1825379bc100SJani Nikula 		else
1826379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
1827379bc100SJani Nikula 
1828379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
1829379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
1830379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
1831379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
1832379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1833379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI;
1834379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
1835379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
1836379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1837379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1838379bc100SJani Nikula 	} else {
1839379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1840379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1841379bc100SJani Nikula 	}
1842379bc100SJani Nikula 
1843379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1844379bc100SJani Nikula }
1845379bc100SJani Nikula 
1846379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
1847379bc100SJani Nikula {
1848379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1849379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1850379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1851379bc100SJani Nikula 	i915_reg_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1852379bc100SJani Nikula 	u32 val = I915_READ(reg);
1853379bc100SJani Nikula 
1854379bc100SJani Nikula 	val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK | TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
1855379bc100SJani Nikula 	val |= TRANS_DDI_PORT_NONE;
1856379bc100SJani Nikula 	I915_WRITE(reg, val);
1857379bc100SJani Nikula 
1858379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
1859379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1860379bc100SJani Nikula 		DRM_DEBUG_KMS("Quirk Increase DDI disabled time\n");
1861379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
1862379bc100SJani Nikula 		msleep(100);
1863379bc100SJani Nikula 	}
1864379bc100SJani Nikula }
1865379bc100SJani Nikula 
1866379bc100SJani Nikula int intel_ddi_toggle_hdcp_signalling(struct intel_encoder *intel_encoder,
1867379bc100SJani Nikula 				     bool enable)
1868379bc100SJani Nikula {
1869379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
1870379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1871379bc100SJani Nikula 	intel_wakeref_t wakeref;
1872379bc100SJani Nikula 	enum pipe pipe = 0;
1873379bc100SJani Nikula 	int ret = 0;
1874379bc100SJani Nikula 	u32 tmp;
1875379bc100SJani Nikula 
1876379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1877379bc100SJani Nikula 						     intel_encoder->power_domain);
1878379bc100SJani Nikula 	if (WARN_ON(!wakeref))
1879379bc100SJani Nikula 		return -ENXIO;
1880379bc100SJani Nikula 
1881379bc100SJani Nikula 	if (WARN_ON(!intel_encoder->get_hw_state(intel_encoder, &pipe))) {
1882379bc100SJani Nikula 		ret = -EIO;
1883379bc100SJani Nikula 		goto out;
1884379bc100SJani Nikula 	}
1885379bc100SJani Nikula 
1886379bc100SJani Nikula 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe));
1887379bc100SJani Nikula 	if (enable)
1888379bc100SJani Nikula 		tmp |= TRANS_DDI_HDCP_SIGNALLING;
1889379bc100SJani Nikula 	else
1890379bc100SJani Nikula 		tmp &= ~TRANS_DDI_HDCP_SIGNALLING;
1891379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(pipe), tmp);
1892379bc100SJani Nikula out:
1893379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
1894379bc100SJani Nikula 	return ret;
1895379bc100SJani Nikula }
1896379bc100SJani Nikula 
1897379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1898379bc100SJani Nikula {
1899379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
1900379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1901379bc100SJani Nikula 	struct intel_encoder *encoder = intel_connector->encoder;
1902379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
1903379bc100SJani Nikula 	enum port port = encoder->port;
1904379bc100SJani Nikula 	enum transcoder cpu_transcoder;
1905379bc100SJani Nikula 	intel_wakeref_t wakeref;
1906379bc100SJani Nikula 	enum pipe pipe = 0;
1907379bc100SJani Nikula 	u32 tmp;
1908379bc100SJani Nikula 	bool ret;
1909379bc100SJani Nikula 
1910379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1911379bc100SJani Nikula 						     encoder->power_domain);
1912379bc100SJani Nikula 	if (!wakeref)
1913379bc100SJani Nikula 		return false;
1914379bc100SJani Nikula 
1915379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
1916379bc100SJani Nikula 		ret = false;
1917379bc100SJani Nikula 		goto out;
1918379bc100SJani Nikula 	}
1919379bc100SJani Nikula 
1920379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A)
1921379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
1922379bc100SJani Nikula 	else
1923379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
1924379bc100SJani Nikula 
1925379bc100SJani Nikula 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1926379bc100SJani Nikula 
1927379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1928379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
1929379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
1930379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
1931379bc100SJani Nikula 		break;
1932379bc100SJani Nikula 
1933379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
1934379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
1935379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
1936379bc100SJani Nikula 		break;
1937379bc100SJani Nikula 
1938379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
1939379bc100SJani Nikula 		/* if the transcoder is in MST state then
1940379bc100SJani Nikula 		 * connector isn't connected */
1941379bc100SJani Nikula 		ret = false;
1942379bc100SJani Nikula 		break;
1943379bc100SJani Nikula 
1944379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
1945379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_VGA;
1946379bc100SJani Nikula 		break;
1947379bc100SJani Nikula 
1948379bc100SJani Nikula 	default:
1949379bc100SJani Nikula 		ret = false;
1950379bc100SJani Nikula 		break;
1951379bc100SJani Nikula 	}
1952379bc100SJani Nikula 
1953379bc100SJani Nikula out:
1954379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1955379bc100SJani Nikula 
1956379bc100SJani Nikula 	return ret;
1957379bc100SJani Nikula }
1958379bc100SJani Nikula 
1959379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
1960379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
1961379bc100SJani Nikula {
1962379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
1963379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1964379bc100SJani Nikula 	enum port port = encoder->port;
1965379bc100SJani Nikula 	intel_wakeref_t wakeref;
1966379bc100SJani Nikula 	enum pipe p;
1967379bc100SJani Nikula 	u32 tmp;
1968379bc100SJani Nikula 	u8 mst_pipe_mask;
1969379bc100SJani Nikula 
1970379bc100SJani Nikula 	*pipe_mask = 0;
1971379bc100SJani Nikula 	*is_dp_mst = false;
1972379bc100SJani Nikula 
1973379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1974379bc100SJani Nikula 						     encoder->power_domain);
1975379bc100SJani Nikula 	if (!wakeref)
1976379bc100SJani Nikula 		return;
1977379bc100SJani Nikula 
1978379bc100SJani Nikula 	tmp = I915_READ(DDI_BUF_CTL(port));
1979379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
1980379bc100SJani Nikula 		goto out;
1981379bc100SJani Nikula 
1982379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A) {
1983379bc100SJani Nikula 		tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
1984379bc100SJani Nikula 
1985379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1986379bc100SJani Nikula 		default:
1987379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
1988379bc100SJani Nikula 			/* fallthrough */
1989379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
1990379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
1991379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
1992379bc100SJani Nikula 			break;
1993379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
1994379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
1995379bc100SJani Nikula 			break;
1996379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
1997379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
1998379bc100SJani Nikula 			break;
1999379bc100SJani Nikula 		}
2000379bc100SJani Nikula 
2001379bc100SJani Nikula 		goto out;
2002379bc100SJani Nikula 	}
2003379bc100SJani Nikula 
2004379bc100SJani Nikula 	mst_pipe_mask = 0;
2005379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
2006379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
2007379bc100SJani Nikula 
2008379bc100SJani Nikula 		tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
2009379bc100SJani Nikula 
2010379bc100SJani Nikula 		if ((tmp & TRANS_DDI_PORT_MASK) != TRANS_DDI_SELECT_PORT(port))
2011379bc100SJani Nikula 			continue;
2012379bc100SJani Nikula 
2013379bc100SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
2014379bc100SJani Nikula 		    TRANS_DDI_MODE_SELECT_DP_MST)
2015379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
2016379bc100SJani Nikula 
2017379bc100SJani Nikula 		*pipe_mask |= BIT(p);
2018379bc100SJani Nikula 	}
2019379bc100SJani Nikula 
2020379bc100SJani Nikula 	if (!*pipe_mask)
2021379bc100SJani Nikula 		DRM_DEBUG_KMS("No pipe for ddi port %c found\n",
2022379bc100SJani Nikula 			      port_name(port));
2023379bc100SJani Nikula 
2024379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
2025379bc100SJani Nikula 		DRM_DEBUG_KMS("Multiple pipes for non DP-MST port %c (pipe_mask %02x)\n",
2026379bc100SJani Nikula 			      port_name(port), *pipe_mask);
2027379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
2028379bc100SJani Nikula 	}
2029379bc100SJani Nikula 
2030379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
2031379bc100SJani Nikula 		DRM_DEBUG_KMS("Conflicting MST and non-MST encoders for port %c (pipe_mask %02x mst_pipe_mask %02x)\n",
2032379bc100SJani Nikula 			      port_name(port), *pipe_mask, mst_pipe_mask);
2033379bc100SJani Nikula 	else
2034379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
2035379bc100SJani Nikula 
2036379bc100SJani Nikula out:
2037379bc100SJani Nikula 	if (*pipe_mask && IS_GEN9_LP(dev_priv)) {
2038379bc100SJani Nikula 		tmp = I915_READ(BXT_PHY_CTL(port));
2039379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
2040379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
2041379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
2042379bc100SJani Nikula 			DRM_ERROR("Port %c enabled but PHY powered down? "
2043379bc100SJani Nikula 				  "(PHY_CTL %08x)\n", port_name(port), tmp);
2044379bc100SJani Nikula 	}
2045379bc100SJani Nikula 
2046379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
2047379bc100SJani Nikula }
2048379bc100SJani Nikula 
2049379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
2050379bc100SJani Nikula 			    enum pipe *pipe)
2051379bc100SJani Nikula {
2052379bc100SJani Nikula 	u8 pipe_mask;
2053379bc100SJani Nikula 	bool is_mst;
2054379bc100SJani Nikula 
2055379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2056379bc100SJani Nikula 
2057379bc100SJani Nikula 	if (is_mst || !pipe_mask)
2058379bc100SJani Nikula 		return false;
2059379bc100SJani Nikula 
2060379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
2061379bc100SJani Nikula 
2062379bc100SJani Nikula 	return true;
2063379bc100SJani Nikula }
2064379bc100SJani Nikula 
2065379bc100SJani Nikula static inline enum intel_display_power_domain
2066379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
2067379bc100SJani Nikula {
2068379bc100SJani Nikula 	/* CNL+ HW requires corresponding AUX IOs to be powered up for PSR with
2069379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
2070379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
2071379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
2072379bc100SJani Nikula 	 * states enabled.
2073379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
2074379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
2075379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
2076379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
2077379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
2078379bc100SJani Nikula 	 * returns the correct domain for other ports too.
2079379bc100SJani Nikula 	 */
2080379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
2081379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
2082379bc100SJani Nikula }
2083379bc100SJani Nikula 
2084379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
2085379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
2086379bc100SJani Nikula {
2087379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2088379bc100SJani Nikula 	struct intel_digital_port *dig_port;
2089379bc100SJani Nikula 
2090379bc100SJani Nikula 	/*
2091379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
2092379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
2093379bc100SJani Nikula 	 * hook.
2094379bc100SJani Nikula 	 */
2095379bc100SJani Nikula 	if (WARN_ON(intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
2096379bc100SJani Nikula 		return;
2097379bc100SJani Nikula 
2098379bc100SJani Nikula 	dig_port = enc_to_dig_port(&encoder->base);
2099379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
2100379bc100SJani Nikula 
2101379bc100SJani Nikula 	/*
2102379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
2103379bc100SJani Nikula 	 * ports.
2104379bc100SJani Nikula 	 */
2105379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
2106379bc100SJani Nikula 	    intel_port_is_tc(dev_priv, encoder->port))
2107379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2108379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
2109379bc100SJani Nikula 
2110379bc100SJani Nikula 	/*
2111379bc100SJani Nikula 	 * VDSC power is needed when DSC is enabled
2112379bc100SJani Nikula 	 */
2113379bc100SJani Nikula 	if (crtc_state->dsc_params.compression_enable)
2114379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2115379bc100SJani Nikula 					intel_dsc_power_domain(crtc_state));
2116379bc100SJani Nikula }
2117379bc100SJani Nikula 
2118379bc100SJani Nikula void intel_ddi_enable_pipe_clock(const struct intel_crtc_state *crtc_state)
2119379bc100SJani Nikula {
2120379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2121379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2122379bc100SJani Nikula 	struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
2123379bc100SJani Nikula 	enum port port = encoder->port;
2124379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2125379bc100SJani Nikula 
2126379bc100SJani Nikula 	if (cpu_transcoder != TRANSCODER_EDP)
2127379bc100SJani Nikula 		I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2128379bc100SJani Nikula 			   TRANS_CLK_SEL_PORT(port));
2129379bc100SJani Nikula }
2130379bc100SJani Nikula 
2131379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
2132379bc100SJani Nikula {
2133379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2134379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2135379bc100SJani Nikula 
2136379bc100SJani Nikula 	if (cpu_transcoder != TRANSCODER_EDP)
2137379bc100SJani Nikula 		I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2138379bc100SJani Nikula 			   TRANS_CLK_SEL_DISABLED);
2139379bc100SJani Nikula }
2140379bc100SJani Nikula 
2141379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
2142379bc100SJani Nikula 				enum port port, u8 iboost)
2143379bc100SJani Nikula {
2144379bc100SJani Nikula 	u32 tmp;
2145379bc100SJani Nikula 
2146379bc100SJani Nikula 	tmp = I915_READ(DISPIO_CR_TX_BMU_CR0);
2147379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
2148379bc100SJani Nikula 	if (iboost)
2149379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
2150379bc100SJani Nikula 	else
2151379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
2152379bc100SJani Nikula 	I915_WRITE(DISPIO_CR_TX_BMU_CR0, tmp);
2153379bc100SJani Nikula }
2154379bc100SJani Nikula 
2155379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
2156379bc100SJani Nikula 			       int level, enum intel_output_type type)
2157379bc100SJani Nikula {
2158379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
2159379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2160379bc100SJani Nikula 	enum port port = encoder->port;
2161379bc100SJani Nikula 	u8 iboost;
2162379bc100SJani Nikula 
2163379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2164379bc100SJani Nikula 		iboost = dev_priv->vbt.ddi_port_info[port].hdmi_boost_level;
2165379bc100SJani Nikula 	else
2166379bc100SJani Nikula 		iboost = dev_priv->vbt.ddi_port_info[port].dp_boost_level;
2167379bc100SJani Nikula 
2168379bc100SJani Nikula 	if (iboost == 0) {
2169379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations;
2170379bc100SJani Nikula 		int n_entries;
2171379bc100SJani Nikula 
2172379bc100SJani Nikula 		if (type == INTEL_OUTPUT_HDMI)
2173379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
2174379bc100SJani Nikula 		else if (type == INTEL_OUTPUT_EDP)
2175379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
2176379bc100SJani Nikula 		else
2177379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
2178379bc100SJani Nikula 
2179379bc100SJani Nikula 		if (WARN_ON_ONCE(!ddi_translations))
2180379bc100SJani Nikula 			return;
2181379bc100SJani Nikula 		if (WARN_ON_ONCE(level >= n_entries))
2182379bc100SJani Nikula 			level = n_entries - 1;
2183379bc100SJani Nikula 
2184379bc100SJani Nikula 		iboost = ddi_translations[level].i_boost;
2185379bc100SJani Nikula 	}
2186379bc100SJani Nikula 
2187379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
2188379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
2189379bc100SJani Nikula 		DRM_ERROR("Invalid I_boost value %u\n", iboost);
2190379bc100SJani Nikula 		return;
2191379bc100SJani Nikula 	}
2192379bc100SJani Nikula 
2193379bc100SJani Nikula 	_skl_ddi_set_iboost(dev_priv, port, iboost);
2194379bc100SJani Nikula 
2195379bc100SJani Nikula 	if (port == PORT_A && intel_dig_port->max_lanes == 4)
2196379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
2197379bc100SJani Nikula }
2198379bc100SJani Nikula 
2199379bc100SJani Nikula static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
2200379bc100SJani Nikula 				    int level, enum intel_output_type type)
2201379bc100SJani Nikula {
2202379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2203379bc100SJani Nikula 	const struct bxt_ddi_buf_trans *ddi_translations;
2204379bc100SJani Nikula 	enum port port = encoder->port;
2205379bc100SJani Nikula 	int n_entries;
2206379bc100SJani Nikula 
2207379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2208379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
2209379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2210379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_edp(dev_priv, &n_entries);
2211379bc100SJani Nikula 	else
2212379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_dp(dev_priv, &n_entries);
2213379bc100SJani Nikula 
2214379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
2215379bc100SJani Nikula 		return;
2216379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
2217379bc100SJani Nikula 		level = n_entries - 1;
2218379bc100SJani Nikula 
2219379bc100SJani Nikula 	bxt_ddi_phy_set_signal_level(dev_priv, port,
2220379bc100SJani Nikula 				     ddi_translations[level].margin,
2221379bc100SJani Nikula 				     ddi_translations[level].scale,
2222379bc100SJani Nikula 				     ddi_translations[level].enable,
2223379bc100SJani Nikula 				     ddi_translations[level].deemphasis);
2224379bc100SJani Nikula }
2225379bc100SJani Nikula 
2226379bc100SJani Nikula u8 intel_ddi_dp_voltage_max(struct intel_encoder *encoder)
2227379bc100SJani Nikula {
2228379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2229379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2230379bc100SJani Nikula 	enum port port = encoder->port;
2231379bc100SJani Nikula 	int n_entries;
2232379bc100SJani Nikula 
2233379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2234379bc100SJani Nikula 		if (intel_port_is_combophy(dev_priv, port))
2235379bc100SJani Nikula 			icl_get_combo_buf_trans(dev_priv, port, encoder->type,
2236379bc100SJani Nikula 						intel_dp->link_rate, &n_entries);
2237379bc100SJani Nikula 		else
2238379bc100SJani Nikula 			n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
2239379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2240379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2241379bc100SJani Nikula 			cnl_get_buf_trans_edp(dev_priv, &n_entries);
2242379bc100SJani Nikula 		else
2243379bc100SJani Nikula 			cnl_get_buf_trans_dp(dev_priv, &n_entries);
2244379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
2245379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2246379bc100SJani Nikula 			bxt_get_buf_trans_edp(dev_priv, &n_entries);
2247379bc100SJani Nikula 		else
2248379bc100SJani Nikula 			bxt_get_buf_trans_dp(dev_priv, &n_entries);
2249379bc100SJani Nikula 	} else {
2250379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2251379bc100SJani Nikula 			intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
2252379bc100SJani Nikula 		else
2253379bc100SJani Nikula 			intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
2254379bc100SJani Nikula 	}
2255379bc100SJani Nikula 
2256379bc100SJani Nikula 	if (WARN_ON(n_entries < 1))
2257379bc100SJani Nikula 		n_entries = 1;
2258379bc100SJani Nikula 	if (WARN_ON(n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
2259379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
2260379bc100SJani Nikula 
2261379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
2262379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
2263379bc100SJani Nikula }
2264379bc100SJani Nikula 
2265379bc100SJani Nikula /*
2266379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
2267379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
2268379bc100SJani Nikula  * rethink this code.
2269379bc100SJani Nikula  */
2270379bc100SJani Nikula u8 intel_ddi_dp_pre_emphasis_max(struct intel_encoder *encoder, u8 voltage_swing)
2271379bc100SJani Nikula {
2272379bc100SJani Nikula 	switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
2273379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2274379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_3;
2275379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2276379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_2;
2277379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2278379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_1;
2279379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
2280379bc100SJani Nikula 	default:
2281379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_0;
2282379bc100SJani Nikula 	}
2283379bc100SJani Nikula }
2284379bc100SJani Nikula 
2285379bc100SJani Nikula static void cnl_ddi_vswing_program(struct intel_encoder *encoder,
2286379bc100SJani Nikula 				   int level, enum intel_output_type type)
2287379bc100SJani Nikula {
2288379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2289379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations;
2290379bc100SJani Nikula 	enum port port = encoder->port;
2291379bc100SJani Nikula 	int n_entries, ln;
2292379bc100SJani Nikula 	u32 val;
2293379bc100SJani Nikula 
2294379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2295379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
2296379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2297379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_edp(dev_priv, &n_entries);
2298379bc100SJani Nikula 	else
2299379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_dp(dev_priv, &n_entries);
2300379bc100SJani Nikula 
2301379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
2302379bc100SJani Nikula 		return;
2303379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
2304379bc100SJani Nikula 		level = n_entries - 1;
2305379bc100SJani Nikula 
2306379bc100SJani Nikula 	/* Set PORT_TX_DW5 Scaling Mode Sel to 010b. */
2307379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2308379bc100SJani Nikula 	val &= ~SCALING_MODE_SEL_MASK;
2309379bc100SJani Nikula 	val |= SCALING_MODE_SEL(2);
2310379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2311379bc100SJani Nikula 
2312379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2313379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW2_LN0(port));
2314379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2315379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2316379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2317379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2318379bc100SJani Nikula 	/* Rcomp scalar is fixed as 0x98 for every table entry */
2319379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2320379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW2_GRP(port), val);
2321379bc100SJani Nikula 
2322379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2323379bc100SJani Nikula 	/* We cannot write to GRP. It would overrite individual loadgen */
2324379bc100SJani Nikula 	for (ln = 0; ln < 4; ln++) {
2325379bc100SJani Nikula 		val = I915_READ(CNL_PORT_TX_DW4_LN(ln, port));
2326379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2327379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2328379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2329379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2330379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2331379bc100SJani Nikula 		I915_WRITE(CNL_PORT_TX_DW4_LN(ln, port), val);
2332379bc100SJani Nikula 	}
2333379bc100SJani Nikula 
2334379bc100SJani Nikula 	/* Program PORT_TX_DW5 */
2335379bc100SJani Nikula 	/* All DW5 values are fixed for every table entry */
2336379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2337379bc100SJani Nikula 	val &= ~RTERM_SELECT_MASK;
2338379bc100SJani Nikula 	val |= RTERM_SELECT(6);
2339379bc100SJani Nikula 	val |= TAP3_DISABLE;
2340379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2341379bc100SJani Nikula 
2342379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2343379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW7_LN0(port));
2344379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2345379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2346379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW7_GRP(port), val);
2347379bc100SJani Nikula }
2348379bc100SJani Nikula 
2349379bc100SJani Nikula static void cnl_ddi_vswing_sequence(struct intel_encoder *encoder,
2350379bc100SJani Nikula 				    int level, enum intel_output_type type)
2351379bc100SJani Nikula {
2352379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2353379bc100SJani Nikula 	enum port port = encoder->port;
2354379bc100SJani Nikula 	int width, rate, ln;
2355379bc100SJani Nikula 	u32 val;
2356379bc100SJani Nikula 
2357379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2358379bc100SJani Nikula 		width = 4;
2359379bc100SJani Nikula 		rate = 0; /* Rate is always < than 6GHz for HDMI */
2360379bc100SJani Nikula 	} else {
2361379bc100SJani Nikula 		struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2362379bc100SJani Nikula 
2363379bc100SJani Nikula 		width = intel_dp->lane_count;
2364379bc100SJani Nikula 		rate = intel_dp->link_rate;
2365379bc100SJani Nikula 	}
2366379bc100SJani Nikula 
2367379bc100SJani Nikula 	/*
2368379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2369379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2370379bc100SJani Nikula 	 * else clear to 0b.
2371379bc100SJani Nikula 	 */
2372379bc100SJani Nikula 	val = I915_READ(CNL_PORT_PCS_DW1_LN0(port));
2373379bc100SJani Nikula 	if (type != INTEL_OUTPUT_HDMI)
2374379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2375379bc100SJani Nikula 	else
2376379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2377379bc100SJani Nikula 	I915_WRITE(CNL_PORT_PCS_DW1_GRP(port), val);
2378379bc100SJani Nikula 
2379379bc100SJani Nikula 	/* 2. Program loadgen select */
2380379bc100SJani Nikula 	/*
2381379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2382379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2383379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2384379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2385379bc100SJani Nikula 	 */
2386379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2387379bc100SJani Nikula 		val = I915_READ(CNL_PORT_TX_DW4_LN(ln, port));
2388379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2389379bc100SJani Nikula 
2390379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1)  ||
2391379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2392379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2393379bc100SJani Nikula 		}
2394379bc100SJani Nikula 		I915_WRITE(CNL_PORT_TX_DW4_LN(ln, port), val);
2395379bc100SJani Nikula 	}
2396379bc100SJani Nikula 
2397379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2398379bc100SJani Nikula 	val = I915_READ(CNL_PORT_CL1CM_DW5);
2399379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2400379bc100SJani Nikula 	I915_WRITE(CNL_PORT_CL1CM_DW5, val);
2401379bc100SJani Nikula 
2402379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2403379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2404379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2405379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2406379bc100SJani Nikula 
2407379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2408379bc100SJani Nikula 	cnl_ddi_vswing_program(encoder, level, type);
2409379bc100SJani Nikula 
2410379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2411379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2412379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2413379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2414379bc100SJani Nikula }
2415379bc100SJani Nikula 
2416379bc100SJani Nikula static void icl_ddi_combo_vswing_program(struct drm_i915_private *dev_priv,
2417379bc100SJani Nikula 					u32 level, enum port port, int type,
2418379bc100SJani Nikula 					int rate)
2419379bc100SJani Nikula {
2420379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations = NULL;
2421379bc100SJani Nikula 	u32 n_entries, val;
2422379bc100SJani Nikula 	int ln;
2423379bc100SJani Nikula 
2424379bc100SJani Nikula 	ddi_translations = icl_get_combo_buf_trans(dev_priv, port, type,
2425379bc100SJani Nikula 						   rate, &n_entries);
2426379bc100SJani Nikula 	if (!ddi_translations)
2427379bc100SJani Nikula 		return;
2428379bc100SJani Nikula 
2429379bc100SJani Nikula 	if (level >= n_entries) {
2430379bc100SJani Nikula 		DRM_DEBUG_KMS("DDI translation not found for level %d. Using %d instead.", level, n_entries - 1);
2431379bc100SJani Nikula 		level = n_entries - 1;
2432379bc100SJani Nikula 	}
2433379bc100SJani Nikula 
2434379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
2435379bc100SJani Nikula 	val = I915_READ(ICL_PORT_TX_DW5_LN0(port));
2436379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
2437379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
2438379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
2439379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
2440379bc100SJani Nikula 	val |= TAP3_DISABLE;
2441379bc100SJani Nikula 	I915_WRITE(ICL_PORT_TX_DW5_GRP(port), val);
2442379bc100SJani Nikula 
2443379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2444379bc100SJani Nikula 	val = I915_READ(ICL_PORT_TX_DW2_LN0(port));
2445379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2446379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2447379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2448379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2449379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
2450379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2451379bc100SJani Nikula 	I915_WRITE(ICL_PORT_TX_DW2_GRP(port), val);
2452379bc100SJani Nikula 
2453379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2454379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
2455379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2456379bc100SJani Nikula 		val = I915_READ(ICL_PORT_TX_DW4_LN(ln, port));
2457379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2458379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2459379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2460379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2461379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2462379bc100SJani Nikula 		I915_WRITE(ICL_PORT_TX_DW4_LN(ln, port), val);
2463379bc100SJani Nikula 	}
2464379bc100SJani Nikula 
2465379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2466379bc100SJani Nikula 	val = I915_READ(ICL_PORT_TX_DW7_LN0(port));
2467379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2468379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2469379bc100SJani Nikula 	I915_WRITE(ICL_PORT_TX_DW7_GRP(port), val);
2470379bc100SJani Nikula }
2471379bc100SJani Nikula 
2472379bc100SJani Nikula static void icl_combo_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2473379bc100SJani Nikula 					      u32 level,
2474379bc100SJani Nikula 					      enum intel_output_type type)
2475379bc100SJani Nikula {
2476379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2477379bc100SJani Nikula 	enum port port = encoder->port;
2478379bc100SJani Nikula 	int width = 0;
2479379bc100SJani Nikula 	int rate = 0;
2480379bc100SJani Nikula 	u32 val;
2481379bc100SJani Nikula 	int ln = 0;
2482379bc100SJani Nikula 
2483379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2484379bc100SJani Nikula 		width = 4;
2485379bc100SJani Nikula 		/* Rate is always < than 6GHz for HDMI */
2486379bc100SJani Nikula 	} else {
2487379bc100SJani Nikula 		struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2488379bc100SJani Nikula 
2489379bc100SJani Nikula 		width = intel_dp->lane_count;
2490379bc100SJani Nikula 		rate = intel_dp->link_rate;
2491379bc100SJani Nikula 	}
2492379bc100SJani Nikula 
2493379bc100SJani Nikula 	/*
2494379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2495379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2496379bc100SJani Nikula 	 * else clear to 0b.
2497379bc100SJani Nikula 	 */
2498379bc100SJani Nikula 	val = I915_READ(ICL_PORT_PCS_DW1_LN0(port));
2499379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2500379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2501379bc100SJani Nikula 	else
2502379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2503379bc100SJani Nikula 	I915_WRITE(ICL_PORT_PCS_DW1_GRP(port), val);
2504379bc100SJani Nikula 
2505379bc100SJani Nikula 	/* 2. Program loadgen select */
2506379bc100SJani Nikula 	/*
2507379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2508379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2509379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2510379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2511379bc100SJani Nikula 	 */
2512379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2513379bc100SJani Nikula 		val = I915_READ(ICL_PORT_TX_DW4_LN(ln, port));
2514379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2515379bc100SJani Nikula 
2516379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
2517379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2518379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2519379bc100SJani Nikula 		}
2520379bc100SJani Nikula 		I915_WRITE(ICL_PORT_TX_DW4_LN(ln, port), val);
2521379bc100SJani Nikula 	}
2522379bc100SJani Nikula 
2523379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2524379bc100SJani Nikula 	val = I915_READ(ICL_PORT_CL_DW5(port));
2525379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2526379bc100SJani Nikula 	I915_WRITE(ICL_PORT_CL_DW5(port), val);
2527379bc100SJani Nikula 
2528379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2529379bc100SJani Nikula 	val = I915_READ(ICL_PORT_TX_DW5_LN0(port));
2530379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2531379bc100SJani Nikula 	I915_WRITE(ICL_PORT_TX_DW5_GRP(port), val);
2532379bc100SJani Nikula 
2533379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2534379bc100SJani Nikula 	icl_ddi_combo_vswing_program(dev_priv, level, port, type, rate);
2535379bc100SJani Nikula 
2536379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2537379bc100SJani Nikula 	val = I915_READ(ICL_PORT_TX_DW5_LN0(port));
2538379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2539379bc100SJani Nikula 	I915_WRITE(ICL_PORT_TX_DW5_GRP(port), val);
2540379bc100SJani Nikula }
2541379bc100SJani Nikula 
2542379bc100SJani Nikula static void icl_mg_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2543379bc100SJani Nikula 					   int link_clock,
2544379bc100SJani Nikula 					   u32 level)
2545379bc100SJani Nikula {
2546379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2547379bc100SJani Nikula 	enum port port = encoder->port;
2548379bc100SJani Nikula 	const struct icl_mg_phy_ddi_buf_trans *ddi_translations;
2549379bc100SJani Nikula 	u32 n_entries, val;
2550379bc100SJani Nikula 	int ln;
2551379bc100SJani Nikula 
2552379bc100SJani Nikula 	n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
2553379bc100SJani Nikula 	ddi_translations = icl_mg_phy_ddi_translations;
2554379bc100SJani Nikula 	/* The table does not have values for level 3 and level 9. */
2555379bc100SJani Nikula 	if (level >= n_entries || level == 3 || level == 9) {
2556379bc100SJani Nikula 		DRM_DEBUG_KMS("DDI translation not found for level %d. Using %d instead.",
2557379bc100SJani Nikula 			      level, n_entries - 2);
2558379bc100SJani Nikula 		level = n_entries - 2;
2559379bc100SJani Nikula 	}
2560379bc100SJani Nikula 
2561379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
2562379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2563379bc100SJani Nikula 		val = I915_READ(MG_TX1_LINK_PARAMS(ln, port));
2564379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2565379bc100SJani Nikula 		I915_WRITE(MG_TX1_LINK_PARAMS(ln, port), val);
2566379bc100SJani Nikula 
2567379bc100SJani Nikula 		val = I915_READ(MG_TX2_LINK_PARAMS(ln, port));
2568379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2569379bc100SJani Nikula 		I915_WRITE(MG_TX2_LINK_PARAMS(ln, port), val);
2570379bc100SJani Nikula 	}
2571379bc100SJani Nikula 
2572379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
2573379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2574379bc100SJani Nikula 		val = I915_READ(MG_TX1_SWINGCTRL(ln, port));
2575379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2576379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2577379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2578379bc100SJani Nikula 		I915_WRITE(MG_TX1_SWINGCTRL(ln, port), val);
2579379bc100SJani Nikula 
2580379bc100SJani Nikula 		val = I915_READ(MG_TX2_SWINGCTRL(ln, port));
2581379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2582379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2583379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2584379bc100SJani Nikula 		I915_WRITE(MG_TX2_SWINGCTRL(ln, port), val);
2585379bc100SJani Nikula 	}
2586379bc100SJani Nikula 
2587379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
2588379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2589379bc100SJani Nikula 		val = I915_READ(MG_TX1_DRVCTRL(ln, port));
2590379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2591379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2592379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2593379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2594379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2595379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2596379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2597379bc100SJani Nikula 		I915_WRITE(MG_TX1_DRVCTRL(ln, port), val);
2598379bc100SJani Nikula 
2599379bc100SJani Nikula 		val = I915_READ(MG_TX2_DRVCTRL(ln, port));
2600379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2601379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2602379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2603379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2604379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2605379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2606379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2607379bc100SJani Nikula 		I915_WRITE(MG_TX2_DRVCTRL(ln, port), val);
2608379bc100SJani Nikula 
2609379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
2610379bc100SJani Nikula 	}
2611379bc100SJani Nikula 
2612379bc100SJani Nikula 	/*
2613379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
2614379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
2615379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
2616379bc100SJani Nikula 	 */
2617379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2618379bc100SJani Nikula 		val = I915_READ(MG_CLKHUB(ln, port));
2619379bc100SJani Nikula 		if (link_clock < 300000)
2620379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
2621379bc100SJani Nikula 		else
2622379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
2623379bc100SJani Nikula 		I915_WRITE(MG_CLKHUB(ln, port), val);
2624379bc100SJani Nikula 	}
2625379bc100SJani Nikula 
2626379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
2627379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2628379bc100SJani Nikula 		val = I915_READ(MG_TX1_DCC(ln, port));
2629379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2630379bc100SJani Nikula 		if (link_clock <= 500000) {
2631379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2632379bc100SJani Nikula 		} else {
2633379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2634379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2635379bc100SJani Nikula 		}
2636379bc100SJani Nikula 		I915_WRITE(MG_TX1_DCC(ln, port), val);
2637379bc100SJani Nikula 
2638379bc100SJani Nikula 		val = I915_READ(MG_TX2_DCC(ln, port));
2639379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2640379bc100SJani Nikula 		if (link_clock <= 500000) {
2641379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2642379bc100SJani Nikula 		} else {
2643379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2644379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2645379bc100SJani Nikula 		}
2646379bc100SJani Nikula 		I915_WRITE(MG_TX2_DCC(ln, port), val);
2647379bc100SJani Nikula 	}
2648379bc100SJani Nikula 
2649379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
2650379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2651379bc100SJani Nikula 		val = I915_READ(MG_TX1_PISO_READLOAD(ln, port));
2652379bc100SJani Nikula 		val |= CRI_CALCINIT;
2653379bc100SJani Nikula 		I915_WRITE(MG_TX1_PISO_READLOAD(ln, port), val);
2654379bc100SJani Nikula 
2655379bc100SJani Nikula 		val = I915_READ(MG_TX2_PISO_READLOAD(ln, port));
2656379bc100SJani Nikula 		val |= CRI_CALCINIT;
2657379bc100SJani Nikula 		I915_WRITE(MG_TX2_PISO_READLOAD(ln, port), val);
2658379bc100SJani Nikula 	}
2659379bc100SJani Nikula }
2660379bc100SJani Nikula 
2661379bc100SJani Nikula static void icl_ddi_vswing_sequence(struct intel_encoder *encoder,
2662379bc100SJani Nikula 				    int link_clock,
2663379bc100SJani Nikula 				    u32 level,
2664379bc100SJani Nikula 				    enum intel_output_type type)
2665379bc100SJani Nikula {
2666379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2667379bc100SJani Nikula 	enum port port = encoder->port;
2668379bc100SJani Nikula 
2669379bc100SJani Nikula 	if (intel_port_is_combophy(dev_priv, port))
2670379bc100SJani Nikula 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2671379bc100SJani Nikula 	else
2672379bc100SJani Nikula 		icl_mg_phy_ddi_vswing_sequence(encoder, link_clock, level);
2673379bc100SJani Nikula }
2674379bc100SJani Nikula 
2675379bc100SJani Nikula static u32 translate_signal_level(int signal_levels)
2676379bc100SJani Nikula {
2677379bc100SJani Nikula 	int i;
2678379bc100SJani Nikula 
2679379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
2680379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
2681379bc100SJani Nikula 			return i;
2682379bc100SJani Nikula 	}
2683379bc100SJani Nikula 
2684379bc100SJani Nikula 	WARN(1, "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
2685379bc100SJani Nikula 	     signal_levels);
2686379bc100SJani Nikula 
2687379bc100SJani Nikula 	return 0;
2688379bc100SJani Nikula }
2689379bc100SJani Nikula 
2690379bc100SJani Nikula static u32 intel_ddi_dp_level(struct intel_dp *intel_dp)
2691379bc100SJani Nikula {
2692379bc100SJani Nikula 	u8 train_set = intel_dp->train_set[0];
2693379bc100SJani Nikula 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2694379bc100SJani Nikula 					 DP_TRAIN_PRE_EMPHASIS_MASK);
2695379bc100SJani Nikula 
2696379bc100SJani Nikula 	return translate_signal_level(signal_levels);
2697379bc100SJani Nikula }
2698379bc100SJani Nikula 
2699379bc100SJani Nikula u32 bxt_signal_levels(struct intel_dp *intel_dp)
2700379bc100SJani Nikula {
2701379bc100SJani Nikula 	struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2702379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
2703379bc100SJani Nikula 	struct intel_encoder *encoder = &dport->base;
2704379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2705379bc100SJani Nikula 
2706379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
2707379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2708379bc100SJani Nikula 					level, encoder->type);
2709379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
2710379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
2711379bc100SJani Nikula 	else
2712379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
2713379bc100SJani Nikula 
2714379bc100SJani Nikula 	return 0;
2715379bc100SJani Nikula }
2716379bc100SJani Nikula 
2717379bc100SJani Nikula u32 ddi_signal_levels(struct intel_dp *intel_dp)
2718379bc100SJani Nikula {
2719379bc100SJani Nikula 	struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2720379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
2721379bc100SJani Nikula 	struct intel_encoder *encoder = &dport->base;
2722379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2723379bc100SJani Nikula 
2724379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
2725379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, encoder->type);
2726379bc100SJani Nikula 
2727379bc100SJani Nikula 	return DDI_BUF_TRANS_SELECT(level);
2728379bc100SJani Nikula }
2729379bc100SJani Nikula 
2730379bc100SJani Nikula static inline
2731379bc100SJani Nikula u32 icl_dpclka_cfgcr0_clk_off(struct drm_i915_private *dev_priv,
2732379bc100SJani Nikula 			      enum port port)
2733379bc100SJani Nikula {
2734379bc100SJani Nikula 	if (intel_port_is_combophy(dev_priv, port)) {
2735379bc100SJani Nikula 		return ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(port);
2736379bc100SJani Nikula 	} else if (intel_port_is_tc(dev_priv, port)) {
2737379bc100SJani Nikula 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
2738379bc100SJani Nikula 
2739379bc100SJani Nikula 		return ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port);
2740379bc100SJani Nikula 	}
2741379bc100SJani Nikula 
2742379bc100SJani Nikula 	return 0;
2743379bc100SJani Nikula }
2744379bc100SJani Nikula 
2745379bc100SJani Nikula static void icl_map_plls_to_ports(struct intel_encoder *encoder,
2746379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2747379bc100SJani Nikula {
2748379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2749379bc100SJani Nikula 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2750379bc100SJani Nikula 	enum port port = encoder->port;
2751379bc100SJani Nikula 	u32 val;
2752379bc100SJani Nikula 
2753379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2754379bc100SJani Nikula 
2755379bc100SJani Nikula 	val = I915_READ(DPCLKA_CFGCR0_ICL);
2756379bc100SJani Nikula 	WARN_ON((val & icl_dpclka_cfgcr0_clk_off(dev_priv, port)) == 0);
2757379bc100SJani Nikula 
2758379bc100SJani Nikula 	if (intel_port_is_combophy(dev_priv, port)) {
2759379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
2760379bc100SJani Nikula 		val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, port);
2761379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0_ICL, val);
2762379bc100SJani Nikula 		POSTING_READ(DPCLKA_CFGCR0_ICL);
2763379bc100SJani Nikula 	}
2764379bc100SJani Nikula 
2765379bc100SJani Nikula 	val &= ~icl_dpclka_cfgcr0_clk_off(dev_priv, port);
2766379bc100SJani Nikula 	I915_WRITE(DPCLKA_CFGCR0_ICL, val);
2767379bc100SJani Nikula 
2768379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
2769379bc100SJani Nikula }
2770379bc100SJani Nikula 
2771379bc100SJani Nikula static void icl_unmap_plls_to_ports(struct intel_encoder *encoder)
2772379bc100SJani Nikula {
2773379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2774379bc100SJani Nikula 	enum port port = encoder->port;
2775379bc100SJani Nikula 	u32 val;
2776379bc100SJani Nikula 
2777379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2778379bc100SJani Nikula 
2779379bc100SJani Nikula 	val = I915_READ(DPCLKA_CFGCR0_ICL);
2780379bc100SJani Nikula 	val |= icl_dpclka_cfgcr0_clk_off(dev_priv, port);
2781379bc100SJani Nikula 	I915_WRITE(DPCLKA_CFGCR0_ICL, val);
2782379bc100SJani Nikula 
2783379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
2784379bc100SJani Nikula }
2785379bc100SJani Nikula 
2786379bc100SJani Nikula void icl_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
2787379bc100SJani Nikula {
2788379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2789379bc100SJani Nikula 	u32 val;
2790379bc100SJani Nikula 	enum port port;
2791379bc100SJani Nikula 	u32 port_mask;
2792379bc100SJani Nikula 	bool ddi_clk_needed;
2793379bc100SJani Nikula 
2794379bc100SJani Nikula 	/*
2795379bc100SJani Nikula 	 * In case of DP MST, we sanitize the primary encoder only, not the
2796379bc100SJani Nikula 	 * virtual ones.
2797379bc100SJani Nikula 	 */
2798379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DP_MST)
2799379bc100SJani Nikula 		return;
2800379bc100SJani Nikula 
2801379bc100SJani Nikula 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2802379bc100SJani Nikula 		u8 pipe_mask;
2803379bc100SJani Nikula 		bool is_mst;
2804379bc100SJani Nikula 
2805379bc100SJani Nikula 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2806379bc100SJani Nikula 		/*
2807379bc100SJani Nikula 		 * In the unlikely case that BIOS enables DP in MST mode, just
2808379bc100SJani Nikula 		 * warn since our MST HW readout is incomplete.
2809379bc100SJani Nikula 		 */
2810379bc100SJani Nikula 		if (WARN_ON(is_mst))
2811379bc100SJani Nikula 			return;
2812379bc100SJani Nikula 	}
2813379bc100SJani Nikula 
2814379bc100SJani Nikula 	port_mask = BIT(encoder->port);
2815379bc100SJani Nikula 	ddi_clk_needed = encoder->base.crtc;
2816379bc100SJani Nikula 
2817379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DSI) {
2818379bc100SJani Nikula 		struct intel_encoder *other_encoder;
2819379bc100SJani Nikula 
2820379bc100SJani Nikula 		port_mask = intel_dsi_encoder_ports(encoder);
2821379bc100SJani Nikula 		/*
2822379bc100SJani Nikula 		 * Sanity check that we haven't incorrectly registered another
2823379bc100SJani Nikula 		 * encoder using any of the ports of this DSI encoder.
2824379bc100SJani Nikula 		 */
2825379bc100SJani Nikula 		for_each_intel_encoder(&dev_priv->drm, other_encoder) {
2826379bc100SJani Nikula 			if (other_encoder == encoder)
2827379bc100SJani Nikula 				continue;
2828379bc100SJani Nikula 
2829379bc100SJani Nikula 			if (WARN_ON(port_mask & BIT(other_encoder->port)))
2830379bc100SJani Nikula 				return;
2831379bc100SJani Nikula 		}
2832379bc100SJani Nikula 		/*
2833379bc100SJani Nikula 		 * For DSI we keep the ddi clocks gated
2834379bc100SJani Nikula 		 * except during enable/disable sequence.
2835379bc100SJani Nikula 		 */
2836379bc100SJani Nikula 		ddi_clk_needed = false;
2837379bc100SJani Nikula 	}
2838379bc100SJani Nikula 
2839379bc100SJani Nikula 	val = I915_READ(DPCLKA_CFGCR0_ICL);
2840379bc100SJani Nikula 	for_each_port_masked(port, port_mask) {
2841379bc100SJani Nikula 		bool ddi_clk_ungated = !(val &
2842379bc100SJani Nikula 					 icl_dpclka_cfgcr0_clk_off(dev_priv,
2843379bc100SJani Nikula 								   port));
2844379bc100SJani Nikula 
2845379bc100SJani Nikula 		if (ddi_clk_needed == ddi_clk_ungated)
2846379bc100SJani Nikula 			continue;
2847379bc100SJani Nikula 
2848379bc100SJani Nikula 		/*
2849379bc100SJani Nikula 		 * Punt on the case now where clock is gated, but it would
2850379bc100SJani Nikula 		 * be needed by the port. Something else is really broken then.
2851379bc100SJani Nikula 		 */
2852379bc100SJani Nikula 		if (WARN_ON(ddi_clk_needed))
2853379bc100SJani Nikula 			continue;
2854379bc100SJani Nikula 
2855379bc100SJani Nikula 		DRM_NOTE("Port %c is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2856379bc100SJani Nikula 			 port_name(port));
2857379bc100SJani Nikula 		val |= icl_dpclka_cfgcr0_clk_off(dev_priv, port);
2858379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0_ICL, val);
2859379bc100SJani Nikula 	}
2860379bc100SJani Nikula }
2861379bc100SJani Nikula 
2862379bc100SJani Nikula static void intel_ddi_clk_select(struct intel_encoder *encoder,
2863379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2864379bc100SJani Nikula {
2865379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2866379bc100SJani Nikula 	enum port port = encoder->port;
2867379bc100SJani Nikula 	u32 val;
2868379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2869379bc100SJani Nikula 
2870379bc100SJani Nikula 	if (WARN_ON(!pll))
2871379bc100SJani Nikula 		return;
2872379bc100SJani Nikula 
2873379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2874379bc100SJani Nikula 
2875379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2876379bc100SJani Nikula 		if (!intel_port_is_combophy(dev_priv, port))
2877379bc100SJani Nikula 			I915_WRITE(DDI_CLK_SEL(port),
2878379bc100SJani Nikula 				   icl_pll_to_ddi_clk_sel(encoder, crtc_state));
2879379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2880379bc100SJani Nikula 		/* Configure DPCLKA_CFGCR0 to map the DPLL to the DDI. */
2881379bc100SJani Nikula 		val = I915_READ(DPCLKA_CFGCR0);
2882379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
2883379bc100SJani Nikula 		val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, port);
2884379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, val);
2885379bc100SJani Nikula 
2886379bc100SJani Nikula 		/*
2887379bc100SJani Nikula 		 * Configure DPCLKA_CFGCR0 to turn on the clock for the DDI.
2888379bc100SJani Nikula 		 * This step and the step before must be done with separate
2889379bc100SJani Nikula 		 * register writes.
2890379bc100SJani Nikula 		 */
2891379bc100SJani Nikula 		val = I915_READ(DPCLKA_CFGCR0);
2892379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_OFF(port);
2893379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, val);
2894379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
2895379bc100SJani Nikula 		/* DDI -> PLL mapping  */
2896379bc100SJani Nikula 		val = I915_READ(DPLL_CTRL2);
2897379bc100SJani Nikula 
2898379bc100SJani Nikula 		val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
2899379bc100SJani Nikula 			 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
2900379bc100SJani Nikula 		val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
2901379bc100SJani Nikula 			DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
2902379bc100SJani Nikula 
2903379bc100SJani Nikula 		I915_WRITE(DPLL_CTRL2, val);
2904379bc100SJani Nikula 
2905379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
2906379bc100SJani Nikula 		I915_WRITE(PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
2907379bc100SJani Nikula 	}
2908379bc100SJani Nikula 
2909379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
2910379bc100SJani Nikula }
2911379bc100SJani Nikula 
2912379bc100SJani Nikula static void intel_ddi_clk_disable(struct intel_encoder *encoder)
2913379bc100SJani Nikula {
2914379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2915379bc100SJani Nikula 	enum port port = encoder->port;
2916379bc100SJani Nikula 
2917379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2918379bc100SJani Nikula 		if (!intel_port_is_combophy(dev_priv, port))
2919379bc100SJani Nikula 			I915_WRITE(DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
2920379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2921379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, I915_READ(DPCLKA_CFGCR0) |
2922379bc100SJani Nikula 			   DPCLKA_CFGCR0_DDI_CLK_OFF(port));
2923379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
2924379bc100SJani Nikula 		I915_WRITE(DPLL_CTRL2, I915_READ(DPLL_CTRL2) |
2925379bc100SJani Nikula 			   DPLL_CTRL2_DDI_CLK_OFF(port));
2926379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
2927379bc100SJani Nikula 		I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
2928379bc100SJani Nikula 	}
2929379bc100SJani Nikula }
2930379bc100SJani Nikula 
2931379bc100SJani Nikula static void icl_enable_phy_clock_gating(struct intel_digital_port *dig_port)
2932379bc100SJani Nikula {
2933379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2934379bc100SJani Nikula 	enum port port = dig_port->base.port;
2935379bc100SJani Nikula 	enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
2936379bc100SJani Nikula 	u32 val;
2937379bc100SJani Nikula 	int ln;
2938379bc100SJani Nikula 
2939379bc100SJani Nikula 	if (tc_port == PORT_TC_NONE)
2940379bc100SJani Nikula 		return;
2941379bc100SJani Nikula 
2942379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2943379bc100SJani Nikula 		val = I915_READ(MG_DP_MODE(ln, port));
2944379bc100SJani Nikula 		val |= MG_DP_MODE_CFG_TR2PWR_GATING |
2945379bc100SJani Nikula 		       MG_DP_MODE_CFG_TRPWR_GATING |
2946379bc100SJani Nikula 		       MG_DP_MODE_CFG_CLNPWR_GATING |
2947379bc100SJani Nikula 		       MG_DP_MODE_CFG_DIGPWR_GATING |
2948379bc100SJani Nikula 		       MG_DP_MODE_CFG_GAONPWR_GATING;
2949379bc100SJani Nikula 		I915_WRITE(MG_DP_MODE(ln, port), val);
2950379bc100SJani Nikula 	}
2951379bc100SJani Nikula 
2952379bc100SJani Nikula 	val = I915_READ(MG_MISC_SUS0(tc_port));
2953379bc100SJani Nikula 	val |= MG_MISC_SUS0_SUSCLK_DYNCLKGATE_MODE(3) |
2954379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_TR2PWR_GATING |
2955379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_CL2PWR_GATING |
2956379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_GAONPWR_GATING |
2957379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_TRPWR_GATING |
2958379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_CL1PWR_GATING |
2959379bc100SJani Nikula 	       MG_MISC_SUS0_CFG_DGPWR_GATING;
2960379bc100SJani Nikula 	I915_WRITE(MG_MISC_SUS0(tc_port), val);
2961379bc100SJani Nikula }
2962379bc100SJani Nikula 
2963379bc100SJani Nikula static void icl_disable_phy_clock_gating(struct intel_digital_port *dig_port)
2964379bc100SJani Nikula {
2965379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
2966379bc100SJani Nikula 	enum port port = dig_port->base.port;
2967379bc100SJani Nikula 	enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
2968379bc100SJani Nikula 	u32 val;
2969379bc100SJani Nikula 	int ln;
2970379bc100SJani Nikula 
2971379bc100SJani Nikula 	if (tc_port == PORT_TC_NONE)
2972379bc100SJani Nikula 		return;
2973379bc100SJani Nikula 
2974379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2975379bc100SJani Nikula 		val = I915_READ(MG_DP_MODE(ln, port));
2976379bc100SJani Nikula 		val &= ~(MG_DP_MODE_CFG_TR2PWR_GATING |
2977379bc100SJani Nikula 			 MG_DP_MODE_CFG_TRPWR_GATING |
2978379bc100SJani Nikula 			 MG_DP_MODE_CFG_CLNPWR_GATING |
2979379bc100SJani Nikula 			 MG_DP_MODE_CFG_DIGPWR_GATING |
2980379bc100SJani Nikula 			 MG_DP_MODE_CFG_GAONPWR_GATING);
2981379bc100SJani Nikula 		I915_WRITE(MG_DP_MODE(ln, port), val);
2982379bc100SJani Nikula 	}
2983379bc100SJani Nikula 
2984379bc100SJani Nikula 	val = I915_READ(MG_MISC_SUS0(tc_port));
2985379bc100SJani Nikula 	val &= ~(MG_MISC_SUS0_SUSCLK_DYNCLKGATE_MODE_MASK |
2986379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_TR2PWR_GATING |
2987379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_CL2PWR_GATING |
2988379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_GAONPWR_GATING |
2989379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_TRPWR_GATING |
2990379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_CL1PWR_GATING |
2991379bc100SJani Nikula 		 MG_MISC_SUS0_CFG_DGPWR_GATING);
2992379bc100SJani Nikula 	I915_WRITE(MG_MISC_SUS0(tc_port), val);
2993379bc100SJani Nikula }
2994379bc100SJani Nikula 
2995379bc100SJani Nikula static void icl_program_mg_dp_mode(struct intel_digital_port *intel_dig_port)
2996379bc100SJani Nikula {
2997379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
2998379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
2999c905eb28SImre Deak 	u32 ln0, ln1, lane_mask;
3000379bc100SJani Nikula 
3001e9b7e142SImre Deak 	if (intel_dig_port->tc_mode == TC_PORT_TBT_ALT)
3002379bc100SJani Nikula 		return;
3003379bc100SJani Nikula 
3004379bc100SJani Nikula 	ln0 = I915_READ(MG_DP_MODE(0, port));
3005379bc100SJani Nikula 	ln1 = I915_READ(MG_DP_MODE(1, port));
3006379bc100SJani Nikula 
3007e9b7e142SImre Deak 	switch (intel_dig_port->tc_mode) {
3008e9b7e142SImre Deak 	case TC_PORT_DP_ALT:
3009379bc100SJani Nikula 		ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3010379bc100SJani Nikula 		ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3011379bc100SJani Nikula 
3012c905eb28SImre Deak 		lane_mask = intel_tc_port_get_lane_mask(intel_dig_port);
3013379bc100SJani Nikula 
3014c905eb28SImre Deak 		switch (lane_mask) {
3015379bc100SJani Nikula 		case 0x1:
3016379bc100SJani Nikula 		case 0x4:
3017379bc100SJani Nikula 			break;
3018379bc100SJani Nikula 		case 0x2:
3019379bc100SJani Nikula 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
3020379bc100SJani Nikula 			break;
3021379bc100SJani Nikula 		case 0x3:
3022379bc100SJani Nikula 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE |
3023379bc100SJani Nikula 			       MG_DP_MODE_CFG_DP_X2_MODE;
3024379bc100SJani Nikula 			break;
3025379bc100SJani Nikula 		case 0x8:
3026379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
3027379bc100SJani Nikula 			break;
3028379bc100SJani Nikula 		case 0xC:
3029379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE |
3030379bc100SJani Nikula 			       MG_DP_MODE_CFG_DP_X2_MODE;
3031379bc100SJani Nikula 			break;
3032379bc100SJani Nikula 		case 0xF:
3033379bc100SJani Nikula 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE |
3034379bc100SJani Nikula 			       MG_DP_MODE_CFG_DP_X2_MODE;
3035379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE |
3036379bc100SJani Nikula 			       MG_DP_MODE_CFG_DP_X2_MODE;
3037379bc100SJani Nikula 			break;
3038379bc100SJani Nikula 		default:
3039c905eb28SImre Deak 			MISSING_CASE(lane_mask);
3040379bc100SJani Nikula 		}
3041379bc100SJani Nikula 		break;
3042379bc100SJani Nikula 
3043379bc100SJani Nikula 	case TC_PORT_LEGACY:
3044379bc100SJani Nikula 		ln0 |= MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE;
3045379bc100SJani Nikula 		ln1 |= MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE;
3046379bc100SJani Nikula 		break;
3047379bc100SJani Nikula 
3048379bc100SJani Nikula 	default:
3049e9b7e142SImre Deak 		MISSING_CASE(intel_dig_port->tc_mode);
3050379bc100SJani Nikula 		return;
3051379bc100SJani Nikula 	}
3052379bc100SJani Nikula 
3053379bc100SJani Nikula 	I915_WRITE(MG_DP_MODE(0, port), ln0);
3054379bc100SJani Nikula 	I915_WRITE(MG_DP_MODE(1, port), ln1);
3055379bc100SJani Nikula }
3056379bc100SJani Nikula 
3057379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
3058379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3059379bc100SJani Nikula {
3060379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3061379bc100SJani Nikula 		return;
3062379bc100SJani Nikula 
3063379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
3064379bc100SJani Nikula 		DRM_DEBUG_KMS("Failed to set FEC_READY in the sink\n");
3065379bc100SJani Nikula }
3066379bc100SJani Nikula 
3067379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
3068379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
3069379bc100SJani Nikula {
3070379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3071379bc100SJani Nikula 	enum port port = encoder->port;
3072379bc100SJani Nikula 	u32 val;
3073379bc100SJani Nikula 
3074379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3075379bc100SJani Nikula 		return;
3076379bc100SJani Nikula 
3077379bc100SJani Nikula 	val = I915_READ(DP_TP_CTL(port));
3078379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
3079379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(port), val);
3080379bc100SJani Nikula 
3081379bc100SJani Nikula 	if (intel_wait_for_register(&dev_priv->uncore, DP_TP_STATUS(port),
3082379bc100SJani Nikula 				    DP_TP_STATUS_FEC_ENABLE_LIVE,
3083379bc100SJani Nikula 				    DP_TP_STATUS_FEC_ENABLE_LIVE,
3084379bc100SJani Nikula 				    1))
3085379bc100SJani Nikula 		DRM_ERROR("Timed out waiting for FEC Enable Status\n");
3086379bc100SJani Nikula }
3087379bc100SJani Nikula 
3088379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
3089379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3090379bc100SJani Nikula {
3091379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3092379bc100SJani Nikula 	enum port port = encoder->port;
3093379bc100SJani Nikula 	u32 val;
3094379bc100SJani Nikula 
3095379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3096379bc100SJani Nikula 		return;
3097379bc100SJani Nikula 
3098379bc100SJani Nikula 	val = I915_READ(DP_TP_CTL(port));
3099379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
3100379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(port), val);
3101379bc100SJani Nikula 	POSTING_READ(DP_TP_CTL(port));
3102379bc100SJani Nikula }
3103379bc100SJani Nikula 
3104379bc100SJani Nikula static void intel_ddi_pre_enable_dp(struct intel_encoder *encoder,
3105379bc100SJani Nikula 				    const struct intel_crtc_state *crtc_state,
3106379bc100SJani Nikula 				    const struct drm_connector_state *conn_state)
3107379bc100SJani Nikula {
3108379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3109379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3110379bc100SJani Nikula 	enum port port = encoder->port;
3111379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3112379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3113379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
3114379bc100SJani Nikula 
3115379bc100SJani Nikula 	WARN_ON(is_mst && (port == PORT_A || port == PORT_E));
3116379bc100SJani Nikula 
3117379bc100SJani Nikula 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
3118379bc100SJani Nikula 				 crtc_state->lane_count, is_mst);
3119379bc100SJani Nikula 
3120379bc100SJani Nikula 	intel_edp_panel_on(intel_dp);
3121379bc100SJani Nikula 
3122379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3123379bc100SJani Nikula 
31243b2ed431SImre Deak 	if (!intel_port_is_tc(dev_priv, port) ||
31253b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
31263b2ed431SImre Deak 		intel_display_power_get(dev_priv,
31273b2ed431SImre Deak 					dig_port->ddi_io_power_domain);
3128379bc100SJani Nikula 
3129379bc100SJani Nikula 	icl_program_mg_dp_mode(dig_port);
3130379bc100SJani Nikula 	icl_disable_phy_clock_gating(dig_port);
3131379bc100SJani Nikula 
3132379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3133379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3134379bc100SJani Nikula 					level, encoder->type);
3135379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3136379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
3137379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3138379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
3139379bc100SJani Nikula 	else
3140379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
3141379bc100SJani Nikula 
3142379bc100SJani Nikula 	if (intel_port_is_combophy(dev_priv, port)) {
3143379bc100SJani Nikula 		bool lane_reversal =
3144379bc100SJani Nikula 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
3145379bc100SJani Nikula 
3146379bc100SJani Nikula 		intel_combo_phy_power_up_lanes(dev_priv, port, false,
3147379bc100SJani Nikula 					       crtc_state->lane_count,
3148379bc100SJani Nikula 					       lane_reversal);
3149379bc100SJani Nikula 	}
3150379bc100SJani Nikula 
3151379bc100SJani Nikula 	intel_ddi_init_dp_buf_reg(encoder);
3152379bc100SJani Nikula 	if (!is_mst)
3153379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
3154379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
3155379bc100SJani Nikula 					      true);
3156379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
3157379bc100SJani Nikula 	intel_dp_start_link_train(intel_dp);
3158379bc100SJani Nikula 	if (port != PORT_A || INTEL_GEN(dev_priv) >= 9)
3159379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3160379bc100SJani Nikula 
3161379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
3162379bc100SJani Nikula 
3163379bc100SJani Nikula 	icl_enable_phy_clock_gating(dig_port);
3164379bc100SJani Nikula 
3165379bc100SJani Nikula 	if (!is_mst)
3166379bc100SJani Nikula 		intel_ddi_enable_pipe_clock(crtc_state);
3167379bc100SJani Nikula 
3168379bc100SJani Nikula 	intel_dsc_enable(encoder, crtc_state);
3169379bc100SJani Nikula }
3170379bc100SJani Nikula 
3171379bc100SJani Nikula static void intel_ddi_pre_enable_hdmi(struct intel_encoder *encoder,
3172379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
3173379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
3174379bc100SJani Nikula {
3175379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
3176379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
3177379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3178379bc100SJani Nikula 	enum port port = encoder->port;
3179379bc100SJani Nikula 	int level = intel_ddi_hdmi_level(dev_priv, port);
3180379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3181379bc100SJani Nikula 
3182379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
3183379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3184379bc100SJani Nikula 
3185379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
3186379bc100SJani Nikula 
3187379bc100SJani Nikula 	icl_program_mg_dp_mode(dig_port);
3188379bc100SJani Nikula 	icl_disable_phy_clock_gating(dig_port);
3189379bc100SJani Nikula 
3190379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3191379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3192379bc100SJani Nikula 					level, INTEL_OUTPUT_HDMI);
3193379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3194379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3195379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3196379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3197379bc100SJani Nikula 	else
3198379bc100SJani Nikula 		intel_prepare_hdmi_ddi_buffers(encoder, level);
3199379bc100SJani Nikula 
3200379bc100SJani Nikula 	icl_enable_phy_clock_gating(dig_port);
3201379bc100SJani Nikula 
3202379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
3203379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, INTEL_OUTPUT_HDMI);
3204379bc100SJani Nikula 
3205379bc100SJani Nikula 	intel_ddi_enable_pipe_clock(crtc_state);
3206379bc100SJani Nikula 
3207379bc100SJani Nikula 	intel_dig_port->set_infoframes(encoder,
3208379bc100SJani Nikula 				       crtc_state->has_infoframe,
3209379bc100SJani Nikula 				       crtc_state, conn_state);
3210379bc100SJani Nikula }
3211379bc100SJani Nikula 
3212379bc100SJani Nikula static void intel_ddi_pre_enable(struct intel_encoder *encoder,
3213379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
3214379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
3215379bc100SJani Nikula {
3216379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3217379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3218379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
3219379bc100SJani Nikula 
3220379bc100SJani Nikula 	/*
3221379bc100SJani Nikula 	 * When called from DP MST code:
3222379bc100SJani Nikula 	 * - conn_state will be NULL
3223379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3224379bc100SJani Nikula 	 * - the main connector associated with this port
3225379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3226379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
3227379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
3228379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
3229379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3230379bc100SJani Nikula 	 *   the DP link parameteres
3231379bc100SJani Nikula 	 */
3232379bc100SJani Nikula 
3233379bc100SJani Nikula 	WARN_ON(crtc_state->has_pch_encoder);
3234379bc100SJani Nikula 
3235379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3236379bc100SJani Nikula 		icl_map_plls_to_ports(encoder, crtc_state);
3237379bc100SJani Nikula 
3238379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3239379bc100SJani Nikula 
3240379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
3241379bc100SJani Nikula 		intel_ddi_pre_enable_hdmi(encoder, crtc_state, conn_state);
3242379bc100SJani Nikula 	} else {
3243379bc100SJani Nikula 		struct intel_lspcon *lspcon =
3244379bc100SJani Nikula 				enc_to_intel_lspcon(&encoder->base);
3245379bc100SJani Nikula 
3246379bc100SJani Nikula 		intel_ddi_pre_enable_dp(encoder, crtc_state, conn_state);
3247379bc100SJani Nikula 		if (lspcon->active) {
3248379bc100SJani Nikula 			struct intel_digital_port *dig_port =
3249379bc100SJani Nikula 					enc_to_dig_port(&encoder->base);
3250379bc100SJani Nikula 
3251379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
3252379bc100SJani Nikula 						 crtc_state->has_infoframe,
3253379bc100SJani Nikula 						 crtc_state, conn_state);
3254379bc100SJani Nikula 		}
3255379bc100SJani Nikula 	}
3256379bc100SJani Nikula }
3257379bc100SJani Nikula 
3258379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
3259379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
3260379bc100SJani Nikula {
3261379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3262379bc100SJani Nikula 	enum port port = encoder->port;
3263379bc100SJani Nikula 	bool wait = false;
3264379bc100SJani Nikula 	u32 val;
3265379bc100SJani Nikula 
3266379bc100SJani Nikula 	val = I915_READ(DDI_BUF_CTL(port));
3267379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
3268379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
3269379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(port), val);
3270379bc100SJani Nikula 		wait = true;
3271379bc100SJani Nikula 	}
3272379bc100SJani Nikula 
3273379bc100SJani Nikula 	val = I915_READ(DP_TP_CTL(port));
3274379bc100SJani Nikula 	val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3275379bc100SJani Nikula 	val |= DP_TP_CTL_LINK_TRAIN_PAT1;
3276379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(port), val);
3277379bc100SJani Nikula 
3278379bc100SJani Nikula 	/* Disable FEC in DP Sink */
3279379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
3280379bc100SJani Nikula 
3281379bc100SJani Nikula 	if (wait)
3282379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
3283379bc100SJani Nikula }
3284379bc100SJani Nikula 
3285379bc100SJani Nikula static void intel_ddi_post_disable_dp(struct intel_encoder *encoder,
3286379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
3287379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
3288379bc100SJani Nikula {
3289379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3290379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3291379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
3292379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
3293379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
3294379bc100SJani Nikula 
3295379bc100SJani Nikula 	if (!is_mst) {
3296379bc100SJani Nikula 		intel_ddi_disable_pipe_clock(old_crtc_state);
3297379bc100SJani Nikula 		/*
3298379bc100SJani Nikula 		 * Power down sink before disabling the port, otherwise we end
3299379bc100SJani Nikula 		 * up getting interrupts from the sink on detecting link loss.
3300379bc100SJani Nikula 		 */
3301379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
3302379bc100SJani Nikula 	}
3303379bc100SJani Nikula 
3304379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3305379bc100SJani Nikula 
3306379bc100SJani Nikula 	intel_edp_panel_vdd_on(intel_dp);
3307379bc100SJani Nikula 	intel_edp_panel_off(intel_dp);
3308379bc100SJani Nikula 
33093b2ed431SImre Deak 	if (!intel_port_is_tc(dev_priv, encoder->port) ||
33103b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
3311379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3312379bc100SJani Nikula 						  dig_port->ddi_io_power_domain);
3313379bc100SJani Nikula 
3314379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3315379bc100SJani Nikula }
3316379bc100SJani Nikula 
3317379bc100SJani Nikula static void intel_ddi_post_disable_hdmi(struct intel_encoder *encoder,
3318379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
3319379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
3320379bc100SJani Nikula {
3321379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3322379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3323379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3324379bc100SJani Nikula 
3325379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
3326379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
3327379bc100SJani Nikula 
3328379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
3329379bc100SJani Nikula 
3330379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3331379bc100SJani Nikula 
3332379bc100SJani Nikula 	intel_display_power_put_unchecked(dev_priv,
3333379bc100SJani Nikula 					  dig_port->ddi_io_power_domain);
3334379bc100SJani Nikula 
3335379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3336379bc100SJani Nikula 
3337379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
3338379bc100SJani Nikula }
3339379bc100SJani Nikula 
3340379bc100SJani Nikula static void intel_ddi_post_disable(struct intel_encoder *encoder,
3341379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3342379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3343379bc100SJani Nikula {
3344379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3345379bc100SJani Nikula 
3346379bc100SJani Nikula 	/*
3347379bc100SJani Nikula 	 * When called from DP MST code:
3348379bc100SJani Nikula 	 * - old_conn_state will be NULL
3349379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3350379bc100SJani Nikula 	 * - the main connector associated with this port
3351379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3352379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
3353379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
3354379bc100SJani Nikula 	 *   stream that was activated last, but each stream
3355379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3356379bc100SJani Nikula 	 *   the DP link parameteres
3357379bc100SJani Nikula 	 */
3358379bc100SJani Nikula 
3359379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3360379bc100SJani Nikula 		intel_ddi_post_disable_hdmi(encoder,
3361379bc100SJani Nikula 					    old_crtc_state, old_conn_state);
3362379bc100SJani Nikula 	else
3363379bc100SJani Nikula 		intel_ddi_post_disable_dp(encoder,
3364379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3365379bc100SJani Nikula 
3366379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3367379bc100SJani Nikula 		icl_unmap_plls_to_ports(encoder);
3368379bc100SJani Nikula }
3369379bc100SJani Nikula 
3370379bc100SJani Nikula void intel_ddi_fdi_post_disable(struct intel_encoder *encoder,
3371379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
3372379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
3373379bc100SJani Nikula {
3374379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3375379bc100SJani Nikula 	u32 val;
3376379bc100SJani Nikula 
3377379bc100SJani Nikula 	/*
3378379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
3379379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
3380379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
3381379bc100SJani Nikula 	 * originally before the BUN.
3382379bc100SJani Nikula 	 */
3383379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3384379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
3385379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3386379bc100SJani Nikula 
3387379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3388379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3389379bc100SJani Nikula 
3390379bc100SJani Nikula 	val = I915_READ(FDI_RX_MISC(PIPE_A));
3391379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
3392379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
3393379bc100SJani Nikula 	I915_WRITE(FDI_RX_MISC(PIPE_A), val);
3394379bc100SJani Nikula 
3395379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3396379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
3397379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3398379bc100SJani Nikula 
3399379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3400379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
3401379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3402379bc100SJani Nikula }
3403379bc100SJani Nikula 
3404379bc100SJani Nikula static void intel_enable_ddi_dp(struct intel_encoder *encoder,
3405379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
3406379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
3407379bc100SJani Nikula {
3408379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3409379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3410379bc100SJani Nikula 	enum port port = encoder->port;
3411379bc100SJani Nikula 
3412379bc100SJani Nikula 	if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
3413379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3414379bc100SJani Nikula 
3415379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
3416379bc100SJani Nikula 	intel_psr_enable(intel_dp, crtc_state);
3417379bc100SJani Nikula 	intel_dp_ycbcr_420_enable(intel_dp, crtc_state);
3418379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3419379bc100SJani Nikula 
3420379bc100SJani Nikula 	if (crtc_state->has_audio)
3421379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3422379bc100SJani Nikula }
3423379bc100SJani Nikula 
3424379bc100SJani Nikula static i915_reg_t
3425379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3426379bc100SJani Nikula 			       enum port port)
3427379bc100SJani Nikula {
3428379bc100SJani Nikula 	static const i915_reg_t regs[] = {
3429379bc100SJani Nikula 		[PORT_A] = CHICKEN_TRANS_EDP,
3430379bc100SJani Nikula 		[PORT_B] = CHICKEN_TRANS_A,
3431379bc100SJani Nikula 		[PORT_C] = CHICKEN_TRANS_B,
3432379bc100SJani Nikula 		[PORT_D] = CHICKEN_TRANS_C,
3433379bc100SJani Nikula 		[PORT_E] = CHICKEN_TRANS_A,
3434379bc100SJani Nikula 	};
3435379bc100SJani Nikula 
3436379bc100SJani Nikula 	WARN_ON(INTEL_GEN(dev_priv) < 9);
3437379bc100SJani Nikula 
3438379bc100SJani Nikula 	if (WARN_ON(port < PORT_A || port > PORT_E))
3439379bc100SJani Nikula 		port = PORT_A;
3440379bc100SJani Nikula 
3441379bc100SJani Nikula 	return regs[port];
3442379bc100SJani Nikula }
3443379bc100SJani Nikula 
3444379bc100SJani Nikula static void intel_enable_ddi_hdmi(struct intel_encoder *encoder,
3445379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3446379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3447379bc100SJani Nikula {
3448379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3449379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3450379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
3451379bc100SJani Nikula 	enum port port = encoder->port;
3452379bc100SJani Nikula 
3453379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3454379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3455379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
3456379bc100SJani Nikula 		DRM_ERROR("[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3457379bc100SJani Nikula 			  connector->base.id, connector->name);
3458379bc100SJani Nikula 
3459379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
3460379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
3461379bc100SJani Nikula 		/*
3462379bc100SJani Nikula 		 * For some reason these chicken bits have been
3463379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3464379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3465379bc100SJani Nikula 		 * a specific transcoder.
3466379bc100SJani Nikula 		 */
3467379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3468379bc100SJani Nikula 		u32 val;
3469379bc100SJani Nikula 
3470379bc100SJani Nikula 		val = I915_READ(reg);
3471379bc100SJani Nikula 
3472379bc100SJani Nikula 		if (port == PORT_E)
3473379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3474379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3475379bc100SJani Nikula 		else
3476379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3477379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3478379bc100SJani Nikula 
3479379bc100SJani Nikula 		I915_WRITE(reg, val);
3480379bc100SJani Nikula 		POSTING_READ(reg);
3481379bc100SJani Nikula 
3482379bc100SJani Nikula 		udelay(1);
3483379bc100SJani Nikula 
3484379bc100SJani Nikula 		if (port == PORT_E)
3485379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3486379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3487379bc100SJani Nikula 		else
3488379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3489379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3490379bc100SJani Nikula 
3491379bc100SJani Nikula 		I915_WRITE(reg, val);
3492379bc100SJani Nikula 	}
3493379bc100SJani Nikula 
3494379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3495379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3496379bc100SJani Nikula 	 * enabling the port.
3497379bc100SJani Nikula 	 */
3498379bc100SJani Nikula 	I915_WRITE(DDI_BUF_CTL(port),
3499379bc100SJani Nikula 		   dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3500379bc100SJani Nikula 
3501379bc100SJani Nikula 	if (crtc_state->has_audio)
3502379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3503379bc100SJani Nikula }
3504379bc100SJani Nikula 
3505379bc100SJani Nikula static void intel_enable_ddi(struct intel_encoder *encoder,
3506379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3507379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3508379bc100SJani Nikula {
3509379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3510379bc100SJani Nikula 		intel_enable_ddi_hdmi(encoder, crtc_state, conn_state);
3511379bc100SJani Nikula 	else
3512379bc100SJani Nikula 		intel_enable_ddi_dp(encoder, crtc_state, conn_state);
3513379bc100SJani Nikula 
3514379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3515379bc100SJani Nikula 	if (conn_state->content_protection ==
3516379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3517379bc100SJani Nikula 		intel_hdcp_enable(to_intel_connector(conn_state->connector));
3518379bc100SJani Nikula }
3519379bc100SJani Nikula 
3520379bc100SJani Nikula static void intel_disable_ddi_dp(struct intel_encoder *encoder,
3521379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3522379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3523379bc100SJani Nikula {
3524379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3525379bc100SJani Nikula 
3526379bc100SJani Nikula 	intel_dp->link_trained = false;
3527379bc100SJani Nikula 
3528379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3529379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3530379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3531379bc100SJani Nikula 
3532379bc100SJani Nikula 	intel_edp_drrs_disable(intel_dp, old_crtc_state);
3533379bc100SJani Nikula 	intel_psr_disable(intel_dp, old_crtc_state);
3534379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3535379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3536379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3537379bc100SJani Nikula 					      false);
3538379bc100SJani Nikula }
3539379bc100SJani Nikula 
3540379bc100SJani Nikula static void intel_disable_ddi_hdmi(struct intel_encoder *encoder,
3541379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3542379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3543379bc100SJani Nikula {
3544379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3545379bc100SJani Nikula 
3546379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3547379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3548379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3549379bc100SJani Nikula 
3550379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3551379bc100SJani Nikula 					       false, false))
3552379bc100SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3553379bc100SJani Nikula 			      connector->base.id, connector->name);
3554379bc100SJani Nikula }
3555379bc100SJani Nikula 
3556379bc100SJani Nikula static void intel_disable_ddi(struct intel_encoder *encoder,
3557379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3558379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3559379bc100SJani Nikula {
3560379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3561379bc100SJani Nikula 
3562379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3563379bc100SJani Nikula 		intel_disable_ddi_hdmi(encoder, old_crtc_state, old_conn_state);
3564379bc100SJani Nikula 	else
3565379bc100SJani Nikula 		intel_disable_ddi_dp(encoder, old_crtc_state, old_conn_state);
3566379bc100SJani Nikula }
3567379bc100SJani Nikula 
3568379bc100SJani Nikula static void intel_ddi_update_pipe_dp(struct intel_encoder *encoder,
3569379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3570379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3571379bc100SJani Nikula {
3572379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3573379bc100SJani Nikula 
3574379bc100SJani Nikula 	intel_ddi_set_pipe_settings(crtc_state);
3575379bc100SJani Nikula 
3576379bc100SJani Nikula 	intel_psr_update(intel_dp, crtc_state);
3577379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3578379bc100SJani Nikula 
3579379bc100SJani Nikula 	intel_panel_update_backlight(encoder, crtc_state, conn_state);
3580379bc100SJani Nikula }
3581379bc100SJani Nikula 
3582379bc100SJani Nikula static void intel_ddi_update_pipe(struct intel_encoder *encoder,
3583379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3584379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3585379bc100SJani Nikula {
3586379bc100SJani Nikula 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3587379bc100SJani Nikula 		intel_ddi_update_pipe_dp(encoder, crtc_state, conn_state);
3588379bc100SJani Nikula 
3589379bc100SJani Nikula 	if (conn_state->content_protection ==
3590379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3591379bc100SJani Nikula 		intel_hdcp_enable(to_intel_connector(conn_state->connector));
3592379bc100SJani Nikula 	else if (conn_state->content_protection ==
3593379bc100SJani Nikula 		 DRM_MODE_CONTENT_PROTECTION_UNDESIRED)
3594379bc100SJani Nikula 		intel_hdcp_disable(to_intel_connector(conn_state->connector));
3595379bc100SJani Nikula }
3596379bc100SJani Nikula 
3597379bc100SJani Nikula static void intel_ddi_set_fia_lane_count(struct intel_encoder *encoder,
3598379bc100SJani Nikula 					 const struct intel_crtc_state *pipe_config,
3599379bc100SJani Nikula 					 enum port port)
3600379bc100SJani Nikula {
3601379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3602379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3603379bc100SJani Nikula 	enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
3604379bc100SJani Nikula 	u32 val = I915_READ(PORT_TX_DFLEXDPMLE1);
3605379bc100SJani Nikula 	bool lane_reversal = dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
3606379bc100SJani Nikula 
3607379bc100SJani Nikula 	val &= ~DFLEXDPMLE1_DPMLETC_MASK(tc_port);
3608379bc100SJani Nikula 	switch (pipe_config->lane_count) {
3609379bc100SJani Nikula 	case 1:
3610379bc100SJani Nikula 		val |= (lane_reversal) ? DFLEXDPMLE1_DPMLETC_ML3(tc_port) :
3611379bc100SJani Nikula 		DFLEXDPMLE1_DPMLETC_ML0(tc_port);
3612379bc100SJani Nikula 		break;
3613379bc100SJani Nikula 	case 2:
3614379bc100SJani Nikula 		val |= (lane_reversal) ? DFLEXDPMLE1_DPMLETC_ML3_2(tc_port) :
3615379bc100SJani Nikula 		DFLEXDPMLE1_DPMLETC_ML1_0(tc_port);
3616379bc100SJani Nikula 		break;
3617379bc100SJani Nikula 	case 4:
3618379bc100SJani Nikula 		val |= DFLEXDPMLE1_DPMLETC_ML3_0(tc_port);
3619379bc100SJani Nikula 		break;
3620379bc100SJani Nikula 	default:
3621379bc100SJani Nikula 		MISSING_CASE(pipe_config->lane_count);
3622379bc100SJani Nikula 	}
3623379bc100SJani Nikula 	I915_WRITE(PORT_TX_DFLEXDPMLE1, val);
3624379bc100SJani Nikula }
3625379bc100SJani Nikula 
3626379bc100SJani Nikula static void
3627379bc100SJani Nikula intel_ddi_pre_pll_enable(struct intel_encoder *encoder,
3628379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3629379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3630379bc100SJani Nikula {
3631379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3632379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3633379bc100SJani Nikula 	enum port port = encoder->port;
3634379bc100SJani Nikula 
3635379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
3636379bc100SJani Nikula 	    intel_port_is_tc(dev_priv, encoder->port))
3637379bc100SJani Nikula 		intel_display_power_get(dev_priv,
3638379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
3639379bc100SJani Nikula 
3640379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
3641379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3642379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3643379bc100SJani Nikula 
3644379bc100SJani Nikula 	/*
3645379bc100SJani Nikula 	 * Program the lane count for static/dynamic connections on Type-C ports.
3646379bc100SJani Nikula 	 * Skip this step for TBT.
3647379bc100SJani Nikula 	 */
3648e9b7e142SImre Deak 	if (dig_port->tc_mode == TC_PORT_TBT_ALT)
3649379bc100SJani Nikula 		return;
3650379bc100SJani Nikula 
3651379bc100SJani Nikula 	intel_ddi_set_fia_lane_count(encoder, crtc_state, port);
3652379bc100SJani Nikula }
3653379bc100SJani Nikula 
3654379bc100SJani Nikula static void
3655379bc100SJani Nikula intel_ddi_post_pll_disable(struct intel_encoder *encoder,
3656379bc100SJani Nikula 			   const struct intel_crtc_state *crtc_state,
3657379bc100SJani Nikula 			   const struct drm_connector_state *conn_state)
3658379bc100SJani Nikula {
3659379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3660379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3661379bc100SJani Nikula 
3662379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
3663379bc100SJani Nikula 	    intel_port_is_tc(dev_priv, encoder->port))
3664379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3665379bc100SJani Nikula 						  intel_ddi_main_link_aux_domain(dig_port));
3666379bc100SJani Nikula }
3667379bc100SJani Nikula 
3668379bc100SJani Nikula static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
3669379bc100SJani Nikula {
3670379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3671379bc100SJani Nikula 	struct drm_i915_private *dev_priv =
3672379bc100SJani Nikula 		to_i915(intel_dig_port->base.base.dev);
3673379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
3674379bc100SJani Nikula 	u32 val;
3675379bc100SJani Nikula 	bool wait = false;
3676379bc100SJani Nikula 
3677379bc100SJani Nikula 	if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
3678379bc100SJani Nikula 		val = I915_READ(DDI_BUF_CTL(port));
3679379bc100SJani Nikula 		if (val & DDI_BUF_CTL_ENABLE) {
3680379bc100SJani Nikula 			val &= ~DDI_BUF_CTL_ENABLE;
3681379bc100SJani Nikula 			I915_WRITE(DDI_BUF_CTL(port), val);
3682379bc100SJani Nikula 			wait = true;
3683379bc100SJani Nikula 		}
3684379bc100SJani Nikula 
3685379bc100SJani Nikula 		val = I915_READ(DP_TP_CTL(port));
3686379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3687379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
3688379bc100SJani Nikula 		I915_WRITE(DP_TP_CTL(port), val);
3689379bc100SJani Nikula 		POSTING_READ(DP_TP_CTL(port));
3690379bc100SJani Nikula 
3691379bc100SJani Nikula 		if (wait)
3692379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3693379bc100SJani Nikula 	}
3694379bc100SJani Nikula 
3695379bc100SJani Nikula 	val = DP_TP_CTL_ENABLE |
3696379bc100SJani Nikula 	      DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
3697379bc100SJani Nikula 	if (intel_dp->link_mst)
3698379bc100SJani Nikula 		val |= DP_TP_CTL_MODE_MST;
3699379bc100SJani Nikula 	else {
3700379bc100SJani Nikula 		val |= DP_TP_CTL_MODE_SST;
3701379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
3702379bc100SJani Nikula 			val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3703379bc100SJani Nikula 	}
3704379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(port), val);
3705379bc100SJani Nikula 	POSTING_READ(DP_TP_CTL(port));
3706379bc100SJani Nikula 
3707379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3708379bc100SJani Nikula 	I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
3709379bc100SJani Nikula 	POSTING_READ(DDI_BUF_CTL(port));
3710379bc100SJani Nikula 
3711379bc100SJani Nikula 	udelay(600);
3712379bc100SJani Nikula }
3713379bc100SJani Nikula 
3714379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3715379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3716379bc100SJani Nikula {
3717379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3718379bc100SJani Nikula 		return false;
3719379bc100SJani Nikula 
3720379bc100SJani Nikula 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO))
3721379bc100SJani Nikula 		return false;
3722379bc100SJani Nikula 
3723379bc100SJani Nikula 	return I915_READ(HSW_AUD_PIN_ELD_CP_VLD) &
3724379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3725379bc100SJani Nikula }
3726379bc100SJani Nikula 
3727379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3728379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3729379bc100SJani Nikula {
3730379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3731379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3732379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv) && crtc_state->port_clock > 594000)
3733379bc100SJani Nikula 		crtc_state->min_voltage_level = 2;
3734379bc100SJani Nikula }
3735379bc100SJani Nikula 
3736379bc100SJani Nikula void intel_ddi_get_config(struct intel_encoder *encoder,
3737379bc100SJani Nikula 			  struct intel_crtc_state *pipe_config)
3738379bc100SJani Nikula {
3739379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3740379bc100SJani Nikula 	struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
3741379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3742379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port;
3743379bc100SJani Nikula 	u32 temp, flags = 0;
3744379bc100SJani Nikula 
3745379bc100SJani Nikula 	/* XXX: DSI transcoder paranoia */
3746379bc100SJani Nikula 	if (WARN_ON(transcoder_is_dsi(cpu_transcoder)))
3747379bc100SJani Nikula 		return;
3748379bc100SJani Nikula 
3749379bc100SJani Nikula 	temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
3750379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3751379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3752379bc100SJani Nikula 	else
3753379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
3754379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
3755379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
3756379bc100SJani Nikula 	else
3757379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
3758379bc100SJani Nikula 
3759379bc100SJani Nikula 	pipe_config->base.adjusted_mode.flags |= flags;
3760379bc100SJani Nikula 
3761379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
3762379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
3763379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
3764379bc100SJani Nikula 		break;
3765379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
3766379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
3767379bc100SJani Nikula 		break;
3768379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
3769379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
3770379bc100SJani Nikula 		break;
3771379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
3772379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
3773379bc100SJani Nikula 		break;
3774379bc100SJani Nikula 	default:
3775379bc100SJani Nikula 		break;
3776379bc100SJani Nikula 	}
3777379bc100SJani Nikula 
3778379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3779379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
3780379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
3781379bc100SJani Nikula 		intel_dig_port = enc_to_dig_port(&encoder->base);
3782379bc100SJani Nikula 
3783379bc100SJani Nikula 		pipe_config->infoframes.enable |=
3784379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3785379bc100SJani Nikula 
3786379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
3787379bc100SJani Nikula 			pipe_config->has_infoframe = true;
3788379bc100SJani Nikula 
3789379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3790379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
3791379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3792379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
3793379bc100SJani Nikula 		/* fall through */
3794379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
3795379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3796379bc100SJani Nikula 		pipe_config->lane_count = 4;
3797379bc100SJani Nikula 		break;
3798379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
3799379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
3800379bc100SJani Nikula 		break;
3801379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
3802379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
3803379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3804379bc100SJani Nikula 		else
3805379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3806379bc100SJani Nikula 		pipe_config->lane_count =
3807379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3808379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
3809379bc100SJani Nikula 		break;
3810379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
3811379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3812379bc100SJani Nikula 		pipe_config->lane_count =
3813379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3814379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
3815379bc100SJani Nikula 		break;
3816379bc100SJani Nikula 	default:
3817379bc100SJani Nikula 		break;
3818379bc100SJani Nikula 	}
3819379bc100SJani Nikula 
3820379bc100SJani Nikula 	pipe_config->has_audio =
3821379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3822379bc100SJani Nikula 
3823379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
3824379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3825379bc100SJani Nikula 		/*
3826379bc100SJani Nikula 		 * This is a big fat ugly hack.
3827379bc100SJani Nikula 		 *
3828379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3829379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3830379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
3831379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3832379bc100SJani Nikula 		 * max, not what it tells us to use.
3833379bc100SJani Nikula 		 *
3834379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
3835379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
3836379bc100SJani Nikula 		 * load.
3837379bc100SJani Nikula 		 */
3838379bc100SJani Nikula 		DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3839379bc100SJani Nikula 			      pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3840379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3841379bc100SJani Nikula 	}
3842379bc100SJani Nikula 
3843379bc100SJani Nikula 	intel_ddi_clock_get(encoder, pipe_config);
3844379bc100SJani Nikula 
3845379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
3846379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3847379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3848379bc100SJani Nikula 
3849379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3850379bc100SJani Nikula 
3851379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3852379bc100SJani Nikula 
3853379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3854379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
3855379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
3856379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3857379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
3858379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
3859379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3860379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
3861379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
3862379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3863379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
3864379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
3865379bc100SJani Nikula }
3866379bc100SJani Nikula 
3867379bc100SJani Nikula static enum intel_output_type
3868379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
3869379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
3870379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
3871379bc100SJani Nikula {
3872379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
3873379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
3874379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
3875379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
3876379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
3877379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
3878379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
3879379bc100SJani Nikula 	default:
3880379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
3881379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
3882379bc100SJani Nikula 	}
3883379bc100SJani Nikula }
3884379bc100SJani Nikula 
3885379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
3886379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
3887379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
3888379bc100SJani Nikula {
3889379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
3890379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3891379bc100SJani Nikula 	enum port port = encoder->port;
3892379bc100SJani Nikula 	int ret;
3893379bc100SJani Nikula 
3894379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A)
3895379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
3896379bc100SJani Nikula 
3897379bc100SJani Nikula 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
3898379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3899379bc100SJani Nikula 	else
3900379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3901379bc100SJani Nikula 	if (ret)
3902379bc100SJani Nikula 		return ret;
3903379bc100SJani Nikula 
3904379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3905379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
3906379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
3907379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
3908379bc100SJani Nikula 			pipe_config->crc_enabled;
3909379bc100SJani Nikula 
3910379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
3911379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3912379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3913379bc100SJani Nikula 
3914379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3915379bc100SJani Nikula 
3916379bc100SJani Nikula 	return 0;
3917379bc100SJani Nikula }
3918379bc100SJani Nikula 
3919379bc100SJani Nikula static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
3920379bc100SJani Nikula {
3921379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3922379bc100SJani Nikula 
3923379bc100SJani Nikula 	intel_dp_encoder_suspend(encoder);
3924379bc100SJani Nikula 
3925379bc100SJani Nikula 	/*
3926379bc100SJani Nikula 	 * TODO: disconnect also from USB DP alternate mode once we have a
3927379bc100SJani Nikula 	 * way to handle the modeset restore in that mode during resume
3928379bc100SJani Nikula 	 * even if the sink has disappeared while being suspended.
3929379bc100SJani Nikula 	 */
3930379bc100SJani Nikula 	if (dig_port->tc_legacy_port)
3931bc85328fSImre Deak 		icl_tc_phy_disconnect(dig_port);
3932379bc100SJani Nikula }
3933379bc100SJani Nikula 
3934379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3935379bc100SJani Nikula {
3936379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3937379bc100SJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->dev);
3938379bc100SJani Nikula 
3939379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
3940379bc100SJani Nikula 
3941379bc100SJani Nikula 	if (intel_port_is_tc(i915, dig_port->base.port))
3942bc85328fSImre Deak 		icl_tc_phy_disconnect(dig_port);
3943379bc100SJani Nikula 
3944379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
3945379bc100SJani Nikula 	kfree(dig_port);
3946379bc100SJani Nikula }
3947379bc100SJani Nikula 
3948379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
3949*32691b58SImre Deak 	.reset = intel_dp_encoder_reset,
3950379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
3951379bc100SJani Nikula };
3952379bc100SJani Nikula 
3953379bc100SJani Nikula static struct intel_connector *
3954379bc100SJani Nikula intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
3955379bc100SJani Nikula {
3956379bc100SJani Nikula 	struct intel_connector *connector;
3957379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
3958379bc100SJani Nikula 
3959379bc100SJani Nikula 	connector = intel_connector_alloc();
3960379bc100SJani Nikula 	if (!connector)
3961379bc100SJani Nikula 		return NULL;
3962379bc100SJani Nikula 
3963379bc100SJani Nikula 	intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
3964379bc100SJani Nikula 	intel_dig_port->dp.prepare_link_retrain =
3965379bc100SJani Nikula 		intel_ddi_prepare_link_retrain;
3966379bc100SJani Nikula 
3967379bc100SJani Nikula 	if (!intel_dp_init_connector(intel_dig_port, connector)) {
3968379bc100SJani Nikula 		kfree(connector);
3969379bc100SJani Nikula 		return NULL;
3970379bc100SJani Nikula 	}
3971379bc100SJani Nikula 
3972379bc100SJani Nikula 	return connector;
3973379bc100SJani Nikula }
3974379bc100SJani Nikula 
3975379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
3976379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
3977379bc100SJani Nikula {
3978379bc100SJani Nikula 	struct drm_atomic_state *state;
3979379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
3980379bc100SJani Nikula 	int ret;
3981379bc100SJani Nikula 
3982379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
3983379bc100SJani Nikula 	if (!state)
3984379bc100SJani Nikula 		return -ENOMEM;
3985379bc100SJani Nikula 
3986379bc100SJani Nikula 	state->acquire_ctx = ctx;
3987379bc100SJani Nikula 
3988379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
3989379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
3990379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
3991379bc100SJani Nikula 		goto out;
3992379bc100SJani Nikula 	}
3993379bc100SJani Nikula 
3994379bc100SJani Nikula 	crtc_state->connectors_changed = true;
3995379bc100SJani Nikula 
3996379bc100SJani Nikula 	ret = drm_atomic_commit(state);
3997379bc100SJani Nikula out:
3998379bc100SJani Nikula 	drm_atomic_state_put(state);
3999379bc100SJani Nikula 
4000379bc100SJani Nikula 	return ret;
4001379bc100SJani Nikula }
4002379bc100SJani Nikula 
4003379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4004379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4005379bc100SJani Nikula {
4006379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4007379bc100SJani Nikula 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(&encoder->base);
4008379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4009379bc100SJani Nikula 	struct i2c_adapter *adapter =
4010379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4011379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4012379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4013379bc100SJani Nikula 	struct intel_crtc *crtc;
4014379bc100SJani Nikula 	u8 config;
4015379bc100SJani Nikula 	int ret;
4016379bc100SJani Nikula 
4017379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4018379bc100SJani Nikula 		return 0;
4019379bc100SJani Nikula 
4020379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4021379bc100SJani Nikula 			       ctx);
4022379bc100SJani Nikula 	if (ret)
4023379bc100SJani Nikula 		return ret;
4024379bc100SJani Nikula 
4025379bc100SJani Nikula 	conn_state = connector->base.state;
4026379bc100SJani Nikula 
4027379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4028379bc100SJani Nikula 	if (!crtc)
4029379bc100SJani Nikula 		return 0;
4030379bc100SJani Nikula 
4031379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4032379bc100SJani Nikula 	if (ret)
4033379bc100SJani Nikula 		return ret;
4034379bc100SJani Nikula 
4035379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4036379bc100SJani Nikula 
4037379bc100SJani Nikula 	WARN_ON(!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4038379bc100SJani Nikula 
4039379bc100SJani Nikula 	if (!crtc_state->base.active)
4040379bc100SJani Nikula 		return 0;
4041379bc100SJani Nikula 
4042379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4043379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4044379bc100SJani Nikula 		return 0;
4045379bc100SJani Nikula 
4046379bc100SJani Nikula 	if (conn_state->commit &&
4047379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4048379bc100SJani Nikula 		return 0;
4049379bc100SJani Nikula 
4050379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4051379bc100SJani Nikula 	if (ret < 0) {
4052379bc100SJani Nikula 		DRM_ERROR("Failed to read TMDS config: %d\n", ret);
4053379bc100SJani Nikula 		return 0;
4054379bc100SJani Nikula 	}
4055379bc100SJani Nikula 
4056379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4057379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4058379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4059379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4060379bc100SJani Nikula 		return 0;
4061379bc100SJani Nikula 
4062379bc100SJani Nikula 	/*
4063379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4064379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4065379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4066379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4067379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4068379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4069379bc100SJani Nikula 	 * the SCDC settings on the fly.
4070379bc100SJani Nikula 	 */
4071379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4072379bc100SJani Nikula }
4073379bc100SJani Nikula 
4074379bc100SJani Nikula static bool intel_ddi_hotplug(struct intel_encoder *encoder,
4075379bc100SJani Nikula 			      struct intel_connector *connector)
4076379bc100SJani Nikula {
4077379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
4078379bc100SJani Nikula 	bool changed;
4079379bc100SJani Nikula 	int ret;
4080379bc100SJani Nikula 
4081379bc100SJani Nikula 	changed = intel_encoder_hotplug(encoder, connector);
4082379bc100SJani Nikula 
4083379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4084379bc100SJani Nikula 
4085379bc100SJani Nikula 	for (;;) {
4086379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4087379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4088379bc100SJani Nikula 		else
4089379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4090379bc100SJani Nikula 
4091379bc100SJani Nikula 		if (ret == -EDEADLK) {
4092379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4093379bc100SJani Nikula 			continue;
4094379bc100SJani Nikula 		}
4095379bc100SJani Nikula 
4096379bc100SJani Nikula 		break;
4097379bc100SJani Nikula 	}
4098379bc100SJani Nikula 
4099379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4100379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
4101379bc100SJani Nikula 	WARN(ret, "Acquiring modeset locks failed with %i\n", ret);
4102379bc100SJani Nikula 
4103379bc100SJani Nikula 	return changed;
4104379bc100SJani Nikula }
4105379bc100SJani Nikula 
4106379bc100SJani Nikula static struct intel_connector *
4107379bc100SJani Nikula intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
4108379bc100SJani Nikula {
4109379bc100SJani Nikula 	struct intel_connector *connector;
4110379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
4111379bc100SJani Nikula 
4112379bc100SJani Nikula 	connector = intel_connector_alloc();
4113379bc100SJani Nikula 	if (!connector)
4114379bc100SJani Nikula 		return NULL;
4115379bc100SJani Nikula 
4116379bc100SJani Nikula 	intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
4117379bc100SJani Nikula 	intel_hdmi_init_connector(intel_dig_port, connector);
4118379bc100SJani Nikula 
4119379bc100SJani Nikula 	return connector;
4120379bc100SJani Nikula }
4121379bc100SJani Nikula 
4122379bc100SJani Nikula static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dport)
4123379bc100SJani Nikula {
4124379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
4125379bc100SJani Nikula 
4126379bc100SJani Nikula 	if (dport->base.port != PORT_A)
4127379bc100SJani Nikula 		return false;
4128379bc100SJani Nikula 
4129379bc100SJani Nikula 	if (dport->saved_port_bits & DDI_A_4_LANES)
4130379bc100SJani Nikula 		return false;
4131379bc100SJani Nikula 
4132379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4133379bc100SJani Nikula 	 *                     supported configuration
4134379bc100SJani Nikula 	 */
4135379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4136379bc100SJani Nikula 		return true;
4137379bc100SJani Nikula 
4138379bc100SJani Nikula 	/* Cannonlake: Most of SKUs don't support DDI_E, and the only
4139379bc100SJani Nikula 	 *             one who does also have a full A/E split called
4140379bc100SJani Nikula 	 *             DDI_F what makes DDI_E useless. However for this
4141379bc100SJani Nikula 	 *             case let's trust VBT info.
4142379bc100SJani Nikula 	 */
4143379bc100SJani Nikula 	if (IS_CANNONLAKE(dev_priv) &&
4144379bc100SJani Nikula 	    !intel_bios_is_port_present(dev_priv, PORT_E))
4145379bc100SJani Nikula 		return true;
4146379bc100SJani Nikula 
4147379bc100SJani Nikula 	return false;
4148379bc100SJani Nikula }
4149379bc100SJani Nikula 
4150379bc100SJani Nikula static int
4151379bc100SJani Nikula intel_ddi_max_lanes(struct intel_digital_port *intel_dport)
4152379bc100SJani Nikula {
4153379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_dport->base.base.dev);
4154379bc100SJani Nikula 	enum port port = intel_dport->base.port;
4155379bc100SJani Nikula 	int max_lanes = 4;
4156379bc100SJani Nikula 
4157379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4158379bc100SJani Nikula 		return max_lanes;
4159379bc100SJani Nikula 
4160379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4161379bc100SJani Nikula 		if (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4162379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4163379bc100SJani Nikula 		else
4164379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4165379bc100SJani Nikula 			max_lanes = 2;
4166379bc100SJani Nikula 	}
4167379bc100SJani Nikula 
4168379bc100SJani Nikula 	/*
4169379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4170379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4171379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4172379bc100SJani Nikula 	 */
4173379bc100SJani Nikula 	if (intel_ddi_a_force_4_lanes(intel_dport)) {
4174379bc100SJani Nikula 		DRM_DEBUG_KMS("Forcing DDI_A_4_LANES for port A\n");
4175379bc100SJani Nikula 		intel_dport->saved_port_bits |= DDI_A_4_LANES;
4176379bc100SJani Nikula 		max_lanes = 4;
4177379bc100SJani Nikula 	}
4178379bc100SJani Nikula 
4179379bc100SJani Nikula 	return max_lanes;
4180379bc100SJani Nikula }
4181379bc100SJani Nikula 
4182379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4183379bc100SJani Nikula {
4184379bc100SJani Nikula 	struct ddi_vbt_port_info *port_info =
4185379bc100SJani Nikula 		&dev_priv->vbt.ddi_port_info[port];
4186379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port;
4187379bc100SJani Nikula 	struct intel_encoder *intel_encoder;
4188379bc100SJani Nikula 	struct drm_encoder *encoder;
4189379bc100SJani Nikula 	bool init_hdmi, init_dp, init_lspcon = false;
4190379bc100SJani Nikula 	enum pipe pipe;
4191379bc100SJani Nikula 
4192379bc100SJani Nikula 	init_hdmi = port_info->supports_dvi || port_info->supports_hdmi;
4193379bc100SJani Nikula 	init_dp = port_info->supports_dp;
4194379bc100SJani Nikula 
4195379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4196379bc100SJani Nikula 		/*
4197379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4198379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4199379bc100SJani Nikula 		 * is initialized before lspcon.
4200379bc100SJani Nikula 		 */
4201379bc100SJani Nikula 		init_dp = true;
4202379bc100SJani Nikula 		init_lspcon = true;
4203379bc100SJani Nikula 		init_hdmi = false;
4204379bc100SJani Nikula 		DRM_DEBUG_KMS("VBT says port %c has lspcon\n", port_name(port));
4205379bc100SJani Nikula 	}
4206379bc100SJani Nikula 
4207379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
4208379bc100SJani Nikula 		DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4209379bc100SJani Nikula 			      port_name(port));
4210379bc100SJani Nikula 		return;
4211379bc100SJani Nikula 	}
4212379bc100SJani Nikula 
4213379bc100SJani Nikula 	intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
4214379bc100SJani Nikula 	if (!intel_dig_port)
4215379bc100SJani Nikula 		return;
4216379bc100SJani Nikula 
4217379bc100SJani Nikula 	intel_encoder = &intel_dig_port->base;
4218379bc100SJani Nikula 	encoder = &intel_encoder->base;
4219379bc100SJani Nikula 
4220379bc100SJani Nikula 	drm_encoder_init(&dev_priv->drm, encoder, &intel_ddi_funcs,
4221379bc100SJani Nikula 			 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
4222379bc100SJani Nikula 
4223379bc100SJani Nikula 	intel_encoder->hotplug = intel_ddi_hotplug;
4224379bc100SJani Nikula 	intel_encoder->compute_output_type = intel_ddi_compute_output_type;
4225379bc100SJani Nikula 	intel_encoder->compute_config = intel_ddi_compute_config;
4226379bc100SJani Nikula 	intel_encoder->enable = intel_enable_ddi;
4227379bc100SJani Nikula 	intel_encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
4228379bc100SJani Nikula 	intel_encoder->post_pll_disable = intel_ddi_post_pll_disable;
4229379bc100SJani Nikula 	intel_encoder->pre_enable = intel_ddi_pre_enable;
4230379bc100SJani Nikula 	intel_encoder->disable = intel_disable_ddi;
4231379bc100SJani Nikula 	intel_encoder->post_disable = intel_ddi_post_disable;
4232379bc100SJani Nikula 	intel_encoder->update_pipe = intel_ddi_update_pipe;
4233379bc100SJani Nikula 	intel_encoder->get_hw_state = intel_ddi_get_hw_state;
4234379bc100SJani Nikula 	intel_encoder->get_config = intel_ddi_get_config;
4235379bc100SJani Nikula 	intel_encoder->suspend = intel_ddi_encoder_suspend;
4236379bc100SJani Nikula 	intel_encoder->get_power_domains = intel_ddi_get_power_domains;
4237379bc100SJani Nikula 	intel_encoder->type = INTEL_OUTPUT_DDI;
4238379bc100SJani Nikula 	intel_encoder->power_domain = intel_port_to_power_domain(port);
4239379bc100SJani Nikula 	intel_encoder->port = port;
4240379bc100SJani Nikula 	intel_encoder->cloneable = 0;
4241379bc100SJani Nikula 	for_each_pipe(dev_priv, pipe)
4242379bc100SJani Nikula 		intel_encoder->crtc_mask |= BIT(pipe);
4243379bc100SJani Nikula 
4244379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4245379bc100SJani Nikula 		intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
4246379bc100SJani Nikula 			DDI_BUF_PORT_REVERSAL;
4247379bc100SJani Nikula 	else
4248379bc100SJani Nikula 		intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
4249379bc100SJani Nikula 			(DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
4250379bc100SJani Nikula 	intel_dig_port->dp.output_reg = INVALID_MMIO_REG;
4251379bc100SJani Nikula 	intel_dig_port->max_lanes = intel_ddi_max_lanes(intel_dig_port);
4252379bc100SJani Nikula 	intel_dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4253379bc100SJani Nikula 
4254ab7bc4e1SImre Deak 	if (intel_port_is_tc(dev_priv, port)) {
4255ab7bc4e1SImre Deak 		bool is_legacy = !port_info->supports_typec_usb &&
4256379bc100SJani Nikula 				 !port_info->supports_tbt;
4257379bc100SJani Nikula 
4258ab7bc4e1SImre Deak 		intel_tc_port_init(intel_dig_port, is_legacy);
4259ab7bc4e1SImre Deak 	}
4260ab7bc4e1SImre Deak 
4261379bc100SJani Nikula 	switch (port) {
4262379bc100SJani Nikula 	case PORT_A:
4263379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4264379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_A_IO;
4265379bc100SJani Nikula 		break;
4266379bc100SJani Nikula 	case PORT_B:
4267379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4268379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_B_IO;
4269379bc100SJani Nikula 		break;
4270379bc100SJani Nikula 	case PORT_C:
4271379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4272379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_C_IO;
4273379bc100SJani Nikula 		break;
4274379bc100SJani Nikula 	case PORT_D:
4275379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4276379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_D_IO;
4277379bc100SJani Nikula 		break;
4278379bc100SJani Nikula 	case PORT_E:
4279379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4280379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_E_IO;
4281379bc100SJani Nikula 		break;
4282379bc100SJani Nikula 	case PORT_F:
4283379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4284379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_F_IO;
4285379bc100SJani Nikula 		break;
4286379bc100SJani Nikula 	default:
4287379bc100SJani Nikula 		MISSING_CASE(port);
4288379bc100SJani Nikula 	}
4289379bc100SJani Nikula 
4290379bc100SJani Nikula 	if (init_dp) {
4291379bc100SJani Nikula 		if (!intel_ddi_init_dp_connector(intel_dig_port))
4292379bc100SJani Nikula 			goto err;
4293379bc100SJani Nikula 
4294379bc100SJani Nikula 		intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
4295379bc100SJani Nikula 	}
4296379bc100SJani Nikula 
4297379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4298379bc100SJani Nikula 	 * case we have some really bad VBTs... */
4299379bc100SJani Nikula 	if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
4300379bc100SJani Nikula 		if (!intel_ddi_init_hdmi_connector(intel_dig_port))
4301379bc100SJani Nikula 			goto err;
4302379bc100SJani Nikula 	}
4303379bc100SJani Nikula 
4304379bc100SJani Nikula 	if (init_lspcon) {
4305379bc100SJani Nikula 		if (lspcon_init(intel_dig_port))
4306379bc100SJani Nikula 			/* TODO: handle hdmi info frame part */
4307379bc100SJani Nikula 			DRM_DEBUG_KMS("LSPCON init success on port %c\n",
4308379bc100SJani Nikula 				port_name(port));
4309379bc100SJani Nikula 		else
4310379bc100SJani Nikula 			/*
4311379bc100SJani Nikula 			 * LSPCON init faied, but DP init was success, so
4312379bc100SJani Nikula 			 * lets try to drive as DP++ port.
4313379bc100SJani Nikula 			 */
4314379bc100SJani Nikula 			DRM_ERROR("LSPCON init failed on port %c\n",
4315379bc100SJani Nikula 				port_name(port));
4316379bc100SJani Nikula 	}
4317379bc100SJani Nikula 
4318379bc100SJani Nikula 	intel_infoframe_init(intel_dig_port);
4319379bc100SJani Nikula 
4320379bc100SJani Nikula 	return;
4321379bc100SJani Nikula 
4322379bc100SJani Nikula err:
4323379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
4324379bc100SJani Nikula 	kfree(intel_dig_port);
4325379bc100SJani Nikula }
4326