dsi_manager.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. struct dsi_connector *dsi_connector = to_dsi_connector(connector);
  170. DBG("");
  171. drm_connector_cleanup(connector);
  172. kfree(dsi_connector);
  173. }
  174. static void dsi_dual_connector_fix_modes(struct drm_connector *connector)
  175. {
  176. struct drm_display_mode *mode, *m;
  177. /* Only support left-right mode */
  178. list_for_each_entry_safe(mode, m, &connector->probed_modes, head) {
  179. mode->clock >>= 1;
  180. mode->hdisplay >>= 1;
  181. mode->hsync_start >>= 1;
  182. mode->hsync_end >>= 1;
  183. mode->htotal >>= 1;
  184. drm_mode_set_name(mode);
  185. }
  186. }
  187. static int dsi_dual_connector_tile_init(
  188. struct drm_connector *connector, int id)
  189. {
  190. struct drm_display_mode *mode;
  191. /* Fake topology id */
  192. char topo_id[8] = {'M', 'S', 'M', 'D', 'U', 'D', 'S', 'I'};
  193. if (connector->tile_group) {
  194. DBG("Tile property has been initialized");
  195. return 0;
  196. }
  197. /* Use the first mode only for now */
  198. mode = list_first_entry(&connector->probed_modes,
  199. struct drm_display_mode,
  200. head);
  201. if (!mode)
  202. return -EINVAL;
  203. connector->tile_group = drm_mode_get_tile_group(
  204. connector->dev, topo_id);
  205. if (!connector->tile_group)
  206. connector->tile_group = drm_mode_create_tile_group(
  207. connector->dev, topo_id);
  208. if (!connector->tile_group) {
  209. pr_err("%s: failed to create tile group\n", __func__);
  210. return -ENOMEM;
  211. }
  212. connector->has_tile = true;
  213. connector->tile_is_single_monitor = true;
  214. /* mode has been fixed */
  215. connector->tile_h_size = mode->hdisplay;
  216. connector->tile_v_size = mode->vdisplay;
  217. /* Only support left-right mode */
  218. connector->num_h_tile = 2;
  219. connector->num_v_tile = 1;
  220. connector->tile_v_loc = 0;
  221. connector->tile_h_loc = (id == DSI_RIGHT) ? 1 : 0;
  222. return 0;
  223. }
  224. static int dsi_mgr_connector_get_modes(struct drm_connector *connector)
  225. {
  226. int id = dsi_mgr_connector_get_id(connector);
  227. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  228. struct drm_panel *panel = msm_dsi->panel;
  229. int ret, num;
  230. if (!panel)
  231. return 0;
  232. /* Since we have 2 connectors, but only 1 drm_panel in dual DSI mode,
  233. * panel should not attach to any connector.
  234. * Only temporarily attach panel to the current connector here,
  235. * to let panel set mode to this connector.
  236. */
  237. drm_panel_attach(panel, connector);
  238. num = drm_panel_get_modes(panel);
  239. drm_panel_detach(panel);
  240. if (!num)
  241. return 0;
  242. if (IS_DUAL_DSI()) {
  243. /* report half resolution to user */
  244. dsi_dual_connector_fix_modes(connector);
  245. ret = dsi_dual_connector_tile_init(connector, id);
  246. if (ret)
  247. return ret;
  248. ret = drm_mode_connector_set_tile_property(connector);
  249. if (ret) {
  250. pr_err("%s: set tile property failed, %d\n",
  251. __func__, ret);
  252. return ret;
  253. }
  254. }
  255. return num;
  256. }
  257. static int dsi_mgr_connector_mode_valid(struct drm_connector *connector,
  258. struct drm_display_mode *mode)
  259. {
  260. int id = dsi_mgr_connector_get_id(connector);
  261. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  262. struct drm_encoder *encoder = msm_dsi_get_encoder(msm_dsi);
  263. struct msm_drm_private *priv = connector->dev->dev_private;
  264. struct msm_kms *kms = priv->kms;
  265. long actual, requested;
  266. DBG("");
  267. requested = 1000 * mode->clock;
  268. actual = kms->funcs->round_pixclk(kms, requested, encoder);
  269. DBG("requested=%ld, actual=%ld", requested, actual);
  270. if (actual != requested)
  271. return MODE_CLOCK_RANGE;
  272. return MODE_OK;
  273. }
  274. static struct drm_encoder *
  275. dsi_mgr_connector_best_encoder(struct drm_connector *connector)
  276. {
  277. int id = dsi_mgr_connector_get_id(connector);
  278. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  279. DBG("");
  280. return msm_dsi_get_encoder(msm_dsi);
  281. }
  282. static void dsi_mgr_bridge_pre_enable(struct drm_bridge *bridge)
  283. {
  284. int id = dsi_mgr_bridge_get_id(bridge);
  285. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  286. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  287. struct mipi_dsi_host *host = msm_dsi->host;
  288. struct drm_panel *panel = msm_dsi->panel;
  289. bool is_dual_dsi = IS_DUAL_DSI();
  290. int ret;
  291. DBG("id=%d", id);
  292. if (!msm_dsi_device_connected(msm_dsi) ||
  293. (is_dual_dsi && (DSI_1 == id)))
  294. return;
  295. ret = msm_dsi_host_power_on(host);
  296. if (ret) {
  297. pr_err("%s: power on host %d failed, %d\n", __func__, id, ret);
  298. goto host_on_fail;
  299. }
  300. if (is_dual_dsi && msm_dsi1) {
  301. ret = msm_dsi_host_power_on(msm_dsi1->host);
  302. if (ret) {
  303. pr_err("%s: power on host1 failed, %d\n",
  304. __func__, ret);
  305. goto host1_on_fail;
  306. }
  307. }
  308. /* Always call panel functions once, because even for dual panels,
  309. * there is only one drm_panel instance.
  310. */
  311. if (panel) {
  312. ret = drm_panel_prepare(panel);
  313. if (ret) {
  314. pr_err("%s: prepare panel %d failed, %d\n", __func__,
  315. id, ret);
  316. goto panel_prep_fail;
  317. }
  318. }
  319. ret = msm_dsi_host_enable(host);
  320. if (ret) {
  321. pr_err("%s: enable host %d failed, %d\n", __func__, id, ret);
  322. goto host_en_fail;
  323. }
  324. if (is_dual_dsi && msm_dsi1) {
  325. ret = msm_dsi_host_enable(msm_dsi1->host);
  326. if (ret) {
  327. pr_err("%s: enable host1 failed, %d\n", __func__, ret);
  328. goto host1_en_fail;
  329. }
  330. }
  331. if (panel) {
  332. ret = drm_panel_enable(panel);
  333. if (ret) {
  334. pr_err("%s: enable panel %d failed, %d\n", __func__, id,
  335. ret);
  336. goto panel_en_fail;
  337. }
  338. }
  339. return;
  340. panel_en_fail:
  341. if (is_dual_dsi && msm_dsi1)
  342. msm_dsi_host_disable(msm_dsi1->host);
  343. host1_en_fail:
  344. msm_dsi_host_disable(host);
  345. host_en_fail:
  346. if (panel)
  347. drm_panel_unprepare(panel);
  348. panel_prep_fail:
  349. if (is_dual_dsi && msm_dsi1)
  350. msm_dsi_host_power_off(msm_dsi1->host);
  351. host1_on_fail:
  352. msm_dsi_host_power_off(host);
  353. host_on_fail:
  354. return;
  355. }
  356. static void dsi_mgr_bridge_enable(struct drm_bridge *bridge)
  357. {
  358. DBG("");
  359. }
  360. static void dsi_mgr_bridge_disable(struct drm_bridge *bridge)
  361. {
  362. DBG("");
  363. }
  364. static void dsi_mgr_bridge_post_disable(struct drm_bridge *bridge)
  365. {
  366. int id = dsi_mgr_bridge_get_id(bridge);
  367. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  368. struct msm_dsi *msm_dsi1 = dsi_mgr_get_dsi(DSI_1);
  369. struct mipi_dsi_host *host = msm_dsi->host;
  370. struct drm_panel *panel = msm_dsi->panel;
  371. bool is_dual_dsi = IS_DUAL_DSI();
  372. int ret;
  373. DBG("id=%d", id);
  374. if (!msm_dsi_device_connected(msm_dsi) ||
  375. (is_dual_dsi && (DSI_1 == id)))
  376. return;
  377. if (panel) {
  378. ret = drm_panel_disable(panel);
  379. if (ret)
  380. pr_err("%s: Panel %d OFF failed, %d\n", __func__, id,
  381. ret);
  382. }
  383. ret = msm_dsi_host_disable(host);
  384. if (ret)
  385. pr_err("%s: host %d disable failed, %d\n", __func__, id, ret);
  386. if (is_dual_dsi && msm_dsi1) {
  387. ret = msm_dsi_host_disable(msm_dsi1->host);
  388. if (ret)
  389. pr_err("%s: host1 disable failed, %d\n", __func__, ret);
  390. }
  391. if (panel) {
  392. ret = drm_panel_unprepare(panel);
  393. if (ret)
  394. pr_err("%s: Panel %d unprepare failed,%d\n", __func__,
  395. id, ret);
  396. }
  397. ret = msm_dsi_host_power_off(host);
  398. if (ret)
  399. pr_err("%s: host %d power off failed,%d\n", __func__, id, ret);
  400. if (is_dual_dsi && msm_dsi1) {
  401. ret = msm_dsi_host_power_off(msm_dsi1->host);
  402. if (ret)
  403. pr_err("%s: host1 power off failed, %d\n",
  404. __func__, ret);
  405. }
  406. }
  407. static void dsi_mgr_bridge_mode_set(struct drm_bridge *bridge,
  408. struct drm_display_mode *mode,
  409. struct drm_display_mode *adjusted_mode)
  410. {
  411. int id = dsi_mgr_bridge_get_id(bridge);
  412. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  413. struct msm_dsi *other_dsi = dsi_mgr_get_other_dsi(id);
  414. struct mipi_dsi_host *host = msm_dsi->host;
  415. bool is_dual_dsi = IS_DUAL_DSI();
  416. DBG("set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  417. mode->base.id, mode->name,
  418. mode->vrefresh, mode->clock,
  419. mode->hdisplay, mode->hsync_start,
  420. mode->hsync_end, mode->htotal,
  421. mode->vdisplay, mode->vsync_start,
  422. mode->vsync_end, mode->vtotal,
  423. mode->type, mode->flags);
  424. if (is_dual_dsi && (DSI_1 == id))
  425. return;
  426. msm_dsi_host_set_display_mode(host, adjusted_mode);
  427. if (is_dual_dsi && other_dsi)
  428. msm_dsi_host_set_display_mode(other_dsi->host, adjusted_mode);
  429. }
  430. static const struct drm_connector_funcs dsi_mgr_connector_funcs = {
  431. .dpms = drm_atomic_helper_connector_dpms,
  432. .detect = dsi_mgr_connector_detect,
  433. .fill_modes = drm_helper_probe_single_connector_modes,
  434. .destroy = dsi_mgr_connector_destroy,
  435. .reset = drm_atomic_helper_connector_reset,
  436. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  437. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  438. };
  439. static const struct drm_connector_helper_funcs dsi_mgr_conn_helper_funcs = {
  440. .get_modes = dsi_mgr_connector_get_modes,
  441. .mode_valid = dsi_mgr_connector_mode_valid,
  442. .best_encoder = dsi_mgr_connector_best_encoder,
  443. };
  444. static const struct drm_bridge_funcs dsi_mgr_bridge_funcs = {
  445. .pre_enable = dsi_mgr_bridge_pre_enable,
  446. .enable = dsi_mgr_bridge_enable,
  447. .disable = dsi_mgr_bridge_disable,
  448. .post_disable = dsi_mgr_bridge_post_disable,
  449. .mode_set = dsi_mgr_bridge_mode_set,
  450. };
  451. /* initialize connector when we're connected to a drm_panel */
  452. struct drm_connector *msm_dsi_manager_connector_init(u8 id)
  453. {
  454. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  455. struct drm_connector *connector = NULL;
  456. struct dsi_connector *dsi_connector;
  457. int ret, i;
  458. dsi_connector = kzalloc(sizeof(*dsi_connector), GFP_KERNEL);
  459. if (!dsi_connector)
  460. return ERR_PTR(-ENOMEM);
  461. dsi_connector->id = id;
  462. connector = &dsi_connector->base;
  463. ret = drm_connector_init(msm_dsi->dev, connector,
  464. &dsi_mgr_connector_funcs, DRM_MODE_CONNECTOR_DSI);
  465. if (ret)
  466. return ERR_PTR(ret);
  467. drm_connector_helper_add(connector, &dsi_mgr_conn_helper_funcs);
  468. /* Enable HPD to let hpd event is handled
  469. * when panel is attached to the host.
  470. */
  471. connector->polled = DRM_CONNECTOR_POLL_HPD;
  472. /* Display driver doesn't support interlace now. */
  473. connector->interlace_allowed = 0;
  474. connector->doublescan_allowed = 0;
  475. for (i = 0; i < MSM_DSI_ENCODER_NUM; i++)
  476. drm_mode_connector_attach_encoder(connector,
  477. msm_dsi->encoders[i]);
  478. return connector;
  479. }
  480. /* initialize bridge */
  481. struct drm_bridge *msm_dsi_manager_bridge_init(u8 id)
  482. {
  483. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  484. struct drm_bridge *bridge = NULL;
  485. struct dsi_bridge *dsi_bridge;
  486. struct drm_encoder *encoder;
  487. int ret;
  488. dsi_bridge = devm_kzalloc(msm_dsi->dev->dev,
  489. sizeof(*dsi_bridge), GFP_KERNEL);
  490. if (!dsi_bridge) {
  491. ret = -ENOMEM;
  492. goto fail;
  493. }
  494. dsi_bridge->id = id;
  495. /*
  496. * HACK: we may not know the external DSI bridge device's mode
  497. * flags here. We'll get to know them only when the device
  498. * attaches to the dsi host. For now, assume the bridge supports
  499. * DSI video mode
  500. */
  501. encoder = msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID];
  502. bridge = &dsi_bridge->base;
  503. bridge->funcs = &dsi_mgr_bridge_funcs;
  504. ret = drm_bridge_attach(encoder, bridge, NULL);
  505. if (ret)
  506. goto fail;
  507. return bridge;
  508. fail:
  509. if (bridge)
  510. msm_dsi_manager_bridge_destroy(bridge);
  511. return ERR_PTR(ret);
  512. }
  513. struct drm_connector *msm_dsi_manager_ext_bridge_init(u8 id)
  514. {
  515. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  516. struct drm_device *dev = msm_dsi->dev;
  517. struct drm_encoder *encoder;
  518. struct drm_bridge *int_bridge, *ext_bridge;
  519. struct drm_connector *connector;
  520. struct list_head *connector_list;
  521. int_bridge = msm_dsi->bridge;
  522. ext_bridge = msm_dsi->external_bridge =
  523. msm_dsi_host_get_bridge(msm_dsi->host);
  524. /*
  525. * HACK: we may not know the external DSI bridge device's mode
  526. * flags here. We'll get to know them only when the device
  527. * attaches to the dsi host. For now, assume the bridge supports
  528. * DSI video mode
  529. */
  530. encoder = msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID];
  531. /* link the internal dsi bridge to the external bridge */
  532. drm_bridge_attach(encoder, ext_bridge, int_bridge);
  533. /*
  534. * we need the drm_connector created by the external bridge
  535. * driver (or someone else) to feed it to our driver's
  536. * priv->connector[] list, mainly for msm_fbdev_init()
  537. */
  538. connector_list = &dev->mode_config.connector_list;
  539. list_for_each_entry(connector, connector_list, head) {
  540. int i;
  541. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  542. if (connector->encoder_ids[i] == encoder->base.id)
  543. return connector;
  544. }
  545. }
  546. return ERR_PTR(-ENODEV);
  547. }
  548. void msm_dsi_manager_bridge_destroy(struct drm_bridge *bridge)
  549. {
  550. }
  551. int msm_dsi_manager_phy_enable(int id,
  552. const unsigned long bit_rate, const unsigned long esc_rate,
  553. u32 *clk_pre, u32 *clk_post)
  554. {
  555. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  556. struct msm_dsi_phy *phy = msm_dsi->phy;
  557. int src_pll_id = IS_DUAL_DSI() ? DSI_CLOCK_MASTER : id;
  558. struct msm_dsi_pll *pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  559. int ret;
  560. ret = msm_dsi_phy_enable(phy, src_pll_id, bit_rate, esc_rate);
  561. if (ret)
  562. return ret;
  563. /*
  564. * Reset DSI PHY silently changes its PLL registers to reset status,
  565. * which will confuse clock driver and result in wrong output rate of
  566. * link clocks. Restore PLL status if its PLL is being used as clock
  567. * source.
  568. */
  569. if (!IS_DUAL_DSI() || (id == DSI_CLOCK_MASTER)) {
  570. ret = msm_dsi_pll_restore_state(pll);
  571. if (ret) {
  572. pr_err("%s: failed to restore pll state\n", __func__);
  573. msm_dsi_phy_disable(phy);
  574. return ret;
  575. }
  576. }
  577. msm_dsi->phy_enabled = true;
  578. msm_dsi_phy_get_clk_pre_post(phy, clk_pre, clk_post);
  579. return 0;
  580. }
  581. void msm_dsi_manager_phy_disable(int id)
  582. {
  583. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  584. struct msm_dsi *mdsi = dsi_mgr_get_dsi(DSI_CLOCK_MASTER);
  585. struct msm_dsi *sdsi = dsi_mgr_get_dsi(DSI_CLOCK_SLAVE);
  586. struct msm_dsi_phy *phy = msm_dsi->phy;
  587. struct msm_dsi_pll *pll = msm_dsi_phy_get_pll(msm_dsi->phy);
  588. /* Save PLL status if it is a clock source */
  589. if (!IS_DUAL_DSI() || (id == DSI_CLOCK_MASTER))
  590. msm_dsi_pll_save_state(pll);
  591. /* disable DSI phy
  592. * In dual-dsi configuration, the phy should be disabled for the
  593. * first controller only when the second controller is disabled.
  594. */
  595. msm_dsi->phy_enabled = false;
  596. if (IS_DUAL_DSI() && mdsi && sdsi) {
  597. if (!mdsi->phy_enabled && !sdsi->phy_enabled) {
  598. msm_dsi_phy_disable(sdsi->phy);
  599. msm_dsi_phy_disable(mdsi->phy);
  600. }
  601. } else {
  602. msm_dsi_phy_disable(phy);
  603. }
  604. }
  605. int msm_dsi_manager_cmd_xfer(int id, const struct mipi_dsi_msg *msg)
  606. {
  607. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  608. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  609. struct mipi_dsi_host *host = msm_dsi->host;
  610. bool is_read = (msg->rx_buf && msg->rx_len);
  611. bool need_sync = (IS_SYNC_NEEDED() && !is_read);
  612. int ret;
  613. if (!msg->tx_buf || !msg->tx_len)
  614. return 0;
  615. /* In dual master case, panel requires the same commands sent to
  616. * both DSI links. Host issues the command trigger to both links
  617. * when DSI_1 calls the cmd transfer function, no matter it happens
  618. * before or after DSI_0 cmd transfer.
  619. */
  620. if (need_sync && (id == DSI_0))
  621. return is_read ? msg->rx_len : msg->tx_len;
  622. if (need_sync && msm_dsi0) {
  623. ret = msm_dsi_host_xfer_prepare(msm_dsi0->host, msg);
  624. if (ret) {
  625. pr_err("%s: failed to prepare non-trigger host, %d\n",
  626. __func__, ret);
  627. return ret;
  628. }
  629. }
  630. ret = msm_dsi_host_xfer_prepare(host, msg);
  631. if (ret) {
  632. pr_err("%s: failed to prepare host, %d\n", __func__, ret);
  633. goto restore_host0;
  634. }
  635. ret = is_read ? msm_dsi_host_cmd_rx(host, msg) :
  636. msm_dsi_host_cmd_tx(host, msg);
  637. msm_dsi_host_xfer_restore(host, msg);
  638. restore_host0:
  639. if (need_sync && msm_dsi0)
  640. msm_dsi_host_xfer_restore(msm_dsi0->host, msg);
  641. return ret;
  642. }
  643. bool msm_dsi_manager_cmd_xfer_trigger(int id, u32 dma_base, u32 len)
  644. {
  645. struct msm_dsi *msm_dsi = dsi_mgr_get_dsi(id);
  646. struct msm_dsi *msm_dsi0 = dsi_mgr_get_dsi(DSI_0);
  647. struct mipi_dsi_host *host = msm_dsi->host;
  648. if (IS_SYNC_NEEDED() && (id == DSI_0))
  649. return false;
  650. if (IS_SYNC_NEEDED() && msm_dsi0)
  651. msm_dsi_host_cmd_xfer_commit(msm_dsi0->host, dma_base, len);
  652. msm_dsi_host_cmd_xfer_commit(host, dma_base, len);
  653. return true;
  654. }
  655. int msm_dsi_manager_register(struct msm_dsi *msm_dsi)
  656. {
  657. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  658. int id = msm_dsi->id;
  659. int ret;
  660. if (id > DSI_MAX) {
  661. pr_err("%s: invalid id %d\n", __func__, id);
  662. return -EINVAL;
  663. }
  664. if (msm_dsim->dsi[id]) {
  665. pr_err("%s: dsi%d already registered\n", __func__, id);
  666. return -EBUSY;
  667. }
  668. msm_dsim->dsi[id] = msm_dsi;
  669. ret = dsi_mgr_parse_dual_dsi(msm_dsi->pdev->dev.of_node, id);
  670. if (ret) {
  671. pr_err("%s: failed to parse dual DSI info\n", __func__);
  672. goto fail;
  673. }
  674. ret = dsi_mgr_host_register(id);
  675. if (ret) {
  676. pr_err("%s: failed to register mipi dsi host for DSI %d\n",
  677. __func__, id);
  678. goto fail;
  679. }
  680. return 0;
  681. fail:
  682. msm_dsim->dsi[id] = NULL;
  683. return ret;
  684. }
  685. void msm_dsi_manager_unregister(struct msm_dsi *msm_dsi)
  686. {
  687. struct msm_dsi_manager *msm_dsim = &msm_dsim_glb;
  688. if (msm_dsi->host)
  689. msm_dsi_host_unregister(msm_dsi->host);
  690. msm_dsim->dsi[msm_dsi->id] = NULL;
  691. }