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