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