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