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