xref: /openbmc/linux/drivers/gpu/drm/msm/dp/dp_panel.c (revision bef7a78d)
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 	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);
175 		return rc;
176 	}
177 
178 	if (dp_panel->dfp_present) {
179 		rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
180 				&count, 1);
181 		if (rlen == 1) {
182 			count = DP_GET_SINK_COUNT(count);
183 			if (!count) {
184 				DRM_ERROR("no downstream ports connected\n");
185 				panel->link->sink_count = 0;
186 				rc = -ENOTCONN;
187 				goto end;
188 			}
189 		}
190 	}
191 
192 	kfree(dp_panel->edid);
193 	dp_panel->edid = NULL;
194 
195 	dp_panel->edid = drm_get_edid(connector,
196 					      &panel->aux->ddc);
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)) {
201 			rc = -ETIMEDOUT;
202 			goto end;
203 		}
204 
205 		/* fail safe edid */
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);
210 	}
211 
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);
220 			return rc;
221 		}
222 		panel->aux_cfg_update_done = false;
223 	}
224 end:
225 	return rc;
226 }
227 
228 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
229 		u32 mode_edid_bpp, u32 mode_pclk_khz)
230 {
231 	struct dp_panel_private *panel;
232 	u32 bpp = mode_edid_bpp;
233 
234 	if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
235 		DRM_ERROR("invalid input\n");
236 		return 0;
237 	}
238 
239 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
240 
241 	if (dp_panel->video_test)
242 		bpp = dp_link_bit_depth_to_bpp(
243 				panel->link->test_video.test_bit_depth);
244 	else
245 		bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
246 				mode_pclk_khz);
247 
248 	return bpp;
249 }
250 
251 int dp_panel_get_modes(struct dp_panel *dp_panel,
252 	struct drm_connector *connector, struct dp_display_mode *mode)
253 {
254 	if (!dp_panel) {
255 		DRM_ERROR("invalid input\n");
256 		return -EINVAL;
257 	}
258 
259 	if (dp_panel->edid)
260 		return dp_panel_update_modes(connector, dp_panel->edid);
261 
262 	return 0;
263 }
264 
265 static u8 dp_panel_get_edid_checksum(struct edid *edid)
266 {
267 	struct edid *last_block;
268 	u8 *raw_edid;
269 	bool is_edid_corrupt;
270 
271 	if (!edid) {
272 		DRM_ERROR("invalid edid input\n");
273 		return 0;
274 	}
275 
276 	raw_edid = (u8 *)edid;
277 	raw_edid += (edid->extensions * EDID_LENGTH);
278 	last_block = (struct edid *)raw_edid;
279 
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;
284 
285 	DRM_ERROR("Invalid block, no checksum\n");
286 	return 0;
287 }
288 
289 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
290 {
291 	struct dp_panel_private *panel;
292 
293 	if (!dp_panel) {
294 		DRM_ERROR("invalid input\n");
295 		return;
296 	}
297 
298 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
299 
300 	if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
301 		u8 checksum = dp_panel_get_edid_checksum(dp_panel->edid);
302 
303 		dp_link_send_edid_checksum(panel->link, checksum);
304 		dp_link_send_test_response(panel->link);
305 	}
306 }
307 
308 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
309 {
310 	struct dp_catalog *catalog;
311 	struct dp_panel_private *panel;
312 
313 	if (!dp_panel) {
314 		DRM_ERROR("invalid input\n");
315 		return;
316 	}
317 
318 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
319 	catalog = panel->catalog;
320 
321 	if (!panel->panel_on) {
322 		DRM_DEBUG_DP("DP panel not enabled, handle TPG on next on\n");
323 		return;
324 	}
325 
326 	if (!enable) {
327 		dp_catalog_panel_tpg_disable(catalog);
328 		return;
329 	}
330 
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);
333 }
334 
335 void dp_panel_dump_regs(struct dp_panel *dp_panel)
336 {
337 	struct dp_catalog *catalog;
338 	struct dp_panel_private *panel;
339 
340 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
341 	catalog = panel->catalog;
342 
343 	dp_catalog_dump_regs(catalog);
344 }
345 
346 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
347 {
348 	int rc = 0;
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;
353 
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;
357 
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);
362 
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);
367 
368 	total_hor = drm_mode->htotal;
369 
370 	total_ver = drm_mode->vtotal;
371 
372 	data = total_ver;
373 	data <<= 16;
374 	data |= total_hor;
375 
376 	catalog->total = data;
377 
378 	data = (drm_mode->vtotal - drm_mode->vsync_start);
379 	data <<= 16;
380 	data |= (drm_mode->htotal - drm_mode->hsync_start);
381 
382 	catalog->sync_start = data;
383 
384 	data = drm_mode->vsync_end - drm_mode->vsync_start;
385 	data <<= 16;
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);
389 
390 	catalog->width_blanking = data;
391 
392 	data = drm_mode->vdisplay;
393 	data <<= 16;
394 	data |= drm_mode->hdisplay;
395 
396 	catalog->dp_active = data;
397 
398 	dp_catalog_panel_timing_cfg(catalog);
399 	panel->panel_on = true;
400 
401 	return rc;
402 }
403 
404 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
405 {
406 	int rc = 0;
407 	struct drm_display_mode *drm_mode;
408 
409 	drm_mode = &dp_panel->dp_mode.drm_mode;
410 
411 	/*
412 	 * print resolution info as this is a result
413 	 * of user initiated action of cable connection
414 	 */
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);
428 
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);
432 
433 	return rc;
434 }
435 
436 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
437 {
438 	struct dp_panel_private *panel;
439 	struct dp_panel *dp_panel;
440 
441 	if (!in->dev || !in->catalog || !in->aux || !in->link) {
442 		DRM_ERROR("invalid input\n");
443 		return ERR_PTR(-EINVAL);
444 	}
445 
446 	panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
447 	if (!panel)
448 		return ERR_PTR(-ENOMEM);
449 
450 	panel->dev = in->dev;
451 	panel->aux = in->aux;
452 	panel->catalog = in->catalog;
453 	panel->link = in->link;
454 
455 	dp_panel = &panel->dp_panel;
456 	dp_panel->max_bw_code = DP_LINK_BW_8_1;
457 	panel->aux_cfg_update_done = false;
458 
459 	return dp_panel;
460 }
461 
462 void dp_panel_put(struct dp_panel *dp_panel)
463 {
464 	if (!dp_panel)
465 		return;
466 
467 	kfree(dp_panel->edid);
468 }
469