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