blob: e0b618851a53535e182f9da762ebbaa5428537ce [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[] = {
Sonika Jindalb7192a52015-04-15 11:02:33 +0530158 { 0x00000018, 0x000000ac },
159 { 0x00005012, 0x0000009d },
160 { 0x00007011, 0x00000088 },
161 { 0x00000018, 0x000000a1 },
162 { 0x00000018, 0x00000098 },
163 { 0x00004013, 0x00000088 },
164 { 0x00006012, 0x00000087 },
165 { 0x00000018, 0x000000df },
166 { 0x00003015, 0x00000087 },
167 { 0x00003015, 0x000000c7 },
168 { 0x00000018, 0x000000c7 },
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000169};
170
Jani Nikula20f4dbe2013-08-30 19:40:28 +0300171enum port intel_ddi_get_encoder_port(struct intel_encoder *intel_encoder)
Paulo Zanonifc914632012-10-05 12:05:54 -0300172{
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300173 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanonifc914632012-10-05 12:05:54 -0300174 int type = intel_encoder->type;
175
Dave Airlie0e32b392014-05-02 14:02:48 +1000176 if (type == INTEL_OUTPUT_DP_MST) {
177 struct intel_digital_port *intel_dig_port = enc_to_mst(encoder)->primary;
178 return intel_dig_port->port;
179 } else if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP ||
Paulo Zanoni00c09d72012-10-26 19:05:52 -0200180 type == INTEL_OUTPUT_HDMI || type == INTEL_OUTPUT_UNKNOWN) {
Paulo Zanoni174edf12012-10-26 19:05:50 -0200181 struct intel_digital_port *intel_dig_port =
182 enc_to_dig_port(encoder);
183 return intel_dig_port->port;
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300184
Paulo Zanonifc914632012-10-05 12:05:54 -0300185 } else if (type == INTEL_OUTPUT_ANALOG) {
186 return PORT_E;
Paulo Zanoni0bdee302012-10-15 15:51:38 -0300187
Paulo Zanonifc914632012-10-05 12:05:54 -0300188 } else {
189 DRM_ERROR("Invalid DDI encoder type %d\n", type);
190 BUG();
191 }
192}
193
Art Runyane58623c2013-11-02 21:07:41 -0700194/*
195 * Starting with Haswell, DDI port buffers must be programmed with correct
196 * values in advance. The buffer values are different for FDI and DP modes,
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300197 * but the HDMI/DVI fields are shared among those. So we program the DDI
198 * in either FDI or DP modes only, as HDMI connections will work with both
199 * of those
200 */
Damien Lespiaub4037452014-08-04 22:01:33 +0100201static void intel_prepare_ddi_buffers(struct drm_device *dev,
202 struct intel_digital_port *intel_dig_port)
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300203{
204 struct drm_i915_private *dev_priv = dev->dev_private;
205 u32 reg;
Damien Lespiaub4037452014-08-04 22:01:33 +0100206 int port = intel_dig_port->port;
Damien Lespiau7ff44672015-03-02 16:19:36 +0000207 int i, n_hdmi_entries, n_dp_entries, n_edp_entries, hdmi_default_entry,
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530208 size;
Paulo Zanoni6acab152013-09-12 17:06:24 -0300209 int hdmi_level = dev_priv->vbt.ddi_port_info[port].hdmi_level_shift;
Jani Nikula10122052014-08-27 16:27:30 +0300210 const struct ddi_buf_trans *ddi_translations_fdi;
211 const struct ddi_buf_trans *ddi_translations_dp;
212 const struct ddi_buf_trans *ddi_translations_edp;
213 const struct ddi_buf_trans *ddi_translations_hdmi;
214 const struct ddi_buf_trans *ddi_translations;
Art Runyane58623c2013-11-02 21:07:41 -0700215
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000216 if (IS_SKYLAKE(dev)) {
217 ddi_translations_fdi = NULL;
218 ddi_translations_dp = skl_ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530219 n_dp_entries = ARRAY_SIZE(skl_ddi_translations_dp);
220 if (dev_priv->vbt.edp_low_vswing) {
221 ddi_translations_edp = skl_ddi_translations_edp;
222 n_edp_entries = ARRAY_SIZE(skl_ddi_translations_edp);
223 } else {
224 ddi_translations_edp = skl_ddi_translations_dp;
225 n_edp_entries = ARRAY_SIZE(skl_ddi_translations_dp);
226 }
227
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000228 ddi_translations_hdmi = skl_ddi_translations_hdmi;
229 n_hdmi_entries = ARRAY_SIZE(skl_ddi_translations_hdmi);
Sonika Jindalb7192a52015-04-15 11:02:33 +0530230 hdmi_default_entry = 7;
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000231 } else if (IS_BROADWELL(dev)) {
Art Runyane58623c2013-11-02 21:07:41 -0700232 ddi_translations_fdi = bdw_ddi_translations_fdi;
233 ddi_translations_dp = bdw_ddi_translations_dp;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700234 ddi_translations_edp = bdw_ddi_translations_edp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100235 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530236 n_edp_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
237 n_dp_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300238 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000239 hdmi_default_entry = 7;
Art Runyane58623c2013-11-02 21:07:41 -0700240 } else if (IS_HASWELL(dev)) {
241 ddi_translations_fdi = hsw_ddi_translations_fdi;
242 ddi_translations_dp = hsw_ddi_translations_dp;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700243 ddi_translations_edp = hsw_ddi_translations_dp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100244 ddi_translations_hdmi = hsw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530245 n_dp_entries = n_edp_entries = ARRAY_SIZE(hsw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300246 n_hdmi_entries = ARRAY_SIZE(hsw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000247 hdmi_default_entry = 6;
Art Runyane58623c2013-11-02 21:07:41 -0700248 } else {
249 WARN(1, "ddi translation table missing\n");
Paulo Zanoni300644c2013-11-02 21:07:42 -0700250 ddi_translations_edp = bdw_ddi_translations_dp;
Art Runyane58623c2013-11-02 21:07:41 -0700251 ddi_translations_fdi = bdw_ddi_translations_fdi;
252 ddi_translations_dp = bdw_ddi_translations_dp;
Damien Lespiaua26aa8b2014-08-01 11:07:55 +0100253 ddi_translations_hdmi = bdw_ddi_translations_hdmi;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530254 n_edp_entries = ARRAY_SIZE(bdw_ddi_translations_edp);
255 n_dp_entries = ARRAY_SIZE(bdw_ddi_translations_dp);
Jani Nikula10122052014-08-27 16:27:30 +0300256 n_hdmi_entries = ARRAY_SIZE(bdw_ddi_translations_hdmi);
Damien Lespiau7ff44672015-03-02 16:19:36 +0000257 hdmi_default_entry = 7;
Art Runyane58623c2013-11-02 21:07:41 -0700258 }
259
Paulo Zanoni300644c2013-11-02 21:07:42 -0700260 switch (port) {
261 case PORT_A:
262 ddi_translations = ddi_translations_edp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530263 size = n_edp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700264 break;
265 case PORT_B:
266 case PORT_C:
Paulo Zanoni300644c2013-11-02 21:07:42 -0700267 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530268 size = n_dp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700269 break;
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700270 case PORT_D:
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530271 if (intel_dp_is_edp(dev, PORT_D)) {
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700272 ddi_translations = ddi_translations_edp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530273 size = n_edp_entries;
274 } else {
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700275 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530276 size = n_dp_entries;
277 }
Paulo Zanoni77d8d002013-11-02 21:07:45 -0700278 break;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700279 case PORT_E:
Damien Lespiau7f88e3a2013-12-03 13:56:25 +0000280 if (ddi_translations_fdi)
281 ddi_translations = ddi_translations_fdi;
282 else
283 ddi_translations = ddi_translations_dp;
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530284 size = n_dp_entries;
Paulo Zanoni300644c2013-11-02 21:07:42 -0700285 break;
286 default:
287 BUG();
288 }
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300289
Sonika Jindal7ad14a22015-02-25 10:29:12 +0530290 for (i = 0, reg = DDI_BUF_TRANS(port); i < size; i++) {
Jani Nikula10122052014-08-27 16:27:30 +0300291 I915_WRITE(reg, ddi_translations[i].trans1);
292 reg += 4;
293 I915_WRITE(reg, ddi_translations[i].trans2);
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300294 reg += 4;
295 }
Damien Lespiauce4dd492014-08-01 11:07:54 +0100296
297 /* Choose a good default if VBT is badly populated */
298 if (hdmi_level == HDMI_LEVEL_SHIFT_UNKNOWN ||
299 hdmi_level >= n_hdmi_entries)
Damien Lespiau7ff44672015-03-02 16:19:36 +0000300 hdmi_level = hdmi_default_entry;
Damien Lespiauce4dd492014-08-01 11:07:54 +0100301
Paulo Zanoni6acab152013-09-12 17:06:24 -0300302 /* Entry 9 is for HDMI: */
Jani Nikula10122052014-08-27 16:27:30 +0300303 I915_WRITE(reg, ddi_translations_hdmi[hdmi_level].trans1);
304 reg += 4;
305 I915_WRITE(reg, ddi_translations_hdmi[hdmi_level].trans2);
306 reg += 4;
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300307}
308
309/* Program DDI buffers translations for DP. By default, program ports A-D in DP
310 * mode and port E for FDI.
311 */
312void intel_prepare_ddi(struct drm_device *dev)
313{
Damien Lespiaub4037452014-08-04 22:01:33 +0100314 struct intel_digital_port *intel_dig_port;
315 bool visited[I915_MAX_PORTS] = { 0, };
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300316
Paulo Zanoni0d536cb2012-11-23 16:46:41 -0200317 if (!HAS_DDI(dev))
318 return;
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300319
Damien Lespiaub4037452014-08-04 22:01:33 +0100320 for_each_digital_port(dev, intel_dig_port) {
321 if (visited[intel_dig_port->port])
322 continue;
323
324 intel_prepare_ddi_buffers(dev, intel_dig_port);
325 visited[intel_dig_port->port] = true;
326 }
Eugeni Dodonov45244b82012-05-09 15:37:20 -0300327}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300328
Paulo Zanoni248138b2012-11-29 11:29:31 -0200329static void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
330 enum port port)
331{
332 uint32_t reg = DDI_BUF_CTL(port);
333 int i;
334
Vandana Kannan3449ca82015-03-27 14:19:09 +0200335 for (i = 0; i < 16; i++) {
Paulo Zanoni248138b2012-11-29 11:29:31 -0200336 udelay(1);
337 if (I915_READ(reg) & DDI_BUF_IS_IDLE)
338 return;
339 }
340 DRM_ERROR("Timeout waiting for DDI BUF %c idle bit\n", port_name(port));
341}
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300342
343/* Starting with Haswell, different DDI ports can work in FDI mode for
344 * connection to the PCH-located connectors. For this, it is necessary to train
345 * both the DDI port and PCH receiver for the desired DDI buffer settings.
346 *
347 * The recommended port to work in FDI mode is DDI E, which we use here. Also,
348 * please note that when FDI mode is active on DDI E, it shares 2 lines with
349 * DDI A (which is used for eDP)
350 */
351
352void hsw_fdi_link_train(struct drm_crtc *crtc)
353{
354 struct drm_device *dev = crtc->dev;
355 struct drm_i915_private *dev_priv = dev->dev_private;
356 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni04945642012-11-01 21:00:59 -0200357 u32 temp, i, rx_ctl_val;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300358
Paulo Zanoni04945642012-11-01 21:00:59 -0200359 /* Set the FDI_RX_MISC pwrdn lanes and the 2 workarounds listed at the
360 * mode set "sequence for CRT port" document:
361 * - TP1 to TP2 time with the default value
362 * - FDI delay to 90h
Damien Lespiau8693a822013-05-03 18:48:11 +0100363 *
364 * WaFDIAutoLinkSetTimingOverrride:hsw
Paulo Zanoni04945642012-11-01 21:00:59 -0200365 */
366 I915_WRITE(_FDI_RXA_MISC, FDI_RX_PWRDN_LANE1_VAL(2) |
367 FDI_RX_PWRDN_LANE0_VAL(2) |
368 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
369
370 /* Enable the PCH Receiver FDI PLL */
Damien Lespiau3e683202012-12-11 18:48:29 +0000371 rx_ctl_val = dev_priv->fdi_rx_config | FDI_RX_ENHANCE_FRAME_ENABLE |
Daniel Vetter33d29b12013-02-13 18:04:45 +0100372 FDI_RX_PLL_ENABLE |
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200373 FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Paulo Zanoni04945642012-11-01 21:00:59 -0200374 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
375 POSTING_READ(_FDI_RXA_CTL);
376 udelay(220);
377
378 /* Switch from Rawclk to PCDclk */
379 rx_ctl_val |= FDI_PCDCLK;
380 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
381
382 /* Configure Port Clock Select */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200383 I915_WRITE(PORT_CLK_SEL(PORT_E), intel_crtc->config->ddi_pll_sel);
384 WARN_ON(intel_crtc->config->ddi_pll_sel != PORT_CLK_SEL_SPLL);
Paulo Zanoni04945642012-11-01 21:00:59 -0200385
386 /* Start the training iterating through available voltages and emphasis,
387 * testing each value twice. */
Jani Nikula10122052014-08-27 16:27:30 +0300388 for (i = 0; i < ARRAY_SIZE(hsw_ddi_translations_fdi) * 2; i++) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300389 /* Configure DP_TP_CTL with auto-training */
390 I915_WRITE(DP_TP_CTL(PORT_E),
391 DP_TP_CTL_FDI_AUTOTRAIN |
392 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
393 DP_TP_CTL_LINK_TRAIN_PAT1 |
394 DP_TP_CTL_ENABLE);
395
Damien Lespiau876a8cd2012-12-11 18:48:30 +0000396 /* Configure and enable DDI_BUF_CTL for DDI E with next voltage.
397 * DDI E does not support port reversal, the functionality is
398 * achieved on the PCH side in FDI_RX_CTL, so no need to set the
399 * port reversal bit */
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300400 I915_WRITE(DDI_BUF_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200401 DDI_BUF_CTL_ENABLE |
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +0200402 ((intel_crtc->config->fdi_lanes - 1) << 1) |
Sonika Jindalc5fe6a02014-08-11 08:57:36 +0530403 DDI_BUF_TRANS_SELECT(i / 2));
Paulo Zanoni04945642012-11-01 21:00:59 -0200404 POSTING_READ(DDI_BUF_CTL(PORT_E));
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300405
406 udelay(600);
407
Paulo Zanoni04945642012-11-01 21:00:59 -0200408 /* Program PCH FDI Receiver TU */
409 I915_WRITE(_FDI_RXA_TUSIZE1, TU_SIZE(64));
Eugeni Dodonov4acf5182012-07-04 20:15:16 -0300410
Paulo Zanoni04945642012-11-01 21:00:59 -0200411 /* Enable PCH FDI Receiver with auto-training */
412 rx_ctl_val |= FDI_RX_ENABLE | FDI_LINK_TRAIN_AUTO;
413 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
414 POSTING_READ(_FDI_RXA_CTL);
415
416 /* Wait for FDI receiver lane calibration */
417 udelay(30);
418
419 /* Unset FDI_RX_MISC pwrdn lanes */
420 temp = I915_READ(_FDI_RXA_MISC);
421 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
422 I915_WRITE(_FDI_RXA_MISC, temp);
423 POSTING_READ(_FDI_RXA_MISC);
424
425 /* Wait for FDI auto training time */
426 udelay(5);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300427
428 temp = I915_READ(DP_TP_STATUS(PORT_E));
429 if (temp & DP_TP_STATUS_AUTOTRAIN_DONE) {
Paulo Zanoni04945642012-11-01 21:00:59 -0200430 DRM_DEBUG_KMS("FDI link training done on step %d\n", i);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300431
432 /* Enable normal pixel sending for FDI */
433 I915_WRITE(DP_TP_CTL(PORT_E),
Paulo Zanoni04945642012-11-01 21:00:59 -0200434 DP_TP_CTL_FDI_AUTOTRAIN |
435 DP_TP_CTL_LINK_TRAIN_NORMAL |
436 DP_TP_CTL_ENHANCED_FRAME_ENABLE |
437 DP_TP_CTL_ENABLE);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300438
Paulo Zanoni04945642012-11-01 21:00:59 -0200439 return;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300440 }
Paulo Zanoni04945642012-11-01 21:00:59 -0200441
Paulo Zanoni248138b2012-11-29 11:29:31 -0200442 temp = I915_READ(DDI_BUF_CTL(PORT_E));
443 temp &= ~DDI_BUF_CTL_ENABLE;
444 I915_WRITE(DDI_BUF_CTL(PORT_E), temp);
445 POSTING_READ(DDI_BUF_CTL(PORT_E));
446
Paulo Zanoni04945642012-11-01 21:00:59 -0200447 /* Disable DP_TP_CTL and FDI_RX_CTL and retry */
Paulo Zanoni248138b2012-11-29 11:29:31 -0200448 temp = I915_READ(DP_TP_CTL(PORT_E));
449 temp &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
450 temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
451 I915_WRITE(DP_TP_CTL(PORT_E), temp);
452 POSTING_READ(DP_TP_CTL(PORT_E));
453
454 intel_wait_ddi_buf_idle(dev_priv, PORT_E);
Paulo Zanoni04945642012-11-01 21:00:59 -0200455
456 rx_ctl_val &= ~FDI_RX_ENABLE;
457 I915_WRITE(_FDI_RXA_CTL, rx_ctl_val);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200458 POSTING_READ(_FDI_RXA_CTL);
Paulo Zanoni04945642012-11-01 21:00:59 -0200459
460 /* Reset FDI_RX_MISC pwrdn lanes */
461 temp = I915_READ(_FDI_RXA_MISC);
462 temp &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
463 temp |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
464 I915_WRITE(_FDI_RXA_MISC, temp);
Paulo Zanoni248138b2012-11-29 11:29:31 -0200465 POSTING_READ(_FDI_RXA_MISC);
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300466 }
467
Paulo Zanoni04945642012-11-01 21:00:59 -0200468 DRM_ERROR("FDI link training failed!\n");
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -0300469}
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -0300470
Dave Airlie44905a272014-05-02 13:36:43 +1000471void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder)
472{
473 struct intel_dp *intel_dp = enc_to_intel_dp(&encoder->base);
474 struct intel_digital_port *intel_dig_port =
475 enc_to_dig_port(&encoder->base);
476
477 intel_dp->DP = intel_dig_port->saved_port_bits |
Sonika Jindalc5fe6a02014-08-11 08:57:36 +0530478 DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
Dave Airlie44905a272014-05-02 13:36:43 +1000479 intel_dp->DP |= DDI_PORT_WIDTH(intel_dp->lane_count);
480
481}
482
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300483static struct intel_encoder *
484intel_ddi_get_crtc_encoder(struct drm_crtc *crtc)
485{
486 struct drm_device *dev = crtc->dev;
487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
488 struct intel_encoder *intel_encoder, *ret = NULL;
489 int num_encoders = 0;
490
491 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
492 ret = intel_encoder;
493 num_encoders++;
494 }
495
496 if (num_encoders != 1)
Ville Syrjälä84f44ce2013-04-17 17:48:49 +0300497 WARN(1, "%d encoders on crtc for pipe %c\n", num_encoders,
498 pipe_name(intel_crtc->pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -0300499
500 BUG_ON(ret == NULL);
501 return ret;
502}
503
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +0530504struct intel_encoder *
Ander Conselvan de Oliveira3165c072015-03-20 16:18:12 +0200505intel_ddi_get_crtc_new_encoder(struct intel_crtc_state *crtc_state)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200506{
Ander Conselvan de Oliveira3165c072015-03-20 16:18:12 +0200507 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
508 struct intel_encoder *ret = NULL;
509 struct drm_atomic_state *state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200510 int num_encoders = 0;
Ander Conselvan de Oliveira3165c072015-03-20 16:18:12 +0200511 int i;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200512
Ander Conselvan de Oliveira3165c072015-03-20 16:18:12 +0200513 state = crtc_state->base.state;
514
515 for (i = 0; i < state->num_connector; i++) {
516 if (!state->connectors[i] ||
517 state->connector_states[i]->crtc != crtc_state->base.crtc)
518 continue;
519
520 ret = to_intel_encoder(state->connector_states[i]->best_encoder);
521 num_encoders++;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200522 }
523
524 WARN(num_encoders != 1, "%d encoders on crtc for pipe %c\n", num_encoders,
525 pipe_name(crtc->pipe));
526
527 BUG_ON(ret == NULL);
528 return ret;
529}
530
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100531#define LC_FREQ 2700
Damien Lespiau27893392014-09-04 12:27:23 +0100532#define LC_FREQ_2K U64_C(LC_FREQ * 2000)
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100533
534#define P_MIN 2
535#define P_MAX 64
536#define P_INC 2
537
538/* Constraints for PLL good behavior */
539#define REF_MIN 48
540#define REF_MAX 400
541#define VCO_MIN 2400
542#define VCO_MAX 4800
543
Damien Lespiau27893392014-09-04 12:27:23 +0100544#define abs_diff(a, b) ({ \
545 typeof(a) __a = (a); \
546 typeof(b) __b = (b); \
547 (void) (&__a == &__b); \
548 __a > __b ? (__a - __b) : (__b - __a); })
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100549
550struct wrpll_rnp {
551 unsigned p, n2, r2;
552};
553
554static unsigned wrpll_get_budget_for_freq(int clock)
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300555{
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100556 unsigned budget;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300557
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100558 switch (clock) {
559 case 25175000:
560 case 25200000:
561 case 27000000:
562 case 27027000:
563 case 37762500:
564 case 37800000:
565 case 40500000:
566 case 40541000:
567 case 54000000:
568 case 54054000:
569 case 59341000:
570 case 59400000:
571 case 72000000:
572 case 74176000:
573 case 74250000:
574 case 81000000:
575 case 81081000:
576 case 89012000:
577 case 89100000:
578 case 108000000:
579 case 108108000:
580 case 111264000:
581 case 111375000:
582 case 148352000:
583 case 148500000:
584 case 162000000:
585 case 162162000:
586 case 222525000:
587 case 222750000:
588 case 296703000:
589 case 297000000:
590 budget = 0;
591 break;
592 case 233500000:
593 case 245250000:
594 case 247750000:
595 case 253250000:
596 case 298000000:
597 budget = 1500;
598 break;
599 case 169128000:
600 case 169500000:
601 case 179500000:
602 case 202000000:
603 budget = 2000;
604 break;
605 case 256250000:
606 case 262500000:
607 case 270000000:
608 case 272500000:
609 case 273750000:
610 case 280750000:
611 case 281250000:
612 case 286000000:
613 case 291750000:
614 budget = 4000;
615 break;
616 case 267250000:
617 case 268500000:
618 budget = 5000;
619 break;
620 default:
621 budget = 1000;
622 break;
623 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300624
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100625 return budget;
626}
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300627
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100628static void wrpll_update_rnp(uint64_t freq2k, unsigned budget,
629 unsigned r2, unsigned n2, unsigned p,
630 struct wrpll_rnp *best)
631{
632 uint64_t a, b, c, d, diff, diff_best;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300633
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100634 /* No best (r,n,p) yet */
635 if (best->p == 0) {
636 best->p = p;
637 best->n2 = n2;
638 best->r2 = r2;
639 return;
640 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300641
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100642 /*
643 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
644 * freq2k.
645 *
646 * delta = 1e6 *
647 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
648 * freq2k;
649 *
650 * and we would like delta <= budget.
651 *
652 * If the discrepancy is above the PPM-based budget, always prefer to
653 * improve upon the previous solution. However, if you're within the
654 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
655 */
656 a = freq2k * budget * p * r2;
657 b = freq2k * budget * best->p * best->r2;
Damien Lespiau27893392014-09-04 12:27:23 +0100658 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
659 diff_best = abs_diff(freq2k * best->p * best->r2,
660 LC_FREQ_2K * best->n2);
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100661 c = 1000000 * diff;
662 d = 1000000 * diff_best;
663
664 if (a < c && b < d) {
665 /* If both are above the budget, pick the closer */
666 if (best->p * best->r2 * diff < p * r2 * diff_best) {
667 best->p = p;
668 best->n2 = n2;
669 best->r2 = r2;
670 }
671 } else if (a >= c && b < d) {
672 /* If A is below the threshold but B is above it? Update. */
673 best->p = p;
674 best->n2 = n2;
675 best->r2 = r2;
676 } else if (a >= c && b >= d) {
677 /* Both are below the limit, so pick the higher n2/(r2*r2) */
678 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
679 best->p = p;
680 best->n2 = n2;
681 best->r2 = r2;
682 }
683 }
684 /* Otherwise a < c && b >= d, do nothing */
685}
686
Jesse Barnes11578552014-01-21 12:42:10 -0800687static int intel_ddi_calc_wrpll_link(struct drm_i915_private *dev_priv,
688 int reg)
689{
690 int refclk = LC_FREQ;
691 int n, p, r;
692 u32 wrpll;
693
694 wrpll = I915_READ(reg);
Daniel Vetter114fe482014-06-25 22:01:48 +0300695 switch (wrpll & WRPLL_PLL_REF_MASK) {
696 case WRPLL_PLL_SSC:
697 case WRPLL_PLL_NON_SSC:
Jesse Barnes11578552014-01-21 12:42:10 -0800698 /*
699 * We could calculate spread here, but our checking
700 * code only cares about 5% accuracy, and spread is a max of
701 * 0.5% downspread.
702 */
703 refclk = 135;
704 break;
Daniel Vetter114fe482014-06-25 22:01:48 +0300705 case WRPLL_PLL_LCPLL:
Jesse Barnes11578552014-01-21 12:42:10 -0800706 refclk = LC_FREQ;
707 break;
708 default:
709 WARN(1, "bad wrpll refclk\n");
710 return 0;
711 }
712
713 r = wrpll & WRPLL_DIVIDER_REF_MASK;
714 p = (wrpll & WRPLL_DIVIDER_POST_MASK) >> WRPLL_DIVIDER_POST_SHIFT;
715 n = (wrpll & WRPLL_DIVIDER_FB_MASK) >> WRPLL_DIVIDER_FB_SHIFT;
716
Jesse Barnes20f0ec12014-01-22 12:58:04 -0800717 /* Convert to KHz, p & r have a fixed point portion */
718 return (refclk * n * 100) / (p * r);
Jesse Barnes11578552014-01-21 12:42:10 -0800719}
720
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000721static int skl_calc_wrpll_link(struct drm_i915_private *dev_priv,
722 uint32_t dpll)
723{
724 uint32_t cfgcr1_reg, cfgcr2_reg;
725 uint32_t cfgcr1_val, cfgcr2_val;
726 uint32_t p0, p1, p2, dco_freq;
727
728 cfgcr1_reg = GET_CFG_CR1_REG(dpll);
729 cfgcr2_reg = GET_CFG_CR2_REG(dpll);
730
731 cfgcr1_val = I915_READ(cfgcr1_reg);
732 cfgcr2_val = I915_READ(cfgcr2_reg);
733
734 p0 = cfgcr2_val & DPLL_CFGCR2_PDIV_MASK;
735 p2 = cfgcr2_val & DPLL_CFGCR2_KDIV_MASK;
736
737 if (cfgcr2_val & DPLL_CFGCR2_QDIV_MODE(1))
738 p1 = (cfgcr2_val & DPLL_CFGCR2_QDIV_RATIO_MASK) >> 8;
739 else
740 p1 = 1;
741
742
743 switch (p0) {
744 case DPLL_CFGCR2_PDIV_1:
745 p0 = 1;
746 break;
747 case DPLL_CFGCR2_PDIV_2:
748 p0 = 2;
749 break;
750 case DPLL_CFGCR2_PDIV_3:
751 p0 = 3;
752 break;
753 case DPLL_CFGCR2_PDIV_7:
754 p0 = 7;
755 break;
756 }
757
758 switch (p2) {
759 case DPLL_CFGCR2_KDIV_5:
760 p2 = 5;
761 break;
762 case DPLL_CFGCR2_KDIV_2:
763 p2 = 2;
764 break;
765 case DPLL_CFGCR2_KDIV_3:
766 p2 = 3;
767 break;
768 case DPLL_CFGCR2_KDIV_1:
769 p2 = 1;
770 break;
771 }
772
773 dco_freq = (cfgcr1_val & DPLL_CFGCR1_DCO_INTEGER_MASK) * 24 * 1000;
774
775 dco_freq += (((cfgcr1_val & DPLL_CFGCR1_DCO_FRACTION_MASK) >> 9) * 24 *
776 1000) / 0x8000;
777
778 return dco_freq / (p0 * p1 * p2 * 5);
779}
780
781
782static void skl_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200783 struct intel_crtc_state *pipe_config)
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000784{
785 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000786 int link_clock = 0;
787 uint32_t dpll_ctl1, dpll;
788
Damien Lespiau134ffa42014-11-14 17:24:34 +0000789 dpll = pipe_config->ddi_pll_sel;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000790
791 dpll_ctl1 = I915_READ(DPLL_CTRL1);
792
793 if (dpll_ctl1 & DPLL_CTRL1_HDMI_MODE(dpll)) {
794 link_clock = skl_calc_wrpll_link(dev_priv, dpll);
795 } else {
796 link_clock = dpll_ctl1 & DPLL_CRTL1_LINK_RATE_MASK(dpll);
797 link_clock >>= DPLL_CRTL1_LINK_RATE_SHIFT(dpll);
798
799 switch (link_clock) {
800 case DPLL_CRTL1_LINK_RATE_810:
801 link_clock = 81000;
802 break;
Sonika Jindala8f3ef62015-03-05 10:02:30 +0530803 case DPLL_CRTL1_LINK_RATE_1080:
804 link_clock = 108000;
805 break;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000806 case DPLL_CRTL1_LINK_RATE_1350:
807 link_clock = 135000;
808 break;
Sonika Jindala8f3ef62015-03-05 10:02:30 +0530809 case DPLL_CRTL1_LINK_RATE_1620:
810 link_clock = 162000;
811 break;
812 case DPLL_CRTL1_LINK_RATE_2160:
813 link_clock = 216000;
814 break;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000815 case DPLL_CRTL1_LINK_RATE_2700:
816 link_clock = 270000;
817 break;
818 default:
819 WARN(1, "Unsupported link rate\n");
820 break;
821 }
822 link_clock *= 2;
823 }
824
825 pipe_config->port_clock = link_clock;
826
827 if (pipe_config->has_dp_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200828 pipe_config->base.adjusted_mode.crtc_clock =
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000829 intel_dotclock_calculate(pipe_config->port_clock,
830 &pipe_config->dp_m_n);
831 else
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200832 pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
Satheeshakrishna M540e7322014-11-13 14:55:16 +0000833}
834
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200835static void hsw_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200836 struct intel_crtc_state *pipe_config)
Jesse Barnes11578552014-01-21 12:42:10 -0800837{
838 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
Jesse Barnes11578552014-01-21 12:42:10 -0800839 int link_clock = 0;
840 u32 val, pll;
841
Daniel Vetter26804af2014-06-25 22:01:55 +0300842 val = pipe_config->ddi_pll_sel;
Jesse Barnes11578552014-01-21 12:42:10 -0800843 switch (val & PORT_CLK_SEL_MASK) {
844 case PORT_CLK_SEL_LCPLL_810:
845 link_clock = 81000;
846 break;
847 case PORT_CLK_SEL_LCPLL_1350:
848 link_clock = 135000;
849 break;
850 case PORT_CLK_SEL_LCPLL_2700:
851 link_clock = 270000;
852 break;
853 case PORT_CLK_SEL_WRPLL1:
854 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL1);
855 break;
856 case PORT_CLK_SEL_WRPLL2:
857 link_clock = intel_ddi_calc_wrpll_link(dev_priv, WRPLL_CTL2);
858 break;
859 case PORT_CLK_SEL_SPLL:
860 pll = I915_READ(SPLL_CTL) & SPLL_PLL_FREQ_MASK;
861 if (pll == SPLL_PLL_FREQ_810MHz)
862 link_clock = 81000;
863 else if (pll == SPLL_PLL_FREQ_1350MHz)
864 link_clock = 135000;
865 else if (pll == SPLL_PLL_FREQ_2700MHz)
866 link_clock = 270000;
867 else {
868 WARN(1, "bad spll freq\n");
869 return;
870 }
871 break;
872 default:
873 WARN(1, "bad port clock sel\n");
874 return;
875 }
876
877 pipe_config->port_clock = link_clock * 2;
878
879 if (pipe_config->has_pch_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200880 pipe_config->base.adjusted_mode.crtc_clock =
Jesse Barnes11578552014-01-21 12:42:10 -0800881 intel_dotclock_calculate(pipe_config->port_clock,
882 &pipe_config->fdi_m_n);
883 else if (pipe_config->has_dp_encoder)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200884 pipe_config->base.adjusted_mode.crtc_clock =
Jesse Barnes11578552014-01-21 12:42:10 -0800885 intel_dotclock_calculate(pipe_config->port_clock,
886 &pipe_config->dp_m_n);
887 else
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +0200888 pipe_config->base.adjusted_mode.crtc_clock = pipe_config->port_clock;
Jesse Barnes11578552014-01-21 12:42:10 -0800889}
890
Satheeshakrishna M977bb382014-08-22 09:49:12 +0530891static int bxt_calc_pll_link(struct drm_i915_private *dev_priv,
892 enum intel_dpll_id dpll)
893{
894 /* FIXME formula not available in bspec */
895 return 0;
896}
897
898static void bxt_ddi_clock_get(struct intel_encoder *encoder,
899 struct intel_crtc_state *pipe_config)
900{
901 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
902 enum port port = intel_ddi_get_encoder_port(encoder);
903 uint32_t dpll = port;
904
905 pipe_config->port_clock =
906 bxt_calc_pll_link(dev_priv, dpll);
907
908 if (pipe_config->has_dp_encoder)
909 pipe_config->base.adjusted_mode.crtc_clock =
910 intel_dotclock_calculate(pipe_config->port_clock,
911 &pipe_config->dp_m_n);
912 else
913 pipe_config->base.adjusted_mode.crtc_clock =
914 pipe_config->port_clock;
915}
916
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200917void intel_ddi_clock_get(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200918 struct intel_crtc_state *pipe_config)
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200919{
Damien Lespiau22606a12014-12-12 14:26:57 +0000920 struct drm_device *dev = encoder->base.dev;
921
922 if (INTEL_INFO(dev)->gen <= 8)
923 hsw_ddi_clock_get(encoder, pipe_config);
Satheeshakrishna M977bb382014-08-22 09:49:12 +0530924 else if (IS_SKYLAKE(dev))
Damien Lespiau22606a12014-12-12 14:26:57 +0000925 skl_ddi_clock_get(encoder, pipe_config);
Satheeshakrishna M977bb382014-08-22 09:49:12 +0530926 else if (IS_BROXTON(dev))
927 bxt_ddi_clock_get(encoder, pipe_config);
Daniel Vetter3d51278a2014-07-29 20:57:08 +0200928}
929
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100930static void
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100931hsw_ddi_calculate_wrpll(int clock /* in Hz */,
932 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
Damien Lespiau1c0b85c2013-05-10 14:01:51 +0100933{
934 uint64_t freq2k;
935 unsigned p, n2, r2;
936 struct wrpll_rnp best = { 0, 0, 0 };
937 unsigned budget;
938
939 freq2k = clock / 100;
940
941 budget = wrpll_get_budget_for_freq(clock);
942
943 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
944 * and directly pass the LC PLL to it. */
945 if (freq2k == 5400000) {
946 *n2_out = 2;
947 *p_out = 1;
948 *r2_out = 2;
949 return;
950 }
951
952 /*
953 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
954 * the WR PLL.
955 *
956 * We want R so that REF_MIN <= Ref <= REF_MAX.
957 * Injecting R2 = 2 * R gives:
958 * REF_MAX * r2 > LC_FREQ * 2 and
959 * REF_MIN * r2 < LC_FREQ * 2
960 *
961 * Which means the desired boundaries for r2 are:
962 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
963 *
964 */
965 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
966 r2 <= LC_FREQ * 2 / REF_MIN;
967 r2++) {
968
969 /*
970 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
971 *
972 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
973 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
974 * VCO_MAX * r2 > n2 * LC_FREQ and
975 * VCO_MIN * r2 < n2 * LC_FREQ)
976 *
977 * Which means the desired boundaries for n2 are:
978 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
979 */
980 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
981 n2 <= VCO_MAX * r2 / LC_FREQ;
982 n2++) {
983
984 for (p = P_MIN; p <= P_MAX; p += P_INC)
985 wrpll_update_rnp(freq2k, budget,
986 r2, n2, p, &best);
987 }
988 }
989
990 *n2_out = best.n2;
991 *p_out = best.p;
992 *r2_out = best.r2;
Paulo Zanoni6441ab52012-10-05 12:05:58 -0300993}
994
Damien Lespiau0220ab62014-07-29 18:06:22 +0100995static bool
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100996hsw_ddi_pll_select(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +0200997 struct intel_crtc_state *crtc_state,
Damien Lespiaud664c0c2014-07-29 18:06:23 +0100998 struct intel_encoder *intel_encoder,
999 int clock)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001000{
Damien Lespiaud664c0c2014-07-29 18:06:23 +01001001 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
Daniel Vettere0b01be2014-06-25 22:02:01 +03001002 struct intel_shared_dpll *pll;
Daniel Vetter716c2e52014-06-25 22:02:02 +03001003 uint32_t val;
Damien Lespiau1c0b85c2013-05-10 14:01:51 +01001004 unsigned p, n2, r2;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001005
Damien Lespiaud664c0c2014-07-29 18:06:23 +01001006 hsw_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001007
Daniel Vetter114fe482014-06-25 22:01:48 +03001008 val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001009 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
1010 WRPLL_DIVIDER_POST(p);
1011
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001012 crtc_state->dpll_hw_state.wrpll = val;
Daniel Vetter716c2e52014-06-25 22:02:02 +03001013
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001014 pll = intel_get_shared_dpll(intel_crtc, crtc_state);
Daniel Vetter716c2e52014-06-25 22:02:02 +03001015 if (pll == NULL) {
1016 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
1017 pipe_name(intel_crtc->pipe));
Paulo Zanoni06940012013-10-30 18:27:43 -02001018 return false;
1019 }
1020
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001021 crtc_state->ddi_pll_sel = PORT_CLK_SEL_WRPLL(pll->id);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001022 }
1023
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001024 return true;
1025}
1026
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001027struct skl_wrpll_params {
1028 uint32_t dco_fraction;
1029 uint32_t dco_integer;
1030 uint32_t qdiv_ratio;
1031 uint32_t qdiv_mode;
1032 uint32_t kdiv;
1033 uint32_t pdiv;
1034 uint32_t central_freq;
1035};
1036
1037static void
1038skl_ddi_calculate_wrpll(int clock /* in Hz */,
1039 struct skl_wrpll_params *wrpll_params)
1040{
1041 uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
Damien Lespiau21318cc2014-11-14 14:20:27 +00001042 uint64_t dco_central_freq[3] = {8400000000ULL,
1043 9000000000ULL,
1044 9600000000ULL};
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001045 uint32_t min_dco_deviation = 400;
1046 uint32_t min_dco_index = 3;
1047 uint32_t P0[4] = {1, 2, 3, 7};
1048 uint32_t P2[4] = {1, 2, 3, 5};
1049 bool found = false;
1050 uint32_t candidate_p = 0;
1051 uint32_t candidate_p0[3] = {0}, candidate_p1[3] = {0};
1052 uint32_t candidate_p2[3] = {0};
1053 uint32_t dco_central_freq_deviation[3];
1054 uint32_t i, P1, k, dco_count;
1055 bool retry_with_odd = false;
1056 uint64_t dco_freq;
1057
1058 /* Determine P0, P1 or P2 */
1059 for (dco_count = 0; dco_count < 3; dco_count++) {
1060 found = false;
1061 candidate_p =
1062 div64_u64(dco_central_freq[dco_count], afe_clock);
1063 if (retry_with_odd == false)
1064 candidate_p = (candidate_p % 2 == 0 ?
1065 candidate_p : candidate_p + 1);
1066
1067 for (P1 = 1; P1 < candidate_p; P1++) {
1068 for (i = 0; i < 4; i++) {
1069 if (!(P0[i] != 1 || P1 == 1))
1070 continue;
1071
1072 for (k = 0; k < 4; k++) {
1073 if (P1 != 1 && P2[k] != 2)
1074 continue;
1075
1076 if (candidate_p == P0[i] * P1 * P2[k]) {
1077 /* Found possible P0, P1, P2 */
1078 found = true;
1079 candidate_p0[dco_count] = P0[i];
1080 candidate_p1[dco_count] = P1;
1081 candidate_p2[dco_count] = P2[k];
1082 goto found;
1083 }
1084
1085 }
1086 }
1087 }
1088
1089found:
1090 if (found) {
1091 dco_central_freq_deviation[dco_count] =
1092 div64_u64(10000 *
1093 abs_diff((candidate_p * afe_clock),
1094 dco_central_freq[dco_count]),
1095 dco_central_freq[dco_count]);
1096
1097 if (dco_central_freq_deviation[dco_count] <
1098 min_dco_deviation) {
1099 min_dco_deviation =
1100 dco_central_freq_deviation[dco_count];
1101 min_dco_index = dco_count;
1102 }
1103 }
1104
1105 if (min_dco_index > 2 && dco_count == 2) {
1106 retry_with_odd = true;
1107 dco_count = 0;
1108 }
1109 }
1110
1111 if (min_dco_index > 2) {
1112 WARN(1, "No valid values found for the given pixel clock\n");
1113 } else {
1114 wrpll_params->central_freq = dco_central_freq[min_dco_index];
1115
1116 switch (dco_central_freq[min_dco_index]) {
Damien Lespiau21318cc2014-11-14 14:20:27 +00001117 case 9600000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001118 wrpll_params->central_freq = 0;
1119 break;
Damien Lespiau21318cc2014-11-14 14:20:27 +00001120 case 9000000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001121 wrpll_params->central_freq = 1;
1122 break;
Damien Lespiau21318cc2014-11-14 14:20:27 +00001123 case 8400000000ULL:
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001124 wrpll_params->central_freq = 3;
1125 }
1126
1127 switch (candidate_p0[min_dco_index]) {
1128 case 1:
1129 wrpll_params->pdiv = 0;
1130 break;
1131 case 2:
1132 wrpll_params->pdiv = 1;
1133 break;
1134 case 3:
1135 wrpll_params->pdiv = 2;
1136 break;
1137 case 7:
1138 wrpll_params->pdiv = 4;
1139 break;
1140 default:
1141 WARN(1, "Incorrect PDiv\n");
1142 }
1143
1144 switch (candidate_p2[min_dco_index]) {
1145 case 5:
1146 wrpll_params->kdiv = 0;
1147 break;
1148 case 2:
1149 wrpll_params->kdiv = 1;
1150 break;
1151 case 3:
1152 wrpll_params->kdiv = 2;
1153 break;
1154 case 1:
1155 wrpll_params->kdiv = 3;
1156 break;
1157 default:
1158 WARN(1, "Incorrect KDiv\n");
1159 }
1160
1161 wrpll_params->qdiv_ratio = candidate_p1[min_dco_index];
1162 wrpll_params->qdiv_mode =
1163 (wrpll_params->qdiv_ratio == 1) ? 0 : 1;
1164
1165 dco_freq = candidate_p0[min_dco_index] *
1166 candidate_p1[min_dco_index] *
1167 candidate_p2[min_dco_index] * afe_clock;
1168
1169 /*
1170 * Intermediate values are in Hz.
1171 * Divide by MHz to match bsepc
1172 */
1173 wrpll_params->dco_integer = div_u64(dco_freq, (24 * MHz(1)));
1174 wrpll_params->dco_fraction =
1175 div_u64(((div_u64(dco_freq, 24) -
1176 wrpll_params->dco_integer * MHz(1)) * 0x8000), MHz(1));
1177
1178 }
1179}
1180
1181
1182static bool
1183skl_ddi_pll_select(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001184 struct intel_crtc_state *crtc_state,
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001185 struct intel_encoder *intel_encoder,
1186 int clock)
1187{
1188 struct intel_shared_dpll *pll;
1189 uint32_t ctrl1, cfgcr1, cfgcr2;
1190
1191 /*
1192 * See comment in intel_dpll_hw_state to understand why we always use 0
1193 * as the DPLL id in this function.
1194 */
1195
1196 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1197
1198 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
1199 struct skl_wrpll_params wrpll_params = { 0, };
1200
1201 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1202
1203 skl_ddi_calculate_wrpll(clock * 1000, &wrpll_params);
1204
1205 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1206 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1207 wrpll_params.dco_integer;
1208
1209 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1210 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1211 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1212 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1213 wrpll_params.central_freq;
1214 } else if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
1215 struct drm_encoder *encoder = &intel_encoder->base;
1216 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1217
1218 switch (intel_dp->link_bw) {
1219 case DP_LINK_BW_1_62:
1220 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_810, 0);
1221 break;
1222 case DP_LINK_BW_2_7:
1223 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_1350, 0);
1224 break;
1225 case DP_LINK_BW_5_4:
1226 ctrl1 |= DPLL_CRTL1_LINK_RATE(DPLL_CRTL1_LINK_RATE_2700, 0);
1227 break;
1228 }
1229
1230 cfgcr1 = cfgcr2 = 0;
1231 } else /* eDP */
1232 return true;
1233
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001234 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1235 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1236 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001237
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001238 pll = intel_get_shared_dpll(intel_crtc, crtc_state);
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001239 if (pll == NULL) {
1240 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
1241 pipe_name(intel_crtc->pipe));
1242 return false;
1243 }
1244
1245 /* shared DPLL id 0 is DPLL 1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001246 crtc_state->ddi_pll_sel = pll->id + 1;
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001247
1248 return true;
1249}
Damien Lespiau0220ab62014-07-29 18:06:22 +01001250
Satheeshakrishna Md683f3b2014-08-22 09:49:08 +05301251/* bxt clock parameters */
1252struct bxt_clk_div {
1253 uint32_t p1;
1254 uint32_t p2;
1255 uint32_t m2_int;
1256 uint32_t m2_frac;
1257 bool m2_frac_en;
1258 uint32_t n;
1259 uint32_t prop_coef;
1260 uint32_t int_coef;
1261 uint32_t gain_ctl;
1262 uint32_t targ_cnt;
1263 uint32_t lanestagger;
1264};
1265
1266/* pre-calculated values for DP linkrates */
1267static struct bxt_clk_div bxt_dp_clk_val[7] = {
1268 /* 162 */ {4, 2, 32, 1677722, 1, 1, 5, 11, 2, 9, 0xd},
1269 /* 270 */ {4, 1, 27, 0, 0, 1, 3, 8, 1, 9, 0xd},
1270 /* 540 */ {2, 1, 27, 0, 0, 1, 3, 8, 1, 9, 0x18},
1271 /* 216 */ {3, 2, 32, 1677722, 1, 1, 5, 11, 2, 9, 0xd},
1272 /* 243 */ {4, 1, 24, 1258291, 1, 1, 5, 11, 2, 9, 0xd},
1273 /* 324 */ {4, 1, 32, 1677722, 1, 1, 5, 11, 2, 9, 0xd},
1274 /* 432 */ {3, 1, 32, 1677722, 1, 1, 5, 11, 2, 9, 0x18}
1275};
1276
1277static bool
1278bxt_ddi_pll_select(struct intel_crtc *intel_crtc,
1279 struct intel_crtc_state *crtc_state,
1280 struct intel_encoder *intel_encoder,
1281 int clock)
1282{
1283 struct intel_shared_dpll *pll;
1284 struct bxt_clk_div clk_div = {0};
1285
1286 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
1287 intel_clock_t best_clock;
1288
1289 /* Calculate HDMI div */
1290 /*
1291 * FIXME: tie the following calculation into
1292 * i9xx_crtc_compute_clock
1293 */
1294 if (!bxt_find_best_dpll(crtc_state, clock, &best_clock)) {
1295 DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
1296 clock, pipe_name(intel_crtc->pipe));
1297 return false;
1298 }
1299
1300 clk_div.p1 = best_clock.p1;
1301 clk_div.p2 = best_clock.p2;
1302 WARN_ON(best_clock.m1 != 2);
1303 clk_div.n = best_clock.n;
1304 clk_div.m2_int = best_clock.m2 >> 22;
1305 clk_div.m2_frac = best_clock.m2 & ((1 << 22) - 1);
1306 clk_div.m2_frac_en = clk_div.m2_frac != 0;
1307
1308 /* FIXME: set coef, gain, targcnt based on freq band */
1309 clk_div.prop_coef = 5;
1310 clk_div.int_coef = 11;
1311 clk_div.gain_ctl = 2;
1312 clk_div.targ_cnt = 9;
1313 if (clock > 270000)
1314 clk_div.lanestagger = 0x18;
1315 else if (clock > 135000)
1316 clk_div.lanestagger = 0x0d;
1317 else if (clock > 67000)
1318 clk_div.lanestagger = 0x07;
1319 else if (clock > 33000)
1320 clk_div.lanestagger = 0x04;
1321 else
1322 clk_div.lanestagger = 0x02;
1323 } else if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
1324 intel_encoder->type == INTEL_OUTPUT_EDP) {
1325 struct drm_encoder *encoder = &intel_encoder->base;
1326 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1327
1328 switch (intel_dp->link_bw) {
1329 case DP_LINK_BW_1_62:
1330 clk_div = bxt_dp_clk_val[0];
1331 break;
1332 case DP_LINK_BW_2_7:
1333 clk_div = bxt_dp_clk_val[1];
1334 break;
1335 case DP_LINK_BW_5_4:
1336 clk_div = bxt_dp_clk_val[2];
1337 break;
1338 default:
1339 clk_div = bxt_dp_clk_val[0];
1340 DRM_ERROR("Unknown link rate\n");
1341 }
1342 }
1343
1344 crtc_state->dpll_hw_state.ebb0 =
1345 PORT_PLL_P1(clk_div.p1) | PORT_PLL_P2(clk_div.p2);
1346 crtc_state->dpll_hw_state.pll0 = clk_div.m2_int;
1347 crtc_state->dpll_hw_state.pll1 = PORT_PLL_N(clk_div.n);
1348 crtc_state->dpll_hw_state.pll2 = clk_div.m2_frac;
1349
1350 if (clk_div.m2_frac_en)
1351 crtc_state->dpll_hw_state.pll3 =
1352 PORT_PLL_M2_FRAC_ENABLE;
1353
1354 crtc_state->dpll_hw_state.pll6 =
1355 clk_div.prop_coef | PORT_PLL_INT_COEFF(clk_div.int_coef);
1356 crtc_state->dpll_hw_state.pll6 |=
1357 PORT_PLL_GAIN_CTL(clk_div.gain_ctl);
1358
1359 crtc_state->dpll_hw_state.pll8 = clk_div.targ_cnt;
1360
1361 crtc_state->dpll_hw_state.pcsdw12 =
1362 LANESTAGGER_STRAP_OVRD | clk_div.lanestagger;
1363
1364 pll = intel_get_shared_dpll(intel_crtc, crtc_state);
1365 if (pll == NULL) {
1366 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
1367 pipe_name(intel_crtc->pipe));
1368 return false;
1369 }
1370
1371 /* shared DPLL id 0 is DPLL A */
1372 crtc_state->ddi_pll_sel = pll->id;
1373
1374 return true;
1375}
1376
Damien Lespiau0220ab62014-07-29 18:06:22 +01001377/*
1378 * Tries to find a *shared* PLL for the CRTC and store it in
1379 * intel_crtc->ddi_pll_sel.
1380 *
1381 * For private DPLLs, compute_config() should do the selection for us. This
1382 * function should be folded into compute_config() eventually.
1383 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001384bool intel_ddi_pll_select(struct intel_crtc *intel_crtc,
1385 struct intel_crtc_state *crtc_state)
Damien Lespiau0220ab62014-07-29 18:06:22 +01001386{
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001387 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02001388 struct intel_encoder *intel_encoder =
Ander Conselvan de Oliveira3165c072015-03-20 16:18:12 +02001389 intel_ddi_get_crtc_new_encoder(crtc_state);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001390 int clock = crtc_state->port_clock;
Damien Lespiau0220ab62014-07-29 18:06:22 +01001391
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001392 if (IS_SKYLAKE(dev))
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001393 return skl_ddi_pll_select(intel_crtc, crtc_state,
1394 intel_encoder, clock);
Satheeshakrishna Md683f3b2014-08-22 09:49:08 +05301395 else if (IS_BROXTON(dev))
1396 return bxt_ddi_pll_select(intel_crtc, crtc_state,
1397 intel_encoder, clock);
Satheeshakrishna M82d35432014-11-13 14:55:20 +00001398 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02001399 return hsw_ddi_pll_select(intel_crtc, crtc_state,
1400 intel_encoder, clock);
Damien Lespiau0220ab62014-07-29 18:06:22 +01001401}
1402
Paulo Zanonidae84792012-10-15 15:51:30 -03001403void intel_ddi_set_pipe_settings(struct drm_crtc *crtc)
1404{
1405 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1407 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001408 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonidae84792012-10-15 15:51:30 -03001409 int type = intel_encoder->type;
1410 uint32_t temp;
1411
Dave Airlie0e32b392014-05-02 14:02:48 +10001412 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP || type == INTEL_OUTPUT_DP_MST) {
Paulo Zanonic9809792012-10-23 18:30:00 -02001413 temp = TRANS_MSA_SYNC_CLK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001414 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonidae84792012-10-15 15:51:30 -03001415 case 18:
Paulo Zanonic9809792012-10-23 18:30:00 -02001416 temp |= TRANS_MSA_6_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001417 break;
1418 case 24:
Paulo Zanonic9809792012-10-23 18:30:00 -02001419 temp |= TRANS_MSA_8_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001420 break;
1421 case 30:
Paulo Zanonic9809792012-10-23 18:30:00 -02001422 temp |= TRANS_MSA_10_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001423 break;
1424 case 36:
Paulo Zanonic9809792012-10-23 18:30:00 -02001425 temp |= TRANS_MSA_12_BPC;
Paulo Zanonidae84792012-10-15 15:51:30 -03001426 break;
1427 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01001428 BUG();
Paulo Zanonidae84792012-10-15 15:51:30 -03001429 }
Paulo Zanonic9809792012-10-23 18:30:00 -02001430 I915_WRITE(TRANS_MSA_MISC(cpu_transcoder), temp);
Paulo Zanonidae84792012-10-15 15:51:30 -03001431 }
1432}
1433
Dave Airlie0e32b392014-05-02 14:02:48 +10001434void intel_ddi_set_vc_payload_alloc(struct drm_crtc *crtc, bool state)
1435{
1436 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1437 struct drm_device *dev = crtc->dev;
1438 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001439 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Dave Airlie0e32b392014-05-02 14:02:48 +10001440 uint32_t temp;
1441 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1442 if (state == true)
1443 temp |= TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1444 else
1445 temp &= ~TRANS_DDI_DP_VC_PAYLOAD_ALLOC;
1446 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
1447}
1448
Damien Lespiau8228c252013-03-07 15:30:27 +00001449void intel_ddi_enable_transcoder_func(struct drm_crtc *crtc)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001450{
1451 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1452 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
Paulo Zanoni7739c332012-10-15 15:51:29 -03001453 struct drm_encoder *encoder = &intel_encoder->base;
Paulo Zanonic7670b12013-11-02 21:07:37 -07001454 struct drm_device *dev = crtc->dev;
1455 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001456 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001457 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001458 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni7739c332012-10-15 15:51:29 -03001459 int type = intel_encoder->type;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001460 uint32_t temp;
1461
Paulo Zanoniad80a812012-10-24 16:06:19 -02001462 /* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
1463 temp = TRANS_DDI_FUNC_ENABLE;
Paulo Zanoni174edf12012-10-26 19:05:50 -02001464 temp |= TRANS_DDI_SELECT_PORT(port);
Paulo Zanonidfcef252012-08-08 14:15:29 -03001465
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001466 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonidfcef252012-08-08 14:15:29 -03001467 case 18:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001468 temp |= TRANS_DDI_BPC_6;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001469 break;
1470 case 24:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001471 temp |= TRANS_DDI_BPC_8;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001472 break;
1473 case 30:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001474 temp |= TRANS_DDI_BPC_10;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001475 break;
1476 case 36:
Paulo Zanoniad80a812012-10-24 16:06:19 -02001477 temp |= TRANS_DDI_BPC_12;
Paulo Zanonidfcef252012-08-08 14:15:29 -03001478 break;
1479 default:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01001480 BUG();
Paulo Zanonidfcef252012-08-08 14:15:29 -03001481 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001482
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001483 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001484 temp |= TRANS_DDI_PVSYNC;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001485 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001486 temp |= TRANS_DDI_PHSYNC;
Paulo Zanonif63eb7c42012-08-08 14:15:28 -03001487
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -02001488 if (cpu_transcoder == TRANSCODER_EDP) {
1489 switch (pipe) {
1490 case PIPE_A:
Paulo Zanonic7670b12013-11-02 21:07:37 -07001491 /* On Haswell, can only use the always-on power well for
1492 * eDP when not using the panel fitter, and when not
1493 * using motion blur mitigation (which we don't
1494 * support). */
Daniel Vetterfabf6e52014-05-29 14:10:22 +02001495 if (IS_HASWELL(dev) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001496 (intel_crtc->config->pch_pfit.enabled ||
1497 intel_crtc->config->pch_pfit.force_thru))
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02001498 temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
1499 else
1500 temp |= TRANS_DDI_EDP_INPUT_A_ON;
Paulo Zanonie6f0bfc2012-10-23 18:30:04 -02001501 break;
1502 case PIPE_B:
1503 temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
1504 break;
1505 case PIPE_C:
1506 temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
1507 break;
1508 default:
1509 BUG();
1510 break;
1511 }
1512 }
1513
Paulo Zanoni7739c332012-10-15 15:51:29 -03001514 if (type == INTEL_OUTPUT_HDMI) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001515 if (intel_crtc->config->has_hdmi_sink)
Paulo Zanoniad80a812012-10-24 16:06:19 -02001516 temp |= TRANS_DDI_MODE_SELECT_HDMI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001517 else
Paulo Zanoniad80a812012-10-24 16:06:19 -02001518 temp |= TRANS_DDI_MODE_SELECT_DVI;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001519
Paulo Zanoni7739c332012-10-15 15:51:29 -03001520 } else if (type == INTEL_OUTPUT_ANALOG) {
Paulo Zanoniad80a812012-10-24 16:06:19 -02001521 temp |= TRANS_DDI_MODE_SELECT_FDI;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001522 temp |= (intel_crtc->config->fdi_lanes - 1) << 1;
Paulo Zanoni7739c332012-10-15 15:51:29 -03001523
1524 } else if (type == INTEL_OUTPUT_DISPLAYPORT ||
1525 type == INTEL_OUTPUT_EDP) {
1526 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1527
Dave Airlie0e32b392014-05-02 14:02:48 +10001528 if (intel_dp->is_mst) {
1529 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1530 } else
1531 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
1532
1533 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
1534 } else if (type == INTEL_OUTPUT_DP_MST) {
1535 struct intel_dp *intel_dp = &enc_to_mst(encoder)->primary->dp;
1536
1537 if (intel_dp->is_mst) {
1538 temp |= TRANS_DDI_MODE_SELECT_DP_MST;
1539 } else
1540 temp |= TRANS_DDI_MODE_SELECT_DP_SST;
Paulo Zanoni7739c332012-10-15 15:51:29 -03001541
Daniel Vetter17aa6be2013-04-30 14:01:40 +02001542 temp |= DDI_PORT_WIDTH(intel_dp->lane_count);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001543 } else {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001544 WARN(1, "Invalid encoder type %d for pipe %c\n",
1545 intel_encoder->type, pipe_name(pipe));
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001546 }
1547
Paulo Zanoniad80a812012-10-24 16:06:19 -02001548 I915_WRITE(TRANS_DDI_FUNC_CTL(cpu_transcoder), temp);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001549}
1550
Paulo Zanoniad80a812012-10-24 16:06:19 -02001551void intel_ddi_disable_transcoder_func(struct drm_i915_private *dev_priv,
1552 enum transcoder cpu_transcoder)
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001553{
Paulo Zanoniad80a812012-10-24 16:06:19 -02001554 uint32_t reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001555 uint32_t val = I915_READ(reg);
1556
Dave Airlie0e32b392014-05-02 14:02:48 +10001557 val &= ~(TRANS_DDI_FUNC_ENABLE | TRANS_DDI_PORT_MASK | TRANS_DDI_DP_VC_PAYLOAD_ALLOC);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001558 val |= TRANS_DDI_PORT_NONE;
Paulo Zanoni8d9ddbc2012-10-05 12:05:53 -03001559 I915_WRITE(reg, val);
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001560}
1561
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001562bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
1563{
1564 struct drm_device *dev = intel_connector->base.dev;
1565 struct drm_i915_private *dev_priv = dev->dev_private;
1566 struct intel_encoder *intel_encoder = intel_connector->encoder;
1567 int type = intel_connector->base.connector_type;
1568 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1569 enum pipe pipe = 0;
1570 enum transcoder cpu_transcoder;
Paulo Zanoni882244a2014-04-01 14:55:12 -03001571 enum intel_display_power_domain power_domain;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001572 uint32_t tmp;
1573
Paulo Zanoni882244a2014-04-01 14:55:12 -03001574 power_domain = intel_display_port_power_domain(intel_encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001575 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Paulo Zanoni882244a2014-04-01 14:55:12 -03001576 return false;
1577
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001578 if (!intel_encoder->get_hw_state(intel_encoder, &pipe))
1579 return false;
1580
1581 if (port == PORT_A)
1582 cpu_transcoder = TRANSCODER_EDP;
1583 else
Daniel Vetter1a240d42012-11-29 22:18:51 +01001584 cpu_transcoder = (enum transcoder) pipe;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001585
1586 tmp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
1587
1588 switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
1589 case TRANS_DDI_MODE_SELECT_HDMI:
1590 case TRANS_DDI_MODE_SELECT_DVI:
1591 return (type == DRM_MODE_CONNECTOR_HDMIA);
1592
1593 case TRANS_DDI_MODE_SELECT_DP_SST:
1594 if (type == DRM_MODE_CONNECTOR_eDP)
1595 return true;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001596 return (type == DRM_MODE_CONNECTOR_DisplayPort);
Dave Airlie0e32b392014-05-02 14:02:48 +10001597 case TRANS_DDI_MODE_SELECT_DP_MST:
1598 /* if the transcoder is in MST state then
1599 * connector isn't connected */
1600 return false;
Paulo Zanonibcbc8892012-10-26 19:05:51 -02001601
1602 case TRANS_DDI_MODE_SELECT_FDI:
1603 return (type == DRM_MODE_CONNECTOR_VGA);
1604
1605 default:
1606 return false;
1607 }
1608}
1609
Daniel Vetter85234cd2012-07-02 13:27:29 +02001610bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
1611 enum pipe *pipe)
1612{
1613 struct drm_device *dev = encoder->base.dev;
1614 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonife43d3f2012-10-15 15:51:39 -03001615 enum port port = intel_ddi_get_encoder_port(encoder);
Imre Deak6d129be2014-03-05 16:20:54 +02001616 enum intel_display_power_domain power_domain;
Daniel Vetter85234cd2012-07-02 13:27:29 +02001617 u32 tmp;
1618 int i;
1619
Imre Deak6d129be2014-03-05 16:20:54 +02001620 power_domain = intel_display_port_power_domain(encoder);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001621 if (!intel_display_power_is_enabled(dev_priv, power_domain))
Imre Deak6d129be2014-03-05 16:20:54 +02001622 return false;
1623
Paulo Zanonife43d3f2012-10-15 15:51:39 -03001624 tmp = I915_READ(DDI_BUF_CTL(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001625
1626 if (!(tmp & DDI_BUF_CTL_ENABLE))
1627 return false;
1628
Paulo Zanoniad80a812012-10-24 16:06:19 -02001629 if (port == PORT_A) {
1630 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001631
Paulo Zanoniad80a812012-10-24 16:06:19 -02001632 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1633 case TRANS_DDI_EDP_INPUT_A_ON:
1634 case TRANS_DDI_EDP_INPUT_A_ONOFF:
1635 *pipe = PIPE_A;
1636 break;
1637 case TRANS_DDI_EDP_INPUT_B_ONOFF:
1638 *pipe = PIPE_B;
1639 break;
1640 case TRANS_DDI_EDP_INPUT_C_ONOFF:
1641 *pipe = PIPE_C;
1642 break;
1643 }
1644
1645 return true;
1646 } else {
1647 for (i = TRANSCODER_A; i <= TRANSCODER_C; i++) {
1648 tmp = I915_READ(TRANS_DDI_FUNC_CTL(i));
1649
1650 if ((tmp & TRANS_DDI_PORT_MASK)
1651 == TRANS_DDI_SELECT_PORT(port)) {
Dave Airlie0e32b392014-05-02 14:02:48 +10001652 if ((tmp & TRANS_DDI_MODE_SELECT_MASK) == TRANS_DDI_MODE_SELECT_DP_MST)
1653 return false;
1654
Paulo Zanoniad80a812012-10-24 16:06:19 -02001655 *pipe = i;
1656 return true;
1657 }
Daniel Vetter85234cd2012-07-02 13:27:29 +02001658 }
1659 }
1660
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03001661 DRM_DEBUG_KMS("No pipe for ddi port %c found\n", port_name(port));
Daniel Vetter85234cd2012-07-02 13:27:29 +02001662
Jesse Barnes22f9fe52013-04-02 10:03:55 -07001663 return false;
Daniel Vetter85234cd2012-07-02 13:27:29 +02001664}
1665
Paulo Zanonifc914632012-10-05 12:05:54 -03001666void intel_ddi_enable_pipe_clock(struct intel_crtc *intel_crtc)
1667{
1668 struct drm_crtc *crtc = &intel_crtc->base;
1669 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
1670 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
1671 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001672 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -03001673
Paulo Zanonibb523fc2012-10-23 18:29:56 -02001674 if (cpu_transcoder != TRANSCODER_EDP)
1675 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1676 TRANS_CLK_SEL_PORT(port));
Paulo Zanonifc914632012-10-05 12:05:54 -03001677}
1678
1679void intel_ddi_disable_pipe_clock(struct intel_crtc *intel_crtc)
1680{
1681 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001682 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanonifc914632012-10-05 12:05:54 -03001683
Paulo Zanonibb523fc2012-10-23 18:29:56 -02001684 if (cpu_transcoder != TRANSCODER_EDP)
1685 I915_WRITE(TRANS_CLK_SEL(cpu_transcoder),
1686 TRANS_CLK_SEL_DISABLED);
Paulo Zanonifc914632012-10-05 12:05:54 -03001687}
1688
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001689static void intel_ddi_pre_enable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001690{
Paulo Zanonic19b0662012-10-15 15:51:41 -03001691 struct drm_encoder *encoder = &intel_encoder->base;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001692 struct drm_device *dev = encoder->dev;
1693 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001694 struct intel_crtc *crtc = to_intel_crtc(encoder->crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001695 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001696 int type = intel_encoder->type;
1697
1698 if (type == INTEL_OUTPUT_EDP) {
1699 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Daniel Vetter4be73782014-01-17 14:39:48 +01001700 intel_edp_panel_on(intel_dp);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001701 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001702
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001703 if (IS_SKYLAKE(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001704 uint32_t dpll = crtc->config->ddi_pll_sel;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001705 uint32_t val;
1706
Damien Lespiau5416d872014-11-14 17:24:33 +00001707 /*
1708 * DPLL0 is used for eDP and is the only "private" DPLL (as
1709 * opposed to shared) on SKL
1710 */
1711 if (type == INTEL_OUTPUT_EDP) {
1712 WARN_ON(dpll != SKL_DPLL0);
1713
1714 val = I915_READ(DPLL_CTRL1);
1715
1716 val &= ~(DPLL_CTRL1_HDMI_MODE(dpll) |
1717 DPLL_CTRL1_SSC(dpll) |
1718 DPLL_CRTL1_LINK_RATE_MASK(dpll));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001719 val |= crtc->config->dpll_hw_state.ctrl1 << (dpll * 6);
Damien Lespiau5416d872014-11-14 17:24:33 +00001720
1721 I915_WRITE(DPLL_CTRL1, val);
1722 POSTING_READ(DPLL_CTRL1);
1723 }
1724
1725 /* DDI -> PLL mapping */
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001726 val = I915_READ(DPLL_CTRL2);
1727
1728 val &= ~(DPLL_CTRL2_DDI_CLK_OFF(port) |
1729 DPLL_CTRL2_DDI_CLK_SEL_MASK(port));
1730 val |= (DPLL_CTRL2_DDI_CLK_SEL(dpll, port) |
1731 DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
1732
1733 I915_WRITE(DPLL_CTRL2, val);
Damien Lespiau5416d872014-11-14 17:24:33 +00001734
Satheeshakrishna M1ab23382014-08-22 09:49:06 +05301735 } else if (INTEL_INFO(dev)->gen < 9) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001736 WARN_ON(crtc->config->ddi_pll_sel == PORT_CLK_SEL_NONE);
1737 I915_WRITE(PORT_CLK_SEL(port), crtc->config->ddi_pll_sel);
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001738 }
Paulo Zanonic19b0662012-10-15 15:51:41 -03001739
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001740 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Paulo Zanonic19b0662012-10-15 15:51:41 -03001741 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001742
Dave Airlie44905a272014-05-02 13:36:43 +10001743 intel_ddi_init_dp_buf_reg(intel_encoder);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001744
1745 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_ON);
1746 intel_dp_start_link_train(intel_dp);
1747 intel_dp_complete_link_train(intel_dp);
Vandana Kannan23f08d82014-11-13 14:55:22 +00001748 if (port != PORT_A || INTEL_INFO(dev)->gen >= 9)
Imre Deak3ab9c632013-05-03 12:57:41 +03001749 intel_dp_stop_link_train(intel_dp);
Daniel Vetter30cf6db2014-04-24 23:54:58 +02001750 } else if (type == INTEL_OUTPUT_HDMI) {
1751 struct intel_hdmi *intel_hdmi = enc_to_intel_hdmi(encoder);
1752
1753 intel_hdmi->set_infoframes(encoder,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001754 crtc->config->has_hdmi_sink,
1755 &crtc->config->base.adjusted_mode);
Paulo Zanonic19b0662012-10-15 15:51:41 -03001756 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001757}
1758
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001759static void intel_ddi_post_disable(struct intel_encoder *intel_encoder)
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001760{
1761 struct drm_encoder *encoder = &intel_encoder->base;
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001762 struct drm_device *dev = encoder->dev;
1763 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001764 enum port port = intel_ddi_get_encoder_port(intel_encoder);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001765 int type = intel_encoder->type;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001766 uint32_t val;
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001767 bool wait = false;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001768
1769 val = I915_READ(DDI_BUF_CTL(port));
1770 if (val & DDI_BUF_CTL_ENABLE) {
1771 val &= ~DDI_BUF_CTL_ENABLE;
1772 I915_WRITE(DDI_BUF_CTL(port), val);
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001773 wait = true;
Paulo Zanoni2886e932012-10-05 12:06:00 -03001774 }
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001775
Paulo Zanonia836bdf2012-10-15 15:51:32 -03001776 val = I915_READ(DP_TP_CTL(port));
1777 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
1778 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
1779 I915_WRITE(DP_TP_CTL(port), val);
1780
1781 if (wait)
1782 intel_wait_ddi_buf_idle(dev_priv, port);
1783
Jani Nikula76bb80e2013-11-15 15:29:57 +02001784 if (type == INTEL_OUTPUT_DISPLAYPORT || type == INTEL_OUTPUT_EDP) {
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001785 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
Jani Nikula76bb80e2013-11-15 15:29:57 +02001786 intel_dp_sink_dpms(intel_dp, DRM_MODE_DPMS_OFF);
Jani Nikula24f3e092014-03-17 16:43:36 +02001787 intel_edp_panel_vdd_on(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01001788 intel_edp_panel_off(intel_dp);
Paulo Zanoni82a4d9c2012-10-23 18:30:07 -02001789 }
1790
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001791 if (IS_SKYLAKE(dev))
1792 I915_WRITE(DPLL_CTRL2, (I915_READ(DPLL_CTRL2) |
1793 DPLL_CTRL2_DDI_CLK_OFF(port)));
Satheeshakrishna M1ab23382014-08-22 09:49:06 +05301794 else if (INTEL_INFO(dev)->gen < 9)
Satheeshakrishna Mefa80ad2014-11-13 14:55:19 +00001795 I915_WRITE(PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03001796}
1797
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001798static void intel_enable_ddi(struct intel_encoder *intel_encoder)
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001799{
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001800 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001801 struct drm_crtc *crtc = encoder->crtc;
1802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001803 struct drm_device *dev = encoder->dev;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001804 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001805 enum port port = intel_ddi_get_encoder_port(intel_encoder);
1806 int type = intel_encoder->type;
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001807
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001808 if (type == INTEL_OUTPUT_HDMI) {
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001809 struct intel_digital_port *intel_dig_port =
1810 enc_to_dig_port(encoder);
1811
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001812 /* In HDMI/DVI mode, the port width, and swing/emphasis values
1813 * are ignored so nothing special needs to be done besides
1814 * enabling the port.
1815 */
Damien Lespiau876a8cd2012-12-11 18:48:30 +00001816 I915_WRITE(DDI_BUF_CTL(port),
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07001817 intel_dig_port->saved_port_bits |
1818 DDI_BUF_CTL_ENABLE);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001819 } else if (type == INTEL_OUTPUT_EDP) {
1820 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1821
Vandana Kannan23f08d82014-11-13 14:55:22 +00001822 if (port == PORT_A && INTEL_INFO(dev)->gen < 9)
Imre Deak3ab9c632013-05-03 12:57:41 +03001823 intel_dp_stop_link_train(intel_dp);
1824
Daniel Vetter4be73782014-01-17 14:39:48 +01001825 intel_edp_backlight_on(intel_dp);
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -08001826 intel_psr_enable(intel_dp);
Vandana Kannanc3955782015-01-22 15:17:40 +05301827 intel_edp_drrs_enable(intel_dp);
Paulo Zanoni6547fef2012-10-15 15:51:40 -03001828 }
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001829
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001830 if (intel_crtc->config->has_audio) {
Paulo Zanonid45a0bf2014-05-21 17:29:31 -03001831 intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
Jani Nikula69bfe1a2014-10-27 16:26:50 +02001832 intel_audio_codec_enable(intel_encoder);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001833 }
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001834}
1835
Paulo Zanoni00c09d72012-10-26 19:05:52 -02001836static void intel_disable_ddi(struct intel_encoder *intel_encoder)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02001837{
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001838 struct drm_encoder *encoder = &intel_encoder->base;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001839 struct drm_crtc *crtc = encoder->crtc;
1840 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001841 int type = intel_encoder->type;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08001842 struct drm_device *dev = encoder->dev;
1843 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001844
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001845 if (intel_crtc->config->has_audio) {
Jani Nikula69bfe1a2014-10-27 16:26:50 +02001846 intel_audio_codec_disable(intel_encoder);
Paulo Zanonid45a0bf2014-05-21 17:29:31 -03001847 intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO);
1848 }
Paulo Zanoni2831d8422013-03-06 20:03:09 -03001849
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001850 if (type == INTEL_OUTPUT_EDP) {
1851 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1852
Vandana Kannanc3955782015-01-22 15:17:40 +05301853 intel_edp_drrs_disable(intel_dp);
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -08001854 intel_psr_disable(intel_dp);
Daniel Vetter4be73782014-01-17 14:39:48 +01001855 intel_edp_backlight_off(intel_dp);
Paulo Zanonid6c50ff2012-10-23 18:30:06 -02001856 }
Eugeni Dodonov72662e12012-05-09 15:37:31 -03001857}
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001858
Daniel Vettere0b01be2014-06-25 22:02:01 +03001859static void hsw_ddi_pll_enable(struct drm_i915_private *dev_priv,
1860 struct intel_shared_dpll *pll)
1861{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001862 I915_WRITE(WRPLL_CTL(pll->id), pll->config.hw_state.wrpll);
Daniel Vettere0b01be2014-06-25 22:02:01 +03001863 POSTING_READ(WRPLL_CTL(pll->id));
1864 udelay(20);
1865}
1866
Daniel Vetter12030432014-06-25 22:02:00 +03001867static void hsw_ddi_pll_disable(struct drm_i915_private *dev_priv,
1868 struct intel_shared_dpll *pll)
1869{
1870 uint32_t val;
1871
1872 val = I915_READ(WRPLL_CTL(pll->id));
Daniel Vetter12030432014-06-25 22:02:00 +03001873 I915_WRITE(WRPLL_CTL(pll->id), val & ~WRPLL_PLL_ENABLE);
1874 POSTING_READ(WRPLL_CTL(pll->id));
1875}
1876
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001877static bool hsw_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1878 struct intel_shared_dpll *pll,
1879 struct intel_dpll_hw_state *hw_state)
1880{
1881 uint32_t val;
1882
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001883 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001884 return false;
1885
1886 val = I915_READ(WRPLL_CTL(pll->id));
1887 hw_state->wrpll = val;
1888
1889 return val & WRPLL_PLL_ENABLE;
1890}
1891
Damien Lespiauca1381b2014-07-15 15:05:33 +01001892static const char * const hsw_ddi_pll_names[] = {
Daniel Vetter9cd86932014-06-25 22:01:57 +03001893 "WRPLL 1",
1894 "WRPLL 2",
1895};
1896
Damien Lespiau143b3072014-07-29 18:06:19 +01001897static void hsw_shared_dplls_init(struct drm_i915_private *dev_priv)
Paulo Zanoni79f689a2012-10-05 12:05:52 -03001898{
Daniel Vetter9cd86932014-06-25 22:01:57 +03001899 int i;
1900
Daniel Vetter716c2e52014-06-25 22:02:02 +03001901 dev_priv->num_shared_dpll = 2;
Daniel Vetter9cd86932014-06-25 22:01:57 +03001902
Daniel Vetter716c2e52014-06-25 22:02:02 +03001903 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter9cd86932014-06-25 22:01:57 +03001904 dev_priv->shared_dplls[i].id = i;
1905 dev_priv->shared_dplls[i].name = hsw_ddi_pll_names[i];
Daniel Vetter12030432014-06-25 22:02:00 +03001906 dev_priv->shared_dplls[i].disable = hsw_ddi_pll_disable;
Daniel Vettere0b01be2014-06-25 22:02:01 +03001907 dev_priv->shared_dplls[i].enable = hsw_ddi_pll_enable;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03001908 dev_priv->shared_dplls[i].get_hw_state =
1909 hsw_ddi_pll_get_hw_state;
Daniel Vetter9cd86932014-06-25 22:01:57 +03001910 }
Damien Lespiau143b3072014-07-29 18:06:19 +01001911}
1912
Satheeshakrishna Md1a2dc72014-11-13 14:55:18 +00001913static const char * const skl_ddi_pll_names[] = {
1914 "DPLL 1",
1915 "DPLL 2",
1916 "DPLL 3",
1917};
1918
1919struct skl_dpll_regs {
1920 u32 ctl, cfgcr1, cfgcr2;
1921};
1922
1923/* this array is indexed by the *shared* pll id */
1924static const struct skl_dpll_regs skl_dpll_regs[3] = {
1925 {
1926 /* DPLL 1 */
1927 .ctl = LCPLL2_CTL,
1928 .cfgcr1 = DPLL1_CFGCR1,
1929 .cfgcr2 = DPLL1_CFGCR2,
1930 },
1931 {
1932 /* DPLL 2 */
1933 .ctl = WRPLL_CTL1,
1934 .cfgcr1 = DPLL2_CFGCR1,
1935 .cfgcr2 = DPLL2_CFGCR2,
1936 },
1937 {
1938 /* DPLL 3 */
1939 .ctl = WRPLL_CTL2,
1940 .cfgcr1 = DPLL3_CFGCR1,
1941 .cfgcr2 = DPLL3_CFGCR2,
1942 },
1943};
1944
1945static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
1946 struct intel_shared_dpll *pll)
1947{
1948 uint32_t val;
1949 unsigned int dpll;
1950 const struct skl_dpll_regs *regs = skl_dpll_regs;
1951
1952 /* DPLL0 is not part of the shared DPLLs, so pll->id is 0 for DPLL1 */
1953 dpll = pll->id + 1;
1954
1955 val = I915_READ(DPLL_CTRL1);
1956
1957 val &= ~(DPLL_CTRL1_HDMI_MODE(dpll) | DPLL_CTRL1_SSC(dpll) |
1958 DPLL_CRTL1_LINK_RATE_MASK(dpll));
1959 val |= pll->config.hw_state.ctrl1 << (dpll * 6);
1960
1961 I915_WRITE(DPLL_CTRL1, val);
1962 POSTING_READ(DPLL_CTRL1);
1963
1964 I915_WRITE(regs[pll->id].cfgcr1, pll->config.hw_state.cfgcr1);
1965 I915_WRITE(regs[pll->id].cfgcr2, pll->config.hw_state.cfgcr2);
1966 POSTING_READ(regs[pll->id].cfgcr1);
1967 POSTING_READ(regs[pll->id].cfgcr2);
1968
1969 /* the enable bit is always bit 31 */
1970 I915_WRITE(regs[pll->id].ctl,
1971 I915_READ(regs[pll->id].ctl) | LCPLL_PLL_ENABLE);
1972
1973 if (wait_for(I915_READ(DPLL_STATUS) & DPLL_LOCK(dpll), 5))
1974 DRM_ERROR("DPLL %d not locked\n", dpll);
1975}
1976
1977static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
1978 struct intel_shared_dpll *pll)
1979{
1980 const struct skl_dpll_regs *regs = skl_dpll_regs;
1981
1982 /* the enable bit is always bit 31 */
1983 I915_WRITE(regs[pll->id].ctl,
1984 I915_READ(regs[pll->id].ctl) & ~LCPLL_PLL_ENABLE);
1985 POSTING_READ(regs[pll->id].ctl);
1986}
1987
1988static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1989 struct intel_shared_dpll *pll,
1990 struct intel_dpll_hw_state *hw_state)
1991{
1992 uint32_t val;
1993 unsigned int dpll;
1994 const struct skl_dpll_regs *regs = skl_dpll_regs;
1995
1996 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
1997 return false;
1998
1999 /* DPLL0 is not part of the shared DPLLs, so pll->id is 0 for DPLL1 */
2000 dpll = pll->id + 1;
2001
2002 val = I915_READ(regs[pll->id].ctl);
2003 if (!(val & LCPLL_PLL_ENABLE))
2004 return false;
2005
2006 val = I915_READ(DPLL_CTRL1);
2007 hw_state->ctrl1 = (val >> (dpll * 6)) & 0x3f;
2008
2009 /* avoid reading back stale values if HDMI mode is not enabled */
2010 if (val & DPLL_CTRL1_HDMI_MODE(dpll)) {
2011 hw_state->cfgcr1 = I915_READ(regs[pll->id].cfgcr1);
2012 hw_state->cfgcr2 = I915_READ(regs[pll->id].cfgcr2);
2013 }
2014
2015 return true;
2016}
2017
2018static void skl_shared_dplls_init(struct drm_i915_private *dev_priv)
2019{
2020 int i;
2021
2022 dev_priv->num_shared_dpll = 3;
2023
2024 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
2025 dev_priv->shared_dplls[i].id = i;
2026 dev_priv->shared_dplls[i].name = skl_ddi_pll_names[i];
2027 dev_priv->shared_dplls[i].disable = skl_ddi_pll_disable;
2028 dev_priv->shared_dplls[i].enable = skl_ddi_pll_enable;
2029 dev_priv->shared_dplls[i].get_hw_state =
2030 skl_ddi_pll_get_hw_state;
2031 }
2032}
2033
Vandana Kannan5c6706e2014-11-24 13:37:39 +05302034static void broxton_phy_init(struct drm_i915_private *dev_priv,
2035 enum dpio_phy phy)
2036{
2037 enum port port;
2038 uint32_t val;
2039
2040 val = I915_READ(BXT_P_CR_GT_DISP_PWRON);
2041 val |= GT_DISPLAY_POWER_ON(phy);
2042 I915_WRITE(BXT_P_CR_GT_DISP_PWRON, val);
2043
2044 /* Considering 10ms timeout until BSpec is updated */
2045 if (wait_for(I915_READ(BXT_PORT_CL1CM_DW0(phy)) & PHY_POWER_GOOD, 10))
2046 DRM_ERROR("timeout during PHY%d power on\n", phy);
2047
2048 for (port = (phy == DPIO_PHY0 ? PORT_B : PORT_A);
2049 port <= (phy == DPIO_PHY0 ? PORT_C : PORT_A); port++) {
2050 int lane;
2051
2052 for (lane = 0; lane < 4; lane++) {
2053 val = I915_READ(BXT_PORT_TX_DW14_LN(port, lane));
2054 /*
2055 * Note that on CHV this flag is called UPAR, but has
2056 * the same function.
2057 */
2058 val &= ~LATENCY_OPTIM;
2059 if (lane != 1)
2060 val |= LATENCY_OPTIM;
2061
2062 I915_WRITE(BXT_PORT_TX_DW14_LN(port, lane), val);
2063 }
2064 }
2065
2066 /* Program PLL Rcomp code offset */
2067 val = I915_READ(BXT_PORT_CL1CM_DW9(phy));
2068 val &= ~IREF0RC_OFFSET_MASK;
2069 val |= 0xE4 << IREF0RC_OFFSET_SHIFT;
2070 I915_WRITE(BXT_PORT_CL1CM_DW9(phy), val);
2071
2072 val = I915_READ(BXT_PORT_CL1CM_DW10(phy));
2073 val &= ~IREF1RC_OFFSET_MASK;
2074 val |= 0xE4 << IREF1RC_OFFSET_SHIFT;
2075 I915_WRITE(BXT_PORT_CL1CM_DW10(phy), val);
2076
2077 /* Program power gating */
2078 val = I915_READ(BXT_PORT_CL1CM_DW28(phy));
2079 val |= OCL1_POWER_DOWN_EN | DW28_OLDO_DYN_PWR_DOWN_EN |
2080 SUS_CLK_CONFIG;
2081 I915_WRITE(BXT_PORT_CL1CM_DW28(phy), val);
2082
2083 if (phy == DPIO_PHY0) {
2084 val = I915_READ(BXT_PORT_CL2CM_DW6_BC);
2085 val |= DW6_OLDO_DYN_PWR_DOWN_EN;
2086 I915_WRITE(BXT_PORT_CL2CM_DW6_BC, val);
2087 }
2088
2089 val = I915_READ(BXT_PORT_CL1CM_DW30(phy));
2090 val &= ~OCL2_LDOFUSE_PWR_DIS;
2091 /*
2092 * On PHY1 disable power on the second channel, since no port is
2093 * connected there. On PHY0 both channels have a port, so leave it
2094 * enabled.
2095 * TODO: port C is only connected on BXT-P, so on BXT0/1 we should
2096 * power down the second channel on PHY0 as well.
2097 */
2098 if (phy == DPIO_PHY1)
2099 val |= OCL2_LDOFUSE_PWR_DIS;
2100 I915_WRITE(BXT_PORT_CL1CM_DW30(phy), val);
2101
2102 if (phy == DPIO_PHY0) {
2103 uint32_t grc_code;
2104 /*
2105 * PHY0 isn't connected to an RCOMP resistor so copy over
2106 * the corresponding calibrated value from PHY1, and disable
2107 * the automatic calibration on PHY0.
2108 */
2109 if (wait_for(I915_READ(BXT_PORT_REF_DW3(DPIO_PHY1)) & GRC_DONE,
2110 10))
2111 DRM_ERROR("timeout waiting for PHY1 GRC\n");
2112
2113 val = I915_READ(BXT_PORT_REF_DW6(DPIO_PHY1));
2114 val = (val & GRC_CODE_MASK) >> GRC_CODE_SHIFT;
2115 grc_code = val << GRC_CODE_FAST_SHIFT |
2116 val << GRC_CODE_SLOW_SHIFT |
2117 val;
2118 I915_WRITE(BXT_PORT_REF_DW6(DPIO_PHY0), grc_code);
2119
2120 val = I915_READ(BXT_PORT_REF_DW8(DPIO_PHY0));
2121 val |= GRC_DIS | GRC_RDY_OVRD;
2122 I915_WRITE(BXT_PORT_REF_DW8(DPIO_PHY0), val);
2123 }
2124
2125 val = I915_READ(BXT_PHY_CTL_FAMILY(phy));
2126 val |= COMMON_RESET_DIS;
2127 I915_WRITE(BXT_PHY_CTL_FAMILY(phy), val);
2128}
2129
2130void broxton_ddi_phy_init(struct drm_device *dev)
2131{
2132 /* Enable PHY1 first since it provides Rcomp for PHY0 */
2133 broxton_phy_init(dev->dev_private, DPIO_PHY1);
2134 broxton_phy_init(dev->dev_private, DPIO_PHY0);
2135}
2136
2137static void broxton_phy_uninit(struct drm_i915_private *dev_priv,
2138 enum dpio_phy phy)
2139{
2140 uint32_t val;
2141
2142 val = I915_READ(BXT_PHY_CTL_FAMILY(phy));
2143 val &= ~COMMON_RESET_DIS;
2144 I915_WRITE(BXT_PHY_CTL_FAMILY(phy), val);
2145}
2146
2147void broxton_ddi_phy_uninit(struct drm_device *dev)
2148{
2149 struct drm_i915_private *dev_priv = dev->dev_private;
2150
2151 broxton_phy_uninit(dev_priv, DPIO_PHY1);
2152 broxton_phy_uninit(dev_priv, DPIO_PHY0);
2153
2154 /* FIXME: do this in broxton_phy_uninit per phy */
2155 I915_WRITE(BXT_P_CR_GT_DISP_PWRON, 0);
2156}
2157
Satheeshakrishna Mdfb82402014-08-22 09:49:09 +05302158static const char * const bxt_ddi_pll_names[] = {
2159 "PORT PLL A",
2160 "PORT PLL B",
2161 "PORT PLL C",
2162};
2163
2164static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
2165 struct intel_shared_dpll *pll)
2166{
2167 uint32_t temp;
2168 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
2169
2170 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
2171 temp &= ~PORT_PLL_REF_SEL;
2172 /* Non-SSC reference */
2173 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
2174
2175 /* Disable 10 bit clock */
2176 temp = I915_READ(BXT_PORT_PLL_EBB_4(port));
2177 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
2178 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
2179
2180 /* Write P1 & P2 */
2181 temp = I915_READ(BXT_PORT_PLL_EBB_0(port));
2182 temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
2183 temp |= pll->config.hw_state.ebb0;
2184 I915_WRITE(BXT_PORT_PLL_EBB_0(port), temp);
2185
2186 /* Write M2 integer */
2187 temp = I915_READ(BXT_PORT_PLL(port, 0));
2188 temp &= ~PORT_PLL_M2_MASK;
2189 temp |= pll->config.hw_state.pll0;
2190 I915_WRITE(BXT_PORT_PLL(port, 0), temp);
2191
2192 /* Write N */
2193 temp = I915_READ(BXT_PORT_PLL(port, 1));
2194 temp &= ~PORT_PLL_N_MASK;
2195 temp |= pll->config.hw_state.pll1;
2196 I915_WRITE(BXT_PORT_PLL(port, 1), temp);
2197
2198 /* Write M2 fraction */
2199 temp = I915_READ(BXT_PORT_PLL(port, 2));
2200 temp &= ~PORT_PLL_M2_FRAC_MASK;
2201 temp |= pll->config.hw_state.pll2;
2202 I915_WRITE(BXT_PORT_PLL(port, 2), temp);
2203
2204 /* Write M2 fraction enable */
2205 temp = I915_READ(BXT_PORT_PLL(port, 3));
2206 temp &= ~PORT_PLL_M2_FRAC_ENABLE;
2207 temp |= pll->config.hw_state.pll3;
2208 I915_WRITE(BXT_PORT_PLL(port, 3), temp);
2209
2210 /* Write coeff */
2211 temp = I915_READ(BXT_PORT_PLL(port, 6));
2212 temp &= ~PORT_PLL_PROP_COEFF_MASK;
2213 temp &= ~PORT_PLL_INT_COEFF_MASK;
2214 temp &= ~PORT_PLL_GAIN_CTL_MASK;
2215 temp |= pll->config.hw_state.pll6;
2216 I915_WRITE(BXT_PORT_PLL(port, 6), temp);
2217
2218 /* Write calibration val */
2219 temp = I915_READ(BXT_PORT_PLL(port, 8));
2220 temp &= ~PORT_PLL_TARGET_CNT_MASK;
2221 temp |= pll->config.hw_state.pll8;
2222 I915_WRITE(BXT_PORT_PLL(port, 8), temp);
2223
2224 /*
2225 * FIXME: program PORT_PLL_9/i_lockthresh according to the latest
2226 * specification update.
2227 */
2228
2229 /* Recalibrate with new settings */
2230 temp = I915_READ(BXT_PORT_PLL_EBB_4(port));
2231 temp |= PORT_PLL_RECALIBRATE;
2232 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
2233 /* Enable 10 bit clock */
2234 temp |= PORT_PLL_10BIT_CLK_ENABLE;
2235 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
2236
2237 /* Enable PLL */
2238 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
2239 temp |= PORT_PLL_ENABLE;
2240 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
2241 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
2242
2243 if (wait_for_atomic_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) &
2244 PORT_PLL_LOCK), 200))
2245 DRM_ERROR("PLL %d not locked\n", port);
2246
2247 /*
2248 * While we write to the group register to program all lanes at once we
2249 * can read only lane registers and we pick lanes 0/1 for that.
2250 */
2251 temp = I915_READ(BXT_PORT_PCS_DW12_LN01(port));
2252 temp &= ~LANE_STAGGER_MASK;
2253 temp &= ~LANESTAGGER_STRAP_OVRD;
2254 temp |= pll->config.hw_state.pcsdw12;
2255 I915_WRITE(BXT_PORT_PCS_DW12_GRP(port), temp);
2256}
2257
2258static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
2259 struct intel_shared_dpll *pll)
2260{
2261 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
2262 uint32_t temp;
2263
2264 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
2265 temp &= ~PORT_PLL_ENABLE;
2266 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
2267 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
2268}
2269
2270static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
2271 struct intel_shared_dpll *pll,
2272 struct intel_dpll_hw_state *hw_state)
2273{
2274 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
2275 uint32_t val;
2276
2277 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
2278 return false;
2279
2280 val = I915_READ(BXT_PORT_PLL_ENABLE(port));
2281 if (!(val & PORT_PLL_ENABLE))
2282 return false;
2283
2284 hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(port));
2285 hw_state->pll0 = I915_READ(BXT_PORT_PLL(port, 0));
2286 hw_state->pll1 = I915_READ(BXT_PORT_PLL(port, 1));
2287 hw_state->pll2 = I915_READ(BXT_PORT_PLL(port, 2));
2288 hw_state->pll3 = I915_READ(BXT_PORT_PLL(port, 3));
2289 hw_state->pll6 = I915_READ(BXT_PORT_PLL(port, 6));
2290 hw_state->pll8 = I915_READ(BXT_PORT_PLL(port, 8));
2291 /*
2292 * While we write to the group register to program all lanes at once we
2293 * can read only lane registers. We configure all lanes the same way, so
2294 * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
2295 */
2296 hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(port));
2297 if (I915_READ(BXT_PORT_PCS_DW12_LN23(port) != hw_state->pcsdw12))
2298 DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
2299 hw_state->pcsdw12,
2300 I915_READ(BXT_PORT_PCS_DW12_LN23(port)));
2301
2302 return true;
2303}
2304
2305static void bxt_shared_dplls_init(struct drm_i915_private *dev_priv)
2306{
2307 int i;
2308
2309 dev_priv->num_shared_dpll = 3;
2310
2311 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
2312 dev_priv->shared_dplls[i].id = i;
2313 dev_priv->shared_dplls[i].name = bxt_ddi_pll_names[i];
2314 dev_priv->shared_dplls[i].disable = bxt_ddi_pll_disable;
2315 dev_priv->shared_dplls[i].enable = bxt_ddi_pll_enable;
2316 dev_priv->shared_dplls[i].get_hw_state =
2317 bxt_ddi_pll_get_hw_state;
2318 }
2319}
2320
Damien Lespiau143b3072014-07-29 18:06:19 +01002321void intel_ddi_pll_init(struct drm_device *dev)
2322{
2323 struct drm_i915_private *dev_priv = dev->dev_private;
2324 uint32_t val = I915_READ(LCPLL_CTL);
2325
Satheeshakrishna Md1a2dc72014-11-13 14:55:18 +00002326 if (IS_SKYLAKE(dev))
2327 skl_shared_dplls_init(dev_priv);
Satheeshakrishna Mdfb82402014-08-22 09:49:09 +05302328 else if (IS_BROXTON(dev))
2329 bxt_shared_dplls_init(dev_priv);
Satheeshakrishna Md1a2dc72014-11-13 14:55:18 +00002330 else
2331 hsw_shared_dplls_init(dev_priv);
Paulo Zanoni79f689a2012-10-05 12:05:52 -03002332
Paulo Zanonib2b877f2013-05-03 17:23:42 -03002333 DRM_DEBUG_KMS("CDCLK running at %dKHz\n",
Ville Syrjälä1652d192015-03-31 14:12:01 +03002334 dev_priv->display.get_display_clock_speed(dev));
Paulo Zanoni79f689a2012-10-05 12:05:52 -03002335
Satheeshakrishna M121643c2014-11-13 14:55:15 +00002336 if (IS_SKYLAKE(dev)) {
2337 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE))
2338 DRM_ERROR("LCPLL1 is disabled\n");
Vandana Kannanf8437dd12014-11-24 13:37:39 +05302339 } else if (IS_BROXTON(dev)) {
2340 broxton_init_cdclk(dev);
Vandana Kannan5c6706e2014-11-24 13:37:39 +05302341 broxton_ddi_phy_init(dev);
Satheeshakrishna M121643c2014-11-13 14:55:15 +00002342 } else {
2343 /*
2344 * The LCPLL register should be turned on by the BIOS. For now
2345 * let's just check its state and print errors in case
2346 * something is wrong. Don't even try to turn it on.
2347 */
Paulo Zanoni79f689a2012-10-05 12:05:52 -03002348
Satheeshakrishna M121643c2014-11-13 14:55:15 +00002349 if (val & LCPLL_CD_SOURCE_FCLK)
2350 DRM_ERROR("CDCLK source is not LCPLL\n");
2351
2352 if (val & LCPLL_PLL_DISABLE)
2353 DRM_ERROR("LCPLL is disabled\n");
2354 }
Paulo Zanoni79f689a2012-10-05 12:05:52 -03002355}
Paulo Zanonic19b0662012-10-15 15:51:41 -03002356
2357void intel_ddi_prepare_link_retrain(struct drm_encoder *encoder)
2358{
Paulo Zanoni174edf12012-10-26 19:05:50 -02002359 struct intel_digital_port *intel_dig_port = enc_to_dig_port(encoder);
2360 struct intel_dp *intel_dp = &intel_dig_port->dp;
Paulo Zanonic19b0662012-10-15 15:51:41 -03002361 struct drm_i915_private *dev_priv = encoder->dev->dev_private;
Paulo Zanoni174edf12012-10-26 19:05:50 -02002362 enum port port = intel_dig_port->port;
Paulo Zanonic19b0662012-10-15 15:51:41 -03002363 uint32_t val;
Syam Sidhardhanf3e227d2013-02-25 04:05:38 +05302364 bool wait = false;
Paulo Zanonic19b0662012-10-15 15:51:41 -03002365
2366 if (I915_READ(DP_TP_CTL(port)) & DP_TP_CTL_ENABLE) {
2367 val = I915_READ(DDI_BUF_CTL(port));
2368 if (val & DDI_BUF_CTL_ENABLE) {
2369 val &= ~DDI_BUF_CTL_ENABLE;
2370 I915_WRITE(DDI_BUF_CTL(port), val);
2371 wait = true;
2372 }
2373
2374 val = I915_READ(DP_TP_CTL(port));
2375 val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2376 val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2377 I915_WRITE(DP_TP_CTL(port), val);
2378 POSTING_READ(DP_TP_CTL(port));
2379
2380 if (wait)
2381 intel_wait_ddi_buf_idle(dev_priv, port);
2382 }
2383
Dave Airlie0e32b392014-05-02 14:02:48 +10002384 val = DP_TP_CTL_ENABLE |
Paulo Zanonic19b0662012-10-15 15:51:41 -03002385 DP_TP_CTL_LINK_TRAIN_PAT1 | DP_TP_CTL_SCRAMBLE_DISABLE;
Dave Airlie0e32b392014-05-02 14:02:48 +10002386 if (intel_dp->is_mst)
2387 val |= DP_TP_CTL_MODE_MST;
2388 else {
2389 val |= DP_TP_CTL_MODE_SST;
2390 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
2391 val |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
2392 }
Paulo Zanonic19b0662012-10-15 15:51:41 -03002393 I915_WRITE(DP_TP_CTL(port), val);
2394 POSTING_READ(DP_TP_CTL(port));
2395
2396 intel_dp->DP |= DDI_BUF_CTL_ENABLE;
2397 I915_WRITE(DDI_BUF_CTL(port), intel_dp->DP);
2398 POSTING_READ(DDI_BUF_CTL(port));
2399
2400 udelay(600);
2401}
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002402
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02002403void intel_ddi_fdi_disable(struct drm_crtc *crtc)
2404{
2405 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
2406 struct intel_encoder *intel_encoder = intel_ddi_get_crtc_encoder(crtc);
2407 uint32_t val;
2408
2409 intel_ddi_post_disable(intel_encoder);
2410
2411 val = I915_READ(_FDI_RXA_CTL);
2412 val &= ~FDI_RX_ENABLE;
2413 I915_WRITE(_FDI_RXA_CTL, val);
2414
2415 val = I915_READ(_FDI_RXA_MISC);
2416 val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
2417 val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
2418 I915_WRITE(_FDI_RXA_MISC, val);
2419
2420 val = I915_READ(_FDI_RXA_CTL);
2421 val &= ~FDI_PCDCLK;
2422 I915_WRITE(_FDI_RXA_CTL, val);
2423
2424 val = I915_READ(_FDI_RXA_CTL);
2425 val &= ~FDI_RX_PLL_ENABLE;
2426 I915_WRITE(_FDI_RXA_CTL, val);
2427}
2428
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002429static void intel_ddi_hot_plug(struct intel_encoder *intel_encoder)
2430{
Dave Airlie0e32b392014-05-02 14:02:48 +10002431 struct intel_digital_port *intel_dig_port = enc_to_dig_port(&intel_encoder->base);
2432 int type = intel_dig_port->base.type;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002433
Dave Airlie0e32b392014-05-02 14:02:48 +10002434 if (type != INTEL_OUTPUT_DISPLAYPORT &&
2435 type != INTEL_OUTPUT_EDP &&
2436 type != INTEL_OUTPUT_UNKNOWN) {
2437 return;
2438 }
2439
2440 intel_dp_hot_plug(intel_encoder);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002441}
2442
Ville Syrjälä6801c182013-09-24 14:24:05 +03002443void intel_ddi_get_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002444 struct intel_crtc_state *pipe_config)
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002445{
2446 struct drm_i915_private *dev_priv = encoder->base.dev->dev_private;
2447 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);
Ander Conselvan de Oliveira0cb09a92015-01-30 12:17:23 +02002448 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
Daniel Vetterbbd440f2014-11-20 22:33:59 +01002449 struct intel_hdmi *intel_hdmi;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002450 u32 temp, flags = 0;
2451
2452 temp = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
2453 if (temp & TRANS_DDI_PHSYNC)
2454 flags |= DRM_MODE_FLAG_PHSYNC;
2455 else
2456 flags |= DRM_MODE_FLAG_NHSYNC;
2457 if (temp & TRANS_DDI_PVSYNC)
2458 flags |= DRM_MODE_FLAG_PVSYNC;
2459 else
2460 flags |= DRM_MODE_FLAG_NVSYNC;
2461
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02002462 pipe_config->base.adjusted_mode.flags |= flags;
Ville Syrjälä42571ae2013-09-06 23:29:00 +03002463
2464 switch (temp & TRANS_DDI_BPC_MASK) {
2465 case TRANS_DDI_BPC_6:
2466 pipe_config->pipe_bpp = 18;
2467 break;
2468 case TRANS_DDI_BPC_8:
2469 pipe_config->pipe_bpp = 24;
2470 break;
2471 case TRANS_DDI_BPC_10:
2472 pipe_config->pipe_bpp = 30;
2473 break;
2474 case TRANS_DDI_BPC_12:
2475 pipe_config->pipe_bpp = 36;
2476 break;
2477 default:
2478 break;
2479 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002480
2481 switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
2482 case TRANS_DDI_MODE_SELECT_HDMI:
Daniel Vetter6897b4b2014-04-24 23:54:47 +02002483 pipe_config->has_hdmi_sink = true;
Daniel Vetterbbd440f2014-11-20 22:33:59 +01002484 intel_hdmi = enc_to_intel_hdmi(&encoder->base);
2485
2486 if (intel_hdmi->infoframe_enabled(&encoder->base))
2487 pipe_config->has_infoframe = true;
Jesse Barnescbc572a2014-11-17 13:08:47 -08002488 break;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03002489 case TRANS_DDI_MODE_SELECT_DVI:
2490 case TRANS_DDI_MODE_SELECT_FDI:
2491 break;
2492 case TRANS_DDI_MODE_SELECT_DP_SST:
2493 case TRANS_DDI_MODE_SELECT_DP_MST:
2494 pipe_config->has_dp_encoder = true;
2495 intel_dp_get_m_n(intel_crtc, pipe_config);
2496 break;
2497 default:
2498 break;
2499 }
Daniel Vetter10214422013-11-18 07:38:16 +01002500
Daniel Vetterf458ebb2014-09-30 10:56:39 +02002501 if (intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO)) {
Paulo Zanonia60551b2014-05-21 16:23:20 -03002502 temp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
Jani Nikula82910ac2014-10-27 16:26:59 +02002503 if (temp & AUDIO_OUTPUT_ENABLE(intel_crtc->pipe))
Paulo Zanonia60551b2014-05-21 16:23:20 -03002504 pipe_config->has_audio = true;
2505 }
Daniel Vetter9ed109a2014-04-24 23:54:52 +02002506
Daniel Vetter10214422013-11-18 07:38:16 +01002507 if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp_bpp &&
2508 pipe_config->pipe_bpp > dev_priv->vbt.edp_bpp) {
2509 /*
2510 * This is a big fat ugly hack.
2511 *
2512 * Some machines in UEFI boot mode provide us a VBT that has 18
2513 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
2514 * unknown we fail to light up. Yet the same BIOS boots up with
2515 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
2516 * max, not what it tells us to use.
2517 *
2518 * Note: This will still be broken if the eDP panel is not lit
2519 * up by the BIOS, and thus we can't get the mode at module
2520 * load.
2521 */
2522 DRM_DEBUG_KMS("pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
2523 pipe_config->pipe_bpp, dev_priv->vbt.edp_bpp);
2524 dev_priv->vbt.edp_bpp = pipe_config->pipe_bpp;
2525 }
Jesse Barnes11578552014-01-21 12:42:10 -08002526
Damien Lespiau22606a12014-12-12 14:26:57 +00002527 intel_ddi_clock_get(encoder, pipe_config);
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002528}
2529
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002530static void intel_ddi_destroy(struct drm_encoder *encoder)
2531{
2532 /* HDMI has nothing special to destroy, so we can go with this. */
2533 intel_dp_encoder_destroy(encoder);
2534}
2535
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002536static bool intel_ddi_compute_config(struct intel_encoder *encoder,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02002537 struct intel_crtc_state *pipe_config)
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002538{
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002539 int type = encoder->type;
Daniel Vettereccb1402013-05-22 00:50:22 +02002540 int port = intel_ddi_get_encoder_port(encoder);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002541
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002542 WARN(type == INTEL_OUTPUT_UNKNOWN, "compute_config() on unknown output!\n");
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002543
Daniel Vettereccb1402013-05-22 00:50:22 +02002544 if (port == PORT_A)
2545 pipe_config->cpu_transcoder = TRANSCODER_EDP;
2546
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002547 if (type == INTEL_OUTPUT_HDMI)
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002548 return intel_hdmi_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002549 else
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002550 return intel_dp_compute_config(encoder, pipe_config);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002551}
2552
2553static const struct drm_encoder_funcs intel_ddi_funcs = {
2554 .destroy = intel_ddi_destroy,
2555};
2556
Paulo Zanoni4a28ae52013-10-09 13:52:36 -03002557static struct intel_connector *
2558intel_ddi_init_dp_connector(struct intel_digital_port *intel_dig_port)
2559{
2560 struct intel_connector *connector;
2561 enum port port = intel_dig_port->port;
2562
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +03002563 connector = intel_connector_alloc();
Paulo Zanoni4a28ae52013-10-09 13:52:36 -03002564 if (!connector)
2565 return NULL;
2566
2567 intel_dig_port->dp.output_reg = DDI_BUF_CTL(port);
2568 if (!intel_dp_init_connector(intel_dig_port, connector)) {
2569 kfree(connector);
2570 return NULL;
2571 }
2572
2573 return connector;
2574}
2575
2576static struct intel_connector *
2577intel_ddi_init_hdmi_connector(struct intel_digital_port *intel_dig_port)
2578{
2579 struct intel_connector *connector;
2580 enum port port = intel_dig_port->port;
2581
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +03002582 connector = intel_connector_alloc();
Paulo Zanoni4a28ae52013-10-09 13:52:36 -03002583 if (!connector)
2584 return NULL;
2585
2586 intel_dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
2587 intel_hdmi_init_connector(intel_dig_port, connector);
2588
2589 return connector;
2590}
2591
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002592void intel_ddi_init(struct drm_device *dev, enum port port)
2593{
Damien Lespiau876a8cd2012-12-11 18:48:30 +00002594 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002595 struct intel_digital_port *intel_dig_port;
2596 struct intel_encoder *intel_encoder;
2597 struct drm_encoder *encoder;
Paulo Zanoni311a2092013-09-12 17:12:18 -03002598 bool init_hdmi, init_dp;
2599
2600 init_hdmi = (dev_priv->vbt.ddi_port_info[port].supports_dvi ||
2601 dev_priv->vbt.ddi_port_info[port].supports_hdmi);
2602 init_dp = dev_priv->vbt.ddi_port_info[port].supports_dp;
2603 if (!init_dp && !init_hdmi) {
Chris Wilsonf68d6972014-08-04 07:15:09 +01002604 DRM_DEBUG_KMS("VBT says port %c is not DVI/HDMI/DP compatible, assuming it is\n",
Paulo Zanoni311a2092013-09-12 17:12:18 -03002605 port_name(port));
2606 init_hdmi = true;
2607 init_dp = true;
2608 }
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002609
Daniel Vetterb14c5672013-09-19 12:18:32 +02002610 intel_dig_port = kzalloc(sizeof(*intel_dig_port), GFP_KERNEL);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002611 if (!intel_dig_port)
2612 return;
2613
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002614 intel_encoder = &intel_dig_port->base;
2615 encoder = &intel_encoder->base;
2616
2617 drm_encoder_init(dev, encoder, &intel_ddi_funcs,
2618 DRM_MODE_ENCODER_TMDS);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002619
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01002620 intel_encoder->compute_config = intel_ddi_compute_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002621 intel_encoder->enable = intel_enable_ddi;
2622 intel_encoder->pre_enable = intel_ddi_pre_enable;
2623 intel_encoder->disable = intel_disable_ddi;
2624 intel_encoder->post_disable = intel_ddi_post_disable;
2625 intel_encoder->get_hw_state = intel_ddi_get_hw_state;
Jesse Barnes045ac3b2013-05-14 17:08:26 -07002626 intel_encoder->get_config = intel_ddi_get_config;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002627
2628 intel_dig_port->port = port;
Stéphane Marchesinbcf53de2013-07-12 13:54:41 -07002629 intel_dig_port->saved_port_bits = I915_READ(DDI_BUF_CTL(port)) &
2630 (DDI_BUF_PORT_REVERSAL |
2631 DDI_A_4_LANES);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002632
2633 intel_encoder->type = INTEL_OUTPUT_UNKNOWN;
Chris Wilsonf68d6972014-08-04 07:15:09 +01002634 intel_encoder->crtc_mask = (1 << 0) | (1 << 1) | (1 << 2);
Ville Syrjäläbc079e82014-03-03 16:15:28 +02002635 intel_encoder->cloneable = 0;
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002636 intel_encoder->hot_plug = intel_ddi_hot_plug;
2637
Chris Wilsonf68d6972014-08-04 07:15:09 +01002638 if (init_dp) {
2639 if (!intel_ddi_init_dp_connector(intel_dig_port))
2640 goto err;
Dave Airlie13cf5502014-06-18 11:29:35 +10002641
Chris Wilsonf68d6972014-08-04 07:15:09 +01002642 intel_dig_port->hpd_pulse = intel_dp_hpd_pulse;
2643 dev_priv->hpd_irq_port[port] = intel_dig_port;
2644 }
Daniel Vetter21a8e6a2013-04-10 23:28:35 +02002645
Paulo Zanoni311a2092013-09-12 17:12:18 -03002646 /* In theory we don't need the encoder->type check, but leave it just in
2647 * case we have some really bad VBTs... */
Chris Wilsonf68d6972014-08-04 07:15:09 +01002648 if (intel_encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
2649 if (!intel_ddi_init_hdmi_connector(intel_dig_port))
2650 goto err;
Daniel Vetter21a8e6a2013-04-10 23:28:35 +02002651 }
Chris Wilsonf68d6972014-08-04 07:15:09 +01002652
2653 return;
2654
2655err:
2656 drm_encoder_cleanup(encoder);
2657 kfree(intel_dig_port);
Paulo Zanoni00c09d72012-10-26 19:05:52 -02002658}