OSDN Git Service

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