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