]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/i915/intel_ddi.c
drm/i915: Use existing DSI backlight ports info
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / i915 / intel_ddi.c
CommitLineData
45244b87
ED
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include "i915_drv.h"
29#include "intel_drv.h"
30
10122051
JN
31struct ddi_buf_trans {
32 u32 trans1; /* balance leg enable, de-emph level */
33 u32 trans2; /* vref sel, vswing */
f8896f5d 34 u8 i_boost; /* SKL: I_boost; valid: 0x0, 0x1, 0x3, 0x7 */
10122051
JN
35};
36
97eeb872
VS
37static const u8 index_to_dp_signal_levels[] = {
38 [0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
39 [1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
40 [2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
41 [3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
42 [4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
43 [5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
44 [6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
45 [7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
46 [8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
47 [9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
48};
49
45244b87
ED
50/* HDMI/DVI modes ignore everything but the last 2 items. So we share
51 * them for both DP and FDI transports, allowing those ports to
52 * automatically adapt to HDMI connections as well
53 */
10122051 54static const struct ddi_buf_trans hsw_ddi_translations_dp[] = {
f8896f5d
DW
55 { 0x00FFFFFF, 0x0006000E, 0x0 },
56 { 0x00D75FFF, 0x0005000A, 0x0 },
57 { 0x00C30FFF, 0x00040006, 0x0 },
58 { 0x80AAAFFF, 0x000B0000, 0x0 },
59 { 0x00FFFFFF, 0x0005000A, 0x0 },
60 { 0x00D75FFF, 0x000C0004, 0x0 },
61 { 0x80C30FFF, 0x000B0000, 0x0 },
62 { 0x00FFFFFF, 0x00040006, 0x0 },
63 { 0x80D75FFF, 0x000B0000, 0x0 },
45244b87
ED
64};
65
10122051 66static const struct ddi_buf_trans hsw_ddi_translations_fdi[] = {
f8896f5d
DW
67 { 0x00FFFFFF, 0x0007000E, 0x0 },
68 { 0x00D75FFF, 0x000F000A, 0x0 },
69 { 0x00C30FFF, 0x00060006, 0x0 },
70 { 0x00AAAFFF, 0x001E0000, 0x0 },
71 { 0x00FFFFFF, 0x000F000A, 0x0 },
72 { 0x00D75FFF, 0x00160004, 0x0 },
73 { 0x00C30FFF, 0x001E0000, 0x0 },
74 { 0x00FFFFFF, 0x00060006, 0x0 },
75 { 0x00D75FFF, 0x001E0000, 0x0 },
6acab15a
PZ
76};
77
10122051
JN
78static const struct ddi_buf_trans hsw_ddi_translations_hdmi[] = {
79 /* Idx NT mV d T mV d db */
f8896f5d
DW
80 { 0x00FFFFFF, 0x0006000E, 0x0 },/* 0: 400 400 0 */
81 { 0x00E79FFF, 0x000E000C, 0x0 },/* 1: 400 500 2 */
82 { 0x00D75FFF, 0x0005000A, 0x0 },/* 2: 400 600 3.5 */
83 { 0x00FFFFFF, 0x0005000A, 0x0 },/* 3: 600 600 0 */
84 { 0x00E79FFF, 0x001D0007, 0x0 },/* 4: 600 750 2 */
85 { 0x00D75FFF, 0x000C0004, 0x0 },/* 5: 600 900 3.5 */
86 { 0x00FFFFFF, 0x00040006, 0x0 },/* 6: 800 800 0 */
87 { 0x80E79FFF, 0x00030002, 0x0 },/* 7: 800 1000 2 */
88 { 0x00FFFFFF, 0x00140005, 0x0 },/* 8: 850 850 0 */
89 { 0x00FFFFFF, 0x000C0004, 0x0 },/* 9: 900 900 0 */
90 { 0x00FFFFFF, 0x001C0003, 0x0 },/* 10: 950 950 0 */
91 { 0x80FFFFFF, 0x00030002, 0x0 },/* 11: 1000 1000 0 */
45244b87
ED
92};
93
10122051 94static const struct ddi_buf_trans bdw_ddi_translations_edp[] = {
f8896f5d
DW
95 { 0x00FFFFFF, 0x00000012, 0x0 },
96 { 0x00EBAFFF, 0x00020011, 0x0 },
97 { 0x00C71FFF, 0x0006000F, 0x0 },
98 { 0x00AAAFFF, 0x000E000A, 0x0 },
99 { 0x00FFFFFF, 0x00020011, 0x0 },
100 { 0x00DB6FFF, 0x0005000F, 0x0 },
101 { 0x00BEEFFF, 0x000A000C, 0x0 },
102 { 0x00FFFFFF, 0x0005000F, 0x0 },
103 { 0x00DB6FFF, 0x000A000C, 0x0 },
300644c7
PZ
104};
105
10122051 106static const struct ddi_buf_trans bdw_ddi_translations_dp[] = {
f8896f5d
DW
107 { 0x00FFFFFF, 0x0007000E, 0x0 },
108 { 0x00D75FFF, 0x000E000A, 0x0 },
109 { 0x00BEFFFF, 0x00140006, 0x0 },
110 { 0x80B2CFFF, 0x001B0002, 0x0 },
111 { 0x00FFFFFF, 0x000E000A, 0x0 },
112 { 0x00DB6FFF, 0x00160005, 0x0 },
113 { 0x80C71FFF, 0x001A0002, 0x0 },
114 { 0x00F7DFFF, 0x00180004, 0x0 },
115 { 0x80D75FFF, 0x001B0002, 0x0 },
e58623cb
AR
116};
117
10122051 118static const struct ddi_buf_trans bdw_ddi_translations_fdi[] = {
f8896f5d
DW
119 { 0x00FFFFFF, 0x0001000E, 0x0 },
120 { 0x00D75FFF, 0x0004000A, 0x0 },
121 { 0x00C30FFF, 0x00070006, 0x0 },
122 { 0x00AAAFFF, 0x000C0000, 0x0 },
123 { 0x00FFFFFF, 0x0004000A, 0x0 },
124 { 0x00D75FFF, 0x00090004, 0x0 },
125 { 0x00C30FFF, 0x000C0000, 0x0 },
126 { 0x00FFFFFF, 0x00070006, 0x0 },
127 { 0x00D75FFF, 0x000C0000, 0x0 },
e58623cb
AR
128};
129
10122051
JN
130static const struct ddi_buf_trans bdw_ddi_translations_hdmi[] = {
131 /* Idx NT mV d T mV df db */
f8896f5d
DW
132 { 0x00FFFFFF, 0x0007000E, 0x0 },/* 0: 400 400 0 */
133 { 0x00D75FFF, 0x000E000A, 0x0 },/* 1: 400 600 3.5 */
134 { 0x00BEFFFF, 0x00140006, 0x0 },/* 2: 400 800 6 */
135 { 0x00FFFFFF, 0x0009000D, 0x0 },/* 3: 450 450 0 */
136 { 0x00FFFFFF, 0x000E000A, 0x0 },/* 4: 600 600 0 */
137 { 0x00D7FFFF, 0x00140006, 0x0 },/* 5: 600 800 2.5 */
138 { 0x80CB2FFF, 0x001B0002, 0x0 },/* 6: 600 1000 4.5 */
139 { 0x00FFFFFF, 0x00140006, 0x0 },/* 7: 800 800 0 */
140 { 0x80E79FFF, 0x001B0002, 0x0 },/* 8: 800 1000 2 */
141 { 0x80FFFFFF, 0x001B0002, 0x0 },/* 9: 1000 1000 0 */
a26aa8ba
DL
142};
143
5f8b2531 144/* Skylake H and S */
7f88e3af 145static const struct ddi_buf_trans skl_ddi_translations_dp[] = {
f8896f5d
DW
146 { 0x00002016, 0x000000A0, 0x0 },
147 { 0x00005012, 0x0000009B, 0x0 },
148 { 0x00007011, 0x00000088, 0x0 },
d7097cff 149 { 0x80009010, 0x000000C0, 0x1 },
f8896f5d
DW
150 { 0x00002016, 0x0000009B, 0x0 },
151 { 0x00005012, 0x00000088, 0x0 },
d7097cff 152 { 0x80007011, 0x000000C0, 0x1 },
f8896f5d 153 { 0x00002016, 0x000000DF, 0x0 },
d7097cff 154 { 0x80005012, 0x000000C0, 0x1 },
7f88e3af
DL
155};
156
f8896f5d
DW
157/* Skylake U */
158static const struct ddi_buf_trans skl_u_ddi_translations_dp[] = {
5f8b2531 159 { 0x0000201B, 0x000000A2, 0x0 },
f8896f5d 160 { 0x00005012, 0x00000088, 0x0 },
5ac90567 161 { 0x80007011, 0x000000CD, 0x1 },
d7097cff 162 { 0x80009010, 0x000000C0, 0x1 },
5f8b2531 163 { 0x0000201B, 0x0000009D, 0x0 },
d7097cff
RV
164 { 0x80005012, 0x000000C0, 0x1 },
165 { 0x80007011, 0x000000C0, 0x1 },
f8896f5d 166 { 0x00002016, 0x00000088, 0x0 },
d7097cff 167 { 0x80005012, 0x000000C0, 0x1 },
f8896f5d
DW
168};
169
5f8b2531
RV
170/* Skylake Y */
171static const struct ddi_buf_trans skl_y_ddi_translations_dp[] = {
f8896f5d
DW
172 { 0x00000018, 0x000000A2, 0x0 },
173 { 0x00005012, 0x00000088, 0x0 },
5ac90567 174 { 0x80007011, 0x000000CD, 0x3 },
d7097cff 175 { 0x80009010, 0x000000C0, 0x3 },
f8896f5d 176 { 0x00000018, 0x0000009D, 0x0 },
d7097cff
RV
177 { 0x80005012, 0x000000C0, 0x3 },
178 { 0x80007011, 0x000000C0, 0x3 },
f8896f5d 179 { 0x00000018, 0x00000088, 0x0 },
d7097cff 180 { 0x80005012, 0x000000C0, 0x3 },
f8896f5d
DW
181};
182
0fdd4918
RV
183/* Kabylake H and S */
184static const struct ddi_buf_trans kbl_ddi_translations_dp[] = {
185 { 0x00002016, 0x000000A0, 0x0 },
186 { 0x00005012, 0x0000009B, 0x0 },
187 { 0x00007011, 0x00000088, 0x0 },
188 { 0x80009010, 0x000000C0, 0x1 },
189 { 0x00002016, 0x0000009B, 0x0 },
190 { 0x00005012, 0x00000088, 0x0 },
191 { 0x80007011, 0x000000C0, 0x1 },
192 { 0x00002016, 0x00000097, 0x0 },
193 { 0x80005012, 0x000000C0, 0x1 },
194};
195
196/* Kabylake U */
197static const struct ddi_buf_trans kbl_u_ddi_translations_dp[] = {
198 { 0x0000201B, 0x000000A1, 0x0 },
199 { 0x00005012, 0x00000088, 0x0 },
200 { 0x80007011, 0x000000CD, 0x3 },
201 { 0x80009010, 0x000000C0, 0x3 },
202 { 0x0000201B, 0x0000009D, 0x0 },
203 { 0x80005012, 0x000000C0, 0x3 },
204 { 0x80007011, 0x000000C0, 0x3 },
205 { 0x00002016, 0x0000004F, 0x0 },
206 { 0x80005012, 0x000000C0, 0x3 },
207};
208
209/* Kabylake Y */
210static const struct ddi_buf_trans kbl_y_ddi_translations_dp[] = {
211 { 0x00001017, 0x000000A1, 0x0 },
212 { 0x00005012, 0x00000088, 0x0 },
213 { 0x80007011, 0x000000CD, 0x3 },
214 { 0x8000800F, 0x000000C0, 0x3 },
215 { 0x00001017, 0x0000009D, 0x0 },
216 { 0x80005012, 0x000000C0, 0x3 },
217 { 0x80007011, 0x000000C0, 0x3 },
218 { 0x00001017, 0x0000004C, 0x0 },
219 { 0x80005012, 0x000000C0, 0x3 },
220};
221
f8896f5d 222/*
0fdd4918 223 * Skylake/Kabylake H and S
f8896f5d
DW
224 * eDP 1.4 low vswing translation parameters
225 */
7ad14a29 226static const struct ddi_buf_trans skl_ddi_translations_edp[] = {
f8896f5d
DW
227 { 0x00000018, 0x000000A8, 0x0 },
228 { 0x00004013, 0x000000A9, 0x0 },
229 { 0x00007011, 0x000000A2, 0x0 },
230 { 0x00009010, 0x0000009C, 0x0 },
231 { 0x00000018, 0x000000A9, 0x0 },
232 { 0x00006013, 0x000000A2, 0x0 },
233 { 0x00007011, 0x000000A6, 0x0 },
234 { 0x00000018, 0x000000AB, 0x0 },
235 { 0x00007013, 0x0000009F, 0x0 },
236 { 0x00000018, 0x000000DF, 0x0 },
237};
238
239/*
0fdd4918 240 * Skylake/Kabylake U
f8896f5d
DW
241 * eDP 1.4 low vswing translation parameters
242 */
243static const struct ddi_buf_trans skl_u_ddi_translations_edp[] = {
244 { 0x00000018, 0x000000A8, 0x0 },
245 { 0x00004013, 0x000000A9, 0x0 },
246 { 0x00007011, 0x000000A2, 0x0 },
247 { 0x00009010, 0x0000009C, 0x0 },
248 { 0x00000018, 0x000000A9, 0x0 },
249 { 0x00006013, 0x000000A2, 0x0 },
250 { 0x00007011, 0x000000A6, 0x0 },
251 { 0x00002016, 0x000000AB, 0x0 },
252 { 0x00005013, 0x0000009F, 0x0 },
253 { 0x00000018, 0x000000DF, 0x0 },
7ad14a29
SJ
254};
255
f8896f5d 256/*
0fdd4918 257 * Skylake/Kabylake Y
f8896f5d
DW
258 * eDP 1.4 low vswing translation parameters
259 */
5f8b2531 260static const struct ddi_buf_trans skl_y_ddi_translations_edp[] = {
f8896f5d
DW
261 { 0x00000018, 0x000000A8, 0x0 },
262 { 0x00004013, 0x000000AB, 0x0 },
263 { 0x00007011, 0x000000A4, 0x0 },
264 { 0x00009010, 0x000000DF, 0x0 },
265 { 0x00000018, 0x000000AA, 0x0 },
266 { 0x00006013, 0x000000A4, 0x0 },
267 { 0x00007011, 0x0000009D, 0x0 },
268 { 0x00000018, 0x000000A0, 0x0 },
269 { 0x00006012, 0x000000DF, 0x0 },
270 { 0x00000018, 0x0000008A, 0x0 },
271};
7ad14a29 272
0fdd4918 273/* Skylake/Kabylake U, H and S */
7f88e3af 274static const struct ddi_buf_trans skl_ddi_translations_hdmi[] = {
f8896f5d
DW
275 { 0x00000018, 0x000000AC, 0x0 },
276 { 0x00005012, 0x0000009D, 0x0 },
277 { 0x00007011, 0x00000088, 0x0 },
278 { 0x00000018, 0x000000A1, 0x0 },
279 { 0x00000018, 0x00000098, 0x0 },
280 { 0x00004013, 0x00000088, 0x0 },
2e78416e 281 { 0x80006012, 0x000000CD, 0x1 },
f8896f5d 282 { 0x00000018, 0x000000DF, 0x0 },
2e78416e
RV
283 { 0x80003015, 0x000000CD, 0x1 }, /* Default */
284 { 0x80003015, 0x000000C0, 0x1 },
285 { 0x80000018, 0x000000C0, 0x1 },
f8896f5d
DW
286};
287
0fdd4918 288/* Skylake/Kabylake Y */
5f8b2531 289static const struct ddi_buf_trans skl_y_ddi_translations_hdmi[] = {
f8896f5d
DW
290 { 0x00000018, 0x000000A1, 0x0 },
291 { 0x00005012, 0x000000DF, 0x0 },
2e78416e 292 { 0x80007011, 0x000000CB, 0x3 },
f8896f5d
DW
293 { 0x00000018, 0x000000A4, 0x0 },
294 { 0x00000018, 0x0000009D, 0x0 },
295 { 0x00004013, 0x00000080, 0x0 },
2e78416e 296 { 0x80006013, 0x000000C0, 0x3 },
f8896f5d 297 { 0x00000018, 0x0000008A, 0x0 },
2e78416e
RV
298 { 0x80003015, 0x000000C0, 0x3 }, /* Default */
299 { 0x80003015, 0x000000C0, 0x3 },
300 { 0x80000018, 0x000000C0, 0x3 },
7f88e3af
DL
301};
302
96fb9f9b 303struct bxt_ddi_buf_trans {
ac3ad6c6
VS
304 u8 margin; /* swing value */
305 u8 scale; /* scale value */
306 u8 enable; /* scale enable */
307 u8 deemphasis;
96fb9f9b
VK
308};
309
96fb9f9b
VK
310static const struct bxt_ddi_buf_trans bxt_ddi_translations_dp[] = {
311 /* Idx NT mV diff db */
043eaf36
VS
312 { 52, 0x9A, 0, 128, }, /* 0: 400 0 */
313 { 78, 0x9A, 0, 85, }, /* 1: 400 3.5 */
314 { 104, 0x9A, 0, 64, }, /* 2: 400 6 */
315 { 154, 0x9A, 0, 43, }, /* 3: 400 9.5 */
316 { 77, 0x9A, 0, 128, }, /* 4: 600 0 */
317 { 116, 0x9A, 0, 85, }, /* 5: 600 3.5 */
318 { 154, 0x9A, 0, 64, }, /* 6: 600 6 */
319 { 102, 0x9A, 0, 128, }, /* 7: 800 0 */
320 { 154, 0x9A, 0, 85, }, /* 8: 800 3.5 */
321 { 154, 0x9A, 1, 128, }, /* 9: 1200 0 */
96fb9f9b
VK
322};
323
d9d7000d
SJ
324static const struct bxt_ddi_buf_trans bxt_ddi_translations_edp[] = {
325 /* Idx NT mV diff db */
043eaf36
VS
326 { 26, 0, 0, 128, }, /* 0: 200 0 */
327 { 38, 0, 0, 112, }, /* 1: 200 1.5 */
328 { 48, 0, 0, 96, }, /* 2: 200 4 */
329 { 54, 0, 0, 69, }, /* 3: 200 6 */
330 { 32, 0, 0, 128, }, /* 4: 250 0 */
331 { 48, 0, 0, 104, }, /* 5: 250 1.5 */
332 { 54, 0, 0, 85, }, /* 6: 250 4 */
333 { 43, 0, 0, 128, }, /* 7: 300 0 */
334 { 54, 0, 0, 101, }, /* 8: 300 1.5 */
335 { 48, 0, 0, 128, }, /* 9: 300 0 */
d9d7000d
SJ
336};
337
96fb9f9b
VK
338/* BSpec has 2 recommended values - entries 0 and 8.
339 * Using the entry with higher vswing.
340 */
341static const struct bxt_ddi_buf_trans bxt_ddi_translations_hdmi[] = {
342 /* Idx NT mV diff db */
043eaf36
VS
343 { 52, 0x9A, 0, 128, }, /* 0: 400 0 */
344 { 52, 0x9A, 0, 85, }, /* 1: 400 3.5 */
345 { 52, 0x9A, 0, 64, }, /* 2: 400 6 */
346 { 42, 0x9A, 0, 43, }, /* 3: 400 9.5 */
347 { 77, 0x9A, 0, 128, }, /* 4: 600 0 */
348 { 77, 0x9A, 0, 85, }, /* 5: 600 3.5 */
349 { 77, 0x9A, 0, 64, }, /* 6: 600 6 */
350 { 102, 0x9A, 0, 128, }, /* 7: 800 0 */
351 { 102, 0x9A, 0, 85, }, /* 8: 800 3.5 */
352 { 154, 0x9A, 1, 128, }, /* 9: 1200 0 */
96fb9f9b
VK
353};
354
83fb7ab4 355struct cnl_ddi_buf_trans {
fb5f4e96
VS
356 u8 dw2_swing_sel;
357 u8 dw7_n_scalar;
358 u8 dw4_cursor_coeff;
359 u8 dw4_post_cursor_2;
360 u8 dw4_post_cursor_1;
83fb7ab4
RV
361};
362
363/* Voltage Swing Programming for VccIO 0.85V for DP */
364static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_85V[] = {
365 /* NT mV Trans mV db */
366 { 0xA, 0x5D, 0x3F, 0x00, 0x00 }, /* 350 350 0.0 */
367 { 0xA, 0x6A, 0x38, 0x00, 0x07 }, /* 350 500 3.1 */
368 { 0xB, 0x7A, 0x32, 0x00, 0x0D }, /* 350 700 6.0 */
369 { 0x6, 0x7C, 0x2D, 0x00, 0x12 }, /* 350 900 8.2 */
370 { 0xA, 0x69, 0x3F, 0x00, 0x00 }, /* 500 500 0.0 */
371 { 0xB, 0x7A, 0x36, 0x00, 0x09 }, /* 500 700 2.9 */
372 { 0x6, 0x7C, 0x30, 0x00, 0x0F }, /* 500 900 5.1 */
373 { 0xB, 0x7D, 0x3C, 0x00, 0x03 }, /* 650 725 0.9 */
374 { 0x6, 0x7C, 0x34, 0x00, 0x0B }, /* 600 900 3.5 */
375 { 0x6, 0x7B, 0x3F, 0x00, 0x00 }, /* 900 900 0.0 */
376};
377
378/* Voltage Swing Programming for VccIO 0.85V for HDMI */
379static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_85V[] = {
380 /* NT mV Trans mV db */
381 { 0xA, 0x60, 0x3F, 0x00, 0x00 }, /* 450 450 0.0 */
382 { 0xB, 0x73, 0x36, 0x00, 0x09 }, /* 450 650 3.2 */
383 { 0x6, 0x7F, 0x31, 0x00, 0x0E }, /* 450 850 5.5 */
384 { 0xB, 0x73, 0x3F, 0x00, 0x00 }, /* 650 650 0.0 */
385 { 0x6, 0x7F, 0x37, 0x00, 0x08 }, /* 650 850 2.3 */
386 { 0x6, 0x7F, 0x3F, 0x00, 0x00 }, /* 850 850 0.0 */
387 { 0x6, 0x7F, 0x35, 0x00, 0x0A }, /* 600 850 3.0 */
388};
389
390/* Voltage Swing Programming for VccIO 0.85V for eDP */
391static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_85V[] = {
392 /* NT mV Trans mV db */
393 { 0xA, 0x66, 0x3A, 0x00, 0x05 }, /* 384 500 2.3 */
394 { 0x0, 0x7F, 0x38, 0x00, 0x07 }, /* 153 200 2.3 */
395 { 0x8, 0x7F, 0x38, 0x00, 0x07 }, /* 192 250 2.3 */
396 { 0x1, 0x7F, 0x38, 0x00, 0x07 }, /* 230 300 2.3 */
397 { 0x9, 0x7F, 0x38, 0x00, 0x07 }, /* 269 350 2.3 */
398 { 0xA, 0x66, 0x3C, 0x00, 0x03 }, /* 446 500 1.0 */
399 { 0xB, 0x70, 0x3C, 0x00, 0x03 }, /* 460 600 2.3 */
400 { 0xC, 0x75, 0x3C, 0x00, 0x03 }, /* 537 700 2.3 */
401 { 0x2, 0x7F, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
402};
403
404/* Voltage Swing Programming for VccIO 0.95V for DP */
405static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_0_95V[] = {
406 /* NT mV Trans mV db */
407 { 0xA, 0x5D, 0x3F, 0x00, 0x00 }, /* 350 350 0.0 */
408 { 0xA, 0x6A, 0x38, 0x00, 0x07 }, /* 350 500 3.1 */
409 { 0xB, 0x7A, 0x32, 0x00, 0x0D }, /* 350 700 6.0 */
410 { 0x6, 0x7C, 0x2D, 0x00, 0x12 }, /* 350 900 8.2 */
411 { 0xA, 0x69, 0x3F, 0x00, 0x00 }, /* 500 500 0.0 */
412 { 0xB, 0x7A, 0x36, 0x00, 0x09 }, /* 500 700 2.9 */
413 { 0x6, 0x7C, 0x30, 0x00, 0x0F }, /* 500 900 5.1 */
414 { 0xB, 0x7D, 0x3C, 0x00, 0x03 }, /* 650 725 0.9 */
415 { 0x6, 0x7C, 0x34, 0x00, 0x0B }, /* 600 900 3.5 */
416 { 0x6, 0x7B, 0x3F, 0x00, 0x00 }, /* 900 900 0.0 */
417};
418
419/* Voltage Swing Programming for VccIO 0.95V for HDMI */
420static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_0_95V[] = {
421 /* NT mV Trans mV db */
422 { 0xA, 0x5C, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
423 { 0xB, 0x69, 0x37, 0x00, 0x08 }, /* 400 600 3.5 */
424 { 0x5, 0x76, 0x31, 0x00, 0x0E }, /* 400 800 6.0 */
425 { 0xA, 0x5E, 0x3F, 0x00, 0x00 }, /* 450 450 0.0 */
426 { 0xB, 0x69, 0x3F, 0x00, 0x00 }, /* 600 600 0.0 */
427 { 0xB, 0x79, 0x35, 0x00, 0x0A }, /* 600 850 3.0 */
428 { 0x6, 0x7D, 0x32, 0x00, 0x0D }, /* 600 1000 4.4 */
429 { 0x5, 0x76, 0x3F, 0x00, 0x00 }, /* 800 800 0.0 */
430 { 0x6, 0x7D, 0x39, 0x00, 0x06 }, /* 800 1000 1.9 */
431 { 0x6, 0x7F, 0x39, 0x00, 0x06 }, /* 850 1050 1.8 */
432 { 0x6, 0x7F, 0x3F, 0x00, 0x00 }, /* 1050 1050 0.0 */
433};
434
435/* Voltage Swing Programming for VccIO 0.95V for eDP */
436static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_0_95V[] = {
437 /* NT mV Trans mV db */
438 { 0xA, 0x61, 0x3A, 0x00, 0x05 }, /* 384 500 2.3 */
439 { 0x0, 0x7F, 0x38, 0x00, 0x07 }, /* 153 200 2.3 */
440 { 0x8, 0x7F, 0x38, 0x00, 0x07 }, /* 192 250 2.3 */
441 { 0x1, 0x7F, 0x38, 0x00, 0x07 }, /* 230 300 2.3 */
442 { 0x9, 0x7F, 0x38, 0x00, 0x07 }, /* 269 350 2.3 */
443 { 0xA, 0x61, 0x3C, 0x00, 0x03 }, /* 446 500 1.0 */
444 { 0xB, 0x68, 0x39, 0x00, 0x06 }, /* 460 600 2.3 */
445 { 0xC, 0x6E, 0x39, 0x00, 0x06 }, /* 537 700 2.3 */
446 { 0x4, 0x7F, 0x3A, 0x00, 0x05 }, /* 460 600 2.3 */
447 { 0x2, 0x7F, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
448};
449
450/* Voltage Swing Programming for VccIO 1.05V for DP */
451static const struct cnl_ddi_buf_trans cnl_ddi_translations_dp_1_05V[] = {
452 /* NT mV Trans mV db */
453 { 0xA, 0x58, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
454 { 0xB, 0x64, 0x37, 0x00, 0x08 }, /* 400 600 3.5 */
455 { 0x5, 0x70, 0x31, 0x00, 0x0E }, /* 400 800 6.0 */
456 { 0x6, 0x7F, 0x2C, 0x00, 0x13 }, /* 400 1050 8.4 */
457 { 0xB, 0x64, 0x3F, 0x00, 0x00 }, /* 600 600 0.0 */
458 { 0x5, 0x73, 0x35, 0x00, 0x0A }, /* 600 850 3.0 */
459 { 0x6, 0x7F, 0x30, 0x00, 0x0F }, /* 550 1050 5.6 */
460 { 0x5, 0x76, 0x3E, 0x00, 0x01 }, /* 850 900 0.5 */
461 { 0x6, 0x7F, 0x36, 0x00, 0x09 }, /* 750 1050 2.9 */
462 { 0x6, 0x7F, 0x3F, 0x00, 0x00 }, /* 1050 1050 0.0 */
463};
464
465/* Voltage Swing Programming for VccIO 1.05V for HDMI */
466static const struct cnl_ddi_buf_trans cnl_ddi_translations_hdmi_1_05V[] = {
467 /* NT mV Trans mV db */
468 { 0xA, 0x58, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
469 { 0xB, 0x64, 0x37, 0x00, 0x08 }, /* 400 600 3.5 */
470 { 0x5, 0x70, 0x31, 0x00, 0x0E }, /* 400 800 6.0 */
471 { 0xA, 0x5B, 0x3F, 0x00, 0x00 }, /* 450 450 0.0 */
472 { 0xB, 0x64, 0x3F, 0x00, 0x00 }, /* 600 600 0.0 */
473 { 0x5, 0x73, 0x35, 0x00, 0x0A }, /* 600 850 3.0 */
474 { 0x6, 0x7C, 0x32, 0x00, 0x0D }, /* 600 1000 4.4 */
475 { 0x5, 0x70, 0x3F, 0x00, 0x00 }, /* 800 800 0.0 */
476 { 0x6, 0x7C, 0x39, 0x00, 0x06 }, /* 800 1000 1.9 */
477 { 0x6, 0x7F, 0x39, 0x00, 0x06 }, /* 850 1050 1.8 */
478 { 0x6, 0x7F, 0x3F, 0x00, 0x00 }, /* 1050 1050 0.0 */
479};
480
481/* Voltage Swing Programming for VccIO 1.05V for eDP */
482static const struct cnl_ddi_buf_trans cnl_ddi_translations_edp_1_05V[] = {
483 /* NT mV Trans mV db */
484 { 0xA, 0x5E, 0x3A, 0x00, 0x05 }, /* 384 500 2.3 */
485 { 0x0, 0x7F, 0x38, 0x00, 0x07 }, /* 153 200 2.3 */
486 { 0x8, 0x7F, 0x38, 0x00, 0x07 }, /* 192 250 2.3 */
487 { 0x1, 0x7F, 0x38, 0x00, 0x07 }, /* 230 300 2.3 */
488 { 0x9, 0x7F, 0x38, 0x00, 0x07 }, /* 269 350 2.3 */
489 { 0xA, 0x5E, 0x3C, 0x00, 0x03 }, /* 446 500 1.0 */
490 { 0xB, 0x64, 0x39, 0x00, 0x06 }, /* 460 600 2.3 */
491 { 0xE, 0x6A, 0x39, 0x00, 0x06 }, /* 537 700 2.3 */
492 { 0x2, 0x7F, 0x3F, 0x00, 0x00 }, /* 400 400 0.0 */
493};
494
5a5d24dc 495enum port intel_ddi_get_encoder_port(struct intel_encoder *encoder)
fc914639 496{
5a5d24dc 497 switch (encoder->type) {
8cd21b7f 498 case INTEL_OUTPUT_DP_MST:
5a5d24dc 499 return enc_to_mst(&encoder->base)->primary->port;
cca0502b 500 case INTEL_OUTPUT_DP:
8cd21b7f
JN
501 case INTEL_OUTPUT_EDP:
502 case INTEL_OUTPUT_HDMI:
503 case INTEL_OUTPUT_UNKNOWN:
5a5d24dc 504 return enc_to_dig_port(&encoder->base)->port;
8cd21b7f 505 case INTEL_OUTPUT_ANALOG:
5a5d24dc
VS
506 return PORT_E;
507 default:
508 MISSING_CASE(encoder->type);
509 return PORT_A;
fc914639
PZ
510 }
511}
512
a930acd9
VS
513static const struct ddi_buf_trans *
514bdw_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
515{
516 if (dev_priv->vbt.edp.low_vswing) {
517 *n_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
518 return bdw_ddi_translations_edp;
519 } else {
520 *n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
521 return bdw_ddi_translations_dp;
522 }
523}
524
acee2998 525static const struct ddi_buf_trans *
78ab0bae 526skl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 527{
0fdd4918 528 if (IS_SKL_ULX(dev_priv)) {
5f8b2531 529 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_dp);
acee2998 530 return skl_y_ddi_translations_dp;
0fdd4918 531 } else if (IS_SKL_ULT(dev_priv)) {
f8896f5d 532 *n_entries = ARRAY_SIZE(skl_u_ddi_translations_dp);
acee2998 533 return skl_u_ddi_translations_dp;
f8896f5d 534 } else {
f8896f5d 535 *n_entries = ARRAY_SIZE(skl_ddi_translations_dp);
acee2998 536 return skl_ddi_translations_dp;
f8896f5d 537 }
f8896f5d
DW
538}
539
0fdd4918
RV
540static const struct ddi_buf_trans *
541kbl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
542{
543 if (IS_KBL_ULX(dev_priv)) {
544 *n_entries = ARRAY_SIZE(kbl_y_ddi_translations_dp);
545 return kbl_y_ddi_translations_dp;
da411a48 546 } else if (IS_KBL_ULT(dev_priv) || IS_CFL_ULT(dev_priv)) {
0fdd4918
RV
547 *n_entries = ARRAY_SIZE(kbl_u_ddi_translations_dp);
548 return kbl_u_ddi_translations_dp;
549 } else {
550 *n_entries = ARRAY_SIZE(kbl_ddi_translations_dp);
551 return kbl_ddi_translations_dp;
552 }
553}
554
acee2998 555static const struct ddi_buf_trans *
78ab0bae 556skl_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 557{
06411f08 558 if (dev_priv->vbt.edp.low_vswing) {
78ab0bae 559 if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv)) {
5f8b2531 560 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_edp);
acee2998 561 return skl_y_ddi_translations_edp;
da411a48
RV
562 } else if (IS_SKL_ULT(dev_priv) || IS_KBL_ULT(dev_priv) ||
563 IS_CFL_ULT(dev_priv)) {
f8896f5d 564 *n_entries = ARRAY_SIZE(skl_u_ddi_translations_edp);
acee2998 565 return skl_u_ddi_translations_edp;
f8896f5d 566 } else {
f8896f5d 567 *n_entries = ARRAY_SIZE(skl_ddi_translations_edp);
acee2998 568 return skl_ddi_translations_edp;
f8896f5d
DW
569 }
570 }
cd1101cb 571
da411a48 572 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
0fdd4918
RV
573 return kbl_get_buf_trans_dp(dev_priv, n_entries);
574 else
575 return skl_get_buf_trans_dp(dev_priv, n_entries);
f8896f5d
DW
576}
577
578static const struct ddi_buf_trans *
78ab0bae 579skl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
f8896f5d 580{
78ab0bae 581 if (IS_SKL_ULX(dev_priv) || IS_KBL_ULX(dev_priv)) {
5f8b2531 582 *n_entries = ARRAY_SIZE(skl_y_ddi_translations_hdmi);
acee2998 583 return skl_y_ddi_translations_hdmi;
f8896f5d 584 } else {
f8896f5d 585 *n_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
acee2998 586 return skl_ddi_translations_hdmi;
f8896f5d 587 }
f8896f5d
DW
588}
589
edba48fd
VS
590static int skl_buf_trans_num_entries(enum port port, int n_entries)
591{
592 /* Only DDIA and DDIE can select the 10th register with DP */
593 if (port == PORT_A || port == PORT_E)
594 return min(n_entries, 10);
595 else
596 return min(n_entries, 9);
597}
598
d8fe2c7f
VS
599static const struct ddi_buf_trans *
600intel_ddi_get_buf_trans_dp(struct drm_i915_private *dev_priv,
edba48fd 601 enum port port, int *n_entries)
d8fe2c7f
VS
602{
603 if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
edba48fd
VS
604 const struct ddi_buf_trans *ddi_translations =
605 kbl_get_buf_trans_dp(dev_priv, n_entries);
606 *n_entries = skl_buf_trans_num_entries(port, *n_entries);
607 return ddi_translations;
d8fe2c7f 608 } else if (IS_SKYLAKE(dev_priv)) {
edba48fd
VS
609 const struct ddi_buf_trans *ddi_translations =
610 skl_get_buf_trans_dp(dev_priv, n_entries);
611 *n_entries = skl_buf_trans_num_entries(port, *n_entries);
612 return ddi_translations;
d8fe2c7f
VS
613 } else if (IS_BROADWELL(dev_priv)) {
614 *n_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
615 return bdw_ddi_translations_dp;
616 } else if (IS_HASWELL(dev_priv)) {
617 *n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
618 return hsw_ddi_translations_dp;
619 }
620
621 *n_entries = 0;
622 return NULL;
623}
624
625static const struct ddi_buf_trans *
626intel_ddi_get_buf_trans_edp(struct drm_i915_private *dev_priv,
edba48fd 627 enum port port, int *n_entries)
d8fe2c7f
VS
628{
629 if (IS_GEN9_BC(dev_priv)) {
edba48fd
VS
630 const struct ddi_buf_trans *ddi_translations =
631 skl_get_buf_trans_edp(dev_priv, n_entries);
632 *n_entries = skl_buf_trans_num_entries(port, *n_entries);
633 return ddi_translations;
d8fe2c7f
VS
634 } else if (IS_BROADWELL(dev_priv)) {
635 return bdw_get_buf_trans_edp(dev_priv, n_entries);
636 } else if (IS_HASWELL(dev_priv)) {
637 *n_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
638 return hsw_ddi_translations_dp;
639 }
640
641 *n_entries = 0;
642 return NULL;
643}
644
645static const struct ddi_buf_trans *
646intel_ddi_get_buf_trans_fdi(struct drm_i915_private *dev_priv,
647 int *n_entries)
648{
649 if (IS_BROADWELL(dev_priv)) {
650 *n_entries = ARRAY_SIZE(bdw_ddi_translations_fdi);
651 return bdw_ddi_translations_fdi;
652 } else if (IS_HASWELL(dev_priv)) {
653 *n_entries = ARRAY_SIZE(hsw_ddi_translations_fdi);
654 return hsw_ddi_translations_fdi;
655 }
656
657 *n_entries = 0;
658 return NULL;
659}
660
975786ee
VS
661static const struct ddi_buf_trans *
662intel_ddi_get_buf_trans_hdmi(struct drm_i915_private *dev_priv,
663 int *n_entries)
664{
665 if (IS_GEN9_BC(dev_priv)) {
666 return skl_get_buf_trans_hdmi(dev_priv, n_entries);
667 } else if (IS_BROADWELL(dev_priv)) {
668 *n_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
669 return bdw_ddi_translations_hdmi;
670 } else if (IS_HASWELL(dev_priv)) {
671 *n_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
672 return hsw_ddi_translations_hdmi;
673 }
674
675 *n_entries = 0;
676 return NULL;
677}
678
7d4f37b5
VS
679static const struct bxt_ddi_buf_trans *
680bxt_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
681{
682 *n_entries = ARRAY_SIZE(bxt_ddi_translations_dp);
683 return bxt_ddi_translations_dp;
684}
685
686static const struct bxt_ddi_buf_trans *
687bxt_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
688{
689 if (dev_priv->vbt.edp.low_vswing) {
690 *n_entries = ARRAY_SIZE(bxt_ddi_translations_edp);
691 return bxt_ddi_translations_edp;
692 }
693
694 return bxt_get_buf_trans_dp(dev_priv, n_entries);
695}
696
697static const struct bxt_ddi_buf_trans *
698bxt_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
699{
700 *n_entries = ARRAY_SIZE(bxt_ddi_translations_hdmi);
701 return bxt_ddi_translations_hdmi;
702}
703
cf3e0fb4
RV
704static const struct cnl_ddi_buf_trans *
705cnl_get_buf_trans_hdmi(struct drm_i915_private *dev_priv, int *n_entries)
706{
707 u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
708
709 if (voltage == VOLTAGE_INFO_0_85V) {
710 *n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_85V);
711 return cnl_ddi_translations_hdmi_0_85V;
712 } else if (voltage == VOLTAGE_INFO_0_95V) {
713 *n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_0_95V);
714 return cnl_ddi_translations_hdmi_0_95V;
715 } else if (voltage == VOLTAGE_INFO_1_05V) {
716 *n_entries = ARRAY_SIZE(cnl_ddi_translations_hdmi_1_05V);
717 return cnl_ddi_translations_hdmi_1_05V;
83482ca3
AB
718 } else {
719 *n_entries = 1; /* shut up gcc */
cf3e0fb4 720 MISSING_CASE(voltage);
83482ca3 721 }
cf3e0fb4
RV
722 return NULL;
723}
724
725static const struct cnl_ddi_buf_trans *
726cnl_get_buf_trans_dp(struct drm_i915_private *dev_priv, int *n_entries)
727{
728 u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
729
730 if (voltage == VOLTAGE_INFO_0_85V) {
731 *n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_85V);
732 return cnl_ddi_translations_dp_0_85V;
733 } else if (voltage == VOLTAGE_INFO_0_95V) {
734 *n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_0_95V);
735 return cnl_ddi_translations_dp_0_95V;
736 } else if (voltage == VOLTAGE_INFO_1_05V) {
737 *n_entries = ARRAY_SIZE(cnl_ddi_translations_dp_1_05V);
738 return cnl_ddi_translations_dp_1_05V;
83482ca3
AB
739 } else {
740 *n_entries = 1; /* shut up gcc */
cf3e0fb4 741 MISSING_CASE(voltage);
83482ca3 742 }
cf3e0fb4
RV
743 return NULL;
744}
745
746static const struct cnl_ddi_buf_trans *
747cnl_get_buf_trans_edp(struct drm_i915_private *dev_priv, int *n_entries)
748{
749 u32 voltage = I915_READ(CNL_PORT_COMP_DW3) & VOLTAGE_INFO_MASK;
750
751 if (dev_priv->vbt.edp.low_vswing) {
752 if (voltage == VOLTAGE_INFO_0_85V) {
753 *n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_85V);
754 return cnl_ddi_translations_edp_0_85V;
755 } else if (voltage == VOLTAGE_INFO_0_95V) {
756 *n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_0_95V);
757 return cnl_ddi_translations_edp_0_95V;
758 } else if (voltage == VOLTAGE_INFO_1_05V) {
759 *n_entries = ARRAY_SIZE(cnl_ddi_translations_edp_1_05V);
760 return cnl_ddi_translations_edp_1_05V;
83482ca3
AB
761 } else {
762 *n_entries = 1; /* shut up gcc */
cf3e0fb4 763 MISSING_CASE(voltage);
83482ca3 764 }
cf3e0fb4
RV
765 return NULL;
766 } else {
767 return cnl_get_buf_trans_dp(dev_priv, n_entries);
768 }
769}
770
8d8bb85e
VS
771static int intel_ddi_hdmi_level(struct drm_i915_private *dev_priv, enum port port)
772{
d02ace87 773 int n_entries, level, default_entry;
8d8bb85e 774
d02ace87 775 level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
8d8bb85e 776
bf503556 777 if (IS_CANNONLAKE(dev_priv)) {
d02ace87
VS
778 cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
779 default_entry = n_entries - 1;
043eaf36 780 } else if (IS_GEN9_LP(dev_priv)) {
d02ace87
VS
781 bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
782 default_entry = n_entries - 1;
bf503556 783 } else if (IS_GEN9_BC(dev_priv)) {
d02ace87
VS
784 intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
785 default_entry = 8;
8d8bb85e 786 } else if (IS_BROADWELL(dev_priv)) {
d02ace87
VS
787 intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
788 default_entry = 7;
8d8bb85e 789 } else if (IS_HASWELL(dev_priv)) {
d02ace87
VS
790 intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
791 default_entry = 6;
8d8bb85e
VS
792 } else {
793 WARN(1, "ddi translation table missing\n");
975786ee 794 return 0;
8d8bb85e
VS
795 }
796
797 /* Choose a good default if VBT is badly populated */
d02ace87
VS
798 if (level == HDMI_LEVEL_SHIFT_UNKNOWN || level >= n_entries)
799 level = default_entry;
8d8bb85e 800
d02ace87 801 if (WARN_ON_ONCE(n_entries == 0))
21b39d2a 802 return 0;
d02ace87
VS
803 if (WARN_ON_ONCE(level >= n_entries))
804 level = n_entries - 1;
21b39d2a 805
d02ace87 806 return level;
8d8bb85e
VS
807}
808
e58623cb
AR
809/*
810 * Starting with Haswell, DDI port buffers must be programmed with correct
32bdc400
VS
811 * values in advance. This function programs the correct values for
812 * DP/eDP/FDI use cases.
45244b87 813 */
d7c530b2 814static void intel_prepare_dp_ddi_buffers(struct intel_encoder *encoder)
45244b87 815{
6a7e4f99 816 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
75067dde 817 u32 iboost_bit = 0;
7d1c42e6 818 int i, n_entries;
32bdc400 819 enum port port = intel_ddi_get_encoder_port(encoder);
10122051 820 const struct ddi_buf_trans *ddi_translations;
e58623cb 821
7d1c42e6
VS
822 switch (encoder->type) {
823 case INTEL_OUTPUT_EDP:
edba48fd 824 ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port,
7d1c42e6
VS
825 &n_entries);
826 break;
827 case INTEL_OUTPUT_DP:
edba48fd 828 ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port,
7d1c42e6
VS
829 &n_entries);
830 break;
831 case INTEL_OUTPUT_ANALOG:
832 ddi_translations = intel_ddi_get_buf_trans_fdi(dev_priv,
833 &n_entries);
834 break;
835 default:
836 MISSING_CASE(encoder->type);
837 return;
e58623cb
AR
838 }
839
edba48fd
VS
840 /* If we're boosting the current, set bit 31 of trans1 */
841 if (IS_GEN9_BC(dev_priv) &&
842 dev_priv->vbt.ddi_port_info[port].dp_boost_level)
843 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
45244b87 844
7d1c42e6 845 for (i = 0; i < n_entries; i++) {
9712e688
VS
846 I915_WRITE(DDI_BUF_TRANS_LO(port, i),
847 ddi_translations[i].trans1 | iboost_bit);
848 I915_WRITE(DDI_BUF_TRANS_HI(port, i),
849 ddi_translations[i].trans2);
45244b87 850 }
32bdc400
VS
851}
852
853/*
854 * Starting with Haswell, DDI port buffers must be programmed with correct
855 * values in advance. This function programs the correct values for
856 * HDMI/DVI use cases.
857 */
7ea79333 858static void intel_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
d02ace87 859 int level)
32bdc400
VS
860{
861 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
862 u32 iboost_bit = 0;
d02ace87 863 int n_entries;
32bdc400 864 enum port port = intel_ddi_get_encoder_port(encoder);
d02ace87 865 const struct ddi_buf_trans *ddi_translations;
ce4dd49e 866
d02ace87 867 ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
1edaaa2f 868
d02ace87 869 if (WARN_ON_ONCE(!ddi_translations))
21b39d2a 870 return;
d02ace87
VS
871 if (WARN_ON_ONCE(level >= n_entries))
872 level = n_entries - 1;
21b39d2a 873
975786ee
VS
874 /* If we're boosting the current, set bit 31 of trans1 */
875 if (IS_GEN9_BC(dev_priv) &&
876 dev_priv->vbt.ddi_port_info[port].hdmi_boost_level)
877 iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
32bdc400 878
6acab15a 879 /* Entry 9 is for HDMI: */
ed9c77d2 880 I915_WRITE(DDI_BUF_TRANS_LO(port, 9),
d02ace87 881 ddi_translations[level].trans1 | iboost_bit);
ed9c77d2 882 I915_WRITE(DDI_BUF_TRANS_HI(port, 9),
d02ace87 883 ddi_translations[level].trans2);
45244b87
ED
884}
885
248138b5
PZ
886static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
887 enum port port)
888{
f0f59a00 889 i915_reg_t reg = DDI_BUF_CTL(port);
248138b5
PZ
890 int i;
891
3449ca85 892 for (i = 0; i < 16; i++) {
248138b5
PZ
893 udelay(1);
894 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
895 return;
896 }
897 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
898}
c82e4d26 899
5f88a9c6 900static uint32_t hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
c856052a
ACO
901{
902 switch (pll->id) {
903 case DPLL_ID_WRPLL1:
904 return PORT_CLK_SEL_WRPLL1;
905 case DPLL_ID_WRPLL2:
906 return PORT_CLK_SEL_WRPLL2;
907 case DPLL_ID_SPLL:
908 return PORT_CLK_SEL_SPLL;
909 case DPLL_ID_LCPLL_810:
910 return PORT_CLK_SEL_LCPLL_810;
911 case DPLL_ID_LCPLL_1350:
912 return PORT_CLK_SEL_LCPLL_1350;
913 case DPLL_ID_LCPLL_2700:
914 return PORT_CLK_SEL_LCPLL_2700;
915 default:
916 MISSING_CASE(pll->id);
917 return PORT_CLK_SEL_NONE;
918 }
919}
920
c82e4d26
ED
921/* Starting with Haswell, different DDI ports can work in FDI mode for
922 * connection to the PCH-located connectors. For this, it is necessary to train
923 * both the DDI port and PCH receiver for the desired DDI buffer settings.
924 *
925 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
926 * please note that when FDI mode is active on DDI E, it shares 2 lines with
927 * DDI A (which is used for eDP)
928 */
929
dc4a1094
ACO
930void hsw_fdi_link_train(struct intel_crtc *crtc,
931 const struct intel_crtc_state *crtc_state)
c82e4d26 932{
4cbe4b2b 933 struct drm_device *dev = crtc->base.dev;
fac5e23e 934 struct drm_i915_private *dev_priv = to_i915(dev);
6a7e4f99 935 struct intel_encoder *encoder;
c856052a 936 u32 temp, i, rx_ctl_val, ddi_pll_sel;
c82e4d26 937
4cbe4b2b 938 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
6a7e4f99 939 WARN_ON(encoder->type != INTEL_OUTPUT_ANALOG);
32bdc400 940 intel_prepare_dp_ddi_buffers(encoder);
6a7e4f99
VS
941 }
942
04945641
PZ
943 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
944 * mode set "sequence for CRT port" document:
945 * - TP1 to TP2 time with the default value
946 * - FDI delay to 90h
8693a824
DL
947 *
948 * WaFDIAutoLinkSetTimingOverrride:hsw
04945641 949 */
eede3b53 950 I915_WRITE(FDI_RX_MISC(PIPE_A), FDI_RX_PWRDN_LANE1_VAL(2) |
04945641
PZ
951 FDI_RX_PWRDN_LANE0_VAL(2) |
952 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
953
954 /* Enable the PCH Receiver FDI PLL */
3e68320e 955 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
33d29b14 956 FDI_RX_PLL_ENABLE |
dc4a1094 957 FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
eede3b53
VS
958 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
959 POSTING_READ(FDI_RX_CTL(PIPE_A));
04945641
PZ
960 udelay(220);
961
962 /* Switch from Rawclk to PCDclk */
963 rx_ctl_val |= FDI_PCDCLK;
eede3b53 964 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
04945641
PZ
965
966 /* Configure Port Clock Select */
dc4a1094 967 ddi_pll_sel = hsw_pll_to_ddi_pll_sel(crtc_state->shared_dpll);
c856052a
ACO
968 I915_WRITE(PORT_CLK_SEL(PORT_E), ddi_pll_sel);
969 WARN_ON(ddi_pll_sel != PORT_CLK_SEL_SPLL);
04945641
PZ
970
971 /* Start the training iterating through available voltages and emphasis,
972 * testing each value twice. */
10122051 973 for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
c82e4d26
ED
974 /* Configure DP_TP_CTL with auto-training */
975 I915_WRITE(DP_TP_CTL(PORT_E),
976 DP_TP_CTL_FDI_AUTOTRAIN |
977 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
978 DP_TP_CTL_LINK_TRAIN_PAT1 |
979 DP_TP_CTL_ENABLE);
980
876a8cdf
DL
981 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
982 * DDI E does not support port reversal, the functionality is
983 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
984 * port reversal bit */
c82e4d26 985 I915_WRITE(DDI_BUF_CTL(PORT_E),
04945641 986 DDI_BUF_CTL_ENABLE |
dc4a1094 987 ((crtc_state->fdi_lanes - 1) << 1) |
c5fe6a06 988 DDI_BUF_TRANS_SELECT(i / 2));
04945641 989 POSTING_READ(DDI_BUF_CTL(PORT_E));
c82e4d26
ED
990
991 udelay(600);
992
04945641 993 /* Program PCH FDI Receiver TU */
eede3b53 994 I915_WRITE(FDI_RX_TUSIZE1(PIPE_A), TU_SIZE(64));
04945641
PZ
995
996 /* Enable PCH FDI Receiver with auto-training */
997 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
eede3b53
VS
998 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
999 POSTING_READ(FDI_RX_CTL(PIPE_A));
04945641
PZ
1000
1001 /* Wait for FDI receiver lane calibration */
1002 udelay(30);
1003
1004 /* Unset FDI_RX_MISC pwrdn lanes */
eede3b53 1005 temp = I915_READ(FDI_RX_MISC(PIPE_A));
04945641 1006 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
eede3b53
VS
1007 I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1008 POSTING_READ(FDI_RX_MISC(PIPE_A));
04945641
PZ
1009
1010 /* Wait for FDI auto training time */
1011 udelay(5);
c82e4d26
ED
1012
1013 temp = I915_READ(DP_TP_STATUS(PORT_E));
1014 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
04945641 1015 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
a308ccb3
VS
1016 break;
1017 }
c82e4d26 1018
a308ccb3
VS
1019 /*
1020 * Leave things enabled even if we failed to train FDI.
1021 * Results in less fireworks from the state checker.
1022 */
1023 if (i == ARRAY_SIZE(hsw_ddi_translations_fdi) * 2 - 1) {
1024 DRM_ERROR("FDI link training failed!\n");
1025 break;
c82e4d26 1026 }
04945641 1027
5b421c57
VS
1028 rx_ctl_val &= ~FDI_RX_ENABLE;
1029 I915_WRITE(FDI_RX_CTL(PIPE_A), rx_ctl_val);
1030 POSTING_READ(FDI_RX_CTL(PIPE_A));
1031
248138b5
PZ
1032 temp = I915_READ(DDI_BUF_CTL(PORT_E));
1033 temp &= ~DDI_BUF_CTL_ENABLE;
1034 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
1035 POSTING_READ(DDI_BUF_CTL(PORT_E));
1036
04945641 1037 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
248138b5
PZ
1038 temp = I915_READ(DP_TP_CTL(PORT_E));
1039 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1040 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
1041 I915_WRITE(DP_TP_CTL(PORT_E), temp);
1042 POSTING_READ(DP_TP_CTL(PORT_E));
1043
1044 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
04945641 1045
04945641 1046 /* Reset FDI_RX_MISC pwrdn lanes */
eede3b53 1047 temp = I915_READ(FDI_RX_MISC(PIPE_A));
04945641
PZ
1048 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
1049 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
eede3b53
VS
1050 I915_WRITE(FDI_RX_MISC(PIPE_A), temp);
1051 POSTING_READ(FDI_RX_MISC(PIPE_A));
c82e4d26
ED
1052 }
1053
a308ccb3
VS
1054 /* Enable normal pixel sending for FDI */
1055 I915_WRITE(DP_TP_CTL(PORT_E),
1056 DP_TP_CTL_FDI_AUTOTRAIN |
1057 DP_TP_CTL_LINK_TRAIN_NORMAL |
1058 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
1059 DP_TP_CTL_ENABLE);
c82e4d26 1060}
0e72a5b5 1061
d7c530b2 1062static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
44905a27
DA
1063{
1064 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
1065 struct intel_digital_port *intel_dig_port =
1066 enc_to_dig_port(&encoder->base);
1067
1068 intel_dp->DP = intel_dig_port->saved_port_bits |
c5fe6a06 1069 DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
901c2daf 1070 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
44905a27
DA
1071}
1072
8d9ddbcb 1073static struct intel_encoder *
e9ce1a62 1074intel_ddi_get_crtc_encoder(struct intel_crtc *crtc)
8d9ddbcb 1075{
e9ce1a62 1076 struct drm_device *dev = crtc->base.dev;
1524e93e 1077 struct intel_encoder *encoder, *ret = NULL;
8d9ddbcb
PZ
1078 int num_encoders = 0;
1079
1524e93e
SS
1080 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
1081 ret = encoder;
8d9ddbcb
PZ
1082 num_encoders++;
1083 }
1084
1085 if (num_encoders != 1)
84f44ce7 1086 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
e9ce1a62 1087 pipe_name(crtc->pipe));
8d9ddbcb
PZ
1088
1089 BUG_ON(ret == NULL);
1090 return ret;
1091}
1092
44a126ba
PZ
1093/* Finds the only possible encoder associated with the given CRTC. */
1094struct intel_encoder *
3165c074 1095intel_ddi_get_crtc_new_encoder(struct intel_crtc_state *crtc_state)
d0737e1d 1096{
3165c074
ACO
1097 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1098 struct intel_encoder *ret = NULL;
1099 struct drm_atomic_state *state;
da3ced29
ACO
1100 struct drm_connector *connector;
1101 struct drm_connector_state *connector_state;
d0737e1d 1102 int num_encoders = 0;
3165c074 1103 int i;
d0737e1d 1104
3165c074
ACO
1105 state = crtc_state->base.state;
1106
b77c7a90 1107 for_each_new_connector_in_state(state, connector, connector_state, i) {
da3ced29 1108 if (connector_state->crtc != crtc_state->base.crtc)
3165c074
ACO
1109 continue;
1110
da3ced29 1111 ret = to_intel_encoder(connector_state->best_encoder);
3165c074 1112 num_encoders++;
d0737e1d
ACO
1113 }
1114
1115 WARN(num_encoders != 1, "%d encoders on crtc for pipe %c\n", num_encoders,
1116 pipe_name(crtc->pipe));
1117
1118 BUG_ON(ret == NULL);
1119 return ret;
1120}
1121
1c0b85c5 1122#define LC_FREQ 2700
1c0b85c5 1123
f0f59a00
VS
1124static int hsw_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
1125 i915_reg_t reg)
11578553
JB
1126{
1127 int refclk = LC_FREQ;
1128 int n, p, r;
1129 u32 wrpll;
1130
1131 wrpll = I915_READ(reg);
114fe488
DV
1132 switch (wrpll & WRPLL_PLL_REF_MASK) {
1133 case WRPLL_PLL_SSC:
1134 case WRPLL_PLL_NON_SSC:
11578553
JB
1135 /*
1136 * We could calculate spread here, but our checking
1137 * code only cares about 5% accuracy, and spread is a max of
1138 * 0.5% downspread.
1139 */
1140 refclk = 135;
1141 break;
114fe488 1142 case WRPLL_PLL_LCPLL:
11578553
JB
1143 refclk = LC_FREQ;
1144 break;
1145 default:
1146 WARN(1, "bad wrpll refclk\n");
1147 return 0;
1148 }
1149
1150 r = wrpll & WRPLL_DIVIDER_REF_MASK;
1151 p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
1152 n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
1153
20f0ec16
JB
1154 /* Convert to KHz, p & r have a fixed point portion */
1155 return (refclk * n * 100) / (p * r);
11578553
JB
1156}
1157
540e732c
S
1158static int skl_calc_wrpll_link(struct drm_i915_private *dev_priv,
1159 uint32_t dpll)
1160{
f0f59a00 1161 i915_reg_t cfgcr1_reg, cfgcr2_reg;
540e732c
S
1162 uint32_t cfgcr1_val, cfgcr2_val;
1163 uint32_t p0, p1, p2, dco_freq;
1164
923c1241
VS
1165 cfgcr1_reg = DPLL_CFGCR1(dpll);
1166 cfgcr2_reg = DPLL_CFGCR2(dpll);
540e732c
S
1167
1168 cfgcr1_val = I915_READ(cfgcr1_reg);
1169 cfgcr2_val = I915_READ(cfgcr2_reg);
1170
1171 p0 = cfgcr2_val & DPLL_CFGCR2_PDIV_MASK;
1172 p2 = cfgcr2_val & DPLL_CFGCR2_KDIV_MASK;
1173
1174 if (cfgcr2_val & DPLL_CFGCR2_QDIV_MODE(1))
1175 p1 = (cfgcr2_val & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
1176 else
1177 p1 = 1;
1178
1179
1180 switch (p0) {
1181 case DPLL_CFGCR2_PDIV_1:
1182 p0 = 1;
1183 break;
1184 case DPLL_CFGCR2_PDIV_2:
1185 p0 = 2;
1186 break;
1187 case DPLL_CFGCR2_PDIV_3:
1188 p0 = 3;
1189 break;
1190 case DPLL_CFGCR2_PDIV_7:
1191 p0 = 7;
1192 break;
1193 }
1194
1195 switch (p2) {
1196 case DPLL_CFGCR2_KDIV_5:
1197 p2 = 5;
1198 break;
1199 case DPLL_CFGCR2_KDIV_2:
1200 p2 = 2;
1201 break;
1202 case DPLL_CFGCR2_KDIV_3:
1203 p2 = 3;
1204 break;
1205 case DPLL_CFGCR2_KDIV_1:
1206 p2 = 1;
1207 break;
1208 }
1209
1210 dco_freq = (cfgcr1_val & DPLL_CFGCR1_DCO_INTEGER_MASK) * 24 * 1000;
1211
1212 dco_freq += (((cfgcr1_val & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9) * 24 *
1213 1000) / 0x8000;
1214
1215 return dco_freq / (p0 * p1 * p2 * 5);
1216}
1217
a9701a89
RV
1218static int cnl_calc_wrpll_link(struct drm_i915_private *dev_priv,
1219 uint32_t pll_id)
1220{
1221 uint32_t cfgcr0, cfgcr1;
1222 uint32_t p0, p1, p2, dco_freq, ref_clock;
1223
1224 cfgcr0 = I915_READ(CNL_DPLL_CFGCR0(pll_id));
1225 cfgcr1 = I915_READ(CNL_DPLL_CFGCR1(pll_id));
1226
1227 p0 = cfgcr1 & DPLL_CFGCR1_PDIV_MASK;
1228 p2 = cfgcr1 & DPLL_CFGCR1_KDIV_MASK;
1229
1230 if (cfgcr1 & DPLL_CFGCR1_QDIV_MODE(1))
1231 p1 = (cfgcr1 & DPLL_CFGCR1_QDIV_RATIO_MASK) >>
1232 DPLL_CFGCR1_QDIV_RATIO_SHIFT;
1233 else
1234 p1 = 1;
1235
1236
1237 switch (p0) {
1238 case DPLL_CFGCR1_PDIV_2:
1239 p0 = 2;
1240 break;
1241 case DPLL_CFGCR1_PDIV_3:
1242 p0 = 3;
1243 break;
1244 case DPLL_CFGCR1_PDIV_5:
1245 p0 = 5;
1246 break;
1247 case DPLL_CFGCR1_PDIV_7:
1248 p0 = 7;
1249 break;
1250 }
1251
1252 switch (p2) {
1253 case DPLL_CFGCR1_KDIV_1:
1254 p2 = 1;
1255 break;
1256 case DPLL_CFGCR1_KDIV_2:
1257 p2 = 2;
1258 break;
1259 case DPLL_CFGCR1_KDIV_4:
1260 p2 = 4;
1261 break;
1262 }
1263
1264 ref_clock = dev_priv->cdclk.hw.ref;
1265
1266 dco_freq = (cfgcr0 & DPLL_CFGCR0_DCO_INTEGER_MASK) * ref_clock;
1267
1268 dco_freq += (((cfgcr0 & DPLL_CFGCR0_DCO_FRACTION_MASK) >>
442aa277 1269 DPLL_CFGCR0_DCO_FRACTION_SHIFT) * ref_clock) / 0x8000;
a9701a89 1270
0e005888
PZ
1271 if (WARN_ON(p0 == 0 || p1 == 0 || p2 == 0))
1272 return 0;
1273
a9701a89
RV
1274 return dco_freq / (p0 * p1 * p2 * 5);
1275}
1276
398a017e
VS
1277static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
1278{
1279 int dotclock;
1280
1281 if (pipe_config->has_pch_encoder)
1282 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1283 &pipe_config->fdi_m_n);
37a5650b 1284 else if (intel_crtc_has_dp_encoder(pipe_config))
398a017e
VS
1285 dotclock = intel_dotclock_calculate(pipe_config->port_clock,
1286 &pipe_config->dp_m_n);
1287 else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp == 36)
1288 dotclock = pipe_config->port_clock * 2 / 3;
1289 else
1290 dotclock = pipe_config->port_clock;
1291
b22ca995
SS
1292 if (pipe_config->ycbcr420)
1293 dotclock *= 2;
1294
398a017e
VS
1295 if (pipe_config->pixel_multiplier)
1296 dotclock /= pipe_config->pixel_multiplier;
1297
1298 pipe_config->base.adjusted_mode.crtc_clock = dotclock;
1299}
540e732c 1300
a9701a89
RV
1301static void cnl_ddi_clock_get(struct intel_encoder *encoder,
1302 struct intel_crtc_state *pipe_config)
1303{
1304 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1305 int link_clock = 0;
1306 uint32_t cfgcr0, pll_id;
1307
1308 pll_id = intel_get_shared_dpll_id(dev_priv, pipe_config->shared_dpll);
1309
1310 cfgcr0 = I915_READ(CNL_DPLL_CFGCR0(pll_id));
1311
1312 if (cfgcr0 & DPLL_CFGCR0_HDMI_MODE) {
1313 link_clock = cnl_calc_wrpll_link(dev_priv, pll_id);
1314 } else {
1315 link_clock = cfgcr0 & DPLL_CFGCR0_LINK_RATE_MASK;
1316
1317 switch (link_clock) {
1318 case DPLL_CFGCR0_LINK_RATE_810:
1319 link_clock = 81000;
1320 break;
1321 case DPLL_CFGCR0_LINK_RATE_1080:
1322 link_clock = 108000;
1323 break;
1324 case DPLL_CFGCR0_LINK_RATE_1350:
1325 link_clock = 135000;
1326 break;
1327 case DPLL_CFGCR0_LINK_RATE_1620:
1328 link_clock = 162000;
1329 break;
1330 case DPLL_CFGCR0_LINK_RATE_2160:
1331 link_clock = 216000;
1332 break;
1333 case DPLL_CFGCR0_LINK_RATE_2700:
1334 link_clock = 270000;
1335 break;
1336 case DPLL_CFGCR0_LINK_RATE_3240:
1337 link_clock = 324000;
1338 break;
1339 case DPLL_CFGCR0_LINK_RATE_4050:
1340 link_clock = 405000;
1341 break;
1342 default:
1343 WARN(1, "Unsupported link rate\n");
1344 break;
1345 }
1346 link_clock *= 2;
1347 }
1348
1349 pipe_config->port_clock = link_clock;
1350
1351 ddi_dotclock_get(pipe_config);
1352}
1353
540e732c 1354static void skl_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 1355 struct intel_crtc_state *pipe_config)
540e732c 1356{
fac5e23e 1357 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
540e732c
S
1358 int link_clock = 0;
1359 uint32_t dpll_ctl1, dpll;
1360
c856052a 1361 dpll = intel_get_shared_dpll_id(dev_priv, pipe_config->shared_dpll);
540e732c
S
1362
1363 dpll_ctl1 = I915_READ(DPLL_CTRL1);
1364
1365 if (dpll_ctl1 & DPLL_CTRL1_HDMI_MODE(dpll)) {
1366 link_clock = skl_calc_wrpll_link(dev_priv, dpll);
1367 } else {
71cd8423
DL
1368 link_clock = dpll_ctl1 & DPLL_CTRL1_LINK_RATE_MASK(dpll);
1369 link_clock >>= DPLL_CTRL1_LINK_RATE_SHIFT(dpll);
540e732c
S
1370
1371 switch (link_clock) {
71cd8423 1372 case DPLL_CTRL1_LINK_RATE_810:
540e732c
S
1373 link_clock = 81000;
1374 break;
71cd8423 1375 case DPLL_CTRL1_LINK_RATE_1080:
a8f3ef61
SJ
1376 link_clock = 108000;
1377 break;
71cd8423 1378 case DPLL_CTRL1_LINK_RATE_1350:
540e732c
S
1379 link_clock = 135000;
1380 break;
71cd8423 1381 case DPLL_CTRL1_LINK_RATE_1620:
a8f3ef61
SJ
1382 link_clock = 162000;
1383 break;
71cd8423 1384 case DPLL_CTRL1_LINK_RATE_2160:
a8f3ef61
SJ
1385 link_clock = 216000;
1386 break;
71cd8423 1387 case DPLL_CTRL1_LINK_RATE_2700:
540e732c
S
1388 link_clock = 270000;
1389 break;
1390 default:
1391 WARN(1, "Unsupported link rate\n");
1392 break;
1393 }
1394 link_clock *= 2;
1395 }
1396
1397 pipe_config->port_clock = link_clock;
1398
398a017e 1399 ddi_dotclock_get(pipe_config);
540e732c
S
1400}
1401
3d51278a 1402static void hsw_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 1403 struct intel_crtc_state *pipe_config)
11578553 1404{
fac5e23e 1405 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
11578553
JB
1406 int link_clock = 0;
1407 u32 val, pll;
1408
c856052a 1409 val = hsw_pll_to_ddi_pll_sel(pipe_config->shared_dpll);
11578553
JB
1410 switch (val & PORT_CLK_SEL_MASK) {
1411 case PORT_CLK_SEL_LCPLL_810:
1412 link_clock = 81000;
1413 break;
1414 case PORT_CLK_SEL_LCPLL_1350:
1415 link_clock = 135000;
1416 break;
1417 case PORT_CLK_SEL_LCPLL_2700:
1418 link_clock = 270000;
1419 break;
1420 case PORT_CLK_SEL_WRPLL1:
01403de3 1421 link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(0));
11578553
JB
1422 break;
1423 case PORT_CLK_SEL_WRPLL2:
01403de3 1424 link_clock = hsw_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL(1));
11578553
JB
1425 break;
1426 case PORT_CLK_SEL_SPLL:
1427 pll = I915_READ(SPLL_CTL) & SPLL_PLL_FREQ_MASK;
1428 if (pll == SPLL_PLL_FREQ_810MHz)
1429 link_clock = 81000;
1430 else if (pll == SPLL_PLL_FREQ_1350MHz)
1431 link_clock = 135000;
1432 else if (pll == SPLL_PLL_FREQ_2700MHz)
1433 link_clock = 270000;
1434 else {
1435 WARN(1, "bad spll freq\n");
1436 return;
1437 }
1438 break;
1439 default:
1440 WARN(1, "bad port clock sel\n");
1441 return;
1442 }
1443
1444 pipe_config->port_clock = link_clock * 2;
1445
398a017e 1446 ddi_dotclock_get(pipe_config);
11578553
JB
1447}
1448
977bb38d
S
1449static int bxt_calc_pll_link(struct drm_i915_private *dev_priv,
1450 enum intel_dpll_id dpll)
1451{
aa610dcb
ID
1452 struct intel_shared_dpll *pll;
1453 struct intel_dpll_hw_state *state;
9e2c8475 1454 struct dpll clock;
aa610dcb
ID
1455
1456 /* For DDI ports we always use a shared PLL. */
1457 if (WARN_ON(dpll == DPLL_ID_PRIVATE))
1458 return 0;
1459
1460 pll = &dev_priv->shared_dplls[dpll];
2c42e535 1461 state = &pll->state.hw_state;
aa610dcb
ID
1462
1463 clock.m1 = 2;
1464 clock.m2 = (state->pll0 & PORT_PLL_M2_MASK) << 22;
1465 if (state->pll3 & PORT_PLL_M2_FRAC_ENABLE)
1466 clock.m2 |= state->pll2 & PORT_PLL_M2_FRAC_MASK;
1467 clock.n = (state->pll1 & PORT_PLL_N_MASK) >> PORT_PLL_N_SHIFT;
1468 clock.p1 = (state->ebb0 & PORT_PLL_P1_MASK) >> PORT_PLL_P1_SHIFT;
1469 clock.p2 = (state->ebb0 & PORT_PLL_P2_MASK) >> PORT_PLL_P2_SHIFT;
1470
1471 return chv_calc_dpll_params(100000, &clock);
977bb38d
S
1472}
1473
1474static void bxt_ddi_clock_get(struct intel_encoder *encoder,
1475 struct intel_crtc_state *pipe_config)
1476{
fac5e23e 1477 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
977bb38d
S
1478 enum port port = intel_ddi_get_encoder_port(encoder);
1479 uint32_t dpll = port;
1480
398a017e 1481 pipe_config->port_clock = bxt_calc_pll_link(dev_priv, dpll);
977bb38d 1482
398a017e 1483 ddi_dotclock_get(pipe_config);
977bb38d
S
1484}
1485
3d51278a 1486void intel_ddi_clock_get(struct intel_encoder *encoder,
5cec258b 1487 struct intel_crtc_state *pipe_config)
3d51278a 1488{
0853723b 1489 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
22606a18 1490
0853723b 1491 if (INTEL_GEN(dev_priv) <= 8)
22606a18 1492 hsw_ddi_clock_get(encoder, pipe_config);
b976dc53 1493 else if (IS_GEN9_BC(dev_priv))
22606a18 1494 skl_ddi_clock_get(encoder, pipe_config);
cc3f90f0 1495 else if (IS_GEN9_LP(dev_priv))
977bb38d 1496 bxt_ddi_clock_get(encoder, pipe_config);
a9701a89
RV
1497 else if (IS_CANNONLAKE(dev_priv))
1498 cnl_ddi_clock_get(encoder, pipe_config);
3d51278a
DV
1499}
1500
3dc38eea 1501void intel_ddi_set_pipe_settings(const struct intel_crtc_state *crtc_state)
dae84799 1502{
3dc38eea 1503 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1504 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1524e93e 1505 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
3dc38eea 1506 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1524e93e 1507 int type = encoder->type;
dae84799
PZ
1508 uint32_t temp;
1509
cca0502b 1510 if (type == INTEL_OUTPUT_DP || type == INTEL_OUTPUT_EDP || type == INTEL_OUTPUT_DP_MST) {
4d1de975
JN
1511 WARN_ON(transcoder_is_dsi(cpu_transcoder));
1512
c9809791 1513 temp = TRANS_MSA_SYNC_CLK;
3dc38eea 1514 switch (crtc_state->pipe_bpp) {
dae84799 1515 case 18:
c9809791 1516 temp |= TRANS_MSA_6_BPC;
dae84799
PZ
1517 break;
1518 case 24:
c9809791 1519 temp |= TRANS_MSA_8_BPC;
dae84799
PZ
1520 break;
1521 case 30:
c9809791 1522 temp |= TRANS_MSA_10_BPC;
dae84799
PZ
1523 break;
1524 case 36:
c9809791 1525 temp |= TRANS_MSA_12_BPC;
dae84799
PZ
1526 break;
1527 default:
4e53c2e0 1528 BUG();
dae84799 1529 }
c9809791 1530 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
dae84799
PZ
1531 }
1532}
1533
3dc38eea
ACO
1534void intel_ddi_set_vc_payload_alloc(const struct intel_crtc_state *crtc_state,
1535 bool state)
0e32b39c 1536{
3dc38eea 1537 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1538 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3dc38eea 1539 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
0e32b39c
DA
1540 uint32_t temp;
1541 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1542 if (state == true)
1543 temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1544 else
1545 temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1546 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1547}
1548
3dc38eea 1549void intel_ddi_enable_transcoder_func(const struct intel_crtc_state *crtc_state)
8d9ddbcb 1550{
3dc38eea 1551 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1524e93e 1552 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
e9ce1a62
ACO
1553 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1554 enum pipe pipe = crtc->pipe;
3dc38eea 1555 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1524e93e
SS
1556 enum port port = intel_ddi_get_encoder_port(encoder);
1557 int type = encoder->type;
8d9ddbcb
PZ
1558 uint32_t temp;
1559
ad80a810
PZ
1560 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1561 temp = TRANS_DDI_FUNC_ENABLE;
174edf1f 1562 temp |= TRANS_DDI_SELECT_PORT(port);
dfcef252 1563
3dc38eea 1564 switch (crtc_state->pipe_bpp) {
dfcef252 1565 case 18:
ad80a810 1566 temp |= TRANS_DDI_BPC_6;
dfcef252
PZ
1567 break;
1568 case 24:
ad80a810 1569 temp |= TRANS_DDI_BPC_8;
dfcef252
PZ
1570 break;
1571 case 30:
ad80a810 1572 temp |= TRANS_DDI_BPC_10;
dfcef252
PZ
1573 break;
1574 case 36:
ad80a810 1575 temp |= TRANS_DDI_BPC_12;
dfcef252
PZ
1576 break;
1577 default:
4e53c2e0 1578 BUG();
dfcef252 1579 }
72662e10 1580
3dc38eea 1581 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
ad80a810 1582 temp |= TRANS_DDI_PVSYNC;
3dc38eea 1583 if (crtc_state->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
ad80a810 1584 temp |= TRANS_DDI_PHSYNC;
f63eb7c4 1585
e6f0bfc4
PZ
1586 if (cpu_transcoder == TRANSCODER_EDP) {
1587 switch (pipe) {
1588 case PIPE_A:
c7670b10
PZ
1589 /* On Haswell, can only use the always-on power well for
1590 * eDP when not using the panel fitter, and when not
1591 * using motion blur mitigation (which we don't
1592 * support). */
772c2a51 1593 if (IS_HASWELL(dev_priv) &&
3dc38eea
ACO
1594 (crtc_state->pch_pfit.enabled ||
1595 crtc_state->pch_pfit.force_thru))
d6dd9eb1
DV
1596 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1597 else
1598 temp |= TRANS_DDI_EDP_INPUT_A_ON;
e6f0bfc4
PZ
1599 break;
1600 case PIPE_B:
1601 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1602 break;
1603 case PIPE_C:
1604 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1605 break;
1606 default:
1607 BUG();
1608 break;
1609 }
1610 }
1611
7739c33b 1612 if (type == INTEL_OUTPUT_HDMI) {
3dc38eea 1613 if (crtc_state->has_hdmi_sink)
ad80a810 1614 temp |= TRANS_DDI_MODE_SELECT_HDMI;
8d9ddbcb 1615 else
ad80a810 1616 temp |= TRANS_DDI_MODE_SELECT_DVI;
15953637
SS
1617
1618 if (crtc_state->hdmi_scrambling)
1619 temp |= TRANS_DDI_HDMI_SCRAMBLING_MASK;
1620 if (crtc_state->hdmi_high_tmds_clock_ratio)
1621 temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
7739c33b 1622 } else if (type == INTEL_OUTPUT_ANALOG) {
ad80a810 1623 temp |= TRANS_DDI_MODE_SELECT_FDI;
3dc38eea 1624 temp |= (crtc_state->fdi_lanes - 1) << 1;
cca0502b 1625 } else if (type == INTEL_OUTPUT_DP ||
7739c33b 1626 type == INTEL_OUTPUT_EDP) {
64ee2fd2 1627 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
3dc38eea 1628 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
0e32b39c 1629 } else if (type == INTEL_OUTPUT_DP_MST) {
64ee2fd2 1630 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
3dc38eea 1631 temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
8d9ddbcb 1632 } else {
84f44ce7 1633 WARN(1, "Invalid encoder type %d for pipe %c\n",
1524e93e 1634 encoder->type, pipe_name(pipe));
8d9ddbcb
PZ
1635 }
1636
ad80a810 1637 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
8d9ddbcb 1638}
72662e10 1639
ad80a810
PZ
1640void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
1641 enum transcoder cpu_transcoder)
8d9ddbcb 1642{
f0f59a00 1643 i915_reg_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
8d9ddbcb
PZ
1644 uint32_t val = I915_READ(reg);
1645
0e32b39c 1646 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK | TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
ad80a810 1647 val |= TRANS_DDI_PORT_NONE;
8d9ddbcb 1648 I915_WRITE(reg, val);
72662e10
ED
1649}
1650
bcbc889b
PZ
1651bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1652{
1653 struct drm_device *dev = intel_connector->base.dev;
fac5e23e 1654 struct drm_i915_private *dev_priv = to_i915(dev);
1524e93e 1655 struct intel_encoder *encoder = intel_connector->encoder;
bcbc889b 1656 int type = intel_connector->base.connector_type;
1524e93e 1657 enum port port = intel_ddi_get_encoder_port(encoder);
bcbc889b
PZ
1658 enum pipe pipe = 0;
1659 enum transcoder cpu_transcoder;
1660 uint32_t tmp;
e27daab4 1661 bool ret;
bcbc889b 1662
79f255a0 1663 if (!intel_display_power_get_if_enabled(dev_priv,
1524e93e 1664 encoder->power_domain))
882244a3
PZ
1665 return false;
1666
1524e93e 1667 if (!encoder->get_hw_state(encoder, &pipe)) {
e27daab4
ID
1668 ret = false;
1669 goto out;
1670 }
bcbc889b
PZ
1671
1672 if (port == PORT_A)
1673 cpu_transcoder = TRANSCODER_EDP;
1674 else
1a240d4d 1675 cpu_transcoder = (enum transcoder) pipe;
bcbc889b
PZ
1676
1677 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1678
1679 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1680 case TRANS_DDI_MODE_SELECT_HDMI:
1681 case TRANS_DDI_MODE_SELECT_DVI:
e27daab4
ID
1682 ret = type == DRM_MODE_CONNECTOR_HDMIA;
1683 break;
bcbc889b
PZ
1684
1685 case TRANS_DDI_MODE_SELECT_DP_SST:
e27daab4
ID
1686 ret = type == DRM_MODE_CONNECTOR_eDP ||
1687 type == DRM_MODE_CONNECTOR_DisplayPort;
1688 break;
1689
0e32b39c
DA
1690 case TRANS_DDI_MODE_SELECT_DP_MST:
1691 /* if the transcoder is in MST state then
1692 * connector isn't connected */
e27daab4
ID
1693 ret = false;
1694 break;
bcbc889b
PZ
1695
1696 case TRANS_DDI_MODE_SELECT_FDI:
e27daab4
ID
1697 ret = type == DRM_MODE_CONNECTOR_VGA;
1698 break;
bcbc889b
PZ
1699
1700 default:
e27daab4
ID
1701 ret = false;
1702 break;
bcbc889b 1703 }
e27daab4
ID
1704
1705out:
1524e93e 1706 intel_display_power_put(dev_priv, encoder->power_domain);
e27daab4
ID
1707
1708 return ret;
bcbc889b
PZ
1709}
1710
85234cdc
DV
1711bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1712 enum pipe *pipe)
1713{
1714 struct drm_device *dev = encoder->base.dev;
fac5e23e 1715 struct drm_i915_private *dev_priv = to_i915(dev);
fe43d3f5 1716 enum port port = intel_ddi_get_encoder_port(encoder);
85234cdc
DV
1717 u32 tmp;
1718 int i;
e27daab4 1719 bool ret;
85234cdc 1720
79f255a0
ACO
1721 if (!intel_display_power_get_if_enabled(dev_priv,
1722 encoder->power_domain))
6d129bea
ID
1723 return false;
1724
e27daab4
ID
1725 ret = false;
1726
fe43d3f5 1727 tmp = I915_READ(DDI_BUF_CTL(port));
85234cdc
DV
1728
1729 if (!(tmp & DDI_BUF_CTL_ENABLE))
e27daab4 1730 goto out;
85234cdc 1731
ad80a810
PZ
1732 if (port == PORT_A) {
1733 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
85234cdc 1734
ad80a810
PZ
1735 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1736 case TRANS_DDI_EDP_INPUT_A_ON:
1737 case TRANS_DDI_EDP_INPUT_A_ONOFF:
1738 *pipe = PIPE_A;
1739 break;
1740 case TRANS_DDI_EDP_INPUT_B_ONOFF:
1741 *pipe = PIPE_B;
1742 break;
1743 case TRANS_DDI_EDP_INPUT_C_ONOFF:
1744 *pipe = PIPE_C;
1745 break;
1746 }
1747
e27daab4 1748 ret = true;
ad80a810 1749
e27daab4
ID
1750 goto out;
1751 }
0e32b39c 1752
e27daab4
ID
1753 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
1754 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
1755
1756 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(port)) {
1757 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
1758 TRANS_DDI_MODE_SELECT_DP_MST)
1759 goto out;
1760
1761 *pipe = i;
1762 ret = true;
1763
1764 goto out;
85234cdc
DV
1765 }
1766 }
1767
84f44ce7 1768 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
85234cdc 1769
e27daab4 1770out:
cc3f90f0 1771 if (ret && IS_GEN9_LP(dev_priv)) {
e93da0a0 1772 tmp = I915_READ(BXT_PHY_CTL(port));
e19c1eb8
ID
1773 if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
1774 BXT_PHY_LANE_POWERDOWN_ACK |
e93da0a0
ID
1775 BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
1776 DRM_ERROR("Port %c enabled but PHY powered down? "
1777 "(PHY_CTL %08x)\n", port_name(port), tmp);
1778 }
1779
79f255a0 1780 intel_display_power_put(dev_priv, encoder->power_domain);
e27daab4
ID
1781
1782 return ret;
85234cdc
DV
1783}
1784
62b69566
ACO
1785static u64 intel_ddi_get_power_domains(struct intel_encoder *encoder)
1786{
1787 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
1788 enum pipe pipe;
1789
1790 if (intel_ddi_get_hw_state(encoder, &pipe))
1791 return BIT_ULL(dig_port->ddi_io_power_domain);
1792
1793 return 0;
1794}
1795
3dc38eea 1796void intel_ddi_enable_pipe_clock(const struct intel_crtc_state *crtc_state)
fc914639 1797{
3dc38eea 1798 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
e9ce1a62 1799 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1524e93e
SS
1800 struct intel_encoder *encoder = intel_ddi_get_crtc_encoder(crtc);
1801 enum port port = intel_ddi_get_encoder_port(encoder);
3dc38eea 1802 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
fc914639 1803
bb523fc0
PZ
1804 if (cpu_transcoder != TRANSCODER_EDP)
1805 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1806 TRANS_CLK_SEL_PORT(port));
fc914639
PZ
1807}
1808
3dc38eea 1809void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
fc914639 1810{
3dc38eea
ACO
1811 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1812 enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
fc914639 1813
bb523fc0
PZ
1814 if (cpu_transcoder != TRANSCODER_EDP)
1815 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1816 TRANS_CLK_SEL_DISABLED);
fc914639
PZ
1817}
1818
a7d8dbc0
VS
1819static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
1820 enum port port, uint8_t iboost)
f8896f5d 1821{
a7d8dbc0
VS
1822 u32 tmp;
1823
1824 tmp = I915_READ(DISPIO_CR_TX_BMU_CR0);
1825 tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
1826 if (iboost)
1827 tmp |= iboost << BALANCE_LEG_SHIFT(port);
1828 else
1829 tmp |= BALANCE_LEG_DISABLE(port);
1830 I915_WRITE(DISPIO_CR_TX_BMU_CR0, tmp);
1831}
1832
081dfcfa
VS
1833static void skl_ddi_set_iboost(struct intel_encoder *encoder,
1834 int level, enum intel_output_type type)
a7d8dbc0
VS
1835{
1836 struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
1837 struct drm_i915_private *dev_priv = to_i915(intel_dig_port->base.base.dev);
1838 enum port port = intel_dig_port->port;
f8896f5d 1839 uint8_t iboost;
f8896f5d 1840
081dfcfa
VS
1841 if (type == INTEL_OUTPUT_HDMI)
1842 iboost = dev_priv->vbt.ddi_port_info[port].hdmi_boost_level;
1843 else
1844 iboost = dev_priv->vbt.ddi_port_info[port].dp_boost_level;
75067dde 1845
081dfcfa
VS
1846 if (iboost == 0) {
1847 const struct ddi_buf_trans *ddi_translations;
1848 int n_entries;
1849
1850 if (type == INTEL_OUTPUT_HDMI)
1851 ddi_translations = intel_ddi_get_buf_trans_hdmi(dev_priv, &n_entries);
1852 else if (type == INTEL_OUTPUT_EDP)
edba48fd 1853 ddi_translations = intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
081dfcfa 1854 else
edba48fd 1855 ddi_translations = intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
10afa0b6 1856
21b39d2a
VS
1857 if (WARN_ON_ONCE(!ddi_translations))
1858 return;
1859 if (WARN_ON_ONCE(level >= n_entries))
1860 level = n_entries - 1;
1861
081dfcfa 1862 iboost = ddi_translations[level].i_boost;
f8896f5d
DW
1863 }
1864
1865 /* Make sure that the requested I_boost is valid */
1866 if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
1867 DRM_ERROR("Invalid I_boost value %u\n", iboost);
1868 return;
1869 }
1870
a7d8dbc0 1871 _skl_ddi_set_iboost(dev_priv, port, iboost);
f8896f5d 1872
a7d8dbc0
VS
1873 if (port == PORT_A && intel_dig_port->max_lanes == 4)
1874 _skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
f8896f5d
DW
1875}
1876
7d4f37b5
VS
1877static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
1878 int level, enum intel_output_type type)
96fb9f9b 1879{
7d4f37b5 1880 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
96fb9f9b 1881 const struct bxt_ddi_buf_trans *ddi_translations;
7d4f37b5 1882 enum port port = encoder->port;
043eaf36 1883 int n_entries;
7d4f37b5
VS
1884
1885 if (type == INTEL_OUTPUT_HDMI)
1886 ddi_translations = bxt_get_buf_trans_hdmi(dev_priv, &n_entries);
1887 else if (type == INTEL_OUTPUT_EDP)
1888 ddi_translations = bxt_get_buf_trans_edp(dev_priv, &n_entries);
1889 else
1890 ddi_translations = bxt_get_buf_trans_dp(dev_priv, &n_entries);
96fb9f9b 1891
21b39d2a
VS
1892 if (WARN_ON_ONCE(!ddi_translations))
1893 return;
1894 if (WARN_ON_ONCE(level >= n_entries))
1895 level = n_entries - 1;
1896
b6e08203
ACO
1897 bxt_ddi_phy_set_signal_level(dev_priv, port,
1898 ddi_translations[level].margin,
1899 ddi_translations[level].scale,
1900 ddi_translations[level].enable,
1901 ddi_translations[level].deemphasis);
96fb9f9b
VK
1902}
1903
ffe5111e
VS
1904u8 intel_ddi_dp_voltage_max(struct intel_encoder *encoder)
1905{
1906 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
edba48fd 1907 enum port port = encoder->port;
ffe5111e
VS
1908 int n_entries;
1909
5fcf34b1
RV
1910 if (IS_CANNONLAKE(dev_priv)) {
1911 if (encoder->type == INTEL_OUTPUT_EDP)
1912 cnl_get_buf_trans_edp(dev_priv, &n_entries);
1913 else
1914 cnl_get_buf_trans_dp(dev_priv, &n_entries);
7d4f37b5
VS
1915 } else if (IS_GEN9_LP(dev_priv)) {
1916 if (encoder->type == INTEL_OUTPUT_EDP)
1917 bxt_get_buf_trans_edp(dev_priv, &n_entries);
1918 else
1919 bxt_get_buf_trans_dp(dev_priv, &n_entries);
5fcf34b1
RV
1920 } else {
1921 if (encoder->type == INTEL_OUTPUT_EDP)
edba48fd 1922 intel_ddi_get_buf_trans_edp(dev_priv, port, &n_entries);
5fcf34b1 1923 else
edba48fd 1924 intel_ddi_get_buf_trans_dp(dev_priv, port, &n_entries);
5fcf34b1 1925 }
ffe5111e
VS
1926
1927 if (WARN_ON(n_entries < 1))
1928 n_entries = 1;
1929 if (WARN_ON(n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1930 n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1931
1932 return index_to_dp_signal_levels[n_entries - 1] &
1933 DP_TRAIN_VOLTAGE_SWING_MASK;
1934}
1935
f3cf4ba4
VS
1936static void cnl_ddi_vswing_program(struct intel_encoder *encoder,
1937 int level, enum intel_output_type type)
cf54ca8b 1938{
f3cf4ba4
VS
1939 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1940 enum port port = intel_ddi_get_encoder_port(encoder);
1941 const struct cnl_ddi_buf_trans *ddi_translations;
1942 int n_entries, ln;
1943 u32 val;
cf54ca8b 1944
f3cf4ba4 1945 if (type == INTEL_OUTPUT_HDMI)
cc9cabfd 1946 ddi_translations = cnl_get_buf_trans_hdmi(dev_priv, &n_entries);
f3cf4ba4 1947 else if (type == INTEL_OUTPUT_EDP)
cc9cabfd 1948 ddi_translations = cnl_get_buf_trans_edp(dev_priv, &n_entries);
f3cf4ba4
VS
1949 else
1950 ddi_translations = cnl_get_buf_trans_dp(dev_priv, &n_entries);
cf54ca8b 1951
21b39d2a 1952 if (WARN_ON_ONCE(!ddi_translations))
cf54ca8b 1953 return;
21b39d2a 1954 if (WARN_ON_ONCE(level >= n_entries))
cf54ca8b 1955 level = n_entries - 1;
cf54ca8b
RV
1956
1957 /* Set PORT_TX_DW5 Scaling Mode Sel to 010b. */
1958 val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
1f588aeb 1959 val &= ~SCALING_MODE_SEL_MASK;
cf54ca8b
RV
1960 val |= SCALING_MODE_SEL(2);
1961 I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
1962
1963 /* Program PORT_TX_DW2 */
1964 val = I915_READ(CNL_PORT_TX_DW2_LN0(port));
1f588aeb
RV
1965 val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
1966 RCOMP_SCALAR_MASK);
cf54ca8b
RV
1967 val |= SWING_SEL_UPPER(ddi_translations[level].dw2_swing_sel);
1968 val |= SWING_SEL_LOWER(ddi_translations[level].dw2_swing_sel);
1969 /* Rcomp scalar is fixed as 0x98 for every table entry */
1970 val |= RCOMP_SCALAR(0x98);
1971 I915_WRITE(CNL_PORT_TX_DW2_GRP(port), val);
1972
20303eb4 1973 /* Program PORT_TX_DW4 */
cf54ca8b
RV
1974 /* We cannot write to GRP. It would overrite individual loadgen */
1975 for (ln = 0; ln < 4; ln++) {
1976 val = I915_READ(CNL_PORT_TX_DW4_LN(port, ln));
1f588aeb
RV
1977 val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
1978 CURSOR_COEFF_MASK);
cf54ca8b
RV
1979 val |= POST_CURSOR_1(ddi_translations[level].dw4_post_cursor_1);
1980 val |= POST_CURSOR_2(ddi_translations[level].dw4_post_cursor_2);
1981 val |= CURSOR_COEFF(ddi_translations[level].dw4_cursor_coeff);
1982 I915_WRITE(CNL_PORT_TX_DW4_LN(port, ln), val);
1983 }
1984
20303eb4 1985 /* Program PORT_TX_DW5 */
cf54ca8b
RV
1986 /* All DW5 values are fixed for every table entry */
1987 val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
1f588aeb 1988 val &= ~RTERM_SELECT_MASK;
cf54ca8b
RV
1989 val |= RTERM_SELECT(6);
1990 val |= TAP3_DISABLE;
1991 I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
1992
20303eb4 1993 /* Program PORT_TX_DW7 */
cf54ca8b 1994 val = I915_READ(CNL_PORT_TX_DW7_LN0(port));
1f588aeb 1995 val &= ~N_SCALAR_MASK;
cf54ca8b
RV
1996 val |= N_SCALAR(ddi_translations[level].dw7_n_scalar);
1997 I915_WRITE(CNL_PORT_TX_DW7_GRP(port), val);
1998}
1999
f3cf4ba4
VS
2000static void cnl_ddi_vswing_sequence(struct intel_encoder *encoder,
2001 int level, enum intel_output_type type)
cf54ca8b 2002{
0091abc3 2003 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
0091abc3 2004 enum port port = intel_ddi_get_encoder_port(encoder);
f3cf4ba4 2005 int width, rate, ln;
cf54ca8b 2006 u32 val;
0091abc3 2007
f3cf4ba4 2008 if (type == INTEL_OUTPUT_HDMI) {
0091abc3 2009 width = 4;
f3cf4ba4 2010 rate = 0; /* Rate is always < than 6GHz for HDMI */
61f3e770 2011 } else {
f3cf4ba4
VS
2012 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2013
2014 width = intel_dp->lane_count;
2015 rate = intel_dp->link_rate;
0091abc3 2016 }
cf54ca8b
RV
2017
2018 /*
2019 * 1. If port type is eDP or DP,
2020 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
2021 * else clear to 0b.
2022 */
2023 val = I915_READ(CNL_PORT_PCS_DW1_LN0(port));
f3cf4ba4 2024 if (type != INTEL_OUTPUT_HDMI)
cf54ca8b
RV
2025 val |= COMMON_KEEPER_EN;
2026 else
2027 val &= ~COMMON_KEEPER_EN;
2028 I915_WRITE(CNL_PORT_PCS_DW1_GRP(port), val);
2029
2030 /* 2. Program loadgen select */
2031 /*
0091abc3
CT
2032 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
2033 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
2034 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
2035 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
cf54ca8b 2036 */
0091abc3
CT
2037 for (ln = 0; ln <= 3; ln++) {
2038 val = I915_READ(CNL_PORT_TX_DW4_LN(port, ln));
2039 val &= ~LOADGEN_SELECT;
2040
a8e45a1c
NM
2041 if ((rate <= 600000 && width == 4 && ln >= 1) ||
2042 (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
0091abc3
CT
2043 val |= LOADGEN_SELECT;
2044 }
2045 I915_WRITE(CNL_PORT_TX_DW4_LN(port, ln), val);
2046 }
cf54ca8b
RV
2047
2048 /* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
2049 val = I915_READ(CNL_PORT_CL1CM_DW5);
2050 val |= SUS_CLOCK_CONFIG;
2051 I915_WRITE(CNL_PORT_CL1CM_DW5, val);
2052
2053 /* 4. Clear training enable to change swing values */
2054 val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2055 val &= ~TX_TRAINING_EN;
2056 I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2057
2058 /* 5. Program swing and de-emphasis */
f3cf4ba4 2059 cnl_ddi_vswing_program(encoder, level, type);
cf54ca8b
RV
2060
2061 /* 6. Set training enable to trigger update */
2062 val = I915_READ(CNL_PORT_TX_DW5_LN0(port));
2063 val |= TX_TRAINING_EN;
2064 I915_WRITE(CNL_PORT_TX_DW5_GRP(port), val);
2065}
2066
f8896f5d
DW
2067static uint32_t translate_signal_level(int signal_levels)
2068{
97eeb872 2069 int i;
f8896f5d 2070
97eeb872
VS
2071 for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
2072 if (index_to_dp_signal_levels[i] == signal_levels)
2073 return i;
f8896f5d
DW
2074 }
2075
97eeb872
VS
2076 WARN(1, "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
2077 signal_levels);
2078
2079 return 0;
f8896f5d
DW
2080}
2081
1b6e2fd2
RV
2082static uint32_t intel_ddi_dp_level(struct intel_dp *intel_dp)
2083{
2084 uint8_t train_set = intel_dp->train_set[0];
2085 int signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
2086 DP_TRAIN_PRE_EMPHASIS_MASK);
2087
2088 return translate_signal_level(signal_levels);
2089}
2090
d509af6c 2091u32 bxt_signal_levels(struct intel_dp *intel_dp)
f8896f5d
DW
2092{
2093 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
78ab0bae 2094 struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
f8896f5d 2095 struct intel_encoder *encoder = &dport->base;
d02ace87 2096 int level = intel_ddi_dp_level(intel_dp);
d509af6c
RV
2097
2098 if (IS_CANNONLAKE(dev_priv))
f3cf4ba4 2099 cnl_ddi_vswing_sequence(encoder, level, encoder->type);
d509af6c 2100 else
7d4f37b5 2101 bxt_ddi_vswing_sequence(encoder, level, encoder->type);
d509af6c
RV
2102
2103 return 0;
2104}
2105
2106uint32_t ddi_signal_levels(struct intel_dp *intel_dp)
2107{
2108 struct intel_digital_port *dport = dp_to_dig_port(intel_dp);
2109 struct drm_i915_private *dev_priv = to_i915(dport->base.base.dev);
2110 struct intel_encoder *encoder = &dport->base;
d02ace87 2111 int level = intel_ddi_dp_level(intel_dp);
f8896f5d 2112
b976dc53 2113 if (IS_GEN9_BC(dev_priv))
081dfcfa 2114 skl_ddi_set_iboost(encoder, level, encoder->type);
d509af6c 2115
f8896f5d
DW
2116 return DDI_BUF_TRANS_SELECT(level);
2117}
2118
d7c530b2 2119static void intel_ddi_clk_select(struct intel_encoder *encoder,
5f88a9c6 2120 const struct intel_shared_dpll *pll)
6441ab5f 2121{
e404ba8d
VS
2122 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2123 enum port port = intel_ddi_get_encoder_port(encoder);
555e38d2 2124 uint32_t val;
6441ab5f 2125
c856052a
ACO
2126 if (WARN_ON(!pll))
2127 return;
2128
555e38d2
RV
2129 if (IS_CANNONLAKE(dev_priv)) {
2130 /* Configure DPCLKA_CFGCR0 to map the DPLL to the DDI. */
2131 val = I915_READ(DPCLKA_CFGCR0);
2132 val |= DPCLKA_CFGCR0_DDI_CLK_SEL(pll->id, port);
2133 I915_WRITE(DPCLKA_CFGCR0, val);
efa80add 2134
555e38d2
RV
2135 /*
2136 * Configure DPCLKA_CFGCR0 to turn on the clock for the DDI.
2137 * This step and the step before must be done with separate
2138 * register writes.
2139 */
2140 val = I915_READ(DPCLKA_CFGCR0);
87145d95 2141 val &= ~DPCLKA_CFGCR0_DDI_CLK_OFF(port);
555e38d2
RV
2142 I915_WRITE(DPCLKA_CFGCR0, val);
2143 } else if (IS_GEN9_BC(dev_priv)) {
5416d871 2144 /* DDI -> PLL mapping */
efa80add
S
2145 val = I915_READ(DPLL_CTRL2);
2146
2147 val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
2148 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
c856052a 2149 val |= (DPLL_CTRL2_DDI_CLK_SEL(pll->id, port) |
efa80add
S
2150 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
2151
2152 I915_WRITE(DPLL_CTRL2, val);
5416d871 2153
e404ba8d 2154 } else if (INTEL_INFO(dev_priv)->gen < 9) {
c856052a 2155 I915_WRITE(PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
efa80add 2156 }
e404ba8d
VS
2157}
2158
6b8506d5
VS
2159static void intel_ddi_clk_disable(struct intel_encoder *encoder)
2160{
2161 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2162 enum port port = intel_ddi_get_encoder_port(encoder);
2163
2164 if (IS_CANNONLAKE(dev_priv))
2165 I915_WRITE(DPCLKA_CFGCR0, I915_READ(DPCLKA_CFGCR0) |
2166 DPCLKA_CFGCR0_DDI_CLK_OFF(port));
2167 else if (IS_GEN9_BC(dev_priv))
2168 I915_WRITE(DPLL_CTRL2, I915_READ(DPLL_CTRL2) |
2169 DPLL_CTRL2_DDI_CLK_OFF(port));
2170 else if (INTEL_GEN(dev_priv) < 9)
2171 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
2172}
2173
ba88d153 2174static void intel_ddi_pre_enable_dp(struct intel_encoder *encoder,
45e0327e
VS
2175 const struct intel_crtc_state *crtc_state,
2176 const struct drm_connector_state *conn_state)
e404ba8d 2177{
ba88d153
MN
2178 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2179 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2180 enum port port = intel_ddi_get_encoder_port(encoder);
62b69566 2181 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
45e0327e 2182 bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
d02ace87 2183 int level = intel_ddi_dp_level(intel_dp);
b2ccb822 2184
45e0327e 2185 WARN_ON(is_mst && (port == PORT_A || port == PORT_E));
e081c846 2186
45e0327e
VS
2187 intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
2188 crtc_state->lane_count, is_mst);
680b71c2
VS
2189
2190 intel_edp_panel_on(intel_dp);
32bdc400 2191
45e0327e 2192 intel_ddi_clk_select(encoder, crtc_state->shared_dpll);
62b69566
ACO
2193
2194 intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
2195
381f9570 2196 if (IS_CANNONLAKE(dev_priv))
f3cf4ba4 2197 cnl_ddi_vswing_sequence(encoder, level, encoder->type);
381f9570 2198 else if (IS_GEN9_LP(dev_priv))
7d4f37b5 2199 bxt_ddi_vswing_sequence(encoder, level, encoder->type);
381f9570 2200 else
2f7460a7
RV
2201 intel_prepare_dp_ddi_buffers(encoder);
2202
ba88d153 2203 intel_ddi_init_dp_buf_reg(encoder);
45e0327e 2204 if (!is_mst)
5ea2355a 2205 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
ba88d153
MN
2206 intel_dp_start_link_train(intel_dp);
2207 if (port != PORT_A || INTEL_GEN(dev_priv) >= 9)
2208 intel_dp_stop_link_train(intel_dp);
2209}
901c2daf 2210
ba88d153 2211static void intel_ddi_pre_enable_hdmi(struct intel_encoder *encoder,
ac240288 2212 const struct intel_crtc_state *crtc_state,
45e0327e 2213 const struct drm_connector_state *conn_state)
ba88d153 2214{
f99be1b3
VS
2215 struct intel_digital_port *intel_dig_port = enc_to_dig_port(&encoder->base);
2216 struct intel_hdmi *intel_hdmi = &intel_dig_port->hdmi;
ba88d153 2217 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
ba88d153
MN
2218 enum port port = intel_ddi_get_encoder_port(encoder);
2219 int level = intel_ddi_hdmi_level(dev_priv, port);
62b69566 2220 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
c19b0669 2221
ba88d153 2222 intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
45e0327e 2223 intel_ddi_clk_select(encoder, crtc_state->shared_dpll);
62b69566
ACO
2224
2225 intel_display_power_get(dev_priv, dig_port->ddi_io_power_domain);
2226
2f7460a7 2227 if (IS_CANNONLAKE(dev_priv))
f3cf4ba4 2228 cnl_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
cc3f90f0 2229 else if (IS_GEN9_LP(dev_priv))
7d4f37b5 2230 bxt_ddi_vswing_sequence(encoder, level, INTEL_OUTPUT_HDMI);
2f7460a7 2231 else
7ea79333 2232 intel_prepare_hdmi_ddi_buffers(encoder, level);
2f7460a7
RV
2233
2234 if (IS_GEN9_BC(dev_priv))
081dfcfa 2235 skl_ddi_set_iboost(encoder, level, INTEL_OUTPUT_HDMI);
8d8bb85e 2236
f99be1b3 2237 intel_dig_port->set_infoframes(&encoder->base,
45e0327e 2238 crtc_state->has_infoframe,
f99be1b3 2239 crtc_state, conn_state);
ba88d153 2240}
32bdc400 2241
1524e93e 2242static void intel_ddi_pre_enable(struct intel_encoder *encoder,
45e0327e 2243 const struct intel_crtc_state *crtc_state,
5f88a9c6 2244 const struct drm_connector_state *conn_state)
ba88d153 2245{
45e0327e
VS
2246 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
2247 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2248 enum pipe pipe = crtc->pipe;
30cf6db8 2249
45e0327e 2250 WARN_ON(crtc_state->has_pch_encoder);
364a3fe1
JN
2251
2252 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2253
45e0327e
VS
2254 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
2255 intel_ddi_pre_enable_hdmi(encoder, crtc_state, conn_state);
2256 else
2257 intel_ddi_pre_enable_dp(encoder, crtc_state, conn_state);
6441ab5f
PZ
2258}
2259
e725f645
VS
2260static void intel_disable_ddi_buf(struct intel_encoder *encoder)
2261{
2262 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2263 enum port port = intel_ddi_get_encoder_port(encoder);
2264 bool wait = false;
2265 u32 val;
2266
2267 val = I915_READ(DDI_BUF_CTL(port));
2268 if (val & DDI_BUF_CTL_ENABLE) {
2269 val &= ~DDI_BUF_CTL_ENABLE;
2270 I915_WRITE(DDI_BUF_CTL(port), val);
2271 wait = true;
2272 }
2273
2274 val = I915_READ(DP_TP_CTL(port));
2275 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2276 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2277 I915_WRITE(DP_TP_CTL(port), val);
2278
2279 if (wait)
2280 intel_wait_ddi_buf_idle(dev_priv, port);
2281}
2282
f45f3da7
VS
2283static void intel_ddi_post_disable_dp(struct intel_encoder *encoder,
2284 const struct intel_crtc_state *old_crtc_state,
2285 const struct drm_connector_state *old_conn_state)
6441ab5f 2286{
f45f3da7
VS
2287 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2288 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
2289 struct intel_dp *intel_dp = &dig_port->dp;
2290 /*
2291 * old_crtc_state and old_conn_state are NULL when called from
2292 * DP_MST. The main connector associated with this port is never
2293 * bound to a crtc for MST.
2294 */
2295 bool is_mst = !old_crtc_state;
2886e93f 2296
f45f3da7
VS
2297 /*
2298 * Power down sink before disabling the port, otherwise we end
2299 * up getting interrupts from the sink on detecting link loss.
2300 */
2301 if (!is_mst)
2302 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
c5f93fcf 2303
f45f3da7 2304 intel_disable_ddi_buf(encoder);
7618138d 2305
f45f3da7
VS
2306 intel_edp_panel_vdd_on(intel_dp);
2307 intel_edp_panel_off(intel_dp);
a836bdf9 2308
f45f3da7 2309 intel_display_power_put(dev_priv, dig_port->ddi_io_power_domain);
c5f93fcf 2310
f45f3da7
VS
2311 intel_ddi_clk_disable(encoder);
2312}
c5f93fcf 2313
f45f3da7
VS
2314static void intel_ddi_post_disable_hdmi(struct intel_encoder *encoder,
2315 const struct intel_crtc_state *old_crtc_state,
2316 const struct drm_connector_state *old_conn_state)
2317{
2318 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2319 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
2320 struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
82a4d9c0 2321
f45f3da7 2322 intel_disable_ddi_buf(encoder);
62b69566 2323
f45f3da7
VS
2324 dig_port->set_infoframes(&encoder->base, false,
2325 old_crtc_state, old_conn_state);
b2ccb822 2326
f45f3da7 2327 intel_display_power_put(dev_priv, dig_port->ddi_io_power_domain);
b2ccb822 2328
f45f3da7
VS
2329 intel_ddi_clk_disable(encoder);
2330
2331 intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2332}
2333
2334static void intel_ddi_post_disable(struct intel_encoder *encoder,
2335 const struct intel_crtc_state *old_crtc_state,
2336 const struct drm_connector_state *old_conn_state)
2337{
2338 /*
2339 * old_crtc_state and old_conn_state are NULL when called from
2340 * DP_MST. The main connector associated with this port is never
2341 * bound to a crtc for MST.
2342 */
2343 if (old_crtc_state &&
2344 intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2345 intel_ddi_post_disable_hdmi(encoder,
2346 old_crtc_state, old_conn_state);
2347 else
2348 intel_ddi_post_disable_dp(encoder,
2349 old_crtc_state, old_conn_state);
6441ab5f
PZ
2350}
2351
1524e93e 2352void intel_ddi_fdi_post_disable(struct intel_encoder *encoder,
5f88a9c6
VS
2353 const struct intel_crtc_state *old_crtc_state,
2354 const struct drm_connector_state *old_conn_state)
b7076546 2355{
1524e93e 2356 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
b7076546
ML
2357 uint32_t val;
2358
2359 /*
2360 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
2361 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
2362 * step 13 is the correct place for it. Step 18 is where it was
2363 * originally before the BUN.
2364 */
2365 val = I915_READ(FDI_RX_CTL(PIPE_A));
2366 val &= ~FDI_RX_ENABLE;
2367 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
2368
fb0bd3bd
VS
2369 intel_disable_ddi_buf(encoder);
2370 intel_ddi_clk_disable(encoder);
b7076546
ML
2371
2372 val = I915_READ(FDI_RX_MISC(PIPE_A));
2373 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
2374 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
2375 I915_WRITE(FDI_RX_MISC(PIPE_A), val);
2376
2377 val = I915_READ(FDI_RX_CTL(PIPE_A));
2378 val &= ~FDI_PCDCLK;
2379 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
2380
2381 val = I915_READ(FDI_RX_CTL(PIPE_A));
2382 val &= ~FDI_RX_PLL_ENABLE;
2383 I915_WRITE(FDI_RX_CTL(PIPE_A), val);
2384}
2385
15d05f0e
VS
2386static void intel_enable_ddi_dp(struct intel_encoder *encoder,
2387 const struct intel_crtc_state *crtc_state,
2388 const struct drm_connector_state *conn_state)
72662e10 2389{
15d05f0e
VS
2390 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2391 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
2392 enum port port = intel_ddi_get_encoder_port(encoder);
72662e10 2393
15d05f0e
VS
2394 if (port == PORT_A && INTEL_GEN(dev_priv) < 9)
2395 intel_dp_stop_link_train(intel_dp);
d6c50ff8 2396
15d05f0e
VS
2397 intel_edp_backlight_on(crtc_state, conn_state);
2398 intel_psr_enable(intel_dp, crtc_state);
2399 intel_edp_drrs_enable(intel_dp, crtc_state);
3ab9c637 2400
15d05f0e
VS
2401 if (crtc_state->has_audio)
2402 intel_audio_codec_enable(encoder, crtc_state, conn_state);
2403}
2404
2405static void intel_enable_ddi_hdmi(struct intel_encoder *encoder,
2406 const struct intel_crtc_state *crtc_state,
2407 const struct drm_connector_state *conn_state)
2408{
2409 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2410 struct intel_digital_port *dig_port = enc_to_dig_port(&encoder->base);
2411 enum port port = intel_ddi_get_encoder_port(encoder);
2412
2413 intel_hdmi_handle_sink_scrambling(encoder,
2414 conn_state->connector,
2415 crtc_state->hdmi_high_tmds_clock_ratio,
2416 crtc_state->hdmi_scrambling);
2417
2418 /* In HDMI/DVI mode, the port width, and swing/emphasis values
2419 * are ignored so nothing special needs to be done besides
2420 * enabling the port.
2421 */
2422 I915_WRITE(DDI_BUF_CTL(port),
2423 dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
7b9f35a6 2424
15d05f0e
VS
2425 if (crtc_state->has_audio)
2426 intel_audio_codec_enable(encoder, crtc_state, conn_state);
2427}
2428
2429static void intel_enable_ddi(struct intel_encoder *encoder,
2430 const struct intel_crtc_state *crtc_state,
2431 const struct drm_connector_state *conn_state)
2432{
2433 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
2434 intel_enable_ddi_hdmi(encoder, crtc_state, conn_state);
2435 else
2436 intel_enable_ddi_dp(encoder, crtc_state, conn_state);
5ab432ef
DV
2437}
2438
33f083f0
VS
2439static void intel_disable_ddi_dp(struct intel_encoder *encoder,
2440 const struct intel_crtc_state *old_crtc_state,
2441 const struct drm_connector_state *old_conn_state)
5ab432ef 2442{
33f083f0 2443 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
d6c50ff8 2444
37255d8d 2445 if (old_crtc_state->has_audio)
33f083f0 2446 intel_audio_codec_disable(encoder);
2831d842 2447
33f083f0
VS
2448 intel_edp_drrs_disable(intel_dp, old_crtc_state);
2449 intel_psr_disable(intel_dp, old_crtc_state);
2450 intel_edp_backlight_off(old_conn_state);
2451}
15953637 2452
33f083f0
VS
2453static void intel_disable_ddi_hdmi(struct intel_encoder *encoder,
2454 const struct intel_crtc_state *old_crtc_state,
2455 const struct drm_connector_state *old_conn_state)
2456{
2457 if (old_crtc_state->has_audio)
2458 intel_audio_codec_disable(encoder);
d6c50ff8 2459
33f083f0
VS
2460 intel_hdmi_handle_sink_scrambling(encoder,
2461 old_conn_state->connector,
2462 false, false);
2463}
2464
2465static void intel_disable_ddi(struct intel_encoder *encoder,
2466 const struct intel_crtc_state *old_crtc_state,
2467 const struct drm_connector_state *old_conn_state)
2468{
2469 if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2470 intel_disable_ddi_hdmi(encoder, old_crtc_state, old_conn_state);
2471 else
2472 intel_disable_ddi_dp(encoder, old_crtc_state, old_conn_state);
72662e10 2473}
79f689aa 2474
fd6bbda9 2475static void bxt_ddi_pre_pll_enable(struct intel_encoder *encoder,
5f88a9c6
VS
2476 const struct intel_crtc_state *pipe_config,
2477 const struct drm_connector_state *conn_state)
95a7a2ae 2478{
3dc38eea 2479 uint8_t mask = pipe_config->lane_lat_optim_mask;
95a7a2ae 2480
47a6bc61 2481 bxt_ddi_phy_set_lane_optim_mask(encoder, mask);
95a7a2ae
ID
2482}
2483
ad64217b 2484void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp)
c19b0669 2485{
ad64217b
ACO
2486 struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
2487 struct drm_i915_private *dev_priv =
2488 to_i915(intel_dig_port->base.base.dev);
174edf1f 2489 enum port port = intel_dig_port->port;
c19b0669 2490 uint32_t val;
f3e227df 2491 bool wait = false;
c19b0669
PZ
2492
2493 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
2494 val = I915_READ(DDI_BUF_CTL(port));
2495 if (val & DDI_BUF_CTL_ENABLE) {
2496 val &= ~DDI_BUF_CTL_ENABLE;
2497 I915_WRITE(DDI_BUF_CTL(port), val);
2498 wait = true;
2499 }
2500
2501 val = I915_READ(DP_TP_CTL(port));
2502 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2503 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2504 I915_WRITE(DP_TP_CTL(port), val);
2505 POSTING_READ(DP_TP_CTL(port));
2506
2507 if (wait)
2508 intel_wait_ddi_buf_idle(dev_priv, port);
2509 }
2510
0e32b39c 2511 val = DP_TP_CTL_ENABLE |
c19b0669 2512 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
64ee2fd2 2513 if (intel_dp->link_mst)
0e32b39c
DA
2514 val |= DP_TP_CTL_MODE_MST;
2515 else {
2516 val |= DP_TP_CTL_MODE_SST;
2517 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2518 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
2519 }
c19b0669
PZ
2520 I915_WRITE(DP_TP_CTL(port), val);
2521 POSTING_READ(DP_TP_CTL(port));
2522
2523 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
2524 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
2525 POSTING_READ(DDI_BUF_CTL(port));
2526
2527 udelay(600);
2528}
00c09d70 2529
9935f7fa
LY
2530bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
2531 struct intel_crtc *intel_crtc)
2532{
2533 u32 temp;
2534
2535 if (intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO)) {
2536 temp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
2537 if (temp & AUDIO_OUTPUT_ENABLE(intel_crtc->pipe))
2538 return true;
2539 }
2540 return false;
2541}
2542
6801c18c 2543void intel_ddi_get_config(struct intel_encoder *encoder,
5cec258b 2544 struct intel_crtc_state *pipe_config)
045ac3b5 2545{
fac5e23e 2546 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
045ac3b5 2547 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
0cb09a97 2548 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
f99be1b3 2549 struct intel_digital_port *intel_dig_port;
045ac3b5
JB
2550 u32 temp, flags = 0;
2551
4d1de975
JN
2552 /* XXX: DSI transcoder paranoia */
2553 if (WARN_ON(transcoder_is_dsi(cpu_transcoder)))
2554 return;
2555
045ac3b5
JB
2556 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
2557 if (temp & TRANS_DDI_PHSYNC)
2558 flags |= DRM_MODE_FLAG_PHSYNC;
2559 else
2560 flags |= DRM_MODE_FLAG_NHSYNC;
2561 if (temp & TRANS_DDI_PVSYNC)
2562 flags |= DRM_MODE_FLAG_PVSYNC;
2563 else
2564 flags |= DRM_MODE_FLAG_NVSYNC;
2565
2d112de7 2566 pipe_config->base.adjusted_mode.flags |= flags;
42571aef
VS
2567
2568 switch (temp & TRANS_DDI_BPC_MASK) {
2569 case TRANS_DDI_BPC_6:
2570 pipe_config->pipe_bpp = 18;
2571 break;
2572 case TRANS_DDI_BPC_8:
2573 pipe_config->pipe_bpp = 24;
2574 break;
2575 case TRANS_DDI_BPC_10:
2576 pipe_config->pipe_bpp = 30;
2577 break;
2578 case TRANS_DDI_BPC_12:
2579 pipe_config->pipe_bpp = 36;
2580 break;
2581 default:
2582 break;
2583 }
eb14cb74
VS
2584
2585 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
2586 case TRANS_DDI_MODE_SELECT_HDMI:
6897b4b5 2587 pipe_config->has_hdmi_sink = true;
f99be1b3 2588 intel_dig_port = enc_to_dig_port(&encoder->base);
bbd440fb 2589
f99be1b3 2590 if (intel_dig_port->infoframe_enabled(&encoder->base, pipe_config))
bbd440fb 2591 pipe_config->has_infoframe = true;
15953637
SS
2592
2593 if ((temp & TRANS_DDI_HDMI_SCRAMBLING_MASK) ==
2594 TRANS_DDI_HDMI_SCRAMBLING_MASK)
2595 pipe_config->hdmi_scrambling = true;
2596 if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
2597 pipe_config->hdmi_high_tmds_clock_ratio = true;
d4d6279a 2598 /* fall through */
eb14cb74 2599 case TRANS_DDI_MODE_SELECT_DVI:
d4d6279a
ACO
2600 pipe_config->lane_count = 4;
2601 break;
eb14cb74
VS
2602 case TRANS_DDI_MODE_SELECT_FDI:
2603 break;
2604 case TRANS_DDI_MODE_SELECT_DP_SST:
2605 case TRANS_DDI_MODE_SELECT_DP_MST:
90a6b7b0
VS
2606 pipe_config->lane_count =
2607 ((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
eb14cb74
VS
2608 intel_dp_get_m_n(intel_crtc, pipe_config);
2609 break;
2610 default:
2611 break;
2612 }
10214420 2613
9935f7fa
LY
2614 pipe_config->has_audio =
2615 intel_ddi_is_audio_enabled(dev_priv, intel_crtc);
9ed109a7 2616
6aa23e65
JN
2617 if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
2618 pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
10214420
DV
2619 /*
2620 * This is a big fat ugly hack.
2621 *
2622 * Some machines in UEFI boot mode provide us a VBT that has 18
2623 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2624 * unknown we fail to light up. Yet the same BIOS boots up with
2625 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2626 * max, not what it tells us to use.
2627 *
2628 * Note: This will still be broken if the eDP panel is not lit
2629 * up by the BIOS, and thus we can't get the mode at module
2630 * load.
2631 */
2632 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
6aa23e65
JN
2633 pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
2634 dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
10214420 2635 }
11578553 2636
22606a18 2637 intel_ddi_clock_get(encoder, pipe_config);
95a7a2ae 2638
cc3f90f0 2639 if (IS_GEN9_LP(dev_priv))
95a7a2ae
ID
2640 pipe_config->lane_lat_optim_mask =
2641 bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
045ac3b5
JB
2642}
2643
5bfe2ac0 2644static bool intel_ddi_compute_config(struct intel_encoder *encoder,
0a478c27
ML
2645 struct intel_crtc_state *pipe_config,
2646 struct drm_connector_state *conn_state)
00c09d70 2647{
fac5e23e 2648 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5bfe2ac0 2649 int type = encoder->type;
eccb140b 2650 int port = intel_ddi_get_encoder_port(encoder);
95a7a2ae 2651 int ret;
00c09d70 2652
5bfe2ac0 2653 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
00c09d70 2654
eccb140b
DV
2655 if (port == PORT_A)
2656 pipe_config->cpu_transcoder = TRANSCODER_EDP;
2657
00c09d70 2658 if (type == INTEL_OUTPUT_HDMI)
0a478c27 2659 ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
00c09d70 2660 else
0a478c27 2661 ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
95a7a2ae 2662
cc3f90f0 2663 if (IS_GEN9_LP(dev_priv) && ret)
95a7a2ae
ID
2664 pipe_config->lane_lat_optim_mask =
2665 bxt_ddi_phy_calc_lane_lat_optim_mask(encoder,
b284eeda 2666 pipe_config->lane_count);
95a7a2ae
ID
2667
2668 return ret;
2669
00c09d70
PZ
2670}
2671
2672static const struct drm_encoder_funcs intel_ddi_funcs = {
bf93ba67
ID
2673 .reset = intel_dp_encoder_reset,
2674 .destroy = intel_dp_encoder_destroy,
00c09d70
PZ
2675};
2676
4a28ae58
PZ
2677static struct intel_connector *
2678intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
2679{
2680 struct intel_connector *connector;
2681 enum port port = intel_dig_port->port;
2682
9bdbd0b9 2683 connector = intel_connector_alloc();
4a28ae58
PZ
2684 if (!connector)
2685 return NULL;
2686
2687 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
2688 if (!intel_dp_init_connector(intel_dig_port, connector)) {
2689 kfree(connector);
2690 return NULL;
2691 }
2692
2693 return connector;
2694}
2695
2696static struct intel_connector *
2697intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
2698{
2699 struct intel_connector *connector;
2700 enum port port = intel_dig_port->port;
2701
9bdbd0b9 2702 connector = intel_connector_alloc();
4a28ae58
PZ
2703 if (!connector)
2704 return NULL;
2705
2706 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
2707 intel_hdmi_init_connector(intel_dig_port, connector);
2708
2709 return connector;
2710}
2711
c39055b0 2712void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
00c09d70
PZ
2713{
2714 struct intel_digital_port *intel_dig_port;
2715 struct intel_encoder *intel_encoder;
2716 struct drm_encoder *encoder;
ff662124 2717 bool init_hdmi, init_dp, init_lspcon = false;
10e7bec3
VS
2718 int max_lanes;
2719
2720 if (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES) {
2721 switch (port) {
2722 case PORT_A:
2723 max_lanes = 4;
2724 break;
2725 case PORT_E:
2726 max_lanes = 0;
2727 break;
2728 default:
2729 max_lanes = 4;
2730 break;
2731 }
2732 } else {
2733 switch (port) {
2734 case PORT_A:
2735 max_lanes = 2;
2736 break;
2737 case PORT_E:
2738 max_lanes = 2;
2739 break;
2740 default:
2741 max_lanes = 4;
2742 break;
2743 }
2744 }
311a2094
PZ
2745
2746 init_hdmi = (dev_priv->vbt.ddi_port_info[port].supports_dvi ||
2747 dev_priv->vbt.ddi_port_info[port].supports_hdmi);
2748 init_dp = dev_priv->vbt.ddi_port_info[port].supports_dp;
ff662124
SS
2749
2750 if (intel_bios_is_lspcon_present(dev_priv, port)) {
2751 /*
2752 * Lspcon device needs to be driven with DP connector
2753 * with special detection sequence. So make sure DP
2754 * is initialized before lspcon.
2755 */
2756 init_dp = true;
2757 init_lspcon = true;
2758 init_hdmi = false;
2759 DRM_DEBUG_KMS("VBT says port %c has lspcon\n", port_name(port));
2760 }
2761
311a2094 2762 if (!init_dp && !init_hdmi) {
500ea70d 2763 DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
311a2094 2764 port_name(port));
500ea70d 2765 return;
311a2094 2766 }
00c09d70 2767
b14c5679 2768 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
00c09d70
PZ
2769 if (!intel_dig_port)
2770 return;
2771
00c09d70
PZ
2772 intel_encoder = &intel_dig_port->base;
2773 encoder = &intel_encoder->base;
2774
c39055b0 2775 drm_encoder_init(&dev_priv->drm, encoder, &intel_ddi_funcs,
580d8ed5 2776 DRM_MODE_ENCODER_TMDS, "DDI %c", port_name(port));
00c09d70 2777
5bfe2ac0 2778 intel_encoder->compute_config = intel_ddi_compute_config;
00c09d70 2779 intel_encoder->enable = intel_enable_ddi;
cc3f90f0 2780 if (IS_GEN9_LP(dev_priv))
95a7a2ae 2781 intel_encoder->pre_pll_enable = bxt_ddi_pre_pll_enable;
00c09d70
PZ
2782 intel_encoder->pre_enable = intel_ddi_pre_enable;
2783 intel_encoder->disable = intel_disable_ddi;
2784 intel_encoder->post_disable = intel_ddi_post_disable;
2785 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
045ac3b5 2786 intel_encoder->get_config = intel_ddi_get_config;
bf93ba67 2787 intel_encoder->suspend = intel_dp_encoder_suspend;
62b69566 2788 intel_encoder->get_power_domains = intel_ddi_get_power_domains;
00c09d70
PZ
2789
2790 intel_dig_port->port = port;
bcf53de4
SM
2791 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
2792 (DDI_BUF_PORT_REVERSAL |
2793 DDI_A_4_LANES);
00c09d70 2794
62b69566
ACO
2795 switch (port) {
2796 case PORT_A:
2797 intel_dig_port->ddi_io_power_domain =
2798 POWER_DOMAIN_PORT_DDI_A_IO;
2799 break;
2800 case PORT_B:
2801 intel_dig_port->ddi_io_power_domain =
2802 POWER_DOMAIN_PORT_DDI_B_IO;
2803 break;
2804 case PORT_C:
2805 intel_dig_port->ddi_io_power_domain =
2806 POWER_DOMAIN_PORT_DDI_C_IO;
2807 break;
2808 case PORT_D:
2809 intel_dig_port->ddi_io_power_domain =
2810 POWER_DOMAIN_PORT_DDI_D_IO;
2811 break;
2812 case PORT_E:
2813 intel_dig_port->ddi_io_power_domain =
2814 POWER_DOMAIN_PORT_DDI_E_IO;
2815 break;
2816 default:
2817 MISSING_CASE(port);
2818 }
2819
6c566dc9
MR
2820 /*
2821 * Bspec says that DDI_A_4_LANES is the only supported configuration
2822 * for Broxton. Yet some BIOS fail to set this bit on port A if eDP
2823 * wasn't lit up at boot. Force this bit on in our internal
2824 * configuration so that we use the proper lane count for our
2825 * calculations.
2826 */
cc3f90f0 2827 if (IS_GEN9_LP(dev_priv) && port == PORT_A) {
6c566dc9
MR
2828 if (!(intel_dig_port->saved_port_bits & DDI_A_4_LANES)) {
2829 DRM_DEBUG_KMS("BXT BIOS forgot to set DDI_A_4_LANES for port A; fixing\n");
2830 intel_dig_port->saved_port_bits |= DDI_A_4_LANES;
ed8d60f4 2831 max_lanes = 4;
6c566dc9
MR
2832 }
2833 }
2834
ed8d60f4
MR
2835 intel_dig_port->max_lanes = max_lanes;
2836
00c09d70 2837 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
79f255a0 2838 intel_encoder->power_domain = intel_port_to_power_domain(port);
03cdc1d4 2839 intel_encoder->port = port;
f68d697e 2840 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
bc079e8b 2841 intel_encoder->cloneable = 0;
00c09d70 2842
385e4de0
VS
2843 intel_infoframe_init(intel_dig_port);
2844
f68d697e
CW
2845 if (init_dp) {
2846 if (!intel_ddi_init_dp_connector(intel_dig_port))
2847 goto err;
13cf5504 2848
f68d697e 2849 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
ca4c3890 2850 dev_priv->hotplug.irq_port[port] = intel_dig_port;
f68d697e 2851 }
21a8e6a4 2852
311a2094
PZ
2853 /* In theory we don't need the encoder->type check, but leave it just in
2854 * case we have some really bad VBTs... */
f68d697e
CW
2855 if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
2856 if (!intel_ddi_init_hdmi_connector(intel_dig_port))
2857 goto err;
21a8e6a4 2858 }
f68d697e 2859
ff662124
SS
2860 if (init_lspcon) {
2861 if (lspcon_init(intel_dig_port))
2862 /* TODO: handle hdmi info frame part */
2863 DRM_DEBUG_KMS("LSPCON init success on port %c\n",
2864 port_name(port));
2865 else
2866 /*
2867 * LSPCON init faied, but DP init was success, so
2868 * lets try to drive as DP++ port.
2869 */
2870 DRM_ERROR("LSPCON init failed on port %c\n",
2871 port_name(port));
2872 }
2873
f68d697e
CW
2874 return;
2875
2876err:
2877 drm_encoder_cleanup(encoder);
2878 kfree(intel_dig_port);
00c09d70 2879}