xref: /openbmc/linux/drivers/gpu/drm/msm/dp/dp_panel.c (revision 79f382b9)
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 dp_panel dp_panel;
15 	struct drm_dp_aux *aux;
16 	struct dp_link *link;
17 	struct dp_catalog *catalog;
18 	bool panel_on;
19 	bool aux_cfg_update_done;
20 };
21 
22 static int dp_panel_read_dpcd(struct dp_panel *dp_panel)
23 {
24 	int rc = 0;
25 	size_t len;
26 	ssize_t rlen;
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;
31 
32 	dpcd = dp_panel->dpcd;
33 
34 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
35 	link_info = &dp_panel->link_info;
36 
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)
42 			rc = rlen;
43 		else
44 			rc = -EINVAL;
45 
46 		goto end;
47 	}
48 
49 	temp = dpcd[DP_TRAINING_AUX_RD_INTERVAL];
50 
51 	/* check for EXTENDED_RECEIVER_CAPABILITY_FIELD_PRESENT */
52 	if (temp & BIT(7)) {
53 		DRM_DEBUG_DP("using EXTENDED_RECEIVER_CAPABILITY_FIELD\n");
54 		offset = DPRX_EXTENDED_DPCD_FIELD;
55 	}
56 
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)
62 			rc = rlen;
63 		else
64 			rc = -EINVAL;
65 
66 		goto end;
67 	}
68 
69 	link_info->revision = dpcd[DP_DPCD_REV];
70 	major = (link_info->revision >> 4) & 0x0f;
71 	minor = link_info->revision & 0x0f;
72 
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;
75 
76 	if (link_info->num_lanes > dp_panel->max_dp_lanes)
77 		link_info->num_lanes = dp_panel->max_dp_lanes;
78 
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);
82 
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);
86 
87 	if (drm_dp_enhanced_frame_cap(dpcd))
88 		link_info->capabilities |= DP_LINK_CAP_ENHANCED_FRAMING;
89 
90 	dp_panel->dfp_present = dpcd[DP_DOWNSTREAMPORT_PRESENT];
91 	dp_panel->dfp_present &= DP_DWN_STRM_PORT_PRESENT;
92 
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;
97 
98 		rlen = drm_dp_dpcd_read(panel->aux,
99 			DP_DOWNSTREAM_PORT_0, dp_panel->ds_cap_info, len);
100 		if (rlen < len) {
101 			DRM_ERROR("ds port status failed, rlen=%zd\n", rlen);
102 			rc = -EINVAL;
103 			goto end;
104 		}
105 	}
106 
107 end:
108 	return rc;
109 }
110 
111 static u32 dp_panel_get_supported_bpp(struct dp_panel *dp_panel,
112 		u32 mode_edid_bpp, u32 mode_pclk_khz)
113 {
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;
117 
118 	bpp = min_t(u32, mode_edid_bpp, max_supported_bpp);
119 
120 	link_info = &dp_panel->link_info;
121 	data_rate_khz = link_info->num_lanes * link_info->rate * 8;
122 
123 	while (bpp > min_supported_bpp) {
124 		if (mode_pclk_khz * bpp <= data_rate_khz)
125 			break;
126 		bpp -= 6;
127 	}
128 
129 	return bpp;
130 }
131 
132 static int dp_panel_update_modes(struct drm_connector *connector,
133 	struct edid *edid)
134 {
135 	int rc = 0;
136 
137 	if (edid) {
138 		rc = drm_connector_update_edid_property(connector, edid);
139 		if (rc) {
140 			DRM_ERROR("failed to update edid property %d\n", rc);
141 			return rc;
142 		}
143 		rc = drm_add_edid_modes(connector, edid);
144 		DRM_DEBUG_DP("%s -", __func__);
145 		return rc;
146 	}
147 
148 	rc = drm_connector_update_edid_property(connector, NULL);
149 	if (rc)
150 		DRM_ERROR("failed to update edid property %d\n", rc);
151 
152 	return rc;
153 }
154 
155 int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
156 	struct drm_connector *connector)
157 {
158 	int rc = 0, bw_code;
159 	int rlen, count;
160 	struct dp_panel_private *panel;
161 
162 	if (!dp_panel || !connector) {
163 		DRM_ERROR("invalid input\n");
164 		return -EINVAL;
165 	}
166 
167 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
168 
169 	rc = dp_panel_read_dpcd(dp_panel);
170 	if (rc) {
171 		DRM_ERROR("read dpcd failed %d\n", rc);
172 		return rc;
173 	}
174 
175 	bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
176 	if (!is_link_rate_valid(bw_code) ||
177 			!is_lane_count_valid(dp_panel->link_info.num_lanes) ||
178 			(bw_code > dp_panel->max_bw_code)) {
179 		DRM_ERROR("Illegal link rate=%d lane=%d\n", dp_panel->link_info.rate,
180 				dp_panel->link_info.num_lanes);
181 		return -EINVAL;
182 	}
183 
184 	if (dp_panel->dfp_present) {
185 		rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
186 				&count, 1);
187 		if (rlen == 1) {
188 			count = DP_GET_SINK_COUNT(count);
189 			if (!count) {
190 				DRM_ERROR("no downstream ports connected\n");
191 				panel->link->sink_count = 0;
192 				rc = -ENOTCONN;
193 				goto end;
194 			}
195 		}
196 	}
197 
198 	kfree(dp_panel->edid);
199 	dp_panel->edid = NULL;
200 
201 	dp_panel->edid = drm_get_edid(connector,
202 					      &panel->aux->ddc);
203 	if (!dp_panel->edid) {
204 		DRM_ERROR("panel edid read failed\n");
205 		/* check edid read fail is due to unplug */
206 		if (!dp_catalog_link_is_connected(panel->catalog)) {
207 			rc = -ETIMEDOUT;
208 			goto end;
209 		}
210 
211 		/* fail safe edid */
212 		mutex_lock(&connector->dev->mode_config.mutex);
213 		if (drm_add_modes_noedid(connector, 640, 480))
214 			drm_set_preferred_mode(connector, 640, 480);
215 		mutex_unlock(&connector->dev->mode_config.mutex);
216 	}
217 
218 	if (panel->aux_cfg_update_done) {
219 		DRM_DEBUG_DP("read DPCD with updated AUX config\n");
220 		rc = dp_panel_read_dpcd(dp_panel);
221 		bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
222 		if (rc || !is_link_rate_valid(bw_code) ||
223 			!is_lane_count_valid(dp_panel->link_info.num_lanes)
224 			|| (bw_code > dp_panel->max_bw_code)) {
225 			DRM_ERROR("read dpcd failed %d\n", rc);
226 			return rc;
227 		}
228 		panel->aux_cfg_update_done = false;
229 	}
230 end:
231 	return rc;
232 }
233 
234 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
235 		u32 mode_edid_bpp, u32 mode_pclk_khz)
236 {
237 	struct dp_panel_private *panel;
238 	u32 bpp = mode_edid_bpp;
239 
240 	if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
241 		DRM_ERROR("invalid input\n");
242 		return 0;
243 	}
244 
245 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
246 
247 	if (dp_panel->video_test)
248 		bpp = dp_link_bit_depth_to_bpp(
249 				panel->link->test_video.test_bit_depth);
250 	else
251 		bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
252 				mode_pclk_khz);
253 
254 	return bpp;
255 }
256 
257 int dp_panel_get_modes(struct dp_panel *dp_panel,
258 	struct drm_connector *connector, struct dp_display_mode *mode)
259 {
260 	if (!dp_panel) {
261 		DRM_ERROR("invalid input\n");
262 		return -EINVAL;
263 	}
264 
265 	if (dp_panel->edid)
266 		return dp_panel_update_modes(connector, dp_panel->edid);
267 
268 	return 0;
269 }
270 
271 static u8 dp_panel_get_edid_checksum(struct edid *edid)
272 {
273 	struct edid *last_block;
274 	u8 *raw_edid;
275 	bool is_edid_corrupt;
276 
277 	if (!edid) {
278 		DRM_ERROR("invalid edid input\n");
279 		return 0;
280 	}
281 
282 	raw_edid = (u8 *)edid;
283 	raw_edid += (edid->extensions * EDID_LENGTH);
284 	last_block = (struct edid *)raw_edid;
285 
286 	/* block type extension */
287 	drm_edid_block_valid(raw_edid, 1, false, &is_edid_corrupt);
288 	if (!is_edid_corrupt)
289 		return last_block->checksum;
290 
291 	DRM_ERROR("Invalid block, no checksum\n");
292 	return 0;
293 }
294 
295 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
296 {
297 	struct dp_panel_private *panel;
298 
299 	if (!dp_panel) {
300 		DRM_ERROR("invalid input\n");
301 		return;
302 	}
303 
304 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
305 
306 	if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
307 		u8 checksum = dp_panel_get_edid_checksum(dp_panel->edid);
308 
309 		dp_link_send_edid_checksum(panel->link, checksum);
310 		dp_link_send_test_response(panel->link);
311 	}
312 }
313 
314 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
315 {
316 	struct dp_catalog *catalog;
317 	struct dp_panel_private *panel;
318 
319 	if (!dp_panel) {
320 		DRM_ERROR("invalid input\n");
321 		return;
322 	}
323 
324 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
325 	catalog = panel->catalog;
326 
327 	if (!panel->panel_on) {
328 		DRM_DEBUG_DP("DP panel not enabled, handle TPG on next on\n");
329 		return;
330 	}
331 
332 	if (!enable) {
333 		dp_catalog_panel_tpg_disable(catalog);
334 		return;
335 	}
336 
337 	DRM_DEBUG_DP("%s: calling catalog tpg_enable\n", __func__);
338 	dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode);
339 }
340 
341 void dp_panel_dump_regs(struct dp_panel *dp_panel)
342 {
343 	struct dp_catalog *catalog;
344 	struct dp_panel_private *panel;
345 
346 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
347 	catalog = panel->catalog;
348 
349 	dp_catalog_dump_regs(catalog);
350 }
351 
352 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
353 {
354 	int rc = 0;
355 	u32 data, total_ver, total_hor;
356 	struct dp_catalog *catalog;
357 	struct dp_panel_private *panel;
358 	struct drm_display_mode *drm_mode;
359 
360 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
361 	catalog = panel->catalog;
362 	drm_mode = &panel->dp_panel.dp_mode.drm_mode;
363 
364 	DRM_DEBUG_DP("width=%d hporch= %d %d %d\n",
365 		drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end,
366 		drm_mode->hsync_start - drm_mode->hdisplay,
367 		drm_mode->hsync_end - drm_mode->hsync_start);
368 
369 	DRM_DEBUG_DP("height=%d vporch= %d %d %d\n",
370 		drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end,
371 		drm_mode->vsync_start - drm_mode->vdisplay,
372 		drm_mode->vsync_end - drm_mode->vsync_start);
373 
374 	total_hor = drm_mode->htotal;
375 
376 	total_ver = drm_mode->vtotal;
377 
378 	data = total_ver;
379 	data <<= 16;
380 	data |= total_hor;
381 
382 	catalog->total = data;
383 
384 	data = (drm_mode->vtotal - drm_mode->vsync_start);
385 	data <<= 16;
386 	data |= (drm_mode->htotal - drm_mode->hsync_start);
387 
388 	catalog->sync_start = data;
389 
390 	data = drm_mode->vsync_end - drm_mode->vsync_start;
391 	data <<= 16;
392 	data |= (panel->dp_panel.dp_mode.v_active_low << 31);
393 	data |= drm_mode->hsync_end - drm_mode->hsync_start;
394 	data |= (panel->dp_panel.dp_mode.h_active_low << 15);
395 
396 	catalog->width_blanking = data;
397 
398 	data = drm_mode->vdisplay;
399 	data <<= 16;
400 	data |= drm_mode->hdisplay;
401 
402 	catalog->dp_active = data;
403 
404 	dp_catalog_panel_timing_cfg(catalog);
405 	panel->panel_on = true;
406 
407 	return rc;
408 }
409 
410 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
411 {
412 	struct drm_display_mode *drm_mode;
413 
414 	drm_mode = &dp_panel->dp_mode.drm_mode;
415 
416 	/*
417 	 * print resolution info as this is a result
418 	 * of user initiated action of cable connection
419 	 */
420 	DRM_DEBUG_DP("SET NEW RESOLUTION:\n");
421 	DRM_DEBUG_DP("%dx%d@%dfps\n", drm_mode->hdisplay,
422 		drm_mode->vdisplay, drm_mode_vrefresh(drm_mode));
423 	DRM_DEBUG_DP("h_porches(back|front|width) = (%d|%d|%d)\n",
424 			drm_mode->htotal - drm_mode->hsync_end,
425 			drm_mode->hsync_start - drm_mode->hdisplay,
426 			drm_mode->hsync_end - drm_mode->hsync_start);
427 	DRM_DEBUG_DP("v_porches(back|front|width) = (%d|%d|%d)\n",
428 			drm_mode->vtotal - drm_mode->vsync_end,
429 			drm_mode->vsync_start - drm_mode->vdisplay,
430 			drm_mode->vsync_end - drm_mode->vsync_start);
431 	DRM_DEBUG_DP("pixel clock (KHz)=(%d)\n", drm_mode->clock);
432 	DRM_DEBUG_DP("bpp = %d\n", dp_panel->dp_mode.bpp);
433 
434 	dp_panel->dp_mode.bpp = max_t(u32, 18,
435 					min_t(u32, dp_panel->dp_mode.bpp, 30));
436 	DRM_DEBUG_DP("updated bpp = %d\n", dp_panel->dp_mode.bpp);
437 
438 	return 0;
439 }
440 
441 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
442 {
443 	struct dp_panel_private *panel;
444 	struct dp_panel *dp_panel;
445 
446 	if (!in->dev || !in->catalog || !in->aux || !in->link) {
447 		DRM_ERROR("invalid input\n");
448 		return ERR_PTR(-EINVAL);
449 	}
450 
451 	panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
452 	if (!panel)
453 		return ERR_PTR(-ENOMEM);
454 
455 	panel->dev = in->dev;
456 	panel->aux = in->aux;
457 	panel->catalog = in->catalog;
458 	panel->link = in->link;
459 
460 	dp_panel = &panel->dp_panel;
461 	dp_panel->max_bw_code = DP_LINK_BW_8_1;
462 	panel->aux_cfg_update_done = false;
463 
464 	return dp_panel;
465 }
466 
467 void dp_panel_put(struct dp_panel *dp_panel)
468 {
469 	if (!dp_panel)
470 		return;
471 
472 	kfree(dp_panel->edid);
473 }
474