dsi_manager.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include "msm_kms.h"
  14. #include "dsi.h"
  15. #define DSI_CLOCK_MASTER DSI_0
  16. #define DSI_CLOCK_SLAVE DSI_1
  17. #define DSI_LEFT DSI_0
  18. #define DSI_RIGHT DSI_1
  19. /* According to the current drm framework sequence, take the encoder of
  20. * DSI_1 as master encoder
  21. */
  22. #define DSI_ENCODER_MASTER DSI_1
  23. #define DSI_ENCODER_SLAVE DSI_0
  24. struct msm_dsi_manager {
  25. struct msm_dsi *dsi[DSI_MAX];
  26. bool is_dual_dsi;
  27. bool is_sync_needed;
  28. int master_dsi_link_id;
  29. };
  30. static struct msm_dsi_manager msm_dsim_glb;
  31. #define IS_DUAL_DSI() (msm_dsim_glb.is_dual_dsi)
  32. #define IS_SYNC_NEEDED() (msm_dsim_glb.is_sync_needed)
  33. #define IS_MASTER_DSI_LINK(id) (msm_dsim_glb.master_dsi_link_id == id)
  34. static inline struct msm_dsi *dsi_mgr_get_dsi(int id)
  35. {
  36. return msm_dsim_glb.dsi[id];
  37. }
  38. static inline struct msm_dsi *dsi_mgr_get_other_dsi(int id)
  39. {
  40. return msm_dsim_glb.dsi[(id + 1) % DSI_MAX];
  41. }
  42. static int dsi_mgr_parse_dual_dsi(struct device_node *np, int id)
  43. {
  44. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  45. /* We assume 2 dsi nodes have the same information of dual-dsi and
  46. * sync-mode, and only one node specifies master in case of dual mode.
  47. */
  48. if (!msm_dsim->is_dual_dsi)
  49. msm_dsim->is_dual_dsi = of_property_read_bool(
  50. np, "qcom,dual-dsi-mode");
  51. if (msm_dsim->is_dual_dsi) {
  52. if (of_property_read_bool(np, "qcom,master-dsi"))
  53. msm_dsim->master_dsi_link_id = id;
  54. if (!msm_dsim->is_sync_needed)
  55. msm_dsim->is_sync_needed = of_property_read_bool(
  56. np, "qcom,sync-dual-dsi");
  57. }
  58. return 0;
  59. }
  60. static int dsi_mgr_host_register(int id)
  61. {
  62. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  63. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  64. struct msm_dsi *clk_master_dsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
  65. struct msm_dsi_pll *src_pll;
  66. int ret;
  67. if (!IS_DUAL_DSI()) {
  68. ret = msm_dsi_host_register(msm_dsi->host, true);
  69. if (ret)
  70. return ret;
  71. src_pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  72. ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
  73. } else if (!other_dsi) {
  74. ret = 0;
  75. } else {
  76. struct msm_dsi *mdsi = IS_MASTER_DSI_LINK(id) ?
  77. msm_dsi : other_dsi;
  78. struct msm_dsi *sdsi = IS_MASTER_DSI_LINK(id) ?
  79. other_dsi : msm_dsi;
  80. /* Register slave host first, so that slave DSI device
  81. * has a chance to probe, and do not block the master
  82. * DSI device's probe.
  83. * Also, do not check defer for the slave host,
  84. * because only master DSI device adds the panel to global
  85. * panel list. The panel's device is the master DSI device.
  86. */
  87. ret = msm_dsi_host_register(sdsi->host, false);
  88. if (ret)
  89. return ret;
  90. ret = msm_dsi_host_register(mdsi->host, true);
  91. if (ret)
  92. return ret;
  93. /* PLL0 is to drive both 2 DSI link clocks in Dual DSI mode. */
  94. src_pll = msm_dsi_phy_get_pll(clk_master_dsi->phy);
  95. ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
  96. if (ret)
  97. return ret;
  98. ret = msm_dsi_host_set_src_pll(other_dsi->host, src_pll);
  99. }
  100. return ret;
  101. }
  102. struct dsi_connector {
  103. struct drm_connector base;
  104. int id;
  105. };
  106. struct dsi_bridge {
  107. struct drm_bridge base;
  108. int id;
  109. };
  110. #define to_dsi_connector(x) container_of(x, struct dsi_connector, base)
  111. #define to_dsi_bridge(x) container_of(x, struct dsi_bridge, base)
  112. static inline int dsi_mgr_connector_get_id(struct drm_connector *connector)
  113. {
  114. struct dsi_connector *dsi_connector = to_dsi_connector(connector);
  115. return dsi_connector->id;
  116. }
  117. static int dsi_mgr_bridge_get_id(struct drm_bridge *bridge)
  118. {
  119. struct dsi_bridge *dsi_bridge = to_dsi_bridge(bridge);
  120. return dsi_bridge->id;
  121. }
  122. static enum drm_connector_status dsi_mgr_connector_detect(
  123. struct drm_connector *connector, bool force)
  124. {
  125. int id = dsi_mgr_connector_get_id(connector);
  126. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  127. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  128. struct msm_drm_private *priv = connector->dev->dev_private;
  129. struct msm_kms *kms = priv->kms;
  130. DBG("id=%d", id);
  131. if (!msm_dsi->panel) {
  132. msm_dsi->panel = msm_dsi_host_get_panel(msm_dsi->host,
  133. &msm_dsi->device_flags);
  134. /* There is only 1 panel in the global panel list
  135. * for dual DSI mode. Therefore slave dsi should get
  136. * the drm_panel instance from master dsi, and
  137. * keep using the panel flags got from the current DSI link.
  138. */
  139. if (!msm_dsi->panel && IS_DUAL_DSI() &&
  140. !IS_MASTER_DSI_LINK(id) && other_dsi)
  141. msm_dsi->panel = msm_dsi_host_get_panel(
  142. other_dsi->host, NULL);
  143. if (msm_dsi->panel && IS_DUAL_DSI())
  144. drm_object_attach_property(&connector->base,
  145. connector->dev->mode_config.tile_property, 0);
  146. /* Set split display info to kms once dual DSI panel is
  147. * connected to both hosts.
  148. */
  149. if (msm_dsi->panel && IS_DUAL_DSI() &&
  150. other_dsi && other_dsi->panel) {
  151. bool cmd_mode = !(msm_dsi->device_flags &
  152. MIPI_DSI_MODE_VIDEO);
  153. struct drm_encoder *encoder = msm_dsi_get_encoder(
  154. dsi_mgr_get_dsi(DSI_ENCODER_MASTER));
  155. struct drm_encoder *slave_enc = msm_dsi_get_encoder(
  156. dsi_mgr_get_dsi(DSI_ENCODER_SLAVE));
  157. if (kms->funcs->set_split_display)
  158. kms->funcs->set_split_display(kms, encoder,
  159. slave_enc, cmd_mode);
  160. else
  161. pr_err("mdp does not support dual DSI\n");
  162. }
  163. }
  164. return msm_dsi->panel ? connector_status_connected :
  165. connector_status_disconnected;
  166. }
  167. static void dsi_mgr_connector_destroy(struct drm_connector *connector)
  168. {
  169. DBG("");
  170. drm_connector_unregister(connector);
  171. drm_connector_cleanup(connector);
  172. }
  173. static void dsi_dual_connector_fix_modes(struct drm_connector *connector)
  174. {
  175. struct drm_display_mode *mode, *m;
  176. /* Only support left-right mode */
  177. list_for_each_entry_safe(mode, m, &connector->probed_modes, head) {
  178. mode->clock >>= 1;
  179. mode->hdisplay >>= 1;
  180. mode->hsync_start >>= 1;
  181. mode->hsync_end >>= 1;
  182. mode->htotal >>= 1;
  183. drm_mode_set_name(mode);
  184. }
  185. }
  186. static int dsi_dual_connector_tile_init(
  187. struct drm_connector *connector, int id)
  188. {
  189. struct drm_display_mode *mode;
  190. /* Fake topology id */
  191. char topo_id[8] = {'M', 'S', 'M', 'D', 'U', 'D', 'S', 'I'};
  192. if (connector->tile_group) {
  193. DBG("Tile property has been initialized");
  194. return 0;
  195. }
  196. /* Use the first mode only for now */
  197. mode = list_first_entry(&connector->probed_modes,
  198. struct drm_display_mode,
  199. head);
  200. if (!mode)
  201. return -EINVAL;
  202. connector->tile_group = drm_mode_get_tile_group(
  203. connector->dev, topo_id);
  204. if (!connector->tile_group)
  205. connector->tile_group = drm_mode_create_tile_group(
  206. connector->dev, topo_id);
  207. if (!connector->tile_group) {
  208. pr_err("%s: failed to create tile group\n", __func__);
  209. return -ENOMEM;
  210. }
  211. connector->has_tile = true;
  212. connector->tile_is_single_monitor = true;
  213. /* mode has been fixed */
  214. connector->tile_h_size = mode->hdisplay;
  215. connector->tile_v_size = mode->vdisplay;
  216. /* Only support left-right mode */
  217. connector->num_h_tile = 2;
  218. connector->num_v_tile = 1;
  219. connector->tile_v_loc = 0;
  220. connector->tile_h_loc = (id == DSI_RIGHT) ? 1 : 0;
  221. return 0;
  222. }
  223. static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
  224. {
  225. int id = dsi_mgr_connector_get_id(connector);
  226. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  227. struct drm_panel *panel = msm_dsi->panel;
  228. int ret, num;
  229. if (!panel)
  230. return 0;
  231. /* Since we have 2 connectors, but only 1 drm_panel in dual DSI mode,
  232. * panel should not attach to any connector.
  233. * Only temporarily attach panel to the current connector here,
  234. * to let panel set mode to this connector.
  235. */
  236. drm_panel_attach(panel, connector);
  237. num = drm_panel_get_modes(panel);
  238. drm_panel_detach(panel);
  239. if (!num)
  240. return 0;
  241. if (IS_DUAL_DSI()) {
  242. /* report half resolution to user */
  243. dsi_dual_connector_fix_modes(connector);
  244. ret = dsi_dual_connector_tile_init(connector, id);
  245. if (ret)
  246. return ret;
  247. ret = drm_mode_connector_set_tile_property(connector);
  248. if (ret) {
  249. pr_err("%s: set tile property failed, %d\n",
  250. __func__, ret);
  251. return ret;
  252. }
  253. }
  254. return num;
  255. }
  256. static int dsi_mgr_connector_mode_valid(struct drm_connector *connector,
  257. struct drm_display_mode *mode)
  258. {
  259. int id = dsi_mgr_connector_get_id(connector);
  260. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  261. struct drm_encoder *encoder = msm_dsi_get_encoder(msm_dsi);
  262. struct msm_drm_private *priv = connector->dev->dev_private;
  263. struct msm_kms *kms = priv->kms;
  264. long actual, requested;
  265. DBG("");
  266. requested = 1000 * mode->clock;
  267. actual = kms->funcs->round_pixclk(kms, requested, encoder);
  268. DBG("requested=%ld, actual=%ld", requested, actual);
  269. if (actual != requested)
  270. return MODE_CLOCK_RANGE;
  271. return MODE_OK;
  272. }
  273. static struct drm_encoder *
  274. dsi_mgr_connector_best_encoder(struct drm_connector *connector)
  275. {
  276. int id = dsi_mgr_connector_get_id(connector);
  277. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  278. DBG("");
  279. return msm_dsi_get_encoder(msm_dsi);
  280. }
  281. static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
  282. {
  283. int id = dsi_mgr_bridge_get_id(bridge);
  284. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  285. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  286. struct mipi_dsi_host *host = msm_dsi->host;
  287. struct drm_panel *panel = msm_dsi->panel;
  288. bool is_dual_dsi = IS_DUAL_DSI();
  289. int ret;
  290. DBG("id=%d", id);
  291. if (!msm_dsi_device_connected(msm_dsi) ||
  292. (is_dual_dsi && (DSI_1 == id)))
  293. return;
  294. ret = msm_dsi_host_power_on(host);
  295. if (ret) {
  296. pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
  297. goto host_on_fail;
  298. }
  299. if (is_dual_dsi && msm_dsi1) {
  300. ret = msm_dsi_host_power_on(msm_dsi1->host);
  301. if (ret) {
  302. pr_err("%s: power on host1 failed, %d\n",
  303. __func__, ret);
  304. goto host1_on_fail;
  305. }
  306. }
  307. /* Always call panel functions once, because even for dual panels,
  308. * there is only one drm_panel instance.
  309. */
  310. if (panel) {
  311. ret = drm_panel_prepare(panel);
  312. if (ret) {
  313. pr_err("%s: prepare panel %d failed, %d\n", __func__,
  314. id, ret);
  315. goto panel_prep_fail;
  316. }
  317. }
  318. ret = msm_dsi_host_enable(host);
  319. if (ret) {
  320. pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
  321. goto host_en_fail;
  322. }
  323. if (is_dual_dsi && msm_dsi1) {
  324. ret = msm_dsi_host_enable(msm_dsi1->host);
  325. if (ret) {
  326. pr_err("%s: enable host1 failed, %d\n", __func__, ret);
  327. goto host1_en_fail;
  328. }
  329. }
  330. if (panel) {
  331. ret = drm_panel_enable(panel);
  332. if (ret) {
  333. pr_err("%s: enable panel %d failed, %d\n", __func__, id,
  334. ret);
  335. goto panel_en_fail;
  336. }
  337. }
  338. return;
  339. panel_en_fail:
  340. if (is_dual_dsi && msm_dsi1)
  341. msm_dsi_host_disable(msm_dsi1->host);
  342. host1_en_fail:
  343. msm_dsi_host_disable(host);
  344. host_en_fail:
  345. if (panel)
  346. drm_panel_unprepare(panel);
  347. panel_prep_fail:
  348. if (is_dual_dsi && msm_dsi1)
  349. msm_dsi_host_power_off(msm_dsi1->host);
  350. host1_on_fail:
  351. msm_dsi_host_power_off(host);
  352. host_on_fail:
  353. return;
  354. }
  355. static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
  356. {
  357. DBG("");
  358. }
  359. static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
  360. {
  361. DBG("");
  362. }
  363. static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
  364. {
  365. int id = dsi_mgr_bridge_get_id(bridge);
  366. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  367. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  368. struct mipi_dsi_host *host = msm_dsi->host;
  369. struct drm_panel *panel = msm_dsi->panel;
  370. bool is_dual_dsi = IS_DUAL_DSI();
  371. int ret;
  372. DBG("id=%d", id);
  373. if (!msm_dsi_device_connected(msm_dsi) ||
  374. (is_dual_dsi && (DSI_1 == id)))
  375. return;
  376. if (panel) {
  377. ret = drm_panel_disable(panel);
  378. if (ret)
  379. pr_err("%s: Panel %d OFF failed, %d\n", __func__, id,
  380. ret);
  381. }
  382. ret = msm_dsi_host_disable(host);
  383. if (ret)
  384. pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);
  385. if (is_dual_dsi && msm_dsi1) {
  386. ret = msm_dsi_host_disable(msm_dsi1->host);
  387. if (ret)
  388. pr_err("%s: host1 disable failed, %d\n", __func__, ret);
  389. }
  390. if (panel) {
  391. ret = drm_panel_unprepare(panel);
  392. if (ret)
  393. pr_err("%s: Panel %d unprepare failed,%d\n", __func__,
  394. id, ret);
  395. }
  396. ret = msm_dsi_host_power_off(host);
  397. if (ret)
  398. pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);
  399. if (is_dual_dsi && msm_dsi1) {
  400. ret = msm_dsi_host_power_off(msm_dsi1->host);
  401. if (ret)
  402. pr_err("%s: host1 power off failed, %d\n",
  403. __func__, ret);
  404. }
  405. }
  406. static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
  407. struct drm_display_mode *mode,
  408. struct drm_display_mode *adjusted_mode)
  409. {
  410. int id = dsi_mgr_bridge_get_id(bridge);
  411. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  412. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  413. struct mipi_dsi_host *host = msm_dsi->host;
  414. bool is_dual_dsi = IS_DUAL_DSI();
  415. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  416. mode->base.id, mode->name,
  417. mode->vrefresh, mode->clock,
  418. mode->hdisplay, mode->hsync_start,
  419. mode->hsync_end, mode->htotal,
  420. mode->vdisplay, mode->vsync_start,
  421. mode->vsync_end, mode->vtotal,
  422. mode->type, mode->flags);
  423. if (is_dual_dsi && (DSI_1 == id))
  424. return;
  425. msm_dsi_host_set_display_mode(host, adjusted_mode);
  426. if (is_dual_dsi && other_dsi)
  427. msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
  428. }
  429. static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
  430. .dpms = drm_atomic_helper_connector_dpms,
  431. .detect = dsi_mgr_connector_detect,
  432. .fill_modes = drm_helper_probe_single_connector_modes,
  433. .destroy = dsi_mgr_connector_destroy,
  434. .reset = drm_atomic_helper_connector_reset,
  435. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  436. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  437. };
  438. static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
  439. .get_modes = dsi_mgr_connector_get_modes,
  440. .mode_valid = dsi_mgr_connector_mode_valid,
  441. .best_encoder = dsi_mgr_connector_best_encoder,
  442. };
  443. static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
  444. .pre_enable = dsi_mgr_bridge_pre_enable,
  445. .enable = dsi_mgr_bridge_enable,
  446. .disable = dsi_mgr_bridge_disable,
  447. .post_disable = dsi_mgr_bridge_post_disable,
  448. .mode_set = dsi_mgr_bridge_mode_set,
  449. };
  450. /* initialize connector when we're connected to a drm_panel */
  451. struct drm_connector *msm_dsi_manager_connector_init(u8 id)
  452. {
  453. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  454. struct drm_connector *connector = NULL;
  455. struct dsi_connector *dsi_connector;
  456. int ret, i;
  457. dsi_connector = devm_kzalloc(msm_dsi->dev->dev,
  458. sizeof(*dsi_connector), GFP_KERNEL);
  459. if (!dsi_connector) {
  460. ret = -ENOMEM;
  461. goto fail;
  462. }
  463. dsi_connector->id = id;
  464. connector = &dsi_connector->base;
  465. ret = drm_connector_init(msm_dsi->dev, connector,
  466. &dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
  467. if (ret)
  468. goto fail;
  469. drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);
  470. /* Enable HPD to let hpd event is handled
  471. * when panel is attached to the host.
  472. */
  473. connector->polled = DRM_CONNECTOR_POLL_HPD;
  474. /* Display driver doesn't support interlace now. */
  475. connector->interlace_allowed = 0;
  476. connector->doublescan_allowed = 0;
  477. ret = drm_connector_register(connector);
  478. if (ret)
  479. goto fail;
  480. for (i = 0; i < MSM_DSI_ENCODER_NUM; i++)
  481. drm_mode_connector_attach_encoder(connector,
  482. msm_dsi->encoders[i]);
  483. return connector;
  484. fail:
  485. if (connector)
  486. dsi_mgr_connector_destroy(connector);
  487. return ERR_PTR(ret);
  488. }
  489. /* initialize bridge */
  490. struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
  491. {
  492. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  493. struct drm_bridge *bridge = NULL;
  494. struct dsi_bridge *dsi_bridge;
  495. int ret;
  496. dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
  497. sizeof(*dsi_bridge), GFP_KERNEL);
  498. if (!dsi_bridge) {
  499. ret = -ENOMEM;
  500. goto fail;
  501. }
  502. dsi_bridge->id = id;
  503. bridge = &dsi_bridge->base;
  504. bridge->funcs = &dsi_mgr_bridge_funcs;
  505. ret = drm_bridge_attach(msm_dsi->dev, bridge);
  506. if (ret)
  507. goto fail;
  508. return bridge;
  509. fail:
  510. if (bridge)
  511. msm_dsi_manager_bridge_destroy(bridge);
  512. return ERR_PTR(ret);
  513. }
  514. struct drm_connector *msm_dsi_manager_ext_bridge_init(u8 id)
  515. {
  516. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  517. struct drm_device *dev = msm_dsi->dev;
  518. struct drm_encoder *encoder;
  519. struct drm_bridge *int_bridge, *ext_bridge;
  520. struct drm_connector *connector;
  521. struct list_head *connector_list;
  522. int_bridge = msm_dsi->bridge;
  523. ext_bridge = msm_dsi->external_bridge =
  524. msm_dsi_host_get_bridge(msm_dsi->host);
  525. /*
  526. * HACK: we may not know the external DSI bridge device's mode
  527. * flags here. We'll get to know them only when the device
  528. * attaches to the dsi host. For now, assume the bridge supports
  529. * DSI video mode
  530. */
  531. encoder = msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID];
  532. /* link the internal dsi bridge to the external bridge */
  533. int_bridge->next = ext_bridge;
  534. /* set the external bridge's encoder as dsi's encoder */
  535. ext_bridge->encoder = encoder;
  536. drm_bridge_attach(dev, ext_bridge);
  537. /*
  538. * we need the drm_connector created by the external bridge
  539. * driver (or someone else) to feed it to our driver's
  540. * priv->connector[] list, mainly for msm_fbdev_init()
  541. */
  542. connector_list = &dev->mode_config.connector_list;
  543. list_for_each_entry(connector, connector_list, head) {
  544. int i;
  545. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  546. if (connector->encoder_ids[i] == encoder->base.id)
  547. return connector;
  548. }
  549. }
  550. return ERR_PTR(-ENODEV);
  551. }
  552. void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
  553. {
  554. }
  555. int msm_dsi_manager_phy_enable(int id,
  556. const unsigned long bit_rate, const unsigned long esc_rate,
  557. u32 *clk_pre, u32 *clk_post)
  558. {
  559. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  560. struct msm_dsi_phy *phy = msm_dsi->phy;
  561. int src_pll_id = IS_DUAL_DSI() ? DSI_CLOCK_MASTER : id;
  562. struct msm_dsi_pll *pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  563. int ret;
  564. ret = msm_dsi_phy_enable(phy, src_pll_id, bit_rate, esc_rate);
  565. if (ret)
  566. return ret;
  567. /*
  568. * Reset DSI PHY silently changes its PLL registers to reset status,
  569. * which will confuse clock driver and result in wrong output rate of
  570. * link clocks. Restore PLL status if its PLL is being used as clock
  571. * source.
  572. */
  573. if (!IS_DUAL_DSI() || (id == DSI_CLOCK_MASTER)) {
  574. ret = msm_dsi_pll_restore_state(pll);
  575. if (ret) {
  576. pr_err("%s: failed to restore pll state\n", __func__);
  577. msm_dsi_phy_disable(phy);
  578. return ret;
  579. }
  580. }
  581. msm_dsi->phy_enabled = true;
  582. msm_dsi_phy_get_clk_pre_post(phy, clk_pre, clk_post);
  583. return 0;
  584. }
  585. void msm_dsi_manager_phy_disable(int id)
  586. {
  587. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  588. struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
  589. struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
  590. struct msm_dsi_phy *phy = msm_dsi->phy;
  591. struct msm_dsi_pll *pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  592. /* Save PLL status if it is a clock source */
  593. if (!IS_DUAL_DSI() || (id == DSI_CLOCK_MASTER))
  594. msm_dsi_pll_save_state(pll);
  595. /* disable DSI phy
  596. * In dual-dsi configuration, the phy should be disabled for the
  597. * first controller only when the second controller is disabled.
  598. */
  599. msm_dsi->phy_enabled = false;
  600. if (IS_DUAL_DSI() && mdsi && sdsi) {
  601. if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
  602. msm_dsi_phy_disable(sdsi->phy);
  603. msm_dsi_phy_disable(mdsi->phy);
  604. }
  605. } else {
  606. msm_dsi_phy_disable(phy);
  607. }
  608. }
  609. int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
  610. {
  611. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  612. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  613. struct mipi_dsi_host *host = msm_dsi->host;
  614. bool is_read = (msg->rx_buf && msg->rx_len);
  615. bool need_sync = (IS_SYNC_NEEDED() && !is_read);
  616. int ret;
  617. if (!msg->tx_buf || !msg->tx_len)
  618. return 0;
  619. /* In dual master case, panel requires the same commands sent to
  620. * both DSI links. Host issues the command trigger to both links
  621. * when DSI_1 calls the cmd transfer function, no matter it happens
  622. * before or after DSI_0 cmd transfer.
  623. */
  624. if (need_sync && (id == DSI_0))
  625. return is_read ? msg->rx_len : msg->tx_len;
  626. if (need_sync && msm_dsi0) {
  627. ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
  628. if (ret) {
  629. pr_err("%s: failed to prepare non-trigger host, %d\n",
  630. __func__, ret);
  631. return ret;
  632. }
  633. }
  634. ret = msm_dsi_host_xfer_prepare(host, msg);
  635. if (ret) {
  636. pr_err("%s: failed to prepare host, %d\n", __func__, ret);
  637. goto restore_host0;
  638. }
  639. ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
  640. msm_dsi_host_cmd_tx(host, msg);
  641. msm_dsi_host_xfer_restore(host, msg);
  642. restore_host0:
  643. if (need_sync && msm_dsi0)
  644. msm_dsi_host_xfer_restore(msm_dsi0->host, msg);
  645. return ret;
  646. }
  647. bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 iova, u32 len)
  648. {
  649. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  650. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  651. struct mipi_dsi_host *host = msm_dsi->host;
  652. if (IS_SYNC_NEEDED() && (id == DSI_0))
  653. return false;
  654. if (IS_SYNC_NEEDED() && msm_dsi0)
  655. msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, iova, len);
  656. msm_dsi_host_cmd_xfer_commit(host, iova, len);
  657. return true;
  658. }
  659. int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
  660. {
  661. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  662. int id = msm_dsi->id;
  663. int ret;
  664. if (id > DSI_MAX) {
  665. pr_err("%s: invalid id %d\n", __func__, id);
  666. return -EINVAL;
  667. }
  668. if (msm_dsim->dsi[id]) {
  669. pr_err("%s: dsi%d already registered\n", __func__, id);
  670. return -EBUSY;
  671. }
  672. msm_dsim->dsi[id] = msm_dsi;
  673. ret = dsi_mgr_parse_dual_dsi(msm_dsi->pdev->dev.of_node, id);
  674. if (ret) {
  675. pr_err("%s: failed to parse dual DSI info\n", __func__);
  676. goto fail;
  677. }
  678. ret = dsi_mgr_host_register(id);
  679. if (ret) {
  680. pr_err("%s: failed to register mipi dsi host for DSI %d\n",
  681. __func__, id);
  682. goto fail;
  683. }
  684. return 0;
  685. fail:
  686. msm_dsim->dsi[id] = NULL;
  687. return ret;
  688. }
  689. void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
  690. {
  691. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  692. if (msm_dsi->host)
  693. msm_dsi_host_unregister(msm_dsi->host);
  694. msm_dsim->dsi[msm_dsi->id] = NULL;
  695. }