cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

dp_display.c (44260B)


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