2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/string.h>
27 #include <linux/acpi.h>
28 #include <linux/version.h>
29 #include <linux/i2c.h>
31 #include <drm/drm_probe_helper.h>
32 #include <drm/amdgpu_drm.h>
33 #include <drm/drm_edid.h>
35 #include "dm_services.h"
38 #include "amdgpu_dm.h"
39 #include "amdgpu_dm_irq.h"
41 #include "dm_helpers.h"
43 /* dm_helpers_parse_edid_caps
47 * @edid: [in] pointer to edid
48 * edid_caps: [in] pointer to edid caps
52 enum dc_edid_status dm_helpers_parse_edid_caps(
53 struct dc_context *ctx,
54 const struct dc_edid *edid,
55 struct dc_edid_caps *edid_caps)
57 struct edid *edid_buf = (struct edid *) edid->raw_edid;
65 enum dc_edid_status result = EDID_OK;
67 if (!edid_caps || !edid)
68 return EDID_BAD_INPUT;
70 if (!drm_edid_is_valid(edid_buf))
71 result = EDID_BAD_CHECKSUM;
73 edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
74 ((uint16_t) edid_buf->mfg_id[1])<<8;
75 edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
76 ((uint16_t) edid_buf->prod_code[1])<<8;
77 edid_caps->serial_number = edid_buf->serial;
78 edid_caps->manufacture_week = edid_buf->mfg_week;
79 edid_caps->manufacture_year = edid_buf->mfg_year;
81 /* One of the four detailed_timings stores the monitor name. It's
82 * stored in an array of length 13. */
83 for (i = 0; i < 4; i++) {
84 if (edid_buf->detailed_timings[i].data.other_data.type == 0xfc) {
85 while (j < 13 && edid_buf->detailed_timings[i].data.other_data.data.str.str[j]) {
86 if (edid_buf->detailed_timings[i].data.other_data.data.str.str[j] == '\n')
89 edid_caps->display_name[j] =
90 edid_buf->detailed_timings[i].data.other_data.data.str.str[j];
96 edid_caps->edid_hdmi = drm_detect_hdmi_monitor(
97 (struct edid *) edid->raw_edid);
99 sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
101 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
105 edid_caps->audio_mode_count = sad_count < DC_MAX_AUDIO_DESC_COUNT ? sad_count : DC_MAX_AUDIO_DESC_COUNT;
106 for (i = 0; i < edid_caps->audio_mode_count; ++i) {
107 struct cea_sad *sad = &sads[i];
109 edid_caps->audio_modes[i].format_code = sad->format;
110 edid_caps->audio_modes[i].channel_count = sad->channels + 1;
111 edid_caps->audio_modes[i].sample_rate = sad->freq;
112 edid_caps->audio_modes[i].sample_size = sad->byte2;
115 sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
117 if (sadb_count < 0) {
118 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
123 edid_caps->speaker_flags = sadb[0];
125 edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
133 static void get_payload_table(
134 struct amdgpu_dm_connector *aconnector,
135 struct dp_mst_stream_allocation_table *proposed_table)
138 struct drm_dp_mst_topology_mgr *mst_mgr =
139 &aconnector->mst_port->mst_mgr;
141 mutex_lock(&mst_mgr->payload_lock);
143 proposed_table->stream_count = 0;
145 /* number of active streams */
146 for (i = 0; i < mst_mgr->max_payloads; i++) {
147 if (mst_mgr->payloads[i].num_slots == 0)
148 break; /* end of vcp_id table */
150 ASSERT(mst_mgr->payloads[i].payload_state !=
151 DP_PAYLOAD_DELETE_LOCAL);
153 if (mst_mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL ||
154 mst_mgr->payloads[i].payload_state ==
157 struct dp_mst_stream_allocation *sa =
158 &proposed_table->stream_allocations[
159 proposed_table->stream_count];
161 sa->slot_count = mst_mgr->payloads[i].num_slots;
162 sa->vcp_id = mst_mgr->proposed_vcpis[i]->vcpi;
163 proposed_table->stream_count++;
167 mutex_unlock(&mst_mgr->payload_lock);
170 void dm_helpers_dp_update_branch_info(
171 struct dc_context *ctx,
172 const struct dc_link *link)
176 * Writes payload allocation table in immediate downstream device.
178 bool dm_helpers_dp_mst_write_payload_allocation_table(
179 struct dc_context *ctx,
180 const struct dc_stream_state *stream,
181 struct dp_mst_stream_allocation_table *proposed_table,
184 struct amdgpu_dm_connector *aconnector;
185 struct drm_dp_mst_topology_mgr *mst_mgr;
186 struct drm_dp_mst_port *mst_port;
193 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
195 if (!aconnector || !aconnector->mst_port)
198 mst_mgr = &aconnector->mst_port->mst_mgr;
200 if (!mst_mgr->mst_state)
203 mst_port = aconnector->port;
206 clock = stream->timing.pix_clk_100hz / 10;
208 switch (stream->timing.display_color_depth) {
210 case COLOR_DEPTH_666:
213 case COLOR_DEPTH_888:
216 case COLOR_DEPTH_101010:
219 case COLOR_DEPTH_121212:
222 case COLOR_DEPTH_141414:
225 case COLOR_DEPTH_161616:
235 /* TODO need to know link rate */
237 pbn = drm_dp_calc_pbn_mode(clock, bpp);
239 slots = drm_dp_find_vcpi_slots(mst_mgr, pbn);
240 ret = drm_dp_mst_allocate_vcpi(mst_mgr, mst_port, pbn, slots);
246 drm_dp_mst_reset_vcpi_slots(mst_mgr, mst_port);
249 ret = drm_dp_update_payload_part1(mst_mgr);
251 /* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
252 * AUX message. The sequence is slot 1-63 allocated sequence for each
253 * stream. AMD ASIC stream slot allocation should follow the same
254 * sequence. copy DRM MST allocation to dc */
256 get_payload_table(aconnector, proposed_table);
265 * poll pending down reply
267 void dm_helpers_dp_mst_poll_pending_down_reply(
268 struct dc_context *ctx,
269 const struct dc_link *link)
273 * Clear payload allocation table before enable MST DP link.
275 void dm_helpers_dp_mst_clear_payload_allocation_table(
276 struct dc_context *ctx,
277 const struct dc_link *link)
281 * Polls for ACT (allocation change trigger) handled and sends
282 * ALLOCATE_PAYLOAD message.
284 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
285 struct dc_context *ctx,
286 const struct dc_stream_state *stream)
288 struct amdgpu_dm_connector *aconnector;
289 struct drm_dp_mst_topology_mgr *mst_mgr;
292 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
294 if (!aconnector || !aconnector->mst_port)
297 mst_mgr = &aconnector->mst_port->mst_mgr;
299 if (!mst_mgr->mst_state)
302 ret = drm_dp_check_act_status(mst_mgr);
310 bool dm_helpers_dp_mst_send_payload_allocation(
311 struct dc_context *ctx,
312 const struct dc_stream_state *stream,
315 struct amdgpu_dm_connector *aconnector;
316 struct drm_dp_mst_topology_mgr *mst_mgr;
317 struct drm_dp_mst_port *mst_port;
320 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
322 if (!aconnector || !aconnector->mst_port)
325 mst_port = aconnector->port;
327 mst_mgr = &aconnector->mst_port->mst_mgr;
329 if (!mst_mgr->mst_state)
332 ret = drm_dp_update_payload_part2(mst_mgr);
338 drm_dp_mst_deallocate_vcpi(mst_mgr, mst_port);
343 void dm_dtn_log_begin(struct dc_context *ctx,
344 struct dc_log_buffer_ctx *log_ctx)
346 static const char msg[] = "[dtn begin]\n";
353 dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
356 void dm_dtn_log_append_v(struct dc_context *ctx,
357 struct dc_log_buffer_ctx *log_ctx,
358 const char *msg, ...)
365 /* No context, redirect to dmesg. */
366 struct va_format vaf;
372 pr_info("%pV", &vaf);
378 /* Measure the output. */
380 n = vsnprintf(NULL, 0, msg, args);
386 /* Reallocate the string buffer as needed. */
387 total = log_ctx->pos + n + 1;
389 if (total > log_ctx->size) {
390 char *buf = (char *)kvcalloc(total, sizeof(char), GFP_KERNEL);
393 memcpy(buf, log_ctx->buf, log_ctx->pos);
397 log_ctx->size = total;
404 /* Write the formatted string to the log buffer. */
407 log_ctx->buf + log_ctx->pos,
408 log_ctx->size - log_ctx->pos,
417 void dm_dtn_log_end(struct dc_context *ctx,
418 struct dc_log_buffer_ctx *log_ctx)
420 static const char msg[] = "[dtn end]\n";
427 dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
430 bool dm_helpers_dp_mst_start_top_mgr(
431 struct dc_context *ctx,
432 const struct dc_link *link,
435 struct amdgpu_dm_connector *aconnector = link->priv;
438 DRM_ERROR("Failed to found connector for link!");
443 DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
444 aconnector, aconnector->base.base.id);
448 DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
449 aconnector, aconnector->base.base.id);
451 return (drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true) == 0);
454 void dm_helpers_dp_mst_stop_top_mgr(
455 struct dc_context *ctx,
456 const struct dc_link *link)
458 struct amdgpu_dm_connector *aconnector = link->priv;
461 DRM_ERROR("Failed to found connector for link!");
465 DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
466 aconnector, aconnector->base.base.id);
468 if (aconnector->mst_mgr.mst_state == true)
469 drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
472 bool dm_helpers_dp_read_dpcd(
473 struct dc_context *ctx,
474 const struct dc_link *link,
480 struct amdgpu_dm_connector *aconnector = link->priv;
483 DRM_ERROR("Failed to found connector for link!");
487 return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address,
491 bool dm_helpers_dp_write_dpcd(
492 struct dc_context *ctx,
493 const struct dc_link *link,
498 struct amdgpu_dm_connector *aconnector = link->priv;
501 DRM_ERROR("Failed to found connector for link!");
505 return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
506 address, (uint8_t *)data, size) > 0;
509 bool dm_helpers_submit_i2c(
510 struct dc_context *ctx,
511 const struct dc_link *link,
512 struct i2c_command *cmd)
514 struct amdgpu_dm_connector *aconnector = link->priv;
515 struct i2c_msg *msgs;
517 int num = cmd->number_of_payloads;
521 DRM_ERROR("Failed to found connector for link!");
525 msgs = kcalloc(num, sizeof(struct i2c_msg), GFP_KERNEL);
530 for (i = 0; i < num; i++) {
531 msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
532 msgs[i].addr = cmd->payloads[i].address;
533 msgs[i].len = cmd->payloads[i].length;
534 msgs[i].buf = cmd->payloads[i].data;
537 result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
543 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
544 bool dm_helpers_dp_write_dsc_enable(
545 struct dc_context *ctx,
546 const struct dc_stream_state *stream,
550 uint8_t enable_dsc = enable ? 1 : 0;
552 return dm_helpers_dp_write_dpcd(ctx, stream->sink->link, DP_DSC_ENABLE, &enable_dsc, 1);
556 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
558 bool dp_sink_present;
559 struct amdgpu_dm_connector *aconnector = link->priv;
562 BUG_ON("Failed to found connector for link!");
566 mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
567 dp_sink_present = dc_link_is_dp_sink_present(link);
568 mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
569 return dp_sink_present;
572 enum dc_edid_status dm_helpers_read_local_edid(
573 struct dc_context *ctx,
574 struct dc_link *link,
575 struct dc_sink *sink)
577 struct amdgpu_dm_connector *aconnector = link->priv;
578 struct i2c_adapter *ddc;
580 enum dc_edid_status edid_status;
584 ddc = &aconnector->dm_dp_aux.aux.ddc;
586 ddc = &aconnector->i2c->base;
588 /* some dongles read edid incorrectly the first time,
589 * do check sum and retry to make sure read correct edid.
593 edid = drm_get_edid(&aconnector->base, ddc);
596 return EDID_NO_RESPONSE;
598 sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
599 memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
601 /* We don't need the original edid anymore */
604 edid_status = dm_helpers_parse_edid_caps(
609 } while (edid_status == EDID_BAD_CHECKSUM && --retry > 0);
611 if (edid_status != EDID_OK)
612 DRM_ERROR("EDID err: %d, on connector: %s",
614 aconnector->base.name);
615 if (link->aux_mode) {
616 union test_request test_request = { {0} };
617 union test_response test_response = { {0} };
619 dm_helpers_dp_read_dpcd(ctx,
623 sizeof(union test_request));
625 if (!test_request.bits.EDID_READ)
628 test_response.bits.EDID_CHECKSUM_WRITE = 1;
630 dm_helpers_dp_write_dpcd(ctx,
632 DP_TEST_EDID_CHECKSUM,
633 &sink->dc_edid.raw_edid[sink->dc_edid.length-1],
636 dm_helpers_dp_write_dpcd(ctx,
640 sizeof(test_response));
647 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
649 /* TODO: something */