xref: /openbmc/linux/drivers/gpu/drm/msm/dsi/dsi_manager.c (revision 08b7cf13)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include "drm/drm_bridge_connector.h"
7 
8 #include "msm_kms.h"
9 #include "dsi.h"
10 
11 #define DSI_CLOCK_MASTER	DSI_0
12 #define DSI_CLOCK_SLAVE		DSI_1
13 
14 #define DSI_LEFT		DSI_0
15 #define DSI_RIGHT		DSI_1
16 
17 /* According to the current drm framework sequence, take the encoder of
18  * DSI_1 as master encoder
19  */
20 #define DSI_ENCODER_MASTER	DSI_1
21 #define DSI_ENCODER_SLAVE	DSI_0
22 
23 struct msm_dsi_manager {
24 	struct msm_dsi *dsi[DSI_MAX];
25 
26 	bool is_bonded_dsi;
27 	bool is_sync_needed;
28 	int master_dsi_link_id;
29 };
30 
31 static struct msm_dsi_manager msm_dsim_glb;
32 
33 #define IS_BONDED_DSI()		(msm_dsim_glb.is_bonded_dsi)
34 #define IS_SYNC_NEEDED()	(msm_dsim_glb.is_sync_needed)
35 #define IS_MASTER_DSI_LINK(id)	(msm_dsim_glb.master_dsi_link_id == id)
36 
37 static inline struct msm_dsi *dsi_mgr_get_dsi(int id)
38 {
39 	return msm_dsim_glb.dsi[id];
40 }
41 
42 static inline struct msm_dsi *dsi_mgr_get_other_dsi(int id)
43 {
44 	return msm_dsim_glb.dsi[(id + 1) % DSI_MAX];
45 }
46 
47 static int dsi_mgr_parse_of(struct device_node *np, int id)
48 {
49 	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
50 
51 	/* We assume 2 dsi nodes have the same information of bonded dsi and
52 	 * sync-mode, and only one node specifies master in case of bonded mode.
53 	 */
54 	if (!msm_dsim->is_bonded_dsi)
55 		msm_dsim->is_bonded_dsi = of_property_read_bool(np, "qcom,dual-dsi-mode");
56 
57 	if (msm_dsim->is_bonded_dsi) {
58 		if (of_property_read_bool(np, "qcom,master-dsi"))
59 			msm_dsim->master_dsi_link_id = id;
60 		if (!msm_dsim->is_sync_needed)
61 			msm_dsim->is_sync_needed = of_property_read_bool(
62 					np, "qcom,sync-dual-dsi");
63 	}
64 
65 	return 0;
66 }
67 
68 static int dsi_mgr_setup_components(int id)
69 {
70 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
71 	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
72 	struct msm_dsi *clk_master_dsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
73 	struct msm_dsi *clk_slave_dsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
74 	int ret;
75 
76 	if (!IS_BONDED_DSI()) {
77 		ret = msm_dsi_host_register(msm_dsi->host);
78 		if (ret)
79 			return ret;
80 
81 		msm_dsi_phy_set_usecase(msm_dsi->phy, MSM_DSI_PHY_STANDALONE);
82 		msm_dsi_host_set_phy_mode(msm_dsi->host, msm_dsi->phy);
83 	} else if (other_dsi) {
84 		struct msm_dsi *master_link_dsi = IS_MASTER_DSI_LINK(id) ?
85 							msm_dsi : other_dsi;
86 		struct msm_dsi *slave_link_dsi = IS_MASTER_DSI_LINK(id) ?
87 							other_dsi : msm_dsi;
88 		/* Register slave host first, so that slave DSI device
89 		 * has a chance to probe, and do not block the master
90 		 * DSI device's probe.
91 		 * Also, do not check defer for the slave host,
92 		 * because only master DSI device adds the panel to global
93 		 * panel list. The panel's device is the master DSI device.
94 		 */
95 		ret = msm_dsi_host_register(slave_link_dsi->host);
96 		if (ret)
97 			return ret;
98 		ret = msm_dsi_host_register(master_link_dsi->host);
99 		if (ret)
100 			return ret;
101 
102 		/* PLL0 is to drive both 2 DSI link clocks in bonded DSI mode. */
103 		msm_dsi_phy_set_usecase(clk_master_dsi->phy,
104 					MSM_DSI_PHY_MASTER);
105 		msm_dsi_phy_set_usecase(clk_slave_dsi->phy,
106 					MSM_DSI_PHY_SLAVE);
107 		msm_dsi_host_set_phy_mode(msm_dsi->host, msm_dsi->phy);
108 		msm_dsi_host_set_phy_mode(other_dsi->host, other_dsi->phy);
109 	}
110 
111 	return 0;
112 }
113 
114 static int enable_phy(struct msm_dsi *msm_dsi,
115 		      struct msm_dsi_phy_shared_timings *shared_timings)
116 {
117 	struct msm_dsi_phy_clk_request clk_req;
118 	int ret;
119 	bool is_bonded_dsi = IS_BONDED_DSI();
120 
121 	msm_dsi_host_get_phy_clk_req(msm_dsi->host, &clk_req, is_bonded_dsi);
122 
123 	ret = msm_dsi_phy_enable(msm_dsi->phy, &clk_req, shared_timings);
124 
125 	return ret;
126 }
127 
128 static int
129 dsi_mgr_phy_enable(int id,
130 		   struct msm_dsi_phy_shared_timings shared_timings[DSI_MAX])
131 {
132 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
133 	struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
134 	struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
135 	int ret;
136 
137 	/* In case of bonded DSI, some registers in PHY1 have been programmed
138 	 * during PLL0 clock's set_rate. The PHY1 reset called by host1 here
139 	 * will silently reset those PHY1 registers. Therefore we need to reset
140 	 * and enable both PHYs before any PLL clock operation.
141 	 */
142 	if (IS_BONDED_DSI() && mdsi && sdsi) {
143 		if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
144 			msm_dsi_host_reset_phy(mdsi->host);
145 			msm_dsi_host_reset_phy(sdsi->host);
146 
147 			ret = enable_phy(mdsi,
148 					 &shared_timings[DSI_CLOCK_MASTER]);
149 			if (ret)
150 				return ret;
151 			ret = enable_phy(sdsi,
152 					 &shared_timings[DSI_CLOCK_SLAVE]);
153 			if (ret) {
154 				msm_dsi_phy_disable(mdsi->phy);
155 				return ret;
156 			}
157 		}
158 	} else {
159 		msm_dsi_host_reset_phy(msm_dsi->host);
160 		ret = enable_phy(msm_dsi, &shared_timings[id]);
161 		if (ret)
162 			return ret;
163 	}
164 
165 	msm_dsi->phy_enabled = true;
166 
167 	return 0;
168 }
169 
170 static void dsi_mgr_phy_disable(int id)
171 {
172 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
173 	struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
174 	struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
175 
176 	/* disable DSI phy
177 	 * In bonded dsi configuration, the phy should be disabled for the
178 	 * first controller only when the second controller is disabled.
179 	 */
180 	msm_dsi->phy_enabled = false;
181 	if (IS_BONDED_DSI() && mdsi && sdsi) {
182 		if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
183 			msm_dsi_phy_disable(sdsi->phy);
184 			msm_dsi_phy_disable(mdsi->phy);
185 		}
186 	} else {
187 		msm_dsi_phy_disable(msm_dsi->phy);
188 	}
189 }
190 
191 struct dsi_connector {
192 	struct drm_connector base;
193 	int id;
194 };
195 
196 struct dsi_bridge {
197 	struct drm_bridge base;
198 	int id;
199 };
200 
201 #define to_dsi_connector(x) container_of(x, struct dsi_connector, base)
202 #define to_dsi_bridge(x) container_of(x, struct dsi_bridge, base)
203 
204 static inline int dsi_mgr_connector_get_id(struct drm_connector *connector)
205 {
206 	struct dsi_connector *dsi_connector = to_dsi_connector(connector);
207 	return dsi_connector->id;
208 }
209 
210 static int dsi_mgr_bridge_get_id(struct drm_bridge *bridge)
211 {
212 	struct dsi_bridge *dsi_bridge = to_dsi_bridge(bridge);
213 	return dsi_bridge->id;
214 }
215 
216 static int msm_dsi_manager_panel_init(struct drm_connector *conn, u8 id)
217 {
218 	struct msm_drm_private *priv = conn->dev->dev_private;
219 	struct msm_kms *kms = priv->kms;
220 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
221 	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
222 	struct msm_dsi *master_dsi, *slave_dsi;
223 	struct drm_panel *panel;
224 
225 	if (IS_BONDED_DSI() && !IS_MASTER_DSI_LINK(id)) {
226 		master_dsi = other_dsi;
227 		slave_dsi = msm_dsi;
228 	} else {
229 		master_dsi = msm_dsi;
230 		slave_dsi = other_dsi;
231 	}
232 
233 	/*
234 	 * There is only 1 panel in the global panel list for bonded DSI mode.
235 	 * Therefore slave dsi should get the drm_panel instance from master
236 	 * dsi.
237 	 */
238 	panel = msm_dsi_host_get_panel(master_dsi->host);
239 	if (IS_ERR(panel)) {
240 		DRM_ERROR("Could not find panel for %u (%ld)\n", msm_dsi->id,
241 			  PTR_ERR(panel));
242 		return PTR_ERR(panel);
243 	}
244 
245 	if (!panel || !IS_BONDED_DSI())
246 		goto out;
247 
248 	drm_object_attach_property(&conn->base,
249 				   conn->dev->mode_config.tile_property, 0);
250 
251 	/*
252 	 * Set split display info to kms once bonded DSI panel is connected to
253 	 * both hosts.
254 	 */
255 	if (other_dsi && other_dsi->panel && kms->funcs->set_split_display) {
256 		kms->funcs->set_split_display(kms, master_dsi->encoder,
257 					      slave_dsi->encoder,
258 					      msm_dsi_is_cmd_mode(msm_dsi));
259 	}
260 
261 out:
262 	msm_dsi->panel = panel;
263 	return 0;
264 }
265 
266 static enum drm_connector_status dsi_mgr_connector_detect(
267 		struct drm_connector *connector, bool force)
268 {
269 	int id = dsi_mgr_connector_get_id(connector);
270 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
271 
272 	return msm_dsi->panel ? connector_status_connected :
273 		connector_status_disconnected;
274 }
275 
276 static void dsi_mgr_connector_destroy(struct drm_connector *connector)
277 {
278 	struct dsi_connector *dsi_connector = to_dsi_connector(connector);
279 
280 	DBG("");
281 
282 	drm_connector_cleanup(connector);
283 
284 	kfree(dsi_connector);
285 }
286 
287 static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
288 {
289 	int id = dsi_mgr_connector_get_id(connector);
290 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
291 	struct drm_panel *panel = msm_dsi->panel;
292 	int num;
293 
294 	if (!panel)
295 		return 0;
296 
297 	/*
298 	 * In bonded DSI mode, we have one connector that can be
299 	 * attached to the drm_panel.
300 	 */
301 	num = drm_panel_get_modes(panel, connector);
302 	if (!num)
303 		return 0;
304 
305 	return num;
306 }
307 
308 static struct drm_encoder *
309 dsi_mgr_connector_best_encoder(struct drm_connector *connector)
310 {
311 	int id = dsi_mgr_connector_get_id(connector);
312 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
313 
314 	DBG("");
315 	return msm_dsi_get_encoder(msm_dsi);
316 }
317 
318 static void dsi_mgr_bridge_power_on(struct drm_bridge *bridge)
319 {
320 	int id = dsi_mgr_bridge_get_id(bridge);
321 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
322 	struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
323 	struct mipi_dsi_host *host = msm_dsi->host;
324 	struct msm_dsi_phy_shared_timings phy_shared_timings[DSI_MAX];
325 	bool is_bonded_dsi = IS_BONDED_DSI();
326 	int ret;
327 
328 	DBG("id=%d", id);
329 	if (!msm_dsi_device_connected(msm_dsi))
330 		return;
331 
332 	/* Do nothing with the host if it is slave-DSI in case of bonded DSI */
333 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
334 		return;
335 
336 	ret = dsi_mgr_phy_enable(id, phy_shared_timings);
337 	if (ret)
338 		goto phy_en_fail;
339 
340 	ret = msm_dsi_host_power_on(host, &phy_shared_timings[id], is_bonded_dsi, msm_dsi->phy);
341 	if (ret) {
342 		pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
343 		goto host_on_fail;
344 	}
345 
346 	if (is_bonded_dsi && msm_dsi1) {
347 		ret = msm_dsi_host_power_on(msm_dsi1->host,
348 				&phy_shared_timings[DSI_1], is_bonded_dsi, msm_dsi1->phy);
349 		if (ret) {
350 			pr_err("%s: power on host1 failed, %d\n",
351 							__func__, ret);
352 			goto host1_on_fail;
353 		}
354 	}
355 
356 	/*
357 	 * Enable before preparing the panel, disable after unpreparing, so
358 	 * that the panel can communicate over the DSI link.
359 	 */
360 	msm_dsi_host_enable_irq(host);
361 	if (is_bonded_dsi && msm_dsi1)
362 		msm_dsi_host_enable_irq(msm_dsi1->host);
363 
364 	return;
365 
366 host1_on_fail:
367 	msm_dsi_host_power_off(host);
368 host_on_fail:
369 	dsi_mgr_phy_disable(id);
370 phy_en_fail:
371 	return;
372 }
373 
374 static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
375 {
376 	int id = dsi_mgr_bridge_get_id(bridge);
377 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
378 	struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
379 	struct mipi_dsi_host *host = msm_dsi->host;
380 	struct drm_panel *panel = msm_dsi->panel;
381 	bool is_bonded_dsi = IS_BONDED_DSI();
382 	int ret;
383 
384 	DBG("id=%d", id);
385 	if (!msm_dsi_device_connected(msm_dsi))
386 		return;
387 
388 	/* Do nothing with the host if it is slave-DSI in case of bonded DSI */
389 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
390 		return;
391 
392 	/* Always call panel functions once, because even for dual panels,
393 	 * there is only one drm_panel instance.
394 	 */
395 	if (panel) {
396 		ret = drm_panel_prepare(panel);
397 		if (ret) {
398 			pr_err("%s: prepare panel %d failed, %d\n", __func__,
399 								id, ret);
400 			goto panel_prep_fail;
401 		}
402 	}
403 
404 	ret = msm_dsi_host_enable(host);
405 	if (ret) {
406 		pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
407 		goto host_en_fail;
408 	}
409 
410 	if (is_bonded_dsi && msm_dsi1) {
411 		ret = msm_dsi_host_enable(msm_dsi1->host);
412 		if (ret) {
413 			pr_err("%s: enable host1 failed, %d\n", __func__, ret);
414 			goto host1_en_fail;
415 		}
416 	}
417 
418 	return;
419 
420 host1_en_fail:
421 	msm_dsi_host_disable(host);
422 host_en_fail:
423 	if (panel)
424 		drm_panel_unprepare(panel);
425 panel_prep_fail:
426 
427 	return;
428 }
429 
430 void msm_dsi_manager_tpg_enable(void)
431 {
432 	struct msm_dsi *m_dsi = dsi_mgr_get_dsi(DSI_0);
433 	struct msm_dsi *s_dsi = dsi_mgr_get_dsi(DSI_1);
434 
435 	/* if dual dsi, trigger tpg on master first then slave */
436 	if (m_dsi) {
437 		msm_dsi_host_test_pattern_en(m_dsi->host);
438 		if (IS_BONDED_DSI() && s_dsi)
439 			msm_dsi_host_test_pattern_en(s_dsi->host);
440 	}
441 }
442 
443 static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
444 {
445 	int id = dsi_mgr_bridge_get_id(bridge);
446 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
447 	struct drm_panel *panel = msm_dsi->panel;
448 	bool is_bonded_dsi = IS_BONDED_DSI();
449 	int ret;
450 
451 	DBG("id=%d", id);
452 	if (!msm_dsi_device_connected(msm_dsi))
453 		return;
454 
455 	/* Do nothing with the host if it is slave-DSI in case of bonded DSI */
456 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
457 		return;
458 
459 	if (panel) {
460 		ret = drm_panel_enable(panel);
461 		if (ret) {
462 			pr_err("%s: enable panel %d failed, %d\n", __func__, id,
463 									ret);
464 		}
465 	}
466 }
467 
468 static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
469 {
470 	int id = dsi_mgr_bridge_get_id(bridge);
471 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
472 	struct drm_panel *panel = msm_dsi->panel;
473 	bool is_bonded_dsi = IS_BONDED_DSI();
474 	int ret;
475 
476 	DBG("id=%d", id);
477 	if (!msm_dsi_device_connected(msm_dsi))
478 		return;
479 
480 	/* Do nothing with the host if it is slave-DSI in case of bonded DSI */
481 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
482 		return;
483 
484 	if (panel) {
485 		ret = drm_panel_disable(panel);
486 		if (ret)
487 			pr_err("%s: Panel %d OFF failed, %d\n", __func__, id,
488 									ret);
489 	}
490 }
491 
492 static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
493 {
494 	int id = dsi_mgr_bridge_get_id(bridge);
495 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
496 	struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
497 	struct mipi_dsi_host *host = msm_dsi->host;
498 	struct drm_panel *panel = msm_dsi->panel;
499 	bool is_bonded_dsi = IS_BONDED_DSI();
500 	int ret;
501 
502 	DBG("id=%d", id);
503 
504 	if (!msm_dsi_device_connected(msm_dsi))
505 		return;
506 
507 	/*
508 	 * Do nothing with the host if it is slave-DSI in case of bonded DSI.
509 	 * It is safe to call dsi_mgr_phy_disable() here because a single PHY
510 	 * won't be diabled until both PHYs request disable.
511 	 */
512 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
513 		goto disable_phy;
514 
515 	ret = msm_dsi_host_disable(host);
516 	if (ret)
517 		pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);
518 
519 	if (is_bonded_dsi && msm_dsi1) {
520 		ret = msm_dsi_host_disable(msm_dsi1->host);
521 		if (ret)
522 			pr_err("%s: host1 disable failed, %d\n", __func__, ret);
523 	}
524 
525 	if (panel) {
526 		ret = drm_panel_unprepare(panel);
527 		if (ret)
528 			pr_err("%s: Panel %d unprepare failed,%d\n", __func__,
529 								id, ret);
530 	}
531 
532 	msm_dsi_host_disable_irq(host);
533 	if (is_bonded_dsi && msm_dsi1)
534 		msm_dsi_host_disable_irq(msm_dsi1->host);
535 
536 	/* Save PHY status if it is a clock source */
537 	msm_dsi_phy_pll_save_state(msm_dsi->phy);
538 
539 	ret = msm_dsi_host_power_off(host);
540 	if (ret)
541 		pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);
542 
543 	if (is_bonded_dsi && msm_dsi1) {
544 		ret = msm_dsi_host_power_off(msm_dsi1->host);
545 		if (ret)
546 			pr_err("%s: host1 power off failed, %d\n",
547 								__func__, ret);
548 	}
549 
550 disable_phy:
551 	dsi_mgr_phy_disable(id);
552 }
553 
554 static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
555 		const struct drm_display_mode *mode,
556 		const struct drm_display_mode *adjusted_mode)
557 {
558 	int id = dsi_mgr_bridge_get_id(bridge);
559 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
560 	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
561 	struct mipi_dsi_host *host = msm_dsi->host;
562 	bool is_bonded_dsi = IS_BONDED_DSI();
563 
564 	DBG("set mode: " DRM_MODE_FMT, DRM_MODE_ARG(mode));
565 
566 	if (is_bonded_dsi && !IS_MASTER_DSI_LINK(id))
567 		return;
568 
569 	msm_dsi_host_set_display_mode(host, adjusted_mode);
570 	if (is_bonded_dsi && other_dsi)
571 		msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
572 
573 	dsi_mgr_bridge_power_on(bridge);
574 }
575 
576 static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
577 	.detect = dsi_mgr_connector_detect,
578 	.fill_modes = drm_helper_probe_single_connector_modes,
579 	.destroy = dsi_mgr_connector_destroy,
580 	.reset = drm_atomic_helper_connector_reset,
581 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
582 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
583 };
584 
585 static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
586 	.get_modes = dsi_mgr_connector_get_modes,
587 	.best_encoder = dsi_mgr_connector_best_encoder,
588 };
589 
590 static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
591 	.pre_enable = dsi_mgr_bridge_pre_enable,
592 	.enable = dsi_mgr_bridge_enable,
593 	.disable = dsi_mgr_bridge_disable,
594 	.post_disable = dsi_mgr_bridge_post_disable,
595 	.mode_set = dsi_mgr_bridge_mode_set,
596 };
597 
598 /* initialize connector when we're connected to a drm_panel */
599 struct drm_connector *msm_dsi_manager_connector_init(u8 id)
600 {
601 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
602 	struct drm_connector *connector = NULL;
603 	struct dsi_connector *dsi_connector;
604 	int ret;
605 
606 	dsi_connector = kzalloc(sizeof(*dsi_connector), GFP_KERNEL);
607 	if (!dsi_connector)
608 		return ERR_PTR(-ENOMEM);
609 
610 	dsi_connector->id = id;
611 
612 	connector = &dsi_connector->base;
613 
614 	ret = drm_connector_init(msm_dsi->dev, connector,
615 			&dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
616 	if (ret)
617 		return ERR_PTR(ret);
618 
619 	drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);
620 
621 	/* Enable HPD to let hpd event is handled
622 	 * when panel is attached to the host.
623 	 */
624 	connector->polled = DRM_CONNECTOR_POLL_HPD;
625 
626 	/* Display driver doesn't support interlace now. */
627 	connector->interlace_allowed = 0;
628 	connector->doublescan_allowed = 0;
629 
630 	drm_connector_attach_encoder(connector, msm_dsi->encoder);
631 
632 	ret = msm_dsi_manager_panel_init(connector, id);
633 	if (ret) {
634 		DRM_DEV_ERROR(msm_dsi->dev->dev, "init panel failed %d\n", ret);
635 		goto fail;
636 	}
637 
638 	return connector;
639 
640 fail:
641 	connector->funcs->destroy(msm_dsi->connector);
642 	return ERR_PTR(ret);
643 }
644 
645 /* initialize bridge */
646 struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
647 {
648 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
649 	struct drm_bridge *bridge = NULL;
650 	struct dsi_bridge *dsi_bridge;
651 	struct drm_encoder *encoder;
652 	int ret;
653 
654 	dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
655 				sizeof(*dsi_bridge), GFP_KERNEL);
656 	if (!dsi_bridge) {
657 		ret = -ENOMEM;
658 		goto fail;
659 	}
660 
661 	dsi_bridge->id = id;
662 
663 	encoder = msm_dsi->encoder;
664 
665 	bridge = &dsi_bridge->base;
666 	bridge->funcs = &dsi_mgr_bridge_funcs;
667 
668 	ret = drm_bridge_attach(encoder, bridge, NULL, 0);
669 	if (ret)
670 		goto fail;
671 
672 	return bridge;
673 
674 fail:
675 	if (bridge)
676 		msm_dsi_manager_bridge_destroy(bridge);
677 
678 	return ERR_PTR(ret);
679 }
680 
681 struct drm_connector *msm_dsi_manager_ext_bridge_init(u8 id)
682 {
683 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
684 	struct drm_device *dev = msm_dsi->dev;
685 	struct drm_connector *connector;
686 	struct drm_encoder *encoder;
687 	struct drm_bridge *int_bridge, *ext_bridge;
688 	int ret;
689 
690 	int_bridge = msm_dsi->bridge;
691 	ext_bridge = msm_dsi->external_bridge =
692 			msm_dsi_host_get_bridge(msm_dsi->host);
693 
694 	encoder = msm_dsi->encoder;
695 
696 	/*
697 	 * Try first to create the bridge without it creating its own
698 	 * connector.. currently some bridges support this, and others
699 	 * do not (and some support both modes)
700 	 */
701 	ret = drm_bridge_attach(encoder, ext_bridge, int_bridge,
702 			DRM_BRIDGE_ATTACH_NO_CONNECTOR);
703 	if (ret == -EINVAL) {
704 		struct drm_connector *connector;
705 		struct list_head *connector_list;
706 
707 		/* link the internal dsi bridge to the external bridge */
708 		drm_bridge_attach(encoder, ext_bridge, int_bridge, 0);
709 
710 		/*
711 		 * we need the drm_connector created by the external bridge
712 		 * driver (or someone else) to feed it to our driver's
713 		 * priv->connector[] list, mainly for msm_fbdev_init()
714 		 */
715 		connector_list = &dev->mode_config.connector_list;
716 
717 		list_for_each_entry(connector, connector_list, head) {
718 			if (drm_connector_has_possible_encoder(connector, encoder))
719 				return connector;
720 		}
721 
722 		return ERR_PTR(-ENODEV);
723 	}
724 
725 	connector = drm_bridge_connector_init(dev, encoder);
726 	if (IS_ERR(connector)) {
727 		DRM_ERROR("Unable to create bridge connector\n");
728 		return ERR_CAST(connector);
729 	}
730 
731 	drm_connector_attach_encoder(connector, encoder);
732 
733 	return connector;
734 }
735 
736 void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
737 {
738 }
739 
740 int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
741 {
742 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
743 	struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
744 	struct mipi_dsi_host *host = msm_dsi->host;
745 	bool is_read = (msg->rx_buf && msg->rx_len);
746 	bool need_sync = (IS_SYNC_NEEDED() && !is_read);
747 	int ret;
748 
749 	if (!msg->tx_buf || !msg->tx_len)
750 		return 0;
751 
752 	/* In bonded master case, panel requires the same commands sent to
753 	 * both DSI links. Host issues the command trigger to both links
754 	 * when DSI_1 calls the cmd transfer function, no matter it happens
755 	 * before or after DSI_0 cmd transfer.
756 	 */
757 	if (need_sync && (id == DSI_0))
758 		return is_read ? msg->rx_len : msg->tx_len;
759 
760 	if (need_sync && msm_dsi0) {
761 		ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
762 		if (ret) {
763 			pr_err("%s: failed to prepare non-trigger host, %d\n",
764 				__func__, ret);
765 			return ret;
766 		}
767 	}
768 	ret = msm_dsi_host_xfer_prepare(host, msg);
769 	if (ret) {
770 		pr_err("%s: failed to prepare host, %d\n", __func__, ret);
771 		goto restore_host0;
772 	}
773 
774 	ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
775 			msm_dsi_host_cmd_tx(host, msg);
776 
777 	msm_dsi_host_xfer_restore(host, msg);
778 
779 restore_host0:
780 	if (need_sync && msm_dsi0)
781 		msm_dsi_host_xfer_restore(msm_dsi0->host, msg);
782 
783 	return ret;
784 }
785 
786 bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len)
787 {
788 	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
789 	struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
790 	struct mipi_dsi_host *host = msm_dsi->host;
791 
792 	if (IS_SYNC_NEEDED() && (id == DSI_0))
793 		return false;
794 
795 	if (IS_SYNC_NEEDED() && msm_dsi0)
796 		msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, dma_base, len);
797 
798 	msm_dsi_host_cmd_xfer_commit(host, dma_base, len);
799 
800 	return true;
801 }
802 
803 int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
804 {
805 	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
806 	int id = msm_dsi->id;
807 	int ret;
808 
809 	if (id >= DSI_MAX) {
810 		pr_err("%s: invalid id %d\n", __func__, id);
811 		return -EINVAL;
812 	}
813 
814 	if (msm_dsim->dsi[id]) {
815 		pr_err("%s: dsi%d already registered\n", __func__, id);
816 		return -EBUSY;
817 	}
818 
819 	msm_dsim->dsi[id] = msm_dsi;
820 
821 	ret = dsi_mgr_parse_of(msm_dsi->pdev->dev.of_node, id);
822 	if (ret) {
823 		pr_err("%s: failed to parse OF DSI info\n", __func__);
824 		goto fail;
825 	}
826 
827 	ret = dsi_mgr_setup_components(id);
828 	if (ret) {
829 		pr_err("%s: failed to register mipi dsi host for DSI %d: %d\n",
830 			__func__, id, ret);
831 		goto fail;
832 	}
833 
834 	return 0;
835 
836 fail:
837 	msm_dsim->dsi[id] = NULL;
838 	return ret;
839 }
840 
841 void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
842 {
843 	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
844 
845 	if (msm_dsi->host)
846 		msm_dsi_host_unregister(msm_dsi->host);
847 
848 	if (msm_dsi->id >= 0)
849 		msm_dsim->dsi[msm_dsi->id] = NULL;
850 }
851 
852 bool msm_dsi_is_bonded_dsi(struct msm_dsi *msm_dsi)
853 {
854 	return IS_BONDED_DSI();
855 }
856 
857 bool msm_dsi_is_master_dsi(struct msm_dsi *msm_dsi)
858 {
859 	return IS_MASTER_DSI_LINK(msm_dsi->id);
860 }
861