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