xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 3b51be4e4061bd5e0f1b73f56cfecaa879c76d51)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2012 Intel Corporation
3379bc100SJani Nikula  *
4379bc100SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5379bc100SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6379bc100SJani Nikula  * to deal in the Software without restriction, including without limitation
7379bc100SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8379bc100SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9379bc100SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10379bc100SJani Nikula  *
11379bc100SJani Nikula  * The above copyright notice and this permission notice (including the next
12379bc100SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13379bc100SJani Nikula  * Software.
14379bc100SJani Nikula  *
15379bc100SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16379bc100SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17379bc100SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18379bc100SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19379bc100SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20379bc100SJani Nikula  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21379bc100SJani Nikula  * IN THE SOFTWARE.
22379bc100SJani Nikula  *
23379bc100SJani Nikula  * Authors:
24379bc100SJani Nikula  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25379bc100SJani Nikula  *
26379bc100SJani Nikula  */
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_scdc_helper.h>
29379bc100SJani Nikula 
30379bc100SJani Nikula #include "i915_drv.h"
31379bc100SJani Nikula #include "intel_audio.h"
32379bc100SJani Nikula #include "intel_combo_phy.h"
33379bc100SJani Nikula #include "intel_connector.h"
34379bc100SJani Nikula #include "intel_ddi.h"
351d455f8dSJani Nikula #include "intel_display_types.h"
36379bc100SJani Nikula #include "intel_dp.h"
37379bc100SJani Nikula #include "intel_dp_link_training.h"
38379bc100SJani Nikula #include "intel_dpio_phy.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 *
8504a8134d5SMatt Roper icl_get_combo_buf_trans(struct drm_i915_private *dev_priv, int type, int rate,
8514a8134d5SMatt Roper 			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;
871d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
872379bc100SJani Nikula 
873379bc100SJani Nikula 	level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
874379bc100SJani Nikula 
875379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
876d8fe2ab6SMatt Roper 		if (intel_phy_is_combo(dev_priv, phy))
8774a8134d5SMatt Roper 			icl_get_combo_buf_trans(dev_priv, INTEL_OUTPUT_HDMI,
878379bc100SJani Nikula 						0, &n_entries);
879379bc100SJani Nikula 		else
880379bc100SJani Nikula 			n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
881379bc100SJani Nikula 		default_entry = n_entries - 1;
882379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
883379bc100SJani Nikula 		cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
884379bc100SJani Nikula 		default_entry = n_entries - 1;
885379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
886379bc100SJani Nikula 		bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
887379bc100SJani Nikula 		default_entry = n_entries - 1;
888379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
889379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
890379bc100SJani Nikula 		default_entry = 8;
891379bc100SJani Nikula 	} else if (IS_BROADWELL(dev_priv)) {
892379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
893379bc100SJani Nikula 		default_entry = 7;
894379bc100SJani Nikula 	} else if (IS_HASWELL(dev_priv)) {
895379bc100SJani Nikula 		intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
896379bc100SJani Nikula 		default_entry = 6;
897379bc100SJani Nikula 	} else {
898379bc100SJani Nikula 		WARN(1, "ddi translation table missing\n");
899379bc100SJani Nikula 		return 0;
900379bc100SJani Nikula 	}
901379bc100SJani Nikula 
902379bc100SJani Nikula 	/* Choose a good default if VBT is badly populated */
903379bc100SJani Nikula 	if (level == HDMI_LEVEL_SHIFT_UNKNOWN || level >= n_entries)
904379bc100SJani Nikula 		level = default_entry;
905379bc100SJani Nikula 
906379bc100SJani Nikula 	if (WARN_ON_ONCE(n_entries == 0))
907379bc100SJani Nikula 		return 0;
908379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
909379bc100SJani Nikula 		level = n_entries - 1;
910379bc100SJani Nikula 
911379bc100SJani Nikula 	return level;
912379bc100SJani Nikula }
913379bc100SJani Nikula 
914379bc100SJani Nikula /*
915379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
916379bc100SJani Nikula  * values in advance. This function programs the correct values for
917379bc100SJani Nikula  * DP/eDP/FDI use cases.
918379bc100SJani Nikula  */
919379bc100SJani Nikula static void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
920379bc100SJani Nikula 					 const struct intel_crtc_state *crtc_state)
921379bc100SJani Nikula {
922379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
923379bc100SJani Nikula 	u32 iboost_bit = 0;
924379bc100SJani Nikula 	int i, n_entries;
925379bc100SJani Nikula 	enum port port = encoder->port;
926379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
927379bc100SJani Nikula 
928379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
929379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
930379bc100SJani Nikula 							       &n_entries);
931379bc100SJani Nikula 	else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
932379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port,
933379bc100SJani Nikula 							       &n_entries);
934379bc100SJani Nikula 	else
935379bc100SJani Nikula 		ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port,
936379bc100SJani Nikula 							      &n_entries);
937379bc100SJani Nikula 
938379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
939379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv) &&
940379bc100SJani Nikula 	    dev_priv->vbt.ddi_port_info[port].dp_boost_level)
941379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
942379bc100SJani Nikula 
943379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
944379bc100SJani Nikula 		I915_WRITE(DDI_BUF_TRANS_LO(port, i),
945379bc100SJani Nikula 			   ddi_translations[i].trans1 | iboost_bit);
946379bc100SJani Nikula 		I915_WRITE(DDI_BUF_TRANS_HI(port, i),
947379bc100SJani Nikula 			   ddi_translations[i].trans2);
948379bc100SJani Nikula 	}
949379bc100SJani Nikula }
950379bc100SJani Nikula 
951379bc100SJani Nikula /*
952379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
953379bc100SJani Nikula  * values in advance. This function programs the correct values for
954379bc100SJani Nikula  * HDMI/DVI use cases.
955379bc100SJani Nikula  */
956379bc100SJani Nikula static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
957379bc100SJani Nikula 					   int level)
958379bc100SJani Nikula {
959379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
960379bc100SJani Nikula 	u32 iboost_bit = 0;
961379bc100SJani Nikula 	int n_entries;
962379bc100SJani Nikula 	enum port port = encoder->port;
963379bc100SJani Nikula 	const struct ddi_buf_trans *ddi_translations;
964379bc100SJani Nikula 
965379bc100SJani Nikula 	ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
966379bc100SJani Nikula 
967379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
968379bc100SJani Nikula 		return;
969379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
970379bc100SJani Nikula 		level = n_entries - 1;
971379bc100SJani Nikula 
972379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
973379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv) &&
974379bc100SJani Nikula 	    dev_priv->vbt.ddi_port_info[port].hdmi_boost_level)
975379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
976379bc100SJani Nikula 
977379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
978379bc100SJani Nikula 	I915_WRITE(DDI_BUF_TRANS_LO(port, 9),
979379bc100SJani Nikula 		   ddi_translations[level].trans1 | iboost_bit);
980379bc100SJani Nikula 	I915_WRITE(DDI_BUF_TRANS_HI(port, 9),
981379bc100SJani Nikula 		   ddi_translations[level].trans2);
982379bc100SJani Nikula }
983379bc100SJani Nikula 
984379bc100SJani Nikula static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
985379bc100SJani Nikula 				    enum port port)
986379bc100SJani Nikula {
987379bc100SJani Nikula 	i915_reg_t reg = DDI_BUF_CTL(port);
988379bc100SJani Nikula 	int i;
989379bc100SJani Nikula 
990379bc100SJani Nikula 	for (i = 0; i < 16; i++) {
991379bc100SJani Nikula 		udelay(1);
992379bc100SJani Nikula 		if (I915_READ(reg) & DDI_BUF_IS_IDLE)
993379bc100SJani Nikula 			return;
994379bc100SJani Nikula 	}
995379bc100SJani Nikula 	DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
996379bc100SJani Nikula }
997379bc100SJani Nikula 
998379bc100SJani Nikula static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
999379bc100SJani Nikula {
1000379bc100SJani Nikula 	switch (pll->info->id) {
1001379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
1002379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
1003379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
1004379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
1005379bc100SJani Nikula 	case DPLL_ID_SPLL:
1006379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
1007379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
1008379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
1009379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
1010379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
1011379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
1012379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
1013379bc100SJani Nikula 	default:
1014379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
1015379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
1016379bc100SJani Nikula 	}
1017379bc100SJani Nikula }
1018379bc100SJani Nikula 
1019379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
1020379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
1021379bc100SJani Nikula {
1022379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1023379bc100SJani Nikula 	int clock = crtc_state->port_clock;
1024379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
1025379bc100SJani Nikula 
1026379bc100SJani Nikula 	switch (id) {
1027379bc100SJani Nikula 	default:
1028379bc100SJani Nikula 		/*
1029379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
1030379bc100SJani Nikula 		 * here, so do warn if this get passed in
1031379bc100SJani Nikula 		 */
1032379bc100SJani Nikula 		MISSING_CASE(id);
1033379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
1034379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
1035379bc100SJani Nikula 		switch (clock) {
1036379bc100SJani Nikula 		case 162000:
1037379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
1038379bc100SJani Nikula 		case 270000:
1039379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
1040379bc100SJani Nikula 		case 540000:
1041379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
1042379bc100SJani Nikula 		case 810000:
1043379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
1044379bc100SJani Nikula 		default:
1045379bc100SJani Nikula 			MISSING_CASE(clock);
1046379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
1047379bc100SJani Nikula 		}
1048379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
1049379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
1050379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
1051379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
10526677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
10536677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
1054379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
1055379bc100SJani Nikula 	}
1056379bc100SJani Nikula }
1057379bc100SJani Nikula 
1058379bc100SJani Nikula /* Starting with Haswell, different DDI ports can work in FDI mode for
1059379bc100SJani Nikula  * connection to the PCH-located connectors. For this, it is necessary to train
1060379bc100SJani Nikula  * both the DDI port and PCH receiver for the desired DDI buffer settings.
1061379bc100SJani Nikula  *
1062379bc100SJani Nikula  * The recommended port to work in FDI mode is DDI E, which we use here. Also,
1063379bc100SJani Nikula  * please note that when FDI mode is active on DDI E, it shares 2 lines with
1064379bc100SJani Nikula  * DDI A (which is used for eDP)
1065379bc100SJani Nikula  */
1066379bc100SJani Nikula 
1067379bc100SJani Nikula void hsw_fdi_link_train(struct intel_crtc *crtc,
1068379bc100SJani Nikula 			const struct intel_crtc_state *crtc_state)
1069379bc100SJani Nikula {
1070379bc100SJani Nikula 	struct drm_device *dev = crtc->base.dev;
1071379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1072379bc100SJani Nikula 	struct intel_encoder *encoder;
1073379bc100SJani Nikula 	u32 temp, i, rx_ctl_val, ddi_pll_sel;
1074379bc100SJani Nikula 
1075379bc100SJani Nikula 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
1076379bc100SJani Nikula 		WARN_ON(encoder->type != INTEL_OUTPUT_ANALOG);
1077379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
1078379bc100SJani Nikula 	}
1079379bc100SJani Nikula 
1080379bc100SJani Nikula 	/* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
1081379bc100SJani Nikula 	 * mode set "sequence for CRT port" document:
1082379bc100SJani Nikula 	 * - TP1 to TP2 time with the default value
1083379bc100SJani Nikula 	 * - FDI delay to 90h
1084379bc100SJani Nikula 	 *
1085379bc100SJani Nikula 	 * WaFDIAutoLinkSetTimingOverrride:hsw
1086379bc100SJani Nikula 	 */
1087379bc100SJani Nikula 	I915_WRITE(FDI_RX_MISC(PIPE_A), FDI_RX_PWRDN_LANE1_VAL(2) |
1088379bc100SJani Nikula 				  FDI_RX_PWRDN_LANE0_VAL(2) |
1089379bc100SJani Nikula 				  FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
1090379bc100SJani Nikula 
1091379bc100SJani Nikula 	/* Enable the PCH Receiver FDI PLL */
1092379bc100SJani Nikula 	rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
1093379bc100SJani Nikula 		     FDI_RX_PLL_ENABLE |
1094379bc100SJani Nikula 		     FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
1095379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1096379bc100SJani Nikula 	POSTING_READ(FDI_RX_CTL(PIPE_A));
1097379bc100SJani Nikula 	udelay(220);
1098379bc100SJani Nikula 
1099379bc100SJani Nikula 	/* Switch from Rawclk to PCDclk */
1100379bc100SJani Nikula 	rx_ctl_val |= FDI_PCDCLK;
1101379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1102379bc100SJani Nikula 
1103379bc100SJani Nikula 	/* Configure Port Clock Select */
1104379bc100SJani Nikula 	ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
1105379bc100SJani Nikula 	I915_WRITE(PORT_CLK_SEL(PORT_E), ddi_pll_sel);
1106379bc100SJani Nikula 	WARN_ON(ddi_pll_sel != PORT_CLK_SEL_SPLL);
1107379bc100SJani Nikula 
1108379bc100SJani Nikula 	/* Start the training iterating through available voltages and emphasis,
1109379bc100SJani Nikula 	 * testing each value twice. */
1110379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
1111379bc100SJani Nikula 		/* Configure DP_TP_CTL with auto-training */
1112379bc100SJani Nikula 		I915_WRITE(DP_TP_CTL(PORT_E),
1113379bc100SJani Nikula 					DP_TP_CTL_FDI_AUTOTRAIN |
1114379bc100SJani Nikula 					DP_TP_CTL_ENHANCED_FRAME_ENABLE |
1115379bc100SJani Nikula 					DP_TP_CTL_LINK_TRAIN_PAT1 |
1116379bc100SJani Nikula 					DP_TP_CTL_ENABLE);
1117379bc100SJani Nikula 
1118379bc100SJani Nikula 		/* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
1119379bc100SJani Nikula 		 * DDI E does not support port reversal, the functionality is
1120379bc100SJani Nikula 		 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
1121379bc100SJani Nikula 		 * port reversal bit */
1122379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(PORT_E),
1123379bc100SJani Nikula 			   DDI_BUF_CTL_ENABLE |
1124379bc100SJani Nikula 			   ((crtc_state->fdi_lanes - 1) << 1) |
1125379bc100SJani Nikula 			   DDI_BUF_TRANS_SELECT(i / 2));
1126379bc100SJani Nikula 		POSTING_READ(DDI_BUF_CTL(PORT_E));
1127379bc100SJani Nikula 
1128379bc100SJani Nikula 		udelay(600);
1129379bc100SJani Nikula 
1130379bc100SJani Nikula 		/* Program PCH FDI Receiver TU */
1131379bc100SJani Nikula 		I915_WRITE(FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
1132379bc100SJani Nikula 
1133379bc100SJani Nikula 		/* Enable PCH FDI Receiver with auto-training */
1134379bc100SJani Nikula 		rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
1135379bc100SJani Nikula 		I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1136379bc100SJani Nikula 		POSTING_READ(FDI_RX_CTL(PIPE_A));
1137379bc100SJani Nikula 
1138379bc100SJani Nikula 		/* Wait for FDI receiver lane calibration */
1139379bc100SJani Nikula 		udelay(30);
1140379bc100SJani Nikula 
1141379bc100SJani Nikula 		/* Unset FDI_RX_MISC pwrdn lanes */
1142379bc100SJani Nikula 		temp = I915_READ(FDI_RX_MISC(PIPE_A));
1143379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1144379bc100SJani Nikula 		I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1145379bc100SJani Nikula 		POSTING_READ(FDI_RX_MISC(PIPE_A));
1146379bc100SJani Nikula 
1147379bc100SJani Nikula 		/* Wait for FDI auto training time */
1148379bc100SJani Nikula 		udelay(5);
1149379bc100SJani Nikula 
1150379bc100SJani Nikula 		temp = I915_READ(DP_TP_STATUS(PORT_E));
1151379bc100SJani Nikula 		if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
1152379bc100SJani Nikula 			DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
1153379bc100SJani Nikula 			break;
1154379bc100SJani Nikula 		}
1155379bc100SJani Nikula 
1156379bc100SJani Nikula 		/*
1157379bc100SJani Nikula 		 * Leave things enabled even if we failed to train FDI.
1158379bc100SJani Nikula 		 * Results in less fireworks from the state checker.
1159379bc100SJani Nikula 		 */
1160379bc100SJani Nikula 		if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
1161379bc100SJani Nikula 			DRM_ERROR("FDI link training failed!\n");
1162379bc100SJani Nikula 			break;
1163379bc100SJani Nikula 		}
1164379bc100SJani Nikula 
1165379bc100SJani Nikula 		rx_ctl_val &= ~FDI_RX_ENABLE;
1166379bc100SJani Nikula 		I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1167379bc100SJani Nikula 		POSTING_READ(FDI_RX_CTL(PIPE_A));
1168379bc100SJani Nikula 
1169379bc100SJani Nikula 		temp = I915_READ(DDI_BUF_CTL(PORT_E));
1170379bc100SJani Nikula 		temp &= ~DDI_BUF_CTL_ENABLE;
1171379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
1172379bc100SJani Nikula 		POSTING_READ(DDI_BUF_CTL(PORT_E));
1173379bc100SJani Nikula 
1174379bc100SJani Nikula 		/* Disable DP_TP_CTL and FDI_RX_CTL and retry */
1175379bc100SJani Nikula 		temp = I915_READ(DP_TP_CTL(PORT_E));
1176379bc100SJani Nikula 		temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1177379bc100SJani Nikula 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1178379bc100SJani Nikula 		I915_WRITE(DP_TP_CTL(PORT_E), temp);
1179379bc100SJani Nikula 		POSTING_READ(DP_TP_CTL(PORT_E));
1180379bc100SJani Nikula 
1181379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, PORT_E);
1182379bc100SJani Nikula 
1183379bc100SJani Nikula 		/* Reset FDI_RX_MISC pwrdn lanes */
1184379bc100SJani Nikula 		temp = I915_READ(FDI_RX_MISC(PIPE_A));
1185379bc100SJani Nikula 		temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1186379bc100SJani Nikula 		temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
1187379bc100SJani Nikula 		I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1188379bc100SJani Nikula 		POSTING_READ(FDI_RX_MISC(PIPE_A));
1189379bc100SJani Nikula 	}
1190379bc100SJani Nikula 
1191379bc100SJani Nikula 	/* Enable normal pixel sending for FDI */
1192379bc100SJani Nikula 	I915_WRITE(DP_TP_CTL(PORT_E),
1193379bc100SJani Nikula 		   DP_TP_CTL_FDI_AUTOTRAIN |
1194379bc100SJani Nikula 		   DP_TP_CTL_LINK_TRAIN_NORMAL |
1195379bc100SJani Nikula 		   DP_TP_CTL_ENHANCED_FRAME_ENABLE |
1196379bc100SJani Nikula 		   DP_TP_CTL_ENABLE);
1197379bc100SJani Nikula }
1198379bc100SJani Nikula 
1199379bc100SJani Nikula static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
1200379bc100SJani Nikula {
1201379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1202379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port =
1203379bc100SJani Nikula 		enc_to_dig_port(&encoder->base);
1204379bc100SJani Nikula 
1205379bc100SJani Nikula 	intel_dp->DP = intel_dig_port->saved_port_bits |
1206379bc100SJani Nikula 		DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
1207379bc100SJani Nikula 	intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
1208379bc100SJani Nikula }
1209379bc100SJani Nikula 
1210379bc100SJani Nikula static struct intel_encoder *
1211379bc100SJani Nikula intel_ddi_get_crtc_encoder(struct intel_crtc *crtc)
1212379bc100SJani Nikula {
1213379bc100SJani Nikula 	struct drm_device *dev = crtc->base.dev;
1214379bc100SJani Nikula 	struct intel_encoder *encoder, *ret = NULL;
1215379bc100SJani Nikula 	int num_encoders = 0;
1216379bc100SJani Nikula 
1217379bc100SJani Nikula 	for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
1218379bc100SJani Nikula 		ret = encoder;
1219379bc100SJani Nikula 		num_encoders++;
1220379bc100SJani Nikula 	}
1221379bc100SJani Nikula 
1222379bc100SJani Nikula 	if (num_encoders != 1)
1223379bc100SJani Nikula 		WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
1224379bc100SJani Nikula 		     pipe_name(crtc->pipe));
1225379bc100SJani Nikula 
1226379bc100SJani Nikula 	BUG_ON(ret == NULL);
1227379bc100SJani Nikula 	return ret;
1228379bc100SJani Nikula }
1229379bc100SJani Nikula 
1230379bc100SJani Nikula static int hsw_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
1231379bc100SJani Nikula 				   i915_reg_t reg)
1232379bc100SJani Nikula {
1233379bc100SJani Nikula 	int refclk;
1234379bc100SJani Nikula 	int n, p, r;
1235379bc100SJani Nikula 	u32 wrpll;
1236379bc100SJani Nikula 
1237379bc100SJani Nikula 	wrpll = I915_READ(reg);
1238379bc100SJani Nikula 	switch (wrpll & WRPLL_REF_MASK) {
1239379bc100SJani Nikula 	case WRPLL_REF_SPECIAL_HSW:
1240379bc100SJani Nikula 		/*
1241379bc100SJani Nikula 		 * muxed-SSC for BDW.
1242379bc100SJani Nikula 		 * non-SSC for non-ULT HSW. Check FUSE_STRAP3
1243379bc100SJani Nikula 		 * for the non-SSC reference frequency.
1244379bc100SJani Nikula 		 */
1245379bc100SJani Nikula 		if (IS_HASWELL(dev_priv) && !IS_HSW_ULT(dev_priv)) {
1246379bc100SJani Nikula 			if (I915_READ(FUSE_STRAP3) & HSW_REF_CLK_SELECT)
1247379bc100SJani Nikula 				refclk = 24;
1248379bc100SJani Nikula 			else
1249379bc100SJani Nikula 				refclk = 135;
1250379bc100SJani Nikula 			break;
1251379bc100SJani Nikula 		}
1252379bc100SJani Nikula 		/* fall through */
1253379bc100SJani Nikula 	case WRPLL_REF_PCH_SSC:
1254379bc100SJani Nikula 		/*
1255379bc100SJani Nikula 		 * We could calculate spread here, but our checking
1256379bc100SJani Nikula 		 * code only cares about 5% accuracy, and spread is a max of
1257379bc100SJani Nikula 		 * 0.5% downspread.
1258379bc100SJani Nikula 		 */
1259379bc100SJani Nikula 		refclk = 135;
1260379bc100SJani Nikula 		break;
1261379bc100SJani Nikula 	case WRPLL_REF_LCPLL:
1262379bc100SJani Nikula 		refclk = 2700;
1263379bc100SJani Nikula 		break;
1264379bc100SJani Nikula 	default:
1265379bc100SJani Nikula 		MISSING_CASE(wrpll);
1266379bc100SJani Nikula 		return 0;
1267379bc100SJani Nikula 	}
1268379bc100SJani Nikula 
1269379bc100SJani Nikula 	r = wrpll & WRPLL_DIVIDER_REF_MASK;
1270379bc100SJani Nikula 	p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
1271379bc100SJani Nikula 	n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
1272379bc100SJani Nikula 
1273379bc100SJani Nikula 	/* Convert to KHz, p & r have a fixed point portion */
1274379bc100SJani Nikula 	return (refclk * n * 100) / (p * r);
1275379bc100SJani Nikula }
1276379bc100SJani Nikula 
1277379bc100SJani Nikula static int skl_calc_wrpll_link(const struct intel_dpll_hw_state *pll_state)
1278379bc100SJani Nikula {
1279379bc100SJani Nikula 	u32 p0, p1, p2, dco_freq;
1280379bc100SJani Nikula 
1281379bc100SJani Nikula 	p0 = pll_state->cfgcr2 & DPLL_CFGCR2_PDIV_MASK;
1282379bc100SJani Nikula 	p2 = pll_state->cfgcr2 & DPLL_CFGCR2_KDIV_MASK;
1283379bc100SJani Nikula 
1284379bc100SJani Nikula 	if (pll_state->cfgcr2 &  DPLL_CFGCR2_QDIV_MODE(1))
1285379bc100SJani Nikula 		p1 = (pll_state->cfgcr2 & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
1286379bc100SJani Nikula 	else
1287379bc100SJani Nikula 		p1 = 1;
1288379bc100SJani Nikula 
1289379bc100SJani Nikula 
1290379bc100SJani Nikula 	switch (p0) {
1291379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_1:
1292379bc100SJani Nikula 		p0 = 1;
1293379bc100SJani Nikula 		break;
1294379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_2:
1295379bc100SJani Nikula 		p0 = 2;
1296379bc100SJani Nikula 		break;
1297379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_3:
1298379bc100SJani Nikula 		p0 = 3;
1299379bc100SJani Nikula 		break;
1300379bc100SJani Nikula 	case DPLL_CFGCR2_PDIV_7:
1301379bc100SJani Nikula 		p0 = 7;
1302379bc100SJani Nikula 		break;
1303379bc100SJani Nikula 	}
1304379bc100SJani Nikula 
1305379bc100SJani Nikula 	switch (p2) {
1306379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_5:
1307379bc100SJani Nikula 		p2 = 5;
1308379bc100SJani Nikula 		break;
1309379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_2:
1310379bc100SJani Nikula 		p2 = 2;
1311379bc100SJani Nikula 		break;
1312379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_3:
1313379bc100SJani Nikula 		p2 = 3;
1314379bc100SJani Nikula 		break;
1315379bc100SJani Nikula 	case DPLL_CFGCR2_KDIV_1:
1316379bc100SJani Nikula 		p2 = 1;
1317379bc100SJani Nikula 		break;
1318379bc100SJani Nikula 	}
1319379bc100SJani Nikula 
1320379bc100SJani Nikula 	dco_freq = (pll_state->cfgcr1 & DPLL_CFGCR1_DCO_INTEGER_MASK)
1321379bc100SJani Nikula 		* 24 * 1000;
1322379bc100SJani Nikula 
1323379bc100SJani Nikula 	dco_freq += (((pll_state->cfgcr1 & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9)
1324379bc100SJani Nikula 		     * 24 * 1000) / 0x8000;
1325379bc100SJani Nikula 
1326379bc100SJani Nikula 	if (WARN_ON(p0 == 0 || p1 == 0 || p2 == 0))
1327379bc100SJani Nikula 		return 0;
1328379bc100SJani Nikula 
1329379bc100SJani Nikula 	return dco_freq / (p0 * p1 * p2 * 5);
1330379bc100SJani Nikula }
1331379bc100SJani Nikula 
1332379bc100SJani Nikula int cnl_calc_wrpll_link(struct drm_i915_private *dev_priv,
1333379bc100SJani Nikula 			struct intel_dpll_hw_state *pll_state)
1334379bc100SJani Nikula {
1335379bc100SJani Nikula 	u32 p0, p1, p2, dco_freq, ref_clock;
1336379bc100SJani Nikula 
1337379bc100SJani Nikula 	p0 = pll_state->cfgcr1 & DPLL_CFGCR1_PDIV_MASK;
1338379bc100SJani Nikula 	p2 = pll_state->cfgcr1 & DPLL_CFGCR1_KDIV_MASK;
1339379bc100SJani Nikula 
1340379bc100SJani Nikula 	if (pll_state->cfgcr1 & DPLL_CFGCR1_QDIV_MODE(1))
1341379bc100SJani Nikula 		p1 = (pll_state->cfgcr1 & DPLL_CFGCR1_QDIV_RATIO_MASK) >>
1342379bc100SJani Nikula 			DPLL_CFGCR1_QDIV_RATIO_SHIFT;
1343379bc100SJani Nikula 	else
1344379bc100SJani Nikula 		p1 = 1;
1345379bc100SJani Nikula 
1346379bc100SJani Nikula 
1347379bc100SJani Nikula 	switch (p0) {
1348379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_2:
1349379bc100SJani Nikula 		p0 = 2;
1350379bc100SJani Nikula 		break;
1351379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_3:
1352379bc100SJani Nikula 		p0 = 3;
1353379bc100SJani Nikula 		break;
1354379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_5:
1355379bc100SJani Nikula 		p0 = 5;
1356379bc100SJani Nikula 		break;
1357379bc100SJani Nikula 	case DPLL_CFGCR1_PDIV_7:
1358379bc100SJani Nikula 		p0 = 7;
1359379bc100SJani Nikula 		break;
1360379bc100SJani Nikula 	}
1361379bc100SJani Nikula 
1362379bc100SJani Nikula 	switch (p2) {
1363379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_1:
1364379bc100SJani Nikula 		p2 = 1;
1365379bc100SJani Nikula 		break;
1366379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_2:
1367379bc100SJani Nikula 		p2 = 2;
1368379bc100SJani Nikula 		break;
1369379bc100SJani Nikula 	case DPLL_CFGCR1_KDIV_3:
1370379bc100SJani Nikula 		p2 = 3;
1371379bc100SJani Nikula 		break;
1372379bc100SJani Nikula 	}
1373379bc100SJani Nikula 
1374379bc100SJani Nikula 	ref_clock = cnl_hdmi_pll_ref_clock(dev_priv);
1375379bc100SJani Nikula 
1376379bc100SJani Nikula 	dco_freq = (pll_state->cfgcr0 & DPLL_CFGCR0_DCO_INTEGER_MASK)
1377379bc100SJani Nikula 		* ref_clock;
1378379bc100SJani Nikula 
1379379bc100SJani Nikula 	dco_freq += (((pll_state->cfgcr0 & DPLL_CFGCR0_DCO_FRACTION_MASK) >>
1380379bc100SJani Nikula 		      DPLL_CFGCR0_DCO_FRACTION_SHIFT) * ref_clock) / 0x8000;
1381379bc100SJani Nikula 
1382379bc100SJani Nikula 	if (WARN_ON(p0 == 0 || p1 == 0 || p2 == 0))
1383379bc100SJani Nikula 		return 0;
1384379bc100SJani Nikula 
1385379bc100SJani Nikula 	return dco_freq / (p0 * p1 * p2 * 5);
1386379bc100SJani Nikula }
1387379bc100SJani Nikula 
1388379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
1389379bc100SJani Nikula 				 enum port port)
1390379bc100SJani Nikula {
1391379bc100SJani Nikula 	u32 val = I915_READ(DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
1392379bc100SJani Nikula 
1393379bc100SJani Nikula 	switch (val) {
1394379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
1395379bc100SJani Nikula 		return 0;
1396379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
1397379bc100SJani Nikula 		return 162000;
1398379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
1399379bc100SJani Nikula 		return 270000;
1400379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
1401379bc100SJani Nikula 		return 540000;
1402379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
1403379bc100SJani Nikula 		return 810000;
1404379bc100SJani Nikula 	default:
1405379bc100SJani Nikula 		MISSING_CASE(val);
1406379bc100SJani Nikula 		return 0;
1407379bc100SJani Nikula 	}
1408379bc100SJani Nikula }
1409379bc100SJani Nikula 
1410379bc100SJani Nikula static int icl_calc_mg_pll_link(struct drm_i915_private *dev_priv,
1411379bc100SJani Nikula 				const struct intel_dpll_hw_state *pll_state)
1412379bc100SJani Nikula {
1413379bc100SJani Nikula 	u32 m1, m2_int, m2_frac, div1, div2, ref_clock;
1414379bc100SJani Nikula 	u64 tmp;
1415379bc100SJani Nikula 
1416379bc100SJani Nikula 	ref_clock = dev_priv->cdclk.hw.ref;
1417379bc100SJani Nikula 
1418ee7de6adSJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12) {
1419ee7de6adSJosé Roberto de Souza 		m1 = pll_state->mg_pll_div0 & DKL_PLL_DIV0_FBPREDIV_MASK;
1420ee7de6adSJosé Roberto de Souza 		m1 = m1 >> DKL_PLL_DIV0_FBPREDIV_SHIFT;
1421ee7de6adSJosé Roberto de Souza 		m2_int = pll_state->mg_pll_div0 & DKL_PLL_DIV0_FBDIV_INT_MASK;
1422ee7de6adSJosé Roberto de Souza 
1423ee7de6adSJosé Roberto de Souza 		if (pll_state->mg_pll_bias & DKL_PLL_BIAS_FRAC_EN_H) {
1424ee7de6adSJosé Roberto de Souza 			m2_frac = pll_state->mg_pll_bias &
1425ee7de6adSJosé Roberto de Souza 				  DKL_PLL_BIAS_FBDIV_FRAC_MASK;
1426ee7de6adSJosé Roberto de Souza 			m2_frac = m2_frac >> DKL_PLL_BIAS_FBDIV_SHIFT;
1427ee7de6adSJosé Roberto de Souza 		} else {
1428ee7de6adSJosé Roberto de Souza 			m2_frac = 0;
1429ee7de6adSJosé Roberto de Souza 		}
1430ee7de6adSJosé Roberto de Souza 	} else {
1431379bc100SJani Nikula 		m1 = pll_state->mg_pll_div1 & MG_PLL_DIV1_FBPREDIV_MASK;
1432379bc100SJani Nikula 		m2_int = pll_state->mg_pll_div0 & MG_PLL_DIV0_FBDIV_INT_MASK;
1433ee7de6adSJosé Roberto de Souza 
1434ee7de6adSJosé Roberto de Souza 		if (pll_state->mg_pll_div0 & MG_PLL_DIV0_FRACNEN_H) {
1435ee7de6adSJosé Roberto de Souza 			m2_frac = pll_state->mg_pll_div0 &
1436ee7de6adSJosé Roberto de Souza 				  MG_PLL_DIV0_FBDIV_FRAC_MASK;
1437ee7de6adSJosé Roberto de Souza 			m2_frac = m2_frac >> MG_PLL_DIV0_FBDIV_FRAC_SHIFT;
1438ee7de6adSJosé Roberto de Souza 		} else {
1439ee7de6adSJosé Roberto de Souza 			m2_frac = 0;
1440ee7de6adSJosé Roberto de Souza 		}
1441ee7de6adSJosé Roberto de Souza 	}
1442379bc100SJani Nikula 
1443379bc100SJani Nikula 	switch (pll_state->mg_clktop2_hsclkctl &
1444379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_MASK) {
1445379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_2:
1446379bc100SJani Nikula 		div1 = 2;
1447379bc100SJani Nikula 		break;
1448379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_3:
1449379bc100SJani Nikula 		div1 = 3;
1450379bc100SJani Nikula 		break;
1451379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_5:
1452379bc100SJani Nikula 		div1 = 5;
1453379bc100SJani Nikula 		break;
1454379bc100SJani Nikula 	case MG_CLKTOP2_HSCLKCTL_HSDIV_RATIO_7:
1455379bc100SJani Nikula 		div1 = 7;
1456379bc100SJani Nikula 		break;
1457379bc100SJani Nikula 	default:
1458379bc100SJani Nikula 		MISSING_CASE(pll_state->mg_clktop2_hsclkctl);
1459379bc100SJani Nikula 		return 0;
1460379bc100SJani Nikula 	}
1461379bc100SJani Nikula 
1462379bc100SJani Nikula 	div2 = (pll_state->mg_clktop2_hsclkctl &
1463379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_MASK) >>
1464379bc100SJani Nikula 		MG_CLKTOP2_HSCLKCTL_DSDIV_RATIO_SHIFT;
1465379bc100SJani Nikula 
1466379bc100SJani Nikula 	/* div2 value of 0 is same as 1 means no div */
1467379bc100SJani Nikula 	if (div2 == 0)
1468379bc100SJani Nikula 		div2 = 1;
1469379bc100SJani Nikula 
1470379bc100SJani Nikula 	/*
1471379bc100SJani Nikula 	 * Adjust the original formula to delay the division by 2^22 in order to
1472379bc100SJani Nikula 	 * minimize possible rounding errors.
1473379bc100SJani Nikula 	 */
1474379bc100SJani Nikula 	tmp = (u64)m1 * m2_int * ref_clock +
1475379bc100SJani Nikula 	      (((u64)m1 * m2_frac * ref_clock) >> 22);
1476379bc100SJani Nikula 	tmp = div_u64(tmp, 5 * div1 * div2);
1477379bc100SJani Nikula 
1478379bc100SJani Nikula 	return tmp;
1479379bc100SJani Nikula }
1480379bc100SJani Nikula 
1481379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
1482379bc100SJani Nikula {
1483379bc100SJani Nikula 	int dotclock;
1484379bc100SJani Nikula 
1485379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
1486379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1487379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
1488379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
1489379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1490379bc100SJani Nikula 						    &pipe_config->dp_m_n);
14912969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
14922969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
1493379bc100SJani Nikula 	else
1494379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
1495379bc100SJani Nikula 
1496379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
1497379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
1498379bc100SJani Nikula 		dotclock *= 2;
1499379bc100SJani Nikula 
1500379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
1501379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
1502379bc100SJani Nikula 
1503379bc100SJani Nikula 	pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1504379bc100SJani Nikula }
1505379bc100SJani Nikula 
1506379bc100SJani Nikula static void icl_ddi_clock_get(struct intel_encoder *encoder,
1507379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1508379bc100SJani Nikula {
1509379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1510379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1511379bc100SJani Nikula 	enum port port = encoder->port;
1512d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
1513379bc100SJani Nikula 	int link_clock;
1514379bc100SJani Nikula 
1515d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
1516379bc100SJani Nikula 		link_clock = cnl_calc_wrpll_link(dev_priv, pll_state);
1517379bc100SJani Nikula 	} else {
1518379bc100SJani Nikula 		enum intel_dpll_id pll_id = intel_get_shared_dpll_id(dev_priv,
1519379bc100SJani Nikula 						pipe_config->shared_dpll);
1520379bc100SJani Nikula 
1521379bc100SJani Nikula 		if (pll_id == DPLL_ID_ICL_TBTPLL)
1522379bc100SJani Nikula 			link_clock = icl_calc_tbt_pll_link(dev_priv, port);
1523379bc100SJani Nikula 		else
1524379bc100SJani Nikula 			link_clock = icl_calc_mg_pll_link(dev_priv, pll_state);
1525379bc100SJani Nikula 	}
1526379bc100SJani Nikula 
1527379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1528379bc100SJani Nikula 
1529379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1530379bc100SJani Nikula }
1531379bc100SJani Nikula 
1532379bc100SJani Nikula static void cnl_ddi_clock_get(struct intel_encoder *encoder,
1533379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1534379bc100SJani Nikula {
1535379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1536379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1537379bc100SJani Nikula 	int link_clock;
1538379bc100SJani Nikula 
1539379bc100SJani Nikula 	if (pll_state->cfgcr0 & DPLL_CFGCR0_HDMI_MODE) {
1540379bc100SJani Nikula 		link_clock = cnl_calc_wrpll_link(dev_priv, pll_state);
1541379bc100SJani Nikula 	} else {
1542379bc100SJani Nikula 		link_clock = pll_state->cfgcr0 & DPLL_CFGCR0_LINK_RATE_MASK;
1543379bc100SJani Nikula 
1544379bc100SJani Nikula 		switch (link_clock) {
1545379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_810:
1546379bc100SJani Nikula 			link_clock = 81000;
1547379bc100SJani Nikula 			break;
1548379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1080:
1549379bc100SJani Nikula 			link_clock = 108000;
1550379bc100SJani Nikula 			break;
1551379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1350:
1552379bc100SJani Nikula 			link_clock = 135000;
1553379bc100SJani Nikula 			break;
1554379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_1620:
1555379bc100SJani Nikula 			link_clock = 162000;
1556379bc100SJani Nikula 			break;
1557379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_2160:
1558379bc100SJani Nikula 			link_clock = 216000;
1559379bc100SJani Nikula 			break;
1560379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_2700:
1561379bc100SJani Nikula 			link_clock = 270000;
1562379bc100SJani Nikula 			break;
1563379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_3240:
1564379bc100SJani Nikula 			link_clock = 324000;
1565379bc100SJani Nikula 			break;
1566379bc100SJani Nikula 		case DPLL_CFGCR0_LINK_RATE_4050:
1567379bc100SJani Nikula 			link_clock = 405000;
1568379bc100SJani Nikula 			break;
1569379bc100SJani Nikula 		default:
1570379bc100SJani Nikula 			WARN(1, "Unsupported link rate\n");
1571379bc100SJani Nikula 			break;
1572379bc100SJani Nikula 		}
1573379bc100SJani Nikula 		link_clock *= 2;
1574379bc100SJani Nikula 	}
1575379bc100SJani Nikula 
1576379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1577379bc100SJani Nikula 
1578379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1579379bc100SJani Nikula }
1580379bc100SJani Nikula 
1581379bc100SJani Nikula static void skl_ddi_clock_get(struct intel_encoder *encoder,
1582379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1583379bc100SJani Nikula {
1584379bc100SJani Nikula 	struct intel_dpll_hw_state *pll_state = &pipe_config->dpll_hw_state;
1585379bc100SJani Nikula 	int link_clock;
1586379bc100SJani Nikula 
1587379bc100SJani Nikula 	/*
1588379bc100SJani Nikula 	 * ctrl1 register is already shifted for each pll, just use 0 to get
1589379bc100SJani Nikula 	 * the internal shift for each field
1590379bc100SJani Nikula 	 */
1591379bc100SJani Nikula 	if (pll_state->ctrl1 & DPLL_CTRL1_HDMI_MODE(0)) {
1592379bc100SJani Nikula 		link_clock = skl_calc_wrpll_link(pll_state);
1593379bc100SJani Nikula 	} else {
1594379bc100SJani Nikula 		link_clock = pll_state->ctrl1 & DPLL_CTRL1_LINK_RATE_MASK(0);
1595379bc100SJani Nikula 		link_clock >>= DPLL_CTRL1_LINK_RATE_SHIFT(0);
1596379bc100SJani Nikula 
1597379bc100SJani Nikula 		switch (link_clock) {
1598379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_810:
1599379bc100SJani Nikula 			link_clock = 81000;
1600379bc100SJani Nikula 			break;
1601379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1080:
1602379bc100SJani Nikula 			link_clock = 108000;
1603379bc100SJani Nikula 			break;
1604379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1350:
1605379bc100SJani Nikula 			link_clock = 135000;
1606379bc100SJani Nikula 			break;
1607379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_1620:
1608379bc100SJani Nikula 			link_clock = 162000;
1609379bc100SJani Nikula 			break;
1610379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_2160:
1611379bc100SJani Nikula 			link_clock = 216000;
1612379bc100SJani Nikula 			break;
1613379bc100SJani Nikula 		case DPLL_CTRL1_LINK_RATE_2700:
1614379bc100SJani Nikula 			link_clock = 270000;
1615379bc100SJani Nikula 			break;
1616379bc100SJani Nikula 		default:
1617379bc100SJani Nikula 			WARN(1, "Unsupported link rate\n");
1618379bc100SJani Nikula 			break;
1619379bc100SJani Nikula 		}
1620379bc100SJani Nikula 		link_clock *= 2;
1621379bc100SJani Nikula 	}
1622379bc100SJani Nikula 
1623379bc100SJani Nikula 	pipe_config->port_clock = link_clock;
1624379bc100SJani Nikula 
1625379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1626379bc100SJani Nikula }
1627379bc100SJani Nikula 
1628379bc100SJani Nikula static void hsw_ddi_clock_get(struct intel_encoder *encoder,
1629379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1630379bc100SJani Nikula {
1631379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1632379bc100SJani Nikula 	int link_clock = 0;
1633379bc100SJani Nikula 	u32 val, pll;
1634379bc100SJani Nikula 
1635379bc100SJani Nikula 	val = hsw_pll_to_ddi_pll_sel(pipe_config->shared_dpll);
1636379bc100SJani Nikula 	switch (val & PORT_CLK_SEL_MASK) {
1637379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_810:
1638379bc100SJani Nikula 		link_clock = 81000;
1639379bc100SJani Nikula 		break;
1640379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_1350:
1641379bc100SJani Nikula 		link_clock = 135000;
1642379bc100SJani Nikula 		break;
1643379bc100SJani Nikula 	case PORT_CLK_SEL_LCPLL_2700:
1644379bc100SJani Nikula 		link_clock = 270000;
1645379bc100SJani Nikula 		break;
1646379bc100SJani Nikula 	case PORT_CLK_SEL_WRPLL1:
1647379bc100SJani Nikula 		link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(0));
1648379bc100SJani Nikula 		break;
1649379bc100SJani Nikula 	case PORT_CLK_SEL_WRPLL2:
1650379bc100SJani Nikula 		link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(1));
1651379bc100SJani Nikula 		break;
1652379bc100SJani Nikula 	case PORT_CLK_SEL_SPLL:
1653379bc100SJani Nikula 		pll = I915_READ(SPLL_CTL) & SPLL_FREQ_MASK;
1654379bc100SJani Nikula 		if (pll == SPLL_FREQ_810MHz)
1655379bc100SJani Nikula 			link_clock = 81000;
1656379bc100SJani Nikula 		else if (pll == SPLL_FREQ_1350MHz)
1657379bc100SJani Nikula 			link_clock = 135000;
1658379bc100SJani Nikula 		else if (pll == SPLL_FREQ_2700MHz)
1659379bc100SJani Nikula 			link_clock = 270000;
1660379bc100SJani Nikula 		else {
1661379bc100SJani Nikula 			WARN(1, "bad spll freq\n");
1662379bc100SJani Nikula 			return;
1663379bc100SJani Nikula 		}
1664379bc100SJani Nikula 		break;
1665379bc100SJani Nikula 	default:
1666379bc100SJani Nikula 		WARN(1, "bad port clock sel\n");
1667379bc100SJani Nikula 		return;
1668379bc100SJani Nikula 	}
1669379bc100SJani Nikula 
1670379bc100SJani Nikula 	pipe_config->port_clock = link_clock * 2;
1671379bc100SJani Nikula 
1672379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1673379bc100SJani Nikula }
1674379bc100SJani Nikula 
1675379bc100SJani Nikula static int bxt_calc_pll_link(const struct intel_dpll_hw_state *pll_state)
1676379bc100SJani Nikula {
1677379bc100SJani Nikula 	struct dpll clock;
1678379bc100SJani Nikula 
1679379bc100SJani Nikula 	clock.m1 = 2;
1680379bc100SJani Nikula 	clock.m2 = (pll_state->pll0 & PORT_PLL_M2_MASK) << 22;
1681379bc100SJani Nikula 	if (pll_state->pll3 & PORT_PLL_M2_FRAC_ENABLE)
1682379bc100SJani Nikula 		clock.m2 |= pll_state->pll2 & PORT_PLL_M2_FRAC_MASK;
1683379bc100SJani Nikula 	clock.n = (pll_state->pll1 & PORT_PLL_N_MASK) >> PORT_PLL_N_SHIFT;
1684379bc100SJani Nikula 	clock.p1 = (pll_state->ebb0 & PORT_PLL_P1_MASK) >> PORT_PLL_P1_SHIFT;
1685379bc100SJani Nikula 	clock.p2 = (pll_state->ebb0 & PORT_PLL_P2_MASK) >> PORT_PLL_P2_SHIFT;
1686379bc100SJani Nikula 
1687379bc100SJani Nikula 	return chv_calc_dpll_params(100000, &clock);
1688379bc100SJani Nikula }
1689379bc100SJani Nikula 
1690379bc100SJani Nikula static void bxt_ddi_clock_get(struct intel_encoder *encoder,
1691379bc100SJani Nikula 			      struct intel_crtc_state *pipe_config)
1692379bc100SJani Nikula {
1693379bc100SJani Nikula 	pipe_config->port_clock =
1694379bc100SJani Nikula 		bxt_calc_pll_link(&pipe_config->dpll_hw_state);
1695379bc100SJani Nikula 
1696379bc100SJani Nikula 	ddi_dotclock_get(pipe_config);
1697379bc100SJani Nikula }
1698379bc100SJani Nikula 
1699379bc100SJani Nikula static void intel_ddi_clock_get(struct intel_encoder *encoder,
1700379bc100SJani Nikula 				struct intel_crtc_state *pipe_config)
1701379bc100SJani Nikula {
1702379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1703379bc100SJani Nikula 
1704379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
1705379bc100SJani Nikula 		icl_ddi_clock_get(encoder, pipe_config);
1706379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
1707379bc100SJani Nikula 		cnl_ddi_clock_get(encoder, pipe_config);
1708379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
1709379bc100SJani Nikula 		bxt_ddi_clock_get(encoder, pipe_config);
1710379bc100SJani Nikula 	else if (IS_GEN9_BC(dev_priv))
1711379bc100SJani Nikula 		skl_ddi_clock_get(encoder, pipe_config);
1712379bc100SJani Nikula 	else if (INTEL_GEN(dev_priv) <= 8)
1713379bc100SJani Nikula 		hsw_ddi_clock_get(encoder, pipe_config);
1714379bc100SJani Nikula }
1715379bc100SJani Nikula 
1716379bc100SJani Nikula void intel_ddi_set_pipe_settings(const struct intel_crtc_state *crtc_state)
1717379bc100SJani Nikula {
1718379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1719379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1720379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1721379bc100SJani Nikula 	u32 temp;
1722379bc100SJani Nikula 
1723379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
1724379bc100SJani Nikula 		return;
1725379bc100SJani Nikula 
1726379bc100SJani Nikula 	WARN_ON(transcoder_is_dsi(cpu_transcoder));
1727379bc100SJani Nikula 
1728379bc100SJani Nikula 	temp = TRANS_MSA_SYNC_CLK;
1729379bc100SJani Nikula 
1730379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1731379bc100SJani Nikula 	case 18:
1732379bc100SJani Nikula 		temp |= TRANS_MSA_6_BPC;
1733379bc100SJani Nikula 		break;
1734379bc100SJani Nikula 	case 24:
1735379bc100SJani Nikula 		temp |= TRANS_MSA_8_BPC;
1736379bc100SJani Nikula 		break;
1737379bc100SJani Nikula 	case 30:
1738379bc100SJani Nikula 		temp |= TRANS_MSA_10_BPC;
1739379bc100SJani Nikula 		break;
1740379bc100SJani Nikula 	case 36:
1741379bc100SJani Nikula 		temp |= TRANS_MSA_12_BPC;
1742379bc100SJani Nikula 		break;
1743379bc100SJani Nikula 	default:
1744379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
1745379bc100SJani Nikula 		break;
1746379bc100SJani Nikula 	}
1747379bc100SJani Nikula 
1748cae154fcSVille Syrjälä 	/* nonsense combination */
1749cae154fcSVille Syrjälä 	WARN_ON(crtc_state->limited_color_range &&
1750cae154fcSVille Syrjälä 		crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
1751cae154fcSVille Syrjälä 
1752cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
1753cae154fcSVille Syrjälä 		temp |= TRANS_MSA_CEA_RANGE;
1754cae154fcSVille Syrjälä 
1755379bc100SJani Nikula 	/*
1756379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
1757379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
1758646d3dc8SVille Syrjälä 	 * colorspace information.
1759379bc100SJani Nikula 	 */
1760379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
1761646d3dc8SVille Syrjälä 		temp |= TRANS_MSA_SAMPLING_444 | TRANS_MSA_CLRSP_YCBCR |
1762646d3dc8SVille Syrjälä 			TRANS_MSA_YCBCR_BT709;
1763646d3dc8SVille Syrjälä 
1764379bc100SJani Nikula 	/*
1765379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
1766379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
1767379bc100SJani Nikula 	 * YCBCR 420 signals we should program MSA MISC1 fields which
1768379bc100SJani Nikula 	 * indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
1769379bc100SJani Nikula 	 */
1770379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
1771379bc100SJani Nikula 		temp |= TRANS_MSA_USE_VSC_SDP;
1772379bc100SJani Nikula 	I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
1773379bc100SJani Nikula }
1774379bc100SJani Nikula 
1775379bc100SJani Nikula void intel_ddi_set_vc_payload_alloc(const struct intel_crtc_state *crtc_state,
1776379bc100SJani Nikula 				    bool state)
1777379bc100SJani Nikula {
1778379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1779379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1780379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1781379bc100SJani Nikula 	u32 temp;
1782379bc100SJani Nikula 
1783379bc100SJani Nikula 	temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1784379bc100SJani Nikula 	if (state == true)
1785379bc100SJani Nikula 		temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1786379bc100SJani Nikula 	else
1787379bc100SJani Nikula 		temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1788379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1789379bc100SJani Nikula }
1790379bc100SJani Nikula 
179199389390SJosé Roberto de Souza /*
179299389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
179399389390SJosé Roberto de Souza  *
179499389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
179599389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
179699389390SJosé Roberto de Souza  */
179799389390SJosé Roberto de Souza static u32
179899389390SJosé Roberto de Souza intel_ddi_transcoder_func_reg_val_get(const struct intel_crtc_state *crtc_state)
1799379bc100SJani Nikula {
1800379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1801379bc100SJani Nikula 	struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
1802379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1803379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
1804379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1805379bc100SJani Nikula 	enum port port = encoder->port;
1806379bc100SJani Nikula 	u32 temp;
1807379bc100SJani Nikula 
1808379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1809379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
1810df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12)
1811df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
1812df16b636SMahesh Kumar 	else
1813379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
1814379bc100SJani Nikula 
1815379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
1816379bc100SJani Nikula 	case 18:
1817379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
1818379bc100SJani Nikula 		break;
1819379bc100SJani Nikula 	case 24:
1820379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
1821379bc100SJani Nikula 		break;
1822379bc100SJani Nikula 	case 30:
1823379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
1824379bc100SJani Nikula 		break;
1825379bc100SJani Nikula 	case 36:
1826379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
1827379bc100SJani Nikula 		break;
1828379bc100SJani Nikula 	default:
1829379bc100SJani Nikula 		BUG();
1830379bc100SJani Nikula 	}
1831379bc100SJani Nikula 
1832379bc100SJani Nikula 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
1833379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
1834379bc100SJani Nikula 	if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
1835379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
1836379bc100SJani Nikula 
1837379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
1838379bc100SJani Nikula 		switch (pipe) {
1839379bc100SJani Nikula 		case PIPE_A:
1840379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
1841379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
1842379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
1843379bc100SJani Nikula 			 * support). */
1844379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
1845379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1846379bc100SJani Nikula 			else
1847379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
1848379bc100SJani Nikula 			break;
1849379bc100SJani Nikula 		case PIPE_B:
1850379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1851379bc100SJani Nikula 			break;
1852379bc100SJani Nikula 		case PIPE_C:
1853379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1854379bc100SJani Nikula 			break;
1855379bc100SJani Nikula 		default:
1856379bc100SJani Nikula 			BUG();
1857379bc100SJani Nikula 			break;
1858379bc100SJani Nikula 		}
1859379bc100SJani Nikula 	}
1860379bc100SJani Nikula 
1861379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1862379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
1863379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
1864379bc100SJani Nikula 		else
1865379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
1866379bc100SJani Nikula 
1867379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
1868379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
1869379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
1870379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
1871379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
1872379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI;
1873379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
1874379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
1875379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1876379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1877379bc100SJani Nikula 	} else {
1878379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1879379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
1880379bc100SJani Nikula 	}
1881379bc100SJani Nikula 
188299389390SJosé Roberto de Souza 	return temp;
188399389390SJosé Roberto de Souza }
188499389390SJosé Roberto de Souza 
188599389390SJosé Roberto de Souza void intel_ddi_enable_transcoder_func(const struct intel_crtc_state *crtc_state)
188699389390SJosé Roberto de Souza {
188799389390SJosé Roberto de Souza 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
188899389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
188999389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
189099389390SJosé Roberto de Souza 	u32 temp;
189199389390SJosé Roberto de Souza 
189299389390SJosé Roberto de Souza 	temp = intel_ddi_transcoder_func_reg_val_get(crtc_state);
189399389390SJosé Roberto de Souza 	I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
189499389390SJosé Roberto de Souza }
189599389390SJosé Roberto de Souza 
189699389390SJosé Roberto de Souza /*
189799389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
189899389390SJosé Roberto de Souza  * bit.
189999389390SJosé Roberto de Souza  */
190099389390SJosé Roberto de Souza static void
190199389390SJosé Roberto de Souza intel_ddi_config_transcoder_func(const struct intel_crtc_state *crtc_state)
190299389390SJosé Roberto de Souza {
190399389390SJosé Roberto de Souza 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
190499389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
190599389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
190699389390SJosé Roberto de Souza 	u32 temp;
190799389390SJosé Roberto de Souza 
190899389390SJosé Roberto de Souza 	temp = intel_ddi_transcoder_func_reg_val_get(crtc_state);
190999389390SJosé Roberto de Souza 	temp &= ~TRANS_DDI_FUNC_ENABLE;
1910379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1911379bc100SJani Nikula }
1912379bc100SJani Nikula 
1913379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
1914379bc100SJani Nikula {
1915379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1916379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1917379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1918379bc100SJani Nikula 	i915_reg_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
1919379bc100SJani Nikula 	u32 val = I915_READ(reg);
1920379bc100SJani Nikula 
1921df16b636SMahesh Kumar 	if (INTEL_GEN(dev_priv) >= 12) {
1922df16b636SMahesh Kumar 		val &= ~(TRANS_DDI_FUNC_ENABLE | TGL_TRANS_DDI_PORT_MASK |
1923df16b636SMahesh Kumar 			 TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
1924df16b636SMahesh Kumar 	} else {
1925df16b636SMahesh Kumar 		val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK |
1926df16b636SMahesh Kumar 			 TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
1927df16b636SMahesh Kumar 	}
1928379bc100SJani Nikula 	I915_WRITE(reg, val);
1929379bc100SJani Nikula 
1930379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
1931379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
1932379bc100SJani Nikula 		DRM_DEBUG_KMS("Quirk Increase DDI disabled time\n");
1933379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
1934379bc100SJani Nikula 		msleep(100);
1935379bc100SJani Nikula 	}
1936379bc100SJani Nikula }
1937379bc100SJani Nikula 
1938379bc100SJani Nikula int intel_ddi_toggle_hdcp_signalling(struct intel_encoder *intel_encoder,
1939379bc100SJani Nikula 				     bool enable)
1940379bc100SJani Nikula {
1941379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
1942379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1943379bc100SJani Nikula 	intel_wakeref_t wakeref;
1944379bc100SJani Nikula 	enum pipe pipe = 0;
1945379bc100SJani Nikula 	int ret = 0;
1946379bc100SJani Nikula 	u32 tmp;
1947379bc100SJani Nikula 
1948379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1949379bc100SJani Nikula 						     intel_encoder->power_domain);
1950379bc100SJani Nikula 	if (WARN_ON(!wakeref))
1951379bc100SJani Nikula 		return -ENXIO;
1952379bc100SJani Nikula 
1953379bc100SJani Nikula 	if (WARN_ON(!intel_encoder->get_hw_state(intel_encoder, &pipe))) {
1954379bc100SJani Nikula 		ret = -EIO;
1955379bc100SJani Nikula 		goto out;
1956379bc100SJani Nikula 	}
1957379bc100SJani Nikula 
1958379bc100SJani Nikula 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe));
1959379bc100SJani Nikula 	if (enable)
1960379bc100SJani Nikula 		tmp |= TRANS_DDI_HDCP_SIGNALLING;
1961379bc100SJani Nikula 	else
1962379bc100SJani Nikula 		tmp &= ~TRANS_DDI_HDCP_SIGNALLING;
1963379bc100SJani Nikula 	I915_WRITE(TRANS_DDI_FUNC_CTL(pipe), tmp);
1964379bc100SJani Nikula out:
1965379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
1966379bc100SJani Nikula 	return ret;
1967379bc100SJani Nikula }
1968379bc100SJani Nikula 
1969379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1970379bc100SJani Nikula {
1971379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
1972379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1973379bc100SJani Nikula 	struct intel_encoder *encoder = intel_connector->encoder;
1974379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
1975379bc100SJani Nikula 	enum port port = encoder->port;
1976379bc100SJani Nikula 	enum transcoder cpu_transcoder;
1977379bc100SJani Nikula 	intel_wakeref_t wakeref;
1978379bc100SJani Nikula 	enum pipe pipe = 0;
1979379bc100SJani Nikula 	u32 tmp;
1980379bc100SJani Nikula 	bool ret;
1981379bc100SJani Nikula 
1982379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1983379bc100SJani Nikula 						     encoder->power_domain);
1984379bc100SJani Nikula 	if (!wakeref)
1985379bc100SJani Nikula 		return false;
1986379bc100SJani Nikula 
1987379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
1988379bc100SJani Nikula 		ret = false;
1989379bc100SJani Nikula 		goto out;
1990379bc100SJani Nikula 	}
1991379bc100SJani Nikula 
1992379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A)
1993379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
1994379bc100SJani Nikula 	else
1995379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
1996379bc100SJani Nikula 
1997379bc100SJani Nikula 	tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1998379bc100SJani Nikula 
1999379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
2000379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
2001379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
2002379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
2003379bc100SJani Nikula 		break;
2004379bc100SJani Nikula 
2005379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
2006379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
2007379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
2008379bc100SJani Nikula 		break;
2009379bc100SJani Nikula 
2010379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
2011379bc100SJani Nikula 		/* if the transcoder is in MST state then
2012379bc100SJani Nikula 		 * connector isn't connected */
2013379bc100SJani Nikula 		ret = false;
2014379bc100SJani Nikula 		break;
2015379bc100SJani Nikula 
2016379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
2017379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_VGA;
2018379bc100SJani Nikula 		break;
2019379bc100SJani Nikula 
2020379bc100SJani Nikula 	default:
2021379bc100SJani Nikula 		ret = false;
2022379bc100SJani Nikula 		break;
2023379bc100SJani Nikula 	}
2024379bc100SJani Nikula 
2025379bc100SJani Nikula out:
2026379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
2027379bc100SJani Nikula 
2028379bc100SJani Nikula 	return ret;
2029379bc100SJani Nikula }
2030379bc100SJani Nikula 
2031379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
2032379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
2033379bc100SJani Nikula {
2034379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
2035379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
2036379bc100SJani Nikula 	enum port port = encoder->port;
2037379bc100SJani Nikula 	intel_wakeref_t wakeref;
2038379bc100SJani Nikula 	enum pipe p;
2039379bc100SJani Nikula 	u32 tmp;
2040379bc100SJani Nikula 	u8 mst_pipe_mask;
2041379bc100SJani Nikula 
2042379bc100SJani Nikula 	*pipe_mask = 0;
2043379bc100SJani Nikula 	*is_dp_mst = false;
2044379bc100SJani Nikula 
2045379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
2046379bc100SJani Nikula 						     encoder->power_domain);
2047379bc100SJani Nikula 	if (!wakeref)
2048379bc100SJani Nikula 		return;
2049379bc100SJani Nikula 
2050379bc100SJani Nikula 	tmp = I915_READ(DDI_BUF_CTL(port));
2051379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
2052379bc100SJani Nikula 		goto out;
2053379bc100SJani Nikula 
2054379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A) {
2055379bc100SJani Nikula 		tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
2056379bc100SJani Nikula 
2057379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
2058379bc100SJani Nikula 		default:
2059379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
2060379bc100SJani Nikula 			/* fallthrough */
2061379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
2062379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
2063379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
2064379bc100SJani Nikula 			break;
2065379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
2066379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
2067379bc100SJani Nikula 			break;
2068379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
2069379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
2070379bc100SJani Nikula 			break;
2071379bc100SJani Nikula 		}
2072379bc100SJani Nikula 
2073379bc100SJani Nikula 		goto out;
2074379bc100SJani Nikula 	}
2075379bc100SJani Nikula 
2076379bc100SJani Nikula 	mst_pipe_mask = 0;
2077379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
2078379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
2079df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
20806aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
20816aa3bef1SJosé Roberto de Souza 
20826aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
20836aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
20846aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
20856aa3bef1SJosé Roberto de Souza 			continue;
2086df16b636SMahesh Kumar 
2087df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12) {
2088df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
2089df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
2090df16b636SMahesh Kumar 		} else {
2091df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
2092df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
2093df16b636SMahesh Kumar 		}
2094379bc100SJani Nikula 
2095379bc100SJani Nikula 		tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
20966aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
20976aa3bef1SJosé Roberto de Souza 					trans_wakeref);
2098379bc100SJani Nikula 
2099df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
2100379bc100SJani Nikula 			continue;
2101379bc100SJani Nikula 
2102379bc100SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
2103379bc100SJani Nikula 		    TRANS_DDI_MODE_SELECT_DP_MST)
2104379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
2105379bc100SJani Nikula 
2106379bc100SJani Nikula 		*pipe_mask |= BIT(p);
2107379bc100SJani Nikula 	}
2108379bc100SJani Nikula 
2109379bc100SJani Nikula 	if (!*pipe_mask)
211066a990ddSVille Syrjälä 		DRM_DEBUG_KMS("No pipe for [ENCODER:%d:%s] found\n",
211166a990ddSVille Syrjälä 			      encoder->base.base.id, encoder->base.name);
2112379bc100SJani Nikula 
2113379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
211466a990ddSVille Syrjälä 		DRM_DEBUG_KMS("Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
211566a990ddSVille Syrjälä 			      encoder->base.base.id, encoder->base.name,
211666a990ddSVille Syrjälä 			      *pipe_mask);
2117379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
2118379bc100SJani Nikula 	}
2119379bc100SJani Nikula 
2120379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
212166a990ddSVille Syrjälä 		DRM_DEBUG_KMS("Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
212266a990ddSVille Syrjälä 			      encoder->base.base.id, encoder->base.name,
212366a990ddSVille Syrjälä 			      *pipe_mask, mst_pipe_mask);
2124379bc100SJani Nikula 	else
2125379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
2126379bc100SJani Nikula 
2127379bc100SJani Nikula out:
2128379bc100SJani Nikula 	if (*pipe_mask && IS_GEN9_LP(dev_priv)) {
2129379bc100SJani Nikula 		tmp = I915_READ(BXT_PHY_CTL(port));
2130379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
2131379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
2132379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
213366a990ddSVille Syrjälä 			DRM_ERROR("[ENCODER:%d:%s] enabled but PHY powered down? "
213466a990ddSVille Syrjälä 				  "(PHY_CTL %08x)\n", encoder->base.base.id,
213566a990ddSVille Syrjälä 				  encoder->base.name, tmp);
2136379bc100SJani Nikula 	}
2137379bc100SJani Nikula 
2138379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
2139379bc100SJani Nikula }
2140379bc100SJani Nikula 
2141379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
2142379bc100SJani Nikula 			    enum pipe *pipe)
2143379bc100SJani Nikula {
2144379bc100SJani Nikula 	u8 pipe_mask;
2145379bc100SJani Nikula 	bool is_mst;
2146379bc100SJani Nikula 
2147379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2148379bc100SJani Nikula 
2149379bc100SJani Nikula 	if (is_mst || !pipe_mask)
2150379bc100SJani Nikula 		return false;
2151379bc100SJani Nikula 
2152379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
2153379bc100SJani Nikula 
2154379bc100SJani Nikula 	return true;
2155379bc100SJani Nikula }
2156379bc100SJani Nikula 
2157379bc100SJani Nikula static inline enum intel_display_power_domain
2158379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
2159379bc100SJani Nikula {
2160379bc100SJani Nikula 	/* CNL+ HW requires corresponding AUX IOs to be powered up for PSR with
2161379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
2162379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
2163379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
2164379bc100SJani Nikula 	 * states enabled.
2165379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
2166379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
2167379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
2168379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
2169379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
2170379bc100SJani Nikula 	 * returns the correct domain for other ports too.
2171379bc100SJani Nikula 	 */
2172379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
2173379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
2174379bc100SJani Nikula }
2175379bc100SJani Nikula 
2176379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
2177379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
2178379bc100SJani Nikula {
2179379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2180379bc100SJani Nikula 	struct intel_digital_port *dig_port;
2181d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2182379bc100SJani Nikula 
2183379bc100SJani Nikula 	/*
2184379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
2185379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
2186379bc100SJani Nikula 	 * hook.
2187379bc100SJani Nikula 	 */
2188379bc100SJani Nikula 	if (WARN_ON(intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
2189379bc100SJani Nikula 		return;
2190379bc100SJani Nikula 
2191379bc100SJani Nikula 	dig_port = enc_to_dig_port(&encoder->base);
2192379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
2193379bc100SJani Nikula 
2194379bc100SJani Nikula 	/*
2195379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
2196379bc100SJani Nikula 	 * ports.
2197379bc100SJani Nikula 	 */
2198379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
2199d8fe2ab6SMatt Roper 	    intel_phy_is_tc(dev_priv, phy))
2200379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2201379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
2202379bc100SJani Nikula 
2203379bc100SJani Nikula 	/*
2204379bc100SJani Nikula 	 * VDSC power is needed when DSC is enabled
2205379bc100SJani Nikula 	 */
2206379bc100SJani Nikula 	if (crtc_state->dsc_params.compression_enable)
2207379bc100SJani Nikula 		intel_display_power_get(dev_priv,
2208379bc100SJani Nikula 					intel_dsc_power_domain(crtc_state));
2209379bc100SJani Nikula }
2210379bc100SJani Nikula 
2211379bc100SJani Nikula void intel_ddi_enable_pipe_clock(const struct intel_crtc_state *crtc_state)
2212379bc100SJani Nikula {
2213379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2214379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2215379bc100SJani Nikula 	struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
2216379bc100SJani Nikula 	enum port port = encoder->port;
2217379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2218379bc100SJani Nikula 
2219df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2220df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2221df16b636SMahesh Kumar 			I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2222df16b636SMahesh Kumar 				   TGL_TRANS_CLK_SEL_PORT(port));
2223df16b636SMahesh Kumar 		else
2224379bc100SJani Nikula 			I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2225379bc100SJani Nikula 				   TRANS_CLK_SEL_PORT(port));
2226379bc100SJani Nikula 	}
2227df16b636SMahesh Kumar }
2228379bc100SJani Nikula 
2229379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
2230379bc100SJani Nikula {
2231379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
2232379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2233379bc100SJani Nikula 
2234df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
2235df16b636SMahesh Kumar 		if (INTEL_GEN(dev_priv) >= 12)
2236df16b636SMahesh Kumar 			I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2237df16b636SMahesh Kumar 				   TGL_TRANS_CLK_SEL_DISABLED);
2238df16b636SMahesh Kumar 		else
2239379bc100SJani Nikula 			I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
2240379bc100SJani Nikula 				   TRANS_CLK_SEL_DISABLED);
2241379bc100SJani Nikula 	}
2242df16b636SMahesh Kumar }
2243379bc100SJani Nikula 
2244379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
2245379bc100SJani Nikula 				enum port port, u8 iboost)
2246379bc100SJani Nikula {
2247379bc100SJani Nikula 	u32 tmp;
2248379bc100SJani Nikula 
2249379bc100SJani Nikula 	tmp = I915_READ(DISPIO_CR_TX_BMU_CR0);
2250379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
2251379bc100SJani Nikula 	if (iboost)
2252379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
2253379bc100SJani Nikula 	else
2254379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
2255379bc100SJani Nikula 	I915_WRITE(DISPIO_CR_TX_BMU_CR0, tmp);
2256379bc100SJani Nikula }
2257379bc100SJani Nikula 
2258379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
2259379bc100SJani Nikula 			       int level, enum intel_output_type type)
2260379bc100SJani Nikula {
2261379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
2262379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2263379bc100SJani Nikula 	enum port port = encoder->port;
2264379bc100SJani Nikula 	u8 iboost;
2265379bc100SJani Nikula 
2266379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2267379bc100SJani Nikula 		iboost = dev_priv->vbt.ddi_port_info[port].hdmi_boost_level;
2268379bc100SJani Nikula 	else
2269379bc100SJani Nikula 		iboost = dev_priv->vbt.ddi_port_info[port].dp_boost_level;
2270379bc100SJani Nikula 
2271379bc100SJani Nikula 	if (iboost == 0) {
2272379bc100SJani Nikula 		const struct ddi_buf_trans *ddi_translations;
2273379bc100SJani Nikula 		int n_entries;
2274379bc100SJani Nikula 
2275379bc100SJani Nikula 		if (type == INTEL_OUTPUT_HDMI)
2276379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
2277379bc100SJani Nikula 		else if (type == INTEL_OUTPUT_EDP)
2278379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
2279379bc100SJani Nikula 		else
2280379bc100SJani Nikula 			ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
2281379bc100SJani Nikula 
2282379bc100SJani Nikula 		if (WARN_ON_ONCE(!ddi_translations))
2283379bc100SJani Nikula 			return;
2284379bc100SJani Nikula 		if (WARN_ON_ONCE(level >= n_entries))
2285379bc100SJani Nikula 			level = n_entries - 1;
2286379bc100SJani Nikula 
2287379bc100SJani Nikula 		iboost = ddi_translations[level].i_boost;
2288379bc100SJani Nikula 	}
2289379bc100SJani Nikula 
2290379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
2291379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
2292379bc100SJani Nikula 		DRM_ERROR("Invalid I_boost value %u\n", iboost);
2293379bc100SJani Nikula 		return;
2294379bc100SJani Nikula 	}
2295379bc100SJani Nikula 
2296379bc100SJani Nikula 	_skl_ddi_set_iboost(dev_priv, port, iboost);
2297379bc100SJani Nikula 
2298379bc100SJani Nikula 	if (port == PORT_A && intel_dig_port->max_lanes == 4)
2299379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
2300379bc100SJani Nikula }
2301379bc100SJani Nikula 
2302379bc100SJani Nikula static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
2303379bc100SJani Nikula 				    int level, enum intel_output_type type)
2304379bc100SJani Nikula {
2305379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2306379bc100SJani Nikula 	const struct bxt_ddi_buf_trans *ddi_translations;
2307379bc100SJani Nikula 	enum port port = encoder->port;
2308379bc100SJani Nikula 	int n_entries;
2309379bc100SJani Nikula 
2310379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2311379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
2312379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2313379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_edp(dev_priv, &n_entries);
2314379bc100SJani Nikula 	else
2315379bc100SJani Nikula 		ddi_translations = bxt_get_buf_trans_dp(dev_priv, &n_entries);
2316379bc100SJani Nikula 
2317379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
2318379bc100SJani Nikula 		return;
2319379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
2320379bc100SJani Nikula 		level = n_entries - 1;
2321379bc100SJani Nikula 
2322379bc100SJani Nikula 	bxt_ddi_phy_set_signal_level(dev_priv, port,
2323379bc100SJani Nikula 				     ddi_translations[level].margin,
2324379bc100SJani Nikula 				     ddi_translations[level].scale,
2325379bc100SJani Nikula 				     ddi_translations[level].enable,
2326379bc100SJani Nikula 				     ddi_translations[level].deemphasis);
2327379bc100SJani Nikula }
2328379bc100SJani Nikula 
2329379bc100SJani Nikula u8 intel_ddi_dp_voltage_max(struct intel_encoder *encoder)
2330379bc100SJani Nikula {
2331379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2332379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2333379bc100SJani Nikula 	enum port port = encoder->port;
2334d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2335379bc100SJani Nikula 	int n_entries;
2336379bc100SJani Nikula 
2337379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2338d8fe2ab6SMatt Roper 		if (intel_phy_is_combo(dev_priv, phy))
23394a8134d5SMatt Roper 			icl_get_combo_buf_trans(dev_priv, encoder->type,
2340379bc100SJani Nikula 						intel_dp->link_rate, &n_entries);
2341379bc100SJani Nikula 		else
2342379bc100SJani Nikula 			n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
2343379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
2344379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2345379bc100SJani Nikula 			cnl_get_buf_trans_edp(dev_priv, &n_entries);
2346379bc100SJani Nikula 		else
2347379bc100SJani Nikula 			cnl_get_buf_trans_dp(dev_priv, &n_entries);
2348379bc100SJani Nikula 	} else if (IS_GEN9_LP(dev_priv)) {
2349379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2350379bc100SJani Nikula 			bxt_get_buf_trans_edp(dev_priv, &n_entries);
2351379bc100SJani Nikula 		else
2352379bc100SJani Nikula 			bxt_get_buf_trans_dp(dev_priv, &n_entries);
2353379bc100SJani Nikula 	} else {
2354379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
2355379bc100SJani Nikula 			intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
2356379bc100SJani Nikula 		else
2357379bc100SJani Nikula 			intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
2358379bc100SJani Nikula 	}
2359379bc100SJani Nikula 
2360379bc100SJani Nikula 	if (WARN_ON(n_entries < 1))
2361379bc100SJani Nikula 		n_entries = 1;
2362379bc100SJani Nikula 	if (WARN_ON(n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
2363379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
2364379bc100SJani Nikula 
2365379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
2366379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
2367379bc100SJani Nikula }
2368379bc100SJani Nikula 
2369379bc100SJani Nikula /*
2370379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
2371379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
2372379bc100SJani Nikula  * rethink this code.
2373379bc100SJani Nikula  */
2374379bc100SJani Nikula u8 intel_ddi_dp_pre_emphasis_max(struct intel_encoder *encoder, u8 voltage_swing)
2375379bc100SJani Nikula {
2376379bc100SJani Nikula 	switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
2377379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_0:
2378379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_3;
2379379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_1:
2380379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_2;
2381379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_2:
2382379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_1;
2383379bc100SJani Nikula 	case DP_TRAIN_VOLTAGE_SWING_LEVEL_3:
2384379bc100SJani Nikula 	default:
2385379bc100SJani Nikula 		return DP_TRAIN_PRE_EMPH_LEVEL_0;
2386379bc100SJani Nikula 	}
2387379bc100SJani Nikula }
2388379bc100SJani Nikula 
2389379bc100SJani Nikula static void cnl_ddi_vswing_program(struct intel_encoder *encoder,
2390379bc100SJani Nikula 				   int level, enum intel_output_type type)
2391379bc100SJani Nikula {
2392379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2393379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations;
2394379bc100SJani Nikula 	enum port port = encoder->port;
2395379bc100SJani Nikula 	int n_entries, ln;
2396379bc100SJani Nikula 	u32 val;
2397379bc100SJani Nikula 
2398379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2399379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
2400379bc100SJani Nikula 	else if (type == INTEL_OUTPUT_EDP)
2401379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_edp(dev_priv, &n_entries);
2402379bc100SJani Nikula 	else
2403379bc100SJani Nikula 		ddi_translations = cnl_get_buf_trans_dp(dev_priv, &n_entries);
2404379bc100SJani Nikula 
2405379bc100SJani Nikula 	if (WARN_ON_ONCE(!ddi_translations))
2406379bc100SJani Nikula 		return;
2407379bc100SJani Nikula 	if (WARN_ON_ONCE(level >= n_entries))
2408379bc100SJani Nikula 		level = n_entries - 1;
2409379bc100SJani Nikula 
2410379bc100SJani Nikula 	/* Set PORT_TX_DW5 Scaling Mode Sel to 010b. */
2411379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2412379bc100SJani Nikula 	val &= ~SCALING_MODE_SEL_MASK;
2413379bc100SJani Nikula 	val |= SCALING_MODE_SEL(2);
2414379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2415379bc100SJani Nikula 
2416379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2417379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW2_LN0(port));
2418379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2419379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2420379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2421379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2422379bc100SJani Nikula 	/* Rcomp scalar is fixed as 0x98 for every table entry */
2423379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2424379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW2_GRP(port), val);
2425379bc100SJani Nikula 
2426379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2427379bc100SJani Nikula 	/* We cannot write to GRP. It would overrite individual loadgen */
2428379bc100SJani Nikula 	for (ln = 0; ln < 4; ln++) {
2429379bc100SJani Nikula 		val = I915_READ(CNL_PORT_TX_DW4_LN(ln, port));
2430379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2431379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2432379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2433379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2434379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2435379bc100SJani Nikula 		I915_WRITE(CNL_PORT_TX_DW4_LN(ln, port), val);
2436379bc100SJani Nikula 	}
2437379bc100SJani Nikula 
2438379bc100SJani Nikula 	/* Program PORT_TX_DW5 */
2439379bc100SJani Nikula 	/* All DW5 values are fixed for every table entry */
2440379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2441379bc100SJani Nikula 	val &= ~RTERM_SELECT_MASK;
2442379bc100SJani Nikula 	val |= RTERM_SELECT(6);
2443379bc100SJani Nikula 	val |= TAP3_DISABLE;
2444379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2445379bc100SJani Nikula 
2446379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2447379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW7_LN0(port));
2448379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2449379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2450379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW7_GRP(port), val);
2451379bc100SJani Nikula }
2452379bc100SJani Nikula 
2453379bc100SJani Nikula static void cnl_ddi_vswing_sequence(struct intel_encoder *encoder,
2454379bc100SJani Nikula 				    int level, enum intel_output_type type)
2455379bc100SJani Nikula {
2456379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2457379bc100SJani Nikula 	enum port port = encoder->port;
2458379bc100SJani Nikula 	int width, rate, ln;
2459379bc100SJani Nikula 	u32 val;
2460379bc100SJani Nikula 
2461379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2462379bc100SJani Nikula 		width = 4;
2463379bc100SJani Nikula 		rate = 0; /* Rate is always < than 6GHz for HDMI */
2464379bc100SJani Nikula 	} else {
2465379bc100SJani Nikula 		struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2466379bc100SJani Nikula 
2467379bc100SJani Nikula 		width = intel_dp->lane_count;
2468379bc100SJani Nikula 		rate = intel_dp->link_rate;
2469379bc100SJani Nikula 	}
2470379bc100SJani Nikula 
2471379bc100SJani Nikula 	/*
2472379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2473379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2474379bc100SJani Nikula 	 * else clear to 0b.
2475379bc100SJani Nikula 	 */
2476379bc100SJani Nikula 	val = I915_READ(CNL_PORT_PCS_DW1_LN0(port));
2477379bc100SJani Nikula 	if (type != INTEL_OUTPUT_HDMI)
2478379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2479379bc100SJani Nikula 	else
2480379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2481379bc100SJani Nikula 	I915_WRITE(CNL_PORT_PCS_DW1_GRP(port), val);
2482379bc100SJani Nikula 
2483379bc100SJani Nikula 	/* 2. Program loadgen select */
2484379bc100SJani Nikula 	/*
2485379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2486379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2487379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2488379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2489379bc100SJani Nikula 	 */
2490379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2491379bc100SJani Nikula 		val = I915_READ(CNL_PORT_TX_DW4_LN(ln, port));
2492379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2493379bc100SJani Nikula 
2494379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1)  ||
2495379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2496379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2497379bc100SJani Nikula 		}
2498379bc100SJani Nikula 		I915_WRITE(CNL_PORT_TX_DW4_LN(ln, port), val);
2499379bc100SJani Nikula 	}
2500379bc100SJani Nikula 
2501379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2502379bc100SJani Nikula 	val = I915_READ(CNL_PORT_CL1CM_DW5);
2503379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2504379bc100SJani Nikula 	I915_WRITE(CNL_PORT_CL1CM_DW5, val);
2505379bc100SJani Nikula 
2506379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2507379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2508379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2509379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2510379bc100SJani Nikula 
2511379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2512379bc100SJani Nikula 	cnl_ddi_vswing_program(encoder, level, type);
2513379bc100SJani Nikula 
2514379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2515379bc100SJani Nikula 	val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2516379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2517379bc100SJani Nikula 	I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2518379bc100SJani Nikula }
2519379bc100SJani Nikula 
2520379bc100SJani Nikula static void icl_ddi_combo_vswing_program(struct drm_i915_private *dev_priv,
2521dc867bc7SMatt Roper 					u32 level, enum phy phy, int type,
2522379bc100SJani Nikula 					int rate)
2523379bc100SJani Nikula {
2524379bc100SJani Nikula 	const struct cnl_ddi_buf_trans *ddi_translations = NULL;
2525379bc100SJani Nikula 	u32 n_entries, val;
2526379bc100SJani Nikula 	int ln;
2527379bc100SJani Nikula 
25284a8134d5SMatt Roper 	ddi_translations = icl_get_combo_buf_trans(dev_priv, type, rate,
25294a8134d5SMatt Roper 						   &n_entries);
2530379bc100SJani Nikula 	if (!ddi_translations)
2531379bc100SJani Nikula 		return;
2532379bc100SJani Nikula 
2533379bc100SJani Nikula 	if (level >= n_entries) {
2534379bc100SJani Nikula 		DRM_DEBUG_KMS("DDI translation not found for level %d. Using %d instead.", level, n_entries - 1);
2535379bc100SJani Nikula 		level = n_entries - 1;
2536379bc100SJani Nikula 	}
2537379bc100SJani Nikula 
2538379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
2539dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_TX_DW5_LN0(phy));
2540379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
2541379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
2542379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
2543379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
2544379bc100SJani Nikula 	val |= TAP3_DISABLE;
2545dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_TX_DW5_GRP(phy), val);
2546379bc100SJani Nikula 
2547379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
2548dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_TX_DW2_LN0(phy));
2549379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
2550379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
2551379bc100SJani Nikula 	val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
2552379bc100SJani Nikula 	val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
2553379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
2554379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
2555dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_TX_DW2_GRP(phy), val);
2556379bc100SJani Nikula 
2557379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
2558379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
2559379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2560dc867bc7SMatt Roper 		val = I915_READ(ICL_PORT_TX_DW4_LN(ln, phy));
2561379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
2562379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
2563379bc100SJani Nikula 		val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
2564379bc100SJani Nikula 		val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
2565379bc100SJani Nikula 		val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
2566dc867bc7SMatt Roper 		I915_WRITE(ICL_PORT_TX_DW4_LN(ln, phy), val);
2567379bc100SJani Nikula 	}
2568379bc100SJani Nikula 
2569379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
2570dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_TX_DW7_LN0(phy));
2571379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
2572379bc100SJani Nikula 	val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
2573dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_TX_DW7_GRP(phy), val);
2574379bc100SJani Nikula }
2575379bc100SJani Nikula 
2576379bc100SJani Nikula static void icl_combo_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2577379bc100SJani Nikula 					      u32 level,
2578379bc100SJani Nikula 					      enum intel_output_type type)
2579379bc100SJani Nikula {
2580379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2581dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2582379bc100SJani Nikula 	int width = 0;
2583379bc100SJani Nikula 	int rate = 0;
2584379bc100SJani Nikula 	u32 val;
2585379bc100SJani Nikula 	int ln = 0;
2586379bc100SJani Nikula 
2587379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI) {
2588379bc100SJani Nikula 		width = 4;
2589379bc100SJani Nikula 		/* Rate is always < than 6GHz for HDMI */
2590379bc100SJani Nikula 	} else {
2591379bc100SJani Nikula 		struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2592379bc100SJani Nikula 
2593379bc100SJani Nikula 		width = intel_dp->lane_count;
2594379bc100SJani Nikula 		rate = intel_dp->link_rate;
2595379bc100SJani Nikula 	}
2596379bc100SJani Nikula 
2597379bc100SJani Nikula 	/*
2598379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
2599379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2600379bc100SJani Nikula 	 * else clear to 0b.
2601379bc100SJani Nikula 	 */
2602dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_PCS_DW1_LN0(phy));
2603379bc100SJani Nikula 	if (type == INTEL_OUTPUT_HDMI)
2604379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
2605379bc100SJani Nikula 	else
2606379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
2607dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_PCS_DW1_GRP(phy), val);
2608379bc100SJani Nikula 
2609379bc100SJani Nikula 	/* 2. Program loadgen select */
2610379bc100SJani Nikula 	/*
2611379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2612379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2613379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2614379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
2615379bc100SJani Nikula 	 */
2616379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
2617dc867bc7SMatt Roper 		val = I915_READ(ICL_PORT_TX_DW4_LN(ln, phy));
2618379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
2619379bc100SJani Nikula 
2620379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
2621379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
2622379bc100SJani Nikula 			val |= LOADGEN_SELECT;
2623379bc100SJani Nikula 		}
2624dc867bc7SMatt Roper 		I915_WRITE(ICL_PORT_TX_DW4_LN(ln, phy), val);
2625379bc100SJani Nikula 	}
2626379bc100SJani Nikula 
2627379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2628dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_CL_DW5(phy));
2629379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
2630dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_CL_DW5(phy), val);
2631379bc100SJani Nikula 
2632379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
2633dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_TX_DW5_LN0(phy));
2634379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
2635dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_TX_DW5_GRP(phy), val);
2636379bc100SJani Nikula 
2637379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
2638dc867bc7SMatt Roper 	icl_ddi_combo_vswing_program(dev_priv, level, phy, type, rate);
2639379bc100SJani Nikula 
2640379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
2641dc867bc7SMatt Roper 	val = I915_READ(ICL_PORT_TX_DW5_LN0(phy));
2642379bc100SJani Nikula 	val |= TX_TRAINING_EN;
2643dc867bc7SMatt Roper 	I915_WRITE(ICL_PORT_TX_DW5_GRP(phy), val);
2644379bc100SJani Nikula }
2645379bc100SJani Nikula 
2646379bc100SJani Nikula static void icl_mg_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
2647379bc100SJani Nikula 					   int link_clock,
2648379bc100SJani Nikula 					   u32 level)
2649379bc100SJani Nikula {
2650379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2651379bc100SJani Nikula 	enum port port = encoder->port;
2652379bc100SJani Nikula 	const struct icl_mg_phy_ddi_buf_trans *ddi_translations;
2653379bc100SJani Nikula 	u32 n_entries, val;
2654379bc100SJani Nikula 	int ln;
2655379bc100SJani Nikula 
2656379bc100SJani Nikula 	n_entries = ARRAY_SIZE(icl_mg_phy_ddi_translations);
2657379bc100SJani Nikula 	ddi_translations = icl_mg_phy_ddi_translations;
2658379bc100SJani Nikula 	/* The table does not have values for level 3 and level 9. */
2659379bc100SJani Nikula 	if (level >= n_entries || level == 3 || level == 9) {
2660379bc100SJani Nikula 		DRM_DEBUG_KMS("DDI translation not found for level %d. Using %d instead.",
2661379bc100SJani Nikula 			      level, n_entries - 2);
2662379bc100SJani Nikula 		level = n_entries - 2;
2663379bc100SJani Nikula 	}
2664379bc100SJani Nikula 
2665379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
2666379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2667379bc100SJani Nikula 		val = I915_READ(MG_TX1_LINK_PARAMS(ln, port));
2668379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2669379bc100SJani Nikula 		I915_WRITE(MG_TX1_LINK_PARAMS(ln, port), val);
2670379bc100SJani Nikula 
2671379bc100SJani Nikula 		val = I915_READ(MG_TX2_LINK_PARAMS(ln, port));
2672379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
2673379bc100SJani Nikula 		I915_WRITE(MG_TX2_LINK_PARAMS(ln, port), val);
2674379bc100SJani Nikula 	}
2675379bc100SJani Nikula 
2676379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
2677379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2678379bc100SJani Nikula 		val = I915_READ(MG_TX1_SWINGCTRL(ln, port));
2679379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2680379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2681379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2682379bc100SJani Nikula 		I915_WRITE(MG_TX1_SWINGCTRL(ln, port), val);
2683379bc100SJani Nikula 
2684379bc100SJani Nikula 		val = I915_READ(MG_TX2_SWINGCTRL(ln, port));
2685379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
2686379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
2687379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_17_12);
2688379bc100SJani Nikula 		I915_WRITE(MG_TX2_SWINGCTRL(ln, port), val);
2689379bc100SJani Nikula 	}
2690379bc100SJani Nikula 
2691379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
2692379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2693379bc100SJani Nikula 		val = I915_READ(MG_TX1_DRVCTRL(ln, port));
2694379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2695379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2696379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2697379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2698379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2699379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2700379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2701379bc100SJani Nikula 		I915_WRITE(MG_TX1_DRVCTRL(ln, port), val);
2702379bc100SJani Nikula 
2703379bc100SJani Nikula 		val = I915_READ(MG_TX2_DRVCTRL(ln, port));
2704379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
2705379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
2706379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
2707379bc100SJani Nikula 			ddi_translations[level].cri_txdeemph_override_5_0) |
2708379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
2709379bc100SJani Nikula 				ddi_translations[level].cri_txdeemph_override_11_6) |
2710379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
2711379bc100SJani Nikula 		I915_WRITE(MG_TX2_DRVCTRL(ln, port), val);
2712379bc100SJani Nikula 
2713379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
2714379bc100SJani Nikula 	}
2715379bc100SJani Nikula 
2716379bc100SJani Nikula 	/*
2717379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
2718379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
2719379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
2720379bc100SJani Nikula 	 */
2721379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2722379bc100SJani Nikula 		val = I915_READ(MG_CLKHUB(ln, port));
2723379bc100SJani Nikula 		if (link_clock < 300000)
2724379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
2725379bc100SJani Nikula 		else
2726379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
2727379bc100SJani Nikula 		I915_WRITE(MG_CLKHUB(ln, port), val);
2728379bc100SJani Nikula 	}
2729379bc100SJani Nikula 
2730379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
2731379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2732379bc100SJani Nikula 		val = I915_READ(MG_TX1_DCC(ln, port));
2733379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2734379bc100SJani Nikula 		if (link_clock <= 500000) {
2735379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2736379bc100SJani Nikula 		} else {
2737379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2738379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2739379bc100SJani Nikula 		}
2740379bc100SJani Nikula 		I915_WRITE(MG_TX1_DCC(ln, port), val);
2741379bc100SJani Nikula 
2742379bc100SJani Nikula 		val = I915_READ(MG_TX2_DCC(ln, port));
2743379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
2744379bc100SJani Nikula 		if (link_clock <= 500000) {
2745379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
2746379bc100SJani Nikula 		} else {
2747379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
2748379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
2749379bc100SJani Nikula 		}
2750379bc100SJani Nikula 		I915_WRITE(MG_TX2_DCC(ln, port), val);
2751379bc100SJani Nikula 	}
2752379bc100SJani Nikula 
2753379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
2754379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
2755379bc100SJani Nikula 		val = I915_READ(MG_TX1_PISO_READLOAD(ln, port));
2756379bc100SJani Nikula 		val |= CRI_CALCINIT;
2757379bc100SJani Nikula 		I915_WRITE(MG_TX1_PISO_READLOAD(ln, port), val);
2758379bc100SJani Nikula 
2759379bc100SJani Nikula 		val = I915_READ(MG_TX2_PISO_READLOAD(ln, port));
2760379bc100SJani Nikula 		val |= CRI_CALCINIT;
2761379bc100SJani Nikula 		I915_WRITE(MG_TX2_PISO_READLOAD(ln, port), val);
2762379bc100SJani Nikula 	}
2763379bc100SJani Nikula }
2764379bc100SJani Nikula 
2765379bc100SJani Nikula static void icl_ddi_vswing_sequence(struct intel_encoder *encoder,
2766379bc100SJani Nikula 				    int link_clock,
2767379bc100SJani Nikula 				    u32 level,
2768379bc100SJani Nikula 				    enum intel_output_type type)
2769379bc100SJani Nikula {
2770379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2771d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2772379bc100SJani Nikula 
2773d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy))
2774379bc100SJani Nikula 		icl_combo_phy_ddi_vswing_sequence(encoder, level, type);
2775379bc100SJani Nikula 	else
2776379bc100SJani Nikula 		icl_mg_phy_ddi_vswing_sequence(encoder, link_clock, level);
2777379bc100SJani Nikula }
2778379bc100SJani Nikula 
2779379bc100SJani Nikula static u32 translate_signal_level(int signal_levels)
2780379bc100SJani Nikula {
2781379bc100SJani Nikula 	int i;
2782379bc100SJani Nikula 
2783379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
2784379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
2785379bc100SJani Nikula 			return i;
2786379bc100SJani Nikula 	}
2787379bc100SJani Nikula 
2788379bc100SJani Nikula 	WARN(1, "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
2789379bc100SJani Nikula 	     signal_levels);
2790379bc100SJani Nikula 
2791379bc100SJani Nikula 	return 0;
2792379bc100SJani Nikula }
2793379bc100SJani Nikula 
2794379bc100SJani Nikula static u32 intel_ddi_dp_level(struct intel_dp *intel_dp)
2795379bc100SJani Nikula {
2796379bc100SJani Nikula 	u8 train_set = intel_dp->train_set[0];
2797379bc100SJani Nikula 	int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2798379bc100SJani Nikula 					 DP_TRAIN_PRE_EMPHASIS_MASK);
2799379bc100SJani Nikula 
2800379bc100SJani Nikula 	return translate_signal_level(signal_levels);
2801379bc100SJani Nikula }
2802379bc100SJani Nikula 
2803379bc100SJani Nikula u32 bxt_signal_levels(struct intel_dp *intel_dp)
2804379bc100SJani Nikula {
2805379bc100SJani Nikula 	struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2806379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
2807379bc100SJani Nikula 	struct intel_encoder *encoder = &dport->base;
2808379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2809379bc100SJani Nikula 
2810379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
2811379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, intel_dp->link_rate,
2812379bc100SJani Nikula 					level, encoder->type);
2813379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
2814379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
2815379bc100SJani Nikula 	else
2816379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
2817379bc100SJani Nikula 
2818379bc100SJani Nikula 	return 0;
2819379bc100SJani Nikula }
2820379bc100SJani Nikula 
2821379bc100SJani Nikula u32 ddi_signal_levels(struct intel_dp *intel_dp)
2822379bc100SJani Nikula {
2823379bc100SJani Nikula 	struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2824379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
2825379bc100SJani Nikula 	struct intel_encoder *encoder = &dport->base;
2826379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2827379bc100SJani Nikula 
2828379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
2829379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, encoder->type);
2830379bc100SJani Nikula 
2831379bc100SJani Nikula 	return DDI_BUF_TRANS_SELECT(level);
2832379bc100SJani Nikula }
2833379bc100SJani Nikula 
2834379bc100SJani Nikula static inline
2835379bc100SJani Nikula u32 icl_dpclka_cfgcr0_clk_off(struct drm_i915_private *dev_priv,
2836befa372bSMatt Roper 			      enum phy phy)
2837379bc100SJani Nikula {
2838befa372bSMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
2839befa372bSMatt Roper 		return ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
2840befa372bSMatt Roper 	} else if (intel_phy_is_tc(dev_priv, phy)) {
2841befa372bSMatt Roper 		enum tc_port tc_port = intel_port_to_tc(dev_priv,
2842befa372bSMatt Roper 							(enum port)phy);
2843379bc100SJani Nikula 
2844379bc100SJani Nikula 		return ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port);
2845379bc100SJani Nikula 	}
2846379bc100SJani Nikula 
2847379bc100SJani Nikula 	return 0;
2848379bc100SJani Nikula }
2849379bc100SJani Nikula 
2850379bc100SJani Nikula static void icl_map_plls_to_ports(struct intel_encoder *encoder,
2851379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2852379bc100SJani Nikula {
2853379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2854379bc100SJani Nikula 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2855befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2856379bc100SJani Nikula 	u32 val;
2857379bc100SJani Nikula 
2858379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2859379bc100SJani Nikula 
2860befa372bSMatt Roper 	val = I915_READ(ICL_DPCLKA_CFGCR0);
2861befa372bSMatt Roper 	WARN_ON((val & icl_dpclka_cfgcr0_clk_off(dev_priv, phy)) == 0);
2862379bc100SJani Nikula 
2863befa372bSMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
2864befa372bSMatt Roper 		/*
2865befa372bSMatt Roper 		 * Even though this register references DDIs, note that we
2866befa372bSMatt Roper 		 * want to pass the PHY rather than the port (DDI).  For
2867befa372bSMatt Roper 		 * ICL, port=phy in all cases so it doesn't matter, but for
2868befa372bSMatt Roper 		 * EHL the bspec notes the following:
2869befa372bSMatt Roper 		 *
2870befa372bSMatt Roper 		 *   "DDID clock tied to DDIA clock, so DPCLKA_CFGCR0 DDIA
2871befa372bSMatt Roper 		 *   Clock Select chooses the PLL for both DDIA and DDID and
2872befa372bSMatt Roper 		 *   drives port A in all cases."
2873befa372bSMatt Roper 		 */
2874befa372bSMatt Roper 		val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
2875befa372bSMatt Roper 		val |= ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
2876befa372bSMatt Roper 		I915_WRITE(ICL_DPCLKA_CFGCR0, val);
2877befa372bSMatt Roper 		POSTING_READ(ICL_DPCLKA_CFGCR0);
2878379bc100SJani Nikula 	}
2879379bc100SJani Nikula 
2880befa372bSMatt Roper 	val &= ~icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2881befa372bSMatt Roper 	I915_WRITE(ICL_DPCLKA_CFGCR0, val);
2882379bc100SJani Nikula 
2883379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
2884379bc100SJani Nikula }
2885379bc100SJani Nikula 
2886379bc100SJani Nikula static void icl_unmap_plls_to_ports(struct intel_encoder *encoder)
2887379bc100SJani Nikula {
2888379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2889befa372bSMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2890379bc100SJani Nikula 	u32 val;
2891379bc100SJani Nikula 
2892379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2893379bc100SJani Nikula 
2894befa372bSMatt Roper 	val = I915_READ(ICL_DPCLKA_CFGCR0);
2895befa372bSMatt Roper 	val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2896befa372bSMatt Roper 	I915_WRITE(ICL_DPCLKA_CFGCR0, val);
2897379bc100SJani Nikula 
2898379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
2899379bc100SJani Nikula }
2900379bc100SJani Nikula 
2901379bc100SJani Nikula void icl_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
2902379bc100SJani Nikula {
2903379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2904379bc100SJani Nikula 	u32 val;
2905379bc100SJani Nikula 	enum port port;
2906379bc100SJani Nikula 	u32 port_mask;
2907379bc100SJani Nikula 	bool ddi_clk_needed;
2908379bc100SJani Nikula 
2909379bc100SJani Nikula 	/*
2910379bc100SJani Nikula 	 * In case of DP MST, we sanitize the primary encoder only, not the
2911379bc100SJani Nikula 	 * virtual ones.
2912379bc100SJani Nikula 	 */
2913379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DP_MST)
2914379bc100SJani Nikula 		return;
2915379bc100SJani Nikula 
2916379bc100SJani Nikula 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2917379bc100SJani Nikula 		u8 pipe_mask;
2918379bc100SJani Nikula 		bool is_mst;
2919379bc100SJani Nikula 
2920379bc100SJani Nikula 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2921379bc100SJani Nikula 		/*
2922379bc100SJani Nikula 		 * In the unlikely case that BIOS enables DP in MST mode, just
2923379bc100SJani Nikula 		 * warn since our MST HW readout is incomplete.
2924379bc100SJani Nikula 		 */
2925379bc100SJani Nikula 		if (WARN_ON(is_mst))
2926379bc100SJani Nikula 			return;
2927379bc100SJani Nikula 	}
2928379bc100SJani Nikula 
2929379bc100SJani Nikula 	port_mask = BIT(encoder->port);
2930379bc100SJani Nikula 	ddi_clk_needed = encoder->base.crtc;
2931379bc100SJani Nikula 
2932379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_DSI) {
2933379bc100SJani Nikula 		struct intel_encoder *other_encoder;
2934379bc100SJani Nikula 
2935379bc100SJani Nikula 		port_mask = intel_dsi_encoder_ports(encoder);
2936379bc100SJani Nikula 		/*
2937379bc100SJani Nikula 		 * Sanity check that we haven't incorrectly registered another
2938379bc100SJani Nikula 		 * encoder using any of the ports of this DSI encoder.
2939379bc100SJani Nikula 		 */
2940379bc100SJani Nikula 		for_each_intel_encoder(&dev_priv->drm, other_encoder) {
2941379bc100SJani Nikula 			if (other_encoder == encoder)
2942379bc100SJani Nikula 				continue;
2943379bc100SJani Nikula 
2944379bc100SJani Nikula 			if (WARN_ON(port_mask & BIT(other_encoder->port)))
2945379bc100SJani Nikula 				return;
2946379bc100SJani Nikula 		}
2947379bc100SJani Nikula 		/*
2948379bc100SJani Nikula 		 * For DSI we keep the ddi clocks gated
2949379bc100SJani Nikula 		 * except during enable/disable sequence.
2950379bc100SJani Nikula 		 */
2951379bc100SJani Nikula 		ddi_clk_needed = false;
2952379bc100SJani Nikula 	}
2953379bc100SJani Nikula 
2954befa372bSMatt Roper 	val = I915_READ(ICL_DPCLKA_CFGCR0);
2955379bc100SJani Nikula 	for_each_port_masked(port, port_mask) {
2956befa372bSMatt Roper 		enum phy phy = intel_port_to_phy(dev_priv, port);
2957befa372bSMatt Roper 
2958379bc100SJani Nikula 		bool ddi_clk_ungated = !(val &
2959379bc100SJani Nikula 					 icl_dpclka_cfgcr0_clk_off(dev_priv,
2960befa372bSMatt Roper 								   phy));
2961379bc100SJani Nikula 
2962379bc100SJani Nikula 		if (ddi_clk_needed == ddi_clk_ungated)
2963379bc100SJani Nikula 			continue;
2964379bc100SJani Nikula 
2965379bc100SJani Nikula 		/*
2966379bc100SJani Nikula 		 * Punt on the case now where clock is gated, but it would
2967379bc100SJani Nikula 		 * be needed by the port. Something else is really broken then.
2968379bc100SJani Nikula 		 */
2969379bc100SJani Nikula 		if (WARN_ON(ddi_clk_needed))
2970379bc100SJani Nikula 			continue;
2971379bc100SJani Nikula 
2972befa372bSMatt Roper 		DRM_NOTE("PHY %c is disabled/in DSI mode with an ungated DDI clock, gate it\n",
2973befa372bSMatt Roper 			 phy_name(port));
2974befa372bSMatt Roper 		val |= icl_dpclka_cfgcr0_clk_off(dev_priv, phy);
2975befa372bSMatt Roper 		I915_WRITE(ICL_DPCLKA_CFGCR0, val);
2976379bc100SJani Nikula 	}
2977379bc100SJani Nikula }
2978379bc100SJani Nikula 
2979379bc100SJani Nikula static void intel_ddi_clk_select(struct intel_encoder *encoder,
2980379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2981379bc100SJani Nikula {
2982379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2983379bc100SJani Nikula 	enum port port = encoder->port;
2984d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2985379bc100SJani Nikula 	u32 val;
2986379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
2987379bc100SJani Nikula 
2988379bc100SJani Nikula 	if (WARN_ON(!pll))
2989379bc100SJani Nikula 		return;
2990379bc100SJani Nikula 
2991379bc100SJani Nikula 	mutex_lock(&dev_priv->dpll_lock);
2992379bc100SJani Nikula 
2993379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
2994d8fe2ab6SMatt Roper 		if (!intel_phy_is_combo(dev_priv, phy))
2995379bc100SJani Nikula 			I915_WRITE(DDI_CLK_SEL(port),
2996379bc100SJani Nikula 				   icl_pll_to_ddi_clk_sel(encoder, crtc_state));
2997c2052d6eSJosé Roberto de Souza 		else if (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C)
2998c2052d6eSJosé Roberto de Souza 			/*
2999c2052d6eSJosé Roberto de Souza 			 * MG does not exist but the programming is required
3000c2052d6eSJosé Roberto de Souza 			 * to ungate DDIC and DDID
3001c2052d6eSJosé Roberto de Souza 			 */
3002c2052d6eSJosé Roberto de Souza 			I915_WRITE(DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
3003379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
3004379bc100SJani Nikula 		/* Configure DPCLKA_CFGCR0 to map the DPLL to the DDI. */
3005379bc100SJani Nikula 		val = I915_READ(DPCLKA_CFGCR0);
3006379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(port);
3007379bc100SJani Nikula 		val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, port);
3008379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, val);
3009379bc100SJani Nikula 
3010379bc100SJani Nikula 		/*
3011379bc100SJani Nikula 		 * Configure DPCLKA_CFGCR0 to turn on the clock for the DDI.
3012379bc100SJani Nikula 		 * This step and the step before must be done with separate
3013379bc100SJani Nikula 		 * register writes.
3014379bc100SJani Nikula 		 */
3015379bc100SJani Nikula 		val = I915_READ(DPCLKA_CFGCR0);
3016379bc100SJani Nikula 		val &= ~DPCLKA_CFGCR0_DDI_CLK_OFF(port);
3017379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, val);
3018379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3019379bc100SJani Nikula 		/* DDI -> PLL mapping  */
3020379bc100SJani Nikula 		val = I915_READ(DPLL_CTRL2);
3021379bc100SJani Nikula 
3022379bc100SJani Nikula 		val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
3023379bc100SJani Nikula 			 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
3024379bc100SJani Nikula 		val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
3025379bc100SJani Nikula 			DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
3026379bc100SJani Nikula 
3027379bc100SJani Nikula 		I915_WRITE(DPLL_CTRL2, val);
3028379bc100SJani Nikula 
3029379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3030379bc100SJani Nikula 		I915_WRITE(PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
3031379bc100SJani Nikula 	}
3032379bc100SJani Nikula 
3033379bc100SJani Nikula 	mutex_unlock(&dev_priv->dpll_lock);
3034379bc100SJani Nikula }
3035379bc100SJani Nikula 
3036379bc100SJani Nikula static void intel_ddi_clk_disable(struct intel_encoder *encoder)
3037379bc100SJani Nikula {
3038379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3039379bc100SJani Nikula 	enum port port = encoder->port;
3040d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3041379bc100SJani Nikula 
3042379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11) {
3043c2052d6eSJosé Roberto de Souza 		if (!intel_phy_is_combo(dev_priv, phy) ||
3044c2052d6eSJosé Roberto de Souza 		    (IS_ELKHARTLAKE(dev_priv) && port >= PORT_C))
3045379bc100SJani Nikula 			I915_WRITE(DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
3046379bc100SJani Nikula 	} else if (IS_CANNONLAKE(dev_priv)) {
3047379bc100SJani Nikula 		I915_WRITE(DPCLKA_CFGCR0, I915_READ(DPCLKA_CFGCR0) |
3048379bc100SJani Nikula 			   DPCLKA_CFGCR0_DDI_CLK_OFF(port));
3049379bc100SJani Nikula 	} else if (IS_GEN9_BC(dev_priv)) {
3050379bc100SJani Nikula 		I915_WRITE(DPLL_CTRL2, I915_READ(DPLL_CTRL2) |
3051379bc100SJani Nikula 			   DPLL_CTRL2_DDI_CLK_OFF(port));
3052379bc100SJani Nikula 	} else if (INTEL_GEN(dev_priv) < 9) {
3053379bc100SJani Nikula 		I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
3054379bc100SJani Nikula 	}
3055379bc100SJani Nikula }
3056379bc100SJani Nikula 
30578aaf5cbdSJosé Roberto de Souza static void
30588aaf5cbdSJosé Roberto de Souza icl_phy_set_clock_gating(struct intel_digital_port *dig_port, bool enable)
3059379bc100SJani Nikula {
3060379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
3061379bc100SJani Nikula 	enum port port = dig_port->base.port;
3062379bc100SJani Nikula 	enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
30638aaf5cbdSJosé Roberto de Souza 	u32 val, bits;
3064379bc100SJani Nikula 	int ln;
3065379bc100SJani Nikula 
3066379bc100SJani Nikula 	if (tc_port == PORT_TC_NONE)
3067379bc100SJani Nikula 		return;
3068379bc100SJani Nikula 
30698aaf5cbdSJosé Roberto de Souza 	bits = MG_DP_MODE_CFG_TR2PWR_GATING | MG_DP_MODE_CFG_TRPWR_GATING |
30708aaf5cbdSJosé Roberto de Souza 	       MG_DP_MODE_CFG_CLNPWR_GATING | MG_DP_MODE_CFG_DIGPWR_GATING |
3071379bc100SJani Nikula 	       MG_DP_MODE_CFG_GAONPWR_GATING;
3072379bc100SJani Nikula 
3073379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
3074379bc100SJani Nikula 		val = I915_READ(MG_DP_MODE(ln, port));
30758aaf5cbdSJosé Roberto de Souza 		if (enable)
30768aaf5cbdSJosé Roberto de Souza 			val |= bits;
30778aaf5cbdSJosé Roberto de Souza 		else
30788aaf5cbdSJosé Roberto de Souza 			val &= ~bits;
3079379bc100SJani Nikula 		I915_WRITE(MG_DP_MODE(ln, port), val);
3080379bc100SJani Nikula 	}
3081379bc100SJani Nikula 
30828aaf5cbdSJosé Roberto de Souza 	bits = MG_MISC_SUS0_CFG_TR2PWR_GATING | MG_MISC_SUS0_CFG_CL2PWR_GATING |
30838aaf5cbdSJosé Roberto de Souza 	       MG_MISC_SUS0_CFG_GAONPWR_GATING | MG_MISC_SUS0_CFG_TRPWR_GATING |
30848aaf5cbdSJosé Roberto de Souza 	       MG_MISC_SUS0_CFG_CL1PWR_GATING | MG_MISC_SUS0_CFG_DGPWR_GATING;
30858aaf5cbdSJosé Roberto de Souza 
3086379bc100SJani Nikula 	val = I915_READ(MG_MISC_SUS0(tc_port));
30878aaf5cbdSJosé Roberto de Souza 	if (enable)
30888aaf5cbdSJosé Roberto de Souza 		val |= (bits | MG_MISC_SUS0_SUSCLK_DYNCLKGATE_MODE(3));
30898aaf5cbdSJosé Roberto de Souza 	else
30908aaf5cbdSJosé Roberto de Souza 		val &= ~(bits | MG_MISC_SUS0_SUSCLK_DYNCLKGATE_MODE_MASK);
3091379bc100SJani Nikula 	I915_WRITE(MG_MISC_SUS0(tc_port), val);
3092379bc100SJani Nikula }
3093379bc100SJani Nikula 
3094*3b51be4eSClinton A Taylor static void
3095*3b51be4eSClinton A Taylor icl_program_mg_dp_mode(struct intel_digital_port *intel_dig_port,
3096*3b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
3097379bc100SJani Nikula {
3098379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
3099379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
3100*3b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
3101*3b51be4eSClinton A Taylor 	u8 width;
3102379bc100SJani Nikula 
3103e9b7e142SImre Deak 	if (intel_dig_port->tc_mode == TC_PORT_TBT_ALT)
3104379bc100SJani Nikula 		return;
3105379bc100SJani Nikula 
3106379bc100SJani Nikula 	ln0 = I915_READ(MG_DP_MODE(0, port));
3107379bc100SJani Nikula 	ln1 = I915_READ(MG_DP_MODE(1, port));
3108379bc100SJani Nikula 
3109*3b51be4eSClinton A Taylor 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X1_MODE);
3110379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
3111379bc100SJani Nikula 
3112*3b51be4eSClinton A Taylor 	/* DPPATC */
3113*3b51be4eSClinton A Taylor 	pin_assignment = intel_tc_port_get_pin_assignment_mask(intel_dig_port);
3114*3b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
3115379bc100SJani Nikula 
3116*3b51be4eSClinton A Taylor 	switch (pin_assignment) {
3117*3b51be4eSClinton A Taylor 	case 0x0:
3118*3b51be4eSClinton A Taylor 		WARN_ON(intel_dig_port->tc_mode != TC_PORT_LEGACY);
3119*3b51be4eSClinton A Taylor 		if (width == 1) {
3120379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
3121*3b51be4eSClinton A Taylor 		} else {
3122*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
3123*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3124379bc100SJani Nikula 		}
3125379bc100SJani Nikula 		break;
3126*3b51be4eSClinton A Taylor 	case 0x1:
3127*3b51be4eSClinton A Taylor 		if (width == 4) {
3128*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
3129*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3130*3b51be4eSClinton A Taylor 		}
3131379bc100SJani Nikula 		break;
3132*3b51be4eSClinton A Taylor 	case 0x2:
3133*3b51be4eSClinton A Taylor 		if (width == 2) {
3134*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
3135*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3136*3b51be4eSClinton A Taylor 		}
3137*3b51be4eSClinton A Taylor 		break;
3138*3b51be4eSClinton A Taylor 	case 0x3:
3139*3b51be4eSClinton A Taylor 	case 0x5:
3140*3b51be4eSClinton A Taylor 		if (width == 1) {
3141*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
3142*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
3143*3b51be4eSClinton A Taylor 		} else {
3144*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
3145*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3146*3b51be4eSClinton A Taylor 		}
3147*3b51be4eSClinton A Taylor 		break;
3148*3b51be4eSClinton A Taylor 	case 0x4:
3149*3b51be4eSClinton A Taylor 	case 0x6:
3150*3b51be4eSClinton A Taylor 		if (width == 1) {
3151*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
3152*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
3153*3b51be4eSClinton A Taylor 		} else {
3154*3b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
3155*3b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
3156*3b51be4eSClinton A Taylor 		}
3157*3b51be4eSClinton A Taylor 		break;
3158379bc100SJani Nikula 	default:
3159*3b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
3160379bc100SJani Nikula 	}
3161379bc100SJani Nikula 
3162379bc100SJani Nikula 	I915_WRITE(MG_DP_MODE(0, port), ln0);
3163379bc100SJani Nikula 	I915_WRITE(MG_DP_MODE(1, port), ln1);
3164379bc100SJani Nikula }
3165379bc100SJani Nikula 
3166379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
3167379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3168379bc100SJani Nikula {
3169379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3170379bc100SJani Nikula 		return;
3171379bc100SJani Nikula 
3172379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
3173379bc100SJani Nikula 		DRM_DEBUG_KMS("Failed to set FEC_READY in the sink\n");
3174379bc100SJani Nikula }
3175379bc100SJani Nikula 
3176379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
3177379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
3178379bc100SJani Nikula {
3179379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
31804444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3181379bc100SJani Nikula 	u32 val;
3182379bc100SJani Nikula 
3183379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3184379bc100SJani Nikula 		return;
3185379bc100SJani Nikula 
31864444df6eSLucas De Marchi 	intel_dp = enc_to_intel_dp(&encoder->base);
31874444df6eSLucas De Marchi 	val = I915_READ(intel_dp->regs.dp_tp_ctl);
3188379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
31894444df6eSLucas De Marchi 	I915_WRITE(intel_dp->regs.dp_tp_ctl, val);
3190379bc100SJani Nikula 
31914444df6eSLucas De Marchi 	if (intel_de_wait_for_set(dev_priv, intel_dp->regs.dp_tp_status,
31924cb3b44dSDaniele Ceraolo Spurio 				  DP_TP_STATUS_FEC_ENABLE_LIVE, 1))
3193379bc100SJani Nikula 		DRM_ERROR("Timed out waiting for FEC Enable Status\n");
3194379bc100SJani Nikula }
3195379bc100SJani Nikula 
3196379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
3197379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
3198379bc100SJani Nikula {
3199379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
32004444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
3201379bc100SJani Nikula 	u32 val;
3202379bc100SJani Nikula 
3203379bc100SJani Nikula 	if (!crtc_state->fec_enable)
3204379bc100SJani Nikula 		return;
3205379bc100SJani Nikula 
32064444df6eSLucas De Marchi 	intel_dp = enc_to_intel_dp(&encoder->base);
32074444df6eSLucas De Marchi 	val = I915_READ(intel_dp->regs.dp_tp_ctl);
3208379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
32094444df6eSLucas De Marchi 	I915_WRITE(intel_dp->regs.dp_tp_ctl, val);
32104444df6eSLucas De Marchi 	POSTING_READ(intel_dp->regs.dp_tp_ctl);
3211379bc100SJani Nikula }
3212379bc100SJani Nikula 
321399389390SJosé Roberto de Souza static void tgl_ddi_pre_enable_dp(struct intel_encoder *encoder,
321499389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
321599389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
321699389390SJosé Roberto de Souza {
321799389390SJosé Roberto de Souza 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
321899389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
321999389390SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
322099389390SJosé Roberto de Souza 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
322199389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
322299389390SJosé Roberto de Souza 	int level = intel_ddi_dp_level(intel_dp);
32234444df6eSLucas De Marchi 	enum transcoder transcoder = crtc_state->cpu_transcoder;
322499389390SJosé Roberto de Souza 
322599389390SJosé Roberto de Souza 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
322699389390SJosé Roberto de Souza 				 crtc_state->lane_count, is_mst);
322799389390SJosé Roberto de Souza 
32284444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_ctl = TGL_DP_TP_CTL(transcoder);
32294444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_status = TGL_DP_TP_STATUS(transcoder);
32304444df6eSLucas De Marchi 
323199389390SJosé Roberto de Souza 	/* 1.a got on intel_atomic_commit_tail() */
323299389390SJosé Roberto de Souza 
323399389390SJosé Roberto de Souza 	/* 2. */
323499389390SJosé Roberto de Souza 	intel_edp_panel_on(intel_dp);
323599389390SJosé Roberto de Souza 
323699389390SJosé Roberto de Souza 	/*
32376171e58bSClinton A Taylor 	 * 1.b, 3. and 4.a is done before tgl_ddi_pre_enable_dp() by:
323899389390SJosé Roberto de Souza 	 * haswell_crtc_enable()->intel_encoders_pre_pll_enable() and
323999389390SJosé Roberto de Souza 	 * haswell_crtc_enable()->intel_enable_shared_dpll()
324099389390SJosé Roberto de Souza 	 */
324199389390SJosé Roberto de Souza 
32426171e58bSClinton A Taylor 	/* 4.b */
32436171e58bSClinton A Taylor 	intel_ddi_clk_select(encoder, crtc_state);
32446171e58bSClinton A Taylor 
324599389390SJosé Roberto de Souza 	/* 5. */
324699389390SJosé Roberto de Souza 	if (!intel_phy_is_tc(dev_priv, phy) ||
324799389390SJosé Roberto de Souza 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
324899389390SJosé Roberto de Souza 		intel_display_power_get(dev_priv,
324999389390SJosé Roberto de Souza 					dig_port->ddi_io_power_domain);
325099389390SJosé Roberto de Souza 
325199389390SJosé Roberto de Souza 	/* 6. */
3252*3b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
325399389390SJosé Roberto de Souza 
325499389390SJosé Roberto de Souza 	/*
325599389390SJosé Roberto de Souza 	 * 7.a - Steps in this function should only be executed over MST
325699389390SJosé Roberto de Souza 	 * master, what will be taken in care by MST hook
325799389390SJosé Roberto de Souza 	 * intel_mst_pre_enable_dp()
325899389390SJosé Roberto de Souza 	 */
325999389390SJosé Roberto de Souza 	intel_ddi_enable_pipe_clock(crtc_state);
326099389390SJosé Roberto de Souza 
326199389390SJosé Roberto de Souza 	/* 7.b */
326299389390SJosé Roberto de Souza 	intel_ddi_config_transcoder_func(crtc_state);
326399389390SJosé Roberto de Souza 
326499389390SJosé Roberto de Souza 	/* 7.d */
32658aaf5cbdSJosé Roberto de Souza 	icl_phy_set_clock_gating(dig_port, false);
326699389390SJosé Roberto de Souza 
326799389390SJosé Roberto de Souza 	/* 7.e */
326899389390SJosé Roberto de Souza 	icl_ddi_vswing_sequence(encoder, crtc_state->port_clock, level,
326999389390SJosé Roberto de Souza 				encoder->type);
327099389390SJosé Roberto de Souza 
327199389390SJosé Roberto de Souza 	/* 7.f */
327299389390SJosé Roberto de Souza 	if (intel_phy_is_combo(dev_priv, phy)) {
327399389390SJosé Roberto de Souza 		bool lane_reversal =
327499389390SJosé Roberto de Souza 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
327599389390SJosé Roberto de Souza 
327699389390SJosé Roberto de Souza 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
327799389390SJosé Roberto de Souza 					       crtc_state->lane_count,
327899389390SJosé Roberto de Souza 					       lane_reversal);
327999389390SJosé Roberto de Souza 	}
328099389390SJosé Roberto de Souza 
328199389390SJosé Roberto de Souza 	/* 7.g */
328299389390SJosé Roberto de Souza 	intel_ddi_init_dp_buf_reg(encoder);
328399389390SJosé Roberto de Souza 
328499389390SJosé Roberto de Souza 	if (!is_mst)
328599389390SJosé Roberto de Souza 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
328699389390SJosé Roberto de Souza 
328799389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
328899389390SJosé Roberto de Souza 	/*
328999389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
329099389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
329199389390SJosé Roberto de Souza 	 * training
329299389390SJosé Roberto de Souza 	 */
329399389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
329499389390SJosé Roberto de Souza 	/* 7.c, 7.h, 7.i, 7.j */
329599389390SJosé Roberto de Souza 	intel_dp_start_link_train(intel_dp);
329699389390SJosé Roberto de Souza 
329799389390SJosé Roberto de Souza 	/* 7.k */
329899389390SJosé Roberto de Souza 	intel_dp_stop_link_train(intel_dp);
329999389390SJosé Roberto de Souza 
330099389390SJosé Roberto de Souza 	/* 7.l */
330199389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
330299389390SJosé Roberto de Souza 	intel_dsc_enable(encoder, crtc_state);
330399389390SJosé Roberto de Souza }
330499389390SJosé Roberto de Souza 
330599389390SJosé Roberto de Souza static void hsw_ddi_pre_enable_dp(struct intel_encoder *encoder,
3306379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3307379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3308379bc100SJani Nikula {
3309379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3310379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3311379bc100SJani Nikula 	enum port port = encoder->port;
3312dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
3313379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3314379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
3315379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
3316379bc100SJani Nikula 
3317379bc100SJani Nikula 	WARN_ON(is_mst && (port == PORT_A || port == PORT_E));
3318379bc100SJani Nikula 
3319379bc100SJani Nikula 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
3320379bc100SJani Nikula 				 crtc_state->lane_count, is_mst);
3321379bc100SJani Nikula 
33224444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_ctl = DP_TP_CTL(port);
33234444df6eSLucas De Marchi 	intel_dp->regs.dp_tp_status = DP_TP_STATUS(port);
33244444df6eSLucas De Marchi 
3325379bc100SJani Nikula 	intel_edp_panel_on(intel_dp);
3326379bc100SJani Nikula 
3327379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3328379bc100SJani Nikula 
3329d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
33303b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
33313b2ed431SImre Deak 		intel_display_power_get(dev_priv,
33323b2ed431SImre Deak 					dig_port->ddi_io_power_domain);
3333379bc100SJani Nikula 
3334*3b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
33358aaf5cbdSJosé Roberto de Souza 	icl_phy_set_clock_gating(dig_port, false);
3336379bc100SJani Nikula 
3337379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3338379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3339379bc100SJani Nikula 					level, encoder->type);
3340379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3341379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, encoder->type);
3342379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3343379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, encoder->type);
3344379bc100SJani Nikula 	else
3345379bc100SJani Nikula 		intel_prepare_dp_ddi_buffers(encoder, crtc_state);
3346379bc100SJani Nikula 
3347d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy)) {
3348379bc100SJani Nikula 		bool lane_reversal =
3349379bc100SJani Nikula 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
3350379bc100SJani Nikula 
3351dc867bc7SMatt Roper 		intel_combo_phy_power_up_lanes(dev_priv, phy, false,
3352379bc100SJani Nikula 					       crtc_state->lane_count,
3353379bc100SJani Nikula 					       lane_reversal);
3354379bc100SJani Nikula 	}
3355379bc100SJani Nikula 
3356379bc100SJani Nikula 	intel_ddi_init_dp_buf_reg(encoder);
3357379bc100SJani Nikula 	if (!is_mst)
3358379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
3359379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
3360379bc100SJani Nikula 					      true);
3361379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
3362379bc100SJani Nikula 	intel_dp_start_link_train(intel_dp);
3363379bc100SJani Nikula 	if (port != PORT_A || INTEL_GEN(dev_priv) >= 9)
3364379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3365379bc100SJani Nikula 
3366379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
3367379bc100SJani Nikula 
33688aaf5cbdSJosé Roberto de Souza 	icl_phy_set_clock_gating(dig_port, true);
3369379bc100SJani Nikula 
3370379bc100SJani Nikula 	if (!is_mst)
3371379bc100SJani Nikula 		intel_ddi_enable_pipe_clock(crtc_state);
3372379bc100SJani Nikula 
3373379bc100SJani Nikula 	intel_dsc_enable(encoder, crtc_state);
3374379bc100SJani Nikula }
3375379bc100SJani Nikula 
337699389390SJosé Roberto de Souza static void intel_ddi_pre_enable_dp(struct intel_encoder *encoder,
337799389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
337899389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
337999389390SJosé Roberto de Souza {
338099389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
338199389390SJosé Roberto de Souza 
338299389390SJosé Roberto de Souza 	if (INTEL_GEN(dev_priv) >= 12)
338399389390SJosé Roberto de Souza 		tgl_ddi_pre_enable_dp(encoder, crtc_state, conn_state);
338499389390SJosé Roberto de Souza 	else
338599389390SJosé Roberto de Souza 		hsw_ddi_pre_enable_dp(encoder, crtc_state, conn_state);
338699389390SJosé Roberto de Souza }
338799389390SJosé Roberto de Souza 
3388379bc100SJani Nikula static void intel_ddi_pre_enable_hdmi(struct intel_encoder *encoder,
3389379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
3390379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
3391379bc100SJani Nikula {
3392379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
3393379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
3394379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3395379bc100SJani Nikula 	enum port port = encoder->port;
3396379bc100SJani Nikula 	int level = intel_ddi_hdmi_level(dev_priv, port);
3397379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3398379bc100SJani Nikula 
3399379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
3400379bc100SJani Nikula 	intel_ddi_clk_select(encoder, crtc_state);
3401379bc100SJani Nikula 
3402379bc100SJani Nikula 	intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
3403379bc100SJani Nikula 
3404*3b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
34058aaf5cbdSJosé Roberto de Souza 	icl_phy_set_clock_gating(dig_port, false);
3406379bc100SJani Nikula 
3407379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3408379bc100SJani Nikula 		icl_ddi_vswing_sequence(encoder, crtc_state->port_clock,
3409379bc100SJani Nikula 					level, INTEL_OUTPUT_HDMI);
3410379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv))
3411379bc100SJani Nikula 		cnl_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3412379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
3413379bc100SJani Nikula 		bxt_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
3414379bc100SJani Nikula 	else
3415379bc100SJani Nikula 		intel_prepare_hdmi_ddi_buffers(encoder, level);
3416379bc100SJani Nikula 
34178aaf5cbdSJosé Roberto de Souza 	icl_phy_set_clock_gating(dig_port, true);
3418379bc100SJani Nikula 
3419379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv))
3420379bc100SJani Nikula 		skl_ddi_set_iboost(encoder, level, INTEL_OUTPUT_HDMI);
3421379bc100SJani Nikula 
3422379bc100SJani Nikula 	intel_ddi_enable_pipe_clock(crtc_state);
3423379bc100SJani Nikula 
3424379bc100SJani Nikula 	intel_dig_port->set_infoframes(encoder,
3425379bc100SJani Nikula 				       crtc_state->has_infoframe,
3426379bc100SJani Nikula 				       crtc_state, conn_state);
3427379bc100SJani Nikula }
3428379bc100SJani Nikula 
3429379bc100SJani Nikula static void intel_ddi_pre_enable(struct intel_encoder *encoder,
3430379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
3431379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
3432379bc100SJani Nikula {
3433379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
3434379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3435379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
3436379bc100SJani Nikula 
3437379bc100SJani Nikula 	/*
3438379bc100SJani Nikula 	 * When called from DP MST code:
3439379bc100SJani Nikula 	 * - conn_state will be NULL
3440379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3441379bc100SJani Nikula 	 * - the main connector associated with this port
3442379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3443379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
3444379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
3445379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
3446379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3447379bc100SJani Nikula 	 *   the DP link parameteres
3448379bc100SJani Nikula 	 */
3449379bc100SJani Nikula 
3450379bc100SJani Nikula 	WARN_ON(crtc_state->has_pch_encoder);
3451379bc100SJani Nikula 
3452379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3453379bc100SJani Nikula 		icl_map_plls_to_ports(encoder, crtc_state);
3454379bc100SJani Nikula 
3455379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3456379bc100SJani Nikula 
3457379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
3458379bc100SJani Nikula 		intel_ddi_pre_enable_hdmi(encoder, crtc_state, conn_state);
3459379bc100SJani Nikula 	} else {
3460379bc100SJani Nikula 		struct intel_lspcon *lspcon =
3461379bc100SJani Nikula 				enc_to_intel_lspcon(&encoder->base);
3462379bc100SJani Nikula 
3463379bc100SJani Nikula 		intel_ddi_pre_enable_dp(encoder, crtc_state, conn_state);
3464379bc100SJani Nikula 		if (lspcon->active) {
3465379bc100SJani Nikula 			struct intel_digital_port *dig_port =
3466379bc100SJani Nikula 					enc_to_dig_port(&encoder->base);
3467379bc100SJani Nikula 
3468379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
3469379bc100SJani Nikula 						 crtc_state->has_infoframe,
3470379bc100SJani Nikula 						 crtc_state, conn_state);
3471379bc100SJani Nikula 		}
3472379bc100SJani Nikula 	}
3473379bc100SJani Nikula }
3474379bc100SJani Nikula 
3475379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
3476379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
3477379bc100SJani Nikula {
3478379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3479379bc100SJani Nikula 	enum port port = encoder->port;
3480379bc100SJani Nikula 	bool wait = false;
3481379bc100SJani Nikula 	u32 val;
3482379bc100SJani Nikula 
3483379bc100SJani Nikula 	val = I915_READ(DDI_BUF_CTL(port));
3484379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
3485379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
3486379bc100SJani Nikula 		I915_WRITE(DDI_BUF_CTL(port), val);
3487379bc100SJani Nikula 		wait = true;
3488379bc100SJani Nikula 	}
3489379bc100SJani Nikula 
3490e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
34914444df6eSLucas De Marchi 		struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
34924444df6eSLucas De Marchi 
34934444df6eSLucas De Marchi 		val = I915_READ(intel_dp->regs.dp_tp_ctl);
3494379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3495379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
34964444df6eSLucas De Marchi 		I915_WRITE(intel_dp->regs.dp_tp_ctl, val);
3497e468ff06SLucas De Marchi 	}
3498379bc100SJani Nikula 
3499379bc100SJani Nikula 	/* Disable FEC in DP Sink */
3500379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
3501379bc100SJani Nikula 
3502379bc100SJani Nikula 	if (wait)
3503379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
3504379bc100SJani Nikula }
3505379bc100SJani Nikula 
3506379bc100SJani Nikula static void intel_ddi_post_disable_dp(struct intel_encoder *encoder,
3507379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
3508379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
3509379bc100SJani Nikula {
3510379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3511379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3512379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
3513379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
3514379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
3515d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3516379bc100SJani Nikula 
3517379bc100SJani Nikula 	if (!is_mst) {
3518379bc100SJani Nikula 		intel_ddi_disable_pipe_clock(old_crtc_state);
3519379bc100SJani Nikula 		/*
3520379bc100SJani Nikula 		 * Power down sink before disabling the port, otherwise we end
3521379bc100SJani Nikula 		 * up getting interrupts from the sink on detecting link loss.
3522379bc100SJani Nikula 		 */
3523379bc100SJani Nikula 		intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
3524379bc100SJani Nikula 	}
3525379bc100SJani Nikula 
3526379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3527379bc100SJani Nikula 
3528379bc100SJani Nikula 	intel_edp_panel_vdd_on(intel_dp);
3529379bc100SJani Nikula 	intel_edp_panel_off(intel_dp);
3530379bc100SJani Nikula 
3531d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
35323b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
3533379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3534379bc100SJani Nikula 						  dig_port->ddi_io_power_domain);
3535379bc100SJani Nikula 
3536379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3537379bc100SJani Nikula }
3538379bc100SJani Nikula 
3539379bc100SJani Nikula static void intel_ddi_post_disable_hdmi(struct intel_encoder *encoder,
3540379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
3541379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
3542379bc100SJani Nikula {
3543379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3544379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3545379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
3546379bc100SJani Nikula 
3547379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
3548379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
3549379bc100SJani Nikula 
3550379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
3551379bc100SJani Nikula 
3552379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3553379bc100SJani Nikula 
3554379bc100SJani Nikula 	intel_display_power_put_unchecked(dev_priv,
3555379bc100SJani Nikula 					  dig_port->ddi_io_power_domain);
3556379bc100SJani Nikula 
3557379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3558379bc100SJani Nikula 
3559379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
3560379bc100SJani Nikula }
3561379bc100SJani Nikula 
3562379bc100SJani Nikula static void intel_ddi_post_disable(struct intel_encoder *encoder,
3563379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3564379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3565379bc100SJani Nikula {
3566379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3567379bc100SJani Nikula 
3568379bc100SJani Nikula 	/*
3569379bc100SJani Nikula 	 * When called from DP MST code:
3570379bc100SJani Nikula 	 * - old_conn_state will be NULL
3571379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
3572379bc100SJani Nikula 	 * - the main connector associated with this port
3573379bc100SJani Nikula 	 *   won't be active or linked to a crtc
3574379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
3575379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
3576379bc100SJani Nikula 	 *   stream that was activated last, but each stream
3577379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
3578379bc100SJani Nikula 	 *   the DP link parameteres
3579379bc100SJani Nikula 	 */
3580379bc100SJani Nikula 
3581379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3582379bc100SJani Nikula 		intel_ddi_post_disable_hdmi(encoder,
3583379bc100SJani Nikula 					    old_crtc_state, old_conn_state);
3584379bc100SJani Nikula 	else
3585379bc100SJani Nikula 		intel_ddi_post_disable_dp(encoder,
3586379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3587379bc100SJani Nikula 
3588379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
3589379bc100SJani Nikula 		icl_unmap_plls_to_ports(encoder);
3590379bc100SJani Nikula }
3591379bc100SJani Nikula 
3592379bc100SJani Nikula void intel_ddi_fdi_post_disable(struct intel_encoder *encoder,
3593379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
3594379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
3595379bc100SJani Nikula {
3596379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3597379bc100SJani Nikula 	u32 val;
3598379bc100SJani Nikula 
3599379bc100SJani Nikula 	/*
3600379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
3601379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
3602379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
3603379bc100SJani Nikula 	 * originally before the BUN.
3604379bc100SJani Nikula 	 */
3605379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3606379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
3607379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3608379bc100SJani Nikula 
3609379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
3610379bc100SJani Nikula 	intel_ddi_clk_disable(encoder);
3611379bc100SJani Nikula 
3612379bc100SJani Nikula 	val = I915_READ(FDI_RX_MISC(PIPE_A));
3613379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
3614379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
3615379bc100SJani Nikula 	I915_WRITE(FDI_RX_MISC(PIPE_A), val);
3616379bc100SJani Nikula 
3617379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3618379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
3619379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3620379bc100SJani Nikula 
3621379bc100SJani Nikula 	val = I915_READ(FDI_RX_CTL(PIPE_A));
3622379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
3623379bc100SJani Nikula 	I915_WRITE(FDI_RX_CTL(PIPE_A), val);
3624379bc100SJani Nikula }
3625379bc100SJani Nikula 
3626379bc100SJani Nikula static void intel_enable_ddi_dp(struct intel_encoder *encoder,
3627379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
3628379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
3629379bc100SJani Nikula {
3630379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3631379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3632379bc100SJani Nikula 	enum port port = encoder->port;
3633379bc100SJani Nikula 
3634379bc100SJani Nikula 	if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
3635379bc100SJani Nikula 		intel_dp_stop_link_train(intel_dp);
3636379bc100SJani Nikula 
3637379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
3638379bc100SJani Nikula 	intel_psr_enable(intel_dp, crtc_state);
3639379bc100SJani Nikula 	intel_dp_ycbcr_420_enable(intel_dp, crtc_state);
3640379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3641379bc100SJani Nikula 
3642379bc100SJani Nikula 	if (crtc_state->has_audio)
3643379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3644379bc100SJani Nikula }
3645379bc100SJani Nikula 
3646379bc100SJani Nikula static i915_reg_t
3647379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3648379bc100SJani Nikula 			       enum port port)
3649379bc100SJani Nikula {
3650379bc100SJani Nikula 	static const i915_reg_t regs[] = {
3651379bc100SJani Nikula 		[PORT_A] = CHICKEN_TRANS_EDP,
3652379bc100SJani Nikula 		[PORT_B] = CHICKEN_TRANS_A,
3653379bc100SJani Nikula 		[PORT_C] = CHICKEN_TRANS_B,
3654379bc100SJani Nikula 		[PORT_D] = CHICKEN_TRANS_C,
3655379bc100SJani Nikula 		[PORT_E] = CHICKEN_TRANS_A,
3656379bc100SJani Nikula 	};
3657379bc100SJani Nikula 
3658379bc100SJani Nikula 	WARN_ON(INTEL_GEN(dev_priv) < 9);
3659379bc100SJani Nikula 
3660379bc100SJani Nikula 	if (WARN_ON(port < PORT_A || port > PORT_E))
3661379bc100SJani Nikula 		port = PORT_A;
3662379bc100SJani Nikula 
3663379bc100SJani Nikula 	return regs[port];
3664379bc100SJani Nikula }
3665379bc100SJani Nikula 
3666379bc100SJani Nikula static void intel_enable_ddi_hdmi(struct intel_encoder *encoder,
3667379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3668379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3669379bc100SJani Nikula {
3670379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3671379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3672379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
3673379bc100SJani Nikula 	enum port port = encoder->port;
3674379bc100SJani Nikula 
3675379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3676379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3677379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
3678379bc100SJani Nikula 		DRM_ERROR("[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3679379bc100SJani Nikula 			  connector->base.id, connector->name);
3680379bc100SJani Nikula 
3681379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
3682379bc100SJani Nikula 	if (IS_GEN9_BC(dev_priv)) {
3683379bc100SJani Nikula 		/*
3684379bc100SJani Nikula 		 * For some reason these chicken bits have been
3685379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3686379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3687379bc100SJani Nikula 		 * a specific transcoder.
3688379bc100SJani Nikula 		 */
3689379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3690379bc100SJani Nikula 		u32 val;
3691379bc100SJani Nikula 
3692379bc100SJani Nikula 		val = I915_READ(reg);
3693379bc100SJani Nikula 
3694379bc100SJani Nikula 		if (port == PORT_E)
3695379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3696379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3697379bc100SJani Nikula 		else
3698379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3699379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3700379bc100SJani Nikula 
3701379bc100SJani Nikula 		I915_WRITE(reg, val);
3702379bc100SJani Nikula 		POSTING_READ(reg);
3703379bc100SJani Nikula 
3704379bc100SJani Nikula 		udelay(1);
3705379bc100SJani Nikula 
3706379bc100SJani Nikula 		if (port == PORT_E)
3707379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3708379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3709379bc100SJani Nikula 		else
3710379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3711379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3712379bc100SJani Nikula 
3713379bc100SJani Nikula 		I915_WRITE(reg, val);
3714379bc100SJani Nikula 	}
3715379bc100SJani Nikula 
3716379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3717379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3718379bc100SJani Nikula 	 * enabling the port.
3719379bc100SJani Nikula 	 */
3720379bc100SJani Nikula 	I915_WRITE(DDI_BUF_CTL(port),
3721379bc100SJani Nikula 		   dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3722379bc100SJani Nikula 
3723379bc100SJani Nikula 	if (crtc_state->has_audio)
3724379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3725379bc100SJani Nikula }
3726379bc100SJani Nikula 
3727379bc100SJani Nikula static void intel_enable_ddi(struct intel_encoder *encoder,
3728379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3729379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3730379bc100SJani Nikula {
3731379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3732379bc100SJani Nikula 		intel_enable_ddi_hdmi(encoder, crtc_state, conn_state);
3733379bc100SJani Nikula 	else
3734379bc100SJani Nikula 		intel_enable_ddi_dp(encoder, crtc_state, conn_state);
3735379bc100SJani Nikula 
3736379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3737379bc100SJani Nikula 	if (conn_state->content_protection ==
3738379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3739d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
3740d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3741379bc100SJani Nikula }
3742379bc100SJani Nikula 
3743379bc100SJani Nikula static void intel_disable_ddi_dp(struct intel_encoder *encoder,
3744379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3745379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3746379bc100SJani Nikula {
3747379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3748379bc100SJani Nikula 
3749379bc100SJani Nikula 	intel_dp->link_trained = false;
3750379bc100SJani Nikula 
3751379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3752379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3753379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3754379bc100SJani Nikula 
3755379bc100SJani Nikula 	intel_edp_drrs_disable(intel_dp, old_crtc_state);
3756379bc100SJani Nikula 	intel_psr_disable(intel_dp, old_crtc_state);
3757379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3758379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3759379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3760379bc100SJani Nikula 					      false);
3761379bc100SJani Nikula }
3762379bc100SJani Nikula 
3763379bc100SJani Nikula static void intel_disable_ddi_hdmi(struct intel_encoder *encoder,
3764379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3765379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3766379bc100SJani Nikula {
3767379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3768379bc100SJani Nikula 
3769379bc100SJani Nikula 	if (old_crtc_state->has_audio)
3770379bc100SJani Nikula 		intel_audio_codec_disable(encoder,
3771379bc100SJani Nikula 					  old_crtc_state, old_conn_state);
3772379bc100SJani Nikula 
3773379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3774379bc100SJani Nikula 					       false, false))
3775379bc100SJani Nikula 		DRM_DEBUG_KMS("[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3776379bc100SJani Nikula 			      connector->base.id, connector->name);
3777379bc100SJani Nikula }
3778379bc100SJani Nikula 
3779379bc100SJani Nikula static void intel_disable_ddi(struct intel_encoder *encoder,
3780379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3781379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3782379bc100SJani Nikula {
3783379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3784379bc100SJani Nikula 
3785379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3786379bc100SJani Nikula 		intel_disable_ddi_hdmi(encoder, old_crtc_state, old_conn_state);
3787379bc100SJani Nikula 	else
3788379bc100SJani Nikula 		intel_disable_ddi_dp(encoder, old_crtc_state, old_conn_state);
3789379bc100SJani Nikula }
3790379bc100SJani Nikula 
3791379bc100SJani Nikula static void intel_ddi_update_pipe_dp(struct intel_encoder *encoder,
3792379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3793379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3794379bc100SJani Nikula {
3795379bc100SJani Nikula 	struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
3796379bc100SJani Nikula 
3797379bc100SJani Nikula 	intel_ddi_set_pipe_settings(crtc_state);
3798379bc100SJani Nikula 
3799379bc100SJani Nikula 	intel_psr_update(intel_dp, crtc_state);
3800379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3801379bc100SJani Nikula 
3802379bc100SJani Nikula 	intel_panel_update_backlight(encoder, crtc_state, conn_state);
3803379bc100SJani Nikula }
3804379bc100SJani Nikula 
3805379bc100SJani Nikula static void intel_ddi_update_pipe(struct intel_encoder *encoder,
3806379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3807379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3808379bc100SJani Nikula {
3809d456512cSRamalingam C 	struct intel_connector *connector =
3810d456512cSRamalingam C 				to_intel_connector(conn_state->connector);
3811d456512cSRamalingam C 	struct intel_hdcp *hdcp = &connector->hdcp;
3812d456512cSRamalingam C 	bool content_protection_type_changed =
3813d456512cSRamalingam C 			(conn_state->hdcp_content_type != hdcp->content_type &&
3814d456512cSRamalingam C 			 conn_state->content_protection !=
3815d456512cSRamalingam C 			 DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
3816d456512cSRamalingam C 
3817379bc100SJani Nikula 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3818379bc100SJani Nikula 		intel_ddi_update_pipe_dp(encoder, crtc_state, conn_state);
3819379bc100SJani Nikula 
3820d456512cSRamalingam C 	/*
3821d456512cSRamalingam C 	 * During the HDCP encryption session if Type change is requested,
3822d456512cSRamalingam C 	 * disable the HDCP and reenable it with new TYPE value.
3823d456512cSRamalingam C 	 */
3824379bc100SJani Nikula 	if (conn_state->content_protection ==
3825d456512cSRamalingam C 	    DRM_MODE_CONTENT_PROTECTION_UNDESIRED ||
3826d456512cSRamalingam C 	    content_protection_type_changed)
3827d456512cSRamalingam C 		intel_hdcp_disable(connector);
3828d456512cSRamalingam C 
3829d456512cSRamalingam C 	/*
3830d456512cSRamalingam C 	 * Mark the hdcp state as DESIRED after the hdcp disable of type
3831d456512cSRamalingam C 	 * change procedure.
3832d456512cSRamalingam C 	 */
3833d456512cSRamalingam C 	if (content_protection_type_changed) {
3834d456512cSRamalingam C 		mutex_lock(&hdcp->mutex);
3835d456512cSRamalingam C 		hdcp->value = DRM_MODE_CONTENT_PROTECTION_DESIRED;
3836d456512cSRamalingam C 		schedule_work(&hdcp->prop_work);
3837d456512cSRamalingam C 		mutex_unlock(&hdcp->mutex);
3838d456512cSRamalingam C 	}
3839d456512cSRamalingam C 
3840d456512cSRamalingam C 	if (conn_state->content_protection ==
3841d456512cSRamalingam C 	    DRM_MODE_CONTENT_PROTECTION_DESIRED ||
3842d456512cSRamalingam C 	    content_protection_type_changed)
3843d456512cSRamalingam C 		intel_hdcp_enable(connector, (u8)conn_state->hdcp_content_type);
3844379bc100SJani Nikula }
3845379bc100SJani Nikula 
3846379bc100SJani Nikula static void
384724a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
384824a7bfe0SImre Deak 			 struct intel_encoder *encoder,
384924a7bfe0SImre Deak 			 struct intel_crtc *crtc)
385024a7bfe0SImre Deak {
385124a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
385224a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
385324a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
385424a7bfe0SImre Deak 
385524a7bfe0SImre Deak 	WARN_ON(crtc && crtc->active);
385624a7bfe0SImre Deak 
385724a7bfe0SImre Deak 	intel_tc_port_get_link(enc_to_dig_port(&encoder->base), required_lanes);
385824a7bfe0SImre Deak 	if (crtc_state && crtc_state->base.active)
385924a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
386024a7bfe0SImre Deak }
386124a7bfe0SImre Deak 
386224a7bfe0SImre Deak static void
386324a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
386424a7bfe0SImre Deak 			  struct intel_encoder *encoder,
386524a7bfe0SImre Deak 			  struct intel_crtc *crtc)
386624a7bfe0SImre Deak {
386724a7bfe0SImre Deak 	intel_tc_port_put_link(enc_to_dig_port(&encoder->base));
386824a7bfe0SImre Deak }
386924a7bfe0SImre Deak 
387024a7bfe0SImre Deak static void
3871379bc100SJani Nikula intel_ddi_pre_pll_enable(struct intel_encoder *encoder,
3872379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3873379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3874379bc100SJani Nikula {
3875379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3876379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3877d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3878d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3879379bc100SJani Nikula 
388024a7bfe0SImre Deak 	if (is_tc_port)
388124a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
388224a7bfe0SImre Deak 
388324a7bfe0SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port)
3884379bc100SJani Nikula 		intel_display_power_get(dev_priv,
3885379bc100SJani Nikula 					intel_ddi_main_link_aux_domain(dig_port));
3886379bc100SJani Nikula 
38879d44dcb9SLucas De Marchi 	if (is_tc_port && dig_port->tc_mode != TC_PORT_TBT_ALT)
38889d44dcb9SLucas De Marchi 		/*
38899d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
38909d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
38919d44dcb9SLucas De Marchi 		 */
38929d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
38939d44dcb9SLucas De Marchi 	else if (IS_GEN9_LP(dev_priv))
3894379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3895379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3896379bc100SJani Nikula }
3897379bc100SJani Nikula 
3898379bc100SJani Nikula static void
3899379bc100SJani Nikula intel_ddi_post_pll_disable(struct intel_encoder *encoder,
3900379bc100SJani Nikula 			   const struct intel_crtc_state *crtc_state,
3901379bc100SJani Nikula 			   const struct drm_connector_state *conn_state)
3902379bc100SJani Nikula {
3903379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3904379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
3905d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3906d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3907379bc100SJani Nikula 
390824a7bfe0SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port)
3909379bc100SJani Nikula 		intel_display_power_put_unchecked(dev_priv,
3910379bc100SJani Nikula 						  intel_ddi_main_link_aux_domain(dig_port));
391124a7bfe0SImre Deak 
391224a7bfe0SImre Deak 	if (is_tc_port)
391324a7bfe0SImre Deak 		intel_tc_port_put_link(dig_port);
3914379bc100SJani Nikula }
3915379bc100SJani Nikula 
3916379bc100SJani Nikula static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
3917379bc100SJani Nikula {
3918379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3919379bc100SJani Nikula 	struct drm_i915_private *dev_priv =
3920379bc100SJani Nikula 		to_i915(intel_dig_port->base.base.dev);
3921379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
3922379bc100SJani Nikula 	u32 val;
3923379bc100SJani Nikula 	bool wait = false;
3924379bc100SJani Nikula 
39254444df6eSLucas De Marchi 	if (I915_READ(intel_dp->regs.dp_tp_ctl) & DP_TP_CTL_ENABLE) {
3926379bc100SJani Nikula 		val = I915_READ(DDI_BUF_CTL(port));
3927379bc100SJani Nikula 		if (val & DDI_BUF_CTL_ENABLE) {
3928379bc100SJani Nikula 			val &= ~DDI_BUF_CTL_ENABLE;
3929379bc100SJani Nikula 			I915_WRITE(DDI_BUF_CTL(port), val);
3930379bc100SJani Nikula 			wait = true;
3931379bc100SJani Nikula 		}
3932379bc100SJani Nikula 
39334444df6eSLucas De Marchi 		val = I915_READ(intel_dp->regs.dp_tp_ctl);
3934379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
3935379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
39364444df6eSLucas De Marchi 		I915_WRITE(intel_dp->regs.dp_tp_ctl, val);
39374444df6eSLucas De Marchi 		POSTING_READ(intel_dp->regs.dp_tp_ctl);
3938379bc100SJani Nikula 
3939379bc100SJani Nikula 		if (wait)
3940379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3941379bc100SJani Nikula 	}
3942379bc100SJani Nikula 
3943379bc100SJani Nikula 	val = DP_TP_CTL_ENABLE |
3944379bc100SJani Nikula 	      DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
3945379bc100SJani Nikula 	if (intel_dp->link_mst)
3946379bc100SJani Nikula 		val |= DP_TP_CTL_MODE_MST;
3947379bc100SJani Nikula 	else {
3948379bc100SJani Nikula 		val |= DP_TP_CTL_MODE_SST;
3949379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
3950379bc100SJani Nikula 			val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3951379bc100SJani Nikula 	}
39524444df6eSLucas De Marchi 	I915_WRITE(intel_dp->regs.dp_tp_ctl, val);
39534444df6eSLucas De Marchi 	POSTING_READ(intel_dp->regs.dp_tp_ctl);
3954379bc100SJani Nikula 
3955379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3956379bc100SJani Nikula 	I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
3957379bc100SJani Nikula 	POSTING_READ(DDI_BUF_CTL(port));
3958379bc100SJani Nikula 
3959379bc100SJani Nikula 	udelay(600);
3960379bc100SJani Nikula }
3961379bc100SJani Nikula 
3962379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3963379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3964379bc100SJani Nikula {
3965379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3966379bc100SJani Nikula 		return false;
3967379bc100SJani Nikula 
3968379bc100SJani Nikula 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO))
3969379bc100SJani Nikula 		return false;
3970379bc100SJani Nikula 
3971379bc100SJani Nikula 	return I915_READ(HSW_AUD_PIN_ELD_CP_VLD) &
3972379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3973379bc100SJani Nikula }
3974379bc100SJani Nikula 
3975379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3976379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3977379bc100SJani Nikula {
3978379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3979379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3980379bc100SJani Nikula 	else if (IS_CANNONLAKE(dev_priv) && crtc_state->port_clock > 594000)
3981379bc100SJani Nikula 		crtc_state->min_voltage_level = 2;
3982379bc100SJani Nikula }
3983379bc100SJani Nikula 
3984379bc100SJani Nikula void intel_ddi_get_config(struct intel_encoder *encoder,
3985379bc100SJani Nikula 			  struct intel_crtc_state *pipe_config)
3986379bc100SJani Nikula {
3987379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3988379bc100SJani Nikula 	struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
3989379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3990379bc100SJani Nikula 	u32 temp, flags = 0;
3991379bc100SJani Nikula 
3992379bc100SJani Nikula 	/* XXX: DSI transcoder paranoia */
3993379bc100SJani Nikula 	if (WARN_ON(transcoder_is_dsi(cpu_transcoder)))
3994379bc100SJani Nikula 		return;
3995379bc100SJani Nikula 
3996379bc100SJani Nikula 	temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
3997379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3998379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3999379bc100SJani Nikula 	else
4000379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
4001379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
4002379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
4003379bc100SJani Nikula 	else
4004379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
4005379bc100SJani Nikula 
4006379bc100SJani Nikula 	pipe_config->base.adjusted_mode.flags |= flags;
4007379bc100SJani Nikula 
4008379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
4009379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
4010379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
4011379bc100SJani Nikula 		break;
4012379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
4013379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
4014379bc100SJani Nikula 		break;
4015379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
4016379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
4017379bc100SJani Nikula 		break;
4018379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
4019379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
4020379bc100SJani Nikula 		break;
4021379bc100SJani Nikula 	default:
4022379bc100SJani Nikula 		break;
4023379bc100SJani Nikula 	}
4024379bc100SJani Nikula 
4025379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
4026379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
4027379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
4028379bc100SJani Nikula 
4029379bc100SJani Nikula 		pipe_config->infoframes.enable |=
4030379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
4031379bc100SJani Nikula 
4032379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
4033379bc100SJani Nikula 			pipe_config->has_infoframe = true;
4034379bc100SJani Nikula 
4035379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
4036379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
4037379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
4038379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
4039379bc100SJani Nikula 		/* fall through */
4040379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
4041379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
4042379bc100SJani Nikula 		pipe_config->lane_count = 4;
4043379bc100SJani Nikula 		break;
4044379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
4045379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
4046379bc100SJani Nikula 		break;
4047379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
4048379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
4049379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
4050379bc100SJani Nikula 		else
4051379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
4052379bc100SJani Nikula 		pipe_config->lane_count =
4053379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
4054379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
40558aa940c8SMaarten Lankhorst 
40568aa940c8SMaarten Lankhorst 		if (INTEL_GEN(dev_priv) >= 11) {
40578aa940c8SMaarten Lankhorst 			i915_reg_t dp_tp_ctl;
40588aa940c8SMaarten Lankhorst 
40598aa940c8SMaarten Lankhorst 			if (IS_GEN(dev_priv, 11))
40608aa940c8SMaarten Lankhorst 				dp_tp_ctl = DP_TP_CTL(encoder->port);
40618aa940c8SMaarten Lankhorst 			else
40628aa940c8SMaarten Lankhorst 				dp_tp_ctl = TGL_DP_TP_CTL(pipe_config->cpu_transcoder);
40638aa940c8SMaarten Lankhorst 
40648aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
40658aa940c8SMaarten Lankhorst 				I915_READ(dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
40668aa940c8SMaarten Lankhorst 
40678aa940c8SMaarten Lankhorst 			DRM_DEBUG_KMS("[ENCODER:%d:%s] Fec status: %u\n",
40688aa940c8SMaarten Lankhorst 				      encoder->base.base.id, encoder->base.name,
40698aa940c8SMaarten Lankhorst 				      pipe_config->fec_enable);
40708aa940c8SMaarten Lankhorst 		}
40718aa940c8SMaarten Lankhorst 
4072379bc100SJani Nikula 		break;
4073379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
4074379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
4075379bc100SJani Nikula 		pipe_config->lane_count =
4076379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
4077379bc100SJani Nikula 		intel_dp_get_m_n(intel_crtc, pipe_config);
4078379bc100SJani Nikula 		break;
4079379bc100SJani Nikula 	default:
4080379bc100SJani Nikula 		break;
4081379bc100SJani Nikula 	}
4082379bc100SJani Nikula 
4083379bc100SJani Nikula 	pipe_config->has_audio =
4084379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
4085379bc100SJani Nikula 
4086379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
4087379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
4088379bc100SJani Nikula 		/*
4089379bc100SJani Nikula 		 * This is a big fat ugly hack.
4090379bc100SJani Nikula 		 *
4091379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
4092379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
4093379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
4094379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
4095379bc100SJani Nikula 		 * max, not what it tells us to use.
4096379bc100SJani Nikula 		 *
4097379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
4098379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
4099379bc100SJani Nikula 		 * load.
4100379bc100SJani Nikula 		 */
4101379bc100SJani Nikula 		DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
4102379bc100SJani Nikula 			      pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
4103379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
4104379bc100SJani Nikula 	}
4105379bc100SJani Nikula 
4106379bc100SJani Nikula 	intel_ddi_clock_get(encoder, pipe_config);
4107379bc100SJani Nikula 
4108379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4109379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4110379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
4111379bc100SJani Nikula 
4112379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4113379bc100SJani Nikula 
4114379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
4115379bc100SJani Nikula 
4116379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4117379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
4118379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
4119379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4120379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
4121379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
4122379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4123379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
4124379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
4125379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
4126379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
4127379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
4128379bc100SJani Nikula }
4129379bc100SJani Nikula 
4130379bc100SJani Nikula static enum intel_output_type
4131379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
4132379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
4133379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
4134379bc100SJani Nikula {
4135379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
4136379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
4137379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
4138379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
4139379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
4140379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
4141379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
4142379bc100SJani Nikula 	default:
4143379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
4144379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
4145379bc100SJani Nikula 	}
4146379bc100SJani Nikula }
4147379bc100SJani Nikula 
4148379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
4149379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
4150379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
4151379bc100SJani Nikula {
4152379bc100SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
4153379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4154379bc100SJani Nikula 	enum port port = encoder->port;
4155379bc100SJani Nikula 	int ret;
4156379bc100SJani Nikula 
4157379bc100SJani Nikula 	if (HAS_TRANSCODER_EDP(dev_priv) && port == PORT_A)
4158379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
4159379bc100SJani Nikula 
4160379bc100SJani Nikula 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI))
4161379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
4162379bc100SJani Nikula 	else
4163379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
4164379bc100SJani Nikula 	if (ret)
4165379bc100SJani Nikula 		return ret;
4166379bc100SJani Nikula 
4167379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
4168379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
4169379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
4170379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
4171379bc100SJani Nikula 			pipe_config->crc_enabled;
4172379bc100SJani Nikula 
4173379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4174379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
4175379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
4176379bc100SJani Nikula 
4177379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
4178379bc100SJani Nikula 
4179379bc100SJani Nikula 	return 0;
4180379bc100SJani Nikula }
4181379bc100SJani Nikula 
4182379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
4183379bc100SJani Nikula {
4184379bc100SJani Nikula 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4185379bc100SJani Nikula 
4186379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
4187379bc100SJani Nikula 
4188379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
4189379bc100SJani Nikula 	kfree(dig_port);
4190379bc100SJani Nikula }
4191379bc100SJani Nikula 
4192379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
419332691b58SImre Deak 	.reset = intel_dp_encoder_reset,
4194379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
4195379bc100SJani Nikula };
4196379bc100SJani Nikula 
4197379bc100SJani Nikula static struct intel_connector *
4198379bc100SJani Nikula intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
4199379bc100SJani Nikula {
4200379bc100SJani Nikula 	struct intel_connector *connector;
4201379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
4202379bc100SJani Nikula 
4203379bc100SJani Nikula 	connector = intel_connector_alloc();
4204379bc100SJani Nikula 	if (!connector)
4205379bc100SJani Nikula 		return NULL;
4206379bc100SJani Nikula 
4207379bc100SJani Nikula 	intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
4208379bc100SJani Nikula 	intel_dig_port->dp.prepare_link_retrain =
4209379bc100SJani Nikula 		intel_ddi_prepare_link_retrain;
4210379bc100SJani Nikula 
4211379bc100SJani Nikula 	if (!intel_dp_init_connector(intel_dig_port, connector)) {
4212379bc100SJani Nikula 		kfree(connector);
4213379bc100SJani Nikula 		return NULL;
4214379bc100SJani Nikula 	}
4215379bc100SJani Nikula 
4216379bc100SJani Nikula 	return connector;
4217379bc100SJani Nikula }
4218379bc100SJani Nikula 
4219379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
4220379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
4221379bc100SJani Nikula {
4222379bc100SJani Nikula 	struct drm_atomic_state *state;
4223379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
4224379bc100SJani Nikula 	int ret;
4225379bc100SJani Nikula 
4226379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
4227379bc100SJani Nikula 	if (!state)
4228379bc100SJani Nikula 		return -ENOMEM;
4229379bc100SJani Nikula 
4230379bc100SJani Nikula 	state->acquire_ctx = ctx;
4231379bc100SJani Nikula 
4232379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
4233379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
4234379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
4235379bc100SJani Nikula 		goto out;
4236379bc100SJani Nikula 	}
4237379bc100SJani Nikula 
4238379bc100SJani Nikula 	crtc_state->connectors_changed = true;
4239379bc100SJani Nikula 
4240379bc100SJani Nikula 	ret = drm_atomic_commit(state);
4241379bc100SJani Nikula out:
4242379bc100SJani Nikula 	drm_atomic_state_put(state);
4243379bc100SJani Nikula 
4244379bc100SJani Nikula 	return ret;
4245379bc100SJani Nikula }
4246379bc100SJani Nikula 
4247379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4248379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4249379bc100SJani Nikula {
4250379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4251379bc100SJani Nikula 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(&encoder->base);
4252379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4253379bc100SJani Nikula 	struct i2c_adapter *adapter =
4254379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4255379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4256379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4257379bc100SJani Nikula 	struct intel_crtc *crtc;
4258379bc100SJani Nikula 	u8 config;
4259379bc100SJani Nikula 	int ret;
4260379bc100SJani Nikula 
4261379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4262379bc100SJani Nikula 		return 0;
4263379bc100SJani Nikula 
4264379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4265379bc100SJani Nikula 			       ctx);
4266379bc100SJani Nikula 	if (ret)
4267379bc100SJani Nikula 		return ret;
4268379bc100SJani Nikula 
4269379bc100SJani Nikula 	conn_state = connector->base.state;
4270379bc100SJani Nikula 
4271379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4272379bc100SJani Nikula 	if (!crtc)
4273379bc100SJani Nikula 		return 0;
4274379bc100SJani Nikula 
4275379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4276379bc100SJani Nikula 	if (ret)
4277379bc100SJani Nikula 		return ret;
4278379bc100SJani Nikula 
4279379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4280379bc100SJani Nikula 
4281379bc100SJani Nikula 	WARN_ON(!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4282379bc100SJani Nikula 
4283379bc100SJani Nikula 	if (!crtc_state->base.active)
4284379bc100SJani Nikula 		return 0;
4285379bc100SJani Nikula 
4286379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4287379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4288379bc100SJani Nikula 		return 0;
4289379bc100SJani Nikula 
4290379bc100SJani Nikula 	if (conn_state->commit &&
4291379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4292379bc100SJani Nikula 		return 0;
4293379bc100SJani Nikula 
4294379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4295379bc100SJani Nikula 	if (ret < 0) {
4296379bc100SJani Nikula 		DRM_ERROR("Failed to read TMDS config: %d\n", ret);
4297379bc100SJani Nikula 		return 0;
4298379bc100SJani Nikula 	}
4299379bc100SJani Nikula 
4300379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4301379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4302379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4303379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4304379bc100SJani Nikula 		return 0;
4305379bc100SJani Nikula 
4306379bc100SJani Nikula 	/*
4307379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4308379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4309379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4310379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4311379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4312379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4313379bc100SJani Nikula 	 * the SCDC settings on the fly.
4314379bc100SJani Nikula 	 */
4315379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4316379bc100SJani Nikula }
4317379bc100SJani Nikula 
43183944709dSImre Deak static enum intel_hotplug_state
43193944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
43203944709dSImre Deak 		  struct intel_connector *connector,
43213944709dSImre Deak 		  bool irq_received)
4322379bc100SJani Nikula {
4323bb80c925SJosé Roberto de Souza 	struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
4324379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
43253944709dSImre Deak 	enum intel_hotplug_state state;
4326379bc100SJani Nikula 	int ret;
4327379bc100SJani Nikula 
43283944709dSImre Deak 	state = intel_encoder_hotplug(encoder, connector, irq_received);
4329379bc100SJani Nikula 
4330379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4331379bc100SJani Nikula 
4332379bc100SJani Nikula 	for (;;) {
4333379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4334379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4335379bc100SJani Nikula 		else
4336379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4337379bc100SJani Nikula 
4338379bc100SJani Nikula 		if (ret == -EDEADLK) {
4339379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4340379bc100SJani Nikula 			continue;
4341379bc100SJani Nikula 		}
4342379bc100SJani Nikula 
4343379bc100SJani Nikula 		break;
4344379bc100SJani Nikula 	}
4345379bc100SJani Nikula 
4346379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4347379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
4348379bc100SJani Nikula 	WARN(ret, "Acquiring modeset locks failed with %i\n", ret);
4349379bc100SJani Nikula 
4350bb80c925SJosé Roberto de Souza 	/*
4351bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4352bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4353bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4354bb80c925SJosé Roberto de Souza 	 *
4355bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4356bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4357bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4358bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4359bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4360bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4361bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4362bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4363bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4364bb80c925SJosé Roberto de Souza 	 * status.
4365bb80c925SJosé Roberto de Souza 	 */
4366bb80c925SJosé Roberto de Souza 	if (state == INTEL_HOTPLUG_UNCHANGED && irq_received &&
4367bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4368bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4369bb80c925SJosé Roberto de Souza 
43703944709dSImre Deak 	return state;
4371379bc100SJani Nikula }
4372379bc100SJani Nikula 
4373379bc100SJani Nikula static struct intel_connector *
4374379bc100SJani Nikula intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
4375379bc100SJani Nikula {
4376379bc100SJani Nikula 	struct intel_connector *connector;
4377379bc100SJani Nikula 	enum port port = intel_dig_port->base.port;
4378379bc100SJani Nikula 
4379379bc100SJani Nikula 	connector = intel_connector_alloc();
4380379bc100SJani Nikula 	if (!connector)
4381379bc100SJani Nikula 		return NULL;
4382379bc100SJani Nikula 
4383379bc100SJani Nikula 	intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
4384379bc100SJani Nikula 	intel_hdmi_init_connector(intel_dig_port, connector);
4385379bc100SJani Nikula 
4386379bc100SJani Nikula 	return connector;
4387379bc100SJani Nikula }
4388379bc100SJani Nikula 
4389379bc100SJani Nikula static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dport)
4390379bc100SJani Nikula {
4391379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
4392379bc100SJani Nikula 
4393379bc100SJani Nikula 	if (dport->base.port != PORT_A)
4394379bc100SJani Nikula 		return false;
4395379bc100SJani Nikula 
4396379bc100SJani Nikula 	if (dport->saved_port_bits & DDI_A_4_LANES)
4397379bc100SJani Nikula 		return false;
4398379bc100SJani Nikula 
4399379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4400379bc100SJani Nikula 	 *                     supported configuration
4401379bc100SJani Nikula 	 */
4402379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
4403379bc100SJani Nikula 		return true;
4404379bc100SJani Nikula 
4405379bc100SJani Nikula 	/* Cannonlake: Most of SKUs don't support DDI_E, and the only
4406379bc100SJani Nikula 	 *             one who does also have a full A/E split called
4407379bc100SJani Nikula 	 *             DDI_F what makes DDI_E useless. However for this
4408379bc100SJani Nikula 	 *             case let's trust VBT info.
4409379bc100SJani Nikula 	 */
4410379bc100SJani Nikula 	if (IS_CANNONLAKE(dev_priv) &&
4411379bc100SJani Nikula 	    !intel_bios_is_port_present(dev_priv, PORT_E))
4412379bc100SJani Nikula 		return true;
4413379bc100SJani Nikula 
4414379bc100SJani Nikula 	return false;
4415379bc100SJani Nikula }
4416379bc100SJani Nikula 
4417379bc100SJani Nikula static int
4418379bc100SJani Nikula intel_ddi_max_lanes(struct intel_digital_port *intel_dport)
4419379bc100SJani Nikula {
4420379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_dport->base.base.dev);
4421379bc100SJani Nikula 	enum port port = intel_dport->base.port;
4422379bc100SJani Nikula 	int max_lanes = 4;
4423379bc100SJani Nikula 
4424379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4425379bc100SJani Nikula 		return max_lanes;
4426379bc100SJani Nikula 
4427379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4428379bc100SJani Nikula 		if (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4429379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4430379bc100SJani Nikula 		else
4431379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4432379bc100SJani Nikula 			max_lanes = 2;
4433379bc100SJani Nikula 	}
4434379bc100SJani Nikula 
4435379bc100SJani Nikula 	/*
4436379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4437379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4438379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4439379bc100SJani Nikula 	 */
4440379bc100SJani Nikula 	if (intel_ddi_a_force_4_lanes(intel_dport)) {
4441379bc100SJani Nikula 		DRM_DEBUG_KMS("Forcing DDI_A_4_LANES for port A\n");
4442379bc100SJani Nikula 		intel_dport->saved_port_bits |= DDI_A_4_LANES;
4443379bc100SJani Nikula 		max_lanes = 4;
4444379bc100SJani Nikula 	}
4445379bc100SJani Nikula 
4446379bc100SJani Nikula 	return max_lanes;
4447379bc100SJani Nikula }
4448379bc100SJani Nikula 
4449379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4450379bc100SJani Nikula {
4451379bc100SJani Nikula 	struct ddi_vbt_port_info *port_info =
4452379bc100SJani Nikula 		&dev_priv->vbt.ddi_port_info[port];
4453379bc100SJani Nikula 	struct intel_digital_port *intel_dig_port;
4454379bc100SJani Nikula 	struct intel_encoder *intel_encoder;
4455379bc100SJani Nikula 	struct drm_encoder *encoder;
4456379bc100SJani Nikula 	bool init_hdmi, init_dp, init_lspcon = false;
4457379bc100SJani Nikula 	enum pipe pipe;
4458d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4459379bc100SJani Nikula 
4460379bc100SJani Nikula 	init_hdmi = port_info->supports_dvi || port_info->supports_hdmi;
4461379bc100SJani Nikula 	init_dp = port_info->supports_dp;
4462379bc100SJani Nikula 
4463379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4464379bc100SJani Nikula 		/*
4465379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4466379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4467379bc100SJani Nikula 		 * is initialized before lspcon.
4468379bc100SJani Nikula 		 */
4469379bc100SJani Nikula 		init_dp = true;
4470379bc100SJani Nikula 		init_lspcon = true;
4471379bc100SJani Nikula 		init_hdmi = false;
4472379bc100SJani Nikula 		DRM_DEBUG_KMS("VBT says port %c has lspcon\n", port_name(port));
4473379bc100SJani Nikula 	}
4474379bc100SJani Nikula 
4475379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
4476379bc100SJani Nikula 		DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4477379bc100SJani Nikula 			      port_name(port));
4478379bc100SJani Nikula 		return;
4479379bc100SJani Nikula 	}
4480379bc100SJani Nikula 
4481379bc100SJani Nikula 	intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
4482379bc100SJani Nikula 	if (!intel_dig_port)
4483379bc100SJani Nikula 		return;
4484379bc100SJani Nikula 
4485379bc100SJani Nikula 	intel_encoder = &intel_dig_port->base;
4486379bc100SJani Nikula 	encoder = &intel_encoder->base;
4487379bc100SJani Nikula 
4488379bc100SJani Nikula 	drm_encoder_init(&dev_priv->drm, encoder, &intel_ddi_funcs,
4489379bc100SJani Nikula 			 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
4490379bc100SJani Nikula 
4491379bc100SJani Nikula 	intel_encoder->hotplug = intel_ddi_hotplug;
4492379bc100SJani Nikula 	intel_encoder->compute_output_type = intel_ddi_compute_output_type;
4493379bc100SJani Nikula 	intel_encoder->compute_config = intel_ddi_compute_config;
4494379bc100SJani Nikula 	intel_encoder->enable = intel_enable_ddi;
4495379bc100SJani Nikula 	intel_encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
4496379bc100SJani Nikula 	intel_encoder->post_pll_disable = intel_ddi_post_pll_disable;
4497379bc100SJani Nikula 	intel_encoder->pre_enable = intel_ddi_pre_enable;
4498379bc100SJani Nikula 	intel_encoder->disable = intel_disable_ddi;
4499379bc100SJani Nikula 	intel_encoder->post_disable = intel_ddi_post_disable;
4500379bc100SJani Nikula 	intel_encoder->update_pipe = intel_ddi_update_pipe;
4501379bc100SJani Nikula 	intel_encoder->get_hw_state = intel_ddi_get_hw_state;
4502379bc100SJani Nikula 	intel_encoder->get_config = intel_ddi_get_config;
4503a171f8e7SImre Deak 	intel_encoder->suspend = intel_dp_encoder_suspend;
4504379bc100SJani Nikula 	intel_encoder->get_power_domains = intel_ddi_get_power_domains;
4505379bc100SJani Nikula 	intel_encoder->type = INTEL_OUTPUT_DDI;
4506379bc100SJani Nikula 	intel_encoder->power_domain = intel_port_to_power_domain(port);
4507379bc100SJani Nikula 	intel_encoder->port = port;
4508379bc100SJani Nikula 	intel_encoder->cloneable = 0;
4509379bc100SJani Nikula 	for_each_pipe(dev_priv, pipe)
4510379bc100SJani Nikula 		intel_encoder->crtc_mask |= BIT(pipe);
4511379bc100SJani Nikula 
4512379bc100SJani Nikula 	if (INTEL_GEN(dev_priv) >= 11)
4513379bc100SJani Nikula 		intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
4514379bc100SJani Nikula 			DDI_BUF_PORT_REVERSAL;
4515379bc100SJani Nikula 	else
4516379bc100SJani Nikula 		intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
4517379bc100SJani Nikula 			(DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
4518379bc100SJani Nikula 	intel_dig_port->dp.output_reg = INVALID_MMIO_REG;
4519379bc100SJani Nikula 	intel_dig_port->max_lanes = intel_ddi_max_lanes(intel_dig_port);
4520379bc100SJani Nikula 	intel_dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4521379bc100SJani Nikula 
4522d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4523ab7bc4e1SImre Deak 		bool is_legacy = !port_info->supports_typec_usb &&
4524379bc100SJani Nikula 				 !port_info->supports_tbt;
4525379bc100SJani Nikula 
4526ab7bc4e1SImre Deak 		intel_tc_port_init(intel_dig_port, is_legacy);
452724a7bfe0SImre Deak 
452824a7bfe0SImre Deak 		intel_encoder->update_prepare = intel_ddi_update_prepare;
452924a7bfe0SImre Deak 		intel_encoder->update_complete = intel_ddi_update_complete;
4530ab7bc4e1SImre Deak 	}
4531ab7bc4e1SImre Deak 
4532379bc100SJani Nikula 	switch (port) {
4533379bc100SJani Nikula 	case PORT_A:
4534379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4535379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_A_IO;
4536379bc100SJani Nikula 		break;
4537379bc100SJani Nikula 	case PORT_B:
4538379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4539379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_B_IO;
4540379bc100SJani Nikula 		break;
4541379bc100SJani Nikula 	case PORT_C:
4542379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4543379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_C_IO;
4544379bc100SJani Nikula 		break;
4545379bc100SJani Nikula 	case PORT_D:
4546379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4547379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_D_IO;
4548379bc100SJani Nikula 		break;
4549379bc100SJani Nikula 	case PORT_E:
4550379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4551379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_E_IO;
4552379bc100SJani Nikula 		break;
4553379bc100SJani Nikula 	case PORT_F:
4554379bc100SJani Nikula 		intel_dig_port->ddi_io_power_domain =
4555379bc100SJani Nikula 			POWER_DOMAIN_PORT_DDI_F_IO;
4556379bc100SJani Nikula 		break;
45576c8337daSVandita Kulkarni 	case PORT_G:
45586c8337daSVandita Kulkarni 		intel_dig_port->ddi_io_power_domain =
45596c8337daSVandita Kulkarni 			POWER_DOMAIN_PORT_DDI_G_IO;
45606c8337daSVandita Kulkarni 		break;
45616c8337daSVandita Kulkarni 	case PORT_H:
45626c8337daSVandita Kulkarni 		intel_dig_port->ddi_io_power_domain =
45636c8337daSVandita Kulkarni 			POWER_DOMAIN_PORT_DDI_H_IO;
45646c8337daSVandita Kulkarni 		break;
45656c8337daSVandita Kulkarni 	case PORT_I:
45666c8337daSVandita Kulkarni 		intel_dig_port->ddi_io_power_domain =
45676c8337daSVandita Kulkarni 			POWER_DOMAIN_PORT_DDI_I_IO;
45686c8337daSVandita Kulkarni 		break;
4569379bc100SJani Nikula 	default:
4570379bc100SJani Nikula 		MISSING_CASE(port);
4571379bc100SJani Nikula 	}
4572379bc100SJani Nikula 
4573379bc100SJani Nikula 	if (init_dp) {
4574379bc100SJani Nikula 		if (!intel_ddi_init_dp_connector(intel_dig_port))
4575379bc100SJani Nikula 			goto err;
4576379bc100SJani Nikula 
4577379bc100SJani Nikula 		intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
4578379bc100SJani Nikula 	}
4579379bc100SJani Nikula 
4580379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4581379bc100SJani Nikula 	 * case we have some really bad VBTs... */
4582379bc100SJani Nikula 	if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
4583379bc100SJani Nikula 		if (!intel_ddi_init_hdmi_connector(intel_dig_port))
4584379bc100SJani Nikula 			goto err;
4585379bc100SJani Nikula 	}
4586379bc100SJani Nikula 
4587379bc100SJani Nikula 	if (init_lspcon) {
4588379bc100SJani Nikula 		if (lspcon_init(intel_dig_port))
4589379bc100SJani Nikula 			/* TODO: handle hdmi info frame part */
4590379bc100SJani Nikula 			DRM_DEBUG_KMS("LSPCON init success on port %c\n",
4591379bc100SJani Nikula 				port_name(port));
4592379bc100SJani Nikula 		else
4593379bc100SJani Nikula 			/*
4594379bc100SJani Nikula 			 * LSPCON init faied, but DP init was success, so
4595379bc100SJani Nikula 			 * lets try to drive as DP++ port.
4596379bc100SJani Nikula 			 */
4597379bc100SJani Nikula 			DRM_ERROR("LSPCON init failed on port %c\n",
4598379bc100SJani Nikula 				port_name(port));
4599379bc100SJani Nikula 	}
4600379bc100SJani Nikula 
4601379bc100SJani Nikula 	intel_infoframe_init(intel_dig_port);
4602379bc100SJani Nikula 
4603379bc100SJani Nikula 	return;
4604379bc100SJani Nikula 
4605379bc100SJani Nikula err:
4606379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
4607379bc100SJani Nikula 	kfree(intel_dig_port);
4608379bc100SJani Nikula }
4609