dsi_manager.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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_setup_components(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 *clk_slave_dsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
  66. struct msm_dsi_pll *src_pll;
  67. int ret;
  68. if (!IS_DUAL_DSI()) {
  69. ret = msm_dsi_host_register(msm_dsi->host, true);
  70. if (ret)
  71. return ret;
  72. msm_dsi_phy_set_usecase(msm_dsi->phy, MSM_DSI_PHY_STANDALONE);
  73. src_pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  74. if (IS_ERR(src_pll))
  75. return PTR_ERR(src_pll);
  76. ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
  77. } else if (!other_dsi) {
  78. ret = 0;
  79. } else {
  80. struct msm_dsi *master_link_dsi = IS_MASTER_DSI_LINK(id) ?
  81. msm_dsi : other_dsi;
  82. struct msm_dsi *slave_link_dsi = IS_MASTER_DSI_LINK(id) ?
  83. other_dsi : msm_dsi;
  84. /* Register slave host first, so that slave DSI device
  85. * has a chance to probe, and do not block the master
  86. * DSI device's probe.
  87. * Also, do not check defer for the slave host,
  88. * because only master DSI device adds the panel to global
  89. * panel list. The panel's device is the master DSI device.
  90. */
  91. ret = msm_dsi_host_register(slave_link_dsi->host, false);
  92. if (ret)
  93. return ret;
  94. ret = msm_dsi_host_register(master_link_dsi->host, true);
  95. if (ret)
  96. return ret;
  97. /* PLL0 is to drive both 2 DSI link clocks in Dual DSI mode. */
  98. msm_dsi_phy_set_usecase(clk_master_dsi->phy,
  99. MSM_DSI_PHY_MASTER);
  100. msm_dsi_phy_set_usecase(clk_slave_dsi->phy,
  101. MSM_DSI_PHY_SLAVE);
  102. src_pll = msm_dsi_phy_get_pll(clk_master_dsi->phy);
  103. if (IS_ERR(src_pll))
  104. return PTR_ERR(src_pll);
  105. ret = msm_dsi_host_set_src_pll(msm_dsi->host, src_pll);
  106. if (ret)
  107. return ret;
  108. ret = msm_dsi_host_set_src_pll(other_dsi->host, src_pll);
  109. }
  110. return ret;
  111. }
  112. static int enable_phy(struct msm_dsi *msm_dsi, int src_pll_id,
  113. struct msm_dsi_phy_shared_timings *shared_timings)
  114. {
  115. struct msm_dsi_phy_clk_request clk_req;
  116. int ret;
  117. bool is_dual_dsi = IS_DUAL_DSI();
  118. msm_dsi_host_get_phy_clk_req(msm_dsi->host, &clk_req, is_dual_dsi);
  119. ret = msm_dsi_phy_enable(msm_dsi->phy, src_pll_id, &clk_req);
  120. msm_dsi_phy_get_shared_timings(msm_dsi->phy, shared_timings);
  121. return ret;
  122. }
  123. static int
  124. dsi_mgr_phy_enable(int id,
  125. struct msm_dsi_phy_shared_timings shared_timings[DSI_MAX])
  126. {
  127. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  128. struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
  129. struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
  130. int src_pll_id = IS_DUAL_DSI() ? DSI_CLOCK_MASTER : id;
  131. int ret;
  132. /* In case of dual DSI, some registers in PHY1 have been programmed
  133. * during PLL0 clock's set_rate. The PHY1 reset called by host1 here
  134. * will silently reset those PHY1 registers. Therefore we need to reset
  135. * and enable both PHYs before any PLL clock operation.
  136. */
  137. if (IS_DUAL_DSI() && mdsi && sdsi) {
  138. if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
  139. msm_dsi_host_reset_phy(mdsi->host);
  140. msm_dsi_host_reset_phy(sdsi->host);
  141. ret = enable_phy(mdsi, src_pll_id,
  142. &shared_timings[DSI_CLOCK_MASTER]);
  143. if (ret)
  144. return ret;
  145. ret = enable_phy(sdsi, src_pll_id,
  146. &shared_timings[DSI_CLOCK_SLAVE]);
  147. if (ret) {
  148. msm_dsi_phy_disable(mdsi->phy);
  149. return ret;
  150. }
  151. }
  152. } else {
  153. msm_dsi_host_reset_phy(msm_dsi->host);
  154. ret = enable_phy(msm_dsi, src_pll_id, &shared_timings[id]);
  155. if (ret)
  156. return ret;
  157. }
  158. msm_dsi->phy_enabled = true;
  159. return 0;
  160. }
  161. static void dsi_mgr_phy_disable(int id)
  162. {
  163. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  164. struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
  165. struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
  166. /* disable DSI phy
  167. * In dual-dsi configuration, the phy should be disabled for the
  168. * first controller only when the second controller is disabled.
  169. */
  170. msm_dsi->phy_enabled = false;
  171. if (IS_DUAL_DSI() && mdsi && sdsi) {
  172. if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
  173. msm_dsi_phy_disable(sdsi->phy);
  174. msm_dsi_phy_disable(mdsi->phy);
  175. }
  176. } else {
  177. msm_dsi_phy_disable(msm_dsi->phy);
  178. }
  179. }
  180. struct dsi_connector {
  181. struct drm_connector base;
  182. int id;
  183. };
  184. struct dsi_bridge {
  185. struct drm_bridge base;
  186. int id;
  187. };
  188. #define to_dsi_connector(x) container_of(x, struct dsi_connector, base)
  189. #define to_dsi_bridge(x) container_of(x, struct dsi_bridge, base)
  190. static inline int dsi_mgr_connector_get_id(struct drm_connector *connector)
  191. {
  192. struct dsi_connector *dsi_connector = to_dsi_connector(connector);
  193. return dsi_connector->id;
  194. }
  195. static int dsi_mgr_bridge_get_id(struct drm_bridge *bridge)
  196. {
  197. struct dsi_bridge *dsi_bridge = to_dsi_bridge(bridge);
  198. return dsi_bridge->id;
  199. }
  200. static enum drm_connector_status dsi_mgr_connector_detect(
  201. struct drm_connector *connector, bool force)
  202. {
  203. int id = dsi_mgr_connector_get_id(connector);
  204. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  205. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  206. struct msm_drm_private *priv = connector->dev->dev_private;
  207. struct msm_kms *kms = priv->kms;
  208. DBG("id=%d", id);
  209. if (!msm_dsi->panel) {
  210. msm_dsi->panel = msm_dsi_host_get_panel(msm_dsi->host,
  211. &msm_dsi->device_flags);
  212. /* There is only 1 panel in the global panel list
  213. * for dual DSI mode. Therefore slave dsi should get
  214. * the drm_panel instance from master dsi, and
  215. * keep using the panel flags got from the current DSI link.
  216. */
  217. if (!msm_dsi->panel && IS_DUAL_DSI() &&
  218. !IS_MASTER_DSI_LINK(id) && other_dsi)
  219. msm_dsi->panel = msm_dsi_host_get_panel(
  220. other_dsi->host, NULL);
  221. if (msm_dsi->panel && kms->funcs->set_encoder_mode) {
  222. bool cmd_mode = !(msm_dsi->device_flags &
  223. MIPI_DSI_MODE_VIDEO);
  224. struct drm_encoder *encoder =
  225. msm_dsi_get_encoder(msm_dsi);
  226. kms->funcs->set_encoder_mode(kms, encoder, cmd_mode);
  227. }
  228. if (msm_dsi->panel && IS_DUAL_DSI())
  229. drm_object_attach_property(&connector->base,
  230. connector->dev->mode_config.tile_property, 0);
  231. /* Set split display info to kms once dual DSI panel is
  232. * connected to both hosts.
  233. */
  234. if (msm_dsi->panel && IS_DUAL_DSI() &&
  235. other_dsi && other_dsi->panel) {
  236. bool cmd_mode = !(msm_dsi->device_flags &
  237. MIPI_DSI_MODE_VIDEO);
  238. struct drm_encoder *encoder = msm_dsi_get_encoder(
  239. dsi_mgr_get_dsi(DSI_ENCODER_MASTER));
  240. struct drm_encoder *slave_enc = msm_dsi_get_encoder(
  241. dsi_mgr_get_dsi(DSI_ENCODER_SLAVE));
  242. if (kms->funcs->set_split_display)
  243. kms->funcs->set_split_display(kms, encoder,
  244. slave_enc, cmd_mode);
  245. else
  246. pr_err("mdp does not support dual DSI\n");
  247. }
  248. }
  249. return msm_dsi->panel ? connector_status_connected :
  250. connector_status_disconnected;
  251. }
  252. static void dsi_mgr_connector_destroy(struct drm_connector *connector)
  253. {
  254. struct dsi_connector *dsi_connector = to_dsi_connector(connector);
  255. DBG("");
  256. drm_connector_cleanup(connector);
  257. kfree(dsi_connector);
  258. }
  259. static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
  260. {
  261. int id = dsi_mgr_connector_get_id(connector);
  262. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  263. struct drm_panel *panel = msm_dsi->panel;
  264. int num;
  265. if (!panel)
  266. return 0;
  267. /*
  268. * In dual DSI mode, we have one connector that can be
  269. * attached to the drm_panel.
  270. */
  271. drm_panel_attach(panel, connector);
  272. num = drm_panel_get_modes(panel);
  273. if (!num)
  274. return 0;
  275. return num;
  276. }
  277. static int dsi_mgr_connector_mode_valid(struct drm_connector *connector,
  278. struct drm_display_mode *mode)
  279. {
  280. int id = dsi_mgr_connector_get_id(connector);
  281. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  282. struct drm_encoder *encoder = msm_dsi_get_encoder(msm_dsi);
  283. struct msm_drm_private *priv = connector->dev->dev_private;
  284. struct msm_kms *kms = priv->kms;
  285. long actual, requested;
  286. DBG("");
  287. requested = 1000 * mode->clock;
  288. actual = kms->funcs->round_pixclk(kms, requested, encoder);
  289. DBG("requested=%ld, actual=%ld", requested, actual);
  290. if (actual != requested)
  291. return MODE_CLOCK_RANGE;
  292. return MODE_OK;
  293. }
  294. static struct drm_encoder *
  295. dsi_mgr_connector_best_encoder(struct drm_connector *connector)
  296. {
  297. int id = dsi_mgr_connector_get_id(connector);
  298. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  299. DBG("");
  300. return msm_dsi_get_encoder(msm_dsi);
  301. }
  302. static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
  303. {
  304. int id = dsi_mgr_bridge_get_id(bridge);
  305. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  306. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  307. struct mipi_dsi_host *host = msm_dsi->host;
  308. struct drm_panel *panel = msm_dsi->panel;
  309. struct msm_dsi_phy_shared_timings phy_shared_timings[DSI_MAX];
  310. bool is_dual_dsi = IS_DUAL_DSI();
  311. int ret;
  312. DBG("id=%d", id);
  313. if (!msm_dsi_device_connected(msm_dsi))
  314. return;
  315. ret = dsi_mgr_phy_enable(id, phy_shared_timings);
  316. if (ret)
  317. goto phy_en_fail;
  318. /* Do nothing with the host if it is slave-DSI in case of dual DSI */
  319. if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
  320. return;
  321. ret = msm_dsi_host_power_on(host, &phy_shared_timings[id], is_dual_dsi);
  322. if (ret) {
  323. pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
  324. goto host_on_fail;
  325. }
  326. if (is_dual_dsi && msm_dsi1) {
  327. ret = msm_dsi_host_power_on(msm_dsi1->host,
  328. &phy_shared_timings[DSI_1], is_dual_dsi);
  329. if (ret) {
  330. pr_err("%s: power on host1 failed, %d\n",
  331. __func__, ret);
  332. goto host1_on_fail;
  333. }
  334. }
  335. /* Always call panel functions once, because even for dual panels,
  336. * there is only one drm_panel instance.
  337. */
  338. if (panel) {
  339. ret = drm_panel_prepare(panel);
  340. if (ret) {
  341. pr_err("%s: prepare panel %d failed, %d\n", __func__,
  342. id, ret);
  343. goto panel_prep_fail;
  344. }
  345. }
  346. ret = msm_dsi_host_enable(host);
  347. if (ret) {
  348. pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
  349. goto host_en_fail;
  350. }
  351. if (is_dual_dsi && msm_dsi1) {
  352. ret = msm_dsi_host_enable(msm_dsi1->host);
  353. if (ret) {
  354. pr_err("%s: enable host1 failed, %d\n", __func__, ret);
  355. goto host1_en_fail;
  356. }
  357. }
  358. if (panel) {
  359. ret = drm_panel_enable(panel);
  360. if (ret) {
  361. pr_err("%s: enable panel %d failed, %d\n", __func__, id,
  362. ret);
  363. goto panel_en_fail;
  364. }
  365. }
  366. return;
  367. panel_en_fail:
  368. if (is_dual_dsi && msm_dsi1)
  369. msm_dsi_host_disable(msm_dsi1->host);
  370. host1_en_fail:
  371. msm_dsi_host_disable(host);
  372. host_en_fail:
  373. if (panel)
  374. drm_panel_unprepare(panel);
  375. panel_prep_fail:
  376. if (is_dual_dsi && msm_dsi1)
  377. msm_dsi_host_power_off(msm_dsi1->host);
  378. host1_on_fail:
  379. msm_dsi_host_power_off(host);
  380. host_on_fail:
  381. dsi_mgr_phy_disable(id);
  382. phy_en_fail:
  383. return;
  384. }
  385. static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
  386. {
  387. DBG("");
  388. }
  389. static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
  390. {
  391. DBG("");
  392. }
  393. static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
  394. {
  395. int id = dsi_mgr_bridge_get_id(bridge);
  396. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  397. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  398. struct mipi_dsi_host *host = msm_dsi->host;
  399. struct drm_panel *panel = msm_dsi->panel;
  400. bool is_dual_dsi = IS_DUAL_DSI();
  401. int ret;
  402. DBG("id=%d", id);
  403. if (!msm_dsi_device_connected(msm_dsi))
  404. return;
  405. /*
  406. * Do nothing with the host if it is slave-DSI in case of dual DSI.
  407. * It is safe to call dsi_mgr_phy_disable() here because a single PHY
  408. * won't be diabled until both PHYs request disable.
  409. */
  410. if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
  411. goto disable_phy;
  412. if (panel) {
  413. ret = drm_panel_disable(panel);
  414. if (ret)
  415. pr_err("%s: Panel %d OFF failed, %d\n", __func__, id,
  416. ret);
  417. }
  418. ret = msm_dsi_host_disable(host);
  419. if (ret)
  420. pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);
  421. if (is_dual_dsi && msm_dsi1) {
  422. ret = msm_dsi_host_disable(msm_dsi1->host);
  423. if (ret)
  424. pr_err("%s: host1 disable failed, %d\n", __func__, ret);
  425. }
  426. if (panel) {
  427. ret = drm_panel_unprepare(panel);
  428. if (ret)
  429. pr_err("%s: Panel %d unprepare failed,%d\n", __func__,
  430. id, ret);
  431. }
  432. ret = msm_dsi_host_power_off(host);
  433. if (ret)
  434. pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);
  435. if (is_dual_dsi && msm_dsi1) {
  436. ret = msm_dsi_host_power_off(msm_dsi1->host);
  437. if (ret)
  438. pr_err("%s: host1 power off failed, %d\n",
  439. __func__, ret);
  440. }
  441. disable_phy:
  442. dsi_mgr_phy_disable(id);
  443. }
  444. static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
  445. struct drm_display_mode *mode,
  446. struct drm_display_mode *adjusted_mode)
  447. {
  448. int id = dsi_mgr_bridge_get_id(bridge);
  449. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  450. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  451. struct mipi_dsi_host *host = msm_dsi->host;
  452. bool is_dual_dsi = IS_DUAL_DSI();
  453. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  454. mode->base.id, mode->name,
  455. mode->vrefresh, mode->clock,
  456. mode->hdisplay, mode->hsync_start,
  457. mode->hsync_end, mode->htotal,
  458. mode->vdisplay, mode->vsync_start,
  459. mode->vsync_end, mode->vtotal,
  460. mode->type, mode->flags);
  461. if (is_dual_dsi && !IS_MASTER_DSI_LINK(id))
  462. return;
  463. msm_dsi_host_set_display_mode(host, adjusted_mode);
  464. if (is_dual_dsi && other_dsi)
  465. msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
  466. }
  467. static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
  468. .detect = dsi_mgr_connector_detect,
  469. .fill_modes = drm_helper_probe_single_connector_modes,
  470. .destroy = dsi_mgr_connector_destroy,
  471. .reset = drm_atomic_helper_connector_reset,
  472. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  473. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  474. };
  475. static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
  476. .get_modes = dsi_mgr_connector_get_modes,
  477. .mode_valid = dsi_mgr_connector_mode_valid,
  478. .best_encoder = dsi_mgr_connector_best_encoder,
  479. };
  480. static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
  481. .pre_enable = dsi_mgr_bridge_pre_enable,
  482. .enable = dsi_mgr_bridge_enable,
  483. .disable = dsi_mgr_bridge_disable,
  484. .post_disable = dsi_mgr_bridge_post_disable,
  485. .mode_set = dsi_mgr_bridge_mode_set,
  486. };
  487. /* initialize connector when we're connected to a drm_panel */
  488. struct drm_connector *msm_dsi_manager_connector_init(u8 id)
  489. {
  490. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  491. struct drm_connector *connector = NULL;
  492. struct dsi_connector *dsi_connector;
  493. int ret;
  494. dsi_connector = kzalloc(sizeof(*dsi_connector), GFP_KERNEL);
  495. if (!dsi_connector)
  496. return ERR_PTR(-ENOMEM);
  497. dsi_connector->id = id;
  498. connector = &dsi_connector->base;
  499. ret = drm_connector_init(msm_dsi->dev, connector,
  500. &dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
  501. if (ret)
  502. return ERR_PTR(ret);
  503. drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);
  504. /* Enable HPD to let hpd event is handled
  505. * when panel is attached to the host.
  506. */
  507. connector->polled = DRM_CONNECTOR_POLL_HPD;
  508. /* Display driver doesn't support interlace now. */
  509. connector->interlace_allowed = 0;
  510. connector->doublescan_allowed = 0;
  511. drm_connector_attach_encoder(connector, msm_dsi->encoder);
  512. return connector;
  513. }
  514. bool msm_dsi_manager_validate_current_config(u8 id)
  515. {
  516. bool is_dual_dsi = IS_DUAL_DSI();
  517. /*
  518. * For dual DSI, we only have one drm panel. For this
  519. * use case, we register only one bridge/connector.
  520. * Skip bridge/connector initialisation if it is
  521. * slave-DSI for dual DSI configuration.
  522. */
  523. if (is_dual_dsi && !IS_MASTER_DSI_LINK(id)) {
  524. DBG("Skip bridge registration for slave DSI->id: %d\n", id);
  525. return false;
  526. }
  527. return true;
  528. }
  529. /* initialize bridge */
  530. struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
  531. {
  532. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  533. struct drm_bridge *bridge = NULL;
  534. struct dsi_bridge *dsi_bridge;
  535. struct drm_encoder *encoder;
  536. int ret;
  537. dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
  538. sizeof(*dsi_bridge), GFP_KERNEL);
  539. if (!dsi_bridge) {
  540. ret = -ENOMEM;
  541. goto fail;
  542. }
  543. dsi_bridge->id = id;
  544. encoder = msm_dsi->encoder;
  545. bridge = &dsi_bridge->base;
  546. bridge->funcs = &dsi_mgr_bridge_funcs;
  547. ret = drm_bridge_attach(encoder, bridge, NULL);
  548. if (ret)
  549. goto fail;
  550. return bridge;
  551. fail:
  552. if (bridge)
  553. msm_dsi_manager_bridge_destroy(bridge);
  554. return ERR_PTR(ret);
  555. }
  556. struct drm_connector *msm_dsi_manager_ext_bridge_init(u8 id)
  557. {
  558. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  559. struct drm_device *dev = msm_dsi->dev;
  560. struct drm_encoder *encoder;
  561. struct drm_bridge *int_bridge, *ext_bridge;
  562. struct drm_connector *connector;
  563. struct list_head *connector_list;
  564. int_bridge = msm_dsi->bridge;
  565. ext_bridge = msm_dsi->external_bridge =
  566. msm_dsi_host_get_bridge(msm_dsi->host);
  567. encoder = msm_dsi->encoder;
  568. /* link the internal dsi bridge to the external bridge */
  569. drm_bridge_attach(encoder, ext_bridge, int_bridge);
  570. /*
  571. * we need the drm_connector created by the external bridge
  572. * driver (or someone else) to feed it to our driver's
  573. * priv->connector[] list, mainly for msm_fbdev_init()
  574. */
  575. connector_list = &dev->mode_config.connector_list;
  576. list_for_each_entry(connector, connector_list, head) {
  577. if (drm_connector_has_possible_encoder(connector, encoder))
  578. return connector;
  579. }
  580. return ERR_PTR(-ENODEV);
  581. }
  582. void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
  583. {
  584. }
  585. int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
  586. {
  587. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  588. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  589. struct mipi_dsi_host *host = msm_dsi->host;
  590. bool is_read = (msg->rx_buf && msg->rx_len);
  591. bool need_sync = (IS_SYNC_NEEDED() && !is_read);
  592. int ret;
  593. if (!msg->tx_buf || !msg->tx_len)
  594. return 0;
  595. /* In dual master case, panel requires the same commands sent to
  596. * both DSI links. Host issues the command trigger to both links
  597. * when DSI_1 calls the cmd transfer function, no matter it happens
  598. * before or after DSI_0 cmd transfer.
  599. */
  600. if (need_sync && (id == DSI_0))
  601. return is_read ? msg->rx_len : msg->tx_len;
  602. if (need_sync && msm_dsi0) {
  603. ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
  604. if (ret) {
  605. pr_err("%s: failed to prepare non-trigger host, %d\n",
  606. __func__, ret);
  607. return ret;
  608. }
  609. }
  610. ret = msm_dsi_host_xfer_prepare(host, msg);
  611. if (ret) {
  612. pr_err("%s: failed to prepare host, %d\n", __func__, ret);
  613. goto restore_host0;
  614. }
  615. ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
  616. msm_dsi_host_cmd_tx(host, msg);
  617. msm_dsi_host_xfer_restore(host, msg);
  618. restore_host0:
  619. if (need_sync && msm_dsi0)
  620. msm_dsi_host_xfer_restore(msm_dsi0->host, msg);
  621. return ret;
  622. }
  623. bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len)
  624. {
  625. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  626. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  627. struct mipi_dsi_host *host = msm_dsi->host;
  628. if (IS_SYNC_NEEDED() && (id == DSI_0))
  629. return false;
  630. if (IS_SYNC_NEEDED() && msm_dsi0)
  631. msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, dma_base, len);
  632. msm_dsi_host_cmd_xfer_commit(host, dma_base, len);
  633. return true;
  634. }
  635. void msm_dsi_manager_attach_dsi_device(int id, u32 device_flags)
  636. {
  637. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  638. struct drm_device *dev = msm_dsi->dev;
  639. struct msm_drm_private *priv;
  640. struct msm_kms *kms;
  641. struct drm_encoder *encoder;
  642. bool cmd_mode;
  643. /*
  644. * drm_device pointer is assigned to msm_dsi only in the modeset_init
  645. * path. If mipi_dsi_attach() happens in DSI driver's probe path
  646. * (generally the case when we're connected to a drm_panel of the type
  647. * mipi_dsi_device), this would be NULL. In such cases, try to set the
  648. * encoder mode in the DSI connector's detect() op.
  649. */
  650. if (!dev)
  651. return;
  652. priv = dev->dev_private;
  653. kms = priv->kms;
  654. encoder = msm_dsi_get_encoder(msm_dsi);
  655. cmd_mode = !(device_flags &
  656. MIPI_DSI_MODE_VIDEO);
  657. if (encoder && kms->funcs->set_encoder_mode)
  658. kms->funcs->set_encoder_mode(kms, encoder, cmd_mode);
  659. }
  660. int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
  661. {
  662. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  663. int id = msm_dsi->id;
  664. int ret;
  665. if (id >= DSI_MAX) {
  666. pr_err("%s: invalid id %d\n", __func__, id);
  667. return -EINVAL;
  668. }
  669. if (msm_dsim->dsi[id]) {
  670. pr_err("%s: dsi%d already registered\n", __func__, id);
  671. return -EBUSY;
  672. }
  673. msm_dsim->dsi[id] = msm_dsi;
  674. ret = dsi_mgr_parse_dual_dsi(msm_dsi->pdev->dev.of_node, id);
  675. if (ret) {
  676. pr_err("%s: failed to parse dual DSI info\n", __func__);
  677. goto fail;
  678. }
  679. ret = dsi_mgr_setup_components(id);
  680. if (ret) {
  681. pr_err("%s: failed to register mipi dsi host for DSI %d\n",
  682. __func__, id);
  683. goto fail;
  684. }
  685. return 0;
  686. fail:
  687. msm_dsim->dsi[id] = NULL;
  688. return ret;
  689. }
  690. void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
  691. {
  692. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  693. if (msm_dsi->host)
  694. msm_dsi_host_unregister(msm_dsi->host);
  695. if (msm_dsi->id >= 0)
  696. msm_dsim->dsi[msm_dsi->id] = NULL;
  697. }