1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4  * Author: Chris Zhong <zyw@rock-chips.com>
5  */
6 
7 #include <linux/clk.h>
8 #include <linux/component.h>
9 #include <linux/extcon.h>
10 #include <linux/firmware.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/phy/phy.h>
13 #include <linux/regmap.h>
14 #include <linux/reset.h>
15 
16 #include <sound/hdmi-codec.h>
17 
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/dp/drm_dp_helper.h>
20 #include <drm/drm_edid.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_simple_kms_helper.h>
24 
25 #include "cdn-dp-core.h"
26 #include "cdn-dp-reg.h"
27 #include "rockchip_drm_vop.h"
28 
29 #define connector_to_dp(c) \
30 		container_of(c, struct cdn_dp_device, connector)
31 
32 #define encoder_to_dp(c) \
33 		container_of(c, struct cdn_dp_device, encoder)
34 
35 #define GRF_SOC_CON9		0x6224
36 #define DP_SEL_VOP_LIT		BIT(12)
37 #define GRF_SOC_CON26		0x6268
38 #define DPTX_HPD_SEL		(3 << 12)
39 #define DPTX_HPD_DEL		(2 << 12)
40 #define DPTX_HPD_SEL_MASK	(3 << 28)
41 
42 #define CDN_FW_TIMEOUT_MS	(64 * 1000)
43 #define CDN_DPCD_TIMEOUT_MS	5000
44 #define CDN_DP_FIRMWARE		"rockchip/dptx.bin"
45 MODULE_FIRMWARE(CDN_DP_FIRMWARE);
46 
47 struct cdn_dp_data {
48 	u8 max_phy;
49 };
50 
51 struct cdn_dp_data rk3399_cdn_dp = {
52 	.max_phy = 2,
53 };
54 
55 static const struct of_device_id cdn_dp_dt_ids[] = {
56 	{ .compatible = "rockchip,rk3399-cdn-dp",
57 		.data = (void *)&rk3399_cdn_dp },
58 	{}
59 };
60 
61 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
62 
63 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
64 			    unsigned int reg, unsigned int val)
65 {
66 	int ret;
67 
68 	ret = clk_prepare_enable(dp->grf_clk);
69 	if (ret) {
70 		DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
71 		return ret;
72 	}
73 
74 	ret = regmap_write(dp->grf, reg, val);
75 	if (ret) {
76 		DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
77 		clk_disable_unprepare(dp->grf_clk);
78 		return ret;
79 	}
80 
81 	clk_disable_unprepare(dp->grf_clk);
82 
83 	return 0;
84 }
85 
86 static int cdn_dp_clk_enable(struct cdn_dp_device *dp)
87 {
88 	int ret;
89 	unsigned long rate;
90 
91 	ret = clk_prepare_enable(dp->pclk);
92 	if (ret < 0) {
93 		DRM_DEV_ERROR(dp->dev, "cannot enable dp pclk %d\n", ret);
94 		goto err_pclk;
95 	}
96 
97 	ret = clk_prepare_enable(dp->core_clk);
98 	if (ret < 0) {
99 		DRM_DEV_ERROR(dp->dev, "cannot enable core_clk %d\n", ret);
100 		goto err_core_clk;
101 	}
102 
103 	ret = pm_runtime_get_sync(dp->dev);
104 	if (ret < 0) {
105 		DRM_DEV_ERROR(dp->dev, "cannot get pm runtime %d\n", ret);
106 		goto err_pm_runtime_get;
107 	}
108 
109 	reset_control_assert(dp->core_rst);
110 	reset_control_assert(dp->dptx_rst);
111 	reset_control_assert(dp->apb_rst);
112 	reset_control_deassert(dp->core_rst);
113 	reset_control_deassert(dp->dptx_rst);
114 	reset_control_deassert(dp->apb_rst);
115 
116 	rate = clk_get_rate(dp->core_clk);
117 	if (!rate) {
118 		DRM_DEV_ERROR(dp->dev, "get clk rate failed\n");
119 		ret = -EINVAL;
120 		goto err_set_rate;
121 	}
122 
123 	cdn_dp_set_fw_clk(dp, rate);
124 	cdn_dp_clock_reset(dp);
125 
126 	return 0;
127 
128 err_set_rate:
129 	pm_runtime_put(dp->dev);
130 err_pm_runtime_get:
131 	clk_disable_unprepare(dp->core_clk);
132 err_core_clk:
133 	clk_disable_unprepare(dp->pclk);
134 err_pclk:
135 	return ret;
136 }
137 
138 static void cdn_dp_clk_disable(struct cdn_dp_device *dp)
139 {
140 	pm_runtime_put_sync(dp->dev);
141 	clk_disable_unprepare(dp->pclk);
142 	clk_disable_unprepare(dp->core_clk);
143 }
144 
145 static int cdn_dp_get_port_lanes(struct cdn_dp_port *port)
146 {
147 	struct extcon_dev *edev = port->extcon;
148 	union extcon_property_value property;
149 	int dptx;
150 	u8 lanes;
151 
152 	dptx = extcon_get_state(edev, EXTCON_DISP_DP);
153 	if (dptx > 0) {
154 		extcon_get_property(edev, EXTCON_DISP_DP,
155 				    EXTCON_PROP_USB_SS, &property);
156 		if (property.intval)
157 			lanes = 2;
158 		else
159 			lanes = 4;
160 	} else {
161 		lanes = 0;
162 	}
163 
164 	return lanes;
165 }
166 
167 static int cdn_dp_get_sink_count(struct cdn_dp_device *dp, u8 *sink_count)
168 {
169 	int ret;
170 	u8 value;
171 
172 	*sink_count = 0;
173 	ret = cdn_dp_dpcd_read(dp, DP_SINK_COUNT, &value, 1);
174 	if (ret)
175 		return ret;
176 
177 	*sink_count = DP_GET_SINK_COUNT(value);
178 	return 0;
179 }
180 
181 static struct cdn_dp_port *cdn_dp_connected_port(struct cdn_dp_device *dp)
182 {
183 	struct cdn_dp_port *port;
184 	int i, lanes;
185 
186 	for (i = 0; i < dp->ports; i++) {
187 		port = dp->port[i];
188 		lanes = cdn_dp_get_port_lanes(port);
189 		if (lanes)
190 			return port;
191 	}
192 	return NULL;
193 }
194 
195 static bool cdn_dp_check_sink_connection(struct cdn_dp_device *dp)
196 {
197 	unsigned long timeout = jiffies + msecs_to_jiffies(CDN_DPCD_TIMEOUT_MS);
198 	struct cdn_dp_port *port;
199 	u8 sink_count = 0;
200 
201 	if (dp->active_port < 0 || dp->active_port >= dp->ports) {
202 		DRM_DEV_ERROR(dp->dev, "active_port is wrong!\n");
203 		return false;
204 	}
205 
206 	port = dp->port[dp->active_port];
207 
208 	/*
209 	 * Attempt to read sink count, retry in case the sink may not be ready.
210 	 *
211 	 * Sinks are *supposed* to come up within 1ms from an off state, but
212 	 * some docks need more time to power up.
213 	 */
214 	while (time_before(jiffies, timeout)) {
215 		if (!extcon_get_state(port->extcon, EXTCON_DISP_DP))
216 			return false;
217 
218 		if (!cdn_dp_get_sink_count(dp, &sink_count))
219 			return sink_count ? true : false;
220 
221 		usleep_range(5000, 10000);
222 	}
223 
224 	DRM_DEV_ERROR(dp->dev, "Get sink capability timed out\n");
225 	return false;
226 }
227 
228 static enum drm_connector_status
229 cdn_dp_connector_detect(struct drm_connector *connector, bool force)
230 {
231 	struct cdn_dp_device *dp = connector_to_dp(connector);
232 	enum drm_connector_status status = connector_status_disconnected;
233 
234 	mutex_lock(&dp->lock);
235 	if (dp->connected)
236 		status = connector_status_connected;
237 	mutex_unlock(&dp->lock);
238 
239 	return status;
240 }
241 
242 static void cdn_dp_connector_destroy(struct drm_connector *connector)
243 {
244 	drm_connector_unregister(connector);
245 	drm_connector_cleanup(connector);
246 }
247 
248 static const struct drm_connector_funcs cdn_dp_atomic_connector_funcs = {
249 	.detect = cdn_dp_connector_detect,
250 	.destroy = cdn_dp_connector_destroy,
251 	.fill_modes = drm_helper_probe_single_connector_modes,
252 	.reset = drm_atomic_helper_connector_reset,
253 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
254 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
255 };
256 
257 static int cdn_dp_connector_get_modes(struct drm_connector *connector)
258 {
259 	struct cdn_dp_device *dp = connector_to_dp(connector);
260 	struct edid *edid;
261 	int ret = 0;
262 
263 	mutex_lock(&dp->lock);
264 	edid = dp->edid;
265 	if (edid) {
266 		DRM_DEV_DEBUG_KMS(dp->dev, "got edid: width[%d] x height[%d]\n",
267 				  edid->width_cm, edid->height_cm);
268 
269 		dp->sink_has_audio = drm_detect_monitor_audio(edid);
270 		ret = drm_add_edid_modes(connector, edid);
271 		if (ret)
272 			drm_connector_update_edid_property(connector,
273 								edid);
274 	}
275 	mutex_unlock(&dp->lock);
276 
277 	return ret;
278 }
279 
280 static int cdn_dp_connector_mode_valid(struct drm_connector *connector,
281 				       struct drm_display_mode *mode)
282 {
283 	struct cdn_dp_device *dp = connector_to_dp(connector);
284 	struct drm_display_info *display_info = &dp->connector.display_info;
285 	u32 requested, actual, rate, sink_max, source_max = 0;
286 	u8 lanes, bpc;
287 
288 	/* If DP is disconnected, every mode is invalid */
289 	if (!dp->connected)
290 		return MODE_BAD;
291 
292 	switch (display_info->bpc) {
293 	case 10:
294 		bpc = 10;
295 		break;
296 	case 6:
297 		bpc = 6;
298 		break;
299 	default:
300 		bpc = 8;
301 		break;
302 	}
303 
304 	requested = mode->clock * bpc * 3 / 1000;
305 
306 	source_max = dp->lanes;
307 	sink_max = drm_dp_max_lane_count(dp->dpcd);
308 	lanes = min(source_max, sink_max);
309 
310 	source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
311 	sink_max = drm_dp_max_link_rate(dp->dpcd);
312 	rate = min(source_max, sink_max);
313 
314 	actual = rate * lanes / 100;
315 
316 	/* efficiency is about 0.8 */
317 	actual = actual * 8 / 10;
318 
319 	if (requested > actual) {
320 		DRM_DEV_DEBUG_KMS(dp->dev,
321 				  "requested=%d, actual=%d, clock=%d\n",
322 				  requested, actual, mode->clock);
323 		return MODE_CLOCK_HIGH;
324 	}
325 
326 	return MODE_OK;
327 }
328 
329 static struct drm_connector_helper_funcs cdn_dp_connector_helper_funcs = {
330 	.get_modes = cdn_dp_connector_get_modes,
331 	.mode_valid = cdn_dp_connector_mode_valid,
332 };
333 
334 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
335 {
336 	int ret;
337 	const u32 *iram_data, *dram_data;
338 	const struct firmware *fw = dp->fw;
339 	const struct cdn_firmware_header *hdr;
340 
341 	hdr = (struct cdn_firmware_header *)fw->data;
342 	if (fw->size != le32_to_cpu(hdr->size_bytes)) {
343 		DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
344 		return -EINVAL;
345 	}
346 
347 	iram_data = (const u32 *)(fw->data + hdr->header_size);
348 	dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
349 
350 	ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
351 				   dram_data, hdr->dram_size);
352 	if (ret)
353 		return ret;
354 
355 	ret = cdn_dp_set_firmware_active(dp, true);
356 	if (ret) {
357 		DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
358 		return ret;
359 	}
360 
361 	return cdn_dp_event_config(dp);
362 }
363 
364 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
365 {
366 	int ret;
367 
368 	if (!cdn_dp_check_sink_connection(dp))
369 		return -ENODEV;
370 
371 	ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
372 			       DP_RECEIVER_CAP_SIZE);
373 	if (ret) {
374 		DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
375 		return ret;
376 	}
377 
378 	kfree(dp->edid);
379 	dp->edid = drm_do_get_edid(&dp->connector,
380 				   cdn_dp_get_edid_block, dp);
381 	return 0;
382 }
383 
384 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
385 {
386 	union extcon_property_value property;
387 	int ret;
388 
389 	if (!port->phy_enabled) {
390 		ret = phy_power_on(port->phy);
391 		if (ret) {
392 			DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
393 				      ret);
394 			goto err_phy;
395 		}
396 		port->phy_enabled = true;
397 	}
398 
399 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
400 			       DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
401 	if (ret) {
402 		DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
403 		goto err_power_on;
404 	}
405 
406 	ret = cdn_dp_get_hpd_status(dp);
407 	if (ret <= 0) {
408 		if (!ret)
409 			DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
410 		goto err_power_on;
411 	}
412 
413 	ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
414 				  EXTCON_PROP_USB_TYPEC_POLARITY, &property);
415 	if (ret) {
416 		DRM_DEV_ERROR(dp->dev, "get property failed\n");
417 		goto err_power_on;
418 	}
419 
420 	port->lanes = cdn_dp_get_port_lanes(port);
421 	ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
422 	if (ret) {
423 		DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
424 			      ret);
425 		goto err_power_on;
426 	}
427 
428 	dp->active_port = port->id;
429 	return 0;
430 
431 err_power_on:
432 	if (phy_power_off(port->phy))
433 		DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
434 	else
435 		port->phy_enabled = false;
436 
437 err_phy:
438 	cdn_dp_grf_write(dp, GRF_SOC_CON26,
439 			 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
440 	return ret;
441 }
442 
443 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
444 			      struct cdn_dp_port *port)
445 {
446 	int ret;
447 
448 	if (port->phy_enabled) {
449 		ret = phy_power_off(port->phy);
450 		if (ret) {
451 			DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
452 			return ret;
453 		}
454 	}
455 
456 	port->phy_enabled = false;
457 	port->lanes = 0;
458 	dp->active_port = -1;
459 	return 0;
460 }
461 
462 static int cdn_dp_disable(struct cdn_dp_device *dp)
463 {
464 	int ret, i;
465 
466 	if (!dp->active)
467 		return 0;
468 
469 	for (i = 0; i < dp->ports; i++)
470 		cdn_dp_disable_phy(dp, dp->port[i]);
471 
472 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
473 			       DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
474 	if (ret) {
475 		DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
476 			      ret);
477 		return ret;
478 	}
479 
480 	cdn_dp_set_firmware_active(dp, false);
481 	cdn_dp_clk_disable(dp);
482 	dp->active = false;
483 	dp->max_lanes = 0;
484 	dp->max_rate = 0;
485 	if (!dp->connected) {
486 		kfree(dp->edid);
487 		dp->edid = NULL;
488 	}
489 
490 	return 0;
491 }
492 
493 static int cdn_dp_enable(struct cdn_dp_device *dp)
494 {
495 	int ret, i, lanes;
496 	struct cdn_dp_port *port;
497 
498 	port = cdn_dp_connected_port(dp);
499 	if (!port) {
500 		DRM_DEV_ERROR(dp->dev,
501 			      "Can't enable without connection\n");
502 		return -ENODEV;
503 	}
504 
505 	if (dp->active)
506 		return 0;
507 
508 	ret = cdn_dp_clk_enable(dp);
509 	if (ret)
510 		return ret;
511 
512 	ret = cdn_dp_firmware_init(dp);
513 	if (ret) {
514 		DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
515 		goto err_clk_disable;
516 	}
517 
518 	/* only enable the port that connected with downstream device */
519 	for (i = port->id; i < dp->ports; i++) {
520 		port = dp->port[i];
521 		lanes = cdn_dp_get_port_lanes(port);
522 		if (lanes) {
523 			ret = cdn_dp_enable_phy(dp, port);
524 			if (ret)
525 				continue;
526 
527 			ret = cdn_dp_get_sink_capability(dp);
528 			if (ret) {
529 				cdn_dp_disable_phy(dp, port);
530 			} else {
531 				dp->active = true;
532 				dp->lanes = port->lanes;
533 				return 0;
534 			}
535 		}
536 	}
537 
538 err_clk_disable:
539 	cdn_dp_clk_disable(dp);
540 	return ret;
541 }
542 
543 static void cdn_dp_encoder_mode_set(struct drm_encoder *encoder,
544 				    struct drm_display_mode *mode,
545 				    struct drm_display_mode *adjusted)
546 {
547 	struct cdn_dp_device *dp = encoder_to_dp(encoder);
548 	struct drm_display_info *display_info = &dp->connector.display_info;
549 	struct video_info *video = &dp->video_info;
550 
551 	switch (display_info->bpc) {
552 	case 10:
553 		video->color_depth = 10;
554 		break;
555 	case 6:
556 		video->color_depth = 6;
557 		break;
558 	default:
559 		video->color_depth = 8;
560 		break;
561 	}
562 
563 	video->color_fmt = PXL_RGB;
564 	video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
565 	video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
566 
567 	memcpy(&dp->mode, adjusted, sizeof(*mode));
568 }
569 
570 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
571 {
572 	u8 link_status[DP_LINK_STATUS_SIZE];
573 	struct cdn_dp_port *port = cdn_dp_connected_port(dp);
574 	u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
575 
576 	if (!port || !dp->max_rate || !dp->max_lanes)
577 		return false;
578 
579 	if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
580 			     DP_LINK_STATUS_SIZE)) {
581 		DRM_ERROR("Failed to get link status\n");
582 		return false;
583 	}
584 
585 	/* if link training is requested we should perform it always */
586 	return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
587 }
588 
589 static void cdn_dp_audio_handle_plugged_change(struct cdn_dp_device *dp,
590 					       bool plugged)
591 {
592 	if (dp->codec_dev)
593 		dp->plugged_cb(dp->codec_dev, plugged);
594 }
595 
596 static void cdn_dp_encoder_enable(struct drm_encoder *encoder)
597 {
598 	struct cdn_dp_device *dp = encoder_to_dp(encoder);
599 	int ret, val;
600 
601 	ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
602 	if (ret < 0) {
603 		DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
604 		return;
605 	}
606 
607 	DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
608 			  (ret) ? "LIT" : "BIG");
609 	if (ret)
610 		val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
611 	else
612 		val = DP_SEL_VOP_LIT << 16;
613 
614 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
615 	if (ret)
616 		return;
617 
618 	mutex_lock(&dp->lock);
619 
620 	ret = cdn_dp_enable(dp);
621 	if (ret) {
622 		DRM_DEV_ERROR(dp->dev, "Failed to enable encoder %d\n",
623 			      ret);
624 		goto out;
625 	}
626 	if (!cdn_dp_check_link_status(dp)) {
627 		ret = cdn_dp_train_link(dp);
628 		if (ret) {
629 			DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
630 			goto out;
631 		}
632 	}
633 
634 	ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
635 	if (ret) {
636 		DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
637 		goto out;
638 	}
639 
640 	ret = cdn_dp_config_video(dp);
641 	if (ret) {
642 		DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
643 		goto out;
644 	}
645 
646 	ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
647 	if (ret) {
648 		DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
649 		goto out;
650 	}
651 
652 	cdn_dp_audio_handle_plugged_change(dp, true);
653 
654 out:
655 	mutex_unlock(&dp->lock);
656 }
657 
658 static void cdn_dp_encoder_disable(struct drm_encoder *encoder)
659 {
660 	struct cdn_dp_device *dp = encoder_to_dp(encoder);
661 	int ret;
662 
663 	mutex_lock(&dp->lock);
664 	cdn_dp_audio_handle_plugged_change(dp, false);
665 
666 	if (dp->active) {
667 		ret = cdn_dp_disable(dp);
668 		if (ret) {
669 			DRM_DEV_ERROR(dp->dev, "Failed to disable encoder %d\n",
670 				      ret);
671 		}
672 	}
673 	mutex_unlock(&dp->lock);
674 
675 	/*
676 	 * In the following 2 cases, we need to run the event_work to re-enable
677 	 * the DP:
678 	 * 1. If there is not just one port device is connected, and remove one
679 	 *    device from a port, the DP will be disabled here, at this case,
680 	 *    run the event_work to re-open DP for the other port.
681 	 * 2. If re-training or re-config failed, the DP will be disabled here.
682 	 *    run the event_work to re-connect it.
683 	 */
684 	if (!dp->connected && cdn_dp_connected_port(dp))
685 		schedule_work(&dp->event_work);
686 }
687 
688 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
689 				       struct drm_crtc_state *crtc_state,
690 				       struct drm_connector_state *conn_state)
691 {
692 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
693 
694 	s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
695 	s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
696 
697 	return 0;
698 }
699 
700 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
701 	.mode_set = cdn_dp_encoder_mode_set,
702 	.enable = cdn_dp_encoder_enable,
703 	.disable = cdn_dp_encoder_disable,
704 	.atomic_check = cdn_dp_encoder_atomic_check,
705 };
706 
707 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
708 {
709 	struct device *dev = dp->dev;
710 	struct device_node *np = dev->of_node;
711 	struct platform_device *pdev = to_platform_device(dev);
712 
713 	dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
714 	if (IS_ERR(dp->grf)) {
715 		DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
716 		return PTR_ERR(dp->grf);
717 	}
718 
719 	dp->regs = devm_platform_ioremap_resource(pdev, 0);
720 	if (IS_ERR(dp->regs)) {
721 		DRM_DEV_ERROR(dev, "ioremap reg failed\n");
722 		return PTR_ERR(dp->regs);
723 	}
724 
725 	dp->core_clk = devm_clk_get(dev, "core-clk");
726 	if (IS_ERR(dp->core_clk)) {
727 		DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
728 		return PTR_ERR(dp->core_clk);
729 	}
730 
731 	dp->pclk = devm_clk_get(dev, "pclk");
732 	if (IS_ERR(dp->pclk)) {
733 		DRM_DEV_ERROR(dev, "cannot get pclk\n");
734 		return PTR_ERR(dp->pclk);
735 	}
736 
737 	dp->spdif_clk = devm_clk_get(dev, "spdif");
738 	if (IS_ERR(dp->spdif_clk)) {
739 		DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
740 		return PTR_ERR(dp->spdif_clk);
741 	}
742 
743 	dp->grf_clk = devm_clk_get(dev, "grf");
744 	if (IS_ERR(dp->grf_clk)) {
745 		DRM_DEV_ERROR(dev, "cannot get grf clk\n");
746 		return PTR_ERR(dp->grf_clk);
747 	}
748 
749 	dp->spdif_rst = devm_reset_control_get(dev, "spdif");
750 	if (IS_ERR(dp->spdif_rst)) {
751 		DRM_DEV_ERROR(dev, "no spdif reset control found\n");
752 		return PTR_ERR(dp->spdif_rst);
753 	}
754 
755 	dp->dptx_rst = devm_reset_control_get(dev, "dptx");
756 	if (IS_ERR(dp->dptx_rst)) {
757 		DRM_DEV_ERROR(dev, "no uphy reset control found\n");
758 		return PTR_ERR(dp->dptx_rst);
759 	}
760 
761 	dp->core_rst = devm_reset_control_get(dev, "core");
762 	if (IS_ERR(dp->core_rst)) {
763 		DRM_DEV_ERROR(dev, "no core reset control found\n");
764 		return PTR_ERR(dp->core_rst);
765 	}
766 
767 	dp->apb_rst = devm_reset_control_get(dev, "apb");
768 	if (IS_ERR(dp->apb_rst)) {
769 		DRM_DEV_ERROR(dev, "no apb reset control found\n");
770 		return PTR_ERR(dp->apb_rst);
771 	}
772 
773 	return 0;
774 }
775 
776 static int cdn_dp_audio_hw_params(struct device *dev,  void *data,
777 				  struct hdmi_codec_daifmt *daifmt,
778 				  struct hdmi_codec_params *params)
779 {
780 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
781 	struct audio_info audio = {
782 		.sample_width = params->sample_width,
783 		.sample_rate = params->sample_rate,
784 		.channels = params->channels,
785 	};
786 	int ret;
787 
788 	mutex_lock(&dp->lock);
789 	if (!dp->active) {
790 		ret = -ENODEV;
791 		goto out;
792 	}
793 
794 	switch (daifmt->fmt) {
795 	case HDMI_I2S:
796 		audio.format = AFMT_I2S;
797 		break;
798 	case HDMI_SPDIF:
799 		audio.format = AFMT_SPDIF;
800 		break;
801 	default:
802 		DRM_DEV_ERROR(dev, "Invalid format %d\n", daifmt->fmt);
803 		ret = -EINVAL;
804 		goto out;
805 	}
806 
807 	ret = cdn_dp_audio_config(dp, &audio);
808 	if (!ret)
809 		dp->audio_info = audio;
810 
811 out:
812 	mutex_unlock(&dp->lock);
813 	return ret;
814 }
815 
816 static void cdn_dp_audio_shutdown(struct device *dev, void *data)
817 {
818 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
819 	int ret;
820 
821 	mutex_lock(&dp->lock);
822 	if (!dp->active)
823 		goto out;
824 
825 	ret = cdn_dp_audio_stop(dp, &dp->audio_info);
826 	if (!ret)
827 		dp->audio_info.format = AFMT_UNUSED;
828 out:
829 	mutex_unlock(&dp->lock);
830 }
831 
832 static int cdn_dp_audio_mute_stream(struct device *dev, void *data,
833 				    bool enable, int direction)
834 {
835 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
836 	int ret;
837 
838 	mutex_lock(&dp->lock);
839 	if (!dp->active) {
840 		ret = -ENODEV;
841 		goto out;
842 	}
843 
844 	ret = cdn_dp_audio_mute(dp, enable);
845 
846 out:
847 	mutex_unlock(&dp->lock);
848 	return ret;
849 }
850 
851 static int cdn_dp_audio_get_eld(struct device *dev, void *data,
852 				u8 *buf, size_t len)
853 {
854 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
855 
856 	memcpy(buf, dp->connector.eld, min(sizeof(dp->connector.eld), len));
857 
858 	return 0;
859 }
860 
861 static int cdn_dp_audio_hook_plugged_cb(struct device *dev, void *data,
862 					hdmi_codec_plugged_cb fn,
863 					struct device *codec_dev)
864 {
865 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
866 
867 	mutex_lock(&dp->lock);
868 	dp->plugged_cb = fn;
869 	dp->codec_dev = codec_dev;
870 	cdn_dp_audio_handle_plugged_change(dp, dp->connected);
871 	mutex_unlock(&dp->lock);
872 
873 	return 0;
874 }
875 
876 static const struct hdmi_codec_ops audio_codec_ops = {
877 	.hw_params = cdn_dp_audio_hw_params,
878 	.audio_shutdown = cdn_dp_audio_shutdown,
879 	.mute_stream = cdn_dp_audio_mute_stream,
880 	.get_eld = cdn_dp_audio_get_eld,
881 	.hook_plugged_cb = cdn_dp_audio_hook_plugged_cb,
882 	.no_capture_mute = 1,
883 };
884 
885 static int cdn_dp_audio_codec_init(struct cdn_dp_device *dp,
886 				   struct device *dev)
887 {
888 	struct hdmi_codec_pdata codec_data = {
889 		.i2s = 1,
890 		.spdif = 1,
891 		.ops = &audio_codec_ops,
892 		.max_i2s_channels = 8,
893 	};
894 
895 	dp->audio_pdev = platform_device_register_data(
896 			 dev, HDMI_CODEC_DRV_NAME, PLATFORM_DEVID_AUTO,
897 			 &codec_data, sizeof(codec_data));
898 
899 	return PTR_ERR_OR_ZERO(dp->audio_pdev);
900 }
901 
902 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
903 {
904 	int ret;
905 	unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
906 	unsigned long sleep = 1000;
907 
908 	WARN_ON(!mutex_is_locked(&dp->lock));
909 
910 	if (dp->fw_loaded)
911 		return 0;
912 
913 	/* Drop the lock before getting the firmware to avoid blocking boot */
914 	mutex_unlock(&dp->lock);
915 
916 	while (time_before(jiffies, timeout)) {
917 		ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
918 		if (ret == -ENOENT) {
919 			msleep(sleep);
920 			sleep *= 2;
921 			continue;
922 		} else if (ret) {
923 			DRM_DEV_ERROR(dp->dev,
924 				      "failed to request firmware: %d\n", ret);
925 			goto out;
926 		}
927 
928 		dp->fw_loaded = true;
929 		ret = 0;
930 		goto out;
931 	}
932 
933 	DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
934 	ret = -ETIMEDOUT;
935 out:
936 	mutex_lock(&dp->lock);
937 	return ret;
938 }
939 
940 static void cdn_dp_pd_event_work(struct work_struct *work)
941 {
942 	struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
943 						event_work);
944 	struct drm_connector *connector = &dp->connector;
945 	enum drm_connector_status old_status;
946 
947 	int ret;
948 
949 	mutex_lock(&dp->lock);
950 
951 	if (dp->suspended)
952 		goto out;
953 
954 	ret = cdn_dp_request_firmware(dp);
955 	if (ret)
956 		goto out;
957 
958 	dp->connected = true;
959 
960 	/* Not connected, notify userspace to disable the block */
961 	if (!cdn_dp_connected_port(dp)) {
962 		DRM_DEV_INFO(dp->dev, "Not connected. Disabling cdn\n");
963 		dp->connected = false;
964 
965 	/* Connected but not enabled, enable the block */
966 	} else if (!dp->active) {
967 		DRM_DEV_INFO(dp->dev, "Connected, not enabled. Enabling cdn\n");
968 		ret = cdn_dp_enable(dp);
969 		if (ret) {
970 			DRM_DEV_ERROR(dp->dev, "Enable dp failed %d\n", ret);
971 			dp->connected = false;
972 		}
973 
974 	/* Enabled and connected to a dongle without a sink, notify userspace */
975 	} else if (!cdn_dp_check_sink_connection(dp)) {
976 		DRM_DEV_INFO(dp->dev, "Connected without sink. Assert hpd\n");
977 		dp->connected = false;
978 
979 	/* Enabled and connected with a sink, re-train if requested */
980 	} else if (!cdn_dp_check_link_status(dp)) {
981 		unsigned int rate = dp->max_rate;
982 		unsigned int lanes = dp->max_lanes;
983 		struct drm_display_mode *mode = &dp->mode;
984 
985 		DRM_DEV_INFO(dp->dev, "Connected with sink. Re-train link\n");
986 		ret = cdn_dp_train_link(dp);
987 		if (ret) {
988 			dp->connected = false;
989 			DRM_DEV_ERROR(dp->dev, "Train link failed %d\n", ret);
990 			goto out;
991 		}
992 
993 		/* If training result is changed, update the video config */
994 		if (mode->clock &&
995 		    (rate != dp->max_rate || lanes != dp->max_lanes)) {
996 			ret = cdn_dp_config_video(dp);
997 			if (ret) {
998 				dp->connected = false;
999 				DRM_DEV_ERROR(dp->dev,
1000 					      "Failed to config video %d\n",
1001 					      ret);
1002 			}
1003 		}
1004 	}
1005 
1006 out:
1007 	mutex_unlock(&dp->lock);
1008 
1009 	old_status = connector->status;
1010 	connector->status = connector->funcs->detect(connector, false);
1011 	if (old_status != connector->status)
1012 		drm_kms_helper_hotplug_event(dp->drm_dev);
1013 }
1014 
1015 static int cdn_dp_pd_event(struct notifier_block *nb,
1016 			   unsigned long event, void *priv)
1017 {
1018 	struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
1019 						event_nb);
1020 	struct cdn_dp_device *dp = port->dp;
1021 
1022 	/*
1023 	 * It would be nice to be able to just do the work inline right here.
1024 	 * However, we need to make a bunch of calls that might sleep in order
1025 	 * to turn on the block/phy, so use a worker instead.
1026 	 */
1027 	schedule_work(&dp->event_work);
1028 
1029 	return NOTIFY_DONE;
1030 }
1031 
1032 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
1033 {
1034 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1035 	struct drm_encoder *encoder;
1036 	struct drm_connector *connector;
1037 	struct cdn_dp_port *port;
1038 	struct drm_device *drm_dev = data;
1039 	int ret, i;
1040 
1041 	ret = cdn_dp_parse_dt(dp);
1042 	if (ret < 0)
1043 		return ret;
1044 
1045 	dp->drm_dev = drm_dev;
1046 	dp->connected = false;
1047 	dp->active = false;
1048 	dp->active_port = -1;
1049 	dp->fw_loaded = false;
1050 
1051 	INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
1052 
1053 	encoder = &dp->encoder;
1054 
1055 	encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
1056 							     dev->of_node);
1057 	DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
1058 
1059 	ret = drm_simple_encoder_init(drm_dev, encoder,
1060 				      DRM_MODE_ENCODER_TMDS);
1061 	if (ret) {
1062 		DRM_ERROR("failed to initialize encoder with drm\n");
1063 		return ret;
1064 	}
1065 
1066 	drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
1067 
1068 	connector = &dp->connector;
1069 	connector->polled = DRM_CONNECTOR_POLL_HPD;
1070 	connector->dpms = DRM_MODE_DPMS_OFF;
1071 
1072 	ret = drm_connector_init(drm_dev, connector,
1073 				 &cdn_dp_atomic_connector_funcs,
1074 				 DRM_MODE_CONNECTOR_DisplayPort);
1075 	if (ret) {
1076 		DRM_ERROR("failed to initialize connector with drm\n");
1077 		goto err_free_encoder;
1078 	}
1079 
1080 	drm_connector_helper_add(connector, &cdn_dp_connector_helper_funcs);
1081 
1082 	ret = drm_connector_attach_encoder(connector, encoder);
1083 	if (ret) {
1084 		DRM_ERROR("failed to attach connector and encoder\n");
1085 		goto err_free_connector;
1086 	}
1087 
1088 	for (i = 0; i < dp->ports; i++) {
1089 		port = dp->port[i];
1090 
1091 		port->event_nb.notifier_call = cdn_dp_pd_event;
1092 		ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1093 						    EXTCON_DISP_DP,
1094 						    &port->event_nb);
1095 		if (ret) {
1096 			DRM_DEV_ERROR(dev,
1097 				      "register EXTCON_DISP_DP notifier err\n");
1098 			goto err_free_connector;
1099 		}
1100 	}
1101 
1102 	pm_runtime_enable(dev);
1103 
1104 	schedule_work(&dp->event_work);
1105 
1106 	return 0;
1107 
1108 err_free_connector:
1109 	drm_connector_cleanup(connector);
1110 err_free_encoder:
1111 	drm_encoder_cleanup(encoder);
1112 	return ret;
1113 }
1114 
1115 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1116 {
1117 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1118 	struct drm_encoder *encoder = &dp->encoder;
1119 	struct drm_connector *connector = &dp->connector;
1120 
1121 	cancel_work_sync(&dp->event_work);
1122 	cdn_dp_encoder_disable(encoder);
1123 	encoder->funcs->destroy(encoder);
1124 	connector->funcs->destroy(connector);
1125 
1126 	pm_runtime_disable(dev);
1127 	if (dp->fw_loaded)
1128 		release_firmware(dp->fw);
1129 	kfree(dp->edid);
1130 	dp->edid = NULL;
1131 }
1132 
1133 static const struct component_ops cdn_dp_component_ops = {
1134 	.bind = cdn_dp_bind,
1135 	.unbind = cdn_dp_unbind,
1136 };
1137 
1138 static int cdn_dp_suspend(struct device *dev)
1139 {
1140 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1141 	int ret = 0;
1142 
1143 	mutex_lock(&dp->lock);
1144 	if (dp->active)
1145 		ret = cdn_dp_disable(dp);
1146 	dp->suspended = true;
1147 	mutex_unlock(&dp->lock);
1148 
1149 	return ret;
1150 }
1151 
1152 static __maybe_unused int cdn_dp_resume(struct device *dev)
1153 {
1154 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1155 
1156 	mutex_lock(&dp->lock);
1157 	dp->suspended = false;
1158 	if (dp->fw_loaded)
1159 		schedule_work(&dp->event_work);
1160 	mutex_unlock(&dp->lock);
1161 
1162 	return 0;
1163 }
1164 
1165 static int cdn_dp_probe(struct platform_device *pdev)
1166 {
1167 	struct device *dev = &pdev->dev;
1168 	const struct of_device_id *match;
1169 	struct cdn_dp_data *dp_data;
1170 	struct cdn_dp_port *port;
1171 	struct cdn_dp_device *dp;
1172 	struct extcon_dev *extcon;
1173 	struct phy *phy;
1174 	int i;
1175 
1176 	dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
1177 	if (!dp)
1178 		return -ENOMEM;
1179 	dp->dev = dev;
1180 
1181 	match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1182 	dp_data = (struct cdn_dp_data *)match->data;
1183 
1184 	for (i = 0; i < dp_data->max_phy; i++) {
1185 		extcon = extcon_get_edev_by_phandle(dev, i);
1186 		phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1187 
1188 		if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1189 		    PTR_ERR(phy) == -EPROBE_DEFER)
1190 			return -EPROBE_DEFER;
1191 
1192 		if (IS_ERR(extcon) || IS_ERR(phy))
1193 			continue;
1194 
1195 		port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1196 		if (!port)
1197 			return -ENOMEM;
1198 
1199 		port->extcon = extcon;
1200 		port->phy = phy;
1201 		port->dp = dp;
1202 		port->id = i;
1203 		dp->port[dp->ports++] = port;
1204 	}
1205 
1206 	if (!dp->ports) {
1207 		DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1208 		return -EINVAL;
1209 	}
1210 
1211 	mutex_init(&dp->lock);
1212 	dev_set_drvdata(dev, dp);
1213 
1214 	cdn_dp_audio_codec_init(dp, dev);
1215 
1216 	return component_add(dev, &cdn_dp_component_ops);
1217 }
1218 
1219 static int cdn_dp_remove(struct platform_device *pdev)
1220 {
1221 	struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1222 
1223 	platform_device_unregister(dp->audio_pdev);
1224 	cdn_dp_suspend(dp->dev);
1225 	component_del(&pdev->dev, &cdn_dp_component_ops);
1226 
1227 	return 0;
1228 }
1229 
1230 static void cdn_dp_shutdown(struct platform_device *pdev)
1231 {
1232 	struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1233 
1234 	cdn_dp_suspend(dp->dev);
1235 }
1236 
1237 static const struct dev_pm_ops cdn_dp_pm_ops = {
1238 	SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1239 				cdn_dp_resume)
1240 };
1241 
1242 struct platform_driver cdn_dp_driver = {
1243 	.probe = cdn_dp_probe,
1244 	.remove = cdn_dp_remove,
1245 	.shutdown = cdn_dp_shutdown,
1246 	.driver = {
1247 		   .name = "cdn-dp",
1248 		   .owner = THIS_MODULE,
1249 		   .of_match_table = of_match_ptr(cdn_dp_dt_ids),
1250 		   .pm = &cdn_dp_pm_ops,
1251 	},
1252 };
1253