1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved.
8 #include <drm/drm_connector.h>
9 #include <drm/drm_edid.h>
10 #include <drm/drm_print.h>
12 struct dp_panel_private {
14 struct drm_device *drm_dev;
15 struct dp_panel dp_panel;
16 struct drm_dp_aux *aux;
18 struct dp_catalog *catalog;
20 bool aux_cfg_update_done;
23 static void dp_panel_read_psr_cap(struct dp_panel_private *panel)
26 struct dp_panel *dp_panel;
28 dp_panel = &panel->dp_panel;
31 if (dp_panel->dpcd[DP_EDP_CONFIGURATION_CAP]) {
32 rlen = drm_dp_dpcd_read(panel->aux, DP_PSR_SUPPORT,
33 &dp_panel->psr_cap, sizeof(dp_panel->psr_cap));
34 if (rlen == sizeof(dp_panel->psr_cap)) {
35 drm_dbg_dp(panel->drm_dev,
36 "psr version: 0x%x, psr_cap: 0x%x\n",
37 dp_panel->psr_cap.version,
38 dp_panel->psr_cap.capabilities);
40 DRM_ERROR("failed to read psr info, rlen=%zd\n", rlen);
44 static int dp_panel_read_dpcd(struct dp_panel *dp_panel)
49 struct dp_panel_private *panel;
50 struct dp_link_info *link_info;
51 u8 *dpcd, major = 0, minor = 0, temp;
52 u32 offset = DP_DPCD_REV;
54 dpcd = dp_panel->dpcd;
56 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
57 link_info = &dp_panel->link_info;
59 rlen = drm_dp_dpcd_read(panel->aux, offset,
60 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
61 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
62 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
63 if (rlen == -ETIMEDOUT)
71 temp = dpcd[DP_TRAINING_AUX_RD_INTERVAL];
73 /* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
75 drm_dbg_dp(panel->drm_dev,
76 "using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
77 offset = DPRX_EXTENDED_DPCD_FIELD;
80 rlen = drm_dp_dpcd_read(panel->aux, offset,
81 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
82 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
83 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
84 if (rlen == -ETIMEDOUT)
92 link_info->revision = dpcd[DP_DPCD_REV];
93 major = (link_info->revision >> 4) & 0x0f;
94 minor = link_info->revision & 0x0f;
96 link_info->rate = drm_dp_bw_code_to_link_rate(dpcd[DP_MAX_LINK_RATE]);
97 link_info->num_lanes = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
99 /* Limit data lanes from data-lanes of endpoint property of dtsi */
100 if (link_info->num_lanes > dp_panel->max_dp_lanes)
101 link_info->num_lanes = dp_panel->max_dp_lanes;
103 /* Limit link rate from link-frequencies of endpoint property of dtsi */
104 if (link_info->rate > dp_panel->max_dp_link_rate)
105 link_info->rate = dp_panel->max_dp_link_rate;
107 drm_dbg_dp(panel->drm_dev, "version: %d.%d\n", major, minor);
108 drm_dbg_dp(panel->drm_dev, "link_rate=%d\n", link_info->rate);
109 drm_dbg_dp(panel->drm_dev, "lane_count=%d\n", link_info->num_lanes);
111 if (drm_dp_enhanced_frame_cap(dpcd))
112 link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
114 dp_panel->dfp_present = dpcd[DP_DOWNSTREAMPORT_PRESENT];
115 dp_panel->dfp_present &= DP_DWN_STRM_PORT_PRESENT;
117 if (dp_panel->dfp_present && (dpcd[DP_DPCD_REV] > 0x10)) {
118 dp_panel->ds_port_cnt = dpcd[DP_DOWN_STREAM_PORT_COUNT];
119 dp_panel->ds_port_cnt &= DP_PORT_COUNT_MASK;
120 len = DP_DOWNSTREAM_PORTS * DP_DOWNSTREAM_CAP_SIZE;
122 rlen = drm_dp_dpcd_read(panel->aux,
123 DP_DOWNSTREAM_PORT_0, dp_panel->ds_cap_info, len);
125 DRM_ERROR("ds port status failed, rlen=%zd\n", rlen);
131 dp_panel_read_psr_cap(panel);
136 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
137 u32 mode_edid_bpp, u32 mode_pclk_khz)
139 struct dp_link_info *link_info;
140 const u32 max_supported_bpp = 30, min_supported_bpp = 18;
141 u32 bpp = 0, data_rate_khz = 0;
143 bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
145 link_info = &dp_panel->link_info;
146 data_rate_khz = link_info->num_lanes * link_info->rate * 8;
148 while (bpp > min_supported_bpp) {
149 if (mode_pclk_khz * bpp <= data_rate_khz)
157 static int dp_panel_update_modes(struct drm_connector *connector,
163 rc = drm_connector_update_edid_property(connector, edid);
165 DRM_ERROR("failed to update edid property %d\n", rc);
168 rc = drm_add_edid_modes(connector, edid);
172 rc = drm_connector_update_edid_property(connector, NULL);
174 DRM_ERROR("failed to update edid property %d\n", rc);
179 int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
180 struct drm_connector *connector)
184 struct dp_panel_private *panel;
186 if (!dp_panel || !connector) {
187 DRM_ERROR("invalid input\n");
191 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
193 rc = dp_panel_read_dpcd(dp_panel);
195 DRM_ERROR("read dpcd failed %d\n", rc);
199 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
200 if (!is_link_rate_valid(bw_code) ||
201 !is_lane_count_valid(dp_panel->link_info.num_lanes) ||
202 (bw_code > dp_panel->max_bw_code)) {
203 DRM_ERROR("Illegal link rate=%d lane=%d\n", dp_panel->link_info.rate,
204 dp_panel->link_info.num_lanes);
208 if (dp_panel->dfp_present) {
209 rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
212 count = DP_GET_SINK_COUNT(count);
214 DRM_ERROR("no downstream ports connected\n");
215 panel->link->sink_count = 0;
222 kfree(dp_panel->edid);
223 dp_panel->edid = NULL;
225 dp_panel->edid = drm_get_edid(connector,
227 if (!dp_panel->edid) {
228 DRM_ERROR("panel edid read failed\n");
229 /* check edid read fail is due to unplug */
230 if (!dp_catalog_link_is_connected(panel->catalog)) {
236 if (panel->aux_cfg_update_done) {
237 drm_dbg_dp(panel->drm_dev,
238 "read DPCD with updated AUX config\n");
239 rc = dp_panel_read_dpcd(dp_panel);
240 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
241 if (rc || !is_link_rate_valid(bw_code) ||
242 !is_lane_count_valid(dp_panel->link_info.num_lanes)
243 || (bw_code > dp_panel->max_bw_code)) {
244 DRM_ERROR("read dpcd failed %d\n", rc);
247 panel->aux_cfg_update_done = false;
253 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
254 u32 mode_edid_bpp, u32 mode_pclk_khz)
256 struct dp_panel_private *panel;
259 if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
260 DRM_ERROR("invalid input\n");
264 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
266 if (dp_panel->video_test)
267 bpp = dp_link_bit_depth_to_bpp(
268 panel->link->test_video.test_bit_depth);
270 bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
276 int dp_panel_get_modes(struct dp_panel *dp_panel,
277 struct drm_connector *connector)
280 DRM_ERROR("invalid input\n");
285 return dp_panel_update_modes(connector, dp_panel->edid);
290 static u8 dp_panel_get_edid_checksum(struct edid *edid)
292 struct edid *last_block;
294 bool is_edid_corrupt = false;
297 DRM_ERROR("invalid edid input\n");
301 raw_edid = (u8 *)edid;
302 raw_edid += (edid->extensions * EDID_LENGTH);
303 last_block = (struct edid *)raw_edid;
305 /* block type extension */
306 drm_edid_block_valid(raw_edid, 1, false, &is_edid_corrupt);
307 if (!is_edid_corrupt)
308 return last_block->checksum;
310 DRM_ERROR("Invalid block, no checksum\n");
314 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
316 struct dp_panel_private *panel;
319 DRM_ERROR("invalid input\n");
323 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
325 if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
329 checksum = dp_panel_get_edid_checksum(dp_panel->edid);
331 checksum = dp_panel->connector->real_edid_checksum;
333 dp_link_send_edid_checksum(panel->link, checksum);
334 dp_link_send_test_response(panel->link);
338 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
340 struct dp_catalog *catalog;
341 struct dp_panel_private *panel;
344 DRM_ERROR("invalid input\n");
348 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
349 catalog = panel->catalog;
351 if (!panel->panel_on) {
352 drm_dbg_dp(panel->drm_dev,
353 "DP panel not enabled, handle TPG on next on\n");
358 dp_catalog_panel_tpg_disable(catalog);
362 drm_dbg_dp(panel->drm_dev, "calling catalog tpg_enable\n");
363 dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode);
366 void dp_panel_dump_regs(struct dp_panel *dp_panel)
368 struct dp_catalog *catalog;
369 struct dp_panel_private *panel;
371 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
372 catalog = panel->catalog;
374 dp_catalog_dump_regs(catalog);
377 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
379 u32 data, total_ver, total_hor;
380 struct dp_catalog *catalog;
381 struct dp_panel_private *panel;
382 struct drm_display_mode *drm_mode;
384 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
385 catalog = panel->catalog;
386 drm_mode = &panel->dp_panel.dp_mode.drm_mode;
388 drm_dbg_dp(panel->drm_dev, "width=%d hporch= %d %d %d\n",
389 drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end,
390 drm_mode->hsync_start - drm_mode->hdisplay,
391 drm_mode->hsync_end - drm_mode->hsync_start);
393 drm_dbg_dp(panel->drm_dev, "height=%d vporch= %d %d %d\n",
394 drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end,
395 drm_mode->vsync_start - drm_mode->vdisplay,
396 drm_mode->vsync_end - drm_mode->vsync_start);
398 total_hor = drm_mode->htotal;
400 total_ver = drm_mode->vtotal;
406 catalog->total = data;
408 data = (drm_mode->vtotal - drm_mode->vsync_start);
410 data |= (drm_mode->htotal - drm_mode->hsync_start);
412 catalog->sync_start = data;
414 data = drm_mode->vsync_end - drm_mode->vsync_start;
416 data |= (panel->dp_panel.dp_mode.v_active_low << 31);
417 data |= drm_mode->hsync_end - drm_mode->hsync_start;
418 data |= (panel->dp_panel.dp_mode.h_active_low << 15);
420 catalog->width_blanking = data;
422 data = drm_mode->vdisplay;
424 data |= drm_mode->hdisplay;
426 catalog->dp_active = data;
428 dp_catalog_panel_timing_cfg(catalog);
429 panel->panel_on = true;
434 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
436 struct drm_display_mode *drm_mode;
437 struct dp_panel_private *panel;
439 drm_mode = &dp_panel->dp_mode.drm_mode;
441 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
444 * print resolution info as this is a result
445 * of user initiated action of cable connection
447 drm_dbg_dp(panel->drm_dev, "SET NEW RESOLUTION:\n");
448 drm_dbg_dp(panel->drm_dev, "%dx%d@%dfps\n",
449 drm_mode->hdisplay, drm_mode->vdisplay, drm_mode_vrefresh(drm_mode));
450 drm_dbg_dp(panel->drm_dev,
451 "h_porches(back|front|width) = (%d|%d|%d)\n",
452 drm_mode->htotal - drm_mode->hsync_end,
453 drm_mode->hsync_start - drm_mode->hdisplay,
454 drm_mode->hsync_end - drm_mode->hsync_start);
455 drm_dbg_dp(panel->drm_dev,
456 "v_porches(back|front|width) = (%d|%d|%d)\n",
457 drm_mode->vtotal - drm_mode->vsync_end,
458 drm_mode->vsync_start - drm_mode->vdisplay,
459 drm_mode->vsync_end - drm_mode->vsync_start);
460 drm_dbg_dp(panel->drm_dev, "pixel clock (KHz)=(%d)\n",
462 drm_dbg_dp(panel->drm_dev, "bpp = %d\n", dp_panel->dp_mode.bpp);
464 dp_panel->dp_mode.bpp = max_t(u32, 18,
465 min_t(u32, dp_panel->dp_mode.bpp, 30));
466 drm_dbg_dp(panel->drm_dev, "updated bpp = %d\n",
467 dp_panel->dp_mode.bpp);
472 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
474 struct dp_panel_private *panel;
475 struct dp_panel *dp_panel;
477 if (!in->dev || !in->catalog || !in->aux || !in->link) {
478 DRM_ERROR("invalid input\n");
479 return ERR_PTR(-EINVAL);
482 panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
484 return ERR_PTR(-ENOMEM);
486 panel->dev = in->dev;
487 panel->aux = in->aux;
488 panel->catalog = in->catalog;
489 panel->link = in->link;
491 dp_panel = &panel->dp_panel;
492 dp_panel->max_bw_code = DP_LINK_BW_8_1;
493 panel->aux_cfg_update_done = false;
498 void dp_panel_put(struct dp_panel *dp_panel)
503 kfree(dp_panel->edid);