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 dp_panel dp_panel;
15 struct drm_dp_aux *aux;
17 struct dp_catalog *catalog;
19 bool aux_cfg_update_done;
22 static int dp_panel_read_dpcd(struct dp_panel *dp_panel)
27 struct dp_panel_private *panel;
28 struct dp_link_info *link_info;
29 u8 *dpcd, major = 0, minor = 0, temp;
30 u32 offset = DP_DPCD_REV;
32 dpcd = dp_panel->dpcd;
34 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
35 link_info = &dp_panel->link_info;
37 rlen = drm_dp_dpcd_read(panel->aux, offset,
38 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
39 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
40 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
41 if (rlen == -ETIMEDOUT)
49 temp = dpcd[DP_TRAINING_AUX_RD_INTERVAL];
51 /* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
53 DRM_DEBUG_DP("using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
54 offset = DPRX_EXTENDED_DPCD_FIELD;
57 rlen = drm_dp_dpcd_read(panel->aux, offset,
58 dpcd, (DP_RECEIVER_CAP_SIZE + 1));
59 if (rlen < (DP_RECEIVER_CAP_SIZE + 1)) {
60 DRM_ERROR("dpcd read failed, rlen=%zd\n", rlen);
61 if (rlen == -ETIMEDOUT)
69 link_info->revision = dpcd[DP_DPCD_REV];
70 major = (link_info->revision >> 4) & 0x0f;
71 minor = link_info->revision & 0x0f;
73 link_info->rate = drm_dp_bw_code_to_link_rate(dpcd[DP_MAX_LINK_RATE]);
74 link_info->num_lanes = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
76 if (link_info->num_lanes > dp_panel->max_dp_lanes)
77 link_info->num_lanes = dp_panel->max_dp_lanes;
79 /* Limit support upto HBR2 until HBR3 support is added */
80 if (link_info->rate >= (drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4)))
81 link_info->rate = drm_dp_bw_code_to_link_rate(DP_LINK_BW_5_4);
83 DRM_DEBUG_DP("version: %d.%d\n", major, minor);
84 DRM_DEBUG_DP("link_rate=%d\n", link_info->rate);
85 DRM_DEBUG_DP("lane_count=%d\n", link_info->num_lanes);
87 if (drm_dp_enhanced_frame_cap(dpcd))
88 link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
90 dp_panel->dfp_present = dpcd[DP_DOWNSTREAMPORT_PRESENT];
91 dp_panel->dfp_present &= DP_DWN_STRM_PORT_PRESENT;
93 if (dp_panel->dfp_present && (dpcd[DP_DPCD_REV] > 0x10)) {
94 dp_panel->ds_port_cnt = dpcd[DP_DOWN_STREAM_PORT_COUNT];
95 dp_panel->ds_port_cnt &= DP_PORT_COUNT_MASK;
96 len = DP_DOWNSTREAM_PORTS * DP_DOWNSTREAM_CAP_SIZE;
98 rlen = drm_dp_dpcd_read(panel->aux,
99 DP_DOWNSTREAM_PORT_0, dp_panel->ds_cap_info, len);
101 DRM_ERROR("ds port status failed, rlen=%zd\n", rlen);
111 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
112 u32 mode_edid_bpp, u32 mode_pclk_khz)
114 struct dp_link_info *link_info;
115 const u32 max_supported_bpp = 30, min_supported_bpp = 18;
116 u32 bpp = 0, data_rate_khz = 0;
118 bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
120 link_info = &dp_panel->link_info;
121 data_rate_khz = link_info->num_lanes * link_info->rate * 8;
123 while (bpp > min_supported_bpp) {
124 if (mode_pclk_khz * bpp <= data_rate_khz)
132 static int dp_panel_update_modes(struct drm_connector *connector,
138 rc = drm_connector_update_edid_property(connector, edid);
140 DRM_ERROR("failed to update edid property %d\n", rc);
143 rc = drm_add_edid_modes(connector, edid);
144 DRM_DEBUG_DP("%s -", __func__);
148 rc = drm_connector_update_edid_property(connector, NULL);
150 DRM_ERROR("failed to update edid property %d\n", rc);
155 int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
156 struct drm_connector *connector)
160 struct dp_panel_private *panel;
162 if (!dp_panel || !connector) {
163 DRM_ERROR("invalid input\n");
167 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
169 rc = dp_panel_read_dpcd(dp_panel);
170 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
171 if (rc || !is_link_rate_valid(bw_code) ||
172 !is_lane_count_valid(dp_panel->link_info.num_lanes) ||
173 (bw_code > dp_panel->max_bw_code)) {
174 DRM_ERROR("read dpcd failed %d\n", rc);
178 if (dp_panel->dfp_present) {
179 rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
182 count = DP_GET_SINK_COUNT(count);
184 DRM_ERROR("no downstream ports connected\n");
185 panel->link->sink_count = 0;
192 kfree(dp_panel->edid);
193 dp_panel->edid = NULL;
195 dp_panel->edid = drm_get_edid(connector,
197 if (!dp_panel->edid) {
198 DRM_ERROR("panel edid read failed\n");
199 /* check edid read fail is due to unplug */
200 if (!dp_catalog_link_is_connected(panel->catalog)) {
206 mutex_lock(&connector->dev->mode_config.mutex);
207 if (drm_add_modes_noedid(connector, 640, 480))
208 drm_set_preferred_mode(connector, 640, 480);
209 mutex_unlock(&connector->dev->mode_config.mutex);
212 if (panel->aux_cfg_update_done) {
213 DRM_DEBUG_DP("read DPCD with updated AUX config\n");
214 rc = dp_panel_read_dpcd(dp_panel);
215 bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
216 if (rc || !is_link_rate_valid(bw_code) ||
217 !is_lane_count_valid(dp_panel->link_info.num_lanes)
218 || (bw_code > dp_panel->max_bw_code)) {
219 DRM_ERROR("read dpcd failed %d\n", rc);
222 panel->aux_cfg_update_done = false;
228 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
229 u32 mode_edid_bpp, u32 mode_pclk_khz)
231 struct dp_panel_private *panel;
232 u32 bpp = mode_edid_bpp;
234 if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
235 DRM_ERROR("invalid input\n");
239 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
241 if (dp_panel->video_test)
242 bpp = dp_link_bit_depth_to_bpp(
243 panel->link->test_video.test_bit_depth);
245 bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
251 int dp_panel_get_modes(struct dp_panel *dp_panel,
252 struct drm_connector *connector, struct dp_display_mode *mode)
255 DRM_ERROR("invalid input\n");
260 return dp_panel_update_modes(connector, dp_panel->edid);
265 static u8 dp_panel_get_edid_checksum(struct edid *edid)
267 struct edid *last_block;
269 bool is_edid_corrupt;
272 DRM_ERROR("invalid edid input\n");
276 raw_edid = (u8 *)edid;
277 raw_edid += (edid->extensions * EDID_LENGTH);
278 last_block = (struct edid *)raw_edid;
280 /* block type extension */
281 drm_edid_block_valid(raw_edid, 1, false, &is_edid_corrupt);
282 if (!is_edid_corrupt)
283 return last_block->checksum;
285 DRM_ERROR("Invalid block, no checksum\n");
289 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
291 struct dp_panel_private *panel;
294 DRM_ERROR("invalid input\n");
298 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
300 if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
301 u8 checksum = dp_panel_get_edid_checksum(dp_panel->edid);
303 dp_link_send_edid_checksum(panel->link, checksum);
304 dp_link_send_test_response(panel->link);
308 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
310 struct dp_catalog *catalog;
311 struct dp_panel_private *panel;
314 DRM_ERROR("invalid input\n");
318 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
319 catalog = panel->catalog;
321 if (!panel->panel_on) {
322 DRM_DEBUG_DP("DP panel not enabled, handle TPG on next on\n");
327 dp_catalog_panel_tpg_disable(catalog);
331 DRM_DEBUG_DP("%s: calling catalog tpg_enable\n", __func__);
332 dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode);
335 void dp_panel_dump_regs(struct dp_panel *dp_panel)
337 struct dp_catalog *catalog;
338 struct dp_panel_private *panel;
340 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
341 catalog = panel->catalog;
343 dp_catalog_dump_regs(catalog);
346 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
349 u32 data, total_ver, total_hor;
350 struct dp_catalog *catalog;
351 struct dp_panel_private *panel;
352 struct drm_display_mode *drm_mode;
354 panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
355 catalog = panel->catalog;
356 drm_mode = &panel->dp_panel.dp_mode.drm_mode;
358 DRM_DEBUG_DP("width=%d hporch= %d %d %d\n",
359 drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end,
360 drm_mode->hsync_start - drm_mode->hdisplay,
361 drm_mode->hsync_end - drm_mode->hsync_start);
363 DRM_DEBUG_DP("height=%d vporch= %d %d %d\n",
364 drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end,
365 drm_mode->vsync_start - drm_mode->vdisplay,
366 drm_mode->vsync_end - drm_mode->vsync_start);
368 total_hor = drm_mode->htotal;
370 total_ver = drm_mode->vtotal;
376 catalog->total = data;
378 data = (drm_mode->vtotal - drm_mode->vsync_start);
380 data |= (drm_mode->htotal - drm_mode->hsync_start);
382 catalog->sync_start = data;
384 data = drm_mode->vsync_end - drm_mode->vsync_start;
386 data |= (panel->dp_panel.dp_mode.v_active_low << 31);
387 data |= drm_mode->hsync_end - drm_mode->hsync_start;
388 data |= (panel->dp_panel.dp_mode.h_active_low << 15);
390 catalog->width_blanking = data;
392 data = drm_mode->vdisplay;
394 data |= drm_mode->hdisplay;
396 catalog->dp_active = data;
398 dp_catalog_panel_timing_cfg(catalog);
399 panel->panel_on = true;
404 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
407 struct drm_display_mode *drm_mode;
409 drm_mode = &dp_panel->dp_mode.drm_mode;
412 * print resolution info as this is a result
413 * of user initiated action of cable connection
415 DRM_DEBUG_DP("SET NEW RESOLUTION:\n");
416 DRM_DEBUG_DP("%dx%d@%dfps\n", drm_mode->hdisplay,
417 drm_mode->vdisplay, drm_mode_vrefresh(drm_mode));
418 DRM_DEBUG_DP("h_porches(back|front|width) = (%d|%d|%d)\n",
419 drm_mode->htotal - drm_mode->hsync_end,
420 drm_mode->hsync_start - drm_mode->hdisplay,
421 drm_mode->hsync_end - drm_mode->hsync_start);
422 DRM_DEBUG_DP("v_porches(back|front|width) = (%d|%d|%d)\n",
423 drm_mode->vtotal - drm_mode->vsync_end,
424 drm_mode->vsync_start - drm_mode->vdisplay,
425 drm_mode->vsync_end - drm_mode->vsync_start);
426 DRM_DEBUG_DP("pixel clock (KHz)=(%d)\n", drm_mode->clock);
427 DRM_DEBUG_DP("bpp = %d\n", dp_panel->dp_mode.bpp);
429 dp_panel->dp_mode.bpp = max_t(u32, 18,
430 min_t(u32, dp_panel->dp_mode.bpp, 30));
431 DRM_DEBUG_DP("updated bpp = %d\n", dp_panel->dp_mode.bpp);
436 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
438 struct dp_panel_private *panel;
439 struct dp_panel *dp_panel;
441 if (!in->dev || !in->catalog || !in->aux || !in->link) {
442 DRM_ERROR("invalid input\n");
443 return ERR_PTR(-EINVAL);
446 panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
448 return ERR_PTR(-ENOMEM);
450 panel->dev = in->dev;
451 panel->aux = in->aux;
452 panel->catalog = in->catalog;
453 panel->link = in->link;
455 dp_panel = &panel->dp_panel;
456 dp_panel->max_bw_code = DP_LINK_BW_8_1;
457 panel->aux_cfg_update_done = false;
462 void dp_panel_put(struct dp_panel *dp_panel)
467 kfree(dp_panel->edid);