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