Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/phy/phy.h>
13#include <linux/delay.h>
14#include <drm/display/drm_dp_aux_bus.h>
15#include <drm/drm_edid.h>
16
17#include "msm_drv.h"
18#include "msm_kms.h"
19#include "dp_ctrl.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_display.h"
26#include "dp_drm.h"
27#include "dp_audio.h"
28#include "dp_debug.h"
29
30static bool psr_enabled = false;
31module_param(psr_enabled, bool, 0);
32MODULE_PARM_DESC(psr_enabled, "enable PSR for eDP and DP displays");
33
34#define HPD_STRING_SIZE 30
35
36enum {
37 ISR_DISCONNECTED,
38 ISR_CONNECT_PENDING,
39 ISR_CONNECTED,
40 ISR_HPD_REPLUG_COUNT,
41 ISR_IRQ_HPD_PULSE_COUNT,
42 ISR_HPD_LO_GLITH_COUNT,
43};
44
45/* event thread connection state */
46enum {
47 ST_DISCONNECTED,
48 ST_MAINLINK_READY,
49 ST_CONNECTED,
50 ST_DISCONNECT_PENDING,
51 ST_DISPLAY_OFF,
52};
53
54enum {
55 EV_NO_EVENT,
56 /* hpd events */
57 EV_HPD_PLUG_INT,
58 EV_IRQ_HPD_INT,
59 EV_HPD_UNPLUG_INT,
60 EV_USER_NOTIFICATION,
61};
62
63#define EVENT_TIMEOUT (HZ/10) /* 100ms */
64#define DP_EVENT_Q_MAX 8
65
66#define DP_TIMEOUT_NONE 0
67
68#define WAIT_FOR_RESUME_TIMEOUT_JIFFIES (HZ / 2)
69
70struct msm_dp_event {
71 u32 event_id;
72 u32 data;
73 u32 delay;
74};
75
76struct msm_dp_display_private {
77 int irq;
78
79 unsigned int id;
80
81 /* state variables */
82 bool core_initialized;
83 bool phy_initialized;
84 bool audio_supported;
85
86 struct drm_device *drm_dev;
87
88 struct msm_dp_catalog *catalog;
89 struct drm_dp_aux *aux;
90 struct msm_dp_link *link;
91 struct msm_dp_panel *panel;
92 struct msm_dp_ctrl *ctrl;
93
94 struct msm_dp_display_mode msm_dp_mode;
95 struct msm_dp msm_dp_display;
96
97 /* wait for audio signaling */
98 struct completion audio_comp;
99
100 /* event related only access by event thread */
101 struct mutex event_mutex;
102 wait_queue_head_t event_q;
103 u32 hpd_state;
104 u32 event_pndx;
105 u32 event_gndx;
106 struct task_struct *ev_tsk;
107 struct msm_dp_event event_list[DP_EVENT_Q_MAX];
108 spinlock_t event_lock;
109
110 bool wide_bus_supported;
111
112 struct msm_dp_audio *audio;
113};
114
115struct msm_dp_desc {
116 phys_addr_t io_start;
117 unsigned int id;
118 bool wide_bus_supported;
119};
120
121static const struct msm_dp_desc msm_dp_desc_sa8775p[] = {
122 { .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
123 { .io_start = 0x0af5c000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
124 { .io_start = 0x22154000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
125 { .io_start = 0x2215c000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
126 {}
127};
128
129static const struct msm_dp_desc msm_dp_desc_sc7180[] = {
130 { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
131 {}
132};
133
134static const struct msm_dp_desc msm_dp_desc_sc7280[] = {
135 { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
136 { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
137 {}
138};
139
140static const struct msm_dp_desc msm_dp_desc_sc8180x[] = {
141 { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
142 { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
143 { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
144 {}
145};
146
147static const struct msm_dp_desc msm_dp_desc_sc8280xp[] = {
148 { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
149 { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
150 { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
151 { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
152 { .io_start = 0x22090000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
153 { .io_start = 0x22098000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
154 { .io_start = 0x2209a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
155 { .io_start = 0x220a0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
156 {}
157};
158
159static const struct msm_dp_desc msm_dp_desc_sm8650[] = {
160 { .io_start = 0x0af54000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
161 {}
162};
163
164static const struct msm_dp_desc msm_dp_desc_x1e80100[] = {
165 { .io_start = 0x0ae90000, .id = MSM_DP_CONTROLLER_0, .wide_bus_supported = true },
166 { .io_start = 0x0ae98000, .id = MSM_DP_CONTROLLER_1, .wide_bus_supported = true },
167 { .io_start = 0x0ae9a000, .id = MSM_DP_CONTROLLER_2, .wide_bus_supported = true },
168 { .io_start = 0x0aea0000, .id = MSM_DP_CONTROLLER_3, .wide_bus_supported = true },
169 {}
170};
171
172static const struct of_device_id msm_dp_dt_match[] = {
173 { .compatible = "qcom,sa8775p-dp", .data = &msm_dp_desc_sa8775p },
174 { .compatible = "qcom,sc7180-dp", .data = &msm_dp_desc_sc7180 },
175 { .compatible = "qcom,sc7280-dp", .data = &msm_dp_desc_sc7280 },
176 { .compatible = "qcom,sc7280-edp", .data = &msm_dp_desc_sc7280 },
177 { .compatible = "qcom,sc8180x-dp", .data = &msm_dp_desc_sc8180x },
178 { .compatible = "qcom,sc8180x-edp", .data = &msm_dp_desc_sc8180x },
179 { .compatible = "qcom,sc8280xp-dp", .data = &msm_dp_desc_sc8280xp },
180 { .compatible = "qcom,sc8280xp-edp", .data = &msm_dp_desc_sc8280xp },
181 { .compatible = "qcom,sdm845-dp", .data = &msm_dp_desc_sc7180 },
182 { .compatible = "qcom,sm8350-dp", .data = &msm_dp_desc_sc7180 },
183 { .compatible = "qcom,sm8650-dp", .data = &msm_dp_desc_sm8650 },
184 { .compatible = "qcom,x1e80100-dp", .data = &msm_dp_desc_x1e80100 },
185 {}
186};
187
188static struct msm_dp_display_private *dev_get_dp_display_private(struct device *dev)
189{
190 struct msm_dp *dp = dev_get_drvdata(dev);
191
192 return container_of(dp, struct msm_dp_display_private, msm_dp_display);
193}
194
195static int msm_dp_add_event(struct msm_dp_display_private *msm_dp_priv, u32 event,
196 u32 data, u32 delay)
197{
198 unsigned long flag;
199 struct msm_dp_event *todo;
200 int pndx;
201
202 spin_lock_irqsave(&msm_dp_priv->event_lock, flag);
203 pndx = msm_dp_priv->event_pndx + 1;
204 pndx %= DP_EVENT_Q_MAX;
205 if (pndx == msm_dp_priv->event_gndx) {
206 pr_err("event_q is full: pndx=%d gndx=%d\n",
207 msm_dp_priv->event_pndx, msm_dp_priv->event_gndx);
208 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
209 return -EPERM;
210 }
211 todo = &msm_dp_priv->event_list[msm_dp_priv->event_pndx++];
212 msm_dp_priv->event_pndx %= DP_EVENT_Q_MAX;
213 todo->event_id = event;
214 todo->data = data;
215 todo->delay = delay;
216 wake_up(&msm_dp_priv->event_q);
217 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
218
219 return 0;
220}
221
222static int msm_dp_del_event(struct msm_dp_display_private *msm_dp_priv, u32 event)
223{
224 unsigned long flag;
225 struct msm_dp_event *todo;
226 u32 gndx;
227
228 spin_lock_irqsave(&msm_dp_priv->event_lock, flag);
229 if (msm_dp_priv->event_pndx == msm_dp_priv->event_gndx) {
230 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
231 return -ENOENT;
232 }
233
234 gndx = msm_dp_priv->event_gndx;
235 while (msm_dp_priv->event_pndx != gndx) {
236 todo = &msm_dp_priv->event_list[gndx];
237 if (todo->event_id == event) {
238 todo->event_id = EV_NO_EVENT; /* deleted */
239 todo->delay = 0;
240 }
241 gndx++;
242 gndx %= DP_EVENT_Q_MAX;
243 }
244 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
245
246 return 0;
247}
248
249void msm_dp_display_signal_audio_start(struct msm_dp *msm_dp_display)
250{
251 struct msm_dp_display_private *dp;
252
253 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
254
255 reinit_completion(&dp->audio_comp);
256}
257
258void msm_dp_display_signal_audio_complete(struct msm_dp *msm_dp_display)
259{
260 struct msm_dp_display_private *dp;
261
262 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
263
264 complete_all(&dp->audio_comp);
265}
266
267static int msm_dp_hpd_event_thread_start(struct msm_dp_display_private *msm_dp_priv);
268
269static int msm_dp_display_bind(struct device *dev, struct device *master,
270 void *data)
271{
272 int rc = 0;
273 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
274 struct msm_drm_private *priv = dev_get_drvdata(master);
275 struct drm_device *drm = priv->dev;
276
277 dp->msm_dp_display.drm_dev = drm;
278 priv->dp[dp->id] = &dp->msm_dp_display;
279
280
281
282 dp->drm_dev = drm;
283 dp->aux->drm_dev = drm;
284 rc = msm_dp_aux_register(dp->aux);
285 if (rc) {
286 DRM_ERROR("DRM DP AUX register failed\n");
287 goto end;
288 }
289
290
291 rc = msm_dp_register_audio_driver(dev, dp->audio);
292 if (rc) {
293 DRM_ERROR("Audio registration Dp failed\n");
294 goto end;
295 }
296
297 rc = msm_dp_hpd_event_thread_start(dp);
298 if (rc) {
299 DRM_ERROR("Event thread create failed\n");
300 goto end;
301 }
302
303 return 0;
304end:
305 return rc;
306}
307
308static void msm_dp_display_unbind(struct device *dev, struct device *master,
309 void *data)
310{
311 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
312 struct msm_drm_private *priv = dev_get_drvdata(master);
313
314 kthread_stop(dp->ev_tsk);
315
316 of_dp_aux_depopulate_bus(dp->aux);
317
318 msm_dp_unregister_audio_driver(dev, dp->audio);
319 msm_dp_aux_unregister(dp->aux);
320 dp->drm_dev = NULL;
321 dp->aux->drm_dev = NULL;
322 priv->dp[dp->id] = NULL;
323}
324
325static const struct component_ops msm_dp_display_comp_ops = {
326 .bind = msm_dp_display_bind,
327 .unbind = msm_dp_display_unbind,
328};
329
330static void msm_dp_display_send_hpd_event(struct msm_dp *msm_dp_display)
331{
332 struct msm_dp_display_private *dp;
333 struct drm_connector *connector;
334
335 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
336
337 connector = dp->msm_dp_display.connector;
338 drm_helper_hpd_irq_event(connector->dev);
339}
340
341static int msm_dp_display_send_hpd_notification(struct msm_dp_display_private *dp,
342 bool hpd)
343{
344 if ((hpd && dp->msm_dp_display.link_ready) ||
345 (!hpd && !dp->msm_dp_display.link_ready)) {
346 drm_dbg_dp(dp->drm_dev, "HPD already %s\n",
347 (hpd ? "on" : "off"));
348 return 0;
349 }
350
351 /* reset video pattern flag on disconnect */
352 if (!hpd) {
353 dp->panel->video_test = false;
354 if (!dp->msm_dp_display.is_edp)
355 drm_dp_set_subconnector_property(dp->msm_dp_display.connector,
356 connector_status_disconnected,
357 dp->panel->dpcd,
358 dp->panel->downstream_ports);
359 }
360
361 dp->msm_dp_display.link_ready = hpd;
362
363 drm_dbg_dp(dp->drm_dev, "type=%d hpd=%d\n",
364 dp->msm_dp_display.connector_type, hpd);
365 msm_dp_display_send_hpd_event(&dp->msm_dp_display);
366
367 return 0;
368}
369
370static int msm_dp_display_process_hpd_high(struct msm_dp_display_private *dp)
371{
372 struct drm_connector *connector = dp->msm_dp_display.connector;
373 const struct drm_display_info *info = &connector->display_info;
374 int rc = 0;
375
376 rc = msm_dp_panel_read_sink_caps(dp->panel, connector);
377 if (rc)
378 goto end;
379
380 msm_dp_link_process_request(dp->link);
381
382 if (!dp->msm_dp_display.is_edp)
383 drm_dp_set_subconnector_property(connector,
384 connector_status_connected,
385 dp->panel->dpcd,
386 dp->panel->downstream_ports);
387
388 dp->msm_dp_display.psr_supported = dp->panel->psr_cap.version && psr_enabled;
389
390 dp->audio_supported = info->has_audio;
391 msm_dp_panel_handle_sink_request(dp->panel);
392
393 /*
394 * set sink to normal operation mode -- D0
395 * before dpcd read
396 */
397 msm_dp_link_psm_config(dp->link, &dp->panel->link_info, false);
398
399 msm_dp_link_reset_phy_params_vx_px(dp->link);
400 rc = msm_dp_ctrl_on_link(dp->ctrl);
401 if (rc) {
402 DRM_ERROR("failed to complete DP link training\n");
403 goto end;
404 }
405
406 msm_dp_add_event(dp, EV_USER_NOTIFICATION, true, 0);
407
408end:
409 return rc;
410}
411
412static void msm_dp_display_host_phy_init(struct msm_dp_display_private *dp)
413{
414 drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
415 dp->msm_dp_display.connector_type, dp->core_initialized,
416 dp->phy_initialized);
417
418 if (!dp->phy_initialized) {
419 msm_dp_ctrl_phy_init(dp->ctrl);
420 dp->phy_initialized = true;
421 }
422}
423
424static void msm_dp_display_host_phy_exit(struct msm_dp_display_private *dp)
425{
426 drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
427 dp->msm_dp_display.connector_type, dp->core_initialized,
428 dp->phy_initialized);
429
430 if (dp->phy_initialized) {
431 msm_dp_ctrl_phy_exit(dp->ctrl);
432 dp->phy_initialized = false;
433 }
434}
435
436static void msm_dp_display_host_init(struct msm_dp_display_private *dp)
437{
438 drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
439 dp->msm_dp_display.connector_type, dp->core_initialized,
440 dp->phy_initialized);
441
442 msm_dp_ctrl_core_clk_enable(dp->ctrl);
443 msm_dp_ctrl_reset_irq_ctrl(dp->ctrl, true);
444 msm_dp_aux_init(dp->aux);
445 dp->core_initialized = true;
446}
447
448static void msm_dp_display_host_deinit(struct msm_dp_display_private *dp)
449{
450 drm_dbg_dp(dp->drm_dev, "type=%d core_init=%d phy_init=%d\n",
451 dp->msm_dp_display.connector_type, dp->core_initialized,
452 dp->phy_initialized);
453
454 msm_dp_ctrl_reset_irq_ctrl(dp->ctrl, false);
455 msm_dp_aux_deinit(dp->aux);
456 msm_dp_ctrl_core_clk_disable(dp->ctrl);
457 dp->core_initialized = false;
458}
459
460static int msm_dp_display_usbpd_configure_cb(struct device *dev)
461{
462 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
463
464 msm_dp_display_host_phy_init(dp);
465
466 return msm_dp_display_process_hpd_high(dp);
467}
468
469static int msm_dp_display_notify_disconnect(struct device *dev)
470{
471 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
472
473 msm_dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
474
475 return 0;
476}
477
478static void msm_dp_display_handle_video_request(struct msm_dp_display_private *dp)
479{
480 if (dp->link->sink_request & DP_TEST_LINK_VIDEO_PATTERN) {
481 dp->panel->video_test = true;
482 msm_dp_link_send_test_response(dp->link);
483 }
484}
485
486static int msm_dp_display_handle_port_status_changed(struct msm_dp_display_private *dp)
487{
488 int rc = 0;
489
490 if (drm_dp_is_branch(dp->panel->dpcd) && dp->link->sink_count == 0) {
491 drm_dbg_dp(dp->drm_dev, "sink count is zero, nothing to do\n");
492 if (dp->hpd_state != ST_DISCONNECTED) {
493 dp->hpd_state = ST_DISCONNECT_PENDING;
494 msm_dp_add_event(dp, EV_USER_NOTIFICATION, false, 0);
495 }
496 } else {
497 if (dp->hpd_state == ST_DISCONNECTED) {
498 dp->hpd_state = ST_MAINLINK_READY;
499 rc = msm_dp_display_process_hpd_high(dp);
500 if (rc)
501 dp->hpd_state = ST_DISCONNECTED;
502 }
503 }
504
505 return rc;
506}
507
508static int msm_dp_display_handle_irq_hpd(struct msm_dp_display_private *dp)
509{
510 u32 sink_request = dp->link->sink_request;
511
512 drm_dbg_dp(dp->drm_dev, "%d\n", sink_request);
513 if (dp->hpd_state == ST_DISCONNECTED) {
514 if (sink_request & DP_LINK_STATUS_UPDATED) {
515 drm_dbg_dp(dp->drm_dev, "Disconnected sink_request: %d\n",
516 sink_request);
517 DRM_ERROR("Disconnected, no DP_LINK_STATUS_UPDATED\n");
518 return -EINVAL;
519 }
520 }
521
522 msm_dp_ctrl_handle_sink_request(dp->ctrl);
523
524 if (sink_request & DP_TEST_LINK_VIDEO_PATTERN)
525 msm_dp_display_handle_video_request(dp);
526
527 return 0;
528}
529
530static int msm_dp_display_usbpd_attention_cb(struct device *dev)
531{
532 int rc = 0;
533 u32 sink_request;
534 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
535
536 /* check for any test request issued by sink */
537 rc = msm_dp_link_process_request(dp->link);
538 if (!rc) {
539 sink_request = dp->link->sink_request;
540 drm_dbg_dp(dp->drm_dev, "hpd_state=%d sink_request=%d\n",
541 dp->hpd_state, sink_request);
542 if (sink_request & DS_PORT_STATUS_CHANGED)
543 rc = msm_dp_display_handle_port_status_changed(dp);
544 else
545 rc = msm_dp_display_handle_irq_hpd(dp);
546 }
547
548 return rc;
549}
550
551static int msm_dp_hpd_plug_handle(struct msm_dp_display_private *dp, u32 data)
552{
553 u32 state;
554 int ret;
555 struct platform_device *pdev = dp->msm_dp_display.pdev;
556
557 msm_dp_aux_enable_xfers(dp->aux, true);
558
559 mutex_lock(&dp->event_mutex);
560
561 state = dp->hpd_state;
562 drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
563 dp->msm_dp_display.connector_type, state);
564
565 if (state == ST_DISPLAY_OFF) {
566 mutex_unlock(&dp->event_mutex);
567 return 0;
568 }
569
570 if (state == ST_MAINLINK_READY || state == ST_CONNECTED) {
571 mutex_unlock(&dp->event_mutex);
572 return 0;
573 }
574
575 if (state == ST_DISCONNECT_PENDING) {
576 /* wait until ST_DISCONNECTED */
577 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 1); /* delay = 1 */
578 mutex_unlock(&dp->event_mutex);
579 return 0;
580 }
581
582 ret = pm_runtime_resume_and_get(&pdev->dev);
583 if (ret) {
584 DRM_ERROR("failed to pm_runtime_resume\n");
585 mutex_unlock(&dp->event_mutex);
586 return ret;
587 }
588
589 ret = msm_dp_display_usbpd_configure_cb(&pdev->dev);
590 if (ret) { /* link train failed */
591 dp->hpd_state = ST_DISCONNECTED;
592 pm_runtime_put_sync(&pdev->dev);
593 } else {
594 dp->hpd_state = ST_MAINLINK_READY;
595 }
596
597 drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
598 dp->msm_dp_display.connector_type, state);
599 mutex_unlock(&dp->event_mutex);
600
601 /* uevent will complete connection part */
602 return 0;
603};
604
605static void msm_dp_display_handle_plugged_change(struct msm_dp *msm_dp_display,
606 bool plugged)
607{
608 struct msm_dp_display_private *dp;
609
610 dp = container_of(msm_dp_display,
611 struct msm_dp_display_private, msm_dp_display);
612
613 /* notify audio subsystem only if sink supports audio */
614 if (msm_dp_display->plugged_cb && msm_dp_display->codec_dev &&
615 dp->audio_supported)
616 msm_dp_display->plugged_cb(msm_dp_display->codec_dev, plugged);
617}
618
619static int msm_dp_hpd_unplug_handle(struct msm_dp_display_private *dp, u32 data)
620{
621 u32 state;
622 struct platform_device *pdev = dp->msm_dp_display.pdev;
623
624 msm_dp_aux_enable_xfers(dp->aux, false);
625
626 mutex_lock(&dp->event_mutex);
627
628 state = dp->hpd_state;
629
630 drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
631 dp->msm_dp_display.connector_type, state);
632
633 /* unplugged, no more irq_hpd handle */
634 msm_dp_del_event(dp, EV_IRQ_HPD_INT);
635
636 if (state == ST_DISCONNECTED) {
637 /* triggered by irq_hdp with sink_count = 0 */
638 if (dp->link->sink_count == 0) {
639 msm_dp_display_host_phy_exit(dp);
640 }
641 msm_dp_display_notify_disconnect(&dp->msm_dp_display.pdev->dev);
642 mutex_unlock(&dp->event_mutex);
643 return 0;
644 } else if (state == ST_DISCONNECT_PENDING) {
645 mutex_unlock(&dp->event_mutex);
646 return 0;
647 } else if (state == ST_MAINLINK_READY) {
648 msm_dp_ctrl_off_link(dp->ctrl);
649 msm_dp_display_host_phy_exit(dp);
650 dp->hpd_state = ST_DISCONNECTED;
651 msm_dp_display_notify_disconnect(&dp->msm_dp_display.pdev->dev);
652 pm_runtime_put_sync(&pdev->dev);
653 mutex_unlock(&dp->event_mutex);
654 return 0;
655 }
656
657 /*
658 * We don't need separate work for disconnect as
659 * connect/attention interrupts are disabled
660 */
661 msm_dp_display_notify_disconnect(&dp->msm_dp_display.pdev->dev);
662
663 if (state == ST_DISPLAY_OFF) {
664 dp->hpd_state = ST_DISCONNECTED;
665 } else {
666 dp->hpd_state = ST_DISCONNECT_PENDING;
667 }
668
669 /* signal the disconnect event early to ensure proper teardown */
670 msm_dp_display_handle_plugged_change(&dp->msm_dp_display, false);
671
672 drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
673 dp->msm_dp_display.connector_type, state);
674
675 /* uevent will complete disconnection part */
676 pm_runtime_put_sync(&pdev->dev);
677 mutex_unlock(&dp->event_mutex);
678 return 0;
679}
680
681static int msm_dp_irq_hpd_handle(struct msm_dp_display_private *dp, u32 data)
682{
683 u32 state;
684
685 mutex_lock(&dp->event_mutex);
686
687 /* irq_hpd can happen at either connected or disconnected state */
688 state = dp->hpd_state;
689 drm_dbg_dp(dp->drm_dev, "Before, type=%d hpd_state=%d\n",
690 dp->msm_dp_display.connector_type, state);
691
692 if (state == ST_DISPLAY_OFF) {
693 mutex_unlock(&dp->event_mutex);
694 return 0;
695 }
696
697 if (state == ST_MAINLINK_READY || state == ST_DISCONNECT_PENDING) {
698 /* wait until ST_CONNECTED */
699 msm_dp_add_event(dp, EV_IRQ_HPD_INT, 0, 1); /* delay = 1 */
700 mutex_unlock(&dp->event_mutex);
701 return 0;
702 }
703
704 msm_dp_display_usbpd_attention_cb(&dp->msm_dp_display.pdev->dev);
705
706 drm_dbg_dp(dp->drm_dev, "After, type=%d hpd_state=%d\n",
707 dp->msm_dp_display.connector_type, state);
708
709 mutex_unlock(&dp->event_mutex);
710
711 return 0;
712}
713
714static void msm_dp_display_deinit_sub_modules(struct msm_dp_display_private *dp)
715{
716 msm_dp_audio_put(dp->audio);
717 msm_dp_panel_put(dp->panel);
718 msm_dp_aux_put(dp->aux);
719}
720
721static int msm_dp_init_sub_modules(struct msm_dp_display_private *dp)
722{
723 int rc = 0;
724 struct device *dev = &dp->msm_dp_display.pdev->dev;
725 struct phy *phy;
726
727 phy = devm_phy_get(dev, "dp");
728 if (IS_ERR(phy))
729 return PTR_ERR(phy);
730
731 rc = phy_set_mode_ext(phy, PHY_MODE_DP,
732 dp->msm_dp_display.is_edp ? PHY_SUBMODE_EDP : PHY_SUBMODE_DP);
733 if (rc) {
734 DRM_ERROR("failed to set phy submode, rc = %d\n", rc);
735 dp->catalog = NULL;
736 goto error;
737 }
738
739 dp->catalog = msm_dp_catalog_get(dev);
740 if (IS_ERR(dp->catalog)) {
741 rc = PTR_ERR(dp->catalog);
742 DRM_ERROR("failed to initialize catalog, rc = %d\n", rc);
743 dp->catalog = NULL;
744 goto error;
745 }
746
747 dp->aux = msm_dp_aux_get(dev, dp->catalog,
748 phy,
749 dp->msm_dp_display.is_edp);
750 if (IS_ERR(dp->aux)) {
751 rc = PTR_ERR(dp->aux);
752 DRM_ERROR("failed to initialize aux, rc = %d\n", rc);
753 dp->aux = NULL;
754 goto error;
755 }
756
757 dp->link = msm_dp_link_get(dev, dp->aux);
758 if (IS_ERR(dp->link)) {
759 rc = PTR_ERR(dp->link);
760 DRM_ERROR("failed to initialize link, rc = %d\n", rc);
761 dp->link = NULL;
762 goto error_link;
763 }
764
765 dp->panel = msm_dp_panel_get(dev, dp->aux, dp->link, dp->catalog);
766 if (IS_ERR(dp->panel)) {
767 rc = PTR_ERR(dp->panel);
768 DRM_ERROR("failed to initialize panel, rc = %d\n", rc);
769 dp->panel = NULL;
770 goto error_link;
771 }
772
773 dp->ctrl = msm_dp_ctrl_get(dev, dp->link, dp->panel, dp->aux,
774 dp->catalog,
775 phy);
776 if (IS_ERR(dp->ctrl)) {
777 rc = PTR_ERR(dp->ctrl);
778 DRM_ERROR("failed to initialize ctrl, rc = %d\n", rc);
779 dp->ctrl = NULL;
780 goto error_ctrl;
781 }
782
783 dp->audio = msm_dp_audio_get(dp->msm_dp_display.pdev, dp->catalog);
784 if (IS_ERR(dp->audio)) {
785 rc = PTR_ERR(dp->audio);
786 pr_err("failed to initialize audio, rc = %d\n", rc);
787 dp->audio = NULL;
788 goto error_ctrl;
789 }
790
791 return rc;
792
793error_ctrl:
794 msm_dp_panel_put(dp->panel);
795error_link:
796 msm_dp_aux_put(dp->aux);
797error:
798 return rc;
799}
800
801static int msm_dp_display_set_mode(struct msm_dp *msm_dp_display,
802 struct msm_dp_display_mode *mode)
803{
804 struct msm_dp_display_private *dp;
805
806 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
807
808 drm_mode_copy(&dp->panel->msm_dp_mode.drm_mode, &mode->drm_mode);
809 dp->panel->msm_dp_mode.bpp = mode->bpp;
810 dp->panel->msm_dp_mode.out_fmt_is_yuv_420 = mode->out_fmt_is_yuv_420;
811 msm_dp_panel_init_panel_info(dp->panel);
812 return 0;
813}
814
815static int msm_dp_display_enable(struct msm_dp_display_private *dp, bool force_link_train)
816{
817 int rc = 0;
818 struct msm_dp *msm_dp_display = &dp->msm_dp_display;
819
820 drm_dbg_dp(dp->drm_dev, "sink_count=%d\n", dp->link->sink_count);
821 if (msm_dp_display->power_on) {
822 drm_dbg_dp(dp->drm_dev, "Link already setup, return\n");
823 return 0;
824 }
825
826 rc = msm_dp_ctrl_on_stream(dp->ctrl, force_link_train);
827 if (!rc)
828 msm_dp_display->power_on = true;
829
830 return rc;
831}
832
833static int msm_dp_display_post_enable(struct msm_dp *msm_dp_display)
834{
835 struct msm_dp_display_private *dp;
836 u32 rate;
837
838 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
839
840 rate = dp->link->link_params.rate;
841
842 if (dp->audio_supported) {
843 dp->audio->bw_code = drm_dp_link_rate_to_bw_code(rate);
844 dp->audio->lane_count = dp->link->link_params.num_lanes;
845 }
846
847 /* signal the connect event late to synchronize video and display */
848 msm_dp_display_handle_plugged_change(msm_dp_display, true);
849
850 if (msm_dp_display->psr_supported)
851 msm_dp_ctrl_config_psr(dp->ctrl);
852
853 return 0;
854}
855
856static int msm_dp_display_disable(struct msm_dp_display_private *dp)
857{
858 struct msm_dp *msm_dp_display = &dp->msm_dp_display;
859
860 if (!msm_dp_display->power_on)
861 return 0;
862
863 /* wait only if audio was enabled */
864 if (msm_dp_display->audio_enabled) {
865 /* signal the disconnect event */
866 msm_dp_display_handle_plugged_change(msm_dp_display, false);
867 if (!wait_for_completion_timeout(&dp->audio_comp,
868 HZ * 5))
869 DRM_ERROR("audio comp timeout\n");
870 }
871
872 msm_dp_display->audio_enabled = false;
873
874 if (dp->link->sink_count == 0) {
875 /*
876 * irq_hpd with sink_count = 0
877 * hdmi unplugged out of dongle
878 */
879 msm_dp_ctrl_off_link_stream(dp->ctrl);
880 } else {
881 /*
882 * unplugged interrupt
883 * dongle unplugged out of DUT
884 */
885 msm_dp_ctrl_off(dp->ctrl);
886 msm_dp_display_host_phy_exit(dp);
887 }
888
889 msm_dp_display->power_on = false;
890
891 drm_dbg_dp(dp->drm_dev, "sink count: %d\n", dp->link->sink_count);
892 return 0;
893}
894
895int msm_dp_display_set_plugged_cb(struct msm_dp *msm_dp_display,
896 hdmi_codec_plugged_cb fn, struct device *codec_dev)
897{
898 bool plugged;
899
900 msm_dp_display->plugged_cb = fn;
901 msm_dp_display->codec_dev = codec_dev;
902 plugged = msm_dp_display->link_ready;
903 msm_dp_display_handle_plugged_change(msm_dp_display, plugged);
904
905 return 0;
906}
907
908/**
909 * msm_dp_bridge_mode_valid - callback to determine if specified mode is valid
910 * @bridge: Pointer to drm bridge structure
911 * @info: display info
912 * @mode: Pointer to drm mode structure
913 * Returns: Validity status for specified mode
914 */
915enum drm_mode_status msm_dp_bridge_mode_valid(struct drm_bridge *bridge,
916 const struct drm_display_info *info,
917 const struct drm_display_mode *mode)
918{
919 const u32 num_components = 3, default_bpp = 24;
920 struct msm_dp_display_private *msm_dp_display;
921 struct msm_dp_link_info *link_info;
922 u32 mode_rate_khz = 0, supported_rate_khz = 0, mode_bpp = 0;
923 struct msm_dp *dp;
924 int mode_pclk_khz = mode->clock;
925
926 dp = to_dp_bridge(bridge)->msm_dp_display;
927
928 if (!dp || !mode_pclk_khz || !dp->connector) {
929 DRM_ERROR("invalid params\n");
930 return -EINVAL;
931 }
932
933 if (mode->clock > DP_MAX_PIXEL_CLK_KHZ)
934 return MODE_CLOCK_HIGH;
935
936 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
937 link_info = &msm_dp_display->panel->link_info;
938
939 if (drm_mode_is_420_only(&dp->connector->display_info, mode) &&
940 msm_dp_display->panel->vsc_sdp_supported)
941 mode_pclk_khz /= 2;
942
943 mode_bpp = dp->connector->display_info.bpc * num_components;
944 if (!mode_bpp)
945 mode_bpp = default_bpp;
946
947 mode_bpp = msm_dp_panel_get_mode_bpp(msm_dp_display->panel,
948 mode_bpp, mode_pclk_khz);
949
950 mode_rate_khz = mode_pclk_khz * mode_bpp;
951 supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
952
953 if (mode_rate_khz > supported_rate_khz)
954 return MODE_BAD;
955
956 return MODE_OK;
957}
958
959int msm_dp_display_get_modes(struct msm_dp *dp)
960{
961 struct msm_dp_display_private *msm_dp_display;
962
963 if (!dp) {
964 DRM_ERROR("invalid params\n");
965 return 0;
966 }
967
968 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
969
970 return msm_dp_panel_get_modes(msm_dp_display->panel,
971 dp->connector);
972}
973
974bool msm_dp_display_check_video_test(struct msm_dp *dp)
975{
976 struct msm_dp_display_private *msm_dp_display;
977
978 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
979
980 return msm_dp_display->panel->video_test;
981}
982
983int msm_dp_display_get_test_bpp(struct msm_dp *dp)
984{
985 struct msm_dp_display_private *msm_dp_display;
986
987 if (!dp) {
988 DRM_ERROR("invalid params\n");
989 return 0;
990 }
991
992 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
993
994 return msm_dp_link_bit_depth_to_bpp(
995 msm_dp_display->link->test_video.test_bit_depth);
996}
997
998void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
999{
1000 struct msm_dp_display_private *msm_dp_display;
1001
1002 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1003
1004 /*
1005 * if we are reading registers we need the link clocks to be on
1006 * however till DP cable is connected this will not happen as we
1007 * do not know the resolution to power up with. Hence check the
1008 * power_on status before dumping DP registers to avoid crash due
1009 * to unclocked access
1010 */
1011 mutex_lock(&msm_dp_display->event_mutex);
1012
1013 if (!dp->power_on) {
1014 mutex_unlock(&msm_dp_display->event_mutex);
1015 return;
1016 }
1017
1018 msm_dp_catalog_snapshot(msm_dp_display->catalog, disp_state);
1019
1020 mutex_unlock(&msm_dp_display->event_mutex);
1021}
1022
1023void msm_dp_display_set_psr(struct msm_dp *msm_dp_display, bool enter)
1024{
1025 struct msm_dp_display_private *dp;
1026
1027 if (!msm_dp_display) {
1028 DRM_ERROR("invalid params\n");
1029 return;
1030 }
1031
1032 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1033 msm_dp_ctrl_set_psr(dp->ctrl, enter);
1034}
1035
1036static int hpd_event_thread(void *data)
1037{
1038 struct msm_dp_display_private *msm_dp_priv;
1039 unsigned long flag;
1040 struct msm_dp_event *todo;
1041 int timeout_mode = 0;
1042
1043 msm_dp_priv = (struct msm_dp_display_private *)data;
1044
1045 while (1) {
1046 if (timeout_mode) {
1047 wait_event_timeout(msm_dp_priv->event_q,
1048 (msm_dp_priv->event_pndx == msm_dp_priv->event_gndx) ||
1049 kthread_should_stop(), EVENT_TIMEOUT);
1050 } else {
1051 wait_event_interruptible(msm_dp_priv->event_q,
1052 (msm_dp_priv->event_pndx != msm_dp_priv->event_gndx) ||
1053 kthread_should_stop());
1054 }
1055
1056 if (kthread_should_stop())
1057 break;
1058
1059 spin_lock_irqsave(&msm_dp_priv->event_lock, flag);
1060 todo = &msm_dp_priv->event_list[msm_dp_priv->event_gndx];
1061 if (todo->delay) {
1062 struct msm_dp_event *todo_next;
1063
1064 msm_dp_priv->event_gndx++;
1065 msm_dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1066
1067 /* re enter delay event into q */
1068 todo_next = &msm_dp_priv->event_list[msm_dp_priv->event_pndx++];
1069 msm_dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1070 todo_next->event_id = todo->event_id;
1071 todo_next->data = todo->data;
1072 todo_next->delay = todo->delay - 1;
1073
1074 /* clean up older event */
1075 todo->event_id = EV_NO_EVENT;
1076 todo->delay = 0;
1077
1078 /* switch to timeout mode */
1079 timeout_mode = 1;
1080 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1081 continue;
1082 }
1083
1084 /* timeout with no events in q */
1085 if (msm_dp_priv->event_pndx == msm_dp_priv->event_gndx) {
1086 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1087 continue;
1088 }
1089
1090 msm_dp_priv->event_gndx++;
1091 msm_dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1092 timeout_mode = 0;
1093 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1094
1095 switch (todo->event_id) {
1096 case EV_HPD_PLUG_INT:
1097 msm_dp_hpd_plug_handle(msm_dp_priv, todo->data);
1098 break;
1099 case EV_HPD_UNPLUG_INT:
1100 msm_dp_hpd_unplug_handle(msm_dp_priv, todo->data);
1101 break;
1102 case EV_IRQ_HPD_INT:
1103 msm_dp_irq_hpd_handle(msm_dp_priv, todo->data);
1104 break;
1105 case EV_USER_NOTIFICATION:
1106 msm_dp_display_send_hpd_notification(msm_dp_priv,
1107 todo->data);
1108 break;
1109 default:
1110 break;
1111 }
1112 }
1113
1114 return 0;
1115}
1116
1117static int msm_dp_hpd_event_thread_start(struct msm_dp_display_private *msm_dp_priv)
1118{
1119 /* set event q to empty */
1120 msm_dp_priv->event_gndx = 0;
1121 msm_dp_priv->event_pndx = 0;
1122
1123 msm_dp_priv->ev_tsk = kthread_run(hpd_event_thread, msm_dp_priv, "dp_hpd_handler");
1124 if (IS_ERR(msm_dp_priv->ev_tsk))
1125 return PTR_ERR(msm_dp_priv->ev_tsk);
1126
1127 return 0;
1128}
1129
1130static irqreturn_t msm_dp_display_irq_handler(int irq, void *dev_id)
1131{
1132 struct msm_dp_display_private *dp = dev_id;
1133 irqreturn_t ret = IRQ_NONE;
1134 u32 hpd_isr_status;
1135
1136 if (!dp) {
1137 DRM_ERROR("invalid data\n");
1138 return IRQ_NONE;
1139 }
1140
1141 hpd_isr_status = msm_dp_catalog_hpd_get_intr_status(dp->catalog);
1142
1143 if (hpd_isr_status & 0x0F) {
1144 drm_dbg_dp(dp->drm_dev, "type=%d isr=0x%x\n",
1145 dp->msm_dp_display.connector_type, hpd_isr_status);
1146 /* hpd related interrupts */
1147 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1148 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1149
1150 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1151 msm_dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1152 }
1153
1154 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1155 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1156 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1157 }
1158
1159 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1160 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1161
1162 ret = IRQ_HANDLED;
1163 }
1164
1165 /* DP controller isr */
1166 ret |= msm_dp_ctrl_isr(dp->ctrl);
1167
1168 /* DP aux isr */
1169 ret |= msm_dp_aux_isr(dp->aux);
1170
1171 return ret;
1172}
1173
1174static int msm_dp_display_request_irq(struct msm_dp_display_private *dp)
1175{
1176 int rc = 0;
1177 struct platform_device *pdev = dp->msm_dp_display.pdev;
1178
1179 dp->irq = platform_get_irq(pdev, 0);
1180 if (dp->irq < 0) {
1181 DRM_ERROR("failed to get irq\n");
1182 return dp->irq;
1183 }
1184
1185 rc = devm_request_irq(&pdev->dev, dp->irq, msm_dp_display_irq_handler,
1186 IRQF_TRIGGER_HIGH|IRQF_NO_AUTOEN,
1187 "dp_display_isr", dp);
1188
1189 if (rc < 0) {
1190 DRM_ERROR("failed to request IRQ%u: %d\n",
1191 dp->irq, rc);
1192 return rc;
1193 }
1194
1195 return 0;
1196}
1197
1198static const struct msm_dp_desc *msm_dp_display_get_desc(struct platform_device *pdev)
1199{
1200 const struct msm_dp_desc *descs = of_device_get_match_data(&pdev->dev);
1201 struct resource *res;
1202 int i;
1203
1204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1205 if (!res)
1206 return NULL;
1207
1208 for (i = 0; i < descs[i].io_start; i++) {
1209 if (descs[i].io_start == res->start)
1210 return &descs[i];
1211 }
1212
1213 dev_err(&pdev->dev, "unknown displayport instance\n");
1214 return NULL;
1215}
1216
1217static int msm_dp_display_probe_tail(struct device *dev)
1218{
1219 struct msm_dp *dp = dev_get_drvdata(dev);
1220 int ret;
1221
1222 /*
1223 * External bridges are mandatory for eDP interfaces: one has to
1224 * provide at least an eDP panel (which gets wrapped into panel-bridge).
1225 *
1226 * For DisplayPort interfaces external bridges are optional, so
1227 * silently ignore an error if one is not present (-ENODEV).
1228 */
1229 dp->next_bridge = devm_drm_of_get_bridge(&dp->pdev->dev, dp->pdev->dev.of_node, 1, 0);
1230 if (IS_ERR(dp->next_bridge)) {
1231 ret = PTR_ERR(dp->next_bridge);
1232 dp->next_bridge = NULL;
1233 if (dp->is_edp || ret != -ENODEV)
1234 return ret;
1235 }
1236
1237 ret = component_add(dev, &msm_dp_display_comp_ops);
1238 if (ret)
1239 DRM_ERROR("component add failed, rc=%d\n", ret);
1240
1241 return ret;
1242}
1243
1244static int msm_dp_auxbus_done_probe(struct drm_dp_aux *aux)
1245{
1246 return msm_dp_display_probe_tail(aux->dev);
1247}
1248
1249static int msm_dp_display_get_connector_type(struct platform_device *pdev,
1250 const struct msm_dp_desc *desc)
1251{
1252 struct device_node *node = pdev->dev.of_node;
1253 struct device_node *aux_bus = of_get_child_by_name(node, "aux-bus");
1254 struct device_node *panel = of_get_child_by_name(aux_bus, "panel");
1255 int connector_type;
1256
1257 if (panel)
1258 connector_type = DRM_MODE_CONNECTOR_eDP;
1259 else
1260 connector_type = DRM_MODE_SUBCONNECTOR_DisplayPort;
1261
1262 of_node_put(panel);
1263 of_node_put(aux_bus);
1264
1265 return connector_type;
1266}
1267
1268static int msm_dp_display_probe(struct platform_device *pdev)
1269{
1270 int rc = 0;
1271 struct msm_dp_display_private *dp;
1272 const struct msm_dp_desc *desc;
1273
1274 if (!pdev || !pdev->dev.of_node) {
1275 DRM_ERROR("pdev not found\n");
1276 return -ENODEV;
1277 }
1278
1279 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1280 if (!dp)
1281 return -ENOMEM;
1282
1283 desc = msm_dp_display_get_desc(pdev);
1284 if (!desc)
1285 return -EINVAL;
1286
1287 dp->msm_dp_display.pdev = pdev;
1288 dp->id = desc->id;
1289 dp->msm_dp_display.connector_type = msm_dp_display_get_connector_type(pdev, desc);
1290 dp->wide_bus_supported = desc->wide_bus_supported;
1291 dp->msm_dp_display.is_edp =
1292 (dp->msm_dp_display.connector_type == DRM_MODE_CONNECTOR_eDP);
1293
1294 rc = msm_dp_init_sub_modules(dp);
1295 if (rc) {
1296 DRM_ERROR("init sub module failed\n");
1297 return -EPROBE_DEFER;
1298 }
1299
1300 /* setup event q */
1301 mutex_init(&dp->event_mutex);
1302 init_waitqueue_head(&dp->event_q);
1303 spin_lock_init(&dp->event_lock);
1304
1305 /* Store DP audio handle inside DP display */
1306 dp->msm_dp_display.msm_dp_audio = dp->audio;
1307
1308 init_completion(&dp->audio_comp);
1309
1310 platform_set_drvdata(pdev, &dp->msm_dp_display);
1311
1312 rc = devm_pm_runtime_enable(&pdev->dev);
1313 if (rc)
1314 goto err;
1315
1316 rc = msm_dp_display_request_irq(dp);
1317 if (rc)
1318 goto err;
1319
1320 if (dp->msm_dp_display.is_edp) {
1321 rc = devm_of_dp_aux_populate_bus(dp->aux, msm_dp_auxbus_done_probe);
1322 if (rc) {
1323 DRM_ERROR("eDP auxbus population failed, rc=%d\n", rc);
1324 goto err;
1325 }
1326 } else {
1327 rc = msm_dp_display_probe_tail(&pdev->dev);
1328 if (rc)
1329 goto err;
1330 }
1331
1332 return rc;
1333
1334err:
1335 msm_dp_display_deinit_sub_modules(dp);
1336 return rc;
1337}
1338
1339static void msm_dp_display_remove(struct platform_device *pdev)
1340{
1341 struct msm_dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
1342
1343 component_del(&pdev->dev, &msm_dp_display_comp_ops);
1344 msm_dp_display_deinit_sub_modules(dp);
1345 platform_set_drvdata(pdev, NULL);
1346}
1347
1348static int msm_dp_pm_runtime_suspend(struct device *dev)
1349{
1350 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1351
1352 disable_irq(dp->irq);
1353
1354 if (dp->msm_dp_display.is_edp) {
1355 msm_dp_display_host_phy_exit(dp);
1356 msm_dp_catalog_ctrl_hpd_disable(dp->catalog);
1357 }
1358 msm_dp_display_host_deinit(dp);
1359
1360 return 0;
1361}
1362
1363static int msm_dp_pm_runtime_resume(struct device *dev)
1364{
1365 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1366
1367 /*
1368 * for eDP, host cotroller, HPD block and PHY are enabled here
1369 * but with HPD irq disabled
1370 *
1371 * for DP, only host controller is enabled here.
1372 * HPD block is enabled at msm_dp_bridge_hpd_enable()
1373 * PHY will be enabled at plugin handler later
1374 */
1375 msm_dp_display_host_init(dp);
1376 if (dp->msm_dp_display.is_edp) {
1377 msm_dp_catalog_ctrl_hpd_enable(dp->catalog);
1378 msm_dp_display_host_phy_init(dp);
1379 }
1380
1381 enable_irq(dp->irq);
1382 return 0;
1383}
1384
1385static const struct dev_pm_ops msm_dp_pm_ops = {
1386 SET_RUNTIME_PM_OPS(msm_dp_pm_runtime_suspend, msm_dp_pm_runtime_resume, NULL)
1387 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1388 pm_runtime_force_resume)
1389};
1390
1391static struct platform_driver msm_dp_display_driver = {
1392 .probe = msm_dp_display_probe,
1393 .remove = msm_dp_display_remove,
1394 .driver = {
1395 .name = "msm-dp-display",
1396 .of_match_table = msm_dp_dt_match,
1397 .suppress_bind_attrs = true,
1398 .pm = &msm_dp_pm_ops,
1399 },
1400};
1401
1402int __init msm_dp_register(void)
1403{
1404 int ret;
1405
1406 ret = platform_driver_register(&msm_dp_display_driver);
1407 if (ret)
1408 DRM_ERROR("Dp display driver register failed");
1409
1410 return ret;
1411}
1412
1413void __exit msm_dp_unregister(void)
1414{
1415 platform_driver_unregister(&msm_dp_display_driver);
1416}
1417
1418bool msm_dp_is_yuv_420_enabled(const struct msm_dp *msm_dp_display,
1419 const struct drm_display_mode *mode)
1420{
1421 struct msm_dp_display_private *dp;
1422 const struct drm_display_info *info;
1423
1424 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1425 info = &msm_dp_display->connector->display_info;
1426
1427 return dp->panel->vsc_sdp_supported && drm_mode_is_420_only(info, mode);
1428}
1429
1430bool msm_dp_needs_periph_flush(const struct msm_dp *msm_dp_display,
1431 const struct drm_display_mode *mode)
1432{
1433 return msm_dp_is_yuv_420_enabled(msm_dp_display, mode);
1434}
1435
1436bool msm_dp_wide_bus_available(const struct msm_dp *msm_dp_display)
1437{
1438 struct msm_dp_display_private *dp;
1439
1440 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1441
1442 if (dp->msm_dp_mode.out_fmt_is_yuv_420)
1443 return false;
1444
1445 return dp->wide_bus_supported;
1446}
1447
1448void msm_dp_display_debugfs_init(struct msm_dp *msm_dp_display, struct dentry *root, bool is_edp)
1449{
1450 struct msm_dp_display_private *dp;
1451 struct device *dev;
1452 int rc;
1453
1454 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1455 dev = &dp->msm_dp_display.pdev->dev;
1456
1457 rc = msm_dp_debug_init(dev, dp->panel, dp->link, dp->msm_dp_display.connector, root, is_edp);
1458 if (rc)
1459 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1460}
1461
1462int msm_dp_modeset_init(struct msm_dp *msm_dp_display, struct drm_device *dev,
1463 struct drm_encoder *encoder, bool yuv_supported)
1464{
1465 struct msm_dp_display_private *msm_dp_priv;
1466 int ret;
1467
1468 msm_dp_display->drm_dev = dev;
1469
1470 msm_dp_priv = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1471
1472 ret = msm_dp_bridge_init(msm_dp_display, dev, encoder, yuv_supported);
1473 if (ret) {
1474 DRM_DEV_ERROR(dev->dev,
1475 "failed to create dp bridge: %d\n", ret);
1476 return ret;
1477 }
1478
1479 msm_dp_display->connector = msm_dp_drm_connector_init(msm_dp_display, encoder);
1480 if (IS_ERR(msm_dp_display->connector)) {
1481 ret = PTR_ERR(msm_dp_display->connector);
1482 DRM_DEV_ERROR(dev->dev,
1483 "failed to create dp connector: %d\n", ret);
1484 msm_dp_display->connector = NULL;
1485 return ret;
1486 }
1487
1488 msm_dp_priv->panel->connector = msm_dp_display->connector;
1489
1490 return 0;
1491}
1492
1493void msm_dp_bridge_atomic_enable(struct drm_bridge *drm_bridge,
1494 struct drm_bridge_state *old_bridge_state)
1495{
1496 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1497 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1498 int rc = 0;
1499 struct msm_dp_display_private *msm_dp_display;
1500 u32 state;
1501 bool force_link_train = false;
1502
1503 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1504 if (!msm_dp_display->msm_dp_mode.drm_mode.clock) {
1505 DRM_ERROR("invalid params\n");
1506 return;
1507 }
1508
1509 if (dp->is_edp)
1510 msm_dp_hpd_plug_handle(msm_dp_display, 0);
1511
1512 mutex_lock(&msm_dp_display->event_mutex);
1513 if (pm_runtime_resume_and_get(&dp->pdev->dev)) {
1514 DRM_ERROR("failed to pm_runtime_resume\n");
1515 mutex_unlock(&msm_dp_display->event_mutex);
1516 return;
1517 }
1518
1519 state = msm_dp_display->hpd_state;
1520 if (state != ST_DISPLAY_OFF && state != ST_MAINLINK_READY) {
1521 mutex_unlock(&msm_dp_display->event_mutex);
1522 return;
1523 }
1524
1525 rc = msm_dp_display_set_mode(dp, &msm_dp_display->msm_dp_mode);
1526 if (rc) {
1527 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1528 mutex_unlock(&msm_dp_display->event_mutex);
1529 return;
1530 }
1531
1532 state = msm_dp_display->hpd_state;
1533
1534 if (state == ST_DISPLAY_OFF) {
1535 msm_dp_display_host_phy_init(msm_dp_display);
1536 force_link_train = true;
1537 }
1538
1539 msm_dp_display_enable(msm_dp_display, force_link_train);
1540
1541 rc = msm_dp_display_post_enable(dp);
1542 if (rc) {
1543 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1544 msm_dp_display_disable(msm_dp_display);
1545 }
1546
1547 /* completed connection */
1548 msm_dp_display->hpd_state = ST_CONNECTED;
1549
1550 drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1551 mutex_unlock(&msm_dp_display->event_mutex);
1552}
1553
1554void msm_dp_bridge_atomic_disable(struct drm_bridge *drm_bridge,
1555 struct drm_bridge_state *old_bridge_state)
1556{
1557 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1558 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1559 struct msm_dp_display_private *msm_dp_display;
1560
1561 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1562
1563 msm_dp_ctrl_push_idle(msm_dp_display->ctrl);
1564}
1565
1566void msm_dp_bridge_atomic_post_disable(struct drm_bridge *drm_bridge,
1567 struct drm_bridge_state *old_bridge_state)
1568{
1569 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1570 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1571 u32 state;
1572 struct msm_dp_display_private *msm_dp_display;
1573
1574 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1575
1576 if (dp->is_edp)
1577 msm_dp_hpd_unplug_handle(msm_dp_display, 0);
1578
1579 mutex_lock(&msm_dp_display->event_mutex);
1580
1581 state = msm_dp_display->hpd_state;
1582 if (state != ST_DISCONNECT_PENDING && state != ST_CONNECTED)
1583 drm_dbg_dp(dp->drm_dev, "type=%d wrong hpd_state=%d\n",
1584 dp->connector_type, state);
1585
1586 msm_dp_display_disable(msm_dp_display);
1587
1588 state = msm_dp_display->hpd_state;
1589 if (state == ST_DISCONNECT_PENDING) {
1590 /* completed disconnection */
1591 msm_dp_display->hpd_state = ST_DISCONNECTED;
1592 } else {
1593 msm_dp_display->hpd_state = ST_DISPLAY_OFF;
1594 }
1595
1596 drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1597
1598 pm_runtime_put_sync(&dp->pdev->dev);
1599 mutex_unlock(&msm_dp_display->event_mutex);
1600}
1601
1602void msm_dp_bridge_mode_set(struct drm_bridge *drm_bridge,
1603 const struct drm_display_mode *mode,
1604 const struct drm_display_mode *adjusted_mode)
1605{
1606 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1607 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1608 struct msm_dp_display_private *msm_dp_display;
1609 struct msm_dp_panel *msm_dp_panel;
1610
1611 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1612 msm_dp_panel = msm_dp_display->panel;
1613
1614 memset(&msm_dp_display->msm_dp_mode, 0x0, sizeof(struct msm_dp_display_mode));
1615
1616 if (msm_dp_display_check_video_test(dp))
1617 msm_dp_display->msm_dp_mode.bpp = msm_dp_display_get_test_bpp(dp);
1618 else /* Default num_components per pixel = 3 */
1619 msm_dp_display->msm_dp_mode.bpp = dp->connector->display_info.bpc * 3;
1620
1621 if (!msm_dp_display->msm_dp_mode.bpp)
1622 msm_dp_display->msm_dp_mode.bpp = 24; /* Default bpp */
1623
1624 drm_mode_copy(&msm_dp_display->msm_dp_mode.drm_mode, adjusted_mode);
1625
1626 msm_dp_display->msm_dp_mode.v_active_low =
1627 !!(msm_dp_display->msm_dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1628
1629 msm_dp_display->msm_dp_mode.h_active_low =
1630 !!(msm_dp_display->msm_dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1631
1632 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 =
1633 drm_mode_is_420_only(&dp->connector->display_info, adjusted_mode) &&
1634 msm_dp_panel->vsc_sdp_supported;
1635
1636 /* populate wide_bus_support to different layers */
1637 msm_dp_display->ctrl->wide_bus_en =
1638 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 ? false : msm_dp_display->wide_bus_supported;
1639 msm_dp_display->catalog->wide_bus_en =
1640 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 ? false : msm_dp_display->wide_bus_supported;
1641}
1642
1643void msm_dp_bridge_hpd_enable(struct drm_bridge *bridge)
1644{
1645 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1646 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1647 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1648
1649 /*
1650 * this is for external DP with hpd irq enabled case,
1651 * step-1: msm_dp_pm_runtime_resume() enable dp host only
1652 * step-2: enable hdp block and have hpd irq enabled here
1653 * step-3: waiting for plugin irq while phy is not initialized
1654 * step-4: DP PHY is initialized at plugin handler before link training
1655 *
1656 */
1657 mutex_lock(&dp->event_mutex);
1658 if (pm_runtime_resume_and_get(&msm_dp_display->pdev->dev)) {
1659 DRM_ERROR("failed to resume power\n");
1660 mutex_unlock(&dp->event_mutex);
1661 return;
1662 }
1663
1664 msm_dp_catalog_ctrl_hpd_enable(dp->catalog);
1665
1666 /* enable HDP interrupts */
1667 msm_dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, true);
1668
1669 msm_dp_display->internal_hpd = true;
1670 mutex_unlock(&dp->event_mutex);
1671}
1672
1673void msm_dp_bridge_hpd_disable(struct drm_bridge *bridge)
1674{
1675 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1676 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1677 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1678
1679 mutex_lock(&dp->event_mutex);
1680 /* disable HDP interrupts */
1681 msm_dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, false);
1682 msm_dp_catalog_ctrl_hpd_disable(dp->catalog);
1683
1684 msm_dp_display->internal_hpd = false;
1685
1686 pm_runtime_put_sync(&msm_dp_display->pdev->dev);
1687 mutex_unlock(&dp->event_mutex);
1688}
1689
1690void msm_dp_bridge_hpd_notify(struct drm_bridge *bridge,
1691 enum drm_connector_status status)
1692{
1693 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1694 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1695 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1696
1697 /* Without next_bridge interrupts are handled by the DP core directly */
1698 if (msm_dp_display->internal_hpd)
1699 return;
1700
1701 if (!msm_dp_display->link_ready && status == connector_status_connected)
1702 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1703 else if (msm_dp_display->link_ready && status == connector_status_disconnected)
1704 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1705}