blob: e4483042aee0b90dd9c9fb6cb943b574a3df603a [file] [log] [blame]
Dave Airlie746c1aa2009-12-08 07:07:28 +10001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the 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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
Jerome Glisse8d1c7022012-07-17 17:17:16 -040025 * Jerome Glisse
Dave Airlie746c1aa2009-12-08 07:07:28 +100026 */
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/radeon_drm.h>
Dave Airlie746c1aa2009-12-08 07:07:28 +100029#include "radeon.h"
30
31#include "atom.h"
32#include "atom-bits.h"
David Howells760285e2012-10-02 18:01:07 +010033#include <drm/drm_dp_helper.h>
Dave Airlie746c1aa2009-12-08 07:07:28 +100034
Alex Deucherf92a8b62009-11-23 18:40:40 -050035/* move these to drm_dp_helper.c/h */
Alex Deucher5801ead2009-11-24 13:32:59 -050036#define DP_LINK_CONFIGURATION_SIZE 9
Daniel Vetter1a644cd2012-10-18 15:32:40 +020037#define DP_DPCD_SIZE DP_RECEIVER_CAP_SIZE
Alex Deucher5801ead2009-11-24 13:32:59 -050038
39static char *voltage_names[] = {
40 "0.4V", "0.6V", "0.8V", "1.2V"
41};
42static char *pre_emph_names[] = {
43 "0dB", "3.5dB", "6dB", "9.5dB"
44};
Alex Deucherf92a8b62009-11-23 18:40:40 -050045
Alex Deucher224d94b2011-05-20 04:34:28 -040046/***** radeon AUX functions *****/
Alex Deucher34be8c92013-07-18 11:13:53 -040047
48/* Atom needs data in little endian format
49 * so swap as appropriate when copying data to
50 * or from atom. Note that atom operates on
51 * dw units.
52 */
Alex Deucher4543eda2013-08-07 19:34:53 -040053void radeon_atom_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
Alex Deucher34be8c92013-07-18 11:13:53 -040054{
55#ifdef __BIG_ENDIAN
56 u8 src_tmp[20], dst_tmp[20]; /* used for byteswapping */
57 u32 *dst32, *src32;
58 int i;
59
60 memcpy(src_tmp, src, num_bytes);
61 src32 = (u32 *)src_tmp;
62 dst32 = (u32 *)dst_tmp;
63 if (to_le) {
64 for (i = 0; i < ((num_bytes + 3) / 4); i++)
65 dst32[i] = cpu_to_le32(src32[i]);
66 memcpy(dst, dst_tmp, num_bytes);
67 } else {
68 u8 dws = num_bytes & ~3;
69 for (i = 0; i < ((num_bytes + 3) / 4); i++)
70 dst32[i] = le32_to_cpu(src32[i]);
71 memcpy(dst, dst_tmp, dws);
72 if (num_bytes % 4) {
73 for (i = 0; i < (num_bytes % 4); i++)
74 dst[dws+i] = dst_tmp[dws+i];
75 }
76 }
77#else
78 memcpy(dst, src, num_bytes);
79#endif
80}
81
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050082union aux_channel_transaction {
83 PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
84 PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
85};
Alex Deucher5801ead2009-11-24 13:32:59 -050086
Alex Deucher834b2902011-05-20 04:34:24 -040087static int radeon_process_aux_ch(struct radeon_i2c_chan *chan,
88 u8 *send, int send_bytes,
89 u8 *recv, int recv_size,
90 u8 delay, u8 *ack)
Dave Airlie746c1aa2009-12-08 07:07:28 +100091{
92 struct drm_device *dev = chan->dev;
93 struct radeon_device *rdev = dev->dev_private;
Alex Deucherbcc1c2a2010-01-12 17:54:34 -050094 union aux_channel_transaction args;
Dave Airlie746c1aa2009-12-08 07:07:28 +100095 int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
96 unsigned char *base;
Alex Deucher834b2902011-05-20 04:34:24 -040097 int recv_bytes;
Alex Deucher1a66c952009-11-20 19:40:13 -050098
Dave Airlie746c1aa2009-12-08 07:07:28 +100099 memset(&args, 0, sizeof(args));
Alex Deucher1a66c952009-11-20 19:40:13 -0500100
Alex Deucher97412a72012-03-20 17:18:06 -0400101 base = (unsigned char *)(rdev->mode_info.atom_context->scratch + 1);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000102
Alex Deucher4543eda2013-08-07 19:34:53 -0400103 radeon_atom_copy_swap(base, send, send_bytes, true);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000104
Alex Deucher34be8c92013-07-18 11:13:53 -0400105 args.v1.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
106 args.v1.lpDataOut = cpu_to_le16((u16)(16 + 4));
Alex Deucherbcc1c2a2010-01-12 17:54:34 -0500107 args.v1.ucDataOutLen = 0;
108 args.v1.ucChannelID = chan->rec.i2c_id;
109 args.v1.ucDelay = delay / 10;
110 if (ASIC_IS_DCE4(rdev))
Alex Deucher8e36ed02010-05-18 19:26:47 -0400111 args.v2.ucHPD_ID = chan->rec.hpd;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000112
113 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
114
Alex Deucher834b2902011-05-20 04:34:24 -0400115 *ack = args.v1.ucReplyStatus;
116
117 /* timeout */
118 if (args.v1.ucReplyStatus == 1) {
119 DRM_DEBUG_KMS("dp_aux_ch timeout\n");
120 return -ETIMEDOUT;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000121 }
122
Alex Deucher834b2902011-05-20 04:34:24 -0400123 /* flags not zero */
124 if (args.v1.ucReplyStatus == 2) {
125 DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
126 return -EBUSY;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000127 }
Alex Deucher834b2902011-05-20 04:34:24 -0400128
129 /* error */
130 if (args.v1.ucReplyStatus == 3) {
131 DRM_DEBUG_KMS("dp_aux_ch error\n");
132 return -EIO;
133 }
134
135 recv_bytes = args.v1.ucDataOutLen;
136 if (recv_bytes > recv_size)
137 recv_bytes = recv_size;
138
139 if (recv && recv_size)
Alex Deucher4543eda2013-08-07 19:34:53 -0400140 radeon_atom_copy_swap(recv, base + 16, recv_bytes, false);
Alex Deucher834b2902011-05-20 04:34:24 -0400141
142 return recv_bytes;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000143}
144
Alex Deucher25377b92014-04-07 10:33:43 -0400145#define BARE_ADDRESS_SIZE 3
146#define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
Alex Deucher496263b2014-03-21 10:34:07 -0400147
148static ssize_t
149radeon_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
Alex Deucher5801ead2009-11-24 13:32:59 -0500150{
Alex Deucher496263b2014-03-21 10:34:07 -0400151 struct radeon_i2c_chan *chan =
152 container_of(aux, struct radeon_i2c_chan, aux);
Alex Deucher834b2902011-05-20 04:34:24 -0400153 int ret;
Alex Deucher496263b2014-03-21 10:34:07 -0400154 u8 tx_buf[20];
155 size_t tx_size;
156 u8 ack, delay = 0;
Alex Deucher5801ead2009-11-24 13:32:59 -0500157
Alex Deucher496263b2014-03-21 10:34:07 -0400158 if (WARN_ON(msg->size > 16))
159 return -E2BIG;
Alex Deucher834b2902011-05-20 04:34:24 -0400160
Alex Deucher496263b2014-03-21 10:34:07 -0400161 tx_buf[0] = msg->address & 0xff;
162 tx_buf[1] = msg->address >> 8;
163 tx_buf[2] = msg->request << 4;
Alex Deucher25377b92014-04-07 10:33:43 -0400164 tx_buf[3] = msg->size ? (msg->size - 1) : 0;
Alex Deucher834b2902011-05-20 04:34:24 -0400165
Alex Deucher496263b2014-03-21 10:34:07 -0400166 switch (msg->request & ~DP_AUX_I2C_MOT) {
167 case DP_AUX_NATIVE_WRITE:
168 case DP_AUX_I2C_WRITE:
Alex Deucher25377b92014-04-07 10:33:43 -0400169 /* tx_size needs to be 4 even for bare address packets since the atom
170 * table needs the info in tx_buf[3].
171 */
Alex Deucher496263b2014-03-21 10:34:07 -0400172 tx_size = HEADER_SIZE + msg->size;
Alex Deucher25377b92014-04-07 10:33:43 -0400173 if (msg->size == 0)
174 tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
175 else
176 tx_buf[3] |= tx_size << 4;
Alex Deucher496263b2014-03-21 10:34:07 -0400177 memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
178 ret = radeon_process_aux_ch(chan,
179 tx_buf, tx_size, NULL, 0, delay, &ack);
180 if (ret >= 0)
181 /* Return payload size. */
182 ret = msg->size;
183 break;
184 case DP_AUX_NATIVE_READ:
185 case DP_AUX_I2C_READ:
Alex Deucher25377b92014-04-07 10:33:43 -0400186 /* tx_size needs to be 4 even for bare address packets since the atom
187 * table needs the info in tx_buf[3].
188 */
Alex Deucher496263b2014-03-21 10:34:07 -0400189 tx_size = HEADER_SIZE;
Alex Deucher25377b92014-04-07 10:33:43 -0400190 if (msg->size == 0)
191 tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
192 else
193 tx_buf[3] |= tx_size << 4;
Alex Deucher496263b2014-03-21 10:34:07 -0400194 ret = radeon_process_aux_ch(chan,
195 tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
196 break;
197 default:
198 ret = -EINVAL;
199 break;
Alex Deucher834b2902011-05-20 04:34:24 -0400200 }
201
Alex Deucher25377b92014-04-07 10:33:43 -0400202 if (ret >= 0)
Alex Deucher496263b2014-03-21 10:34:07 -0400203 msg->reply = ack >> 4;
204
205 return ret;
Alex Deucher5801ead2009-11-24 13:32:59 -0500206}
207
Alex Deucher496263b2014-03-21 10:34:07 -0400208void radeon_dp_aux_init(struct radeon_connector *radeon_connector)
Alex Deucher5801ead2009-11-24 13:32:59 -0500209{
210 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
Alex Deucher834b2902011-05-20 04:34:24 -0400211
Alex Deucher496263b2014-03-21 10:34:07 -0400212 dig_connector->dp_i2c_bus->aux.dev = radeon_connector->base.kdev;
213 dig_connector->dp_i2c_bus->aux.transfer = radeon_dp_aux_transfer;
Alex Deucher5801ead2009-11-24 13:32:59 -0500214}
215
Dave Airlie746c1aa2009-12-08 07:07:28 +1000216int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
Alex Deucher834b2902011-05-20 04:34:24 -0400217 u8 write_byte, u8 *read_byte)
Dave Airlie746c1aa2009-12-08 07:07:28 +1000218{
219 struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
Alex Deucherf3381df2014-03-17 23:48:14 -0400220 struct radeon_i2c_chan *auxch = i2c_get_adapdata(adapter);
Alex Deucher834b2902011-05-20 04:34:24 -0400221 u16 address = algo_data->address;
222 u8 msg[5];
223 u8 reply[2];
224 unsigned retry;
225 int msg_bytes;
226 int reply_bytes = 1;
227 int ret;
228 u8 ack;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000229
Alex Deucherdca0be02014-03-17 23:48:17 -0400230 /* Set up the address */
Dave Airlie746c1aa2009-12-08 07:07:28 +1000231 msg[0] = address;
232 msg[1] = address >> 8;
233
Alex Deucherdca0be02014-03-17 23:48:17 -0400234 /* Set up the command byte */
235 if (mode & MODE_I2C_READ) {
236 msg[2] = DP_AUX_I2C_READ << 4;
237 msg_bytes = 4;
238 msg[3] = msg_bytes << 4;
239 } else {
240 msg[2] = DP_AUX_I2C_WRITE << 4;
Alex Deucher834b2902011-05-20 04:34:24 -0400241 msg_bytes = 5;
242 msg[3] = msg_bytes << 4;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000243 msg[4] = write_byte;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000244 }
245
Alex Deucherdca0be02014-03-17 23:48:17 -0400246 /* special handling for start/stop */
247 if (mode & (MODE_I2C_START | MODE_I2C_STOP))
248 msg[3] = 3 << 4;
249
250 /* Set MOT bit for all but stop */
251 if ((mode & MODE_I2C_STOP) == 0)
252 msg[2] |= DP_AUX_I2C_MOT << 4;
253
Alex Deucher21386812014-01-14 10:29:59 -0500254 for (retry = 0; retry < 7; retry++) {
Alex Deucher834b2902011-05-20 04:34:24 -0400255 ret = radeon_process_aux_ch(auxch,
256 msg, msg_bytes, reply, reply_bytes, 0, &ack);
Alex Deucher4f332842011-10-04 17:23:15 -0400257 if (ret == -EBUSY)
258 continue;
259 else if (ret < 0) {
Alex Deucher834b2902011-05-20 04:34:24 -0400260 DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
261 return ret;
262 }
Dave Airlie746c1aa2009-12-08 07:07:28 +1000263
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100264 switch ((ack >> 4) & DP_AUX_NATIVE_REPLY_MASK) {
265 case DP_AUX_NATIVE_REPLY_ACK:
Alex Deucher834b2902011-05-20 04:34:24 -0400266 /* I2C-over-AUX Reply field is only valid
267 * when paired with AUX ACK.
268 */
269 break;
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100270 case DP_AUX_NATIVE_REPLY_NACK:
Alex Deucher834b2902011-05-20 04:34:24 -0400271 DRM_DEBUG_KMS("aux_ch native nack\n");
272 return -EREMOTEIO;
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100273 case DP_AUX_NATIVE_REPLY_DEFER:
Alex Deucher834b2902011-05-20 04:34:24 -0400274 DRM_DEBUG_KMS("aux_ch native defer\n");
Alex Deucherab9f51c2014-01-14 10:37:33 -0500275 usleep_range(500, 600);
Alex Deucher834b2902011-05-20 04:34:24 -0400276 continue;
277 default:
278 DRM_ERROR("aux_ch invalid native reply 0x%02x\n", ack);
279 return -EREMOTEIO;
280 }
281
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100282 switch ((ack >> 4) & DP_AUX_I2C_REPLY_MASK) {
283 case DP_AUX_I2C_REPLY_ACK:
Alex Deucher834b2902011-05-20 04:34:24 -0400284 if (mode == MODE_I2C_READ)
285 *read_byte = reply[0];
286 return ret;
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100287 case DP_AUX_I2C_REPLY_NACK:
Alex Deucher834b2902011-05-20 04:34:24 -0400288 DRM_DEBUG_KMS("aux_i2c nack\n");
289 return -EREMOTEIO;
Thierry Reding6b27f7f2013-12-16 17:01:29 +0100290 case DP_AUX_I2C_REPLY_DEFER:
Alex Deucher834b2902011-05-20 04:34:24 -0400291 DRM_DEBUG_KMS("aux_i2c defer\n");
Alex Deucherab9f51c2014-01-14 10:37:33 -0500292 usleep_range(400, 500);
Alex Deucher834b2902011-05-20 04:34:24 -0400293 break;
294 default:
295 DRM_ERROR("aux_i2c invalid reply 0x%02x\n", ack);
296 return -EREMOTEIO;
297 }
Dave Airlie746c1aa2009-12-08 07:07:28 +1000298 }
Alex Deucher834b2902011-05-20 04:34:24 -0400299
Alex Deucher091264f2011-11-08 10:09:58 -0500300 DRM_DEBUG_KMS("aux i2c too many retries, giving up\n");
Dave Airlie746c1aa2009-12-08 07:07:28 +1000301 return -EREMOTEIO;
302}
Alex Deucher5801ead2009-11-24 13:32:59 -0500303
Alex Deucher224d94b2011-05-20 04:34:28 -0400304/***** general DP utility functions *****/
305
Alex Deucher224d94b2011-05-20 04:34:28 -0400306#define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
307#define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPHASIS_9_5
308
309static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
310 int lane_count,
311 u8 train_set[4])
312{
313 u8 v = 0;
314 u8 p = 0;
315 int lane;
316
317 for (lane = 0; lane < lane_count; lane++) {
Daniel Vetter0f037bd2012-10-18 10:15:27 +0200318 u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
319 u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
Alex Deucher224d94b2011-05-20 04:34:28 -0400320
321 DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
322 lane,
323 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
324 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
325
326 if (this_v > v)
327 v = this_v;
328 if (this_p > p)
329 p = this_p;
330 }
331
332 if (v >= DP_VOLTAGE_MAX)
333 v |= DP_TRAIN_MAX_SWING_REACHED;
334
335 if (p >= DP_PRE_EMPHASIS_MAX)
336 p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
337
338 DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
339 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
340 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
341
342 for (lane = 0; lane < 4; lane++)
343 train_set[lane] = v | p;
344}
345
346/* convert bits per color to bits per pixel */
347/* get bpc from the EDID */
348static int convert_bpc_to_bpp(int bpc)
349{
350 if (bpc == 0)
351 return 24;
352 else
353 return bpc * 3;
354}
355
356/* get the max pix clock supported by the link rate and lane num */
357static int dp_get_max_dp_pix_clock(int link_rate,
358 int lane_num,
359 int bpp)
360{
361 return (link_rate * lane_num * 8) / bpp;
362}
363
Alex Deucher224d94b2011-05-20 04:34:28 -0400364/***** radeon specific DP functions *****/
365
366/* First get the min lane# when low rate is used according to pixel clock
367 * (prefer low rate), second check max lane# supported by DP panel,
368 * if the max lane# < low rate lane# then use max lane# instead.
369 */
370static int radeon_dp_get_dp_lane_number(struct drm_connector *connector,
371 u8 dpcd[DP_DPCD_SIZE],
372 int pix_clock)
373{
Alex Deuchereccea792012-03-26 15:12:54 -0400374 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200375 int max_link_rate = drm_dp_max_link_rate(dpcd);
Daniel Vetter397fe152012-10-22 22:56:43 +0200376 int max_lane_num = drm_dp_max_lane_count(dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400377 int lane_num;
378 int max_dp_pix_clock;
379
380 for (lane_num = 1; lane_num < max_lane_num; lane_num <<= 1) {
381 max_dp_pix_clock = dp_get_max_dp_pix_clock(max_link_rate, lane_num, bpp);
382 if (pix_clock <= max_dp_pix_clock)
383 break;
384 }
385
386 return lane_num;
387}
388
389static int radeon_dp_get_dp_link_clock(struct drm_connector *connector,
390 u8 dpcd[DP_DPCD_SIZE],
391 int pix_clock)
392{
Alex Deuchereccea792012-03-26 15:12:54 -0400393 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
Alex Deucher224d94b2011-05-20 04:34:28 -0400394 int lane_num, max_pix_clock;
395
Alex Deucherfdca78c2011-10-25 11:54:52 -0400396 if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
397 ENCODER_OBJECT_ID_NUTMEG)
Alex Deucher224d94b2011-05-20 04:34:28 -0400398 return 270000;
399
400 lane_num = radeon_dp_get_dp_lane_number(connector, dpcd, pix_clock);
401 max_pix_clock = dp_get_max_dp_pix_clock(162000, lane_num, bpp);
402 if (pix_clock <= max_pix_clock)
403 return 162000;
404 max_pix_clock = dp_get_max_dp_pix_clock(270000, lane_num, bpp);
405 if (pix_clock <= max_pix_clock)
406 return 270000;
407 if (radeon_connector_is_dp12_capable(connector)) {
408 max_pix_clock = dp_get_max_dp_pix_clock(540000, lane_num, bpp);
409 if (pix_clock <= max_pix_clock)
410 return 540000;
411 }
412
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200413 return drm_dp_max_link_rate(dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400414}
415
416static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
417 int action, int dp_clock,
418 u8 ucconfig, u8 lane_num)
419{
420 DP_ENCODER_SERVICE_PARAMETERS args;
421 int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
422
423 memset(&args, 0, sizeof(args));
424 args.ucLinkClock = dp_clock / 10;
425 args.ucConfig = ucconfig;
426 args.ucAction = action;
427 args.ucLaneNum = lane_num;
428 args.ucStatus = 0;
429
430 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
431 return args.ucStatus;
432}
433
434u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
435{
436 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
437 struct drm_device *dev = radeon_connector->base.dev;
438 struct radeon_device *rdev = dev->dev_private;
439
440 return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
441 dig_connector->dp_i2c_bus->rec.i2c_id, 0);
442}
443
Adam Jackson40c5d872012-05-14 16:05:48 -0400444static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
445{
446 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
447 u8 buf[3];
448
449 if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
450 return;
451
Alex Deucher496263b2014-03-21 10:34:07 -0400452 if (drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_SINK_OUI, buf, 3))
Adam Jackson40c5d872012-05-14 16:05:48 -0400453 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
454 buf[0], buf[1], buf[2]);
455
Alex Deucher496263b2014-03-21 10:34:07 -0400456 if (drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_BRANCH_OUI, buf, 3))
Adam Jackson40c5d872012-05-14 16:05:48 -0400457 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
458 buf[0], buf[1], buf[2]);
459}
460
Alex Deucher224d94b2011-05-20 04:34:28 -0400461bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
462{
463 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200464 u8 msg[DP_DPCD_SIZE];
Alex Deucher224d94b2011-05-20 04:34:28 -0400465 int ret, i;
466
Alex Deucher496263b2014-03-21 10:34:07 -0400467 ret = drm_dp_dpcd_read(&dig_connector->dp_i2c_bus->aux, DP_DPCD_REV, msg,
468 DP_DPCD_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400469 if (ret > 0) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200470 memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400471 DRM_DEBUG_KMS("DPCD: ");
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200472 for (i = 0; i < DP_DPCD_SIZE; i++)
Alex Deucher224d94b2011-05-20 04:34:28 -0400473 DRM_DEBUG_KMS("%02x ", msg[i]);
474 DRM_DEBUG_KMS("\n");
Adam Jackson40c5d872012-05-14 16:05:48 -0400475
476 radeon_dp_probe_oui(radeon_connector);
477
Alex Deucher224d94b2011-05-20 04:34:28 -0400478 return true;
479 }
480 dig_connector->dpcd[0] = 0;
481 return false;
482}
483
Alex Deucher386d4d72012-01-20 15:01:29 -0500484int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
485 struct drm_connector *connector)
Alex Deucher224d94b2011-05-20 04:34:28 -0400486{
487 struct drm_device *dev = encoder->dev;
488 struct radeon_device *rdev = dev->dev_private;
Alex Deucher00dfb8d2011-10-31 08:54:41 -0400489 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
Alex Deucher496263b2014-03-21 10:34:07 -0400490 struct radeon_connector_atom_dig *dig_connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400491 int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
Alex Deucher0ceb9962012-08-27 17:48:18 -0400492 u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
493 u8 tmp;
Alex Deucher224d94b2011-05-20 04:34:28 -0400494
495 if (!ASIC_IS_DCE4(rdev))
Alex Deucher386d4d72012-01-20 15:01:29 -0500496 return panel_mode;
Alex Deucher224d94b2011-05-20 04:34:28 -0400497
Alex Deucher496263b2014-03-21 10:34:07 -0400498 if (!radeon_connector->con_priv)
499 return panel_mode;
500
501 dig_connector = radeon_connector->con_priv;
502
Alex Deucher0ceb9962012-08-27 17:48:18 -0400503 if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
504 /* DP bridge chips */
Alex Deucher496263b2014-03-21 10:34:07 -0400505 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux,
506 DP_EDP_CONFIGURATION_CAP, &tmp);
Alex Deucher0ceb9962012-08-27 17:48:18 -0400507 if (tmp & 1)
508 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
509 else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
510 (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
Alex Deucher304a4842012-02-02 10:18:00 -0500511 panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
512 else
Alex Deucher0ceb9962012-08-27 17:48:18 -0400513 panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
Alex Deucher304a4842012-02-02 10:18:00 -0500514 } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
Alex Deucher0ceb9962012-08-27 17:48:18 -0400515 /* eDP */
Alex Deucher496263b2014-03-21 10:34:07 -0400516 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux,
517 DP_EDP_CONFIGURATION_CAP, &tmp);
Alex Deucher00dfb8d2011-10-31 08:54:41 -0400518 if (tmp & 1)
519 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
520 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400521
Alex Deucher386d4d72012-01-20 15:01:29 -0500522 return panel_mode;
Alex Deucher224d94b2011-05-20 04:34:28 -0400523}
524
525void radeon_dp_set_link_config(struct drm_connector *connector,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200526 const struct drm_display_mode *mode)
Alex Deucher224d94b2011-05-20 04:34:28 -0400527{
528 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
529 struct radeon_connector_atom_dig *dig_connector;
530
531 if (!radeon_connector->con_priv)
532 return;
533 dig_connector = radeon_connector->con_priv;
534
535 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
536 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
537 dig_connector->dp_clock =
538 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
539 dig_connector->dp_lane_count =
540 radeon_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
541 }
542}
543
544int radeon_dp_mode_valid_helper(struct drm_connector *connector,
545 struct drm_display_mode *mode)
546{
547 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
548 struct radeon_connector_atom_dig *dig_connector;
549 int dp_clock;
550
551 if (!radeon_connector->con_priv)
552 return MODE_CLOCK_HIGH;
553 dig_connector = radeon_connector->con_priv;
554
555 dp_clock =
556 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
557
558 if ((dp_clock == 540000) &&
559 (!radeon_connector_is_dp12_capable(connector)))
560 return MODE_CLOCK_HIGH;
561
562 return MODE_OK;
563}
564
Alex Deucherd5811e82011-08-13 13:36:13 -0400565bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
566{
567 u8 link_status[DP_LINK_STATUS_SIZE];
568 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
569
Alex Deucherab8f1a22014-03-21 10:34:08 -0400570 if (drm_dp_dpcd_read_link_status(&dig->dp_i2c_bus->aux, link_status) <= 0)
Alex Deucherd5811e82011-08-13 13:36:13 -0400571 return false;
Daniel Vetter1ffdff12012-10-18 10:15:24 +0200572 if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
Alex Deucherd5811e82011-08-13 13:36:13 -0400573 return false;
574 return true;
575}
576
Alex Deucher2953da12014-03-17 23:48:15 -0400577void radeon_dp_set_rx_power_state(struct drm_connector *connector,
578 u8 power_state)
579{
580 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
581 struct radeon_connector_atom_dig *dig_connector;
582
583 if (!radeon_connector->con_priv)
584 return;
585
586 dig_connector = radeon_connector->con_priv;
587
588 /* power up/down the sink */
589 if (dig_connector->dpcd[0] >= 0x11) {
Alex Deucher496263b2014-03-21 10:34:07 -0400590 drm_dp_dpcd_writeb(&dig_connector->dp_i2c_bus->aux,
Alex Deucher2953da12014-03-17 23:48:15 -0400591 DP_SET_POWER, power_state);
592 usleep_range(1000, 2000);
593 }
594}
595
596
Alex Deucher224d94b2011-05-20 04:34:28 -0400597struct radeon_dp_link_train_info {
598 struct radeon_device *rdev;
599 struct drm_encoder *encoder;
600 struct drm_connector *connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400601 int enc_id;
602 int dp_clock;
603 int dp_lane_count;
Alex Deucher224d94b2011-05-20 04:34:28 -0400604 bool tp3_supported;
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200605 u8 dpcd[DP_RECEIVER_CAP_SIZE];
Alex Deucher224d94b2011-05-20 04:34:28 -0400606 u8 train_set[4];
607 u8 link_status[DP_LINK_STATUS_SIZE];
608 u8 tries;
Jerome Glisse5a96a892011-07-25 11:57:43 -0400609 bool use_dpencoder;
Alex Deucher496263b2014-03-21 10:34:07 -0400610 struct drm_dp_aux *aux;
Alex Deucher224d94b2011-05-20 04:34:28 -0400611};
612
613static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
614{
615 /* set the initial vs/emph on the source */
616 atombios_dig_transmitter_setup(dp_info->encoder,
617 ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
618 0, dp_info->train_set[0]); /* sets all lanes at once */
619
620 /* set the vs/emph on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400621 drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
622 dp_info->train_set, dp_info->dp_lane_count);
Alex Deucher224d94b2011-05-20 04:34:28 -0400623}
624
625static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
626{
627 int rtp = 0;
628
629 /* set training pattern on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400630 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400631 switch (tp) {
632 case DP_TRAINING_PATTERN_1:
633 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
634 break;
635 case DP_TRAINING_PATTERN_2:
636 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
637 break;
638 case DP_TRAINING_PATTERN_3:
639 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
640 break;
641 }
642 atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
643 } else {
644 switch (tp) {
645 case DP_TRAINING_PATTERN_1:
646 rtp = 0;
647 break;
648 case DP_TRAINING_PATTERN_2:
649 rtp = 1;
650 break;
651 }
652 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
653 dp_info->dp_clock, dp_info->enc_id, rtp);
654 }
655
656 /* enable training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400657 drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400658}
659
660static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
661{
Alex Deucher386d4d72012-01-20 15:01:29 -0500662 struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
663 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher224d94b2011-05-20 04:34:28 -0400664 u8 tmp;
665
666 /* power up the sink */
Alex Deucher2953da12014-03-17 23:48:15 -0400667 radeon_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
Alex Deucher224d94b2011-05-20 04:34:28 -0400668
669 /* possibly enable downspread on the sink */
670 if (dp_info->dpcd[3] & 0x1)
Alex Deucher496263b2014-03-21 10:34:07 -0400671 drm_dp_dpcd_writeb(dp_info->aux,
672 DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
Alex Deucher224d94b2011-05-20 04:34:28 -0400673 else
Alex Deucher496263b2014-03-21 10:34:07 -0400674 drm_dp_dpcd_writeb(dp_info->aux,
675 DP_DOWNSPREAD_CTRL, 0);
Alex Deucher224d94b2011-05-20 04:34:28 -0400676
Alex Deucher386d4d72012-01-20 15:01:29 -0500677 if ((dp_info->connector->connector_type == DRM_MODE_CONNECTOR_eDP) &&
678 (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)) {
Alex Deucher496263b2014-03-21 10:34:07 -0400679 drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
Alex Deucher386d4d72012-01-20 15:01:29 -0500680 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400681
682 /* set the lane count on the sink */
683 tmp = dp_info->dp_lane_count;
Jani Nikula27f75dc62013-10-04 15:08:09 +0300684 if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
Alex Deucher224d94b2011-05-20 04:34:28 -0400685 tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
Alex Deucher496263b2014-03-21 10:34:07 -0400686 drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400687
688 /* set the link rate on the sink */
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200689 tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
Alex Deucher496263b2014-03-21 10:34:07 -0400690 drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400691
692 /* start training on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400693 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
Alex Deucher224d94b2011-05-20 04:34:28 -0400694 atombios_dig_encoder_setup(dp_info->encoder,
695 ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
696 else
697 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
698 dp_info->dp_clock, dp_info->enc_id, 0);
699
700 /* disable the training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400701 drm_dp_dpcd_writeb(dp_info->aux,
702 DP_TRAINING_PATTERN_SET,
703 DP_TRAINING_PATTERN_DISABLE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400704
705 return 0;
706}
707
708static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
709{
710 udelay(400);
711
712 /* disable the training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400713 drm_dp_dpcd_writeb(dp_info->aux,
714 DP_TRAINING_PATTERN_SET,
715 DP_TRAINING_PATTERN_DISABLE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400716
717 /* disable the training pattern on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400718 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
Alex Deucher224d94b2011-05-20 04:34:28 -0400719 atombios_dig_encoder_setup(dp_info->encoder,
720 ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
721 else
722 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
723 dp_info->dp_clock, dp_info->enc_id, 0);
724
725 return 0;
726}
727
728static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
729{
730 bool clock_recovery;
731 u8 voltage;
732 int i;
733
734 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
735 memset(dp_info->train_set, 0, 4);
736 radeon_dp_update_vs_emph(dp_info);
737
738 udelay(400);
739
740 /* clock recovery loop */
741 clock_recovery = false;
742 dp_info->tries = 0;
743 voltage = 0xff;
744 while (1) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200745 drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400746
Alex Deucherab8f1a22014-03-21 10:34:08 -0400747 if (drm_dp_dpcd_read_link_status(dp_info->aux,
748 dp_info->link_status) <= 0) {
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400749 DRM_ERROR("displayport link status failed\n");
Alex Deucher224d94b2011-05-20 04:34:28 -0400750 break;
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400751 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400752
Daniel Vetter01916272012-10-18 10:15:25 +0200753 if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400754 clock_recovery = true;
755 break;
756 }
757
758 for (i = 0; i < dp_info->dp_lane_count; i++) {
759 if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
760 break;
761 }
762 if (i == dp_info->dp_lane_count) {
763 DRM_ERROR("clock recovery reached max voltage\n");
764 break;
765 }
766
767 if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
768 ++dp_info->tries;
769 if (dp_info->tries == 5) {
770 DRM_ERROR("clock recovery tried 5 times\n");
771 break;
772 }
773 } else
774 dp_info->tries = 0;
775
776 voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
777
778 /* Compute new train_set as requested by sink */
779 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
780
781 radeon_dp_update_vs_emph(dp_info);
782 }
783 if (!clock_recovery) {
784 DRM_ERROR("clock recovery failed\n");
785 return -1;
786 } else {
787 DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
788 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
789 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
790 DP_TRAIN_PRE_EMPHASIS_SHIFT);
791 return 0;
792 }
793}
794
795static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
796{
797 bool channel_eq;
798
799 if (dp_info->tp3_supported)
800 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
801 else
802 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
803
804 /* channel equalization loop */
805 dp_info->tries = 0;
806 channel_eq = false;
807 while (1) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200808 drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400809
Alex Deucherab8f1a22014-03-21 10:34:08 -0400810 if (drm_dp_dpcd_read_link_status(dp_info->aux,
811 dp_info->link_status) <= 0) {
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400812 DRM_ERROR("displayport link status failed\n");
Alex Deucher224d94b2011-05-20 04:34:28 -0400813 break;
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400814 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400815
Daniel Vetter1ffdff12012-10-18 10:15:24 +0200816 if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400817 channel_eq = true;
818 break;
819 }
820
821 /* Try 5 times */
822 if (dp_info->tries > 5) {
823 DRM_ERROR("channel eq failed: 5 tries\n");
824 break;
825 }
826
827 /* Compute new train_set as requested by sink */
828 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
829
830 radeon_dp_update_vs_emph(dp_info);
831 dp_info->tries++;
832 }
833
834 if (!channel_eq) {
835 DRM_ERROR("channel eq failed\n");
836 return -1;
837 } else {
838 DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
839 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
840 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
841 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
842 return 0;
843 }
844}
845
846void radeon_dp_link_train(struct drm_encoder *encoder,
847 struct drm_connector *connector)
848{
849 struct drm_device *dev = encoder->dev;
850 struct radeon_device *rdev = dev->dev_private;
851 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
852 struct radeon_encoder_atom_dig *dig;
853 struct radeon_connector *radeon_connector;
854 struct radeon_connector_atom_dig *dig_connector;
855 struct radeon_dp_link_train_info dp_info;
Jerome Glisse5a96a892011-07-25 11:57:43 -0400856 int index;
857 u8 tmp, frev, crev;
Alex Deucher224d94b2011-05-20 04:34:28 -0400858
859 if (!radeon_encoder->enc_priv)
860 return;
861 dig = radeon_encoder->enc_priv;
862
863 radeon_connector = to_radeon_connector(connector);
864 if (!radeon_connector->con_priv)
865 return;
866 dig_connector = radeon_connector->con_priv;
867
868 if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
869 (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
870 return;
871
Jerome Glisse5a96a892011-07-25 11:57:43 -0400872 /* DPEncoderService newer than 1.1 can't program properly the
873 * training pattern. When facing such version use the
874 * DIGXEncoderControl (X== 1 | 2)
875 */
876 dp_info.use_dpencoder = true;
877 index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
878 if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
879 if (crev > 1) {
880 dp_info.use_dpencoder = false;
881 }
882 }
883
Alex Deucher224d94b2011-05-20 04:34:28 -0400884 dp_info.enc_id = 0;
885 if (dig->dig_encoder)
886 dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
887 else
888 dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
889 if (dig->linkb)
890 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
891 else
892 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
893
Alex Deucher496263b2014-03-21 10:34:07 -0400894 drm_dp_dpcd_readb(&dig_connector->dp_i2c_bus->aux, DP_MAX_LANE_COUNT, &tmp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400895 if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
896 dp_info.tp3_supported = true;
897 else
898 dp_info.tp3_supported = false;
899
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200900 memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400901 dp_info.rdev = rdev;
902 dp_info.encoder = encoder;
903 dp_info.connector = connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400904 dp_info.dp_lane_count = dig_connector->dp_lane_count;
905 dp_info.dp_clock = dig_connector->dp_clock;
Alex Deucher496263b2014-03-21 10:34:07 -0400906 dp_info.aux = &dig_connector->dp_i2c_bus->aux;
Alex Deucher224d94b2011-05-20 04:34:28 -0400907
908 if (radeon_dp_link_train_init(&dp_info))
909 goto done;
910 if (radeon_dp_link_train_cr(&dp_info))
911 goto done;
912 if (radeon_dp_link_train_ce(&dp_info))
913 goto done;
914done:
915 if (radeon_dp_link_train_finish(&dp_info))
916 return;
917}