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 
dp_panel_read_dpcd(struct dp_panel * dp_panel)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 
dp_panel_get_supported_bpp(struct dp_panel * dp_panel,u32 mode_edid_bpp,u32 mode_pclk_khz)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 
dp_panel_update_modes(struct drm_connector * connector,struct edid * edid)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 		return rc;
145 	}
146 
147 	rc = drm_connector_update_edid_property(connector, NULL);
148 	if (rc)
149 		DRM_ERROR("failed to update edid property %d\n", rc);
150 
151 	return rc;
152 }
153 
dp_panel_read_sink_caps(struct dp_panel * dp_panel,struct drm_connector * connector)154 int dp_panel_read_sink_caps(struct dp_panel *dp_panel,
155 	struct drm_connector *connector)
156 {
157 	int rc = 0, bw_code;
158 	int rlen, count;
159 	struct dp_panel_private *panel;
160 
161 	if (!dp_panel || !connector) {
162 		DRM_ERROR("invalid input\n");
163 		return -EINVAL;
164 	}
165 
166 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
167 
168 	rc = dp_panel_read_dpcd(dp_panel);
169 	if (rc) {
170 		DRM_ERROR("read dpcd failed %d\n", rc);
171 		return rc;
172 	}
173 
174 	bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
175 	if (!is_link_rate_valid(bw_code) ||
176 			!is_lane_count_valid(dp_panel->link_info.num_lanes) ||
177 			(bw_code > dp_panel->max_bw_code)) {
178 		DRM_ERROR("Illegal link rate=%d lane=%d\n", dp_panel->link_info.rate,
179 				dp_panel->link_info.num_lanes);
180 		return -EINVAL;
181 	}
182 
183 	if (dp_panel->dfp_present) {
184 		rlen = drm_dp_dpcd_read(panel->aux, DP_SINK_COUNT,
185 				&count, 1);
186 		if (rlen == 1) {
187 			count = DP_GET_SINK_COUNT(count);
188 			if (!count) {
189 				DRM_ERROR("no downstream ports connected\n");
190 				panel->link->sink_count = 0;
191 				rc = -ENOTCONN;
192 				goto end;
193 			}
194 		}
195 	}
196 
197 	kfree(dp_panel->edid);
198 	dp_panel->edid = NULL;
199 
200 	dp_panel->edid = drm_get_edid(connector,
201 					      &panel->aux->ddc);
202 	if (!dp_panel->edid) {
203 		DRM_ERROR("panel edid read failed\n");
204 		/* check edid read fail is due to unplug */
205 		if (!dp_catalog_link_is_connected(panel->catalog)) {
206 			rc = -ETIMEDOUT;
207 			goto end;
208 		}
209 
210 		/* fail safe edid */
211 		mutex_lock(&connector->dev->mode_config.mutex);
212 		if (drm_add_modes_noedid(connector, 640, 480))
213 			drm_set_preferred_mode(connector, 640, 480);
214 		mutex_unlock(&connector->dev->mode_config.mutex);
215 	}
216 
217 	if (panel->aux_cfg_update_done) {
218 		DRM_DEBUG_DP("read DPCD with updated AUX config\n");
219 		rc = dp_panel_read_dpcd(dp_panel);
220 		bw_code = drm_dp_link_rate_to_bw_code(dp_panel->link_info.rate);
221 		if (rc || !is_link_rate_valid(bw_code) ||
222 			!is_lane_count_valid(dp_panel->link_info.num_lanes)
223 			|| (bw_code > dp_panel->max_bw_code)) {
224 			DRM_ERROR("read dpcd failed %d\n", rc);
225 			return rc;
226 		}
227 		panel->aux_cfg_update_done = false;
228 	}
229 end:
230 	return rc;
231 }
232 
dp_panel_get_mode_bpp(struct dp_panel * dp_panel,u32 mode_edid_bpp,u32 mode_pclk_khz)233 u32 dp_panel_get_mode_bpp(struct dp_panel *dp_panel,
234 		u32 mode_edid_bpp, u32 mode_pclk_khz)
235 {
236 	struct dp_panel_private *panel;
237 	u32 bpp;
238 
239 	if (!dp_panel || !mode_edid_bpp || !mode_pclk_khz) {
240 		DRM_ERROR("invalid input\n");
241 		return 0;
242 	}
243 
244 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
245 
246 	if (dp_panel->video_test)
247 		bpp = dp_link_bit_depth_to_bpp(
248 				panel->link->test_video.test_bit_depth);
249 	else
250 		bpp = dp_panel_get_supported_bpp(dp_panel, mode_edid_bpp,
251 				mode_pclk_khz);
252 
253 	return bpp;
254 }
255 
dp_panel_get_modes(struct dp_panel * dp_panel,struct drm_connector * connector,struct dp_display_mode * mode)256 int dp_panel_get_modes(struct dp_panel *dp_panel,
257 	struct drm_connector *connector, struct dp_display_mode *mode)
258 {
259 	if (!dp_panel) {
260 		DRM_ERROR("invalid input\n");
261 		return -EINVAL;
262 	}
263 
264 	if (dp_panel->edid)
265 		return dp_panel_update_modes(connector, dp_panel->edid);
266 
267 	return 0;
268 }
269 
dp_panel_get_edid_checksum(struct edid * edid)270 static u8 dp_panel_get_edid_checksum(struct edid *edid)
271 {
272 	struct edid *last_block;
273 	u8 *raw_edid;
274 	bool is_edid_corrupt = false;
275 
276 	if (!edid) {
277 		DRM_ERROR("invalid edid input\n");
278 		return 0;
279 	}
280 
281 	raw_edid = (u8 *)edid;
282 	raw_edid += (edid->extensions * EDID_LENGTH);
283 	last_block = (struct edid *)raw_edid;
284 
285 	/* block type extension */
286 	drm_edid_block_valid(raw_edid, 1, false, &is_edid_corrupt);
287 	if (!is_edid_corrupt)
288 		return last_block->checksum;
289 
290 	DRM_ERROR("Invalid block, no checksum\n");
291 	return 0;
292 }
293 
dp_panel_handle_sink_request(struct dp_panel * dp_panel)294 void dp_panel_handle_sink_request(struct dp_panel *dp_panel)
295 {
296 	struct dp_panel_private *panel;
297 
298 	if (!dp_panel) {
299 		DRM_ERROR("invalid input\n");
300 		return;
301 	}
302 
303 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
304 
305 	if (panel->link->sink_request & DP_TEST_LINK_EDID_READ) {
306 		u8 checksum;
307 
308 		if (dp_panel->edid)
309 			checksum = dp_panel_get_edid_checksum(dp_panel->edid);
310 		else
311 			checksum = dp_panel->connector->real_edid_checksum;
312 
313 		dp_link_send_edid_checksum(panel->link, checksum);
314 		dp_link_send_test_response(panel->link);
315 	}
316 }
317 
dp_panel_tpg_config(struct dp_panel * dp_panel,bool enable)318 void dp_panel_tpg_config(struct dp_panel *dp_panel, bool enable)
319 {
320 	struct dp_catalog *catalog;
321 	struct dp_panel_private *panel;
322 
323 	if (!dp_panel) {
324 		DRM_ERROR("invalid input\n");
325 		return;
326 	}
327 
328 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
329 	catalog = panel->catalog;
330 
331 	if (!panel->panel_on) {
332 		DRM_DEBUG_DP("DP panel not enabled, handle TPG on next on\n");
333 		return;
334 	}
335 
336 	if (!enable) {
337 		dp_catalog_panel_tpg_disable(catalog);
338 		return;
339 	}
340 
341 	DRM_DEBUG_DP("%s: calling catalog tpg_enable\n", __func__);
342 	dp_catalog_panel_tpg_enable(catalog, &panel->dp_panel.dp_mode.drm_mode);
343 }
344 
dp_panel_dump_regs(struct dp_panel * dp_panel)345 void dp_panel_dump_regs(struct dp_panel *dp_panel)
346 {
347 	struct dp_catalog *catalog;
348 	struct dp_panel_private *panel;
349 
350 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
351 	catalog = panel->catalog;
352 
353 	dp_catalog_dump_regs(catalog);
354 }
355 
dp_panel_timing_cfg(struct dp_panel * dp_panel)356 int dp_panel_timing_cfg(struct dp_panel *dp_panel)
357 {
358 	u32 data, total_ver, total_hor;
359 	struct dp_catalog *catalog;
360 	struct dp_panel_private *panel;
361 	struct drm_display_mode *drm_mode;
362 
363 	panel = container_of(dp_panel, struct dp_panel_private, dp_panel);
364 	catalog = panel->catalog;
365 	drm_mode = &panel->dp_panel.dp_mode.drm_mode;
366 
367 	DRM_DEBUG_DP("width=%d hporch= %d %d %d\n",
368 		drm_mode->hdisplay, drm_mode->htotal - drm_mode->hsync_end,
369 		drm_mode->hsync_start - drm_mode->hdisplay,
370 		drm_mode->hsync_end - drm_mode->hsync_start);
371 
372 	DRM_DEBUG_DP("height=%d vporch= %d %d %d\n",
373 		drm_mode->vdisplay, drm_mode->vtotal - drm_mode->vsync_end,
374 		drm_mode->vsync_start - drm_mode->vdisplay,
375 		drm_mode->vsync_end - drm_mode->vsync_start);
376 
377 	total_hor = drm_mode->htotal;
378 
379 	total_ver = drm_mode->vtotal;
380 
381 	data = total_ver;
382 	data <<= 16;
383 	data |= total_hor;
384 
385 	catalog->total = data;
386 
387 	data = (drm_mode->vtotal - drm_mode->vsync_start);
388 	data <<= 16;
389 	data |= (drm_mode->htotal - drm_mode->hsync_start);
390 
391 	catalog->sync_start = data;
392 
393 	data = drm_mode->vsync_end - drm_mode->vsync_start;
394 	data <<= 16;
395 	data |= (panel->dp_panel.dp_mode.v_active_low << 31);
396 	data |= drm_mode->hsync_end - drm_mode->hsync_start;
397 	data |= (panel->dp_panel.dp_mode.h_active_low << 15);
398 
399 	catalog->width_blanking = data;
400 
401 	data = drm_mode->vdisplay;
402 	data <<= 16;
403 	data |= drm_mode->hdisplay;
404 
405 	catalog->dp_active = data;
406 
407 	dp_catalog_panel_timing_cfg(catalog);
408 	panel->panel_on = true;
409 
410 	return 0;
411 }
412 
dp_panel_init_panel_info(struct dp_panel * dp_panel)413 int dp_panel_init_panel_info(struct dp_panel *dp_panel)
414 {
415 	struct drm_display_mode *drm_mode;
416 
417 	drm_mode = &dp_panel->dp_mode.drm_mode;
418 
419 	/*
420 	 * print resolution info as this is a result
421 	 * of user initiated action of cable connection
422 	 */
423 	DRM_DEBUG_DP("SET NEW RESOLUTION:\n");
424 	DRM_DEBUG_DP("%dx%d@%dfps\n", drm_mode->hdisplay,
425 		drm_mode->vdisplay, drm_mode_vrefresh(drm_mode));
426 	DRM_DEBUG_DP("h_porches(back|front|width) = (%d|%d|%d)\n",
427 			drm_mode->htotal - drm_mode->hsync_end,
428 			drm_mode->hsync_start - drm_mode->hdisplay,
429 			drm_mode->hsync_end - drm_mode->hsync_start);
430 	DRM_DEBUG_DP("v_porches(back|front|width) = (%d|%d|%d)\n",
431 			drm_mode->vtotal - drm_mode->vsync_end,
432 			drm_mode->vsync_start - drm_mode->vdisplay,
433 			drm_mode->vsync_end - drm_mode->vsync_start);
434 	DRM_DEBUG_DP("pixel clock (KHz)=(%d)\n", drm_mode->clock);
435 	DRM_DEBUG_DP("bpp = %d\n", dp_panel->dp_mode.bpp);
436 
437 	dp_panel->dp_mode.bpp = max_t(u32, 18,
438 					min_t(u32, dp_panel->dp_mode.bpp, 30));
439 	DRM_DEBUG_DP("updated bpp = %d\n", dp_panel->dp_mode.bpp);
440 
441 	return 0;
442 }
443 
dp_panel_get(struct dp_panel_in * in)444 struct dp_panel *dp_panel_get(struct dp_panel_in *in)
445 {
446 	struct dp_panel_private *panel;
447 	struct dp_panel *dp_panel;
448 
449 	if (!in->dev || !in->catalog || !in->aux || !in->link) {
450 		DRM_ERROR("invalid input\n");
451 		return ERR_PTR(-EINVAL);
452 	}
453 
454 	panel = devm_kzalloc(in->dev, sizeof(*panel), GFP_KERNEL);
455 	if (!panel)
456 		return ERR_PTR(-ENOMEM);
457 
458 	panel->dev = in->dev;
459 	panel->aux = in->aux;
460 	panel->catalog = in->catalog;
461 	panel->link = in->link;
462 
463 	dp_panel = &panel->dp_panel;
464 	dp_panel->max_bw_code = DP_LINK_BW_8_1;
465 	panel->aux_cfg_update_done = false;
466 
467 	return dp_panel;
468 }
469 
dp_panel_put(struct dp_panel * dp_panel)470 void dp_panel_put(struct dp_panel *dp_panel)
471 {
472 	if (!dp_panel)
473 		return;
474 
475 	kfree(dp_panel->edid);
476 }
477