dsi_manager.c 23 KB

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