dsi_manager.c 22.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "msm_kms.h"
#include "dsi.h"

17 18 19 20 21 22 23 24 25 26 27 28
#define DSI_CLOCK_MASTER	DSI_0
#define DSI_CLOCK_SLAVE		DSI_1

#define DSI_LEFT		DSI_0
#define DSI_RIGHT		DSI_1

/* According to the current drm framework sequence, take the encoder of
 * DSI_1 as master encoder
 */
#define DSI_ENCODER_MASTER	DSI_1
#define DSI_ENCODER_SLAVE	DSI_0

29 30 31
struct msm_dsi_manager {
	struct msm_dsi *dsi[DSI_MAX];

32
	bool is_dual_dsi;
33
	bool is_sync_needed;
34
	int master_dsi_link_id;
35 36 37 38
};

static struct msm_dsi_manager msm_dsim_glb;

39
#define IS_DUAL_DSI()		(msm_dsim_glb.is_dual_dsi)
40
#define IS_SYNC_NEEDED()	(msm_dsim_glb.is_sync_needed)
41
#define IS_MASTER_DSI_LINK(id)	(msm_dsim_glb.master_dsi_link_id == id)
42 43 44 45 46 47 48 49 50 51 52

static inline struct msm_dsi *dsi_mgr_get_dsi(int id)
{
	return msm_dsim_glb.dsi[id];
}

static inline struct msm_dsi *dsi_mgr_get_other_dsi(int id)
{
	return msm_dsim_glb.dsi[(id + 1) % DSI_MAX];
}

53
static int dsi_mgr_parse_dual_dsi(struct device_node *np, int id)
54 55 56
{
	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;

57
	/* We assume 2 dsi nodes have the same information of dual-dsi and
58 59
	 * sync-mode, and only one node specifies master in case of dual mode.
	 */
60 61 62
	if (!msm_dsim->is_dual_dsi)
		msm_dsim->is_dual_dsi = of_property_read_bool(
						np, "qcom,dual-dsi-mode");
63

64 65 66
	if (msm_dsim->is_dual_dsi) {
		if (of_property_read_bool(np, "qcom,master-dsi"))
			msm_dsim->master_dsi_link_id = id;
67 68
		if (!msm_dsim->is_sync_needed)
			msm_dsim->is_sync_needed = of_property_read_bool(
69
					np, "qcom,sync-dual-dsi");
70 71 72 73 74
	}

	return 0;
}

H
Hai Li 已提交
75
static int dsi_mgr_setup_components(int id)
76 77 78 79
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
	struct msm_dsi *clk_master_dsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
H
Hai Li 已提交
80
	struct msm_dsi *clk_slave_dsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
81 82 83
	struct msm_dsi_pll *src_pll;
	int ret;

84
	if (!IS_DUAL_DSI()) {
85 86 87 88
		ret = msm_dsi_host_register(msm_dsi->host, true);
		if (ret)
			return ret;

H
Hai Li 已提交
89
		msm_dsi_phy_set_usecase(msm_dsi->phy, MSM_DSI_PHY_STANDALONE);
90
		src_pll = msm_dsi_phy_get_pll(msm_dsi->phy);
91 92
		if (IS_ERR(src_pll))
			return PTR_ERR(src_pll);
93 94 95 96
		ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
	} else if (!other_dsi) {
		ret = 0;
	} else {
H
Hai Li 已提交
97 98 99 100
		struct msm_dsi *master_link_dsi = IS_MASTER_DSI_LINK(id) ?
							msm_dsi : other_dsi;
		struct msm_dsi *slave_link_dsi = IS_MASTER_DSI_LINK(id) ?
							other_dsi : msm_dsi;
101 102 103 104 105 106 107
		/* Register slave host first, so that slave DSI device
		 * has a chance to probe, and do not block the master
		 * DSI device's probe.
		 * Also, do not check defer for the slave host,
		 * because only master DSI device adds the panel to global
		 * panel list. The panel's device is the master DSI device.
		 */
H
Hai Li 已提交
108
		ret = msm_dsi_host_register(slave_link_dsi->host, false);
109 110
		if (ret)
			return ret;
H
Hai Li 已提交
111
		ret = msm_dsi_host_register(master_link_dsi->host, true);
112 113 114 115
		if (ret)
			return ret;

		/* PLL0 is to drive both 2 DSI link clocks in Dual DSI mode. */
H
Hai Li 已提交
116 117 118 119
		msm_dsi_phy_set_usecase(clk_master_dsi->phy,
					MSM_DSI_PHY_MASTER);
		msm_dsi_phy_set_usecase(clk_slave_dsi->phy,
					MSM_DSI_PHY_SLAVE);
120
		src_pll = msm_dsi_phy_get_pll(clk_master_dsi->phy);
121 122
		if (IS_ERR(src_pll))
			return PTR_ERR(src_pll);
123 124 125 126 127 128 129 130 131
		ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
		if (ret)
			return ret;
		ret = msm_dsi_host_set_src_pll(other_dsi->host, src_pll);
	}

	return ret;
}

132 133 134 135 136
static int enable_phy(struct msm_dsi *msm_dsi, int src_pll_id,
		      struct msm_dsi_phy_shared_timings *shared_timings)
{
	struct msm_dsi_phy_clk_request clk_req;
	int ret;
137
	bool is_dual_dsi = IS_DUAL_DSI();
138

139
	msm_dsi_host_get_phy_clk_req(msm_dsi->host, &clk_req, is_dual_dsi);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	ret = msm_dsi_phy_enable(msm_dsi->phy, src_pll_id, &clk_req);
	msm_dsi_phy_get_shared_timings(msm_dsi->phy, shared_timings);

	return ret;
}

static int
dsi_mgr_phy_enable(int id,
		   struct msm_dsi_phy_shared_timings shared_timings[DSI_MAX])
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
	struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
	int src_pll_id = IS_DUAL_DSI() ? DSI_CLOCK_MASTER : id;
	int ret;

	/* In case of dual DSI, some registers in PHY1 have been programmed
	 * during PLL0 clock's set_rate. The PHY1 reset called by host1 here
	 * will silently reset those PHY1 registers. Therefore we need to reset
	 * and enable both PHYs before any PLL clock operation.
	 */
	if (IS_DUAL_DSI() && mdsi && sdsi) {
		if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
			msm_dsi_host_reset_phy(mdsi->host);
			msm_dsi_host_reset_phy(sdsi->host);

			ret = enable_phy(mdsi, src_pll_id,
					 &shared_timings[DSI_CLOCK_MASTER]);
			if (ret)
				return ret;
			ret = enable_phy(sdsi, src_pll_id,
					 &shared_timings[DSI_CLOCK_SLAVE]);
			if (ret) {
				msm_dsi_phy_disable(mdsi->phy);
				return ret;
			}
		}
	} else {
179
		msm_dsi_host_reset_phy(msm_dsi->host);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		ret = enable_phy(msm_dsi, src_pll_id, &shared_timings[id]);
		if (ret)
			return ret;
	}

	msm_dsi->phy_enabled = true;

	return 0;
}

static void dsi_mgr_phy_disable(int id)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
	struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);

	/* disable DSI phy
	 * In dual-dsi configuration, the phy should be disabled for the
	 * first controller only when the second controller is disabled.
	 */
	msm_dsi->phy_enabled = false;
	if (IS_DUAL_DSI() && mdsi && sdsi) {
		if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
			msm_dsi_phy_disable(sdsi->phy);
			msm_dsi_phy_disable(mdsi->phy);
		}
	} else {
		msm_dsi_phy_disable(msm_dsi->phy);
	}
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
struct dsi_connector {
	struct drm_connector base;
	int id;
};

struct dsi_bridge {
	struct drm_bridge base;
	int id;
};

#define to_dsi_connector(x) container_of(x, struct dsi_connector, base)
#define to_dsi_bridge(x) container_of(x, struct dsi_bridge, base)

static inline int dsi_mgr_connector_get_id(struct drm_connector *connector)
{
	struct dsi_connector *dsi_connector = to_dsi_connector(connector);
	return dsi_connector->id;
}

static int dsi_mgr_bridge_get_id(struct drm_bridge *bridge)
{
	struct dsi_bridge *dsi_bridge = to_dsi_bridge(bridge);
	return dsi_bridge->id;
}

static enum drm_connector_status dsi_mgr_connector_detect(
		struct drm_connector *connector, bool force)
{
	int id = dsi_mgr_connector_get_id(connector);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
	struct msm_drm_private *priv = connector->dev->dev_private;
	struct msm_kms *kms = priv->kms;

	DBG("id=%d", id);
	if (!msm_dsi->panel) {
		msm_dsi->panel = msm_dsi_host_get_panel(msm_dsi->host,
248
						&msm_dsi->device_flags);
249 250

		/* There is only 1 panel in the global panel list
251
		 * for dual DSI mode. Therefore slave dsi should get
252 253 254
		 * the drm_panel instance from master dsi, and
		 * keep using the panel flags got from the current DSI link.
		 */
255 256
		if (!msm_dsi->panel && IS_DUAL_DSI() &&
			!IS_MASTER_DSI_LINK(id) && other_dsi)
257 258 259
			msm_dsi->panel = msm_dsi_host_get_panel(
					other_dsi->host, NULL);

260 261 262 263 264 265 266 267 268 269

		if (msm_dsi->panel && kms->funcs->set_encoder_mode) {
			bool cmd_mode = !(msm_dsi->device_flags &
					  MIPI_DSI_MODE_VIDEO);
			struct drm_encoder *encoder =
					msm_dsi_get_encoder(msm_dsi);

			kms->funcs->set_encoder_mode(kms, encoder, cmd_mode);
		}

270
		if (msm_dsi->panel && IS_DUAL_DSI())
271 272 273
			drm_object_attach_property(&connector->base,
				connector->dev->mode_config.tile_property, 0);

274 275
		/* Set split display info to kms once dual DSI panel is
		 * connected to both hosts.
276
		 */
277
		if (msm_dsi->panel && IS_DUAL_DSI() &&
278
			other_dsi && other_dsi->panel) {
279
			bool cmd_mode = !(msm_dsi->device_flags &
280 281 282 283 284 285 286 287 288 289
						MIPI_DSI_MODE_VIDEO);
			struct drm_encoder *encoder = msm_dsi_get_encoder(
					dsi_mgr_get_dsi(DSI_ENCODER_MASTER));
			struct drm_encoder *slave_enc = msm_dsi_get_encoder(
					dsi_mgr_get_dsi(DSI_ENCODER_SLAVE));

			if (kms->funcs->set_split_display)
				kms->funcs->set_split_display(kms, encoder,
							slave_enc, cmd_mode);
			else
290
				pr_err("mdp does not support dual DSI\n");
291 292 293 294 295 296 297 298 299
		}
	}

	return msm_dsi->panel ? connector_status_connected :
		connector_status_disconnected;
}

static void dsi_mgr_connector_destroy(struct drm_connector *connector)
{
300 301
	struct dsi_connector *dsi_connector = to_dsi_connector(connector);

302
	DBG("");
303

304
	drm_connector_cleanup(connector);
305 306

	kfree(dsi_connector);
307 308 309 310 311 312 313
}

static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
{
	int id = dsi_mgr_connector_get_id(connector);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_panel *panel = msm_dsi->panel;
314
	int num;
315 316 317 318

	if (!panel)
		return 0;

319 320 321
	/*
	 * In dual DSI mode, we have one connector that can be
	 * attached to the drm_panel.
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
	 */
	drm_panel_attach(panel, connector);
	num = drm_panel_get_modes(panel);
	if (!num)
		return 0;

	return num;
}

static int dsi_mgr_connector_mode_valid(struct drm_connector *connector,
				struct drm_display_mode *mode)
{
	int id = dsi_mgr_connector_get_id(connector);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_encoder *encoder = msm_dsi_get_encoder(msm_dsi);
	struct msm_drm_private *priv = connector->dev->dev_private;
	struct msm_kms *kms = priv->kms;
	long actual, requested;

	DBG("");
	requested = 1000 * mode->clock;
	actual = kms->funcs->round_pixclk(kms, requested, encoder);

	DBG("requested=%ld, actual=%ld", requested, actual);
	if (actual != requested)
		return MODE_CLOCK_RANGE;

	return MODE_OK;
}

static struct drm_encoder *
dsi_mgr_connector_best_encoder(struct drm_connector *connector)
{
	int id = dsi_mgr_connector_get_id(connector);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);

	DBG("");
	return msm_dsi_get_encoder(msm_dsi);
}

static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
{
	int id = dsi_mgr_bridge_get_id(bridge);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
	struct mipi_dsi_host *host = msm_dsi->host;
	struct drm_panel *panel = msm_dsi->panel;
369
	struct msm_dsi_phy_shared_timings phy_shared_timings[DSI_MAX];
370
	bool is_dual_dsi = IS_DUAL_DSI();
371 372 373
	int ret;

	DBG("id=%d", id);
374
	if (!msm_dsi_device_connected(msm_dsi))
375 376
		return;

377 378 379 380
	ret = dsi_mgr_phy_enable(id, phy_shared_timings);
	if (ret)
		goto phy_en_fail;

381 382
	/* Do nothing with the host if it is slave-DSI in case of dual DSI */
	if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
383 384
		return;

385
	ret = msm_dsi_host_power_on(host, &phy_shared_timings[id], is_dual_dsi);
386 387 388 389 390
	if (ret) {
		pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
		goto host_on_fail;
	}

391
	if (is_dual_dsi && msm_dsi1) {
392
		ret = msm_dsi_host_power_on(msm_dsi1->host,
393
				&phy_shared_timings[DSI_1], is_dual_dsi);
394 395 396 397 398 399 400 401 402 403
		if (ret) {
			pr_err("%s: power on host1 failed, %d\n",
							__func__, ret);
			goto host1_on_fail;
		}
	}

	/* Always call panel functions once, because even for dual panels,
	 * there is only one drm_panel instance.
	 */
404 405 406 407 408 409 410
	if (panel) {
		ret = drm_panel_prepare(panel);
		if (ret) {
			pr_err("%s: prepare panel %d failed, %d\n", __func__,
								id, ret);
			goto panel_prep_fail;
		}
411 412 413 414 415 416 417 418
	}

	ret = msm_dsi_host_enable(host);
	if (ret) {
		pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
		goto host_en_fail;
	}

419
	if (is_dual_dsi && msm_dsi1) {
420 421 422 423 424 425 426
		ret = msm_dsi_host_enable(msm_dsi1->host);
		if (ret) {
			pr_err("%s: enable host1 failed, %d\n", __func__, ret);
			goto host1_en_fail;
		}
	}

427 428 429 430 431 432 433
	if (panel) {
		ret = drm_panel_enable(panel);
		if (ret) {
			pr_err("%s: enable panel %d failed, %d\n", __func__, id,
									ret);
			goto panel_en_fail;
		}
434 435 436 437 438
	}

	return;

panel_en_fail:
439
	if (is_dual_dsi && msm_dsi1)
440 441 442 443
		msm_dsi_host_disable(msm_dsi1->host);
host1_en_fail:
	msm_dsi_host_disable(host);
host_en_fail:
444 445
	if (panel)
		drm_panel_unprepare(panel);
446
panel_prep_fail:
447
	if (is_dual_dsi && msm_dsi1)
448 449 450 451
		msm_dsi_host_power_off(msm_dsi1->host);
host1_on_fail:
	msm_dsi_host_power_off(host);
host_on_fail:
452 453
	dsi_mgr_phy_disable(id);
phy_en_fail:
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
	return;
}

static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
{
	DBG("");
}

static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
{
	DBG("");
}

static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
{
	int id = dsi_mgr_bridge_get_id(bridge);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
	struct mipi_dsi_host *host = msm_dsi->host;
	struct drm_panel *panel = msm_dsi->panel;
474
	bool is_dual_dsi = IS_DUAL_DSI();
475 476 477 478
	int ret;

	DBG("id=%d", id);

479
	if (!msm_dsi_device_connected(msm_dsi))
480 481
		return;

482
	/*
483
	 * Do nothing with the host if it is slave-DSI in case of dual DSI.
484 485 486
	 * It is safe to call dsi_mgr_phy_disable() here because a single PHY
	 * won't be diabled until both PHYs request disable.
	 */
487
	if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
488 489
		goto disable_phy;

490 491 492 493 494 495
	if (panel) {
		ret = drm_panel_disable(panel);
		if (ret)
			pr_err("%s: Panel %d OFF failed, %d\n", __func__, id,
									ret);
	}
496 497 498 499 500

	ret = msm_dsi_host_disable(host);
	if (ret)
		pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);

501
	if (is_dual_dsi && msm_dsi1) {
502 503 504 505 506
		ret = msm_dsi_host_disable(msm_dsi1->host);
		if (ret)
			pr_err("%s: host1 disable failed, %d\n", __func__, ret);
	}

507 508 509 510 511 512
	if (panel) {
		ret = drm_panel_unprepare(panel);
		if (ret)
			pr_err("%s: Panel %d unprepare failed,%d\n", __func__,
								id, ret);
	}
513 514 515 516 517

	ret = msm_dsi_host_power_off(host);
	if (ret)
		pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);

518
	if (is_dual_dsi && msm_dsi1) {
519 520 521 522 523
		ret = msm_dsi_host_power_off(msm_dsi1->host);
		if (ret)
			pr_err("%s: host1 power off failed, %d\n",
								__func__, ret);
	}
524 525 526

disable_phy:
	dsi_mgr_phy_disable(id);
527 528 529 530 531 532 533 534 535 536
}

static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
		struct drm_display_mode *mode,
		struct drm_display_mode *adjusted_mode)
{
	int id = dsi_mgr_bridge_get_id(bridge);
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
	struct mipi_dsi_host *host = msm_dsi->host;
537
	bool is_dual_dsi = IS_DUAL_DSI();
538 539 540 541 542 543 544 545 546 547

	DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
			mode->base.id, mode->name,
			mode->vrefresh, mode->clock,
			mode->hdisplay, mode->hsync_start,
			mode->hsync_end, mode->htotal,
			mode->vdisplay, mode->vsync_start,
			mode->vsync_end, mode->vtotal,
			mode->type, mode->flags);

548
	if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
549 550 551
		return;

	msm_dsi_host_set_display_mode(host, adjusted_mode);
552
	if (is_dual_dsi && other_dsi)
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
		msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
}

static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
	.detect = dsi_mgr_connector_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = dsi_mgr_connector_destroy,
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};

static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
	.get_modes = dsi_mgr_connector_get_modes,
	.mode_valid = dsi_mgr_connector_mode_valid,
	.best_encoder = dsi_mgr_connector_best_encoder,
};

static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
	.pre_enable = dsi_mgr_bridge_pre_enable,
	.enable = dsi_mgr_bridge_enable,
	.disable = dsi_mgr_bridge_disable,
	.post_disable = dsi_mgr_bridge_post_disable,
	.mode_set = dsi_mgr_bridge_mode_set,
};

579
/* initialize connector when we're connected to a drm_panel */
580 581 582 583 584
struct drm_connector *msm_dsi_manager_connector_init(u8 id)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_connector *connector = NULL;
	struct dsi_connector *dsi_connector;
585
	int ret;
586

587 588 589
	dsi_connector = kzalloc(sizeof(*dsi_connector), GFP_KERNEL);
	if (!dsi_connector)
		return ERR_PTR(-ENOMEM);
590 591 592 593 594 595 596 597

	dsi_connector->id = id;

	connector = &dsi_connector->base;

	ret = drm_connector_init(msm_dsi->dev, connector,
			&dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
	if (ret)
598
		return ERR_PTR(ret);
599 600 601 602 603 604 605 606 607 608 609 610

	drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);

	/* Enable HPD to let hpd event is handled
	 * when panel is attached to the host.
	 */
	connector->polled = DRM_CONNECTOR_POLL_HPD;

	/* Display driver doesn't support interlace now. */
	connector->interlace_allowed = 0;
	connector->doublescan_allowed = 0;

611
	drm_connector_attach_encoder(connector, msm_dsi->encoder);
612

613 614 615
	return connector;
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
bool msm_dsi_manager_validate_current_config(u8 id)
{
	bool is_dual_dsi = IS_DUAL_DSI();

	/*
	 * For dual DSI, we only have one drm panel. For this
	 * use case, we register only one bridge/connector.
	 * Skip bridge/connector initialisation if it is
	 * slave-DSI for dual DSI configuration.
	 */
	if (is_dual_dsi && !IS_MASTER_DSI_LINK(id)) {
		DBG("Skip bridge registration for slave DSI->id: %d\n", id);
		return false;
	}
	return true;
}

633 634 635 636 637 638
/* initialize bridge */
struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_bridge *bridge = NULL;
	struct dsi_bridge *dsi_bridge;
639
	struct drm_encoder *encoder;
640 641 642 643 644 645 646 647 648 649 650
	int ret;

	dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
				sizeof(*dsi_bridge), GFP_KERNEL);
	if (!dsi_bridge) {
		ret = -ENOMEM;
		goto fail;
	}

	dsi_bridge->id = id;

651
	encoder = msm_dsi->encoder;
652

653 654 655
	bridge = &dsi_bridge->base;
	bridge->funcs = &dsi_mgr_bridge_funcs;

656
	ret = drm_bridge_attach(encoder, bridge, NULL);
657 658 659 660 661 662 663 664 665 666 667 668
	if (ret)
		goto fail;

	return bridge;

fail:
	if (bridge)
		msm_dsi_manager_bridge_destroy(bridge);

	return ERR_PTR(ret);
}

669 670 671 672 673 674 675 676 677 678 679 680 681
struct drm_connector *msm_dsi_manager_ext_bridge_init(u8 id)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_device *dev = msm_dsi->dev;
	struct drm_encoder *encoder;
	struct drm_bridge *int_bridge, *ext_bridge;
	struct drm_connector *connector;
	struct list_head *connector_list;

	int_bridge = msm_dsi->bridge;
	ext_bridge = msm_dsi->external_bridge =
			msm_dsi_host_get_bridge(msm_dsi->host);

682
	encoder = msm_dsi->encoder;
683 684

	/* link the internal dsi bridge to the external bridge */
685
	drm_bridge_attach(encoder, ext_bridge, int_bridge);
686 687 688 689 690 691 692 693 694

	/*
	 * we need the drm_connector created by the external bridge
	 * driver (or someone else) to feed it to our driver's
	 * priv->connector[] list, mainly for msm_fbdev_init()
	 */
	connector_list = &dev->mode_config.connector_list;

	list_for_each_entry(connector, connector_list, head) {
695 696
		if (drm_connector_has_possible_encoder(connector, encoder))
			return connector;
697 698 699 700 701
	}

	return ERR_PTR(-ENODEV);
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
{
}

int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
	struct mipi_dsi_host *host = msm_dsi->host;
	bool is_read = (msg->rx_buf && msg->rx_len);
	bool need_sync = (IS_SYNC_NEEDED() && !is_read);
	int ret;

	if (!msg->tx_buf || !msg->tx_len)
		return 0;

	/* In dual master case, panel requires the same commands sent to
	 * both DSI links. Host issues the command trigger to both links
	 * when DSI_1 calls the cmd transfer function, no matter it happens
	 * before or after DSI_0 cmd transfer.
	 */
	if (need_sync && (id == DSI_0))
		return is_read ? msg->rx_len : msg->tx_len;

	if (need_sync && msm_dsi0) {
		ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
		if (ret) {
			pr_err("%s: failed to prepare non-trigger host, %d\n",
				__func__, ret);
			return ret;
		}
	}
	ret = msm_dsi_host_xfer_prepare(host, msg);
	if (ret) {
		pr_err("%s: failed to prepare host, %d\n", __func__, ret);
		goto restore_host0;
	}

	ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
			msm_dsi_host_cmd_tx(host, msg);

	msm_dsi_host_xfer_restore(host, msg);

restore_host0:
	if (need_sync && msm_dsi0)
		msm_dsi_host_xfer_restore(msm_dsi0->host, msg);

	return ret;
}

752
bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len)
753 754 755 756 757 758 759 760 761
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
	struct mipi_dsi_host *host = msm_dsi->host;

	if (IS_SYNC_NEEDED() && (id == DSI_0))
		return false;

	if (IS_SYNC_NEEDED() && msm_dsi0)
762
		msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, dma_base, len);
763

764
	msm_dsi_host_cmd_xfer_commit(host, dma_base, len);
765 766 767 768

	return true;
}

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
void msm_dsi_manager_attach_dsi_device(int id, u32 device_flags)
{
	struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
	struct drm_device *dev = msm_dsi->dev;
	struct msm_drm_private *priv;
	struct msm_kms *kms;
	struct drm_encoder *encoder;

	/*
	 * drm_device pointer is assigned to msm_dsi only in the modeset_init
	 * path. If mipi_dsi_attach() happens in DSI driver's probe path
	 * (generally the case when we're connected to a drm_panel of the type
	 * mipi_dsi_device), this would be NULL. In such cases, try to set the
	 * encoder mode in the DSI connector's detect() op.
	 */
	if (!dev)
		return;

	priv = dev->dev_private;
	kms = priv->kms;
	encoder = msm_dsi_get_encoder(msm_dsi);

	if (encoder && kms->funcs->set_encoder_mode)
		if (!(device_flags & MIPI_DSI_MODE_VIDEO))
			kms->funcs->set_encoder_mode(kms, encoder, true);
}

796 797 798 799 800 801
int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
{
	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
	int id = msm_dsi->id;
	int ret;

802
	if (id >= DSI_MAX) {
803 804 805 806 807 808 809 810 811 812 813
		pr_err("%s: invalid id %d\n", __func__, id);
		return -EINVAL;
	}

	if (msm_dsim->dsi[id]) {
		pr_err("%s: dsi%d already registered\n", __func__, id);
		return -EBUSY;
	}

	msm_dsim->dsi[id] = msm_dsi;

814
	ret = dsi_mgr_parse_dual_dsi(msm_dsi->pdev->dev.of_node, id);
815
	if (ret) {
816
		pr_err("%s: failed to parse dual DSI info\n", __func__);
817
		goto fail;
818 819
	}

H
Hai Li 已提交
820
	ret = dsi_mgr_setup_components(id);
821 822 823 824
	if (ret) {
		pr_err("%s: failed to register mipi dsi host for DSI %d\n",
			__func__, id);
		goto fail;
825 826
	}

827 828 829 830
	return 0;

fail:
	msm_dsim->dsi[id] = NULL;
831 832 833 834 835 836 837 838 839 840 841 842
	return ret;
}

void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
{
	struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;

	if (msm_dsi->host)
		msm_dsi_host_unregister(msm_dsi->host);
	msm_dsim->dsi[msm_dsi->id] = NULL;
}