blob: 834820b0b766b616a5dc75587419be05db82abf3 [file] [log] [blame]
Eugeni Dodonov45244b82012-05-09 15:37:20 -03001/*
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
Jani Nikula10122052014-08-27 16:27:30 +030031struct ddi_buf_trans {
32 u32 trans1; /* balance leg enable, de-emph level */
33 u32 trans2; /* vref sel, vswing */
34};
35
Eugeni Dodonov45244b82012-05-09 15:37:20 -030036/* HDMI/DVI modes ignore everything but the last 2 items. So we share
37 * them for both DP and FDI transports, allowing those ports to
38 * automatically adapt to HDMI connections as well
39 */
Jani Nikula10122052014-08-27 16:27:30 +030040static const struct ddi_buf_trans hsw_ddi_translations_dp[] = {
41 { 0x00FFFFFF, 0x0006000E },
42 { 0x00D75FFF, 0x0005000A },
43 { 0x00C30FFF, 0x00040006 },
44 { 0x80AAAFFF, 0x000B0000 },
45 { 0x00FFFFFF, 0x0005000A },
46 { 0x00D75FFF, 0x000C0004 },
47 { 0x80C30FFF, 0x000B0000 },
48 { 0x00FFFFFF, 0x00040006 },
49 { 0x80D75FFF, 0x000B0000 },
Eugeni Dodonov45244b82012-05-09 15:37:20 -030050};
51
Jani Nikula10122052014-08-27 16:27:30 +030052static const struct ddi_buf_trans hsw_ddi_translations_fdi[] = {
53 { 0x00FFFFFF, 0x0007000E },
54 { 0x00D75FFF, 0x000F000A },
55 { 0x00C30FFF, 0x00060006 },
56 { 0x00AAAFFF, 0x001E0000 },
57 { 0x00FFFFFF, 0x000F000A },
58 { 0x00D75FFF, 0x00160004 },
59 { 0x00C30FFF, 0x001E0000 },
60 { 0x00FFFFFF, 0x00060006 },
61 { 0x00D75FFF, 0x001E0000 },
Paulo Zanoni6acab152013-09-12 17:06:24 -030062};
63
Jani Nikula10122052014-08-27 16:27:30 +030064static const struct ddi_buf_trans hsw_ddi_translations_hdmi[] = {
65 /* Idx NT mV d T mV d db */
66 { 0x00FFFFFF, 0x0006000E }, /* 0: 400 400 0 */
67 { 0x00E79FFF, 0x000E000C }, /* 1: 400 500 2 */
68 { 0x00D75FFF, 0x0005000A }, /* 2: 400 600 3.5 */
69 { 0x00FFFFFF, 0x0005000A }, /* 3: 600 600 0 */
70 { 0x00E79FFF, 0x001D0007 }, /* 4: 600 750 2 */
71 { 0x00D75FFF, 0x000C0004 }, /* 5: 600 900 3.5 */
72 { 0x00FFFFFF, 0x00040006 }, /* 6: 800 800 0 */
73 { 0x80E79FFF, 0x00030002 }, /* 7: 800 1000 2 */
74 { 0x00FFFFFF, 0x00140005 }, /* 8: 850 850 0 */
75 { 0x00FFFFFF, 0x000C0004 }, /* 9: 900 900 0 */
76 { 0x00FFFFFF, 0x001C0003 }, /* 10: 950 950 0 */
77 { 0x80FFFFFF, 0x00030002 }, /* 11: 1000 1000 0 */
Eugeni Dodonov45244b82012-05-09 15:37:20 -030078};
79
Jani Nikula10122052014-08-27 16:27:30 +030080static const struct ddi_buf_trans bdw_ddi_translations_edp[] = {
81 { 0x00FFFFFF, 0x00000012 },
82 { 0x00EBAFFF, 0x00020011 },
83 { 0x00C71FFF, 0x0006000F },
84 { 0x00AAAFFF, 0x000E000A },
85 { 0x00FFFFFF, 0x00020011 },
86 { 0x00DB6FFF, 0x0005000F },
87 { 0x00BEEFFF, 0x000A000C },
88 { 0x00FFFFFF, 0x0005000F },
89 { 0x00DB6FFF, 0x000A000C },
Paulo Zanoni300644c2013-11-02 21:07:42 -070090};
91
Jani Nikula10122052014-08-27 16:27:30 +030092static const struct ddi_buf_trans bdw_ddi_translations_dp[] = {
93 { 0x00FFFFFF, 0x0007000E },
94 { 0x00D75FFF, 0x000E000A },
95 { 0x00BEFFFF, 0x00140006 },
96 { 0x80B2CFFF, 0x001B0002 },
97 { 0x00FFFFFF, 0x000E000A },
Rodrigo Vivi17b523b2014-09-24 20:32:43 -040098 { 0x00DB6FFF, 0x00160005 },
Rodrigo Vivi6805b2a2014-09-25 12:28:32 -040099 { 0x80C71FFF, 0x001A0002 },
Jani Nikula10122052014-08-27 16:27:30 +0300100 { 0x00F7DFFF, 0x00180004 },
101 { 0x80D75FFF, 0x001B0002 },
Art Runyane58623c2013-11-02 21:07:41 -0700102};
103
Jani Nikula10122052014-08-27 16:27:30 +0300104static const struct ddi_buf_trans bdw_ddi_translations_fdi[] = {
105 { 0x00FFFFFF, 0x0001000E },
106 { 0x00D75FFF, 0x0004000A },
107 { 0x00C30FFF, 0x00070006 },
108 { 0x00AAAFFF, 0x000C0000 },
109 { 0x00FFFFFF, 0x0004000A },
110 { 0x00D75FFF, 0x00090004 },
111 { 0x00C30FFF, 0x000C0000 },
112 { 0x00FFFFFF, 0x00070006 },
113 { 0x00D75FFF, 0x000C0000 },
Art Runyane58623c2013-11-02 21:07:41 -0700114};
115
Jani Nikula10122052014-08-27 16:27:30 +0300116static const struct ddi_buf_trans bdw_ddi_translations_hdmi[] = {
117 /* Idx NT mV d T mV df db */
118 { 0x00FFFFFF, 0x0007000E }, /* 0: 400 400 0 */
119 { 0x00D75FFF, 0x000E000A }, /* 1: 400 600 3.5 */
120 { 0x00BEFFFF, 0x00140006 }, /* 2: 400 800 6 */
121 { 0x00FFFFFF, 0x0009000D }, /* 3: 450 450 0 */
122 { 0x00FFFFFF, 0x000E000A }, /* 4: 600 600 0 */
123 { 0x00D7FFFF, 0x00140006 }, /* 5: 600 800 2.5 */
124 { 0x80CB2FFF, 0x001B0002 }, /* 6: 600 1000 4.5 */
125 { 0x00FFFFFF, 0x00140006 }, /* 7: 800 800 0 */
126 { 0x80E79FFF, 0x001B0002 }, /* 8: 800 1000 2 */
127 { 0x80FFFFFF, 0x001B0002 }, /* 9: 1000 1000 0 */
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100128};
129
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000130static const struct ddi_buf_trans skl_ddi_translations_dp[] = {
Damien Lespiau6c930682014-11-26 13:37:26 +0000131 { 0x00000018, 0x000000a2 },
132 { 0x00004014, 0x0000009B },
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000133 { 0x00006012, 0x00000088 },
Damien Lespiau6c930682014-11-26 13:37:26 +0000134 { 0x00008010, 0x00000087 },
135 { 0x00000018, 0x0000009B },
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000136 { 0x00004014, 0x00000088 },
Damien Lespiau6c930682014-11-26 13:37:26 +0000137 { 0x00006012, 0x00000087 },
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000138 { 0x00000018, 0x00000088 },
Damien Lespiau6c930682014-11-26 13:37:26 +0000139 { 0x00004014, 0x00000087 },
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000140};
141
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530142/* eDP 1.4 low vswing translation parameters */
143static const struct ddi_buf_trans skl_ddi_translations_edp[] = {
144 { 0x00000018, 0x000000a8 },
145 { 0x00002016, 0x000000ab },
146 { 0x00006012, 0x000000a2 },
147 { 0x00008010, 0x00000088 },
148 { 0x00000018, 0x000000ab },
149 { 0x00004014, 0x000000a2 },
150 { 0x00006012, 0x000000a6 },
151 { 0x00000018, 0x000000a2 },
152 { 0x00005013, 0x0000009c },
153 { 0x00000018, 0x00000088 },
154};
155
156
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000157static const struct ddi_buf_trans skl_ddi_translations_hdmi[] = {
158 /* Idx NT mV T mV db */
Damien Lespiau7ff44672015-03-02 16:19:36 +0000159 { 0x00004014, 0x00000087 }, /* 0: 800 1000 2 */
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000160};
161
Jani Nikula20f4dbe2013-08-30 19:40:28 +0300162enum port intel_ddi_get_encoder_port(struct intel_encoder *intel_encoder)
Paulo Zanonifc914632012-10-05 12:05:54 -0300163{
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300164 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanonifc914632012-10-05 12:05:54 -0300165 int type = intel_encoder->type;
166
Dave Airlie0e32b392014-05-02 14:02:48 +1000167 if (type == INTEL_OUTPUT_DP_MST) {
168 struct intel_digital_port *intel_dig_port = enc_to_mst(encoder)->primary;
169 return intel_dig_port->port;
170 } else if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP ||
Paulo Zanoni00c09d72012-10-26 19:05:52 -0200171 type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_UNKNOWN) {
Paulo Zanoni174edf12012-10-26 19:05:50 -0200172 struct intel_digital_port *intel_dig_port =
173 enc_to_dig_port(encoder);
174 return intel_dig_port->port;
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300175
Paulo Zanonifc914632012-10-05 12:05:54 -0300176 } else if (type == INTEL_OUTPUT_ANALOG) {
177 return PORT_E;
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300178
Paulo Zanonifc914632012-10-05 12:05:54 -0300179 } else {
180 DRM_ERROR("Invalid DDI encoder type %d\n", type);
181 BUG();
182 }
183}
184
Art Runyane58623c2013-11-02 21:07:41 -0700185/*
186 * Starting with Haswell, DDI port buffers must be programmed with correct
187 * values in advance. The buffer values are different for FDI and DP modes,
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300188 * but the HDMI/DVI fields are shared among those. So we program the DDI
189 * in either FDI or DP modes only, as HDMI connections will work with both
190 * of those
191 */
Paulo Zanoniad8d2702013-08-05 17:25:56 -0300192static void intel_prepare_ddi_buffers(struct drm_device *dev, enum port port)
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300193{
194 struct drm_i915_private *dev_priv = dev->dev_private;
195 u32 reg;
Damien Lespiau7ff44672015-03-02 16:19:36 +0000196 int i, n_hdmi_entries, n_dp_entries, n_edp_entries, hdmi_default_entry,
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530197 size;
Paulo Zanoni6acab152013-09-12 17:06:24 -0300198 int hdmi_level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
Jani Nikula10122052014-08-27 16:27:30 +0300199 const struct ddi_buf_trans *ddi_translations_fdi;
200 const struct ddi_buf_trans *ddi_translations_dp;
201 const struct ddi_buf_trans *ddi_translations_edp;
202 const struct ddi_buf_trans *ddi_translations_hdmi;
203 const struct ddi_buf_trans *ddi_translations;
Art Runyane58623c2013-11-02 21:07:41 -0700204
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000205 if (IS_SKYLAKE(dev)) {
206 ddi_translations_fdi = NULL;
207 ddi_translations_dp = skl_ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530208 n_dp_entries = ARRAY_SIZE(skl_ddi_translations_dp);
209 if (dev_priv->vbt.edp_low_vswing) {
210 ddi_translations_edp = skl_ddi_translations_edp;
211 n_edp_entries = ARRAY_SIZE(skl_ddi_translations_edp);
212 } else {
213 ddi_translations_edp = skl_ddi_translations_dp;
214 n_edp_entries = ARRAY_SIZE(skl_ddi_translations_dp);
215 }
216
Damien Lespiau7ff44672015-03-02 16:19:36 +0000217 /*
218 * On SKL, the recommendation from the hw team is to always use
219 * a certain type of level shifter (and thus the corresponding
220 * 800mV+2dB entry). Given that's the only validated entry, we
221 * override what is in the VBT, at least until further notice.
222 */
223 hdmi_level = 0;
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000224 ddi_translations_hdmi = skl_ddi_translations_hdmi;
225 n_hdmi_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000226 hdmi_default_entry = 0;
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000227 } else if (IS_BROADWELL(dev)) {
Art Runyane58623c2013-11-02 21:07:41 -0700228 ddi_translations_fdi = bdw_ddi_translations_fdi;
229 ddi_translations_dp = bdw_ddi_translations_dp;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700230 ddi_translations_edp = bdw_ddi_translations_edp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100231 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530232 n_edp_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
233 n_dp_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300234 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000235 hdmi_default_entry = 7;
Art Runyane58623c2013-11-02 21:07:41 -0700236 } else if (IS_HASWELL(dev)) {
237 ddi_translations_fdi = hsw_ddi_translations_fdi;
238 ddi_translations_dp = hsw_ddi_translations_dp;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700239 ddi_translations_edp = hsw_ddi_translations_dp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100240 ddi_translations_hdmi = hsw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530241 n_dp_entries = n_edp_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300242 n_hdmi_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000243 hdmi_default_entry = 6;
Art Runyane58623c2013-11-02 21:07:41 -0700244 } else {
245 WARN(1, "ddi translation table missing\n");
Paulo Zanoni300644c2013-11-02 21:07:42 -0700246 ddi_translations_edp = bdw_ddi_translations_dp;
Art Runyane58623c2013-11-02 21:07:41 -0700247 ddi_translations_fdi = bdw_ddi_translations_fdi;
248 ddi_translations_dp = bdw_ddi_translations_dp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100249 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530250 n_edp_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
251 n_dp_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300252 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000253 hdmi_default_entry = 7;
Art Runyane58623c2013-11-02 21:07:41 -0700254 }
255
Paulo Zanoni300644c2013-11-02 21:07:42 -0700256 switch (port) {
257 case PORT_A:
258 ddi_translations = ddi_translations_edp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530259 size = n_edp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700260 break;
261 case PORT_B:
262 case PORT_C:
Paulo Zanoni300644c2013-11-02 21:07:42 -0700263 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530264 size = n_dp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700265 break;
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700266 case PORT_D:
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530267 if (intel_dp_is_edp(dev, PORT_D)) {
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700268 ddi_translations = ddi_translations_edp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530269 size = n_edp_entries;
270 } else {
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700271 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530272 size = n_dp_entries;
273 }
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700274 break;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700275 case PORT_E:
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000276 if (ddi_translations_fdi)
277 ddi_translations = ddi_translations_fdi;
278 else
279 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530280 size = n_dp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700281 break;
282 default:
283 BUG();
284 }
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300285
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530286 for (i = 0, reg = DDI_BUF_TRANS(port); i < size; i++) {
Jani Nikula10122052014-08-27 16:27:30 +0300287 I915_WRITE(reg, ddi_translations[i].trans1);
288 reg += 4;
289 I915_WRITE(reg, ddi_translations[i].trans2);
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300290 reg += 4;
291 }
Damien Lespiauce4dd492014-08-01 11:07:54 +0100292
293 /* Choose a good default if VBT is badly populated */
294 if (hdmi_level == HDMI_LEVEL_SHIFT_UNKNOWN ||
295 hdmi_level >= n_hdmi_entries)
Damien Lespiau7ff44672015-03-02 16:19:36 +0000296 hdmi_level = hdmi_default_entry;
Damien Lespiauce4dd492014-08-01 11:07:54 +0100297
Paulo Zanoni6acab152013-09-12 17:06:24 -0300298 /* Entry 9 is for HDMI: */
Jani Nikula10122052014-08-27 16:27:30 +0300299 I915_WRITE(reg, ddi_translations_hdmi[hdmi_level].trans1);
300 reg += 4;
301 I915_WRITE(reg, ddi_translations_hdmi[hdmi_level].trans2);
302 reg += 4;
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300303}
304
305/* Program DDI buffers translations for DP. By default, program ports A-D in DP
306 * mode and port E for FDI.
307 */
308void intel_prepare_ddi(struct drm_device *dev)
309{
310 int port;
311
Paulo Zanoni0d536cb42012-11-23 16:46:41 -0200312 if (!HAS_DDI(dev))
313 return;
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300314
Paulo Zanoniad8d2702013-08-05 17:25:56 -0300315 for (port = PORT_A; port <= PORT_E; port++)
316 intel_prepare_ddi_buffers(dev, port);
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300317}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300318
Paulo Zanoni248138b2012-11-29 11:29:31 -0200319static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
320 enum port port)
321{
322 uint32_t reg = DDI_BUF_CTL(port);
323 int i;
324
325 for (i = 0; i < 8; i++) {
326 udelay(1);
327 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
328 return;
329 }
330 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
331}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300332
333/* Starting with Haswell, different DDI ports can work in FDI mode for
334 * connection to the PCH-located connectors. For this, it is necessary to train
335 * both the DDI port and PCH receiver for the desired DDI buffer settings.
336 *
337 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
338 * please note that when FDI mode is active on DDI E, it shares 2 lines with
339 * DDI A (which is used for eDP)
340 */
341
342void hsw_fdi_link_train(struct drm_crtc *crtc)
343{
344 struct drm_device *dev = crtc->dev;
345 struct drm_i915_private *dev_priv = dev->dev_private;
346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni04945642012-11-01 21:00:59 -0200347 u32 temp, i, rx_ctl_val;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300348
Paulo Zanoni04945642012-11-01 21:00:59 -0200349 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
350 * mode set "sequence for CRT port" document:
351 * - TP1 to TP2 time with the default value
352 * - FDI delay to 90h
Damien Lespiau8693a822013-05-03 18:48:11 +0100353 *
354 * WaFDIAutoLinkSetTimingOverrride:hsw
Paulo Zanoni04945642012-11-01 21:00:59 -0200355 */
356 I915_WRITE(_FDI_RXA_MISC, FDI_RX_PWRDN_LANE1_VAL(2) |
357 FDI_RX_PWRDN_LANE0_VAL(2) |
358 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
359
360 /* Enable the PCH Receiver FDI PLL */
Damien Lespiau3e683202012-12-11 18:48:29 +0000361 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
Daniel Vetter33d29b12013-02-13 18:04:45 +0100362 FDI_RX_PLL_ENABLE |
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200363 FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Paulo Zanoni04945642012-11-01 21:00:59 -0200364 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
365 POSTING_READ(_FDI_RXA_CTL);
366 udelay(220);
367
368 /* Switch from Rawclk to PCDclk */
369 rx_ctl_val |= FDI_PCDCLK;
370 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
371
372 /* Configure Port Clock Select */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200373 I915_WRITE(PORT_CLK_SEL(PORT_E), intel_crtc->config->ddi_pll_sel);
374 WARN_ON(intel_crtc->config->ddi_pll_sel != PORT_CLK_SEL_SPLL);
Paulo Zanoni04945642012-11-01 21:00:59 -0200375
376 /* Start the training iterating through available voltages and emphasis,
377 * testing each value twice. */
Jani Nikula10122052014-08-27 16:27:30 +0300378 for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300379 /* Configure DP_TP_CTL with auto-training */
380 I915_WRITE(DP_TP_CTL(PORT_E),
381 DP_TP_CTL_FDI_AUTOTRAIN |
382 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
383 DP_TP_CTL_LINK_TRAIN_PAT1 |
384 DP_TP_CTL_ENABLE);
385
Damien Lespiau876a8cd2012-12-11 18:48:30 +0000386 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
387 * DDI E does not support port reversal, the functionality is
388 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
389 * port reversal bit */
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300390 I915_WRITE(DDI_BUF_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200391 DDI_BUF_CTL_ENABLE |
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200392 ((intel_crtc->config->fdi_lanes - 1) << 1) |
Sonika Jindalc5fe6a02014-08-11 08:57:36 +0530393 DDI_BUF_TRANS_SELECT(i / 2));
Paulo Zanoni04945642012-11-01 21:00:59 -0200394 POSTING_READ(DDI_BUF_CTL(PORT_E));
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300395
396 udelay(600);
397
Paulo Zanoni04945642012-11-01 21:00:59 -0200398 /* Program PCH FDI Receiver TU */
399 I915_WRITE(_FDI_RXA_TUSIZE1, TU_SIZE(64));
Eugeni Dodonov4acf5182012-07-04 20:15:16 -0300400
Paulo Zanoni04945642012-11-01 21:00:59 -0200401 /* Enable PCH FDI Receiver with auto-training */
402 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
403 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
404 POSTING_READ(_FDI_RXA_CTL);
405
406 /* Wait for FDI receiver lane calibration */
407 udelay(30);
408
409 /* Unset FDI_RX_MISC pwrdn lanes */
410 temp = I915_READ(_FDI_RXA_MISC);
411 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
412 I915_WRITE(_FDI_RXA_MISC, temp);
413 POSTING_READ(_FDI_RXA_MISC);
414
415 /* Wait for FDI auto training time */
416 udelay(5);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300417
418 temp = I915_READ(DP_TP_STATUS(PORT_E));
419 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
Paulo Zanoni04945642012-11-01 21:00:59 -0200420 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300421
422 /* Enable normal pixel sending for FDI */
423 I915_WRITE(DP_TP_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200424 DP_TP_CTL_FDI_AUTOTRAIN |
425 DP_TP_CTL_LINK_TRAIN_NORMAL |
426 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
427 DP_TP_CTL_ENABLE);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300428
Paulo Zanoni04945642012-11-01 21:00:59 -0200429 return;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300430 }
Paulo Zanoni04945642012-11-01 21:00:59 -0200431
Paulo Zanoni248138b2012-11-29 11:29:31 -0200432 temp = I915_READ(DDI_BUF_CTL(PORT_E));
433 temp &= ~DDI_BUF_CTL_ENABLE;
434 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
435 POSTING_READ(DDI_BUF_CTL(PORT_E));
436
Paulo Zanoni04945642012-11-01 21:00:59 -0200437 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
Paulo Zanoni248138b2012-11-29 11:29:31 -0200438 temp = I915_READ(DP_TP_CTL(PORT_E));
439 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
440 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
441 I915_WRITE(DP_TP_CTL(PORT_E), temp);
442 POSTING_READ(DP_TP_CTL(PORT_E));
443
444 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
Paulo Zanoni04945642012-11-01 21:00:59 -0200445
446 rx_ctl_val &= ~FDI_RX_ENABLE;
447 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200448 POSTING_READ(_FDI_RXA_CTL);
Paulo Zanoni04945642012-11-01 21:00:59 -0200449
450 /* Reset FDI_RX_MISC pwrdn lanes */
451 temp = I915_READ(_FDI_RXA_MISC);
452 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
453 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
454 I915_WRITE(_FDI_RXA_MISC, temp);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200455 POSTING_READ(_FDI_RXA_MISC);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300456 }
457
Paulo Zanoni04945642012-11-01 21:00:59 -0200458 DRM_ERROR("FDI link training failed!\n");
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300459}
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -0300460
Dave Airlie44905a272014-05-02 13:36:43 +1000461void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
462{
463 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
464 struct intel_digital_port *intel_dig_port =
465 enc_to_dig_port(&encoder->base);
466
467 intel_dp->DP = intel_dig_port->saved_port_bits |
Sonika Jindalc5fe6a02014-08-11 08:57:36 +0530468 DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
Dave Airlie44905a272014-05-02 13:36:43 +1000469 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
470
471}
472
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300473static struct intel_encoder *
474intel_ddi_get_crtc_encoder(struct drm_crtc *crtc)
475{
476 struct drm_device *dev = crtc->dev;
477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
478 struct intel_encoder *intel_encoder, *ret = NULL;
479 int num_encoders = 0;
480
481 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
482 ret = intel_encoder;
483 num_encoders++;
484 }
485
486 if (num_encoders != 1)
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300487 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
488 pipe_name(intel_crtc->pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300489
490 BUG_ON(ret == NULL);
491 return ret;
492}
493
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200494static struct intel_encoder *
495intel_ddi_get_crtc_new_encoder(struct intel_crtc *crtc)
496{
497 struct drm_device *dev = crtc->base.dev;
498 struct intel_encoder *intel_encoder, *ret = NULL;
499 int num_encoders = 0;
500
501 for_each_intel_encoder(dev, intel_encoder) {
502 if (intel_encoder->new_crtc == crtc) {
503 ret = intel_encoder;
504 num_encoders++;
505 }
506 }
507
508 WARN(num_encoders != 1, "%d encoders on crtc for pipe %c\n", num_encoders,
509 pipe_name(crtc->pipe));
510
511 BUG_ON(ret == NULL);
512 return ret;
513}
514
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100515#define LC_FREQ 2700
Damien Lespiau27893392014-09-04 12:27:23 +0100516#define LC_FREQ_2K U64_C(LC_FREQ * 2000)
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100517
518#define P_MIN 2
519#define P_MAX 64
520#define P_INC 2
521
522/* Constraints for PLL good behavior */
523#define REF_MIN 48
524#define REF_MAX 400
525#define VCO_MIN 2400
526#define VCO_MAX 4800
527
Damien Lespiau27893392014-09-04 12:27:23 +0100528#define abs_diff(a, b) ({ \
529 typeof(a) __a = (a); \
530 typeof(b) __b = (b); \
531 (void) (&__a == &__b); \
532 __a > __b ? (__a - __b) : (__b - __a); })
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100533
534struct wrpll_rnp {
535 unsigned p, n2, r2;
536};
537
538static unsigned wrpll_get_budget_for_freq(int clock)
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300539{
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100540 unsigned budget;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300541
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100542 switch (clock) {
543 case 25175000:
544 case 25200000:
545 case 27000000:
546 case 27027000:
547 case 37762500:
548 case 37800000:
549 case 40500000:
550 case 40541000:
551 case 54000000:
552 case 54054000:
553 case 59341000:
554 case 59400000:
555 case 72000000:
556 case 74176000:
557 case 74250000:
558 case 81000000:
559 case 81081000:
560 case 89012000:
561 case 89100000:
562 case 108000000:
563 case 108108000:
564 case 111264000:
565 case 111375000:
566 case 148352000:
567 case 148500000:
568 case 162000000:
569 case 162162000:
570 case 222525000:
571 case 222750000:
572 case 296703000:
573 case 297000000:
574 budget = 0;
575 break;
576 case 233500000:
577 case 245250000:
578 case 247750000:
579 case 253250000:
580 case 298000000:
581 budget = 1500;
582 break;
583 case 169128000:
584 case 169500000:
585 case 179500000:
586 case 202000000:
587 budget = 2000;
588 break;
589 case 256250000:
590 case 262500000:
591 case 270000000:
592 case 272500000:
593 case 273750000:
594 case 280750000:
595 case 281250000:
596 case 286000000:
597 case 291750000:
598 budget = 4000;
599 break;
600 case 267250000:
601 case 268500000:
602 budget = 5000;
603 break;
604 default:
605 budget = 1000;
606 break;
607 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300608
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100609 return budget;
610}
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300611
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100612static void wrpll_update_rnp(uint64_t freq2k, unsigned budget,
613 unsigned r2, unsigned n2, unsigned p,
614 struct wrpll_rnp *best)
615{
616 uint64_t a, b, c, d, diff, diff_best;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300617
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100618 /* No best (r,n,p) yet */
619 if (best->p == 0) {
620 best->p = p;
621 best->n2 = n2;
622 best->r2 = r2;
623 return;
624 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300625
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100626 /*
627 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
628 * freq2k.
629 *
630 * delta = 1e6 *
631 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
632 * freq2k;
633 *
634 * and we would like delta <= budget.
635 *
636 * If the discrepancy is above the PPM-based budget, always prefer to
637 * improve upon the previous solution. However, if you're within the
638 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
639 */
640 a = freq2k * budget * p * r2;
641 b = freq2k * budget * best->p * best->r2;
Damien Lespiau27893392014-09-04 12:27:23 +0100642 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
643 diff_best = abs_diff(freq2k * best->p * best->r2,
644 LC_FREQ_2K * best->n2);
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100645 c = 1000000 * diff;
646 d = 1000000 * diff_best;
647
648 if (a < c && b < d) {
649 /* If both are above the budget, pick the closer */
650 if (best->p * best->r2 * diff < p * r2 * diff_best) {
651 best->p = p;
652 best->n2 = n2;
653 best->r2 = r2;
654 }
655 } else if (a >= c && b < d) {
656 /* If A is below the threshold but B is above it? Update. */
657 best->p = p;
658 best->n2 = n2;
659 best->r2 = r2;
660 } else if (a >= c && b >= d) {
661 /* Both are below the limit, so pick the higher n2/(r2*r2) */
662 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
663 best->p = p;
664 best->n2 = n2;
665 best->r2 = r2;
666 }
667 }
668 /* Otherwise a < c && b >= d, do nothing */
669}
670
Jesse Barnes11578552014-01-21 12:42:10 -0800671static int intel_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
672 int reg)
673{
674 int refclk = LC_FREQ;
675 int n, p, r;
676 u32 wrpll;
677
678 wrpll = I915_READ(reg);
Daniel Vetter114fe482014-06-25 22:01:48 +0300679 switch (wrpll & WRPLL_PLL_REF_MASK) {
680 case WRPLL_PLL_SSC:
681 case WRPLL_PLL_NON_SSC:
Jesse Barnes11578552014-01-21 12:42:10 -0800682 /*
683 * We could calculate spread here, but our checking
684 * code only cares about 5% accuracy, and spread is a max of
685 * 0.5% downspread.
686 */
687 refclk = 135;
688 break;
Daniel Vetter114fe482014-06-25 22:01:48 +0300689 case WRPLL_PLL_LCPLL:
Jesse Barnes11578552014-01-21 12:42:10 -0800690 refclk = LC_FREQ;
691 break;
692 default:
693 WARN(1, "bad wrpll refclk\n");
694 return 0;
695 }
696
697 r = wrpll & WRPLL_DIVIDER_REF_MASK;
698 p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
699 n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
700
Jesse Barnes20f0ec12014-01-22 12:58:04 -0800701 /* Convert to KHz, p & r have a fixed point portion */
702 return (refclk * n * 100) / (p * r);
Jesse Barnes11578552014-01-21 12:42:10 -0800703}
704
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000705static int skl_calc_wrpll_link(struct drm_i915_private *dev_priv,
706 uint32_t dpll)
707{
708 uint32_t cfgcr1_reg, cfgcr2_reg;
709 uint32_t cfgcr1_val, cfgcr2_val;
710 uint32_t p0, p1, p2, dco_freq;
711
712 cfgcr1_reg = GET_CFG_CR1_REG(dpll);
713 cfgcr2_reg = GET_CFG_CR2_REG(dpll);
714
715 cfgcr1_val = I915_READ(cfgcr1_reg);
716 cfgcr2_val = I915_READ(cfgcr2_reg);
717
718 p0 = cfgcr2_val & DPLL_CFGCR2_PDIV_MASK;
719 p2 = cfgcr2_val & DPLL_CFGCR2_KDIV_MASK;
720
721 if (cfgcr2_val & DPLL_CFGCR2_QDIV_MODE(1))
722 p1 = (cfgcr2_val & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
723 else
724 p1 = 1;
725
726
727 switch (p0) {
728 case DPLL_CFGCR2_PDIV_1:
729 p0 = 1;
730 break;
731 case DPLL_CFGCR2_PDIV_2:
732 p0 = 2;
733 break;
734 case DPLL_CFGCR2_PDIV_3:
735 p0 = 3;
736 break;
737 case DPLL_CFGCR2_PDIV_7:
738 p0 = 7;
739 break;
740 }
741
742 switch (p2) {
743 case DPLL_CFGCR2_KDIV_5:
744 p2 = 5;
745 break;
746 case DPLL_CFGCR2_KDIV_2:
747 p2 = 2;
748 break;
749 case DPLL_CFGCR2_KDIV_3:
750 p2 = 3;
751 break;
752 case DPLL_CFGCR2_KDIV_1:
753 p2 = 1;
754 break;
755 }
756
757 dco_freq = (cfgcr1_val & DPLL_CFGCR1_DCO_INTEGER_MASK) * 24 * 1000;
758
759 dco_freq += (((cfgcr1_val & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9) * 24 *
760 1000) / 0x8000;
761
762 return dco_freq / (p0 * p1 * p2 * 5);
763}
764
765
766static void skl_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200767 struct intel_crtc_state *pipe_config)
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000768{
769 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000770 int link_clock = 0;
771 uint32_t dpll_ctl1, dpll;
772
Damien Lespiau134ffa42014-11-14 17:24:34 +0000773 dpll = pipe_config->ddi_pll_sel;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000774
775 dpll_ctl1 = I915_READ(DPLL_CTRL1);
776
777 if (dpll_ctl1 & DPLL_CTRL1_HDMI_MODE(dpll)) {
778 link_clock = skl_calc_wrpll_link(dev_priv, dpll);
779 } else {
780 link_clock = dpll_ctl1 & DPLL_CRTL1_LINK_RATE_MASK(dpll);
781 link_clock >>= DPLL_CRTL1_LINK_RATE_SHIFT(dpll);
782
783 switch (link_clock) {
784 case DPLL_CRTL1_LINK_RATE_810:
785 link_clock = 81000;
786 break;
787 case DPLL_CRTL1_LINK_RATE_1350:
788 link_clock = 135000;
789 break;
790 case DPLL_CRTL1_LINK_RATE_2700:
791 link_clock = 270000;
792 break;
793 default:
794 WARN(1, "Unsupported link rate\n");
795 break;
796 }
797 link_clock *= 2;
798 }
799
800 pipe_config->port_clock = link_clock;
801
802 if (pipe_config->has_dp_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200803 pipe_config->base.adjusted_mode.crtc_clock =
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000804 intel_dotclock_calculate(pipe_config->port_clock,
805 &pipe_config->dp_m_n);
806 else
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200807 pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000808}
809
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200810static void hsw_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200811 struct intel_crtc_state *pipe_config)
Jesse Barnes11578552014-01-21 12:42:10 -0800812{
813 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Jesse Barnes11578552014-01-21 12:42:10 -0800814 int link_clock = 0;
815 u32 val, pll;
816
Daniel Vetter26804af2014-06-25 22:01:55 +0300817 val = pipe_config->ddi_pll_sel;
Jesse Barnes11578552014-01-21 12:42:10 -0800818 switch (val & PORT_CLK_SEL_MASK) {
819 case PORT_CLK_SEL_LCPLL_810:
820 link_clock = 81000;
821 break;
822 case PORT_CLK_SEL_LCPLL_1350:
823 link_clock = 135000;
824 break;
825 case PORT_CLK_SEL_LCPLL_2700:
826 link_clock = 270000;
827 break;
828 case PORT_CLK_SEL_WRPLL1:
829 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL1);
830 break;
831 case PORT_CLK_SEL_WRPLL2:
832 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL2);
833 break;
834 case PORT_CLK_SEL_SPLL:
835 pll = I915_READ(SPLL_CTL) & SPLL_PLL_FREQ_MASK;
836 if (pll == SPLL_PLL_FREQ_810MHz)
837 link_clock = 81000;
838 else if (pll == SPLL_PLL_FREQ_1350MHz)
839 link_clock = 135000;
840 else if (pll == SPLL_PLL_FREQ_2700MHz)
841 link_clock = 270000;
842 else {
843 WARN(1, "bad spll freq\n");
844 return;
845 }
846 break;
847 default:
848 WARN(1, "bad port clock sel\n");
849 return;
850 }
851
852 pipe_config->port_clock = link_clock * 2;
853
854 if (pipe_config->has_pch_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200855 pipe_config->base.adjusted_mode.crtc_clock =
Jesse Barnes11578552014-01-21 12:42:10 -0800856 intel_dotclock_calculate(pipe_config->port_clock,
857 &pipe_config->fdi_m_n);
858 else if (pipe_config->has_dp_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200859 pipe_config->base.adjusted_mode.crtc_clock =
Jesse Barnes11578552014-01-21 12:42:10 -0800860 intel_dotclock_calculate(pipe_config->port_clock,
861 &pipe_config->dp_m_n);
862 else
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200863 pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
Jesse Barnes11578552014-01-21 12:42:10 -0800864}
865
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200866void intel_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200867 struct intel_crtc_state *pipe_config)
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200868{
Damien Lespiau22606a12014-12-12 14:26:57 +0000869 struct drm_device *dev = encoder->base.dev;
870
871 if (INTEL_INFO(dev)->gen <= 8)
872 hsw_ddi_clock_get(encoder, pipe_config);
873 else
874 skl_ddi_clock_get(encoder, pipe_config);
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200875}
876
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100877static void
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100878hsw_ddi_calculate_wrpll(int clock /* in Hz */,
879 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100880{
881 uint64_t freq2k;
882 unsigned p, n2, r2;
883 struct wrpll_rnp best = { 0, 0, 0 };
884 unsigned budget;
885
886 freq2k = clock / 100;
887
888 budget = wrpll_get_budget_for_freq(clock);
889
890 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
891 * and directly pass the LC PLL to it. */
892 if (freq2k == 5400000) {
893 *n2_out = 2;
894 *p_out = 1;
895 *r2_out = 2;
896 return;
897 }
898
899 /*
900 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
901 * the WR PLL.
902 *
903 * We want R so that REF_MIN <= Ref <= REF_MAX.
904 * Injecting R2 = 2 * R gives:
905 * REF_MAX * r2 > LC_FREQ * 2 and
906 * REF_MIN * r2 < LC_FREQ * 2
907 *
908 * Which means the desired boundaries for r2 are:
909 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
910 *
911 */
912 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
913 r2 <= LC_FREQ * 2 / REF_MIN;
914 r2++) {
915
916 /*
917 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
918 *
919 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
920 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
921 * VCO_MAX * r2 > n2 * LC_FREQ and
922 * VCO_MIN * r2 < n2 * LC_FREQ)
923 *
924 * Which means the desired boundaries for n2 are:
925 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
926 */
927 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
928 n2 <= VCO_MAX * r2 / LC_FREQ;
929 n2++) {
930
931 for (p = P_MIN; p <= P_MAX; p += P_INC)
932 wrpll_update_rnp(freq2k, budget,
933 r2, n2, p, &best);
934 }
935 }
936
937 *n2_out = best.n2;
938 *p_out = best.p;
939 *r2_out = best.r2;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300940}
941
Damien Lespiau0220ab62014-07-29 18:06:22 +0100942static bool
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100943hsw_ddi_pll_select(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +0200944 struct intel_crtc_state *crtc_state,
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100945 struct intel_encoder *intel_encoder,
946 int clock)
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300947{
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100948 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
Daniel Vettere0b01be2014-06-25 22:02:01 +0300949 struct intel_shared_dpll *pll;
Daniel Vetter716c2e52014-06-25 22:02:02 +0300950 uint32_t val;
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100951 unsigned p, n2, r2;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300952
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100953 hsw_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300954
Daniel Vetter114fe482014-06-25 22:01:48 +0300955 val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300956 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
957 WRPLL_DIVIDER_POST(p);
958
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +0200959 crtc_state->dpll_hw_state.wrpll = val;
Daniel Vetter716c2e52014-06-25 22:02:02 +0300960
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +0200961 pll = intel_get_shared_dpll(intel_crtc, crtc_state);
Daniel Vetter716c2e52014-06-25 22:02:02 +0300962 if (pll == NULL) {
963 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
964 pipe_name(intel_crtc->pipe));
Paulo Zanoni06940012013-10-30 18:27:43 -0200965 return false;
966 }
967
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +0200968 crtc_state->ddi_pll_sel = PORT_CLK_SEL_WRPLL(pll->id);
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300969 }
970
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300971 return true;
972}
973
Satheeshakrishna M82d35432014-11-13 14:55:20 +0000974struct skl_wrpll_params {
975 uint32_t dco_fraction;
976 uint32_t dco_integer;
977 uint32_t qdiv_ratio;
978 uint32_t qdiv_mode;
979 uint32_t kdiv;
980 uint32_t pdiv;
981 uint32_t central_freq;
982};
983
984static void
985skl_ddi_calculate_wrpll(int clock /* in Hz */,
986 struct skl_wrpll_params *wrpll_params)
987{
988 uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
Damien Lespiau21318cc2014-11-14 14:20:27 +0000989 uint64_t dco_central_freq[3] = {8400000000ULL,
990 9000000000ULL,
991 9600000000ULL};
Satheeshakrishna M82d35432014-11-13 14:55:20 +0000992 uint32_t min_dco_deviation = 400;
993 uint32_t min_dco_index = 3;
994 uint32_t P0[4] = {1, 2, 3, 7};
995 uint32_t P2[4] = {1, 2, 3, 5};
996 bool found = false;
997 uint32_t candidate_p = 0;
998 uint32_t candidate_p0[3] = {0}, candidate_p1[3] = {0};
999 uint32_t candidate_p2[3] = {0};
1000 uint32_t dco_central_freq_deviation[3];
1001 uint32_t i, P1, k, dco_count;
1002 bool retry_with_odd = false;
1003 uint64_t dco_freq;
1004
1005 /* Determine P0, P1 or P2 */
1006 for (dco_count = 0; dco_count < 3; dco_count++) {
1007 found = false;
1008 candidate_p =
1009 div64_u64(dco_central_freq[dco_count], afe_clock);
1010 if (retry_with_odd == false)
1011 candidate_p = (candidate_p % 2 == 0 ?
1012 candidate_p : candidate_p + 1);
1013
1014 for (P1 = 1; P1 < candidate_p; P1++) {
1015 for (i = 0; i < 4; i++) {
1016 if (!(P0[i] != 1 || P1 == 1))
1017 continue;
1018
1019 for (k = 0; k < 4; k++) {
1020 if (P1 != 1 && P2[k] != 2)
1021 continue;
1022
1023 if (candidate_p == P0[i] * P1 * P2[k]) {
1024 /* Found possible P0, P1, P2 */
1025 found = true;
1026 candidate_p0[dco_count] = P0[i];
1027 candidate_p1[dco_count] = P1;
1028 candidate_p2[dco_count] = P2[k];
1029 goto found;
1030 }
1031
1032 }
1033 }
1034 }
1035
1036found:
1037 if (found) {
1038 dco_central_freq_deviation[dco_count] =
1039 div64_u64(10000 *
1040 abs_diff((candidate_p * afe_clock),
1041 dco_central_freq[dco_count]),
1042 dco_central_freq[dco_count]);
1043
1044 if (dco_central_freq_deviation[dco_count] <
1045 min_dco_deviation) {
1046 min_dco_deviation =
1047 dco_central_freq_deviation[dco_count];
1048 min_dco_index = dco_count;
1049 }
1050 }
1051
1052 if (min_dco_index > 2 && dco_count == 2) {
1053 retry_with_odd = true;
1054 dco_count = 0;
1055 }
1056 }
1057
1058 if (min_dco_index > 2) {
1059 WARN(1, "No valid values found for the given pixel clock\n");
1060 } else {
1061 wrpll_params->central_freq = dco_central_freq[min_dco_index];
1062
1063 switch (dco_central_freq[min_dco_index]) {
Damien Lespiau21318cc2014-11-14 14:20:27 +00001064 case 9600000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001065 wrpll_params->central_freq = 0;
1066 break;
Damien Lespiau21318cc2014-11-14 14:20:27 +00001067 case 9000000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001068 wrpll_params->central_freq = 1;
1069 break;
Damien Lespiau21318cc2014-11-14 14:20:27 +00001070 case 8400000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001071 wrpll_params->central_freq = 3;
1072 }
1073
1074 switch (candidate_p0[min_dco_index]) {
1075 case 1:
1076 wrpll_params->pdiv = 0;
1077 break;
1078 case 2:
1079 wrpll_params->pdiv = 1;
1080 break;
1081 case 3:
1082 wrpll_params->pdiv = 2;
1083 break;
1084 case 7:
1085 wrpll_params->pdiv = 4;
1086 break;
1087 default:
1088 WARN(1, "Incorrect PDiv\n");
1089 }
1090
1091 switch (candidate_p2[min_dco_index]) {
1092 case 5:
1093 wrpll_params->kdiv = 0;
1094 break;
1095 case 2:
1096 wrpll_params->kdiv = 1;
1097 break;
1098 case 3:
1099 wrpll_params->kdiv = 2;
1100 break;
1101 case 1:
1102 wrpll_params->kdiv = 3;
1103 break;
1104 default:
1105 WARN(1, "Incorrect KDiv\n");
1106 }
1107
1108 wrpll_params->qdiv_ratio = candidate_p1[min_dco_index];
1109 wrpll_params->qdiv_mode =
1110 (wrpll_params->qdiv_ratio == 1) ? 0 : 1;
1111
1112 dco_freq = candidate_p0[min_dco_index] *
1113 candidate_p1[min_dco_index] *
1114 candidate_p2[min_dco_index] * afe_clock;
1115
1116 /*
1117 * Intermediate values are in Hz.
1118 * Divide by MHz to match bsepc
1119 */
1120 wrpll_params->dco_integer = div_u64(dco_freq, (24 * MHz(1)));
1121 wrpll_params->dco_fraction =
1122 div_u64(((div_u64(dco_freq, 24) -
1123 wrpll_params->dco_integer * MHz(1)) * 0x8000), MHz(1));
1124
1125 }
1126}
1127
1128
1129static bool
1130skl_ddi_pll_select(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001131 struct intel_crtc_state *crtc_state,
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001132 struct intel_encoder *intel_encoder,
1133 int clock)
1134{
1135 struct intel_shared_dpll *pll;
1136 uint32_t ctrl1, cfgcr1, cfgcr2;
1137
1138 /*
1139 * See comment in intel_dpll_hw_state to understand why we always use 0
1140 * as the DPLL id in this function.
1141 */
1142
1143 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1144
1145 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
1146 struct skl_wrpll_params wrpll_params = { 0, };
1147
1148 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1149
1150 skl_ddi_calculate_wrpll(clock * 1000, &wrpll_params);
1151
1152 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1153 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1154 wrpll_params.dco_integer;
1155
1156 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1157 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1158 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1159 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1160 wrpll_params.central_freq;
1161 } else if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
1162 struct drm_encoder *encoder = &intel_encoder->base;
1163 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1164
1165 switch (intel_dp->link_bw) {
1166 case DP_LINK_BW_1_62:
1167 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_810, 0);
1168 break;
1169 case DP_LINK_BW_2_7:
1170 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_1350, 0);
1171 break;
1172 case DP_LINK_BW_5_4:
1173 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_2700, 0);
1174 break;
1175 }
1176
1177 cfgcr1 = cfgcr2 = 0;
1178 } else /* eDP */
1179 return true;
1180
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001181 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1182 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1183 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001184
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001185 pll = intel_get_shared_dpll(intel_crtc, crtc_state);
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001186 if (pll == NULL) {
1187 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
1188 pipe_name(intel_crtc->pipe));
1189 return false;
1190 }
1191
1192 /* shared DPLL id 0 is DPLL 1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001193 crtc_state->ddi_pll_sel = pll->id + 1;
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001194
1195 return true;
1196}
Damien Lespiau0220ab62014-07-29 18:06:22 +01001197
1198/*
1199 * Tries to find a *shared* PLL for the CRTC and store it in
1200 * intel_crtc->ddi_pll_sel.
1201 *
1202 * For private DPLLs, compute_config() should do the selection for us. This
1203 * function should be folded into compute_config() eventually.
1204 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001205bool intel_ddi_pll_select(struct intel_crtc *intel_crtc,
1206 struct intel_crtc_state *crtc_state)
Damien Lespiau0220ab62014-07-29 18:06:22 +01001207{
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001208 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02001209 struct intel_encoder *intel_encoder =
1210 intel_ddi_get_crtc_new_encoder(intel_crtc);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001211 int clock = crtc_state->port_clock;
Damien Lespiau0220ab62014-07-29 18:06:22 +01001212
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001213 if (IS_SKYLAKE(dev))
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001214 return skl_ddi_pll_select(intel_crtc, crtc_state,
1215 intel_encoder, clock);
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001216 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001217 return hsw_ddi_pll_select(intel_crtc, crtc_state,
1218 intel_encoder, clock);
Damien Lespiau0220ab62014-07-29 18:06:22 +01001219}
1220
Paulo Zanonidae84792012-10-15 15:51:30 -03001221void intel_ddi_set_pipe_settings(struct drm_crtc *crtc)
1222{
1223 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1225 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001226 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonidae84792012-10-15 15:51:30 -03001227 int type = intel_encoder->type;
1228 uint32_t temp;
1229
Dave Airlie0e32b392014-05-02 14:02:48 +10001230 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP || type == INTEL_OUTPUT_DP_MST) {
Paulo Zanonic9809792012-10-23 18:30:00 -02001231 temp = TRANS_MSA_SYNC_CLK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001232 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonidae84792012-10-15 15:51:30 -03001233 case 18:
Paulo Zanonic9809792012-10-23 18:30:00 -02001234 temp |= TRANS_MSA_6_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001235 break;
1236 case 24:
Paulo Zanonic9809792012-10-23 18:30:00 -02001237 temp |= TRANS_MSA_8_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001238 break;
1239 case 30:
Paulo Zanonic9809792012-10-23 18:30:00 -02001240 temp |= TRANS_MSA_10_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001241 break;
1242 case 36:
Paulo Zanonic9809792012-10-23 18:30:00 -02001243 temp |= TRANS_MSA_12_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001244 break;
1245 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01001246 BUG();
Paulo Zanonidae84792012-10-15 15:51:30 -03001247 }
Paulo Zanonic9809792012-10-23 18:30:00 -02001248 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
Paulo Zanonidae84792012-10-15 15:51:30 -03001249 }
1250}
1251
Dave Airlie0e32b392014-05-02 14:02:48 +10001252void intel_ddi_set_vc_payload_alloc(struct drm_crtc *crtc, bool state)
1253{
1254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1255 struct drm_device *dev = crtc->dev;
1256 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001257 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Dave Airlie0e32b392014-05-02 14:02:48 +10001258 uint32_t temp;
1259 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1260 if (state == true)
1261 temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1262 else
1263 temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1264 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1265}
1266
Damien Lespiau8228c252013-03-07 15:30:27 +00001267void intel_ddi_enable_transcoder_func(struct drm_crtc *crtc)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001268{
1269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1270 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Paulo Zanoni7739c332012-10-15 15:51:29 -03001271 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanonic7670b12013-11-02 21:07:37 -07001272 struct drm_device *dev = crtc->dev;
1273 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001274 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001275 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001276 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni7739c332012-10-15 15:51:29 -03001277 int type = intel_encoder->type;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001278 uint32_t temp;
1279
Paulo Zanoniad80a812012-10-24 16:06:19 -02001280 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1281 temp = TRANS_DDI_FUNC_ENABLE;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001282 temp |= TRANS_DDI_SELECT_PORT(port);
Paulo Zanonidfcef252012-08-08 14:15:29 -03001283
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001284 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonidfcef252012-08-08 14:15:29 -03001285 case 18:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001286 temp |= TRANS_DDI_BPC_6;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001287 break;
1288 case 24:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001289 temp |= TRANS_DDI_BPC_8;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001290 break;
1291 case 30:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001292 temp |= TRANS_DDI_BPC_10;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001293 break;
1294 case 36:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001295 temp |= TRANS_DDI_BPC_12;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001296 break;
1297 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01001298 BUG();
Paulo Zanonidfcef252012-08-08 14:15:29 -03001299 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001300
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001301 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001302 temp |= TRANS_DDI_PVSYNC;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001303 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001304 temp |= TRANS_DDI_PHSYNC;
Paulo Zanonif63eb7c42012-08-08 14:15:28 -03001305
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -02001306 if (cpu_transcoder == TRANSCODER_EDP) {
1307 switch (pipe) {
1308 case PIPE_A:
Paulo Zanonic7670b12013-11-02 21:07:37 -07001309 /* On Haswell, can only use the always-on power well for
1310 * eDP when not using the panel fitter, and when not
1311 * using motion blur mitigation (which we don't
1312 * support). */
Daniel Vetterfabf6e52014-05-29 14:10:22 +02001313 if (IS_HASWELL(dev) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001314 (intel_crtc->config->pch_pfit.enabled ||
1315 intel_crtc->config->pch_pfit.force_thru))
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02001316 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1317 else
1318 temp |= TRANS_DDI_EDP_INPUT_A_ON;
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -02001319 break;
1320 case PIPE_B:
1321 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1322 break;
1323 case PIPE_C:
1324 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1325 break;
1326 default:
1327 BUG();
1328 break;
1329 }
1330 }
1331
Paulo Zanoni7739c332012-10-15 15:51:29 -03001332 if (type == INTEL_OUTPUT_HDMI) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001333 if (intel_crtc->config->has_hdmi_sink)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001334 temp |= TRANS_DDI_MODE_SELECT_HDMI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001335 else
Paulo Zanoniad80a812012-10-24 16:06:19 -02001336 temp |= TRANS_DDI_MODE_SELECT_DVI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001337
Paulo Zanoni7739c332012-10-15 15:51:29 -03001338 } else if (type == INTEL_OUTPUT_ANALOG) {
Paulo Zanoniad80a812012-10-24 16:06:19 -02001339 temp |= TRANS_DDI_MODE_SELECT_FDI;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001340 temp |= (intel_crtc->config->fdi_lanes - 1) << 1;
Paulo Zanoni7739c332012-10-15 15:51:29 -03001341
1342 } else if (type == INTEL_OUTPUT_DISPLAYPORT ||
1343 type == INTEL_OUTPUT_EDP) {
1344 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1345
Dave Airlie0e32b392014-05-02 14:02:48 +10001346 if (intel_dp->is_mst) {
1347 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1348 } else
1349 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1350
1351 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
1352 } else if (type == INTEL_OUTPUT_DP_MST) {
1353 struct intel_dp *intel_dp = &enc_to_mst(encoder)->primary->dp;
1354
1355 if (intel_dp->is_mst) {
1356 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1357 } else
1358 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
Paulo Zanoni7739c332012-10-15 15:51:29 -03001359
Daniel Vetter17aa6be2013-04-30 14:01:40 +02001360 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001361 } else {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001362 WARN(1, "Invalid encoder type %d for pipe %c\n",
1363 intel_encoder->type, pipe_name(pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001364 }
1365
Paulo Zanoniad80a812012-10-24 16:06:19 -02001366 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001367}
1368
Paulo Zanoniad80a812012-10-24 16:06:19 -02001369void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
1370 enum transcoder cpu_transcoder)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001371{
Paulo Zanoniad80a812012-10-24 16:06:19 -02001372 uint32_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001373 uint32_t val = I915_READ(reg);
1374
Dave Airlie0e32b392014-05-02 14:02:48 +10001375 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK | TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001376 val |= TRANS_DDI_PORT_NONE;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001377 I915_WRITE(reg, val);
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001378}
1379
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001380bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1381{
1382 struct drm_device *dev = intel_connector->base.dev;
1383 struct drm_i915_private *dev_priv = dev->dev_private;
1384 struct intel_encoder *intel_encoder = intel_connector->encoder;
1385 int type = intel_connector->base.connector_type;
1386 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1387 enum pipe pipe = 0;
1388 enum transcoder cpu_transcoder;
Paulo Zanoni882244a2014-04-01 14:55:12 -03001389 enum intel_display_power_domain power_domain;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001390 uint32_t tmp;
1391
Paulo Zanoni882244a2014-04-01 14:55:12 -03001392 power_domain = intel_display_port_power_domain(intel_encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001393 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Paulo Zanoni882244a2014-04-01 14:55:12 -03001394 return false;
1395
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001396 if (!intel_encoder->get_hw_state(intel_encoder, &pipe))
1397 return false;
1398
1399 if (port == PORT_A)
1400 cpu_transcoder = TRANSCODER_EDP;
1401 else
Daniel Vetter1a240d42012-11-29 22:18:51 +01001402 cpu_transcoder = (enum transcoder) pipe;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001403
1404 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1405
1406 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1407 case TRANS_DDI_MODE_SELECT_HDMI:
1408 case TRANS_DDI_MODE_SELECT_DVI:
1409 return (type == DRM_MODE_CONNECTOR_HDMIA);
1410
1411 case TRANS_DDI_MODE_SELECT_DP_SST:
1412 if (type == DRM_MODE_CONNECTOR_eDP)
1413 return true;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001414 return (type == DRM_MODE_CONNECTOR_DisplayPort);
Dave Airlie0e32b392014-05-02 14:02:48 +10001415 case TRANS_DDI_MODE_SELECT_DP_MST:
1416 /* if the transcoder is in MST state then
1417 * connector isn't connected */
1418 return false;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001419
1420 case TRANS_DDI_MODE_SELECT_FDI:
1421 return (type == DRM_MODE_CONNECTOR_VGA);
1422
1423 default:
1424 return false;
1425 }
1426}
1427
Daniel Vetter85234cd2012-07-02 13:27:29 +02001428bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1429 enum pipe *pipe)
1430{
1431 struct drm_device *dev = encoder->base.dev;
1432 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonife43d3f2012-10-15 15:51:39 -03001433 enum port port = intel_ddi_get_encoder_port(encoder);
Imre Deak6d129be2014-03-05 16:20:54 +02001434 enum intel_display_power_domain power_domain;
Daniel Vetter85234cd2012-07-02 13:27:29 +02001435 u32 tmp;
1436 int i;
1437
Imre Deak6d129be2014-03-05 16:20:54 +02001438 power_domain = intel_display_port_power_domain(encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001439 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +02001440 return false;
1441
Paulo Zanonife43d3f2012-10-15 15:51:39 -03001442 tmp = I915_READ(DDI_BUF_CTL(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001443
1444 if (!(tmp & DDI_BUF_CTL_ENABLE))
1445 return false;
1446
Paulo Zanoniad80a812012-10-24 16:06:19 -02001447 if (port == PORT_A) {
1448 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001449
Paulo Zanoniad80a812012-10-24 16:06:19 -02001450 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1451 case TRANS_DDI_EDP_INPUT_A_ON:
1452 case TRANS_DDI_EDP_INPUT_A_ONOFF:
1453 *pipe = PIPE_A;
1454 break;
1455 case TRANS_DDI_EDP_INPUT_B_ONOFF:
1456 *pipe = PIPE_B;
1457 break;
1458 case TRANS_DDI_EDP_INPUT_C_ONOFF:
1459 *pipe = PIPE_C;
1460 break;
1461 }
1462
1463 return true;
1464 } else {
1465 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
1466 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
1467
1468 if ((tmp & TRANS_DDI_PORT_MASK)
1469 == TRANS_DDI_SELECT_PORT(port)) {
Dave Airlie0e32b392014-05-02 14:02:48 +10001470 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST)
1471 return false;
1472
Paulo Zanoniad80a812012-10-24 16:06:19 -02001473 *pipe = i;
1474 return true;
1475 }
Daniel Vetter85234cd2012-07-02 13:27:29 +02001476 }
1477 }
1478
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001479 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001480
Jesse Barnes22f9fe52013-04-02 10:03:55 -07001481 return false;
Daniel Vetter85234cd2012-07-02 13:27:29 +02001482}
1483
Paulo Zanonifc914632012-10-05 12:05:54 -03001484void intel_ddi_enable_pipe_clock(struct intel_crtc *intel_crtc)
1485{
1486 struct drm_crtc *crtc = &intel_crtc->base;
1487 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1488 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
1489 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001490 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -03001491
Paulo Zanonibb523fc2012-10-23 18:29:56 -02001492 if (cpu_transcoder != TRANSCODER_EDP)
1493 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1494 TRANS_CLK_SEL_PORT(port));
Paulo Zanonifc914632012-10-05 12:05:54 -03001495}
1496
1497void intel_ddi_disable_pipe_clock(struct intel_crtc *intel_crtc)
1498{
1499 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001500 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -03001501
Paulo Zanonibb523fc2012-10-23 18:29:56 -02001502 if (cpu_transcoder != TRANSCODER_EDP)
1503 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1504 TRANS_CLK_SEL_DISABLED);
Paulo Zanonifc914632012-10-05 12:05:54 -03001505}
1506
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001507static void intel_ddi_pre_enable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001508{
Paulo Zanonic19b0662012-10-15 15:51:41 -03001509 struct drm_encoder *encoder = &intel_encoder->base;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001510 struct drm_device *dev = encoder->dev;
1511 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001512 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001513 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001514 int type = intel_encoder->type;
1515
1516 if (type == INTEL_OUTPUT_EDP) {
1517 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Daniel Vetter4be73782014-01-17 14:39:48 +01001518 intel_edp_panel_on(intel_dp);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001519 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001520
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001521 if (IS_SKYLAKE(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001522 uint32_t dpll = crtc->config->ddi_pll_sel;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001523 uint32_t val;
1524
Damien Lespiau5416d872014-11-14 17:24:33 +00001525 /*
1526 * DPLL0 is used for eDP and is the only "private" DPLL (as
1527 * opposed to shared) on SKL
1528 */
1529 if (type == INTEL_OUTPUT_EDP) {
1530 WARN_ON(dpll != SKL_DPLL0);
1531
1532 val = I915_READ(DPLL_CTRL1);
1533
1534 val &= ~(DPLL_CTRL1_HDMI_MODE(dpll) |
1535 DPLL_CTRL1_SSC(dpll) |
1536 DPLL_CRTL1_LINK_RATE_MASK(dpll));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001537 val |= crtc->config->dpll_hw_state.ctrl1 << (dpll * 6);
Damien Lespiau5416d872014-11-14 17:24:33 +00001538
1539 I915_WRITE(DPLL_CTRL1, val);
1540 POSTING_READ(DPLL_CTRL1);
1541 }
1542
1543 /* DDI -> PLL mapping */
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001544 val = I915_READ(DPLL_CTRL2);
1545
1546 val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
1547 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
1548 val |= (DPLL_CTRL2_DDI_CLK_SEL(dpll, port) |
1549 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
1550
1551 I915_WRITE(DPLL_CTRL2, val);
Damien Lespiau5416d872014-11-14 17:24:33 +00001552
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001553 } else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001554 WARN_ON(crtc->config->ddi_pll_sel == PORT_CLK_SEL_NONE);
1555 I915_WRITE(PORT_CLK_SEL(port), crtc->config->ddi_pll_sel);
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001556 }
Paulo Zanonic19b0662012-10-15 15:51:41 -03001557
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001558 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Paulo Zanonic19b0662012-10-15 15:51:41 -03001559 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001560
Dave Airlie44905a272014-05-02 13:36:43 +10001561 intel_ddi_init_dp_buf_reg(intel_encoder);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001562
1563 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1564 intel_dp_start_link_train(intel_dp);
1565 intel_dp_complete_link_train(intel_dp);
Vandana Kannan23f08d82014-11-13 14:55:22 +00001566 if (port != PORT_A || INTEL_INFO(dev)->gen >= 9)
Imre Deak3ab9c632013-05-03 12:57:41 +03001567 intel_dp_stop_link_train(intel_dp);
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001568 } else if (type == INTEL_OUTPUT_HDMI) {
1569 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
1570
1571 intel_hdmi->set_infoframes(encoder,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001572 crtc->config->has_hdmi_sink,
1573 &crtc->config->base.adjusted_mode);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001574 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001575}
1576
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001577static void intel_ddi_post_disable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001578{
1579 struct drm_encoder *encoder = &intel_encoder->base;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001580 struct drm_device *dev = encoder->dev;
1581 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001582 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001583 int type = intel_encoder->type;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001584 uint32_t val;
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001585 bool wait = false;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001586
1587 val = I915_READ(DDI_BUF_CTL(port));
1588 if (val & DDI_BUF_CTL_ENABLE) {
1589 val &= ~DDI_BUF_CTL_ENABLE;
1590 I915_WRITE(DDI_BUF_CTL(port), val);
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001591 wait = true;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001592 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001593
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001594 val = I915_READ(DP_TP_CTL(port));
1595 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1596 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1597 I915_WRITE(DP_TP_CTL(port), val);
1598
1599 if (wait)
1600 intel_wait_ddi_buf_idle(dev_priv, port);
1601
Jani Nikula76bb80e2013-11-15 15:29:57 +02001602 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001603 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jani Nikula76bb80e2013-11-15 15:29:57 +02001604 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
Jani Nikula24f3e092014-03-17 16:43:36 +02001605 intel_edp_panel_vdd_on(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01001606 intel_edp_panel_off(intel_dp);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001607 }
1608
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001609 if (IS_SKYLAKE(dev))
1610 I915_WRITE(DPLL_CTRL2, (I915_READ(DPLL_CTRL2) |
1611 DPLL_CTRL2_DDI_CLK_OFF(port)));
1612 else
1613 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001614}
1615
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001616static void intel_enable_ddi(struct intel_encoder *intel_encoder)
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001617{
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001618 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001619 struct drm_crtc *crtc = encoder->crtc;
1620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001621 struct drm_device *dev = encoder->dev;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001622 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001623 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1624 int type = intel_encoder->type;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001625
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001626 if (type == INTEL_OUTPUT_HDMI) {
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001627 struct intel_digital_port *intel_dig_port =
1628 enc_to_dig_port(encoder);
1629
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001630 /* In HDMI/DVI mode, the port width, and swing/emphasis values
1631 * are ignored so nothing special needs to be done besides
1632 * enabling the port.
1633 */
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001634 I915_WRITE(DDI_BUF_CTL(port),
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07001635 intel_dig_port->saved_port_bits |
1636 DDI_BUF_CTL_ENABLE);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001637 } else if (type == INTEL_OUTPUT_EDP) {
1638 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1639
Vandana Kannan23f08d82014-11-13 14:55:22 +00001640 if (port == PORT_A && INTEL_INFO(dev)->gen < 9)
Imre Deak3ab9c632013-05-03 12:57:41 +03001641 intel_dp_stop_link_train(intel_dp);
1642
Daniel Vetter4be73782014-01-17 14:39:48 +01001643 intel_edp_backlight_on(intel_dp);
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -08001644 intel_psr_enable(intel_dp);
Vandana Kannanc3955782015-01-22 15:17:40 +05301645 intel_edp_drrs_enable(intel_dp);
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001646 }
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001647
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001648 if (intel_crtc->config->has_audio) {
Paulo Zanonid45a0bf2014-05-21 17:29:31 -03001649 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Jani Nikula69bfe1a2014-10-27 16:26:50 +02001650 intel_audio_codec_enable(intel_encoder);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001651 }
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001652}
1653
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001654static void intel_disable_ddi(struct intel_encoder *intel_encoder)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001655{
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001656 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001657 struct drm_crtc *crtc = encoder->crtc;
1658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001659 int type = intel_encoder->type;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001660 struct drm_device *dev = encoder->dev;
1661 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001662
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001663 if (intel_crtc->config->has_audio) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +02001664 intel_audio_codec_disable(intel_encoder);
Paulo Zanonid45a0bf2014-05-21 17:29:31 -03001665 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
1666 }
Paulo Zanoni2831d8422013-03-06 20:03:09 -03001667
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001668 if (type == INTEL_OUTPUT_EDP) {
1669 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1670
Vandana Kannanc3955782015-01-22 15:17:40 +05301671 intel_edp_drrs_disable(intel_dp);
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -08001672 intel_psr_disable(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01001673 intel_edp_backlight_off(intel_dp);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001674 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001675}
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001676
Satheeshakrishna M121643c2014-11-13 14:55:15 +00001677static int skl_get_cdclk_freq(struct drm_i915_private *dev_priv)
1678{
1679 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
1680 uint32_t cdctl = I915_READ(CDCLK_CTL);
1681 uint32_t linkrate;
1682
1683 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
1684 WARN(1, "LCPLL1 not enabled\n");
1685 return 24000; /* 24MHz is the cd freq with NSSC ref */
1686 }
1687
1688 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
1689 return 540000;
1690
1691 linkrate = (I915_READ(DPLL_CTRL1) &
1692 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1693
1694 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
1695 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
1696 /* vco 8640 */
1697 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
1698 case CDCLK_FREQ_450_432:
1699 return 432000;
1700 case CDCLK_FREQ_337_308:
1701 return 308570;
1702 case CDCLK_FREQ_675_617:
1703 return 617140;
1704 default:
1705 WARN(1, "Unknown cd freq selection\n");
1706 }
1707 } else {
1708 /* vco 8100 */
1709 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
1710 case CDCLK_FREQ_450_432:
1711 return 450000;
1712 case CDCLK_FREQ_337_308:
1713 return 337500;
1714 case CDCLK_FREQ_675_617:
1715 return 675000;
1716 default:
1717 WARN(1, "Unknown cd freq selection\n");
1718 }
1719 }
1720
1721 /* error case, do as if DPLL0 isn't enabled */
1722 return 24000;
1723}
1724
Damien Lespiauad13d602014-07-29 18:06:24 +01001725static int bdw_get_cdclk_freq(struct drm_i915_private *dev_priv)
1726{
1727 uint32_t lcpll = I915_READ(LCPLL_CTL);
1728 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
1729
1730 if (lcpll & LCPLL_CD_SOURCE_FCLK)
1731 return 800000;
1732 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
1733 return 450000;
1734 else if (freq == LCPLL_CLK_FREQ_450)
1735 return 450000;
1736 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
1737 return 540000;
1738 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
1739 return 337500;
1740 else
1741 return 675000;
1742}
1743
1744static int hsw_get_cdclk_freq(struct drm_i915_private *dev_priv)
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001745{
Paulo Zanonie39bf982013-11-02 21:07:36 -07001746 struct drm_device *dev = dev_priv->dev;
Paulo Zanonia4006642013-08-06 18:57:11 -03001747 uint32_t lcpll = I915_READ(LCPLL_CTL);
Paulo Zanonie39bf982013-11-02 21:07:36 -07001748 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
Paulo Zanonia4006642013-08-06 18:57:11 -03001749
Damien Lespiauad13d602014-07-29 18:06:24 +01001750 if (lcpll & LCPLL_CD_SOURCE_FCLK)
Paulo Zanonia4006642013-08-06 18:57:11 -03001751 return 800000;
Damien Lespiauad13d602014-07-29 18:06:24 +01001752 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001753 return 450000;
Damien Lespiauad13d602014-07-29 18:06:24 +01001754 else if (freq == LCPLL_CLK_FREQ_450)
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001755 return 450000;
Damien Lespiau95626e72014-10-01 20:04:16 +01001756 else if (IS_HSW_ULT(dev))
Damien Lespiauad13d602014-07-29 18:06:24 +01001757 return 337500;
1758 else
1759 return 540000;
1760}
1761
1762int intel_ddi_get_cdclk_freq(struct drm_i915_private *dev_priv)
1763{
1764 struct drm_device *dev = dev_priv->dev;
1765
Satheeshakrishna M121643c2014-11-13 14:55:15 +00001766 if (IS_SKYLAKE(dev))
1767 return skl_get_cdclk_freq(dev_priv);
1768
Damien Lespiauad13d602014-07-29 18:06:24 +01001769 if (IS_BROADWELL(dev))
1770 return bdw_get_cdclk_freq(dev_priv);
1771
1772 /* Haswell */
1773 return hsw_get_cdclk_freq(dev_priv);
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001774}
1775
Daniel Vettere0b01be2014-06-25 22:02:01 +03001776static void hsw_ddi_pll_enable(struct drm_i915_private *dev_priv,
1777 struct intel_shared_dpll *pll)
1778{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001779 I915_WRITE(WRPLL_CTL(pll->id), pll->config.hw_state.wrpll);
Daniel Vettere0b01be2014-06-25 22:02:01 +03001780 POSTING_READ(WRPLL_CTL(pll->id));
1781 udelay(20);
1782}
1783
Daniel Vetter12030432014-06-25 22:02:00 +03001784static void hsw_ddi_pll_disable(struct drm_i915_private *dev_priv,
1785 struct intel_shared_dpll *pll)
1786{
1787 uint32_t val;
1788
1789 val = I915_READ(WRPLL_CTL(pll->id));
Daniel Vetter12030432014-06-25 22:02:00 +03001790 I915_WRITE(WRPLL_CTL(pll->id), val & ~WRPLL_PLL_ENABLE);
1791 POSTING_READ(WRPLL_CTL(pll->id));
1792}
1793
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001794static bool hsw_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1795 struct intel_shared_dpll *pll,
1796 struct intel_dpll_hw_state *hw_state)
1797{
1798 uint32_t val;
1799
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001800 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001801 return false;
1802
1803 val = I915_READ(WRPLL_CTL(pll->id));
1804 hw_state->wrpll = val;
1805
1806 return val & WRPLL_PLL_ENABLE;
1807}
1808
Damien Lespiauca1381b2014-07-15 15:05:33 +01001809static const char * const hsw_ddi_pll_names[] = {
Daniel Vetter9cd86932014-06-25 22:01:57 +03001810 "WRPLL 1",
1811 "WRPLL 2",
1812};
1813
Damien Lespiau143b3072014-07-29 18:06:19 +01001814static void hsw_shared_dplls_init(struct drm_i915_private *dev_priv)
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001815{
Daniel Vetter9cd86932014-06-25 22:01:57 +03001816 int i;
1817
Daniel Vetter716c2e52014-06-25 22:02:02 +03001818 dev_priv->num_shared_dpll = 2;
Daniel Vetter9cd86932014-06-25 22:01:57 +03001819
Daniel Vetter716c2e52014-06-25 22:02:02 +03001820 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter9cd86932014-06-25 22:01:57 +03001821 dev_priv->shared_dplls[i].id = i;
1822 dev_priv->shared_dplls[i].name = hsw_ddi_pll_names[i];
Daniel Vetter12030432014-06-25 22:02:00 +03001823 dev_priv->shared_dplls[i].disable = hsw_ddi_pll_disable;
Daniel Vettere0b01be2014-06-25 22:02:01 +03001824 dev_priv->shared_dplls[i].enable = hsw_ddi_pll_enable;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001825 dev_priv->shared_dplls[i].get_hw_state =
1826 hsw_ddi_pll_get_hw_state;
Daniel Vetter9cd86932014-06-25 22:01:57 +03001827 }
Damien Lespiau143b3072014-07-29 18:06:19 +01001828}
1829
Satheeshakrishna Md1a2dc72014-11-13 14:55:18 +00001830static const char * const skl_ddi_pll_names[] = {
1831 "DPLL 1",
1832 "DPLL 2",
1833 "DPLL 3",
1834};
1835
1836struct skl_dpll_regs {
1837 u32 ctl, cfgcr1, cfgcr2;
1838};
1839
1840/* this array is indexed by the *shared* pll id */
1841static const struct skl_dpll_regs skl_dpll_regs[3] = {
1842 {
1843 /* DPLL 1 */
1844 .ctl = LCPLL2_CTL,
1845 .cfgcr1 = DPLL1_CFGCR1,
1846 .cfgcr2 = DPLL1_CFGCR2,
1847 },
1848 {
1849 /* DPLL 2 */
1850 .ctl = WRPLL_CTL1,
1851 .cfgcr1 = DPLL2_CFGCR1,
1852 .cfgcr2 = DPLL2_CFGCR2,
1853 },
1854 {
1855 /* DPLL 3 */
1856 .ctl = WRPLL_CTL2,
1857 .cfgcr1 = DPLL3_CFGCR1,
1858 .cfgcr2 = DPLL3_CFGCR2,
1859 },
1860};
1861
1862static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
1863 struct intel_shared_dpll *pll)
1864{
1865 uint32_t val;
1866 unsigned int dpll;
1867 const struct skl_dpll_regs *regs = skl_dpll_regs;
1868
1869 /* DPLL0 is not part of the shared DPLLs, so pll->id is 0 for DPLL1 */
1870 dpll = pll->id + 1;
1871
1872 val = I915_READ(DPLL_CTRL1);
1873
1874 val &= ~(DPLL_CTRL1_HDMI_MODE(dpll) | DPLL_CTRL1_SSC(dpll) |
1875 DPLL_CRTL1_LINK_RATE_MASK(dpll));
1876 val |= pll->config.hw_state.ctrl1 << (dpll * 6);
1877
1878 I915_WRITE(DPLL_CTRL1, val);
1879 POSTING_READ(DPLL_CTRL1);
1880
1881 I915_WRITE(regs[pll->id].cfgcr1, pll->config.hw_state.cfgcr1);
1882 I915_WRITE(regs[pll->id].cfgcr2, pll->config.hw_state.cfgcr2);
1883 POSTING_READ(regs[pll->id].cfgcr1);
1884 POSTING_READ(regs[pll->id].cfgcr2);
1885
1886 /* the enable bit is always bit 31 */
1887 I915_WRITE(regs[pll->id].ctl,
1888 I915_READ(regs[pll->id].ctl) | LCPLL_PLL_ENABLE);
1889
1890 if (wait_for(I915_READ(DPLL_STATUS) & DPLL_LOCK(dpll), 5))
1891 DRM_ERROR("DPLL %d not locked\n", dpll);
1892}
1893
1894static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
1895 struct intel_shared_dpll *pll)
1896{
1897 const struct skl_dpll_regs *regs = skl_dpll_regs;
1898
1899 /* the enable bit is always bit 31 */
1900 I915_WRITE(regs[pll->id].ctl,
1901 I915_READ(regs[pll->id].ctl) & ~LCPLL_PLL_ENABLE);
1902 POSTING_READ(regs[pll->id].ctl);
1903}
1904
1905static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1906 struct intel_shared_dpll *pll,
1907 struct intel_dpll_hw_state *hw_state)
1908{
1909 uint32_t val;
1910 unsigned int dpll;
1911 const struct skl_dpll_regs *regs = skl_dpll_regs;
1912
1913 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
1914 return false;
1915
1916 /* DPLL0 is not part of the shared DPLLs, so pll->id is 0 for DPLL1 */
1917 dpll = pll->id + 1;
1918
1919 val = I915_READ(regs[pll->id].ctl);
1920 if (!(val & LCPLL_PLL_ENABLE))
1921 return false;
1922
1923 val = I915_READ(DPLL_CTRL1);
1924 hw_state->ctrl1 = (val >> (dpll * 6)) & 0x3f;
1925
1926 /* avoid reading back stale values if HDMI mode is not enabled */
1927 if (val & DPLL_CTRL1_HDMI_MODE(dpll)) {
1928 hw_state->cfgcr1 = I915_READ(regs[pll->id].cfgcr1);
1929 hw_state->cfgcr2 = I915_READ(regs[pll->id].cfgcr2);
1930 }
1931
1932 return true;
1933}
1934
1935static void skl_shared_dplls_init(struct drm_i915_private *dev_priv)
1936{
1937 int i;
1938
1939 dev_priv->num_shared_dpll = 3;
1940
1941 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
1942 dev_priv->shared_dplls[i].id = i;
1943 dev_priv->shared_dplls[i].name = skl_ddi_pll_names[i];
1944 dev_priv->shared_dplls[i].disable = skl_ddi_pll_disable;
1945 dev_priv->shared_dplls[i].enable = skl_ddi_pll_enable;
1946 dev_priv->shared_dplls[i].get_hw_state =
1947 skl_ddi_pll_get_hw_state;
1948 }
1949}
1950
Damien Lespiau143b3072014-07-29 18:06:19 +01001951void intel_ddi_pll_init(struct drm_device *dev)
1952{
1953 struct drm_i915_private *dev_priv = dev->dev_private;
1954 uint32_t val = I915_READ(LCPLL_CTL);
1955
Satheeshakrishna Md1a2dc72014-11-13 14:55:18 +00001956 if (IS_SKYLAKE(dev))
1957 skl_shared_dplls_init(dev_priv);
1958 else
1959 hsw_shared_dplls_init(dev_priv);
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001960
Paulo Zanonib2b877f2013-05-03 17:23:42 -03001961 DRM_DEBUG_KMS("CDCLK running at %dKHz\n",
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001962 intel_ddi_get_cdclk_freq(dev_priv));
1963
Satheeshakrishna M121643c2014-11-13 14:55:15 +00001964 if (IS_SKYLAKE(dev)) {
1965 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE))
1966 DRM_ERROR("LCPLL1 is disabled\n");
1967 } else {
1968 /*
1969 * The LCPLL register should be turned on by the BIOS. For now
1970 * let's just check its state and print errors in case
1971 * something is wrong. Don't even try to turn it on.
1972 */
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001973
Satheeshakrishna M121643c2014-11-13 14:55:15 +00001974 if (val & LCPLL_CD_SOURCE_FCLK)
1975 DRM_ERROR("CDCLK source is not LCPLL\n");
1976
1977 if (val & LCPLL_PLL_DISABLE)
1978 DRM_ERROR("LCPLL is disabled\n");
1979 }
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001980}
Paulo Zanonic19b0662012-10-15 15:51:41 -03001981
1982void intel_ddi_prepare_link_retrain(struct drm_encoder *encoder)
1983{
Paulo Zanoni174edf12012-10-26 19:05:50 -02001984 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
1985 struct intel_dp *intel_dp = &intel_dig_port->dp;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001986 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001987 enum port port = intel_dig_port->port;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001988 uint32_t val;
Syam Sidhardhanf3e227d2013-02-25 04:05:38 +05301989 bool wait = false;
Paulo Zanonic19b0662012-10-15 15:51:41 -03001990
1991 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
1992 val = I915_READ(DDI_BUF_CTL(port));
1993 if (val & DDI_BUF_CTL_ENABLE) {
1994 val &= ~DDI_BUF_CTL_ENABLE;
1995 I915_WRITE(DDI_BUF_CTL(port), val);
1996 wait = true;
1997 }
1998
1999 val = I915_READ(DP_TP_CTL(port));
2000 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2001 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2002 I915_WRITE(DP_TP_CTL(port), val);
2003 POSTING_READ(DP_TP_CTL(port));
2004
2005 if (wait)
2006 intel_wait_ddi_buf_idle(dev_priv, port);
2007 }
2008
Dave Airlie0e32b392014-05-02 14:02:48 +10002009 val = DP_TP_CTL_ENABLE |
Paulo Zanonic19b0662012-10-15 15:51:41 -03002010 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
Dave Airlie0e32b392014-05-02 14:02:48 +10002011 if (intel_dp->is_mst)
2012 val |= DP_TP_CTL_MODE_MST;
2013 else {
2014 val |= DP_TP_CTL_MODE_SST;
2015 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2016 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
2017 }
Paulo Zanonic19b0662012-10-15 15:51:41 -03002018 I915_WRITE(DP_TP_CTL(port), val);
2019 POSTING_READ(DP_TP_CTL(port));
2020
2021 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
2022 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
2023 POSTING_READ(DDI_BUF_CTL(port));
2024
2025 udelay(600);
2026}
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002027
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02002028void intel_ddi_fdi_disable(struct drm_crtc *crtc)
2029{
2030 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
2031 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
2032 uint32_t val;
2033
2034 intel_ddi_post_disable(intel_encoder);
2035
2036 val = I915_READ(_FDI_RXA_CTL);
2037 val &= ~FDI_RX_ENABLE;
2038 I915_WRITE(_FDI_RXA_CTL, val);
2039
2040 val = I915_READ(_FDI_RXA_MISC);
2041 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
2042 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
2043 I915_WRITE(_FDI_RXA_MISC, val);
2044
2045 val = I915_READ(_FDI_RXA_CTL);
2046 val &= ~FDI_PCDCLK;
2047 I915_WRITE(_FDI_RXA_CTL, val);
2048
2049 val = I915_READ(_FDI_RXA_CTL);
2050 val &= ~FDI_RX_PLL_ENABLE;
2051 I915_WRITE(_FDI_RXA_CTL, val);
2052}
2053
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002054static void intel_ddi_hot_plug(struct intel_encoder *intel_encoder)
2055{
Dave Airlie0e32b392014-05-02 14:02:48 +10002056 struct intel_digital_port *intel_dig_port = enc_to_dig_port(&intel_encoder->base);
2057 int type = intel_dig_port->base.type;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002058
Dave Airlie0e32b392014-05-02 14:02:48 +10002059 if (type != INTEL_OUTPUT_DISPLAYPORT &&
2060 type != INTEL_OUTPUT_EDP &&
2061 type != INTEL_OUTPUT_UNKNOWN) {
2062 return;
2063 }
2064
2065 intel_dp_hot_plug(intel_encoder);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002066}
2067
Ville Syrjälä6801c182013-09-24 14:24:05 +03002068void intel_ddi_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002069 struct intel_crtc_state *pipe_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002070{
2071 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
2072 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Ander Conselvan de Oliveira0cb09a92015-01-30 12:17:23 +02002073 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Daniel Vetterbbd440f2014-11-20 22:33:59 +01002074 struct intel_hdmi *intel_hdmi;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002075 u32 temp, flags = 0;
2076
2077 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
2078 if (temp & TRANS_DDI_PHSYNC)
2079 flags |= DRM_MODE_FLAG_PHSYNC;
2080 else
2081 flags |= DRM_MODE_FLAG_NHSYNC;
2082 if (temp & TRANS_DDI_PVSYNC)
2083 flags |= DRM_MODE_FLAG_PVSYNC;
2084 else
2085 flags |= DRM_MODE_FLAG_NVSYNC;
2086
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002087 pipe_config->base.adjusted_mode.flags |= flags;
Ville Syrjälä42571ae2013-09-06 23:29:00 +03002088
2089 switch (temp & TRANS_DDI_BPC_MASK) {
2090 case TRANS_DDI_BPC_6:
2091 pipe_config->pipe_bpp = 18;
2092 break;
2093 case TRANS_DDI_BPC_8:
2094 pipe_config->pipe_bpp = 24;
2095 break;
2096 case TRANS_DDI_BPC_10:
2097 pipe_config->pipe_bpp = 30;
2098 break;
2099 case TRANS_DDI_BPC_12:
2100 pipe_config->pipe_bpp = 36;
2101 break;
2102 default:
2103 break;
2104 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002105
2106 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
2107 case TRANS_DDI_MODE_SELECT_HDMI:
Daniel Vetter6897b4b52014-04-24 23:54:47 +02002108 pipe_config->has_hdmi_sink = true;
Daniel Vetterbbd440f2014-11-20 22:33:59 +01002109 intel_hdmi = enc_to_intel_hdmi(&encoder->base);
2110
2111 if (intel_hdmi->infoframe_enabled(&encoder->base))
2112 pipe_config->has_infoframe = true;
Jesse Barnescbc572a2014-11-17 13:08:47 -08002113 break;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002114 case TRANS_DDI_MODE_SELECT_DVI:
2115 case TRANS_DDI_MODE_SELECT_FDI:
2116 break;
2117 case TRANS_DDI_MODE_SELECT_DP_SST:
2118 case TRANS_DDI_MODE_SELECT_DP_MST:
2119 pipe_config->has_dp_encoder = true;
2120 intel_dp_get_m_n(intel_crtc, pipe_config);
2121 break;
2122 default:
2123 break;
2124 }
Daniel Vetter10214422013-11-18 07:38:16 +01002125
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002126 if (intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO)) {
Paulo Zanonia60551b2014-05-21 16:23:20 -03002127 temp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +02002128 if (temp & AUDIO_OUTPUT_ENABLE(intel_crtc->pipe))
Paulo Zanonia60551b2014-05-21 16:23:20 -03002129 pipe_config->has_audio = true;
2130 }
Daniel Vetter9ed109a2014-04-24 23:54:52 +02002131
Daniel Vetter10214422013-11-18 07:38:16 +01002132 if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp_bpp &&
2133 pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
2134 /*
2135 * This is a big fat ugly hack.
2136 *
2137 * Some machines in UEFI boot mode provide us a VBT that has 18
2138 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2139 * unknown we fail to light up. Yet the same BIOS boots up with
2140 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2141 * max, not what it tells us to use.
2142 *
2143 * Note: This will still be broken if the eDP panel is not lit
2144 * up by the BIOS, and thus we can't get the mode at module
2145 * load.
2146 */
2147 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
2148 pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
2149 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
2150 }
Jesse Barnes11578552014-01-21 12:42:10 -08002151
Damien Lespiau22606a12014-12-12 14:26:57 +00002152 intel_ddi_clock_get(encoder, pipe_config);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002153}
2154
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002155static void intel_ddi_destroy(struct drm_encoder *encoder)
2156{
2157 /* HDMI has nothing special to destroy, so we can go with this. */
2158 intel_dp_encoder_destroy(encoder);
2159}
2160
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002161static bool intel_ddi_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002162 struct intel_crtc_state *pipe_config)
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002163{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002164 int type = encoder->type;
Daniel Vettereccb1402013-05-22 00:50:22 +02002165 int port = intel_ddi_get_encoder_port(encoder);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002166
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002167 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002168
Daniel Vettereccb1402013-05-22 00:50:22 +02002169 if (port == PORT_A)
2170 pipe_config->cpu_transcoder = TRANSCODER_EDP;
2171
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002172 if (type == INTEL_OUTPUT_HDMI)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002173 return intel_hdmi_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002174 else
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002175 return intel_dp_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002176}
2177
2178static const struct drm_encoder_funcs intel_ddi_funcs = {
2179 .destroy = intel_ddi_destroy,
2180};
2181
Paulo Zanoni4a28ae52013-10-09 13:52:36 -03002182static struct intel_connector *
2183intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
2184{
2185 struct intel_connector *connector;
2186 enum port port = intel_dig_port->port;
2187
2188 connector = kzalloc(sizeof(*connector), GFP_KERNEL);
2189 if (!connector)
2190 return NULL;
2191
2192 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
2193 if (!intel_dp_init_connector(intel_dig_port, connector)) {
2194 kfree(connector);
2195 return NULL;
2196 }
2197
2198 return connector;
2199}
2200
2201static struct intel_connector *
2202intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
2203{
2204 struct intel_connector *connector;
2205 enum port port = intel_dig_port->port;
2206
2207 connector = kzalloc(sizeof(*connector), GFP_KERNEL);
2208 if (!connector)
2209 return NULL;
2210
2211 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
2212 intel_hdmi_init_connector(intel_dig_port, connector);
2213
2214 return connector;
2215}
2216
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002217void intel_ddi_init(struct drm_device *dev, enum port port)
2218{
Damien Lespiau876a8cd2012-12-11 18:48:30 +00002219 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002220 struct intel_digital_port *intel_dig_port;
2221 struct intel_encoder *intel_encoder;
2222 struct drm_encoder *encoder;
Paulo Zanoni311a2092013-09-12 17:12:18 -03002223 bool init_hdmi, init_dp;
2224
2225 init_hdmi = (dev_priv->vbt.ddi_port_info[port].supports_dvi ||
2226 dev_priv->vbt.ddi_port_info[port].supports_hdmi);
2227 init_dp = dev_priv->vbt.ddi_port_info[port].supports_dp;
2228 if (!init_dp && !init_hdmi) {
Chris Wilsonf68d6972014-08-04 07:15:09 +01002229 DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, assuming it is\n",
Paulo Zanoni311a2092013-09-12 17:12:18 -03002230 port_name(port));
2231 init_hdmi = true;
2232 init_dp = true;
2233 }
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002234
Daniel Vetterb14c5672013-09-19 12:18:32 +02002235 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002236 if (!intel_dig_port)
2237 return;
2238
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002239 intel_encoder = &intel_dig_port->base;
2240 encoder = &intel_encoder->base;
2241
2242 drm_encoder_init(dev, encoder, &intel_ddi_funcs,
2243 DRM_MODE_ENCODER_TMDS);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002244
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002245 intel_encoder->compute_config = intel_ddi_compute_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002246 intel_encoder->enable = intel_enable_ddi;
2247 intel_encoder->pre_enable = intel_ddi_pre_enable;
2248 intel_encoder->disable = intel_disable_ddi;
2249 intel_encoder->post_disable = intel_ddi_post_disable;
2250 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002251 intel_encoder->get_config = intel_ddi_get_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002252
2253 intel_dig_port->port = port;
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07002254 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
2255 (DDI_BUF_PORT_REVERSAL |
2256 DDI_A_4_LANES);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002257
2258 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
Chris Wilsonf68d6972014-08-04 07:15:09 +01002259 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Ville Syrjäläbc079e82014-03-03 16:15:28 +02002260 intel_encoder->cloneable = 0;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002261 intel_encoder->hot_plug = intel_ddi_hot_plug;
2262
Chris Wilsonf68d6972014-08-04 07:15:09 +01002263 if (init_dp) {
2264 if (!intel_ddi_init_dp_connector(intel_dig_port))
2265 goto err;
Dave Airlie13cf5502014-06-18 11:29:35 +10002266
Chris Wilsonf68d6972014-08-04 07:15:09 +01002267 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
2268 dev_priv->hpd_irq_port[port] = intel_dig_port;
2269 }
Daniel Vetter21a8e6a2013-04-10 23:28:35 +02002270
Paulo Zanoni311a2092013-09-12 17:12:18 -03002271 /* In theory we don't need the encoder->type check, but leave it just in
2272 * case we have some really bad VBTs... */
Chris Wilsonf68d6972014-08-04 07:15:09 +01002273 if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
2274 if (!intel_ddi_init_hdmi_connector(intel_dig_port))
2275 goto err;
Daniel Vetter21a8e6a2013-04-10 23:28:35 +02002276 }
Chris Wilsonf68d6972014-08-04 07:15:09 +01002277
2278 return;
2279
2280err:
2281 drm_encoder_cleanup(encoder);
2282 kfree(intel_dig_port);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002283}