xref: /linux/drivers/gpu/drm/rockchip/cdn-dp-core.c (revision f6e8dc9edf963dbc99085e54f6ced6da9daa6100)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 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/display/drm_dp_helper.h>
19 #include <drm/display/drm_hdmi_audio_helper.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_bridge_connector.h>
22 #include <drm/drm_edid.h>
23 #include <drm/drm_of.h>
24 #include <drm/drm_print.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_simple_kms_helper.h>
27 
28 #include "cdn-dp-core.h"
29 #include "cdn-dp-reg.h"
30 
31 static inline struct cdn_dp_device *bridge_to_dp(struct drm_bridge *bridge)
32 {
33 	return container_of(bridge, struct cdn_dp_device, bridge);
34 }
35 
36 static inline struct cdn_dp_device *encoder_to_dp(struct drm_encoder *encoder)
37 {
38 	struct rockchip_encoder *rkencoder = to_rockchip_encoder(encoder);
39 
40 	return container_of(rkencoder, struct cdn_dp_device, encoder);
41 }
42 
43 #define GRF_SOC_CON9		0x6224
44 #define DP_SEL_VOP_LIT		BIT(12)
45 #define GRF_SOC_CON26		0x6268
46 #define DPTX_HPD_SEL		(3 << 12)
47 #define DPTX_HPD_DEL		(2 << 12)
48 #define DPTX_HPD_SEL_MASK	(3 << 28)
49 
50 #define CDN_FW_TIMEOUT_MS	(64 * 1000)
51 #define CDN_DPCD_TIMEOUT_MS	5000
52 #define CDN_DP_FIRMWARE		"rockchip/dptx.bin"
53 MODULE_FIRMWARE(CDN_DP_FIRMWARE);
54 
55 struct cdn_dp_data {
56 	u8 max_phy;
57 };
58 
59 static struct cdn_dp_data rk3399_cdn_dp = {
60 	.max_phy = 2,
61 };
62 
63 static const struct of_device_id cdn_dp_dt_ids[] = {
64 	{ .compatible = "rockchip,rk3399-cdn-dp",
65 		.data = (void *)&rk3399_cdn_dp },
66 	{}
67 };
68 
69 MODULE_DEVICE_TABLE(of, cdn_dp_dt_ids);
70 
71 static int cdn_dp_grf_write(struct cdn_dp_device *dp,
72 			    unsigned int reg, unsigned int val)
73 {
74 	int ret;
75 
76 	ret = clk_prepare_enable(dp->grf_clk);
77 	if (ret) {
78 		DRM_DEV_ERROR(dp->dev, "Failed to prepare_enable grf clock\n");
79 		return ret;
80 	}
81 
82 	ret = regmap_write(dp->grf, reg, val);
83 	if (ret) {
84 		DRM_DEV_ERROR(dp->dev, "Could not write to GRF: %d\n", ret);
85 		clk_disable_unprepare(dp->grf_clk);
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_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)
238 {
239 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
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 const struct drm_edid *
251 cdn_dp_bridge_edid_read(struct drm_bridge *bridge, struct drm_connector *connector)
252 {
253 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
254 	const struct drm_edid *drm_edid;
255 
256 	mutex_lock(&dp->lock);
257 	drm_edid = drm_edid_read_custom(connector, cdn_dp_get_edid_block, dp);
258 	mutex_unlock(&dp->lock);
259 
260 	return drm_edid;
261 }
262 
263 static enum drm_mode_status
264 cdn_dp_bridge_mode_valid(struct drm_bridge *bridge,
265 			 const struct drm_display_info *display_info,
266 			 const struct drm_display_mode *mode)
267 {
268 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
269 	u32 requested, actual, rate, sink_max, source_max = 0;
270 	u8 lanes, bpc;
271 
272 	/* If DP is disconnected, every mode is invalid */
273 	if (!dp->connected)
274 		return MODE_BAD;
275 
276 	switch (display_info->bpc) {
277 	case 10:
278 		bpc = 10;
279 		break;
280 	case 6:
281 		bpc = 6;
282 		break;
283 	default:
284 		bpc = 8;
285 		break;
286 	}
287 
288 	requested = mode->clock * bpc * 3 / 1000;
289 
290 	source_max = dp->lanes;
291 	sink_max = drm_dp_max_lane_count(dp->dpcd);
292 	lanes = min(source_max, sink_max);
293 
294 	source_max = drm_dp_bw_code_to_link_rate(CDN_DP_MAX_LINK_RATE);
295 	sink_max = drm_dp_max_link_rate(dp->dpcd);
296 	rate = min(source_max, sink_max);
297 
298 	actual = rate * lanes / 100;
299 
300 	/* efficiency is about 0.8 */
301 	actual = actual * 8 / 10;
302 
303 	if (requested > actual) {
304 		DRM_DEV_DEBUG_KMS(dp->dev,
305 				  "requested=%d, actual=%d, clock=%d\n",
306 				  requested, actual, mode->clock);
307 		return MODE_CLOCK_HIGH;
308 	}
309 
310 	return MODE_OK;
311 }
312 
313 static int cdn_dp_firmware_init(struct cdn_dp_device *dp)
314 {
315 	int ret;
316 	const u32 *iram_data, *dram_data;
317 	const struct firmware *fw = dp->fw;
318 	const struct cdn_firmware_header *hdr;
319 
320 	hdr = (struct cdn_firmware_header *)fw->data;
321 	if (fw->size != le32_to_cpu(hdr->size_bytes)) {
322 		DRM_DEV_ERROR(dp->dev, "firmware is invalid\n");
323 		return -EINVAL;
324 	}
325 
326 	iram_data = (const u32 *)(fw->data + hdr->header_size);
327 	dram_data = (const u32 *)(fw->data + hdr->header_size + hdr->iram_size);
328 
329 	ret = cdn_dp_load_firmware(dp, iram_data, hdr->iram_size,
330 				   dram_data, hdr->dram_size);
331 	if (ret)
332 		return ret;
333 
334 	ret = cdn_dp_set_firmware_active(dp, true);
335 	if (ret) {
336 		DRM_DEV_ERROR(dp->dev, "active ucpu failed: %d\n", ret);
337 		return ret;
338 	}
339 
340 	return cdn_dp_event_config(dp);
341 }
342 
343 static int cdn_dp_get_sink_capability(struct cdn_dp_device *dp)
344 {
345 	int ret;
346 
347 	if (!cdn_dp_check_sink_connection(dp))
348 		return -ENODEV;
349 
350 	ret = cdn_dp_dpcd_read(dp, DP_DPCD_REV, dp->dpcd,
351 			       DP_RECEIVER_CAP_SIZE);
352 	if (ret) {
353 		DRM_DEV_ERROR(dp->dev, "Failed to get caps %d\n", ret);
354 		return ret;
355 	}
356 
357 	return 0;
358 }
359 
360 static int cdn_dp_enable_phy(struct cdn_dp_device *dp, struct cdn_dp_port *port)
361 {
362 	union extcon_property_value property;
363 	int ret;
364 
365 	if (!port->phy_enabled) {
366 		ret = phy_power_on(port->phy);
367 		if (ret) {
368 			DRM_DEV_ERROR(dp->dev, "phy power on failed: %d\n",
369 				      ret);
370 			goto err_phy;
371 		}
372 		port->phy_enabled = true;
373 	}
374 
375 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
376 			       DPTX_HPD_SEL_MASK | DPTX_HPD_SEL);
377 	if (ret) {
378 		DRM_DEV_ERROR(dp->dev, "Failed to write HPD_SEL %d\n", ret);
379 		goto err_power_on;
380 	}
381 
382 	ret = cdn_dp_get_hpd_status(dp);
383 	if (ret <= 0) {
384 		if (!ret)
385 			DRM_DEV_ERROR(dp->dev, "hpd does not exist\n");
386 		goto err_power_on;
387 	}
388 
389 	ret = extcon_get_property(port->extcon, EXTCON_DISP_DP,
390 				  EXTCON_PROP_USB_TYPEC_POLARITY, &property);
391 	if (ret) {
392 		DRM_DEV_ERROR(dp->dev, "get property failed\n");
393 		goto err_power_on;
394 	}
395 
396 	port->lanes = cdn_dp_get_port_lanes(port);
397 	ret = cdn_dp_set_host_cap(dp, port->lanes, property.intval);
398 	if (ret) {
399 		DRM_DEV_ERROR(dp->dev, "set host capabilities failed: %d\n",
400 			      ret);
401 		goto err_power_on;
402 	}
403 
404 	dp->active_port = port->id;
405 	return 0;
406 
407 err_power_on:
408 	if (phy_power_off(port->phy))
409 		DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
410 	else
411 		port->phy_enabled = false;
412 
413 err_phy:
414 	cdn_dp_grf_write(dp, GRF_SOC_CON26,
415 			 DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
416 	return ret;
417 }
418 
419 static int cdn_dp_disable_phy(struct cdn_dp_device *dp,
420 			      struct cdn_dp_port *port)
421 {
422 	int ret;
423 
424 	if (port->phy_enabled) {
425 		ret = phy_power_off(port->phy);
426 		if (ret) {
427 			DRM_DEV_ERROR(dp->dev, "phy power off failed: %d", ret);
428 			return ret;
429 		}
430 	}
431 
432 	port->phy_enabled = false;
433 	port->lanes = 0;
434 	dp->active_port = -1;
435 	return 0;
436 }
437 
438 static int cdn_dp_disable(struct cdn_dp_device *dp)
439 {
440 	int ret, i;
441 
442 	if (!dp->active)
443 		return 0;
444 
445 	for (i = 0; i < dp->ports; i++)
446 		cdn_dp_disable_phy(dp, dp->port[i]);
447 
448 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON26,
449 			       DPTX_HPD_SEL_MASK | DPTX_HPD_DEL);
450 	if (ret) {
451 		DRM_DEV_ERROR(dp->dev, "Failed to clear hpd sel %d\n",
452 			      ret);
453 		return ret;
454 	}
455 
456 	cdn_dp_set_firmware_active(dp, false);
457 	cdn_dp_clk_disable(dp);
458 	dp->active = false;
459 	dp->max_lanes = 0;
460 	dp->max_rate = 0;
461 
462 	return 0;
463 }
464 
465 static int cdn_dp_enable(struct cdn_dp_device *dp)
466 {
467 	int ret, i, lanes;
468 	struct cdn_dp_port *port;
469 
470 	port = cdn_dp_connected_port(dp);
471 	if (!port) {
472 		DRM_DEV_ERROR(dp->dev,
473 			      "Can't enable without connection\n");
474 		return -ENODEV;
475 	}
476 
477 	if (dp->active)
478 		return 0;
479 
480 	ret = cdn_dp_clk_enable(dp);
481 	if (ret)
482 		return ret;
483 
484 	ret = cdn_dp_firmware_init(dp);
485 	if (ret) {
486 		DRM_DEV_ERROR(dp->dev, "firmware init failed: %d", ret);
487 		goto err_clk_disable;
488 	}
489 
490 	/* only enable the port that connected with downstream device */
491 	for (i = port->id; i < dp->ports; i++) {
492 		port = dp->port[i];
493 		lanes = cdn_dp_get_port_lanes(port);
494 		if (lanes) {
495 			ret = cdn_dp_enable_phy(dp, port);
496 			if (ret)
497 				continue;
498 
499 			ret = cdn_dp_get_sink_capability(dp);
500 			if (ret) {
501 				cdn_dp_disable_phy(dp, port);
502 			} else {
503 				dp->active = true;
504 				dp->lanes = port->lanes;
505 				return 0;
506 			}
507 		}
508 	}
509 
510 err_clk_disable:
511 	cdn_dp_clk_disable(dp);
512 	return ret;
513 }
514 
515 static void cdn_dp_bridge_mode_set(struct drm_bridge *bridge,
516 				   const struct drm_display_mode *mode,
517 				   const struct drm_display_mode *adjusted)
518 {
519 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
520 	struct video_info *video = &dp->video_info;
521 
522 	video->color_fmt = PXL_RGB;
523 	video->v_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NVSYNC);
524 	video->h_sync_polarity = !!(mode->flags & DRM_MODE_FLAG_NHSYNC);
525 
526 	drm_mode_copy(&dp->mode, adjusted);
527 }
528 
529 static bool cdn_dp_check_link_status(struct cdn_dp_device *dp)
530 {
531 	u8 link_status[DP_LINK_STATUS_SIZE];
532 	struct cdn_dp_port *port = cdn_dp_connected_port(dp);
533 	u8 sink_lanes = drm_dp_max_lane_count(dp->dpcd);
534 
535 	if (!port || !dp->max_rate || !dp->max_lanes)
536 		return false;
537 
538 	if (cdn_dp_dpcd_read(dp, DP_LANE0_1_STATUS, link_status,
539 			     DP_LINK_STATUS_SIZE)) {
540 		DRM_ERROR("Failed to get link status\n");
541 		return false;
542 	}
543 
544 	/* if link training is requested we should perform it always */
545 	return drm_dp_channel_eq_ok(link_status, min(port->lanes, sink_lanes));
546 }
547 
548 static void cdn_dp_display_info_update(struct cdn_dp_device *dp,
549 				       struct drm_display_info *display_info)
550 {
551 	struct video_info *video = &dp->video_info;
552 
553 	switch (display_info->bpc) {
554 	case 10:
555 		video->color_depth = 10;
556 		break;
557 	case 6:
558 		video->color_depth = 6;
559 		break;
560 	default:
561 		video->color_depth = 8;
562 		break;
563 	}
564 }
565 
566 static void cdn_dp_bridge_atomic_enable(struct drm_bridge *bridge, struct drm_atomic_state *state)
567 {
568 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
569 	struct drm_connector *connector;
570 	int ret, val;
571 
572 	connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
573 	if (!connector)
574 		return;
575 
576 	cdn_dp_display_info_update(dp, &connector->display_info);
577 
578 	ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, &dp->encoder.encoder);
579 	if (ret < 0) {
580 		DRM_DEV_ERROR(dp->dev, "Could not get vop id, %d", ret);
581 		return;
582 	}
583 
584 	DRM_DEV_DEBUG_KMS(dp->dev, "vop %s output to cdn-dp\n",
585 			  (ret) ? "LIT" : "BIG");
586 	if (ret)
587 		val = DP_SEL_VOP_LIT | (DP_SEL_VOP_LIT << 16);
588 	else
589 		val = DP_SEL_VOP_LIT << 16;
590 
591 	ret = cdn_dp_grf_write(dp, GRF_SOC_CON9, val);
592 	if (ret)
593 		return;
594 
595 	mutex_lock(&dp->lock);
596 
597 	ret = cdn_dp_enable(dp);
598 	if (ret) {
599 		DRM_DEV_ERROR(dp->dev, "Failed to enable bridge %d\n",
600 			      ret);
601 		goto out;
602 	}
603 	if (!cdn_dp_check_link_status(dp)) {
604 		ret = cdn_dp_train_link(dp);
605 		if (ret) {
606 			DRM_DEV_ERROR(dp->dev, "Failed link train %d\n", ret);
607 			goto out;
608 		}
609 	}
610 
611 	ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_IDLE);
612 	if (ret) {
613 		DRM_DEV_ERROR(dp->dev, "Failed to idle video %d\n", ret);
614 		goto out;
615 	}
616 
617 	ret = cdn_dp_config_video(dp);
618 	if (ret) {
619 		DRM_DEV_ERROR(dp->dev, "Failed to config video %d\n", ret);
620 		goto out;
621 	}
622 
623 	ret = cdn_dp_set_video_status(dp, CONTROL_VIDEO_VALID);
624 	if (ret) {
625 		DRM_DEV_ERROR(dp->dev, "Failed to valid video %d\n", ret);
626 		goto out;
627 	}
628 
629 out:
630 	mutex_unlock(&dp->lock);
631 }
632 
633 static void cdn_dp_bridge_atomic_disable(struct drm_bridge *bridge, struct drm_atomic_state *state)
634 {
635 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
636 	int ret;
637 
638 	mutex_lock(&dp->lock);
639 
640 	if (dp->active) {
641 		ret = cdn_dp_disable(dp);
642 		if (ret) {
643 			DRM_DEV_ERROR(dp->dev, "Failed to disable bridge %d\n",
644 				      ret);
645 		}
646 	}
647 	mutex_unlock(&dp->lock);
648 
649 	/*
650 	 * In the following 2 cases, we need to run the event_work to re-enable
651 	 * the DP:
652 	 * 1. If there is not just one port device is connected, and remove one
653 	 *    device from a port, the DP will be disabled here, at this case,
654 	 *    run the event_work to re-open DP for the other port.
655 	 * 2. If re-training or re-config failed, the DP will be disabled here.
656 	 *    run the event_work to re-connect it.
657 	 */
658 	if (!dp->connected && cdn_dp_connected_port(dp))
659 		schedule_work(&dp->event_work);
660 }
661 
662 static int cdn_dp_encoder_atomic_check(struct drm_encoder *encoder,
663 				       struct drm_crtc_state *crtc_state,
664 				       struct drm_connector_state *conn_state)
665 {
666 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
667 
668 	s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
669 	s->output_type = DRM_MODE_CONNECTOR_DisplayPort;
670 
671 	return 0;
672 }
673 
674 static const struct drm_encoder_helper_funcs cdn_dp_encoder_helper_funcs = {
675 	.atomic_check = cdn_dp_encoder_atomic_check,
676 };
677 
678 static int cdn_dp_parse_dt(struct cdn_dp_device *dp)
679 {
680 	struct device *dev = dp->dev;
681 	struct device_node *np = dev->of_node;
682 	struct platform_device *pdev = to_platform_device(dev);
683 
684 	dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
685 	if (IS_ERR(dp->grf)) {
686 		DRM_DEV_ERROR(dev, "cdn-dp needs rockchip,grf property\n");
687 		return PTR_ERR(dp->grf);
688 	}
689 
690 	dp->regs = devm_platform_ioremap_resource(pdev, 0);
691 	if (IS_ERR(dp->regs)) {
692 		DRM_DEV_ERROR(dev, "ioremap reg failed\n");
693 		return PTR_ERR(dp->regs);
694 	}
695 
696 	dp->core_clk = devm_clk_get(dev, "core-clk");
697 	if (IS_ERR(dp->core_clk)) {
698 		DRM_DEV_ERROR(dev, "cannot get core_clk_dp\n");
699 		return PTR_ERR(dp->core_clk);
700 	}
701 
702 	dp->pclk = devm_clk_get(dev, "pclk");
703 	if (IS_ERR(dp->pclk)) {
704 		DRM_DEV_ERROR(dev, "cannot get pclk\n");
705 		return PTR_ERR(dp->pclk);
706 	}
707 
708 	dp->spdif_clk = devm_clk_get(dev, "spdif");
709 	if (IS_ERR(dp->spdif_clk)) {
710 		DRM_DEV_ERROR(dev, "cannot get spdif_clk\n");
711 		return PTR_ERR(dp->spdif_clk);
712 	}
713 
714 	dp->grf_clk = devm_clk_get(dev, "grf");
715 	if (IS_ERR(dp->grf_clk)) {
716 		DRM_DEV_ERROR(dev, "cannot get grf clk\n");
717 		return PTR_ERR(dp->grf_clk);
718 	}
719 
720 	dp->spdif_rst = devm_reset_control_get(dev, "spdif");
721 	if (IS_ERR(dp->spdif_rst)) {
722 		DRM_DEV_ERROR(dev, "no spdif reset control found\n");
723 		return PTR_ERR(dp->spdif_rst);
724 	}
725 
726 	dp->dptx_rst = devm_reset_control_get(dev, "dptx");
727 	if (IS_ERR(dp->dptx_rst)) {
728 		DRM_DEV_ERROR(dev, "no uphy reset control found\n");
729 		return PTR_ERR(dp->dptx_rst);
730 	}
731 
732 	dp->core_rst = devm_reset_control_get(dev, "core");
733 	if (IS_ERR(dp->core_rst)) {
734 		DRM_DEV_ERROR(dev, "no core reset control found\n");
735 		return PTR_ERR(dp->core_rst);
736 	}
737 
738 	dp->apb_rst = devm_reset_control_get(dev, "apb");
739 	if (IS_ERR(dp->apb_rst)) {
740 		DRM_DEV_ERROR(dev, "no apb reset control found\n");
741 		return PTR_ERR(dp->apb_rst);
742 	}
743 
744 	return 0;
745 }
746 
747 static int cdn_dp_audio_prepare(struct drm_bridge *bridge,
748 				struct drm_connector *connector,
749 				struct hdmi_codec_daifmt *daifmt,
750 				struct hdmi_codec_params *params)
751 {
752 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
753 	struct audio_info audio = {
754 		.sample_width = params->sample_width,
755 		.sample_rate = params->sample_rate,
756 		.channels = params->channels,
757 	};
758 	int ret;
759 
760 	mutex_lock(&dp->lock);
761 	if (!dp->active) {
762 		ret = -ENODEV;
763 		goto out;
764 	}
765 
766 	switch (daifmt->fmt) {
767 	case HDMI_I2S:
768 		audio.format = AFMT_I2S;
769 		break;
770 	case HDMI_SPDIF:
771 		audio.format = AFMT_SPDIF;
772 		break;
773 	default:
774 		drm_err(bridge->dev, "Invalid format %d\n", daifmt->fmt);
775 		ret = -EINVAL;
776 		goto out;
777 	}
778 
779 	ret = cdn_dp_audio_config(dp, &audio);
780 	if (!ret)
781 		dp->audio_info = audio;
782 
783 out:
784 	mutex_unlock(&dp->lock);
785 	return ret;
786 }
787 
788 static void cdn_dp_audio_shutdown(struct drm_bridge *bridge,
789 				  struct drm_connector *connector)
790 {
791 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
792 	int ret;
793 
794 	mutex_lock(&dp->lock);
795 	if (!dp->active)
796 		goto out;
797 
798 	ret = cdn_dp_audio_stop(dp, &dp->audio_info);
799 	if (!ret)
800 		dp->audio_info.format = AFMT_UNUSED;
801 out:
802 	mutex_unlock(&dp->lock);
803 }
804 
805 static int cdn_dp_audio_mute_stream(struct drm_bridge *bridge,
806 				    struct drm_connector *connector,
807 				    bool enable, int direction)
808 {
809 	struct cdn_dp_device *dp = bridge_to_dp(bridge);
810 	int ret;
811 
812 	mutex_lock(&dp->lock);
813 	if (!dp->active) {
814 		ret = -ENODEV;
815 		goto out;
816 	}
817 
818 	ret = cdn_dp_audio_mute(dp, enable);
819 
820 out:
821 	mutex_unlock(&dp->lock);
822 	return ret;
823 }
824 
825 static const struct drm_bridge_funcs cdn_dp_bridge_funcs = {
826 	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
827 	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
828 	.atomic_reset = drm_atomic_helper_bridge_reset,
829 	.detect = cdn_dp_bridge_detect,
830 	.edid_read = cdn_dp_bridge_edid_read,
831 	.atomic_enable = cdn_dp_bridge_atomic_enable,
832 	.atomic_disable = cdn_dp_bridge_atomic_disable,
833 	.mode_valid = cdn_dp_bridge_mode_valid,
834 	.mode_set = cdn_dp_bridge_mode_set,
835 
836 	.dp_audio_prepare = cdn_dp_audio_prepare,
837 	.dp_audio_mute_stream = cdn_dp_audio_mute_stream,
838 	.dp_audio_shutdown = cdn_dp_audio_shutdown,
839 };
840 
841 static int cdn_dp_request_firmware(struct cdn_dp_device *dp)
842 {
843 	int ret;
844 	unsigned long timeout = jiffies + msecs_to_jiffies(CDN_FW_TIMEOUT_MS);
845 	unsigned long sleep = 1000;
846 
847 	WARN_ON(!mutex_is_locked(&dp->lock));
848 
849 	if (dp->fw_loaded)
850 		return 0;
851 
852 	/* Drop the lock before getting the firmware to avoid blocking boot */
853 	mutex_unlock(&dp->lock);
854 
855 	while (time_before(jiffies, timeout)) {
856 		ret = request_firmware(&dp->fw, CDN_DP_FIRMWARE, dp->dev);
857 		if (ret == -ENOENT) {
858 			msleep(sleep);
859 			sleep *= 2;
860 			continue;
861 		} else if (ret) {
862 			DRM_DEV_ERROR(dp->dev,
863 				      "failed to request firmware: %d\n", ret);
864 			goto out;
865 		}
866 
867 		dp->fw_loaded = true;
868 		ret = 0;
869 		goto out;
870 	}
871 
872 	DRM_DEV_ERROR(dp->dev, "Timed out trying to load firmware\n");
873 	ret = -ETIMEDOUT;
874 out:
875 	mutex_lock(&dp->lock);
876 	return ret;
877 }
878 
879 static void cdn_dp_pd_event_work(struct work_struct *work)
880 {
881 	struct cdn_dp_device *dp = container_of(work, struct cdn_dp_device,
882 						event_work);
883 	int ret;
884 
885 	mutex_lock(&dp->lock);
886 
887 	if (dp->suspended)
888 		goto out;
889 
890 	ret = cdn_dp_request_firmware(dp);
891 	if (ret)
892 		goto out;
893 
894 	dp->connected = true;
895 
896 	/* Not connected, notify userspace to disable the block */
897 	if (!cdn_dp_connected_port(dp)) {
898 		DRM_DEV_INFO(dp->dev, "Not connected; disabling cdn\n");
899 		dp->connected = false;
900 
901 	/* Connected but not enabled, enable the block */
902 	} else if (!dp->active) {
903 		DRM_DEV_INFO(dp->dev, "Connected, not enabled; enabling cdn\n");
904 		ret = cdn_dp_enable(dp);
905 		if (ret) {
906 			DRM_DEV_ERROR(dp->dev, "Enabling dp failed: %d\n", ret);
907 			dp->connected = false;
908 		}
909 
910 	/* Enabled and connected to a dongle without a sink, notify userspace */
911 	} else if (!cdn_dp_check_sink_connection(dp)) {
912 		DRM_DEV_INFO(dp->dev, "Connected without sink; assert hpd\n");
913 		dp->connected = false;
914 
915 	/* Enabled and connected with a sink, re-train if requested */
916 	} else if (!cdn_dp_check_link_status(dp)) {
917 		unsigned int rate = dp->max_rate;
918 		unsigned int lanes = dp->max_lanes;
919 		struct drm_display_mode *mode = &dp->mode;
920 
921 		DRM_DEV_INFO(dp->dev, "Connected with sink; re-train link\n");
922 		ret = cdn_dp_train_link(dp);
923 		if (ret) {
924 			dp->connected = false;
925 			DRM_DEV_ERROR(dp->dev, "Training link failed: %d\n", ret);
926 			goto out;
927 		}
928 
929 		/* If training result is changed, update the video config */
930 		if (mode->clock &&
931 		    (rate != dp->max_rate || lanes != dp->max_lanes)) {
932 			ret = cdn_dp_config_video(dp);
933 			if (ret) {
934 				dp->connected = false;
935 				DRM_DEV_ERROR(dp->dev, "Failed to configure video: %d\n", ret);
936 			}
937 		}
938 	}
939 
940 out:
941 	mutex_unlock(&dp->lock);
942 	drm_bridge_hpd_notify(&dp->bridge,
943 			      dp->connected ? connector_status_connected
944 					    : connector_status_disconnected);
945 }
946 
947 static int cdn_dp_pd_event(struct notifier_block *nb,
948 			   unsigned long event, void *priv)
949 {
950 	struct cdn_dp_port *port = container_of(nb, struct cdn_dp_port,
951 						event_nb);
952 	struct cdn_dp_device *dp = port->dp;
953 
954 	/*
955 	 * It would be nice to be able to just do the work inline right here.
956 	 * However, we need to make a bunch of calls that might sleep in order
957 	 * to turn on the block/phy, so use a worker instead.
958 	 */
959 	schedule_work(&dp->event_work);
960 
961 	return NOTIFY_DONE;
962 }
963 
964 static int cdn_dp_bind(struct device *dev, struct device *master, void *data)
965 {
966 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
967 	struct drm_encoder *encoder;
968 	struct drm_connector *connector;
969 	struct cdn_dp_port *port;
970 	struct drm_device *drm_dev = data;
971 	int ret, i;
972 
973 	ret = cdn_dp_parse_dt(dp);
974 	if (ret < 0)
975 		return ret;
976 
977 	dp->drm_dev = drm_dev;
978 	dp->connected = false;
979 	dp->active = false;
980 	dp->active_port = -1;
981 	dp->fw_loaded = false;
982 
983 	INIT_WORK(&dp->event_work, cdn_dp_pd_event_work);
984 
985 	encoder = &dp->encoder.encoder;
986 
987 	encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
988 							     dev->of_node);
989 	DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
990 
991 	ret = drm_simple_encoder_init(drm_dev, encoder,
992 				      DRM_MODE_ENCODER_TMDS);
993 	if (ret) {
994 		DRM_ERROR("failed to initialize encoder with drm\n");
995 		return ret;
996 	}
997 
998 	drm_encoder_helper_add(encoder, &cdn_dp_encoder_helper_funcs);
999 
1000 	dp->bridge.ops =
1001 			DRM_BRIDGE_OP_DETECT |
1002 			DRM_BRIDGE_OP_EDID |
1003 			DRM_BRIDGE_OP_HPD |
1004 			DRM_BRIDGE_OP_DP_AUDIO;
1005 	dp->bridge.of_node = dp->dev->of_node;
1006 	dp->bridge.type = DRM_MODE_CONNECTOR_DisplayPort;
1007 	dp->bridge.hdmi_audio_dev = dp->dev;
1008 	dp->bridge.hdmi_audio_max_i2s_playback_channels = 8;
1009 	dp->bridge.hdmi_audio_spdif_playback = 1;
1010 	dp->bridge.hdmi_audio_dai_port = -1;
1011 
1012 	ret = devm_drm_bridge_add(dev, &dp->bridge);
1013 	if (ret)
1014 		return ret;
1015 
1016 	ret = drm_bridge_attach(encoder, &dp->bridge, NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
1017 	if (ret)
1018 		return ret;
1019 
1020 	connector = drm_bridge_connector_init(drm_dev, encoder);
1021 	if (IS_ERR(connector)) {
1022 		ret = PTR_ERR(connector);
1023 		dev_err(dp->dev, "failed to init bridge connector: %d\n", ret);
1024 		return ret;
1025 	}
1026 
1027 	drm_connector_attach_encoder(connector, encoder);
1028 
1029 	for (i = 0; i < dp->ports; i++) {
1030 		port = dp->port[i];
1031 
1032 		port->event_nb.notifier_call = cdn_dp_pd_event;
1033 		ret = devm_extcon_register_notifier(dp->dev, port->extcon,
1034 						    EXTCON_DISP_DP,
1035 						    &port->event_nb);
1036 		if (ret) {
1037 			DRM_DEV_ERROR(dev,
1038 				      "register EXTCON_DISP_DP notifier err\n");
1039 			return ret;
1040 		}
1041 	}
1042 
1043 	pm_runtime_enable(dev);
1044 
1045 	schedule_work(&dp->event_work);
1046 
1047 	return 0;
1048 }
1049 
1050 static void cdn_dp_unbind(struct device *dev, struct device *master, void *data)
1051 {
1052 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1053 	struct drm_encoder *encoder = &dp->encoder.encoder;
1054 
1055 	cancel_work_sync(&dp->event_work);
1056 	encoder->funcs->destroy(encoder);
1057 
1058 	pm_runtime_disable(dev);
1059 	if (dp->fw_loaded)
1060 		release_firmware(dp->fw);
1061 }
1062 
1063 static const struct component_ops cdn_dp_component_ops = {
1064 	.bind = cdn_dp_bind,
1065 	.unbind = cdn_dp_unbind,
1066 };
1067 
1068 static int cdn_dp_suspend(struct device *dev)
1069 {
1070 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1071 	int ret = 0;
1072 
1073 	mutex_lock(&dp->lock);
1074 	if (dp->active)
1075 		ret = cdn_dp_disable(dp);
1076 	dp->suspended = true;
1077 	mutex_unlock(&dp->lock);
1078 
1079 	return ret;
1080 }
1081 
1082 static __maybe_unused int cdn_dp_resume(struct device *dev)
1083 {
1084 	struct cdn_dp_device *dp = dev_get_drvdata(dev);
1085 
1086 	mutex_lock(&dp->lock);
1087 	dp->suspended = false;
1088 	if (dp->fw_loaded)
1089 		schedule_work(&dp->event_work);
1090 	mutex_unlock(&dp->lock);
1091 
1092 	return 0;
1093 }
1094 
1095 static int cdn_dp_probe(struct platform_device *pdev)
1096 {
1097 	struct device *dev = &pdev->dev;
1098 	const struct of_device_id *match;
1099 	struct cdn_dp_data *dp_data;
1100 	struct cdn_dp_port *port;
1101 	struct cdn_dp_device *dp;
1102 	struct extcon_dev *extcon;
1103 	struct phy *phy;
1104 	int ret;
1105 	int i;
1106 
1107 	dp = devm_drm_bridge_alloc(dev, struct cdn_dp_device, bridge,
1108 				   &cdn_dp_bridge_funcs);
1109 	if (IS_ERR(dp))
1110 		return PTR_ERR(dp);
1111 	dp->dev = dev;
1112 
1113 	match = of_match_node(cdn_dp_dt_ids, pdev->dev.of_node);
1114 	dp_data = (struct cdn_dp_data *)match->data;
1115 
1116 	for (i = 0; i < dp_data->max_phy; i++) {
1117 		extcon = extcon_get_edev_by_phandle(dev, i);
1118 		phy = devm_of_phy_get_by_index(dev, dev->of_node, i);
1119 
1120 		if (PTR_ERR(extcon) == -EPROBE_DEFER ||
1121 		    PTR_ERR(phy) == -EPROBE_DEFER)
1122 			return -EPROBE_DEFER;
1123 
1124 		if (IS_ERR(extcon) || IS_ERR(phy))
1125 			continue;
1126 
1127 		port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
1128 		if (!port)
1129 			return -ENOMEM;
1130 
1131 		port->extcon = extcon;
1132 		port->phy = phy;
1133 		port->dp = dp;
1134 		port->id = i;
1135 		dp->port[dp->ports++] = port;
1136 	}
1137 
1138 	if (!dp->ports) {
1139 		DRM_DEV_ERROR(dev, "missing extcon or phy\n");
1140 		return -EINVAL;
1141 	}
1142 
1143 	mutex_init(&dp->lock);
1144 	dev_set_drvdata(dev, dp);
1145 
1146 	ret = component_add(dev, &cdn_dp_component_ops);
1147 	if (ret)
1148 		return ret;
1149 
1150 	return 0;
1151 }
1152 
1153 static void cdn_dp_remove(struct platform_device *pdev)
1154 {
1155 	struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1156 
1157 	platform_device_unregister(dp->audio_pdev);
1158 	cdn_dp_suspend(dp->dev);
1159 	component_del(&pdev->dev, &cdn_dp_component_ops);
1160 }
1161 
1162 static void cdn_dp_shutdown(struct platform_device *pdev)
1163 {
1164 	struct cdn_dp_device *dp = platform_get_drvdata(pdev);
1165 
1166 	cdn_dp_suspend(dp->dev);
1167 }
1168 
1169 static const struct dev_pm_ops cdn_dp_pm_ops = {
1170 	SET_SYSTEM_SLEEP_PM_OPS(cdn_dp_suspend,
1171 				cdn_dp_resume)
1172 };
1173 
1174 struct platform_driver cdn_dp_driver = {
1175 	.probe = cdn_dp_probe,
1176 	.remove = cdn_dp_remove,
1177 	.shutdown = cdn_dp_shutdown,
1178 	.driver = {
1179 		   .name = "cdn-dp",
1180 		   .of_match_table = cdn_dp_dt_ids,
1181 		   .pm = &cdn_dp_pm_ops,
1182 	},
1183 };
1184