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