blob: 54e4f52549af47f19edf39340754a4e33a4da49c [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{
Alex Deucher834b2902011-05-20 04:34:24 -0400210 int ret;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000211
Alex Deucherad47b8f2014-04-22 02:02:06 -0400212 radeon_connector->ddc_bus->rec.hpd = radeon_connector->hpd.hpd;
Alex Deucher379dfc22014-04-07 10:33:46 -0400213 radeon_connector->ddc_bus->aux.dev = radeon_connector->base.kdev;
214 radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer;
215 ret = drm_dp_aux_register_i2c_bus(&radeon_connector->ddc_bus->aux);
216 if (!ret)
217 radeon_connector->ddc_bus->has_aux = true;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000218
Alex Deucher379dfc22014-04-07 10:33:46 -0400219 WARN(ret, "drm_dp_aux_register_i2c_bus() failed with error %d\n", ret);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000220}
Alex Deucher5801ead2009-11-24 13:32:59 -0500221
Alex Deucher224d94b2011-05-20 04:34:28 -0400222/***** general DP utility functions *****/
223
Alex Deucher224d94b2011-05-20 04:34:28 -0400224#define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
225#define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPHASIS_9_5
226
227static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
228 int lane_count,
229 u8 train_set[4])
230{
231 u8 v = 0;
232 u8 p = 0;
233 int lane;
234
235 for (lane = 0; lane < lane_count; lane++) {
Daniel Vetter0f037bd2012-10-18 10:15:27 +0200236 u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
237 u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
Alex Deucher224d94b2011-05-20 04:34:28 -0400238
239 DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
240 lane,
241 voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
242 pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
243
244 if (this_v > v)
245 v = this_v;
246 if (this_p > p)
247 p = this_p;
248 }
249
250 if (v >= DP_VOLTAGE_MAX)
251 v |= DP_TRAIN_MAX_SWING_REACHED;
252
253 if (p >= DP_PRE_EMPHASIS_MAX)
254 p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
255
256 DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
257 voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
258 pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
259
260 for (lane = 0; lane < 4; lane++)
261 train_set[lane] = v | p;
262}
263
264/* convert bits per color to bits per pixel */
265/* get bpc from the EDID */
266static int convert_bpc_to_bpp(int bpc)
267{
268 if (bpc == 0)
269 return 24;
270 else
271 return bpc * 3;
272}
273
274/* get the max pix clock supported by the link rate and lane num */
275static int dp_get_max_dp_pix_clock(int link_rate,
276 int lane_num,
277 int bpp)
278{
279 return (link_rate * lane_num * 8) / bpp;
280}
281
Alex Deucher224d94b2011-05-20 04:34:28 -0400282/***** radeon specific DP functions *****/
283
284/* First get the min lane# when low rate is used according to pixel clock
285 * (prefer low rate), second check max lane# supported by DP panel,
286 * if the max lane# < low rate lane# then use max lane# instead.
287 */
288static int radeon_dp_get_dp_lane_number(struct drm_connector *connector,
289 u8 dpcd[DP_DPCD_SIZE],
290 int pix_clock)
291{
Alex Deuchereccea792012-03-26 15:12:54 -0400292 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200293 int max_link_rate = drm_dp_max_link_rate(dpcd);
Daniel Vetter397fe152012-10-22 22:56:43 +0200294 int max_lane_num = drm_dp_max_lane_count(dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400295 int lane_num;
296 int max_dp_pix_clock;
297
298 for (lane_num = 1; lane_num < max_lane_num; lane_num <<= 1) {
299 max_dp_pix_clock = dp_get_max_dp_pix_clock(max_link_rate, lane_num, bpp);
300 if (pix_clock <= max_dp_pix_clock)
301 break;
302 }
303
304 return lane_num;
305}
306
307static int radeon_dp_get_dp_link_clock(struct drm_connector *connector,
308 u8 dpcd[DP_DPCD_SIZE],
309 int pix_clock)
310{
Alex Deuchereccea792012-03-26 15:12:54 -0400311 int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
Alex Deucher224d94b2011-05-20 04:34:28 -0400312 int lane_num, max_pix_clock;
313
Alex Deucherfdca78c2011-10-25 11:54:52 -0400314 if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
315 ENCODER_OBJECT_ID_NUTMEG)
Alex Deucher224d94b2011-05-20 04:34:28 -0400316 return 270000;
317
318 lane_num = radeon_dp_get_dp_lane_number(connector, dpcd, pix_clock);
319 max_pix_clock = dp_get_max_dp_pix_clock(162000, lane_num, bpp);
320 if (pix_clock <= max_pix_clock)
321 return 162000;
322 max_pix_clock = dp_get_max_dp_pix_clock(270000, lane_num, bpp);
323 if (pix_clock <= max_pix_clock)
324 return 270000;
325 if (radeon_connector_is_dp12_capable(connector)) {
326 max_pix_clock = dp_get_max_dp_pix_clock(540000, lane_num, bpp);
327 if (pix_clock <= max_pix_clock)
328 return 540000;
329 }
330
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200331 return drm_dp_max_link_rate(dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400332}
333
334static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
335 int action, int dp_clock,
336 u8 ucconfig, u8 lane_num)
337{
338 DP_ENCODER_SERVICE_PARAMETERS args;
339 int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
340
341 memset(&args, 0, sizeof(args));
342 args.ucLinkClock = dp_clock / 10;
343 args.ucConfig = ucconfig;
344 args.ucAction = action;
345 args.ucLaneNum = lane_num;
346 args.ucStatus = 0;
347
348 atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
349 return args.ucStatus;
350}
351
352u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
353{
Alex Deucher224d94b2011-05-20 04:34:28 -0400354 struct drm_device *dev = radeon_connector->base.dev;
355 struct radeon_device *rdev = dev->dev_private;
356
357 return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
Alex Deucher379dfc22014-04-07 10:33:46 -0400358 radeon_connector->ddc_bus->rec.i2c_id, 0);
Alex Deucher224d94b2011-05-20 04:34:28 -0400359}
360
Adam Jackson40c5d872012-05-14 16:05:48 -0400361static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
362{
363 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
364 u8 buf[3];
365
366 if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
367 return;
368
Alex Deucheraa019b72014-04-30 09:27:15 -0400369 if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_SINK_OUI, buf, 3) == 3)
Adam Jackson40c5d872012-05-14 16:05:48 -0400370 DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
371 buf[0], buf[1], buf[2]);
372
Alex Deucheraa019b72014-04-30 09:27:15 -0400373 if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_BRANCH_OUI, buf, 3) == 3)
Adam Jackson40c5d872012-05-14 16:05:48 -0400374 DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
375 buf[0], buf[1], buf[2]);
376}
377
Alex Deucher224d94b2011-05-20 04:34:28 -0400378bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
379{
380 struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200381 u8 msg[DP_DPCD_SIZE];
Alex Deucher224d94b2011-05-20 04:34:28 -0400382 int ret, i;
383
Alex Deucher379dfc22014-04-07 10:33:46 -0400384 ret = drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_DPCD_REV, msg,
Alex Deucher496263b2014-03-21 10:34:07 -0400385 DP_DPCD_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400386 if (ret > 0) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200387 memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400388 DRM_DEBUG_KMS("DPCD: ");
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200389 for (i = 0; i < DP_DPCD_SIZE; i++)
Alex Deucher224d94b2011-05-20 04:34:28 -0400390 DRM_DEBUG_KMS("%02x ", msg[i]);
391 DRM_DEBUG_KMS("\n");
Adam Jackson40c5d872012-05-14 16:05:48 -0400392
393 radeon_dp_probe_oui(radeon_connector);
394
Alex Deucher224d94b2011-05-20 04:34:28 -0400395 return true;
396 }
397 dig_connector->dpcd[0] = 0;
398 return false;
399}
400
Alex Deucher386d4d72012-01-20 15:01:29 -0500401int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
402 struct drm_connector *connector)
Alex Deucher224d94b2011-05-20 04:34:28 -0400403{
404 struct drm_device *dev = encoder->dev;
405 struct radeon_device *rdev = dev->dev_private;
Alex Deucher00dfb8d2011-10-31 08:54:41 -0400406 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
Alex Deucher496263b2014-03-21 10:34:07 -0400407 struct radeon_connector_atom_dig *dig_connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400408 int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
Alex Deucher0ceb9962012-08-27 17:48:18 -0400409 u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
410 u8 tmp;
Alex Deucher224d94b2011-05-20 04:34:28 -0400411
412 if (!ASIC_IS_DCE4(rdev))
Alex Deucher386d4d72012-01-20 15:01:29 -0500413 return panel_mode;
Alex Deucher224d94b2011-05-20 04:34:28 -0400414
Alex Deucher496263b2014-03-21 10:34:07 -0400415 if (!radeon_connector->con_priv)
416 return panel_mode;
417
418 dig_connector = radeon_connector->con_priv;
419
Alex Deucher0ceb9962012-08-27 17:48:18 -0400420 if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
421 /* DP bridge chips */
Alex Deucheraa019b72014-04-30 09:27:15 -0400422 if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
423 DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
424 if (tmp & 1)
425 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
426 else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
427 (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
428 panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
429 else
430 panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
431 }
Alex Deucher304a4842012-02-02 10:18:00 -0500432 } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
Alex Deucher0ceb9962012-08-27 17:48:18 -0400433 /* eDP */
Alex Deucheraa019b72014-04-30 09:27:15 -0400434 if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
435 DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
436 if (tmp & 1)
437 panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
438 }
Alex Deucher00dfb8d2011-10-31 08:54:41 -0400439 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400440
Alex Deucher386d4d72012-01-20 15:01:29 -0500441 return panel_mode;
Alex Deucher224d94b2011-05-20 04:34:28 -0400442}
443
444void radeon_dp_set_link_config(struct drm_connector *connector,
Laurent Pincharte811f5a2012-07-17 17:56:50 +0200445 const struct drm_display_mode *mode)
Alex Deucher224d94b2011-05-20 04:34:28 -0400446{
447 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
448 struct radeon_connector_atom_dig *dig_connector;
449
450 if (!radeon_connector->con_priv)
451 return;
452 dig_connector = radeon_connector->con_priv;
453
454 if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
455 (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
456 dig_connector->dp_clock =
457 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
458 dig_connector->dp_lane_count =
459 radeon_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
460 }
461}
462
463int radeon_dp_mode_valid_helper(struct drm_connector *connector,
464 struct drm_display_mode *mode)
465{
466 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
467 struct radeon_connector_atom_dig *dig_connector;
468 int dp_clock;
469
470 if (!radeon_connector->con_priv)
471 return MODE_CLOCK_HIGH;
472 dig_connector = radeon_connector->con_priv;
473
474 dp_clock =
475 radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
476
477 if ((dp_clock == 540000) &&
478 (!radeon_connector_is_dp12_capable(connector)))
479 return MODE_CLOCK_HIGH;
480
481 return MODE_OK;
482}
483
Alex Deucherd5811e82011-08-13 13:36:13 -0400484bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
485{
486 u8 link_status[DP_LINK_STATUS_SIZE];
487 struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
488
Alex Deucher379dfc22014-04-07 10:33:46 -0400489 if (drm_dp_dpcd_read_link_status(&radeon_connector->ddc_bus->aux, link_status)
490 <= 0)
Alex Deucherd5811e82011-08-13 13:36:13 -0400491 return false;
Daniel Vetter1ffdff12012-10-18 10:15:24 +0200492 if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
Alex Deucherd5811e82011-08-13 13:36:13 -0400493 return false;
494 return true;
495}
496
Alex Deucher2953da12014-03-17 23:48:15 -0400497void radeon_dp_set_rx_power_state(struct drm_connector *connector,
498 u8 power_state)
499{
500 struct radeon_connector *radeon_connector = to_radeon_connector(connector);
501 struct radeon_connector_atom_dig *dig_connector;
502
503 if (!radeon_connector->con_priv)
504 return;
505
506 dig_connector = radeon_connector->con_priv;
507
508 /* power up/down the sink */
509 if (dig_connector->dpcd[0] >= 0x11) {
Alex Deucher379dfc22014-04-07 10:33:46 -0400510 drm_dp_dpcd_writeb(&radeon_connector->ddc_bus->aux,
Alex Deucher2953da12014-03-17 23:48:15 -0400511 DP_SET_POWER, power_state);
512 usleep_range(1000, 2000);
513 }
514}
515
516
Alex Deucher224d94b2011-05-20 04:34:28 -0400517struct radeon_dp_link_train_info {
518 struct radeon_device *rdev;
519 struct drm_encoder *encoder;
520 struct drm_connector *connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400521 int enc_id;
522 int dp_clock;
523 int dp_lane_count;
Alex Deucher224d94b2011-05-20 04:34:28 -0400524 bool tp3_supported;
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200525 u8 dpcd[DP_RECEIVER_CAP_SIZE];
Alex Deucher224d94b2011-05-20 04:34:28 -0400526 u8 train_set[4];
527 u8 link_status[DP_LINK_STATUS_SIZE];
528 u8 tries;
Jerome Glisse5a96a892011-07-25 11:57:43 -0400529 bool use_dpencoder;
Alex Deucher496263b2014-03-21 10:34:07 -0400530 struct drm_dp_aux *aux;
Alex Deucher224d94b2011-05-20 04:34:28 -0400531};
532
533static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
534{
535 /* set the initial vs/emph on the source */
536 atombios_dig_transmitter_setup(dp_info->encoder,
537 ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
538 0, dp_info->train_set[0]); /* sets all lanes at once */
539
540 /* set the vs/emph on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400541 drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
542 dp_info->train_set, dp_info->dp_lane_count);
Alex Deucher224d94b2011-05-20 04:34:28 -0400543}
544
545static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
546{
547 int rtp = 0;
548
549 /* set training pattern on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400550 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400551 switch (tp) {
552 case DP_TRAINING_PATTERN_1:
553 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
554 break;
555 case DP_TRAINING_PATTERN_2:
556 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
557 break;
558 case DP_TRAINING_PATTERN_3:
559 rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
560 break;
561 }
562 atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
563 } else {
564 switch (tp) {
565 case DP_TRAINING_PATTERN_1:
566 rtp = 0;
567 break;
568 case DP_TRAINING_PATTERN_2:
569 rtp = 1;
570 break;
571 }
572 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
573 dp_info->dp_clock, dp_info->enc_id, rtp);
574 }
575
576 /* enable training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400577 drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400578}
579
580static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
581{
Alex Deucher386d4d72012-01-20 15:01:29 -0500582 struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
583 struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
Alex Deucher224d94b2011-05-20 04:34:28 -0400584 u8 tmp;
585
586 /* power up the sink */
Alex Deucher2953da12014-03-17 23:48:15 -0400587 radeon_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
Alex Deucher224d94b2011-05-20 04:34:28 -0400588
589 /* possibly enable downspread on the sink */
590 if (dp_info->dpcd[3] & 0x1)
Alex Deucher496263b2014-03-21 10:34:07 -0400591 drm_dp_dpcd_writeb(dp_info->aux,
592 DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
Alex Deucher224d94b2011-05-20 04:34:28 -0400593 else
Alex Deucher496263b2014-03-21 10:34:07 -0400594 drm_dp_dpcd_writeb(dp_info->aux,
595 DP_DOWNSPREAD_CTRL, 0);
Alex Deucher224d94b2011-05-20 04:34:28 -0400596
Alex Deucher386d4d72012-01-20 15:01:29 -0500597 if ((dp_info->connector->connector_type == DRM_MODE_CONNECTOR_eDP) &&
598 (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)) {
Alex Deucher496263b2014-03-21 10:34:07 -0400599 drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
Alex Deucher386d4d72012-01-20 15:01:29 -0500600 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400601
602 /* set the lane count on the sink */
603 tmp = dp_info->dp_lane_count;
Jani Nikula27f75dc62013-10-04 15:08:09 +0300604 if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
Alex Deucher224d94b2011-05-20 04:34:28 -0400605 tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
Alex Deucher496263b2014-03-21 10:34:07 -0400606 drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400607
608 /* set the link rate on the sink */
Daniel Vetter3b5c6622012-10-18 10:15:31 +0200609 tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
Alex Deucher496263b2014-03-21 10:34:07 -0400610 drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
Alex Deucher224d94b2011-05-20 04:34:28 -0400611
612 /* start training on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400613 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
Alex Deucher224d94b2011-05-20 04:34:28 -0400614 atombios_dig_encoder_setup(dp_info->encoder,
615 ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
616 else
617 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
618 dp_info->dp_clock, dp_info->enc_id, 0);
619
620 /* disable the training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400621 drm_dp_dpcd_writeb(dp_info->aux,
622 DP_TRAINING_PATTERN_SET,
623 DP_TRAINING_PATTERN_DISABLE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400624
625 return 0;
626}
627
628static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
629{
630 udelay(400);
631
632 /* disable the training pattern on the sink */
Alex Deucher496263b2014-03-21 10:34:07 -0400633 drm_dp_dpcd_writeb(dp_info->aux,
634 DP_TRAINING_PATTERN_SET,
635 DP_TRAINING_PATTERN_DISABLE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400636
637 /* disable the training pattern on the source */
Jerome Glisse5a96a892011-07-25 11:57:43 -0400638 if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
Alex Deucher224d94b2011-05-20 04:34:28 -0400639 atombios_dig_encoder_setup(dp_info->encoder,
640 ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
641 else
642 radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
643 dp_info->dp_clock, dp_info->enc_id, 0);
644
645 return 0;
646}
647
648static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
649{
650 bool clock_recovery;
651 u8 voltage;
652 int i;
653
654 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
655 memset(dp_info->train_set, 0, 4);
656 radeon_dp_update_vs_emph(dp_info);
657
658 udelay(400);
659
660 /* clock recovery loop */
661 clock_recovery = false;
662 dp_info->tries = 0;
663 voltage = 0xff;
664 while (1) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200665 drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400666
Alex Deucherab8f1a22014-03-21 10:34:08 -0400667 if (drm_dp_dpcd_read_link_status(dp_info->aux,
668 dp_info->link_status) <= 0) {
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400669 DRM_ERROR("displayport link status failed\n");
Alex Deucher224d94b2011-05-20 04:34:28 -0400670 break;
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400671 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400672
Daniel Vetter01916272012-10-18 10:15:25 +0200673 if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400674 clock_recovery = true;
675 break;
676 }
677
678 for (i = 0; i < dp_info->dp_lane_count; i++) {
679 if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
680 break;
681 }
682 if (i == dp_info->dp_lane_count) {
683 DRM_ERROR("clock recovery reached max voltage\n");
684 break;
685 }
686
687 if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
688 ++dp_info->tries;
689 if (dp_info->tries == 5) {
690 DRM_ERROR("clock recovery tried 5 times\n");
691 break;
692 }
693 } else
694 dp_info->tries = 0;
695
696 voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
697
698 /* Compute new train_set as requested by sink */
699 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
700
701 radeon_dp_update_vs_emph(dp_info);
702 }
703 if (!clock_recovery) {
704 DRM_ERROR("clock recovery failed\n");
705 return -1;
706 } else {
707 DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
708 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
709 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
710 DP_TRAIN_PRE_EMPHASIS_SHIFT);
711 return 0;
712 }
713}
714
715static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
716{
717 bool channel_eq;
718
719 if (dp_info->tp3_supported)
720 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
721 else
722 radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
723
724 /* channel equalization loop */
725 dp_info->tries = 0;
726 channel_eq = false;
727 while (1) {
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200728 drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
Alex Deucher224d94b2011-05-20 04:34:28 -0400729
Alex Deucherab8f1a22014-03-21 10:34:08 -0400730 if (drm_dp_dpcd_read_link_status(dp_info->aux,
731 dp_info->link_status) <= 0) {
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400732 DRM_ERROR("displayport link status failed\n");
Alex Deucher224d94b2011-05-20 04:34:28 -0400733 break;
Jerome Glisse8d1c7022012-07-17 17:17:16 -0400734 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400735
Daniel Vetter1ffdff12012-10-18 10:15:24 +0200736 if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
Alex Deucher224d94b2011-05-20 04:34:28 -0400737 channel_eq = true;
738 break;
739 }
740
741 /* Try 5 times */
742 if (dp_info->tries > 5) {
743 DRM_ERROR("channel eq failed: 5 tries\n");
744 break;
745 }
746
747 /* Compute new train_set as requested by sink */
748 dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
749
750 radeon_dp_update_vs_emph(dp_info);
751 dp_info->tries++;
752 }
753
754 if (!channel_eq) {
755 DRM_ERROR("channel eq failed\n");
756 return -1;
757 } else {
758 DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
759 dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
760 (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
761 >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
762 return 0;
763 }
764}
765
766void radeon_dp_link_train(struct drm_encoder *encoder,
767 struct drm_connector *connector)
768{
769 struct drm_device *dev = encoder->dev;
770 struct radeon_device *rdev = dev->dev_private;
771 struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
772 struct radeon_encoder_atom_dig *dig;
773 struct radeon_connector *radeon_connector;
774 struct radeon_connector_atom_dig *dig_connector;
775 struct radeon_dp_link_train_info dp_info;
Jerome Glisse5a96a892011-07-25 11:57:43 -0400776 int index;
777 u8 tmp, frev, crev;
Alex Deucher224d94b2011-05-20 04:34:28 -0400778
779 if (!radeon_encoder->enc_priv)
780 return;
781 dig = radeon_encoder->enc_priv;
782
783 radeon_connector = to_radeon_connector(connector);
784 if (!radeon_connector->con_priv)
785 return;
786 dig_connector = radeon_connector->con_priv;
787
788 if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
789 (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
790 return;
791
Jerome Glisse5a96a892011-07-25 11:57:43 -0400792 /* DPEncoderService newer than 1.1 can't program properly the
793 * training pattern. When facing such version use the
794 * DIGXEncoderControl (X== 1 | 2)
795 */
796 dp_info.use_dpencoder = true;
797 index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
798 if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
799 if (crev > 1) {
800 dp_info.use_dpencoder = false;
801 }
802 }
803
Alex Deucher224d94b2011-05-20 04:34:28 -0400804 dp_info.enc_id = 0;
805 if (dig->dig_encoder)
806 dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
807 else
808 dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
809 if (dig->linkb)
810 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
811 else
812 dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
813
Alex Deucheraa019b72014-04-30 09:27:15 -0400814 if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux, DP_MAX_LANE_COUNT, &tmp)
815 == 1) {
816 if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
817 dp_info.tp3_supported = true;
818 else
819 dp_info.tp3_supported = false;
820 } else {
Alex Deucher224d94b2011-05-20 04:34:28 -0400821 dp_info.tp3_supported = false;
Alex Deucheraa019b72014-04-30 09:27:15 -0400822 }
Alex Deucher224d94b2011-05-20 04:34:28 -0400823
Daniel Vetter1a644cd2012-10-18 15:32:40 +0200824 memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
Alex Deucher224d94b2011-05-20 04:34:28 -0400825 dp_info.rdev = rdev;
826 dp_info.encoder = encoder;
827 dp_info.connector = connector;
Alex Deucher224d94b2011-05-20 04:34:28 -0400828 dp_info.dp_lane_count = dig_connector->dp_lane_count;
829 dp_info.dp_clock = dig_connector->dp_clock;
Alex Deucher379dfc22014-04-07 10:33:46 -0400830 dp_info.aux = &radeon_connector->ddc_bus->aux;
Alex Deucher224d94b2011-05-20 04:34:28 -0400831
832 if (radeon_dp_link_train_init(&dp_info))
833 goto done;
834 if (radeon_dp_link_train_cr(&dp_info))
835 goto done;
836 if (radeon_dp_link_train_ce(&dp_info))
837 goto done;
838done:
839 if (radeon_dp_link_train_finish(&dp_info))
840 return;
841}