xref: /linux/drivers/gpu/drm/ast/ast_dp.c (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2021, ASPEED Technology Inc.
3 // Authors: KuoHsiang Chou <kuohsiang_chou@aspeedtech.com>
4 
5 #include <linux/firmware.h>
6 #include <linux/delay.h>
7 
8 #include <drm/drm_atomic.h>
9 #include <drm/drm_atomic_state_helper.h>
10 #include <drm/drm_edid.h>
11 #include <drm/drm_modeset_helper_vtables.h>
12 #include <drm/drm_print.h>
13 #include <drm/drm_probe_helper.h>
14 
15 #include "ast_drv.h"
16 #include "ast_vbios.h"
17 
18 struct ast_astdp_mode_index_table_entry {
19 	unsigned int hdisplay;
20 	unsigned int vdisplay;
21 	unsigned int mode_index;
22 };
23 
24 /* FIXME: Do refresh rate and flags actually matter? */
25 static const struct ast_astdp_mode_index_table_entry ast_astdp_mode_index_table[] = {
26 	{  320,  240, ASTDP_320x240_60 },
27 	{  400,  300, ASTDP_400x300_60 },
28 	{  512,  384, ASTDP_512x384_60 },
29 	{  640,  480, ASTDP_640x480_60 },
30 	{  800,  600, ASTDP_800x600_56 },
31 	{ 1024,  768, ASTDP_1024x768_60 },
32 	{ 1152,  864, ASTDP_1152x864_75 },
33 	{ 1280,  800, ASTDP_1280x800_60_RB },
34 	{ 1280, 1024, ASTDP_1280x1024_60 },
35 	{ 1360,  768, ASTDP_1366x768_60 }, // same as 1366x786
36 	{ 1366,  768, ASTDP_1366x768_60 },
37 	{ 1440,  900, ASTDP_1440x900_60_RB },
38 	{ 1600,  900, ASTDP_1600x900_60_RB },
39 	{ 1600, 1200, ASTDP_1600x1200_60 },
40 	{ 1680, 1050, ASTDP_1680x1050_60_RB },
41 	{ 1920, 1080, ASTDP_1920x1080_60 },
42 	{ 1920, 1200, ASTDP_1920x1200_60 },
43 	{ 0 }
44 };
45 
46 struct ast_astdp_connector_state {
47 	struct drm_connector_state base;
48 
49 	int mode_index;
50 };
51 
52 static struct ast_astdp_connector_state *
53 to_ast_astdp_connector_state(const struct drm_connector_state *state)
54 {
55 	return container_of(state, struct ast_astdp_connector_state, base);
56 }
57 
58 static int ast_astdp_get_mode_index(unsigned int hdisplay, unsigned int vdisplay)
59 {
60 	const struct ast_astdp_mode_index_table_entry *entry = ast_astdp_mode_index_table;
61 
62 	while (entry->hdisplay && entry->vdisplay) {
63 		if (entry->hdisplay == hdisplay && entry->vdisplay == vdisplay)
64 			return entry->mode_index;
65 		++entry;
66 	}
67 
68 	return -EINVAL;
69 }
70 
71 static bool ast_astdp_is_connected(struct ast_device *ast)
72 {
73 	if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDF, AST_IO_VGACRDF_HPD))
74 		return false;
75 	/*
76 	 * HPD might be set even if no monitor is connected, so also check that
77 	 * the link training was successful.
78 	 */
79 	if (!ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xDC, AST_IO_VGACRDC_LINK_SUCCESS))
80 		return false;
81 	return true;
82 }
83 
84 static int ast_astdp_read_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
85 {
86 	struct ast_device *ast = data;
87 	size_t rdlen = round_up(len, 4);
88 	int ret = 0;
89 	unsigned int i;
90 
91 	if (block > 1) {
92 		/* ASPEED DP's EDID buffer holds blocks 0 and 1 only (256
93 		 * bytes). Report anything beyond that as an all-zero block
94 		 * rather than a read failure.
95 		 */
96 		memset(buf, 0, len);
97 		return 0;
98 	}
99 
100 	/*
101 	 * Protect access to I/O registers from concurrent modesetting
102 	 * by acquiring the I/O-register lock.
103 	 */
104 	mutex_lock(&ast->modeset_lock);
105 
106 	/* Start reading EDID data */
107 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE, 0x00);
108 
109 	for (i = 0; i < rdlen; i += 4) {
110 		unsigned int offset;
111 		unsigned int j;
112 		u8 ediddata[4];
113 		u8 vgacre4;
114 
115 		offset = (i + block * EDID_LENGTH) / 4;
116 		if (offset >= 64) {
117 			ret = -EIO;
118 			goto out;
119 		}
120 		vgacre4 = offset;
121 
122 		/*
123 		 * CRE4[7:0]: Read-Pointer for EDID (Unit: 4bytes); valid range: 0~64
124 		 */
125 		ast_set_index_reg(ast, AST_IO_VGACRI, 0xe4, vgacre4);
126 
127 		/*
128 		 * CRD7[b0]: valid flag for EDID
129 		 * CRD6[b0]: mirror read pointer for EDID
130 		 */
131 		for (j = 0; j < 200; ++j) {
132 			u8 vgacrd7, vgacrd6;
133 
134 			/*
135 			 * Delay are getting longer with each retry.
136 			 *
137 			 * 1. No delay on first try
138 			 * 2. The Delays are often 2 loops when users request "Display Settings"
139 			 *	  of right-click of mouse.
140 			 * 3. The Delays are often longer a lot when system resume from S3/S4.
141 			 */
142 			if (j)
143 				msleep(j + 1);
144 
145 			/* Wait for EDID offset to show up in mirror register */
146 			vgacrd7 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd7);
147 			if (vgacrd7 & AST_IO_VGACRD7_EDID_VALID_FLAG) {
148 				vgacrd6 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd6);
149 				if (vgacrd6 == offset)
150 					break;
151 			}
152 		}
153 		if (j == 200) {
154 			ret = -EBUSY;
155 			goto out;
156 		}
157 
158 		ediddata[0] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd8);
159 		ediddata[1] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd9);
160 		ediddata[2] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xda);
161 		ediddata[3] = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdb);
162 
163 		memcpy(buf, ediddata, min((len - i), 4));
164 		buf += 4;
165 	}
166 
167 out:
168 	/* Signal end of reading */
169 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5, (u8)~AST_IO_VGACRE5_EDID_READ_DONE,
170 			       AST_IO_VGACRE5_EDID_READ_DONE);
171 
172 	mutex_unlock(&ast->modeset_lock);
173 
174 	return ret;
175 }
176 
177 /*
178  * Launch Aspeed DP
179  */
180 int ast_dp_launch(struct ast_device *ast)
181 {
182 	struct drm_device *dev = &ast->base;
183 	unsigned int i = 10;
184 
185 	while (i) {
186 		u8 vgacrd1 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xd1);
187 
188 		if (vgacrd1 & AST_IO_VGACRD1_MCU_FW_EXECUTING)
189 			break;
190 		--i;
191 		msleep(100);
192 	}
193 	if (!i) {
194 		drm_err(dev, "Wait DPMCU executing timeout\n");
195 		return -ENODEV;
196 	}
197 
198 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe5,
199 			       (u8) ~AST_IO_VGACRE5_EDID_READ_DONE,
200 			       AST_IO_VGACRE5_EDID_READ_DONE);
201 
202 	return 0;
203 }
204 
205 static bool ast_dp_get_phy_sleep(struct ast_device *ast)
206 {
207 	u8 vgacre3 = ast_get_index_reg(ast, AST_IO_VGACRI, 0xe3);
208 
209 	return (vgacre3 & AST_IO_VGACRE3_DP_PHY_SLEEP);
210 }
211 
212 static void ast_dp_set_phy_sleep(struct ast_device *ast, bool sleep)
213 {
214 	u8 vgacre3 = 0x00;
215 
216 	if (sleep)
217 		vgacre3 |= AST_IO_VGACRE3_DP_PHY_SLEEP;
218 
219 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe3, (u8)~AST_IO_VGACRE3_DP_PHY_SLEEP,
220 			       vgacre3);
221 	msleep(50);
222 }
223 
224 static void ast_dp_link_training(struct ast_device *ast)
225 {
226 	struct drm_device *dev = &ast->base;
227 	int i;
228 
229 	for (i = 0; i < 10; i++) {
230 		u8 vgacrdc;
231 
232 		if (i)
233 			msleep(100);
234 
235 		vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
236 		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
237 			return;
238 	}
239 	drm_err(dev, "Link training failed\n");
240 }
241 
242 static bool __ast_dp_wait_enable(struct ast_device *ast, bool enabled)
243 {
244 	u8 vgacrdf_test = 0x00;
245 	u8 vgacrdf;
246 	unsigned int i;
247 
248 	if (enabled)
249 		vgacrdf_test |= AST_IO_VGACRDF_DP_VIDEO_ENABLE;
250 
251 	for (i = 0; i < 1000; ++i) {
252 		if (i)
253 			mdelay(1);
254 		vgacrdf = ast_get_index_reg_mask(ast, AST_IO_VGACRI, 0xdf,
255 						 AST_IO_VGACRDF_DP_VIDEO_ENABLE);
256 		if (vgacrdf == vgacrdf_test)
257 			return true;
258 	}
259 
260 	return false;
261 }
262 
263 static void ast_dp_set_enable(struct ast_device *ast, bool enabled)
264 {
265 	struct drm_device *dev = &ast->base;
266 	u8 vgacre3 = 0x00;
267 
268 	if (enabled)
269 		vgacre3 |= AST_IO_VGACRE3_DP_VIDEO_ENABLE;
270 
271 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xe3, (u8)~AST_IO_VGACRE3_DP_VIDEO_ENABLE,
272 			       vgacre3);
273 
274 	drm_WARN_ON(dev, !__ast_dp_wait_enable(ast, enabled));
275 }
276 
277 static void ast_wait_for_vretrace(struct ast_device *ast)
278 {
279 	unsigned long timeout = jiffies + HZ;
280 	u8 vgair1;
281 
282 	do {
283 		vgair1 = ast_io_read8(ast, AST_IO_VGAIR1_R);
284 	} while (!(vgair1 & AST_IO_VGAIR1_VREFRESH) && time_before(jiffies, timeout));
285 }
286 
287 /*
288  * Encoder
289  */
290 
291 static const struct drm_encoder_funcs ast_astdp_encoder_funcs = {
292 	.destroy = drm_encoder_cleanup,
293 };
294 
295 static enum drm_mode_status
296 ast_astdp_encoder_helper_mode_valid(struct drm_encoder *encoder,
297 				    const struct drm_display_mode *mode)
298 {
299 	int res;
300 
301 	res = ast_astdp_get_mode_index(mode->hdisplay, mode->vdisplay);
302 	if (res < 0)
303 		return MODE_NOMODE;
304 
305 	return MODE_OK;
306 }
307 
308 static void ast_astdp_encoder_helper_atomic_mode_set(struct drm_encoder *encoder,
309 						     struct drm_crtc_state *crtc_state,
310 						     struct drm_connector_state *conn_state)
311 {
312 	struct drm_device *dev = encoder->dev;
313 	struct ast_device *ast = to_ast_device(dev);
314 	struct ast_crtc_state *ast_crtc_state = to_ast_crtc_state(crtc_state);
315 	const struct ast_vbios_enhtable *vmode = ast_crtc_state->vmode;
316 	struct ast_astdp_connector_state *astdp_conn_state =
317 		to_ast_astdp_connector_state(conn_state);
318 	int mode_index = astdp_conn_state->mode_index;
319 	u8 refresh_rate_index;
320 	u8 vgacre0, vgacre1, vgacre2;
321 
322 	if (drm_WARN_ON(dev, vmode->refresh_rate_index < 1 || vmode->refresh_rate_index > 255))
323 		return;
324 	refresh_rate_index = vmode->refresh_rate_index - 1;
325 
326 	/* FIXME: Why are we doing this? */
327 	switch (mode_index) {
328 	case ASTDP_1280x800_60_RB:
329 	case ASTDP_1440x900_60_RB:
330 	case ASTDP_1600x900_60_RB:
331 	case ASTDP_1680x1050_60_RB:
332 		mode_index = (u8)(mode_index - (u8)refresh_rate_index);
333 		break;
334 	default:
335 		mode_index = (u8)(mode_index + (u8)refresh_rate_index);
336 		break;
337 	}
338 
339 	/*
340 	 * CRE0[7:0]: MISC0 ((0x00: 18-bpp) or (0x20: 24-bpp)
341 	 * CRE1[7:0]: MISC1 (default: 0x00)
342 	 * CRE2[7:0]: video format index (0x00 ~ 0x20 or 0x40 ~ 0x50)
343 	 */
344 	vgacre0 = AST_IO_VGACRE0_24BPP;
345 	vgacre1 = 0x00;
346 	vgacre2 = mode_index & 0xff;
347 
348 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xe0, vgacre0);
349 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xe1, vgacre1);
350 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xe2, vgacre2);
351 }
352 
353 static void ast_astdp_encoder_helper_atomic_enable(struct drm_encoder *encoder,
354 						   struct drm_atomic_commit *state)
355 {
356 	struct ast_device *ast = to_ast_device(encoder->dev);
357 	struct ast_connector *ast_connector = &ast->output.astdp.connector;
358 
359 	if (ast_connector->physical_status == connector_status_connected) {
360 		ast_dp_set_phy_sleep(ast, false);
361 		ast_dp_link_training(ast);
362 
363 		ast_wait_for_vretrace(ast);
364 		ast_dp_set_enable(ast, true);
365 	}
366 }
367 
368 static void ast_astdp_encoder_helper_atomic_disable(struct drm_encoder *encoder,
369 						    struct drm_atomic_commit *state)
370 {
371 	struct ast_device *ast = to_ast_device(encoder->dev);
372 
373 	ast_dp_set_enable(ast, false);
374 	ast_dp_set_phy_sleep(ast, true);
375 }
376 
377 static int ast_astdp_encoder_helper_atomic_check(struct drm_encoder *encoder,
378 						 struct drm_crtc_state *crtc_state,
379 						 struct drm_connector_state *conn_state)
380 {
381 	const struct drm_display_mode *mode = &crtc_state->mode;
382 	struct ast_astdp_connector_state *astdp_conn_state =
383 		to_ast_astdp_connector_state(conn_state);
384 	int res;
385 
386 	if (drm_atomic_crtc_needs_modeset(crtc_state)) {
387 		res = ast_astdp_get_mode_index(mode->hdisplay, mode->vdisplay);
388 		if (res < 0)
389 			return res;
390 		astdp_conn_state->mode_index = res;
391 	}
392 
393 	return 0;
394 }
395 
396 static const struct drm_encoder_helper_funcs ast_astdp_encoder_helper_funcs = {
397 	.mode_valid = ast_astdp_encoder_helper_mode_valid,
398 	.atomic_mode_set = ast_astdp_encoder_helper_atomic_mode_set,
399 	.atomic_enable = ast_astdp_encoder_helper_atomic_enable,
400 	.atomic_disable = ast_astdp_encoder_helper_atomic_disable,
401 	.atomic_check = ast_astdp_encoder_helper_atomic_check,
402 };
403 
404 /*
405  * Connector
406  */
407 
408 static int ast_astdp_connector_helper_get_modes(struct drm_connector *connector)
409 {
410 	struct ast_connector *ast_connector = to_ast_connector(connector);
411 	int count;
412 
413 	if (ast_connector->physical_status == connector_status_connected) {
414 		struct ast_device *ast = to_ast_device(connector->dev);
415 		const struct drm_edid *drm_edid;
416 
417 		drm_edid = drm_edid_read_custom(connector, ast_astdp_read_edid_block, ast);
418 		drm_edid_connector_update(connector, drm_edid);
419 		count = drm_edid_connector_add_modes(connector);
420 		drm_edid_free(drm_edid);
421 	} else {
422 		drm_edid_connector_update(connector, NULL);
423 
424 		/*
425 		 * There's no EDID data without a connected monitor. Set BMC-
426 		 * compatible modes in this case. The XGA default resolution
427 		 * should work well for all BMCs.
428 		 */
429 		count = drm_add_modes_noedid(connector, 4096, 4096);
430 		if (count)
431 			drm_set_preferred_mode(connector, 1024, 768);
432 	}
433 
434 	return count;
435 }
436 
437 static int ast_astdp_connector_helper_detect_ctx(struct drm_connector *connector,
438 						 struct drm_modeset_acquire_ctx *ctx,
439 						 bool force)
440 {
441 	struct ast_connector *ast_connector = to_ast_connector(connector);
442 	struct ast_device *ast = to_ast_device(connector->dev);
443 	enum drm_connector_status status = connector_status_disconnected;
444 	bool phy_sleep;
445 
446 	mutex_lock(&ast->modeset_lock);
447 
448 	phy_sleep = ast_dp_get_phy_sleep(ast);
449 	if (phy_sleep)
450 		ast_dp_set_phy_sleep(ast, false);
451 
452 	if (ast_astdp_is_connected(ast))
453 		status = connector_status_connected;
454 
455 	if (phy_sleep && status == connector_status_disconnected)
456 		ast_dp_set_phy_sleep(ast, true);
457 
458 	mutex_unlock(&ast->modeset_lock);
459 
460 	if (status != ast_connector->physical_status)
461 		++connector->epoch_counter;
462 	ast_connector->physical_status = status;
463 
464 	return connector_status_connected;
465 }
466 
467 static const struct drm_connector_helper_funcs ast_astdp_connector_helper_funcs = {
468 	.get_modes = ast_astdp_connector_helper_get_modes,
469 	.detect_ctx = ast_astdp_connector_helper_detect_ctx,
470 };
471 
472 static void ast_astdp_connector_reset(struct drm_connector *connector)
473 {
474 	struct ast_astdp_connector_state *astdp_state = kzalloc_obj(*astdp_state);
475 
476 	if (connector->state)
477 		connector->funcs->atomic_destroy_state(connector, connector->state);
478 
479 	if (astdp_state)
480 		__drm_atomic_helper_connector_reset(connector, &astdp_state->base);
481 	else
482 		__drm_atomic_helper_connector_reset(connector, NULL);
483 }
484 
485 static struct drm_connector_state *
486 ast_astdp_connector_atomic_duplicate_state(struct drm_connector *connector)
487 {
488 	struct ast_astdp_connector_state *new_astdp_state, *astdp_state;
489 	struct drm_device *dev = connector->dev;
490 
491 	if (drm_WARN_ON(dev, !connector->state))
492 		return NULL;
493 
494 	new_astdp_state = kmalloc_obj(*new_astdp_state);
495 	if (!new_astdp_state)
496 		return NULL;
497 	__drm_atomic_helper_connector_duplicate_state(connector, &new_astdp_state->base);
498 
499 	astdp_state = to_ast_astdp_connector_state(connector->state);
500 
501 	new_astdp_state->mode_index = astdp_state->mode_index;
502 
503 	return &new_astdp_state->base;
504 }
505 
506 static void ast_astdp_connector_atomic_destroy_state(struct drm_connector *connector,
507 						     struct drm_connector_state *state)
508 {
509 	struct ast_astdp_connector_state *astdp_state = to_ast_astdp_connector_state(state);
510 
511 	__drm_atomic_helper_connector_destroy_state(&astdp_state->base);
512 	kfree(astdp_state);
513 }
514 
515 static const struct drm_connector_funcs ast_astdp_connector_funcs = {
516 	.reset = ast_astdp_connector_reset,
517 	.fill_modes = drm_helper_probe_single_connector_modes,
518 	.destroy = drm_connector_cleanup,
519 	.atomic_duplicate_state = ast_astdp_connector_atomic_duplicate_state,
520 	.atomic_destroy_state = ast_astdp_connector_atomic_destroy_state,
521 };
522 
523 /*
524  * Output
525  */
526 
527 int ast_astdp_output_init(struct ast_device *ast)
528 {
529 	struct drm_device *dev = &ast->base;
530 	struct drm_crtc *crtc = &ast->crtc;
531 	struct drm_encoder *encoder;
532 	struct ast_connector *ast_connector;
533 	struct drm_connector *connector;
534 	int ret;
535 
536 	/* encoder */
537 
538 	encoder = &ast->output.astdp.encoder;
539 	ret = drm_encoder_init(dev, encoder, &ast_astdp_encoder_funcs,
540 			       DRM_MODE_ENCODER_TMDS, NULL);
541 	if (ret)
542 		return ret;
543 	drm_encoder_helper_add(encoder, &ast_astdp_encoder_helper_funcs);
544 
545 	encoder->possible_crtcs = drm_crtc_mask(crtc);
546 
547 	/* connector */
548 
549 	ast_connector = &ast->output.astdp.connector;
550 	connector = &ast_connector->base;
551 	ret = drm_connector_init(dev, connector, &ast_astdp_connector_funcs,
552 				 DRM_MODE_CONNECTOR_DisplayPort);
553 	if (ret)
554 		return ret;
555 	drm_connector_helper_add(connector, &ast_astdp_connector_helper_funcs);
556 
557 	connector->interlace_allowed = 0;
558 	connector->doublescan_allowed = 0;
559 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
560 
561 	ast_connector->physical_status = connector->status;
562 
563 	ret = drm_connector_attach_encoder(connector, encoder);
564 	if (ret)
565 		return ret;
566 
567 	return 0;
568 }
569