Linux Audio

Check our new training course

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