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
30 static bool psr_enabled = false;
31 module_param(psr_enabled, bool, 0);
32 MODULE_PARM_DESC(psr_enabled, "enable PSR for eDP and DP displays");
33
34 #define HPD_STRING_SIZE 30
35
36 enum {
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 */
46 enum {
47 ST_DISCONNECTED,
48 ST_MAINLINK_READY,
49 ST_CONNECTED,
50 ST_DISCONNECT_PENDING,
51 ST_DISPLAY_OFF,
52 };
53
54 enum {
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
70 struct msm_dp_event {
71 u32 event_id;
72 u32 data;
73 u32 delay;
74 };
75
76 struct 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
115 struct msm_dp_desc {
116 phys_addr_t io_start;
117 unsigned int id;
118 bool wide_bus_supported;
119 };
120
121 static 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
129 static 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
134 static 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
140 static 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
147 static 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
159 static 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
164 static 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
172 static 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
dev_get_dp_display_private(struct device * dev)188 static 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
msm_dp_add_event(struct msm_dp_display_private * msm_dp_priv,u32 event,u32 data,u32 delay)195 static 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
msm_dp_del_event(struct msm_dp_display_private * msm_dp_priv,u32 event)222 static 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
msm_dp_display_signal_audio_start(struct msm_dp * msm_dp_display)249 void 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
msm_dp_display_signal_audio_complete(struct msm_dp * msm_dp_display)258 void 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
267 static int msm_dp_hpd_event_thread_start(struct msm_dp_display_private *msm_dp_priv);
268
msm_dp_display_bind(struct device * dev,struct device * master,void * data)269 static 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;
304 end:
305 return rc;
306 }
307
msm_dp_display_unbind(struct device * dev,struct device * master,void * data)308 static 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
325 static const struct component_ops msm_dp_display_comp_ops = {
326 .bind = msm_dp_display_bind,
327 .unbind = msm_dp_display_unbind,
328 };
329
msm_dp_display_send_hpd_event(struct msm_dp * msm_dp_display)330 static 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
msm_dp_display_send_hpd_notification(struct msm_dp_display_private * dp,bool hpd)341 static 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
msm_dp_display_process_hpd_high(struct msm_dp_display_private * dp)370 static 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
408 end:
409 return rc;
410 }
411
msm_dp_display_host_phy_init(struct msm_dp_display_private * dp)412 static 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
msm_dp_display_host_phy_exit(struct msm_dp_display_private * dp)424 static 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
msm_dp_display_host_init(struct msm_dp_display_private * dp)436 static 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
msm_dp_display_host_deinit(struct msm_dp_display_private * dp)448 static 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
msm_dp_display_usbpd_configure_cb(struct device * dev)460 static 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
msm_dp_display_notify_disconnect(struct device * dev)469 static 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
msm_dp_display_handle_video_request(struct msm_dp_display_private * dp)478 static 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
msm_dp_display_handle_port_status_changed(struct msm_dp_display_private * dp)486 static 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
msm_dp_display_handle_irq_hpd(struct msm_dp_display_private * dp)508 static 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
msm_dp_display_usbpd_attention_cb(struct device * dev)530 static 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
msm_dp_hpd_plug_handle(struct msm_dp_display_private * dp,u32 data)551 static 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
msm_dp_display_handle_plugged_change(struct msm_dp * msm_dp_display,bool plugged)605 static 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
msm_dp_hpd_unplug_handle(struct msm_dp_display_private * dp,u32 data)619 static 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
msm_dp_irq_hpd_handle(struct msm_dp_display_private * dp,u32 data)681 static 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
msm_dp_display_deinit_sub_modules(struct msm_dp_display_private * dp)714 static 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
msm_dp_init_sub_modules(struct msm_dp_display_private * dp)721 static 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
793 error_ctrl:
794 msm_dp_panel_put(dp->panel);
795 error_link:
796 msm_dp_aux_put(dp->aux);
797 error:
798 return rc;
799 }
800
msm_dp_display_set_mode(struct msm_dp * msm_dp_display,struct msm_dp_display_mode * mode)801 static 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
msm_dp_display_enable(struct msm_dp_display_private * dp,bool force_link_train)815 static 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
msm_dp_display_post_enable(struct msm_dp * msm_dp_display)833 static 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
msm_dp_display_disable(struct msm_dp_display_private * dp)856 static 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
msm_dp_display_set_plugged_cb(struct msm_dp * msm_dp_display,hdmi_codec_plugged_cb fn,struct device * codec_dev)895 int 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 */
msm_dp_bridge_mode_valid(struct drm_bridge * bridge,const struct drm_display_info * info,const struct drm_display_mode * mode)915 enum 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 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
934 link_info = &msm_dp_display->panel->link_info;
935
936 if ((drm_mode_is_420_only(&dp->connector->display_info, mode) &&
937 msm_dp_display->panel->vsc_sdp_supported) ||
938 msm_dp_wide_bus_available(dp))
939 mode_pclk_khz /= 2;
940
941 if (mode_pclk_khz > DP_MAX_PIXEL_CLK_KHZ)
942 return MODE_CLOCK_HIGH;
943
944 mode_bpp = dp->connector->display_info.bpc * num_components;
945 if (!mode_bpp)
946 mode_bpp = default_bpp;
947
948 mode_bpp = msm_dp_panel_get_mode_bpp(msm_dp_display->panel,
949 mode_bpp, mode_pclk_khz);
950
951 mode_rate_khz = mode_pclk_khz * mode_bpp;
952 supported_rate_khz = link_info->num_lanes * link_info->rate * 8;
953
954 if (mode_rate_khz > supported_rate_khz)
955 return MODE_BAD;
956
957 return MODE_OK;
958 }
959
msm_dp_display_get_modes(struct msm_dp * dp)960 int msm_dp_display_get_modes(struct msm_dp *dp)
961 {
962 struct msm_dp_display_private *msm_dp_display;
963
964 if (!dp) {
965 DRM_ERROR("invalid params\n");
966 return 0;
967 }
968
969 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
970
971 return msm_dp_panel_get_modes(msm_dp_display->panel,
972 dp->connector);
973 }
974
msm_dp_display_check_video_test(struct msm_dp * dp)975 bool msm_dp_display_check_video_test(struct msm_dp *dp)
976 {
977 struct msm_dp_display_private *msm_dp_display;
978
979 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
980
981 return msm_dp_display->panel->video_test;
982 }
983
msm_dp_display_get_test_bpp(struct msm_dp * dp)984 int msm_dp_display_get_test_bpp(struct msm_dp *dp)
985 {
986 struct msm_dp_display_private *msm_dp_display;
987
988 if (!dp) {
989 DRM_ERROR("invalid params\n");
990 return 0;
991 }
992
993 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
994
995 return msm_dp_link_bit_depth_to_bpp(
996 msm_dp_display->link->test_video.test_bit_depth);
997 }
998
msm_dp_snapshot(struct msm_disp_state * disp_state,struct msm_dp * dp)999 void msm_dp_snapshot(struct msm_disp_state *disp_state, struct msm_dp *dp)
1000 {
1001 struct msm_dp_display_private *msm_dp_display;
1002
1003 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1004
1005 /*
1006 * if we are reading registers we need the link clocks to be on
1007 * however till DP cable is connected this will not happen as we
1008 * do not know the resolution to power up with. Hence check the
1009 * power_on status before dumping DP registers to avoid crash due
1010 * to unclocked access
1011 */
1012 mutex_lock(&msm_dp_display->event_mutex);
1013
1014 if (!dp->power_on) {
1015 mutex_unlock(&msm_dp_display->event_mutex);
1016 return;
1017 }
1018
1019 msm_dp_catalog_snapshot(msm_dp_display->catalog, disp_state);
1020
1021 mutex_unlock(&msm_dp_display->event_mutex);
1022 }
1023
msm_dp_display_set_psr(struct msm_dp * msm_dp_display,bool enter)1024 void msm_dp_display_set_psr(struct msm_dp *msm_dp_display, bool enter)
1025 {
1026 struct msm_dp_display_private *dp;
1027
1028 if (!msm_dp_display) {
1029 DRM_ERROR("invalid params\n");
1030 return;
1031 }
1032
1033 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1034 msm_dp_ctrl_set_psr(dp->ctrl, enter);
1035 }
1036
hpd_event_thread(void * data)1037 static int hpd_event_thread(void *data)
1038 {
1039 struct msm_dp_display_private *msm_dp_priv;
1040 unsigned long flag;
1041 struct msm_dp_event *todo;
1042 int timeout_mode = 0;
1043
1044 msm_dp_priv = (struct msm_dp_display_private *)data;
1045
1046 while (1) {
1047 if (timeout_mode) {
1048 wait_event_timeout(msm_dp_priv->event_q,
1049 (msm_dp_priv->event_pndx == msm_dp_priv->event_gndx) ||
1050 kthread_should_stop(), EVENT_TIMEOUT);
1051 } else {
1052 wait_event_interruptible(msm_dp_priv->event_q,
1053 (msm_dp_priv->event_pndx != msm_dp_priv->event_gndx) ||
1054 kthread_should_stop());
1055 }
1056
1057 if (kthread_should_stop())
1058 break;
1059
1060 spin_lock_irqsave(&msm_dp_priv->event_lock, flag);
1061 todo = &msm_dp_priv->event_list[msm_dp_priv->event_gndx];
1062 if (todo->delay) {
1063 struct msm_dp_event *todo_next;
1064
1065 msm_dp_priv->event_gndx++;
1066 msm_dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1067
1068 /* re enter delay event into q */
1069 todo_next = &msm_dp_priv->event_list[msm_dp_priv->event_pndx++];
1070 msm_dp_priv->event_pndx %= DP_EVENT_Q_MAX;
1071 todo_next->event_id = todo->event_id;
1072 todo_next->data = todo->data;
1073 todo_next->delay = todo->delay - 1;
1074
1075 /* clean up older event */
1076 todo->event_id = EV_NO_EVENT;
1077 todo->delay = 0;
1078
1079 /* switch to timeout mode */
1080 timeout_mode = 1;
1081 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1082 continue;
1083 }
1084
1085 /* timeout with no events in q */
1086 if (msm_dp_priv->event_pndx == msm_dp_priv->event_gndx) {
1087 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1088 continue;
1089 }
1090
1091 msm_dp_priv->event_gndx++;
1092 msm_dp_priv->event_gndx %= DP_EVENT_Q_MAX;
1093 timeout_mode = 0;
1094 spin_unlock_irqrestore(&msm_dp_priv->event_lock, flag);
1095
1096 switch (todo->event_id) {
1097 case EV_HPD_PLUG_INT:
1098 msm_dp_hpd_plug_handle(msm_dp_priv, todo->data);
1099 break;
1100 case EV_HPD_UNPLUG_INT:
1101 msm_dp_hpd_unplug_handle(msm_dp_priv, todo->data);
1102 break;
1103 case EV_IRQ_HPD_INT:
1104 msm_dp_irq_hpd_handle(msm_dp_priv, todo->data);
1105 break;
1106 case EV_USER_NOTIFICATION:
1107 msm_dp_display_send_hpd_notification(msm_dp_priv,
1108 todo->data);
1109 break;
1110 default:
1111 break;
1112 }
1113 }
1114
1115 return 0;
1116 }
1117
msm_dp_hpd_event_thread_start(struct msm_dp_display_private * msm_dp_priv)1118 static int msm_dp_hpd_event_thread_start(struct msm_dp_display_private *msm_dp_priv)
1119 {
1120 /* set event q to empty */
1121 msm_dp_priv->event_gndx = 0;
1122 msm_dp_priv->event_pndx = 0;
1123
1124 msm_dp_priv->ev_tsk = kthread_run(hpd_event_thread, msm_dp_priv, "dp_hpd_handler");
1125 if (IS_ERR(msm_dp_priv->ev_tsk))
1126 return PTR_ERR(msm_dp_priv->ev_tsk);
1127
1128 return 0;
1129 }
1130
msm_dp_display_irq_handler(int irq,void * dev_id)1131 static irqreturn_t msm_dp_display_irq_handler(int irq, void *dev_id)
1132 {
1133 struct msm_dp_display_private *dp = dev_id;
1134 irqreturn_t ret = IRQ_NONE;
1135 u32 hpd_isr_status;
1136
1137 if (!dp) {
1138 DRM_ERROR("invalid data\n");
1139 return IRQ_NONE;
1140 }
1141
1142 hpd_isr_status = msm_dp_catalog_hpd_get_intr_status(dp->catalog);
1143
1144 if (hpd_isr_status & 0x0F) {
1145 drm_dbg_dp(dp->drm_dev, "type=%d isr=0x%x\n",
1146 dp->msm_dp_display.connector_type, hpd_isr_status);
1147 /* hpd related interrupts */
1148 if (hpd_isr_status & DP_DP_HPD_PLUG_INT_MASK)
1149 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1150
1151 if (hpd_isr_status & DP_DP_IRQ_HPD_INT_MASK) {
1152 msm_dp_add_event(dp, EV_IRQ_HPD_INT, 0, 0);
1153 }
1154
1155 if (hpd_isr_status & DP_DP_HPD_REPLUG_INT_MASK) {
1156 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1157 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 3);
1158 }
1159
1160 if (hpd_isr_status & DP_DP_HPD_UNPLUG_INT_MASK)
1161 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1162
1163 ret = IRQ_HANDLED;
1164 }
1165
1166 /* DP controller isr */
1167 ret |= msm_dp_ctrl_isr(dp->ctrl);
1168
1169 /* DP aux isr */
1170 ret |= msm_dp_aux_isr(dp->aux);
1171
1172 return ret;
1173 }
1174
msm_dp_display_request_irq(struct msm_dp_display_private * dp)1175 static int msm_dp_display_request_irq(struct msm_dp_display_private *dp)
1176 {
1177 int rc = 0;
1178 struct platform_device *pdev = dp->msm_dp_display.pdev;
1179
1180 dp->irq = platform_get_irq(pdev, 0);
1181 if (dp->irq < 0) {
1182 DRM_ERROR("failed to get irq\n");
1183 return dp->irq;
1184 }
1185
1186 rc = devm_request_irq(&pdev->dev, dp->irq, msm_dp_display_irq_handler,
1187 IRQF_TRIGGER_HIGH|IRQF_NO_AUTOEN,
1188 "dp_display_isr", dp);
1189
1190 if (rc < 0) {
1191 DRM_ERROR("failed to request IRQ%u: %d\n",
1192 dp->irq, rc);
1193 return rc;
1194 }
1195
1196 return 0;
1197 }
1198
msm_dp_display_get_desc(struct platform_device * pdev)1199 static const struct msm_dp_desc *msm_dp_display_get_desc(struct platform_device *pdev)
1200 {
1201 const struct msm_dp_desc *descs = of_device_get_match_data(&pdev->dev);
1202 struct resource *res;
1203 int i;
1204
1205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1206 if (!res)
1207 return NULL;
1208
1209 for (i = 0; i < descs[i].io_start; i++) {
1210 if (descs[i].io_start == res->start)
1211 return &descs[i];
1212 }
1213
1214 dev_err(&pdev->dev, "unknown displayport instance\n");
1215 return NULL;
1216 }
1217
msm_dp_display_probe_tail(struct device * dev)1218 static int msm_dp_display_probe_tail(struct device *dev)
1219 {
1220 struct msm_dp *dp = dev_get_drvdata(dev);
1221 int ret;
1222
1223 /*
1224 * External bridges are mandatory for eDP interfaces: one has to
1225 * provide at least an eDP panel (which gets wrapped into panel-bridge).
1226 *
1227 * For DisplayPort interfaces external bridges are optional, so
1228 * silently ignore an error if one is not present (-ENODEV).
1229 */
1230 dp->next_bridge = devm_drm_of_get_bridge(&dp->pdev->dev, dp->pdev->dev.of_node, 1, 0);
1231 if (IS_ERR(dp->next_bridge)) {
1232 ret = PTR_ERR(dp->next_bridge);
1233 dp->next_bridge = NULL;
1234 if (dp->is_edp || ret != -ENODEV)
1235 return ret;
1236 }
1237
1238 ret = component_add(dev, &msm_dp_display_comp_ops);
1239 if (ret)
1240 DRM_ERROR("component add failed, rc=%d\n", ret);
1241
1242 return ret;
1243 }
1244
msm_dp_auxbus_done_probe(struct drm_dp_aux * aux)1245 static int msm_dp_auxbus_done_probe(struct drm_dp_aux *aux)
1246 {
1247 return msm_dp_display_probe_tail(aux->dev);
1248 }
1249
msm_dp_display_get_connector_type(struct platform_device * pdev,const struct msm_dp_desc * desc)1250 static int msm_dp_display_get_connector_type(struct platform_device *pdev,
1251 const struct msm_dp_desc *desc)
1252 {
1253 struct device_node *node = pdev->dev.of_node;
1254 struct device_node *aux_bus = of_get_child_by_name(node, "aux-bus");
1255 struct device_node *panel = of_get_child_by_name(aux_bus, "panel");
1256 int connector_type;
1257
1258 if (panel)
1259 connector_type = DRM_MODE_CONNECTOR_eDP;
1260 else
1261 connector_type = DRM_MODE_SUBCONNECTOR_DisplayPort;
1262
1263 of_node_put(panel);
1264 of_node_put(aux_bus);
1265
1266 return connector_type;
1267 }
1268
msm_dp_display_probe(struct platform_device * pdev)1269 static int msm_dp_display_probe(struct platform_device *pdev)
1270 {
1271 int rc = 0;
1272 struct msm_dp_display_private *dp;
1273 const struct msm_dp_desc *desc;
1274
1275 if (!pdev || !pdev->dev.of_node) {
1276 DRM_ERROR("pdev not found\n");
1277 return -ENODEV;
1278 }
1279
1280 dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1281 if (!dp)
1282 return -ENOMEM;
1283
1284 desc = msm_dp_display_get_desc(pdev);
1285 if (!desc)
1286 return -EINVAL;
1287
1288 dp->msm_dp_display.pdev = pdev;
1289 dp->id = desc->id;
1290 dp->msm_dp_display.connector_type = msm_dp_display_get_connector_type(pdev, desc);
1291 dp->wide_bus_supported = desc->wide_bus_supported;
1292 dp->msm_dp_display.is_edp =
1293 (dp->msm_dp_display.connector_type == DRM_MODE_CONNECTOR_eDP);
1294
1295 rc = msm_dp_init_sub_modules(dp);
1296 if (rc) {
1297 DRM_ERROR("init sub module failed\n");
1298 return -EPROBE_DEFER;
1299 }
1300
1301 /* setup event q */
1302 mutex_init(&dp->event_mutex);
1303 init_waitqueue_head(&dp->event_q);
1304 spin_lock_init(&dp->event_lock);
1305
1306 /* Store DP audio handle inside DP display */
1307 dp->msm_dp_display.msm_dp_audio = dp->audio;
1308
1309 init_completion(&dp->audio_comp);
1310
1311 platform_set_drvdata(pdev, &dp->msm_dp_display);
1312
1313 rc = devm_pm_runtime_enable(&pdev->dev);
1314 if (rc)
1315 goto err;
1316
1317 rc = msm_dp_display_request_irq(dp);
1318 if (rc)
1319 goto err;
1320
1321 if (dp->msm_dp_display.is_edp) {
1322 rc = devm_of_dp_aux_populate_bus(dp->aux, msm_dp_auxbus_done_probe);
1323 if (rc) {
1324 DRM_ERROR("eDP auxbus population failed, rc=%d\n", rc);
1325 goto err;
1326 }
1327 } else {
1328 rc = msm_dp_display_probe_tail(&pdev->dev);
1329 if (rc)
1330 goto err;
1331 }
1332
1333 return rc;
1334
1335 err:
1336 msm_dp_display_deinit_sub_modules(dp);
1337 return rc;
1338 }
1339
msm_dp_display_remove(struct platform_device * pdev)1340 static void msm_dp_display_remove(struct platform_device *pdev)
1341 {
1342 struct msm_dp_display_private *dp = dev_get_dp_display_private(&pdev->dev);
1343
1344 component_del(&pdev->dev, &msm_dp_display_comp_ops);
1345 msm_dp_display_deinit_sub_modules(dp);
1346 platform_set_drvdata(pdev, NULL);
1347 }
1348
msm_dp_pm_runtime_suspend(struct device * dev)1349 static int msm_dp_pm_runtime_suspend(struct device *dev)
1350 {
1351 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1352
1353 disable_irq(dp->irq);
1354
1355 if (dp->msm_dp_display.is_edp) {
1356 msm_dp_display_host_phy_exit(dp);
1357 msm_dp_catalog_ctrl_hpd_disable(dp->catalog);
1358 }
1359 msm_dp_display_host_deinit(dp);
1360
1361 return 0;
1362 }
1363
msm_dp_pm_runtime_resume(struct device * dev)1364 static int msm_dp_pm_runtime_resume(struct device *dev)
1365 {
1366 struct msm_dp_display_private *dp = dev_get_dp_display_private(dev);
1367
1368 /*
1369 * for eDP, host cotroller, HPD block and PHY are enabled here
1370 * but with HPD irq disabled
1371 *
1372 * for DP, only host controller is enabled here.
1373 * HPD block is enabled at msm_dp_bridge_hpd_enable()
1374 * PHY will be enabled at plugin handler later
1375 */
1376 msm_dp_display_host_init(dp);
1377 if (dp->msm_dp_display.is_edp) {
1378 msm_dp_catalog_ctrl_hpd_enable(dp->catalog);
1379 msm_dp_display_host_phy_init(dp);
1380 }
1381
1382 enable_irq(dp->irq);
1383 return 0;
1384 }
1385
1386 static const struct dev_pm_ops msm_dp_pm_ops = {
1387 SET_RUNTIME_PM_OPS(msm_dp_pm_runtime_suspend, msm_dp_pm_runtime_resume, NULL)
1388 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1389 pm_runtime_force_resume)
1390 };
1391
1392 static struct platform_driver msm_dp_display_driver = {
1393 .probe = msm_dp_display_probe,
1394 .remove = msm_dp_display_remove,
1395 .driver = {
1396 .name = "msm-dp-display",
1397 .of_match_table = msm_dp_dt_match,
1398 .suppress_bind_attrs = true,
1399 .pm = &msm_dp_pm_ops,
1400 },
1401 };
1402
msm_dp_register(void)1403 int __init msm_dp_register(void)
1404 {
1405 int ret;
1406
1407 ret = platform_driver_register(&msm_dp_display_driver);
1408 if (ret)
1409 DRM_ERROR("Dp display driver register failed");
1410
1411 return ret;
1412 }
1413
msm_dp_unregister(void)1414 void __exit msm_dp_unregister(void)
1415 {
1416 platform_driver_unregister(&msm_dp_display_driver);
1417 }
1418
msm_dp_is_yuv_420_enabled(const struct msm_dp * msm_dp_display,const struct drm_display_mode * mode)1419 bool msm_dp_is_yuv_420_enabled(const struct msm_dp *msm_dp_display,
1420 const struct drm_display_mode *mode)
1421 {
1422 struct msm_dp_display_private *dp;
1423 const struct drm_display_info *info;
1424
1425 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1426 info = &msm_dp_display->connector->display_info;
1427
1428 return dp->panel->vsc_sdp_supported && drm_mode_is_420_only(info, mode);
1429 }
1430
msm_dp_needs_periph_flush(const struct msm_dp * msm_dp_display,const struct drm_display_mode * mode)1431 bool msm_dp_needs_periph_flush(const struct msm_dp *msm_dp_display,
1432 const struct drm_display_mode *mode)
1433 {
1434 return msm_dp_is_yuv_420_enabled(msm_dp_display, mode);
1435 }
1436
msm_dp_wide_bus_available(const struct msm_dp * msm_dp_display)1437 bool msm_dp_wide_bus_available(const struct msm_dp *msm_dp_display)
1438 {
1439 struct msm_dp_display_private *dp;
1440
1441 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1442
1443 if (dp->msm_dp_mode.out_fmt_is_yuv_420)
1444 return false;
1445
1446 return dp->wide_bus_supported;
1447 }
1448
msm_dp_display_debugfs_init(struct msm_dp * msm_dp_display,struct dentry * root,bool is_edp)1449 void msm_dp_display_debugfs_init(struct msm_dp *msm_dp_display, struct dentry *root, bool is_edp)
1450 {
1451 struct msm_dp_display_private *dp;
1452 struct device *dev;
1453 int rc;
1454
1455 dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1456 dev = &dp->msm_dp_display.pdev->dev;
1457
1458 rc = msm_dp_debug_init(dev, dp->panel, dp->link, dp->msm_dp_display.connector, root, is_edp);
1459 if (rc)
1460 DRM_ERROR("failed to initialize debug, rc = %d\n", rc);
1461 }
1462
msm_dp_modeset_init(struct msm_dp * msm_dp_display,struct drm_device * dev,struct drm_encoder * encoder,bool yuv_supported)1463 int msm_dp_modeset_init(struct msm_dp *msm_dp_display, struct drm_device *dev,
1464 struct drm_encoder *encoder, bool yuv_supported)
1465 {
1466 struct msm_dp_display_private *msm_dp_priv;
1467 int ret;
1468
1469 msm_dp_display->drm_dev = dev;
1470
1471 msm_dp_priv = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1472
1473 ret = msm_dp_bridge_init(msm_dp_display, dev, encoder, yuv_supported);
1474 if (ret) {
1475 DRM_DEV_ERROR(dev->dev,
1476 "failed to create dp bridge: %d\n", ret);
1477 return ret;
1478 }
1479
1480 msm_dp_display->connector = msm_dp_drm_connector_init(msm_dp_display, encoder);
1481 if (IS_ERR(msm_dp_display->connector)) {
1482 ret = PTR_ERR(msm_dp_display->connector);
1483 DRM_DEV_ERROR(dev->dev,
1484 "failed to create dp connector: %d\n", ret);
1485 msm_dp_display->connector = NULL;
1486 return ret;
1487 }
1488
1489 msm_dp_priv->panel->connector = msm_dp_display->connector;
1490
1491 return 0;
1492 }
1493
msm_dp_bridge_atomic_enable(struct drm_bridge * drm_bridge,struct drm_bridge_state * old_bridge_state)1494 void msm_dp_bridge_atomic_enable(struct drm_bridge *drm_bridge,
1495 struct drm_bridge_state *old_bridge_state)
1496 {
1497 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1498 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1499 int rc = 0;
1500 struct msm_dp_display_private *msm_dp_display;
1501 u32 state;
1502 bool force_link_train = false;
1503
1504 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1505 if (!msm_dp_display->msm_dp_mode.drm_mode.clock) {
1506 DRM_ERROR("invalid params\n");
1507 return;
1508 }
1509
1510 if (dp->is_edp)
1511 msm_dp_hpd_plug_handle(msm_dp_display, 0);
1512
1513 mutex_lock(&msm_dp_display->event_mutex);
1514 if (pm_runtime_resume_and_get(&dp->pdev->dev)) {
1515 DRM_ERROR("failed to pm_runtime_resume\n");
1516 mutex_unlock(&msm_dp_display->event_mutex);
1517 return;
1518 }
1519
1520 state = msm_dp_display->hpd_state;
1521 if (state != ST_DISPLAY_OFF && state != ST_MAINLINK_READY) {
1522 mutex_unlock(&msm_dp_display->event_mutex);
1523 return;
1524 }
1525
1526 rc = msm_dp_display_set_mode(dp, &msm_dp_display->msm_dp_mode);
1527 if (rc) {
1528 DRM_ERROR("Failed to perform a mode set, rc=%d\n", rc);
1529 mutex_unlock(&msm_dp_display->event_mutex);
1530 return;
1531 }
1532
1533 state = msm_dp_display->hpd_state;
1534
1535 if (state == ST_DISPLAY_OFF) {
1536 msm_dp_display_host_phy_init(msm_dp_display);
1537 force_link_train = true;
1538 }
1539
1540 msm_dp_display_enable(msm_dp_display, force_link_train);
1541
1542 rc = msm_dp_display_post_enable(dp);
1543 if (rc) {
1544 DRM_ERROR("DP display post enable failed, rc=%d\n", rc);
1545 msm_dp_display_disable(msm_dp_display);
1546 }
1547
1548 /* completed connection */
1549 msm_dp_display->hpd_state = ST_CONNECTED;
1550
1551 drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1552 mutex_unlock(&msm_dp_display->event_mutex);
1553 }
1554
msm_dp_bridge_atomic_disable(struct drm_bridge * drm_bridge,struct drm_bridge_state * old_bridge_state)1555 void msm_dp_bridge_atomic_disable(struct drm_bridge *drm_bridge,
1556 struct drm_bridge_state *old_bridge_state)
1557 {
1558 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1559 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1560 struct msm_dp_display_private *msm_dp_display;
1561
1562 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1563
1564 msm_dp_ctrl_push_idle(msm_dp_display->ctrl);
1565 }
1566
msm_dp_bridge_atomic_post_disable(struct drm_bridge * drm_bridge,struct drm_bridge_state * old_bridge_state)1567 void msm_dp_bridge_atomic_post_disable(struct drm_bridge *drm_bridge,
1568 struct drm_bridge_state *old_bridge_state)
1569 {
1570 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1571 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1572 u32 state;
1573 struct msm_dp_display_private *msm_dp_display;
1574
1575 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1576
1577 if (dp->is_edp)
1578 msm_dp_hpd_unplug_handle(msm_dp_display, 0);
1579
1580 mutex_lock(&msm_dp_display->event_mutex);
1581
1582 state = msm_dp_display->hpd_state;
1583 if (state != ST_DISCONNECT_PENDING && state != ST_CONNECTED)
1584 drm_dbg_dp(dp->drm_dev, "type=%d wrong hpd_state=%d\n",
1585 dp->connector_type, state);
1586
1587 msm_dp_display_disable(msm_dp_display);
1588
1589 state = msm_dp_display->hpd_state;
1590 if (state == ST_DISCONNECT_PENDING) {
1591 /* completed disconnection */
1592 msm_dp_display->hpd_state = ST_DISCONNECTED;
1593 } else {
1594 msm_dp_display->hpd_state = ST_DISPLAY_OFF;
1595 }
1596
1597 drm_dbg_dp(dp->drm_dev, "type=%d Done\n", dp->connector_type);
1598
1599 pm_runtime_put_sync(&dp->pdev->dev);
1600 mutex_unlock(&msm_dp_display->event_mutex);
1601 }
1602
msm_dp_bridge_mode_set(struct drm_bridge * drm_bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)1603 void msm_dp_bridge_mode_set(struct drm_bridge *drm_bridge,
1604 const struct drm_display_mode *mode,
1605 const struct drm_display_mode *adjusted_mode)
1606 {
1607 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(drm_bridge);
1608 struct msm_dp *dp = msm_dp_bridge->msm_dp_display;
1609 struct msm_dp_display_private *msm_dp_display;
1610 struct msm_dp_panel *msm_dp_panel;
1611
1612 msm_dp_display = container_of(dp, struct msm_dp_display_private, msm_dp_display);
1613 msm_dp_panel = msm_dp_display->panel;
1614
1615 memset(&msm_dp_display->msm_dp_mode, 0x0, sizeof(struct msm_dp_display_mode));
1616
1617 if (msm_dp_display_check_video_test(dp))
1618 msm_dp_display->msm_dp_mode.bpp = msm_dp_display_get_test_bpp(dp);
1619 else /* Default num_components per pixel = 3 */
1620 msm_dp_display->msm_dp_mode.bpp = dp->connector->display_info.bpc * 3;
1621
1622 if (!msm_dp_display->msm_dp_mode.bpp)
1623 msm_dp_display->msm_dp_mode.bpp = 24; /* Default bpp */
1624
1625 drm_mode_copy(&msm_dp_display->msm_dp_mode.drm_mode, adjusted_mode);
1626
1627 msm_dp_display->msm_dp_mode.v_active_low =
1628 !!(msm_dp_display->msm_dp_mode.drm_mode.flags & DRM_MODE_FLAG_NVSYNC);
1629
1630 msm_dp_display->msm_dp_mode.h_active_low =
1631 !!(msm_dp_display->msm_dp_mode.drm_mode.flags & DRM_MODE_FLAG_NHSYNC);
1632
1633 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 =
1634 drm_mode_is_420_only(&dp->connector->display_info, adjusted_mode) &&
1635 msm_dp_panel->vsc_sdp_supported;
1636
1637 /* populate wide_bus_support to different layers */
1638 msm_dp_display->ctrl->wide_bus_en =
1639 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 ? false : msm_dp_display->wide_bus_supported;
1640 msm_dp_display->catalog->wide_bus_en =
1641 msm_dp_display->msm_dp_mode.out_fmt_is_yuv_420 ? false : msm_dp_display->wide_bus_supported;
1642 }
1643
msm_dp_bridge_hpd_enable(struct drm_bridge * bridge)1644 void msm_dp_bridge_hpd_enable(struct drm_bridge *bridge)
1645 {
1646 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1647 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1648 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1649
1650 /*
1651 * this is for external DP with hpd irq enabled case,
1652 * step-1: msm_dp_pm_runtime_resume() enable dp host only
1653 * step-2: enable hdp block and have hpd irq enabled here
1654 * step-3: waiting for plugin irq while phy is not initialized
1655 * step-4: DP PHY is initialized at plugin handler before link training
1656 *
1657 */
1658 mutex_lock(&dp->event_mutex);
1659 if (pm_runtime_resume_and_get(&msm_dp_display->pdev->dev)) {
1660 DRM_ERROR("failed to resume power\n");
1661 mutex_unlock(&dp->event_mutex);
1662 return;
1663 }
1664
1665 msm_dp_catalog_ctrl_hpd_enable(dp->catalog);
1666
1667 /* enable HDP interrupts */
1668 msm_dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, true);
1669
1670 msm_dp_display->internal_hpd = true;
1671 mutex_unlock(&dp->event_mutex);
1672 }
1673
msm_dp_bridge_hpd_disable(struct drm_bridge * bridge)1674 void msm_dp_bridge_hpd_disable(struct drm_bridge *bridge)
1675 {
1676 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1677 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1678 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1679
1680 mutex_lock(&dp->event_mutex);
1681 /* disable HDP interrupts */
1682 msm_dp_catalog_hpd_config_intr(dp->catalog, DP_DP_HPD_INT_MASK, false);
1683 msm_dp_catalog_ctrl_hpd_disable(dp->catalog);
1684
1685 msm_dp_display->internal_hpd = false;
1686
1687 pm_runtime_put_sync(&msm_dp_display->pdev->dev);
1688 mutex_unlock(&dp->event_mutex);
1689 }
1690
msm_dp_bridge_hpd_notify(struct drm_bridge * bridge,enum drm_connector_status status)1691 void msm_dp_bridge_hpd_notify(struct drm_bridge *bridge,
1692 enum drm_connector_status status)
1693 {
1694 struct msm_dp_bridge *msm_dp_bridge = to_dp_bridge(bridge);
1695 struct msm_dp *msm_dp_display = msm_dp_bridge->msm_dp_display;
1696 struct msm_dp_display_private *dp = container_of(msm_dp_display, struct msm_dp_display_private, msm_dp_display);
1697
1698 /* Without next_bridge interrupts are handled by the DP core directly */
1699 if (msm_dp_display->internal_hpd)
1700 return;
1701
1702 if (!msm_dp_display->link_ready && status == connector_status_connected)
1703 msm_dp_add_event(dp, EV_HPD_PLUG_INT, 0, 0);
1704 else if (msm_dp_display->link_ready && status == connector_status_disconnected)
1705 msm_dp_add_event(dp, EV_HPD_UNPLUG_INT, 0, 0);
1706 }
1707