Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
   4 */
   5
   6#include <linux/module.h>
   7#include <linux/slab.h>
   8#include <linux/uaccess.h>
   9#include <linux/debugfs.h>
  10#include <linux/component.h>
  11#include <linux/of_irq.h>
  12#include <linux/delay.h>
  13
  14#include "msm_drv.h"
  15#include "msm_kms.h"
  16#include "dp_hpd.h"
  17#include "dp_parser.h"
  18#include "dp_power.h"
  19#include "dp_catalog.h"
  20#include "dp_aux.h"
  21#include "dp_reg.h"
  22#include "dp_link.h"
  23#include "dp_panel.h"
  24#include "dp_ctrl.h"
  25#include "dp_display.h"
  26#include "dp_drm.h"
  27#include "dp_audio.h"
  28#include "dp_debug.h"
  29
  30static struct msm_dp *g_dp_display;
  31#define HPD_STRING_SIZE 30
  32
  33enum {
  34	ISR_DISCONNECTED,
  35	ISR_CONNECT_PENDING,
  36	ISR_CONNECTED,
  37	ISR_HPD_REPLUG_COUNT,
  38	ISR_IRQ_HPD_PULSE_COUNT,
  39	ISR_HPD_LO_GLITH_COUNT,
  40};
  41
  42/* event thread connection state */
  43enum {
  44	ST_DISCONNECTED,
  45	ST_CONNECT_PENDING,
  46	ST_CONNECTED,
  47	ST_DISCONNECT_PENDING,
  48	ST_DISPLAY_OFF,
  49	ST_SUSPENDED,
  50};
  51
  52enum {
  53	EV_NO_EVENT,
  54	/* hpd events */
  55	EV_HPD_INIT_SETUP,
  56	EV_HPD_PLUG_INT,
  57	EV_IRQ_HPD_INT,
  58	EV_HPD_UNPLUG_INT,
  59	EV_USER_NOTIFICATION,
  60	EV_CONNECT_PENDING_TIMEOUT,
  61	EV_DISCONNECT_PENDING_TIMEOUT,
  62};
  63
  64#define EVENT_TIMEOUT	(HZ/10)	/* 100ms */
  65#define DP_EVENT_Q_MAX	8
  66
  67#define DP_TIMEOUT_5_SECOND	(5000/EVENT_TIMEOUT)
  68#define DP_TIMEOUT_NONE		0
  69
  70#define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
  71
  72struct dp_event {
  73	u32 event_id;
  74	u32 data;
  75	u32 delay;
  76};
  77
  78struct dp_display_private {
  79	char *name;
  80	int irq;
  81
  82	/* state variables */
  83	bool core_initialized;
  84	bool hpd_irq_on;
  85	bool audio_supported;
  86
  87	struct platform_device *pdev;
  88	struct dentry *root;
  89
  90	struct dp_usbpd   *usbpd;
  91	struct dp_parser  *parser;
  92	struct dp_power   *power;
  93	struct dp_catalog *catalog;
  94	struct drm_dp_aux *aux;
  95	struct dp_link    *link;
  96	struct dp_panel   *panel;
  97	struct dp_ctrl    *ctrl;
  98	struct dp_debug   *debug;
  99
 100	struct dp_usbpd_cb usbpd_cb;
 101	struct dp_display_mode dp_mode;
 102	struct msm_dp dp_display;
 103
 104	bool encoder_mode_set;
 105
 106	/* wait for audio signaling */
 107	struct completion audio_comp;
 108
 109	/* event related only access by event thread */
 110	struct mutex event_mutex;
 111	wait_queue_head_t event_q;
 112	u32 hpd_state;
 113	u32 event_pndx;
 114	u32 event_gndx;
 115	struct dp_event event_list[DP_EVENT_Q_MAX];
 116	spinlock_t event_lock;
 117
 118	struct dp_audio *audio;
 119};
 120
 121static const struct of_device_id dp_dt_match[] = {
 122	{.compatible = "qcom,sc7180-dp"},
 123	{}
 124};
 125
 126static int dp_add_event(struct dp_display_private *dp_priv, u32 event,
 127						u32 data, u32 delay)
 128{
 129	unsigned long flag;
 130	struct dp_event *todo;
 131	int pndx;
 132
 133	spin_lock_irqsave(&dp_priv->event_lock, flag);
 134	pndx = dp_priv->event_pndx + 1;
 135	pndx %= DP_EVENT_Q_MAX;
 136	if (pndx == dp_priv->event_gndx) {
 137		pr_err("event_q is full: pndx=%d gndx=%d\n",
 138			dp_priv->event_pndx, dp_priv->event_gndx);
 139		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
 140		return -EPERM;
 141	}
 142	todo = &dp_priv->event_list[dp_priv->event_pndx++];
 143	dp_priv->event_pndx %= DP_EVENT_Q_MAX;
 144	todo->event_id = event;
 145	todo->data = data;
 146	todo->delay = delay;
 147	wake_up(&dp_priv->event_q);
 148	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
 149
 150	return 0;
 151}
 152
 153static int dp_del_event(struct dp_display_private *dp_priv, u32 event)
 154{
 155	unsigned long flag;
 156	struct dp_event *todo;
 157	u32	gndx;
 158
 159	spin_lock_irqsave(&dp_priv->event_lock, flag);
 160	if (dp_priv->event_pndx == dp_priv->event_gndx) {
 161		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
 162		return -ENOENT;
 163	}
 164
 165	gndx = dp_priv->event_gndx;
 166	while (dp_priv->event_pndx != gndx) {
 167		todo = &dp_priv->event_list[gndx];
 168		if (todo->event_id == event) {
 169			todo->event_id = EV_NO_EVENT;	/* deleted */
 170			todo->delay = 0;
 171		}
 172		gndx++;
 173		gndx %= DP_EVENT_Q_MAX;
 174	}
 175	spin_unlock_irqrestore(&dp_priv->event_lock, flag);
 176
 177	return 0;
 178}
 179
 180void dp_display_signal_audio_start(struct msm_dp *dp_display)
 181{
 182	struct dp_display_private *dp;
 183
 184	dp = container_of(dp_display, struct dp_display_private, dp_display);
 185
 186	reinit_completion(&dp->audio_comp);
 187}
 188
 189void dp_display_signal_audio_complete(struct msm_dp *dp_display)
 190{
 191	struct dp_display_private *dp;
 192
 193	dp = container_of(dp_display, struct dp_display_private, dp_display);
 194
 195	complete_all(&dp->audio_comp);
 196}
 197
 198static int dp_display_bind(struct device *dev, struct device *master,
 199			   void *data)
 200{
 201	int rc = 0;
 202	struct dp_display_private *dp;
 203	struct drm_device *drm;
 204	struct msm_drm_private *priv;
 205
 206	drm = dev_get_drvdata(master);
 207
 208	dp = container_of(g_dp_display,
 209			struct dp_display_private, dp_display);
 210
 211	dp->dp_display.drm_dev = drm;
 212	priv = drm->dev_private;
 213	priv->dp = &(dp->dp_display);
 214
 215	rc = dp->parser->parse(dp->parser);
 216	if (rc) {
 217		DRM_ERROR("device tree parsing failed\n");
 218		goto end;
 219	}
 220
 221	dp->aux->drm_dev = drm;
 222	rc = dp_aux_register(dp->aux);
 223	if (rc) {
 224		DRM_ERROR("DRM DP AUX register failed\n");
 225		goto end;
 226	}
 227
 228	rc = dp_power_client_init(dp->power);
 229	if (rc) {
 230		DRM_ERROR("Power client create failed\n");
 231		goto end;
 232	}
 233
 234	rc = dp_register_audio_driver(dev, dp->audio);
 235	if (rc)
 236		DRM_ERROR("Audio registration Dp failed\n");
 237
 238end:
 239	return rc;
 240}
 241
 242static void dp_display_unbind(struct device *dev, struct device *master,
 243			      void *data)
 244{
 245	struct dp_display_private *dp;
 246	struct drm_device *drm = dev_get_drvdata(master);
 247	struct msm_drm_private *priv = drm->dev_private;
 248
 249	dp = container_of(g_dp_display,
 250			struct dp_display_private, dp_display);
 251
 252	dp_power_client_deinit(dp->power);
 253	dp_aux_unregister(dp->aux);
 254	priv->dp = NULL;
 255}
 256
 257static const struct component_ops dp_display_comp_ops = {
 258	.bind = dp_display_bind,
 259	.unbind = dp_display_unbind,
 260};
 261
 262static bool dp_display_is_ds_bridge(struct dp_panel *panel)
 263{
 264	return (panel->dpcd[DP_DOWNSTREAMPORT_PRESENT] &
 265		DP_DWN_STRM_PORT_PRESENT);
 266}
 267
 268static bool dp_display_is_sink_count_zero(struct dp_display_private *dp)
 269{
 270	return dp_display_is_ds_bridge(dp->panel) &&
 271		(dp->link->sink_count == 0);
 272}
 273
 274static void dp_display_send_hpd_event(struct msm_dp *dp_display)
 275{
 276	struct dp_display_private *dp;
 277	struct drm_connector *connector;
 278
 279	dp = container_of(dp_display, struct dp_display_private, dp_display);
 280
 281	connector = dp->dp_display.connector;
 282	drm_helper_hpd_irq_event(connector->dev);
 283}
 284
 285
 286static void dp_display_set_encoder_mode(struct dp_display_private *dp)
 287{
 288	struct msm_drm_private *priv = dp->dp_display.drm_dev->dev_private;
 289	struct msm_kms *kms = priv->kms;
 290
 291	if (!dp->encoder_mode_set && dp->dp_display.encoder &&
 292				kms->funcs->set_encoder_mode) {
 293		kms->funcs->set_encoder_mode(kms,
 294				dp->dp_display.encoder, false);
 295
 296		dp->encoder_mode_set = true;
 297	}
 298}
 299
 300static int dp_display_send_hpd_notification(struct dp_display_private *dp,
 301					    bool hpd)
 302{
 303	if ((hpd && dp->dp_display.is_connected) ||
 304			(!hpd && !dp->dp_display.is_connected)) {
 305		DRM_DEBUG_DP("HPD already %s\n", (hpd ? "on" : "off"));
 306		return 0;
 307	}
 308
 309	/* reset video pattern flag on disconnect */
 310	if (!hpd)
 311		dp->panel->video_test = false;
 312
 313	dp->dp_display.is_connected = hpd;
 314
 315	dp_display_send_hpd_event(&dp->dp_display);
 316
 317	return 0;
 318}
 319
 320static int dp_display_process_hpd_high(struct dp_display_private *dp)
 321{
 322	int rc = 0;
 323	struct edid *edid;
 324
 325	dp->panel->max_dp_lanes = dp->parser->max_dp_lanes;
 326
 327	rc = dp_panel_read_sink_caps(dp->panel, dp->dp_display.connector);
 328	if (rc)
 329		goto end;
 330
 331	dp_link_process_request(dp->link);
 332
 333	edid = dp->panel->edid;
 334
 335	dp->audio_supported = drm_detect_monitor_audio(edid);
 336	dp_panel_handle_sink_request(dp->panel);
 337
 338	dp->dp_display.max_pclk_khz = DP_MAX_PIXEL_CLK_KHZ;
 339	dp->dp_display.max_dp_lanes = dp->parser->max_dp_lanes;
 340
 341	/*
 342	 * set sink to normal operation mode -- D0
 343	 * before dpcd read
 344	 */
 345	dp_link_psm_config(dp->link, &dp->panel->link_info, false);
 346
 347	dp_link_reset_phy_params_vx_px(dp->link);
 348	rc = dp_ctrl_on_link(dp->ctrl);
 349	if (rc) {
 350		DRM_ERROR("failed to complete DP link training\n");
 351		goto end;
 352	}
 353
 354	dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
 355
 356end:
 357	return rc;
 358}
 359
 360static void dp_display_host_init(struct dp_display_private *dp, int reset)
 361{
 362	bool flip = false;
 363
 364	if (dp->core_initialized) {
 365		DRM_DEBUG_DP("DP core already initialized\n");
 366		return;
 367	}
 368
 369	if (dp->usbpd->orientation == ORIENTATION_CC2)
 370		flip = true;
 371
 372	dp_display_set_encoder_mode(dp);
 373
 374	dp_power_init(dp->power, flip);
 375	dp_ctrl_host_init(dp->ctrl, flip, reset);
 376	dp_aux_init(dp->aux);
 377	dp->core_initialized = true;
 378}
 379
 380static void dp_display_host_deinit(struct dp_display_private *dp)
 381{
 382	if (!dp->core_initialized) {
 383		DRM_DEBUG_DP("DP core not initialized\n");
 384		return;
 385	}
 386
 387	dp_ctrl_host_deinit(dp->ctrl);
 388	dp_aux_deinit(dp->aux);
 389	dp_power_deinit(dp->power);
 390
 391	dp->core_initialized = false;
 392}
 393
 394static int dp_display_usbpd_configure_cb(struct device *dev)
 395{
 396	int rc = 0;
 397	struct dp_display_private *dp;
 398
 399	if (!dev) {
 400		DRM_ERROR("invalid dev\n");
 401		rc = -EINVAL;
 402		goto end;
 403	}
 404
 405	dp = container_of(g_dp_display,
 406			struct dp_display_private, dp_display);
 407
 408	dp_display_host_init(dp, false);
 409
 410	rc = dp_display_process_hpd_high(dp);
 411end:
 412	return rc;
 413}
 414
 415static int dp_display_usbpd_disconnect_cb(struct device *dev)
 416{
 417	int rc = 0;
 418	struct dp_display_private *dp;
 419
 420	if (!dev) {
 421		DRM_ERROR("invalid dev\n");
 422		rc = -EINVAL;
 423		return rc;
 424	}
 425
 426	dp = container_of(g_dp_display,
 427			struct dp_display_private, dp_display);
 428
 429	dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
 430
 431	return rc;
 432}
 433
 434static void dp_display_handle_video_request(struct dp_display_private *dp)
 435{
 436	if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
 437		dp->panel->video_test = true;
 438		dp_link_send_test_response(dp->link);
 439	}
 440}
 441
 442static int dp_display_handle_port_ststus_changed(struct dp_display_private *dp)
 443{
 444	int rc = 0;
 445
 446	if (dp_display_is_sink_count_zero(dp)) {
 447		DRM_DEBUG_DP("sink count is zero, nothing to do\n");
 448		if (dp->hpd_state != ST_DISCONNECTED) {
 449			dp->hpd_state = ST_DISCONNECT_PENDING;
 450			dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
 451		}
 452	} else {
 453		if (dp->hpd_state == ST_DISCONNECTED) {
 454			dp->hpd_state = ST_CONNECT_PENDING;
 455			rc = dp_display_process_hpd_high(dp);
 456			if (rc)
 457				dp->hpd_state = ST_DISCONNECTED;
 458		}
 459	}
 460
 461	return rc;
 462}
 463
 464static int dp_display_handle_irq_hpd(struct dp_display_private *dp)
 465{
 466	u32 sink_request = dp->link->sink_request;
 467
 468	if (dp->hpd_state == ST_DISCONNECTED) {
 469		if (sink_request & DP_LINK_STATUS_UPDATED) {
 470			DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
 471			return -EINVAL;
 472		}
 473	}
 474
 475	dp_ctrl_handle_sink_request(dp->ctrl);
 476
 477	if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
 478		dp_display_handle_video_request(dp);
 479
 480	return 0;
 481}
 482
 483static int dp_display_usbpd_attention_cb(struct device *dev)
 484{
 485	int rc = 0;
 486	u32 sink_request;
 487	struct dp_display_private *dp;
 488
 489	if (!dev) {
 490		DRM_ERROR("invalid dev\n");
 491		return -EINVAL;
 492	}
 493
 494	dp = container_of(g_dp_display,
 495			struct dp_display_private, dp_display);
 496
 497	/* check for any test request issued by sink */
 498	rc = dp_link_process_request(dp->link);
 499	if (!rc) {
 500		sink_request = dp->link->sink_request;
 501		if (sink_request & DS_PORT_STATUS_CHANGED)
 502			rc = dp_display_handle_port_ststus_changed(dp);
 503		else
 504			rc = dp_display_handle_irq_hpd(dp);
 505	}
 506
 507	return rc;
 508}
 509
 510static int dp_hpd_plug_handle(struct dp_display_private *dp, u32 data)
 511{
 512	struct dp_usbpd *hpd = dp->usbpd;
 513	u32 state;
 514	u32 tout = DP_TIMEOUT_5_SECOND;
 515	int ret;
 516
 517	if (!hpd)
 518		return 0;
 519
 520	mutex_lock(&dp->event_mutex);
 521
 522	state =  dp->hpd_state;
 523	if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
 524		mutex_unlock(&dp->event_mutex);
 525		return 0;
 526	}
 527
 528	if (state == ST_CONNECT_PENDING || state == ST_CONNECTED) {
 529		mutex_unlock(&dp->event_mutex);
 530		return 0;
 531	}
 532
 533	if (state == ST_DISCONNECT_PENDING) {
 534		/* wait until ST_DISCONNECTED */
 535		dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
 536		mutex_unlock(&dp->event_mutex);
 537		return 0;
 538	}
 539
 540	dp->hpd_state = ST_CONNECT_PENDING;
 541
 542	hpd->hpd_high = 1;
 543
 544	ret = dp_display_usbpd_configure_cb(&dp->pdev->dev);
 545	if (ret) {	/* link train failed */
 546		hpd->hpd_high = 0;
 547		dp->hpd_state = ST_DISCONNECTED;
 548
 549		if (ret == -ECONNRESET) { /* cable unplugged */
 550			dp->core_initialized = false;
 551		}
 552
 553	} else {
 554		/* start sentinel checking in case of missing uevent */
 555		dp_add_event(dp, EV_CONNECT_PENDING_TIMEOUT, 0, tout);
 556	}
 557
 558	/* enable HDP irq_hpd/replug interrupt */
 559	dp_catalog_hpd_config_intr(dp->catalog,
 560		DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, true);
 561
 562	mutex_unlock(&dp->event_mutex);
 563
 564	/* uevent will complete connection part */
 565	return 0;
 566};
 567
 568static int dp_display_enable(struct dp_display_private *dp, u32 data);
 569static int dp_display_disable(struct dp_display_private *dp, u32 data);
 570
 571static int dp_connect_pending_timeout(struct dp_display_private *dp, u32 data)
 572{
 573	u32 state;
 574
 575	mutex_lock(&dp->event_mutex);
 576
 577	state = dp->hpd_state;
 578	if (state == ST_CONNECT_PENDING)
 579		dp->hpd_state = ST_CONNECTED;
 580
 581	mutex_unlock(&dp->event_mutex);
 582
 583	return 0;
 584}
 585
 586static void dp_display_handle_plugged_change(struct msm_dp *dp_display,
 587		bool plugged)
 588{
 589	struct dp_display_private *dp;
 590
 591	dp = container_of(dp_display,
 592			struct dp_display_private, dp_display);
 593
 594	/* notify audio subsystem only if sink supports audio */
 595	if (dp_display->plugged_cb && dp_display->codec_dev &&
 596			dp->audio_supported)
 597		dp_display->plugged_cb(dp_display->codec_dev, plugged);
 598}
 599
 600static int dp_hpd_unplug_handle(struct dp_display_private *dp, u32 data)
 601{
 602	struct dp_usbpd *hpd = dp->usbpd;
 603	u32 state;
 604
 605	if (!hpd)
 606		return 0;
 607
 608	mutex_lock(&dp->event_mutex);
 609
 610	state = dp->hpd_state;
 611
 612	/* disable irq_hpd/replug interrupts */
 613	dp_catalog_hpd_config_intr(dp->catalog,
 614		DP_DP_IRQ_HPD_INT_MASK | DP_DP_HPD_REPLUG_INT_MASK, false);
 615
 616	/* unplugged, no more irq_hpd handle */
 617	dp_del_event(dp, EV_IRQ_HPD_INT);
 618
 619	if (state == ST_DISCONNECTED) {
 620		/* triggered by irq_hdp with sink_count = 0 */
 621		if (dp->link->sink_count == 0) {
 622			dp_ctrl_off_phy(dp->ctrl);
 623			hpd->hpd_high = 0;
 624			dp->core_initialized = false;
 625		}
 626		mutex_unlock(&dp->event_mutex);
 627		return 0;
 628	}
 629
 630	if (state == ST_DISCONNECT_PENDING) {
 631		mutex_unlock(&dp->event_mutex);
 632		return 0;
 633	}
 634
 635	if (state == ST_CONNECT_PENDING) {
 636		/* wait until CONNECTED */
 637		dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 1); /* delay = 1 */
 638		mutex_unlock(&dp->event_mutex);
 639		return 0;
 640	}
 641
 642	dp->hpd_state = ST_DISCONNECT_PENDING;
 643
 644	/* disable HPD plug interrupts */
 645	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, false);
 646
 647	hpd->hpd_high = 0;
 648
 649	/*
 650	 * We don't need separate work for disconnect as
 651	 * connect/attention interrupts are disabled
 652	 */
 653	dp_display_usbpd_disconnect_cb(&dp->pdev->dev);
 654
 655	/* start sentinel checking in case of missing uevent */
 656	dp_add_event(dp, EV_DISCONNECT_PENDING_TIMEOUT, 0, DP_TIMEOUT_5_SECOND);
 657
 658	/* signal the disconnect event early to ensure proper teardown */
 659	dp_display_handle_plugged_change(g_dp_display, false);
 660
 661	/* enable HDP plug interrupt to prepare for next plugin */
 662	dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_PLUG_INT_MASK, true);
 663
 664	/* uevent will complete disconnection part */
 665	mutex_unlock(&dp->event_mutex);
 666	return 0;
 667}
 668
 669static int dp_disconnect_pending_timeout(struct dp_display_private *dp, u32 data)
 670{
 671	u32 state;
 672
 673	mutex_lock(&dp->event_mutex);
 674
 675	state =  dp->hpd_state;
 676	if (state == ST_DISCONNECT_PENDING)
 677		dp->hpd_state = ST_DISCONNECTED;
 678
 679	mutex_unlock(&dp->event_mutex);
 680
 681	return 0;
 682}
 683
 684static int dp_irq_hpd_handle(struct dp_display_private *dp, u32 data)
 685{
 686	u32 state;
 687	int ret;
 688
 689	mutex_lock(&dp->event_mutex);
 690
 691	/* irq_hpd can happen at either connected or disconnected state */
 692	state =  dp->hpd_state;
 693	if (state == ST_DISPLAY_OFF || state == ST_SUSPENDED) {
 694		mutex_unlock(&dp->event_mutex);
 695		return 0;
 696	}
 697
 698	if (state == ST_CONNECT_PENDING) {
 699		/* wait until ST_CONNECTED */
 700		dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
 701		mutex_unlock(&dp->event_mutex);
 702		return 0;
 703	}
 704
 705	if (state == ST_CONNECT_PENDING || state == ST_DISCONNECT_PENDING) {
 706		/* wait until ST_CONNECTED */
 707		dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
 708		mutex_unlock(&dp->event_mutex);
 709		return 0;
 710	}
 711
 712	ret = dp_display_usbpd_attention_cb(&dp->pdev->dev);
 713	if (ret == -ECONNRESET) { /* cable unplugged */
 714		dp->core_initialized = false;
 715	}
 716
 717	mutex_unlock(&dp->event_mutex);
 718
 719	return 0;
 720}
 721
 722static void dp_display_deinit_sub_modules(struct dp_display_private *dp)
 723{
 724	dp_debug_put(dp->debug);
 725	dp_panel_put(dp->panel);
 726	dp_aux_put(dp->aux);
 727	dp_audio_put(dp->audio);
 728}
 729
 730static int dp_init_sub_modules(struct dp_display_private *dp)
 731{
 732	int rc = 0;
 733	struct device *dev = &dp->pdev->dev;
 734	struct dp_usbpd_cb *cb = &dp->usbpd_cb;
 735	struct dp_panel_in panel_in = {
 736		.dev = dev,
 737	};
 738
 739	/* Callback APIs used for cable status change event */
 740	cb->configure  = dp_display_usbpd_configure_cb;
 741	cb->disconnect = dp_display_usbpd_disconnect_cb;
 742	cb->attention  = dp_display_usbpd_attention_cb;
 743
 744	dp->usbpd = dp_hpd_get(dev, cb);
 745	if (IS_ERR(dp->usbpd)) {
 746		rc = PTR_ERR(dp->usbpd);
 747		DRM_ERROR("failed to initialize hpd, rc = %d\n", rc);
 748		dp->usbpd = NULL;
 749		goto error;
 750	}
 751
 752	dp->parser = dp_parser_get(dp->pdev);
 753	if (IS_ERR(dp->parser)) {
 754		rc = PTR_ERR(dp->parser);
 755		DRM_ERROR("failed to initialize parser, rc = %d\n", rc);
 756		dp->parser = NULL;
 757		goto error;
 758	}
 759
 760	dp->catalog = dp_catalog_get(dev, &dp->parser->io);
 761	if (IS_ERR(dp->catalog)) {
 762		rc = PTR_ERR(dp->catalog);
 763		DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
 764		dp->catalog = NULL;
 765		goto error;
 766	}
 767
 768	dp->power = dp_power_get(dev, dp->parser);
 769	if (IS_ERR(dp->power)) {
 770		rc = PTR_ERR(dp->power);
 771		DRM_ERROR("failed to initialize power, rc = %d\n", rc);
 772		dp->power = NULL;
 773		goto error;
 774	}
 775
 776	dp->aux = dp_aux_get(dev, dp->catalog);
 777	if (IS_ERR(dp->aux)) {
 778		rc = PTR_ERR(dp->aux);
 779		DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
 780		dp->aux = NULL;
 781		goto error;
 782	}
 783
 784	dp->link = dp_link_get(dev, dp->aux);
 785	if (IS_ERR(dp->link)) {
 786		rc = PTR_ERR(dp->link);
 787		DRM_ERROR("failed to initialize link, rc = %d\n", rc);
 788		dp->link = NULL;
 789		goto error_link;
 790	}
 791
 792	panel_in.aux = dp->aux;
 793	panel_in.catalog = dp->catalog;
 794	panel_in.link = dp->link;
 795
 796	dp->panel = dp_panel_get(&panel_in);
 797	if (IS_ERR(dp->panel)) {
 798		rc = PTR_ERR(dp->panel);
 799		DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
 800		dp->panel = NULL;
 801		goto error_link;
 802	}
 803
 804	dp->ctrl = dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
 805			       dp->power, dp->catalog, dp->parser);
 806	if (IS_ERR(dp->ctrl)) {
 807		rc = PTR_ERR(dp->ctrl);
 808		DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
 809		dp->ctrl = NULL;
 810		goto error_ctrl;
 811	}
 812
 813	dp->audio = dp_audio_get(dp->pdev, dp->panel, dp->catalog);
 814	if (IS_ERR(dp->audio)) {
 815		rc = PTR_ERR(dp->audio);
 816		pr_err("failed to initialize audio, rc = %d\n", rc);
 817		dp->audio = NULL;
 818		goto error_ctrl;
 819	}
 820
 821	return rc;
 822
 823error_ctrl:
 824	dp_panel_put(dp->panel);
 825error_link:
 826	dp_aux_put(dp->aux);
 827error:
 828	return rc;
 829}
 830
 831static int dp_display_set_mode(struct msm_dp *dp_display,
 832			       struct dp_display_mode *mode)
 833{
 834	struct dp_display_private *dp;
 835
 836	dp = container_of(dp_display, struct dp_display_private, dp_display);
 837
 838	dp->panel->dp_mode.drm_mode = mode->drm_mode;
 839	dp->panel->dp_mode.bpp = mode->bpp;
 840	dp->panel->dp_mode.capabilities = mode->capabilities;
 841	dp_panel_init_panel_info(dp->panel);
 842	return 0;
 843}
 844
 845static int dp_display_prepare(struct msm_dp *dp)
 846{
 847	return 0;
 848}
 849
 850static int dp_display_enable(struct dp_display_private *dp, u32 data)
 851{
 852	int rc = 0;
 853	struct msm_dp *dp_display;
 854
 855	dp_display = g_dp_display;
 856
 857	if (dp_display->power_on) {
 858		DRM_DEBUG_DP("Link already setup, return\n");
 859		return 0;
 860	}
 861
 862	rc = dp_ctrl_on_stream(dp->ctrl);
 863	if (!rc)
 864		dp_display->power_on = true;
 865
 866	return rc;
 867}
 868
 869static int dp_display_post_enable(struct msm_dp *dp_display)
 870{
 871	struct dp_display_private *dp;
 872	u32 rate;
 873
 874	dp = container_of(dp_display, struct dp_display_private, dp_display);
 875
 876	rate = dp->link->link_params.rate;
 877
 878	if (dp->audio_supported) {
 879		dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
 880		dp->audio->lane_count = dp->link->link_params.num_lanes;
 881	}
 882
 883	/* signal the connect event late to synchronize video and display */
 884	dp_display_handle_plugged_change(dp_display, true);
 885	return 0;
 886}
 887
 888static int dp_display_disable(struct dp_display_private *dp, u32 data)
 889{
 890	struct msm_dp *dp_display;
 891
 892	dp_display = g_dp_display;
 893
 894	if (!dp_display->power_on)
 895		return 0;
 896
 897	/* wait only if audio was enabled */
 898	if (dp_display->audio_enabled) {
 899		/* signal the disconnect event */
 900		dp_display_handle_plugged_change(dp_display, false);
 901		if (!wait_for_completion_timeout(&dp->audio_comp,
 902				HZ * 5))
 903			DRM_ERROR("audio comp timeout\n");
 904	}
 905
 906	dp_display->audio_enabled = false;
 907
 908	/* triggered by irq_hpd with sink_count = 0 */
 909	if (dp->link->sink_count == 0) {
 910		dp_ctrl_off_link_stream(dp->ctrl);
 911	} else {
 912		dp_ctrl_off(dp->ctrl);
 913		dp->core_initialized = false;
 914	}
 915
 916	dp_display->power_on = false;
 917
 918	return 0;
 919}
 920
 921static int dp_display_unprepare(struct msm_dp *dp)
 922{
 923	return 0;
 924}
 925
 926int dp_display_set_plugged_cb(struct msm_dp *dp_display,
 927		hdmi_codec_plugged_cb fn, struct device *codec_dev)
 928{
 929	bool plugged;
 930
 931	dp_display->plugged_cb = fn;
 932	dp_display->codec_dev = codec_dev;
 933	plugged = dp_display->is_connected;
 934	dp_display_handle_plugged_change(dp_display, plugged);
 935
 936	return 0;
 937}
 938
 939int dp_display_validate_mode(struct msm_dp *dp, u32 mode_pclk_khz)
 940{
 941	const u32 num_components = 3, default_bpp = 24;
 942	struct dp_display_private *dp_display;
 943	struct dp_link_info *link_info;
 944	u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
 945
 946	if (!dp || !mode_pclk_khz || !dp->connector) {
 947		DRM_ERROR("invalid params\n");
 948		return -EINVAL;
 949	}
 950
 951	dp_display = container_of(dp, struct dp_display_private, dp_display);
 952	link_info = &dp_display->panel->link_info;
 953
 954	mode_bpp = dp->connector->display_info.bpc * num_components;
 955	if (!mode_bpp)
 956		mode_bpp = default_bpp;
 957
 958	mode_bpp = dp_panel_get_mode_bpp(dp_display->panel,
 959			mode_bpp, mode_pclk_khz);
 960
 961	mode_rate_khz = mode_pclk_khz * mode_bpp;
 962	supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
 963
 964	if (mode_rate_khz > supported_rate_khz)
 965		return MODE_BAD;
 966
 967	return MODE_OK;
 968}
 969
 970int dp_display_get_modes(struct msm_dp *dp,
 971				struct dp_display_mode *dp_mode)
 972{
 973	struct dp_display_private *dp_display;
 974	int ret = 0;
 975
 976	if (!dp) {
 977		DRM_ERROR("invalid params\n");
 978		return 0;
 979	}
 980
 981	dp_display = container_of(dp, struct dp_display_private, dp_display);
 982
 983	ret = dp_panel_get_modes(dp_display->panel,
 984		dp->connector, dp_mode);
 985	if (dp_mode->drm_mode.clock)
 986		dp->max_pclk_khz = dp_mode->drm_mode.clock;
 987	return ret;
 988}
 989
 990bool dp_display_check_video_test(struct msm_dp *dp)
 991{
 992	struct dp_display_private *dp_display;
 993
 994	dp_display = container_of(dp, struct dp_display_private, dp_display);
 995
 996	return dp_display->panel->video_test;
 997}
 998
 999int dp_display_get_test_bpp(struct msm_dp *dp)
1000{
1001	struct dp_display_private *dp_display;
1002
1003	if (!dp) {
1004		DRM_ERROR("invalid params\n");
1005		return 0;
1006	}
1007
1008	dp_display = container_of(dp, struct dp_display_private, dp_display);
1009
1010	return dp_link_bit_depth_to_bpp(
1011		dp_display->link->test_video.test_bit_depth);
1012}
1013
1014void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1015{
1016	struct dp_display_private *dp_display;
1017	struct drm_device *drm;
1018
1019	dp_display = container_of(dp, struct dp_display_private, dp_display);
1020	drm = dp->drm_dev;
1021
1022	/*
1023	 * if we are reading registers we need the link clocks to be on
1024	 * however till DP cable is connected this will not happen as we
1025	 * do not know the resolution to power up with. Hence check the
1026	 * power_on status before dumping DP registers to avoid crash due
1027	 * to unclocked access
1028	 */
1029	mutex_lock(&dp_display->event_mutex);
1030
1031	if (!dp->power_on) {
1032		mutex_unlock(&dp_display->event_mutex);
1033		return;
1034	}
1035
1036	dp_catalog_snapshot(dp_display->catalog, disp_state);
1037
1038	mutex_unlock(&dp_display->event_mutex);
1039}
1040
1041static void dp_display_config_hpd(struct dp_display_private *dp)
1042{
1043
1044	dp_display_host_init(dp, true);
1045	dp_catalog_ctrl_hpd_config(dp->catalog);
1046
1047	/* Enable interrupt first time
1048	 * we are leaving dp clocks on during disconnect
1049	 * and never disable interrupt
1050	 */
1051	enable_irq(dp->irq);
1052}
1053
1054static int hpd_event_thread(void *data)
1055{
1056	struct dp_display_private *dp_priv;
1057	unsigned long flag;
1058	struct dp_event *todo;
1059	int timeout_mode = 0;
1060
1061	dp_priv = (struct dp_display_private *)data;
1062
1063	while (1) {
1064		if (timeout_mode) {
1065			wait_event_timeout(dp_priv->event_q,
1066				(dp_priv->event_pndx == dp_priv->event_gndx),
1067						EVENT_TIMEOUT);
1068		} else {
1069			wait_event_interruptible(dp_priv->event_q,
1070				(dp_priv->event_pndx != dp_priv->event_gndx));
1071		}
1072		spin_lock_irqsave(&dp_priv->event_lock, flag);
1073		todo = &dp_priv->event_list[dp_priv->event_gndx];
1074		if (todo->delay) {
1075			struct dp_event *todo_next;
1076
1077			dp_priv->event_gndx++;
1078			dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1079
1080			/* re enter delay event into q */
1081			todo_next = &dp_priv->event_list[dp_priv->event_pndx++];
1082			dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1083			todo_next->event_id = todo->event_id;
1084			todo_next->data = todo->data;
1085			todo_next->delay = todo->delay - 1;
1086
1087			/* clean up older event */
1088			todo->event_id = EV_NO_EVENT;
1089			todo->delay = 0;
1090
1091			/* switch to timeout mode */
1092			timeout_mode = 1;
1093			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1094			continue;
1095		}
1096
1097		/* timeout with no events in q */
1098		if (dp_priv->event_pndx == dp_priv->event_gndx) {
1099			spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1100			continue;
1101		}
1102
1103		dp_priv->event_gndx++;
1104		dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1105		timeout_mode = 0;
1106		spin_unlock_irqrestore(&dp_priv->event_lock, flag);
1107
1108		switch (todo->event_id) {
1109		case EV_HPD_INIT_SETUP:
1110			dp_display_config_hpd(dp_priv);
1111			break;
1112		case EV_HPD_PLUG_INT:
1113			dp_hpd_plug_handle(dp_priv, todo->data);
1114			break;
1115		case EV_HPD_UNPLUG_INT:
1116			dp_hpd_unplug_handle(dp_priv, todo->data);
1117			break;
1118		case EV_IRQ_HPD_INT:
1119			dp_irq_hpd_handle(dp_priv, todo->data);
1120			break;
1121		case EV_USER_NOTIFICATION:
1122			dp_display_send_hpd_notification(dp_priv,
1123						todo->data);
1124			break;
1125		case EV_CONNECT_PENDING_TIMEOUT:
1126			dp_connect_pending_timeout(dp_priv,
1127						todo->data);
1128			break;
1129		case EV_DISCONNECT_PENDING_TIMEOUT:
1130			dp_disconnect_pending_timeout(dp_priv,
1131						todo->data);
1132			break;
1133		default:
1134			break;
1135		}
1136	}
1137
1138	return 0;
1139}
1140
1141static void dp_hpd_event_setup(struct dp_display_private *dp_priv)
1142{
1143	init_waitqueue_head(&dp_priv->event_q);
1144	spin_lock_init(&dp_priv->event_lock);
1145
1146	kthread_run(hpd_event_thread, dp_priv, "dp_hpd_handler");
1147}
1148
1149static irqreturn_t dp_display_irq_handler(int irq, void *dev_id)
1150{
1151	struct dp_display_private *dp = dev_id;
1152	irqreturn_t ret = IRQ_HANDLED;
1153	u32 hpd_isr_status;
1154
1155	if (!dp) {
1156		DRM_ERROR("invalid data\n");
1157		return IRQ_NONE;
1158	}
1159
1160	hpd_isr_status = dp_catalog_hpd_get_intr_status(dp->catalog);
1161
1162	if (hpd_isr_status & 0x0F) {
1163		/* hpd related interrupts */
1164		if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1165			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1166
1167		if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1168			/* stop sentinel connect pending checking */
1169			dp_del_event(dp, EV_CONNECT_PENDING_TIMEOUT);
1170			dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1171		}
1172
1173		if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1174			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1175			dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1176		}
1177
1178		if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1179			dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1180	}
1181
1182	/* DP controller isr */
1183	dp_ctrl_isr(dp->ctrl);
1184
1185	/* DP aux isr */
1186	dp_aux_isr(dp->aux);
1187
1188	return ret;
1189}
1190
1191int dp_display_request_irq(struct msm_dp *dp_display)
1192{
1193	int rc = 0;
1194	struct dp_display_private *dp;
1195
1196	if (!dp_display) {
1197		DRM_ERROR("invalid input\n");
1198		return -EINVAL;
1199	}
1200
1201	dp = container_of(dp_display, struct dp_display_private, dp_display);
1202
1203	dp->irq = irq_of_parse_and_map(dp->pdev->dev.of_node, 0);
1204	if (dp->irq < 0) {
1205		rc = dp->irq;
1206		DRM_ERROR("failed to get irq: %d\n", rc);
1207		return rc;
1208	}
1209
1210	rc = devm_request_irq(&dp->pdev->dev, dp->irq,
1211			dp_display_irq_handler,
1212			IRQF_TRIGGER_HIGH, "dp_display_isr", dp);
1213	if (rc < 0) {
1214		DRM_ERROR("failed to request IRQ%u: %d\n",
1215				dp->irq, rc);
1216		return rc;
1217	}
1218	disable_irq(dp->irq);
1219
1220	return 0;
1221}
1222
1223static int dp_display_probe(struct platform_device *pdev)
1224{
1225	int rc = 0;
1226	struct dp_display_private *dp;
1227
1228	if (!pdev || !pdev->dev.of_node) {
1229		DRM_ERROR("pdev not found\n");
1230		return -ENODEV;
1231	}
1232
1233	dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1234	if (!dp)
1235		return -ENOMEM;
1236
1237	dp->pdev = pdev;
1238	dp->name = "drm_dp";
1239
1240	rc = dp_init_sub_modules(dp);
1241	if (rc) {
1242		DRM_ERROR("init sub module failed\n");
1243		return -EPROBE_DEFER;
1244	}
1245
1246	mutex_init(&dp->event_mutex);
1247	g_dp_display = &dp->dp_display;
1248
1249	/* Store DP audio handle inside DP display */
1250	g_dp_display->dp_audio = dp->audio;
1251
1252	init_completion(&dp->audio_comp);
1253
1254	platform_set_drvdata(pdev, g_dp_display);
1255
1256	rc = component_add(&pdev->dev, &dp_display_comp_ops);
1257	if (rc) {
1258		DRM_ERROR("component add failed, rc=%d\n", rc);
1259		dp_display_deinit_sub_modules(dp);
1260	}
1261
1262	return rc;
1263}
1264
1265static int dp_display_remove(struct platform_device *pdev)
1266{
1267	struct dp_display_private *dp;
1268
1269	dp = container_of(g_dp_display,
1270			struct dp_display_private, dp_display);
1271
1272	dp_display_deinit_sub_modules(dp);
1273
1274	component_del(&pdev->dev, &dp_display_comp_ops);
1275	platform_set_drvdata(pdev, NULL);
1276
1277	return 0;
1278}
1279
1280static int dp_pm_resume(struct device *dev)
1281{
1282	struct platform_device *pdev = to_platform_device(dev);
1283	struct msm_dp *dp_display = platform_get_drvdata(pdev);
1284	struct dp_display_private *dp;
1285	int sink_count = 0;
1286
1287	dp = container_of(dp_display, struct dp_display_private, dp_display);
1288
1289	mutex_lock(&dp->event_mutex);
1290
1291	/* start from disconnected state */
1292	dp->hpd_state = ST_DISCONNECTED;
1293
1294	/* turn on dp ctrl/phy */
1295	dp_display_host_init(dp, true);
1296
1297	dp_catalog_ctrl_hpd_config(dp->catalog);
1298
1299	/*
1300	 * set sink to normal operation mode -- D0
1301	 * before dpcd read
1302	 */
1303	dp_link_psm_config(dp->link, &dp->panel->link_info, false);
1304
1305	if (dp_catalog_link_is_connected(dp->catalog)) {
1306		sink_count = drm_dp_read_sink_count(dp->aux);
1307		if (sink_count < 0)
1308			sink_count = 0;
1309	}
1310
1311	dp->link->sink_count = sink_count;
1312	/*
1313	 * can not declared display is connected unless
1314	 * HDMI cable is plugged in and sink_count of
1315	 * dongle become 1
1316	 */
1317	if (dp->link->sink_count)
1318		dp->dp_display.is_connected = true;
1319	else
1320		dp->dp_display.is_connected = false;
1321
1322	dp_display_handle_plugged_change(g_dp_display,
1323				dp->dp_display.is_connected);
1324
1325
1326	mutex_unlock(&dp->event_mutex);
1327
1328	return 0;
1329}
1330
1331static int dp_pm_suspend(struct device *dev)
1332{
1333	struct platform_device *pdev = to_platform_device(dev);
1334	struct msm_dp *dp_display = platform_get_drvdata(pdev);
1335	struct dp_display_private *dp;
1336
1337	dp = container_of(dp_display, struct dp_display_private, dp_display);
1338
1339	mutex_lock(&dp->event_mutex);
1340
1341	if (dp->core_initialized == true) {
1342		/* mainlink enabled */
1343		if (dp_power_clk_status(dp->power, DP_CTRL_PM))
1344			dp_ctrl_off_link_stream(dp->ctrl);
1345
1346		dp_display_host_deinit(dp);
1347	}
1348
1349	dp->hpd_state = ST_SUSPENDED;
1350
1351	/* host_init will be called at pm_resume */
1352	dp->core_initialized = false;
1353
1354	mutex_unlock(&dp->event_mutex);
1355
1356	return 0;
1357}
1358
1359static int dp_pm_prepare(struct device *dev)
1360{
1361	return 0;
1362}
1363
1364static void dp_pm_complete(struct device *dev)
1365{
1366
1367}
1368
1369static const struct dev_pm_ops dp_pm_ops = {
1370	.suspend = dp_pm_suspend,
1371	.resume =  dp_pm_resume,
1372	.prepare = dp_pm_prepare,
1373	.complete = dp_pm_complete,
1374};
1375
1376static struct platform_driver dp_display_driver = {
1377	.probe  = dp_display_probe,
1378	.remove = dp_display_remove,
1379	.driver = {
1380		.name = "msm-dp-display",
1381		.of_match_table = dp_dt_match,
1382		.suppress_bind_attrs = true,
1383		.pm = &dp_pm_ops,
1384	},
1385};
1386
1387int __init msm_dp_register(void)
1388{
1389	int ret;
1390
1391	ret = platform_driver_register(&dp_display_driver);
1392	if (ret)
1393		DRM_ERROR("Dp display driver register failed");
1394
1395	return ret;
1396}
1397
1398void __exit msm_dp_unregister(void)
1399{
1400	platform_driver_unregister(&dp_display_driver);
1401}
1402
1403void msm_dp_irq_postinstall(struct msm_dp *dp_display)
1404{
1405	struct dp_display_private *dp;
1406
1407	if (!dp_display)
1408		return;
1409
1410	dp = container_of(dp_display, struct dp_display_private, dp_display);
1411
1412	dp_hpd_event_setup(dp);
1413
1414	dp_add_event(dp, EV_HPD_INIT_SETUP, 0, 100);
1415}
1416
1417void msm_dp_debugfs_init(struct msm_dp *dp_display, struct drm_minor *minor)
1418{
1419	struct dp_display_private *dp;
1420	struct device *dev;
1421	int rc;
1422
1423	dp = container_of(dp_display, struct dp_display_private, dp_display);
1424	dev = &dp->pdev->dev;
1425
1426	dp->debug = dp_debug_get(dev, dp->panel, dp->usbpd,
1427					dp->link, &dp->dp_display.connector,
1428					minor);
1429	if (IS_ERR(dp->debug)) {
1430		rc = PTR_ERR(dp->debug);
1431		DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1432		dp->debug = NULL;
1433	}
1434}
1435
1436int msm_dp_modeset_init(struct msm_dp *dp_display, struct drm_device *dev,
1437			struct drm_encoder *encoder)
1438{
1439	struct msm_drm_private *priv;
1440	int ret;
1441
1442	if (WARN_ON(!encoder) || WARN_ON(!dp_display) || WARN_ON(!dev))
1443		return -EINVAL;
1444
1445	priv = dev->dev_private;
1446	dp_display->drm_dev = dev;
1447
1448	ret = dp_display_request_irq(dp_display);
1449	if (ret) {
1450		DRM_ERROR("request_irq failed, ret=%d\n", ret);
1451		return ret;
1452	}
1453
1454	dp_display->encoder = encoder;
1455
1456	dp_display->connector = dp_drm_connector_init(dp_display);
1457	if (IS_ERR(dp_display->connector)) {
1458		ret = PTR_ERR(dp_display->connector);
1459		DRM_DEV_ERROR(dev->dev,
1460			"failed to create dp connector: %d\n", ret);
1461		dp_display->connector = NULL;
1462		return ret;
1463	}
1464
1465	priv->connectors[priv->num_connectors++] = dp_display->connector;
1466	return 0;
1467}
1468
1469int msm_dp_display_enable(struct msm_dp *dp, struct drm_encoder *encoder)
1470{
1471	int rc = 0;
1472	struct dp_display_private *dp_display;
1473	u32 state;
1474
1475	dp_display = container_of(dp, struct dp_display_private, dp_display);
1476	if (!dp_display->dp_mode.drm_mode.clock) {
1477		DRM_ERROR("invalid params\n");
1478		return -EINVAL;
1479	}
1480
1481	mutex_lock(&dp_display->event_mutex);
1482
1483	/* stop sentinel checking */
1484	dp_del_event(dp_display, EV_CONNECT_PENDING_TIMEOUT);
1485
1486	rc = dp_display_set_mode(dp, &dp_display->dp_mode);
1487	if (rc) {
1488		DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1489		mutex_unlock(&dp_display->event_mutex);
1490		return rc;
1491	}
1492
1493	rc = dp_display_prepare(dp);
1494	if (rc) {
1495		DRM_ERROR("DP display prepare failed, rc=%d\n", rc);
1496		mutex_unlock(&dp_display->event_mutex);
1497		return rc;
1498	}
1499
1500	state =  dp_display->hpd_state;
1501
1502	if (state == ST_DISPLAY_OFF)
1503		dp_display_host_init(dp_display, true);
1504
1505	dp_display_enable(dp_display, 0);
1506
1507	rc = dp_display_post_enable(dp);
1508	if (rc) {
1509		DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1510		dp_display_disable(dp_display, 0);
1511		dp_display_unprepare(dp);
1512	}
1513
1514	/* manual kick off plug event to train link */
1515	if (state == ST_DISPLAY_OFF)
1516		dp_add_event(dp_display, EV_IRQ_HPD_INT, 0, 0);
1517
1518	/* completed connection */
1519	dp_display->hpd_state = ST_CONNECTED;
1520
1521	mutex_unlock(&dp_display->event_mutex);
1522
1523	return rc;
1524}
1525
1526int msm_dp_display_pre_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1527{
1528	struct dp_display_private *dp_display;
1529
1530	dp_display = container_of(dp, struct dp_display_private, dp_display);
1531
1532	dp_ctrl_push_idle(dp_display->ctrl);
1533
1534	return 0;
1535}
1536
1537int msm_dp_display_disable(struct msm_dp *dp, struct drm_encoder *encoder)
1538{
1539	int rc = 0;
1540	u32 state;
1541	struct dp_display_private *dp_display;
1542
1543	dp_display = container_of(dp, struct dp_display_private, dp_display);
1544
1545	mutex_lock(&dp_display->event_mutex);
1546
1547	/* stop sentinel checking */
1548	dp_del_event(dp_display, EV_DISCONNECT_PENDING_TIMEOUT);
1549
1550	dp_display_disable(dp_display, 0);
1551
1552	rc = dp_display_unprepare(dp);
1553	if (rc)
1554		DRM_ERROR("DP display unprepare failed, rc=%d\n", rc);
1555
1556	state =  dp_display->hpd_state;
1557	if (state == ST_DISCONNECT_PENDING) {
1558		/* completed disconnection */
1559		dp_display->hpd_state = ST_DISCONNECTED;
1560	} else {
1561		dp_display->hpd_state = ST_DISPLAY_OFF;
1562	}
1563
1564	mutex_unlock(&dp_display->event_mutex);
1565	return rc;
1566}
1567
1568void msm_dp_display_mode_set(struct msm_dp *dp, struct drm_encoder *encoder,
1569				struct drm_display_mode *mode,
1570				struct drm_display_mode *adjusted_mode)
1571{
1572	struct dp_display_private *dp_display;
1573
1574	dp_display = container_of(dp, struct dp_display_private, dp_display);
1575
1576	memset(&dp_display->dp_mode, 0x0, sizeof(struct dp_display_mode));
1577
1578	if (dp_display_check_video_test(dp))
1579		dp_display->dp_mode.bpp = dp_display_get_test_bpp(dp);
1580	else /* Default num_components per pixel = 3 */
1581		dp_display->dp_mode.bpp = dp->connector->display_info.bpc * 3;
1582
1583	if (!dp_display->dp_mode.bpp)
1584		dp_display->dp_mode.bpp = 24; /* Default bpp */
1585
1586	drm_mode_copy(&dp_display->dp_mode.drm_mode, adjusted_mode);
1587
1588	dp_display->dp_mode.v_active_low =
1589		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1590
1591	dp_display->dp_mode.h_active_low =
1592		!!(dp_display->dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1593}