1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012-2020, The Linux Foundation. All rights reserved. 4 */ 5 6 #include "dp_panel.h" 7 #include "dp_utils.h" 8 9 #include <drm/drm_connector.h> 10 #include <drm/drm_edid.h> 11 #include <drm/drm_of.h> 12 #include <drm/drm_print.h> 13 14 #define DP_MAX_NUM_DP_LANES 4 15 #define DP_LINK_RATE_HBR2 540000 /* kbytes */ 16 17 struct dp_panel_private { 18 struct device *dev; 19 struct drm_device *drm_dev; 20 struct dp_panel dp_panel; 21 struct drm_dp_aux *aux; 22 struct dp_link *link; 23 struct dp_catalog *catalog; 24 bool panel_on; 25 }; 26 27 static void dp_panel_read_psr_cap(struct dp_panel_private *panel) 28 { 29 ssize_t rlen; 30 struct dp_panel *dp_panel; 31 32 dp_panel = &panel->dp_panel; 33 34 /* edp sink */ 35 if (dp_panel->dpcd[DP_EDP_CONFIGURATION_CAP]) { 36 rlen = drm_dp_dpcd_read(panel->aux, DP_PSR_SUPPORT, 37 &dp_panel->psr_cap, sizeof(dp_panel->psr_cap)); 38 if (rlen == sizeof(dp_panel->psr_cap)) { 39 drm_dbg_dp(panel->drm_dev, 40 "psr version: 0x%x, psr_cap: 0x%x\n", 41 dp_panel->psr_cap.version, 42 dp_panel->psr_cap.capabilities); 43 } else 44 DRM_ERROR("failed to read psr info, rlen=%zd\n", rlen); 45 } 46 } 47 48 static int dp_panel_read_dpcd(struct dp_panel *dp_panel) 49 { 50 int rc; 51 struct dp_panel_private *panel; 52 struct dp_link_info *link_info; 53 u8 *dpcd, major, minor; 54 55 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 56 dpcd = dp_panel->dpcd; 57 rc = drm_dp_read_dpcd_caps(panel->aux, dpcd); 58 if (rc) 59 return rc; 60 61 dp_panel->vsc_sdp_supported = drm_dp_vsc_sdp_supported(panel->aux, dpcd); 62 link_info = &dp_panel->link_info; 63 link_info->revision = dpcd[DP_DPCD_REV]; 64 major = (link_info->revision >> 4) & 0x0f; 65 minor = link_info->revision & 0x0f; 66 67 link_info->rate = drm_dp_max_link_rate(dpcd); 68 link_info->num_lanes = drm_dp_max_lane_count(dpcd); 69 70 /* Limit data lanes from data-lanes of endpoint property of dtsi */ 71 if (link_info->num_lanes > dp_panel->max_dp_lanes) 72 link_info->num_lanes = dp_panel->max_dp_lanes; 73 74 /* Limit link rate from link-frequencies of endpoint property of dtsi */ 75 if (link_info->rate > dp_panel->max_dp_link_rate) 76 link_info->rate = dp_panel->max_dp_link_rate; 77 78 drm_dbg_dp(panel->drm_dev, "version: %d.%d\n", major, minor); 79 drm_dbg_dp(panel->drm_dev, "link_rate=%d\n", link_info->rate); 80 drm_dbg_dp(panel->drm_dev, "lane_count=%d\n", link_info->num_lanes); 81 82 if (drm_dp_enhanced_frame_cap(dpcd)) 83 link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING; 84 85 dp_panel_read_psr_cap(panel); 86 87 return rc; 88 } 89 90 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel, 91 u32 mode_edid_bpp, u32 mode_pclk_khz) 92 { 93 struct dp_link_info *link_info; 94 const u32 max_supported_bpp = 30, min_supported_bpp = 18; 95 u32 bpp = 0, data_rate_khz = 0; 96 97 bpp = min_t(u32, mode_edid_bpp, max_supported_bpp); 98 99 link_info = &dp_panel->link_info; 100 data_rate_khz = link_info->num_lanes * link_info->rate * 8; 101 102 while (bpp > min_supported_bpp) { 103 if (mode_pclk_khz * bpp <= data_rate_khz) 104 break; 105 bpp -= 6; 106 } 107 108 return bpp; 109 } 110 111 static int dp_panel_update_modes(struct drm_connector *connector, 112 struct edid *edid) 113 { 114 int rc = 0; 115 116 if (edid) { 117 rc = drm_connector_update_edid_property(connector, edid); 118 if (rc) { 119 DRM_ERROR("failed to update edid property %d\n", rc); 120 return rc; 121 } 122 rc = drm_add_edid_modes(connector, edid); 123 return rc; 124 } 125 126 rc = drm_connector_update_edid_property(connector, NULL); 127 if (rc) 128 DRM_ERROR("failed to update edid property %d\n", rc); 129 130 return rc; 131 } 132 133 int dp_panel_read_sink_caps(struct dp_panel *dp_panel, 134 struct drm_connector *connector) 135 { 136 int rc, bw_code; 137 int count; 138 struct dp_panel_private *panel; 139 140 if (!dp_panel || !connector) { 141 DRM_ERROR("invalid input\n"); 142 return -EINVAL; 143 } 144 145 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 146 147 drm_dbg_dp(panel->drm_dev, "max_lanes=%d max_link_rate=%d\n", 148 dp_panel->max_dp_lanes, dp_panel->max_dp_link_rate); 149 150 rc = dp_panel_read_dpcd(dp_panel); 151 if (rc) { 152 DRM_ERROR("read dpcd failed %d\n", rc); 153 return rc; 154 } 155 156 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate); 157 if (!is_link_rate_valid(bw_code) || 158 !is_lane_count_valid(dp_panel->link_info.num_lanes) || 159 (bw_code > dp_panel->max_bw_code)) { 160 DRM_ERROR("Illegal link rate=%d lane=%d\n", dp_panel->link_info.rate, 161 dp_panel->link_info.num_lanes); 162 return -EINVAL; 163 } 164 165 if (drm_dp_is_branch(dp_panel->dpcd)) { 166 count = drm_dp_read_sink_count(panel->aux); 167 if (!count) { 168 panel->link->sink_count = 0; 169 return -ENOTCONN; 170 } 171 } 172 173 rc = drm_dp_read_downstream_info(panel->aux, dp_panel->dpcd, 174 dp_panel->downstream_ports); 175 if (rc) 176 return rc; 177 178 kfree(dp_panel->edid); 179 dp_panel->edid = NULL; 180 181 dp_panel->edid = drm_get_edid(connector, 182 &panel->aux->ddc); 183 if (!dp_panel->edid) { 184 DRM_ERROR("panel edid read failed\n"); 185 /* check edid read fail is due to unplug */ 186 if (!dp_catalog_link_is_connected(panel->catalog)) { 187 rc = -ETIMEDOUT; 188 goto end; 189 } 190 } 191 192 end: 193 return rc; 194 } 195 196 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel, 197 u32 mode_edid_bpp, u32 mode_pclk_khz) 198 { 199 struct dp_panel_private *panel; 200 u32 bpp; 201 202 if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) { 203 DRM_ERROR("invalid input\n"); 204 return 0; 205 } 206 207 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 208 209 if (dp_panel->video_test) 210 bpp = dp_link_bit_depth_to_bpp( 211 panel->link->test_video.test_bit_depth); 212 else 213 bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp, 214 mode_pclk_khz); 215 216 return bpp; 217 } 218 219 int dp_panel_get_modes(struct dp_panel *dp_panel, 220 struct drm_connector *connector) 221 { 222 if (!dp_panel) { 223 DRM_ERROR("invalid input\n"); 224 return -EINVAL; 225 } 226 227 if (dp_panel->edid) 228 return dp_panel_update_modes(connector, dp_panel->edid); 229 230 return 0; 231 } 232 233 static u8 dp_panel_get_edid_checksum(struct edid *edid) 234 { 235 edid += edid->extensions; 236 237 return edid->checksum; 238 } 239 240 void dp_panel_handle_sink_request(struct dp_panel *dp_panel) 241 { 242 struct dp_panel_private *panel; 243 244 if (!dp_panel) { 245 DRM_ERROR("invalid input\n"); 246 return; 247 } 248 249 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 250 251 if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) { 252 u8 checksum; 253 254 if (dp_panel->edid) 255 checksum = dp_panel_get_edid_checksum(dp_panel->edid); 256 else 257 checksum = dp_panel->connector->real_edid_checksum; 258 259 dp_link_send_edid_checksum(panel->link, checksum); 260 dp_link_send_test_response(panel->link); 261 } 262 } 263 264 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable) 265 { 266 struct dp_catalog *catalog; 267 struct dp_panel_private *panel; 268 269 if (!dp_panel) { 270 DRM_ERROR("invalid input\n"); 271 return; 272 } 273 274 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 275 catalog = panel->catalog; 276 277 if (!panel->panel_on) { 278 drm_dbg_dp(panel->drm_dev, 279 "DP panel not enabled, handle TPG on next on\n"); 280 return; 281 } 282 283 if (!enable) { 284 dp_catalog_panel_tpg_disable(catalog); 285 return; 286 } 287 288 drm_dbg_dp(panel->drm_dev, "calling catalog tpg_enable\n"); 289 dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode); 290 } 291 292 static int dp_panel_setup_vsc_sdp_yuv_420(struct dp_panel *dp_panel) 293 { 294 struct dp_catalog *catalog; 295 struct dp_panel_private *panel; 296 struct dp_display_mode *dp_mode; 297 struct drm_dp_vsc_sdp vsc_sdp_data; 298 struct dp_sdp vsc_sdp; 299 ssize_t len; 300 301 if (!dp_panel) { 302 DRM_ERROR("invalid input\n"); 303 return -EINVAL; 304 } 305 306 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 307 catalog = panel->catalog; 308 dp_mode = &dp_panel->dp_mode; 309 310 memset(&vsc_sdp_data, 0, sizeof(vsc_sdp_data)); 311 312 /* VSC SDP header as per table 2-118 of DP 1.4 specification */ 313 vsc_sdp_data.sdp_type = DP_SDP_VSC; 314 vsc_sdp_data.revision = 0x05; 315 vsc_sdp_data.length = 0x13; 316 317 /* VSC SDP Payload for DB16 */ 318 vsc_sdp_data.pixelformat = DP_PIXELFORMAT_YUV420; 319 vsc_sdp_data.colorimetry = DP_COLORIMETRY_DEFAULT; 320 321 /* VSC SDP Payload for DB17 */ 322 vsc_sdp_data.bpc = dp_mode->bpp / 3; 323 vsc_sdp_data.dynamic_range = DP_DYNAMIC_RANGE_CTA; 324 325 /* VSC SDP Payload for DB18 */ 326 vsc_sdp_data.content_type = DP_CONTENT_TYPE_GRAPHICS; 327 328 len = drm_dp_vsc_sdp_pack(&vsc_sdp_data, &vsc_sdp); 329 if (len < 0) { 330 DRM_ERROR("unable to pack vsc sdp\n"); 331 return len; 332 } 333 334 dp_catalog_panel_enable_vsc_sdp(catalog, &vsc_sdp); 335 336 return 0; 337 } 338 339 void dp_panel_dump_regs(struct dp_panel *dp_panel) 340 { 341 struct dp_catalog *catalog; 342 struct dp_panel_private *panel; 343 344 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 345 catalog = panel->catalog; 346 347 dp_catalog_dump_regs(catalog); 348 } 349 350 int dp_panel_timing_cfg(struct dp_panel *dp_panel) 351 { 352 u32 data, total_ver, total_hor; 353 struct dp_catalog *catalog; 354 struct dp_panel_private *panel; 355 struct drm_display_mode *drm_mode; 356 u32 width_blanking; 357 u32 sync_start; 358 u32 dp_active; 359 u32 total; 360 361 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 362 catalog = panel->catalog; 363 drm_mode = &panel->dp_panel.dp_mode.drm_mode; 364 365 drm_dbg_dp(panel->drm_dev, "width=%d hporch= %d %d %d\n", 366 drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end, 367 drm_mode->hsync_start - drm_mode->hdisplay, 368 drm_mode->hsync_end - drm_mode->hsync_start); 369 370 drm_dbg_dp(panel->drm_dev, "height=%d vporch= %d %d %d\n", 371 drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end, 372 drm_mode->vsync_start - drm_mode->vdisplay, 373 drm_mode->vsync_end - drm_mode->vsync_start); 374 375 total_hor = drm_mode->htotal; 376 377 total_ver = drm_mode->vtotal; 378 379 data = total_ver; 380 data <<= 16; 381 data |= total_hor; 382 383 total = data; 384 385 data = (drm_mode->vtotal - drm_mode->vsync_start); 386 data <<= 16; 387 data |= (drm_mode->htotal - drm_mode->hsync_start); 388 389 sync_start = data; 390 391 data = drm_mode->vsync_end - drm_mode->vsync_start; 392 data <<= 16; 393 data |= (panel->dp_panel.dp_mode.v_active_low << 31); 394 data |= drm_mode->hsync_end - drm_mode->hsync_start; 395 data |= (panel->dp_panel.dp_mode.h_active_low << 15); 396 397 width_blanking = data; 398 399 data = drm_mode->vdisplay; 400 data <<= 16; 401 data |= drm_mode->hdisplay; 402 403 dp_active = data; 404 405 dp_catalog_panel_timing_cfg(catalog, total, sync_start, width_blanking, dp_active); 406 407 if (dp_panel->dp_mode.out_fmt_is_yuv_420) 408 dp_panel_setup_vsc_sdp_yuv_420(dp_panel); 409 410 panel->panel_on = true; 411 412 return 0; 413 } 414 415 int dp_panel_init_panel_info(struct dp_panel *dp_panel) 416 { 417 struct drm_display_mode *drm_mode; 418 struct dp_panel_private *panel; 419 420 drm_mode = &dp_panel->dp_mode.drm_mode; 421 422 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 423 424 /* 425 * print resolution info as this is a result 426 * of user initiated action of cable connection 427 */ 428 drm_dbg_dp(panel->drm_dev, "SET NEW RESOLUTION:\n"); 429 drm_dbg_dp(panel->drm_dev, "%dx%d@%dfps\n", 430 drm_mode->hdisplay, drm_mode->vdisplay, drm_mode_vrefresh(drm_mode)); 431 drm_dbg_dp(panel->drm_dev, 432 "h_porches(back|front|width) = (%d|%d|%d)\n", 433 drm_mode->htotal - drm_mode->hsync_end, 434 drm_mode->hsync_start - drm_mode->hdisplay, 435 drm_mode->hsync_end - drm_mode->hsync_start); 436 drm_dbg_dp(panel->drm_dev, 437 "v_porches(back|front|width) = (%d|%d|%d)\n", 438 drm_mode->vtotal - drm_mode->vsync_end, 439 drm_mode->vsync_start - drm_mode->vdisplay, 440 drm_mode->vsync_end - drm_mode->vsync_start); 441 drm_dbg_dp(panel->drm_dev, "pixel clock (KHz)=(%d)\n", 442 drm_mode->clock); 443 drm_dbg_dp(panel->drm_dev, "bpp = %d\n", dp_panel->dp_mode.bpp); 444 445 dp_panel->dp_mode.bpp = max_t(u32, 18, 446 min_t(u32, dp_panel->dp_mode.bpp, 30)); 447 drm_dbg_dp(panel->drm_dev, "updated bpp = %d\n", 448 dp_panel->dp_mode.bpp); 449 450 return 0; 451 } 452 453 static u32 dp_panel_link_frequencies(struct device_node *of_node) 454 { 455 struct device_node *endpoint; 456 u64 frequency = 0; 457 int cnt; 458 459 endpoint = of_graph_get_endpoint_by_regs(of_node, 1, 0); /* port@1 */ 460 if (!endpoint) 461 return 0; 462 463 cnt = of_property_count_u64_elems(endpoint, "link-frequencies"); 464 465 if (cnt > 0) 466 of_property_read_u64_index(endpoint, "link-frequencies", 467 cnt - 1, &frequency); 468 of_node_put(endpoint); 469 470 do_div(frequency, 471 10 * /* from symbol rate to link rate */ 472 1000); /* kbytes */ 473 474 return frequency; 475 } 476 477 static int dp_panel_parse_dt(struct dp_panel *dp_panel) 478 { 479 struct dp_panel_private *panel; 480 struct device_node *of_node; 481 int cnt; 482 483 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 484 of_node = panel->dev->of_node; 485 486 /* 487 * data-lanes is the property of dp_out endpoint 488 */ 489 cnt = drm_of_get_data_lanes_count_ep(of_node, 1, 0, 1, DP_MAX_NUM_DP_LANES); 490 if (cnt < 0) { 491 /* legacy code, data-lanes is the property of mdss_dp node */ 492 cnt = drm_of_get_data_lanes_count(of_node, 1, DP_MAX_NUM_DP_LANES); 493 } 494 495 if (cnt > 0) 496 dp_panel->max_dp_lanes = cnt; 497 else 498 dp_panel->max_dp_lanes = DP_MAX_NUM_DP_LANES; /* 4 lanes */ 499 500 dp_panel->max_dp_link_rate = dp_panel_link_frequencies(of_node); 501 if (!dp_panel->max_dp_link_rate) 502 dp_panel->max_dp_link_rate = DP_LINK_RATE_HBR2; 503 504 return 0; 505 } 506 507 struct dp_panel *dp_panel_get(struct dp_panel_in *in) 508 { 509 struct dp_panel_private *panel; 510 struct dp_panel *dp_panel; 511 int ret; 512 513 if (!in->dev || !in->catalog || !in->aux || !in->link) { 514 DRM_ERROR("invalid input\n"); 515 return ERR_PTR(-EINVAL); 516 } 517 518 panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL); 519 if (!panel) 520 return ERR_PTR(-ENOMEM); 521 522 panel->dev = in->dev; 523 panel->aux = in->aux; 524 panel->catalog = in->catalog; 525 panel->link = in->link; 526 527 dp_panel = &panel->dp_panel; 528 dp_panel->max_bw_code = DP_LINK_BW_8_1; 529 530 ret = dp_panel_parse_dt(dp_panel); 531 if (ret) 532 return ERR_PTR(ret); 533 534 return dp_panel; 535 } 536 537 void dp_panel_put(struct dp_panel *dp_panel) 538 { 539 if (!dp_panel) 540 return; 541 542 kfree(dp_panel->edid); 543 } 544