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 8 #include <drm/drm_connector.h> 9 #include <drm/drm_edid.h> 10 #include <drm/drm_print.h> 11 12 struct dp_panel_private { 13 struct device *dev; 14 struct drm_device *drm_dev; 15 struct dp_panel dp_panel; 16 struct drm_dp_aux *aux; 17 struct dp_link *link; 18 struct dp_catalog *catalog; 19 bool panel_on; 20 }; 21 22 static void dp_panel_read_psr_cap(struct dp_panel_private *panel) 23 { 24 ssize_t rlen; 25 struct dp_panel *dp_panel; 26 27 dp_panel = &panel->dp_panel; 28 29 /* edp sink */ 30 if (dp_panel->dpcd[DP_EDP_CONFIGURATION_CAP]) { 31 rlen = drm_dp_dpcd_read(panel->aux, DP_PSR_SUPPORT, 32 &dp_panel->psr_cap, sizeof(dp_panel->psr_cap)); 33 if (rlen == sizeof(dp_panel->psr_cap)) { 34 drm_dbg_dp(panel->drm_dev, 35 "psr version: 0x%x, psr_cap: 0x%x\n", 36 dp_panel->psr_cap.version, 37 dp_panel->psr_cap.capabilities); 38 } else 39 DRM_ERROR("failed to read psr info, rlen=%zd\n", rlen); 40 } 41 } 42 43 static int dp_panel_read_dpcd(struct dp_panel *dp_panel) 44 { 45 int rc; 46 struct dp_panel_private *panel; 47 struct dp_link_info *link_info; 48 u8 *dpcd, major, minor; 49 50 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 51 dpcd = dp_panel->dpcd; 52 rc = drm_dp_read_dpcd_caps(panel->aux, dpcd); 53 if (rc) 54 return rc; 55 56 link_info = &dp_panel->link_info; 57 link_info->revision = dpcd[DP_DPCD_REV]; 58 major = (link_info->revision >> 4) & 0x0f; 59 minor = link_info->revision & 0x0f; 60 61 link_info->rate = drm_dp_max_link_rate(dpcd); 62 link_info->num_lanes = drm_dp_max_lane_count(dpcd); 63 64 /* Limit data lanes from data-lanes of endpoint property of dtsi */ 65 if (link_info->num_lanes > dp_panel->max_dp_lanes) 66 link_info->num_lanes = dp_panel->max_dp_lanes; 67 68 /* Limit link rate from link-frequencies of endpoint property of dtsi */ 69 if (link_info->rate > dp_panel->max_dp_link_rate) 70 link_info->rate = dp_panel->max_dp_link_rate; 71 72 drm_dbg_dp(panel->drm_dev, "version: %d.%d\n", major, minor); 73 drm_dbg_dp(panel->drm_dev, "link_rate=%d\n", link_info->rate); 74 drm_dbg_dp(panel->drm_dev, "lane_count=%d\n", link_info->num_lanes); 75 76 if (drm_dp_enhanced_frame_cap(dpcd)) 77 link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING; 78 79 dp_panel_read_psr_cap(panel); 80 81 return rc; 82 } 83 84 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel, 85 u32 mode_edid_bpp, u32 mode_pclk_khz) 86 { 87 struct dp_link_info *link_info; 88 const u32 max_supported_bpp = 30, min_supported_bpp = 18; 89 u32 bpp = 0, data_rate_khz = 0; 90 91 bpp = min_t(u32, mode_edid_bpp, max_supported_bpp); 92 93 link_info = &dp_panel->link_info; 94 data_rate_khz = link_info->num_lanes * link_info->rate * 8; 95 96 while (bpp > min_supported_bpp) { 97 if (mode_pclk_khz * bpp <= data_rate_khz) 98 break; 99 bpp -= 6; 100 } 101 102 return bpp; 103 } 104 105 static int dp_panel_update_modes(struct drm_connector *connector, 106 struct edid *edid) 107 { 108 int rc = 0; 109 110 if (edid) { 111 rc = drm_connector_update_edid_property(connector, edid); 112 if (rc) { 113 DRM_ERROR("failed to update edid property %d\n", rc); 114 return rc; 115 } 116 rc = drm_add_edid_modes(connector, edid); 117 return rc; 118 } 119 120 rc = drm_connector_update_edid_property(connector, NULL); 121 if (rc) 122 DRM_ERROR("failed to update edid property %d\n", rc); 123 124 return rc; 125 } 126 127 int dp_panel_read_sink_caps(struct dp_panel *dp_panel, 128 struct drm_connector *connector) 129 { 130 int rc, bw_code; 131 int count; 132 struct dp_panel_private *panel; 133 134 if (!dp_panel || !connector) { 135 DRM_ERROR("invalid input\n"); 136 return -EINVAL; 137 } 138 139 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 140 141 rc = dp_panel_read_dpcd(dp_panel); 142 if (rc) { 143 DRM_ERROR("read dpcd failed %d\n", rc); 144 return rc; 145 } 146 147 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate); 148 if (!is_link_rate_valid(bw_code) || 149 !is_lane_count_valid(dp_panel->link_info.num_lanes) || 150 (bw_code > dp_panel->max_bw_code)) { 151 DRM_ERROR("Illegal link rate=%d lane=%d\n", dp_panel->link_info.rate, 152 dp_panel->link_info.num_lanes); 153 return -EINVAL; 154 } 155 156 if (drm_dp_is_branch(dp_panel->dpcd)) { 157 count = drm_dp_read_sink_count(panel->aux); 158 if (!count) { 159 panel->link->sink_count = 0; 160 return -ENOTCONN; 161 } 162 } 163 164 rc = drm_dp_read_downstream_info(panel->aux, dp_panel->dpcd, 165 dp_panel->downstream_ports); 166 if (rc) 167 return rc; 168 169 kfree(dp_panel->edid); 170 dp_panel->edid = NULL; 171 172 dp_panel->edid = drm_get_edid(connector, 173 &panel->aux->ddc); 174 if (!dp_panel->edid) { 175 DRM_ERROR("panel edid read failed\n"); 176 /* check edid read fail is due to unplug */ 177 if (!dp_catalog_link_is_connected(panel->catalog)) { 178 rc = -ETIMEDOUT; 179 goto end; 180 } 181 } 182 183 end: 184 return rc; 185 } 186 187 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel, 188 u32 mode_edid_bpp, u32 mode_pclk_khz) 189 { 190 struct dp_panel_private *panel; 191 u32 bpp; 192 193 if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) { 194 DRM_ERROR("invalid input\n"); 195 return 0; 196 } 197 198 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 199 200 if (dp_panel->video_test) 201 bpp = dp_link_bit_depth_to_bpp( 202 panel->link->test_video.test_bit_depth); 203 else 204 bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp, 205 mode_pclk_khz); 206 207 return bpp; 208 } 209 210 int dp_panel_get_modes(struct dp_panel *dp_panel, 211 struct drm_connector *connector) 212 { 213 if (!dp_panel) { 214 DRM_ERROR("invalid input\n"); 215 return -EINVAL; 216 } 217 218 if (dp_panel->edid) 219 return dp_panel_update_modes(connector, dp_panel->edid); 220 221 return 0; 222 } 223 224 static u8 dp_panel_get_edid_checksum(struct edid *edid) 225 { 226 edid += edid->extensions; 227 228 return edid->checksum; 229 } 230 231 void dp_panel_handle_sink_request(struct dp_panel *dp_panel) 232 { 233 struct dp_panel_private *panel; 234 235 if (!dp_panel) { 236 DRM_ERROR("invalid input\n"); 237 return; 238 } 239 240 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 241 242 if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) { 243 u8 checksum; 244 245 if (dp_panel->edid) 246 checksum = dp_panel_get_edid_checksum(dp_panel->edid); 247 else 248 checksum = dp_panel->connector->real_edid_checksum; 249 250 dp_link_send_edid_checksum(panel->link, checksum); 251 dp_link_send_test_response(panel->link); 252 } 253 } 254 255 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable) 256 { 257 struct dp_catalog *catalog; 258 struct dp_panel_private *panel; 259 260 if (!dp_panel) { 261 DRM_ERROR("invalid input\n"); 262 return; 263 } 264 265 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 266 catalog = panel->catalog; 267 268 if (!panel->panel_on) { 269 drm_dbg_dp(panel->drm_dev, 270 "DP panel not enabled, handle TPG on next on\n"); 271 return; 272 } 273 274 if (!enable) { 275 dp_catalog_panel_tpg_disable(catalog); 276 return; 277 } 278 279 drm_dbg_dp(panel->drm_dev, "calling catalog tpg_enable\n"); 280 dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode); 281 } 282 283 void dp_panel_dump_regs(struct dp_panel *dp_panel) 284 { 285 struct dp_catalog *catalog; 286 struct dp_panel_private *panel; 287 288 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 289 catalog = panel->catalog; 290 291 dp_catalog_dump_regs(catalog); 292 } 293 294 int dp_panel_timing_cfg(struct dp_panel *dp_panel) 295 { 296 u32 data, total_ver, total_hor; 297 struct dp_catalog *catalog; 298 struct dp_panel_private *panel; 299 struct drm_display_mode *drm_mode; 300 301 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 302 catalog = panel->catalog; 303 drm_mode = &panel->dp_panel.dp_mode.drm_mode; 304 305 drm_dbg_dp(panel->drm_dev, "width=%d hporch= %d %d %d\n", 306 drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end, 307 drm_mode->hsync_start - drm_mode->hdisplay, 308 drm_mode->hsync_end - drm_mode->hsync_start); 309 310 drm_dbg_dp(panel->drm_dev, "height=%d vporch= %d %d %d\n", 311 drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end, 312 drm_mode->vsync_start - drm_mode->vdisplay, 313 drm_mode->vsync_end - drm_mode->vsync_start); 314 315 total_hor = drm_mode->htotal; 316 317 total_ver = drm_mode->vtotal; 318 319 data = total_ver; 320 data <<= 16; 321 data |= total_hor; 322 323 catalog->total = data; 324 325 data = (drm_mode->vtotal - drm_mode->vsync_start); 326 data <<= 16; 327 data |= (drm_mode->htotal - drm_mode->hsync_start); 328 329 catalog->sync_start = data; 330 331 data = drm_mode->vsync_end - drm_mode->vsync_start; 332 data <<= 16; 333 data |= (panel->dp_panel.dp_mode.v_active_low << 31); 334 data |= drm_mode->hsync_end - drm_mode->hsync_start; 335 data |= (panel->dp_panel.dp_mode.h_active_low << 15); 336 337 catalog->width_blanking = data; 338 339 data = drm_mode->vdisplay; 340 data <<= 16; 341 data |= drm_mode->hdisplay; 342 343 catalog->dp_active = data; 344 345 dp_catalog_panel_timing_cfg(catalog); 346 panel->panel_on = true; 347 348 return 0; 349 } 350 351 int dp_panel_init_panel_info(struct dp_panel *dp_panel) 352 { 353 struct drm_display_mode *drm_mode; 354 struct dp_panel_private *panel; 355 356 drm_mode = &dp_panel->dp_mode.drm_mode; 357 358 panel = container_of(dp_panel, struct dp_panel_private, dp_panel); 359 360 /* 361 * print resolution info as this is a result 362 * of user initiated action of cable connection 363 */ 364 drm_dbg_dp(panel->drm_dev, "SET NEW RESOLUTION:\n"); 365 drm_dbg_dp(panel->drm_dev, "%dx%d@%dfps\n", 366 drm_mode->hdisplay, drm_mode->vdisplay, drm_mode_vrefresh(drm_mode)); 367 drm_dbg_dp(panel->drm_dev, 368 "h_porches(back|front|width) = (%d|%d|%d)\n", 369 drm_mode->htotal - drm_mode->hsync_end, 370 drm_mode->hsync_start - drm_mode->hdisplay, 371 drm_mode->hsync_end - drm_mode->hsync_start); 372 drm_dbg_dp(panel->drm_dev, 373 "v_porches(back|front|width) = (%d|%d|%d)\n", 374 drm_mode->vtotal - drm_mode->vsync_end, 375 drm_mode->vsync_start - drm_mode->vdisplay, 376 drm_mode->vsync_end - drm_mode->vsync_start); 377 drm_dbg_dp(panel->drm_dev, "pixel clock (KHz)=(%d)\n", 378 drm_mode->clock); 379 drm_dbg_dp(panel->drm_dev, "bpp = %d\n", dp_panel->dp_mode.bpp); 380 381 dp_panel->dp_mode.bpp = max_t(u32, 18, 382 min_t(u32, dp_panel->dp_mode.bpp, 30)); 383 drm_dbg_dp(panel->drm_dev, "updated bpp = %d\n", 384 dp_panel->dp_mode.bpp); 385 386 return 0; 387 } 388 389 struct dp_panel *dp_panel_get(struct dp_panel_in *in) 390 { 391 struct dp_panel_private *panel; 392 struct dp_panel *dp_panel; 393 394 if (!in->dev || !in->catalog || !in->aux || !in->link) { 395 DRM_ERROR("invalid input\n"); 396 return ERR_PTR(-EINVAL); 397 } 398 399 panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL); 400 if (!panel) 401 return ERR_PTR(-ENOMEM); 402 403 panel->dev = in->dev; 404 panel->aux = in->aux; 405 panel->catalog = in->catalog; 406 panel->link = in->link; 407 408 dp_panel = &panel->dp_panel; 409 dp_panel->max_bw_code = DP_LINK_BW_8_1; 410 411 return dp_panel; 412 } 413 414 void dp_panel_put(struct dp_panel *dp_panel) 415 { 416 if (!dp_panel) 417 return; 418 419 kfree(dp_panel->edid); 420 } 421