dsi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 "dsi.h"
  14. struct drm_encoder *msm_dsi_get_encoder(struct msm_dsi *msm_dsi)
  15. {
  16. if (!msm_dsi || !msm_dsi_device_connected(msm_dsi))
  17. return NULL;
  18. return (msm_dsi->device_flags & MIPI_DSI_MODE_VIDEO) ?
  19. msm_dsi->encoders[MSM_DSI_VIDEO_ENCODER_ID] :
  20. msm_dsi->encoders[MSM_DSI_CMD_ENCODER_ID];
  21. }
  22. static int dsi_get_phy(struct msm_dsi *msm_dsi)
  23. {
  24. struct platform_device *pdev = msm_dsi->pdev;
  25. struct platform_device *phy_pdev;
  26. struct device_node *phy_node;
  27. phy_node = of_parse_phandle(pdev->dev.of_node, "qcom,dsi-phy", 0);
  28. if (!phy_node) {
  29. dev_err(&pdev->dev, "cannot find phy device\n");
  30. return -ENXIO;
  31. }
  32. phy_pdev = of_find_device_by_node(phy_node);
  33. if (phy_pdev)
  34. msm_dsi->phy = platform_get_drvdata(phy_pdev);
  35. of_node_put(phy_node);
  36. if (!phy_pdev || !msm_dsi->phy) {
  37. dev_err(&pdev->dev, "%s: phy driver is not ready\n", __func__);
  38. return -EPROBE_DEFER;
  39. }
  40. msm_dsi->phy_dev = get_device(&phy_pdev->dev);
  41. return 0;
  42. }
  43. static void dsi_destroy(struct msm_dsi *msm_dsi)
  44. {
  45. if (!msm_dsi)
  46. return;
  47. msm_dsi_manager_unregister(msm_dsi);
  48. if (msm_dsi->phy_dev) {
  49. put_device(msm_dsi->phy_dev);
  50. msm_dsi->phy = NULL;
  51. msm_dsi->phy_dev = NULL;
  52. }
  53. if (msm_dsi->host) {
  54. msm_dsi_host_destroy(msm_dsi->host);
  55. msm_dsi->host = NULL;
  56. }
  57. platform_set_drvdata(msm_dsi->pdev, NULL);
  58. }
  59. static struct msm_dsi *dsi_init(struct platform_device *pdev)
  60. {
  61. struct msm_dsi *msm_dsi;
  62. int ret;
  63. if (!pdev)
  64. return ERR_PTR(-ENXIO);
  65. msm_dsi = devm_kzalloc(&pdev->dev, sizeof(*msm_dsi), GFP_KERNEL);
  66. if (!msm_dsi)
  67. return ERR_PTR(-ENOMEM);
  68. DBG("dsi probed=%p", msm_dsi);
  69. msm_dsi->pdev = pdev;
  70. platform_set_drvdata(pdev, msm_dsi);
  71. /* Init dsi host */
  72. ret = msm_dsi_host_init(msm_dsi);
  73. if (ret)
  74. goto destroy_dsi;
  75. /* GET dsi PHY */
  76. ret = dsi_get_phy(msm_dsi);
  77. if (ret)
  78. goto destroy_dsi;
  79. /* Register to dsi manager */
  80. ret = msm_dsi_manager_register(msm_dsi);
  81. if (ret)
  82. goto destroy_dsi;
  83. return msm_dsi;
  84. destroy_dsi:
  85. dsi_destroy(msm_dsi);
  86. return ERR_PTR(ret);
  87. }
  88. static int dsi_bind(struct device *dev, struct device *master, void *data)
  89. {
  90. struct drm_device *drm = dev_get_drvdata(master);
  91. struct msm_drm_private *priv = drm->dev_private;
  92. struct platform_device *pdev = to_platform_device(dev);
  93. struct msm_dsi *msm_dsi;
  94. DBG("");
  95. msm_dsi = dsi_init(pdev);
  96. if (IS_ERR(msm_dsi))
  97. return PTR_ERR(msm_dsi);
  98. priv->dsi[msm_dsi->id] = msm_dsi;
  99. return 0;
  100. }
  101. static void dsi_unbind(struct device *dev, struct device *master,
  102. void *data)
  103. {
  104. struct drm_device *drm = dev_get_drvdata(master);
  105. struct msm_drm_private *priv = drm->dev_private;
  106. struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
  107. int id = msm_dsi->id;
  108. if (priv->dsi[id]) {
  109. dsi_destroy(msm_dsi);
  110. priv->dsi[id] = NULL;
  111. }
  112. }
  113. static const struct component_ops dsi_ops = {
  114. .bind = dsi_bind,
  115. .unbind = dsi_unbind,
  116. };
  117. static int dsi_dev_probe(struct platform_device *pdev)
  118. {
  119. return component_add(&pdev->dev, &dsi_ops);
  120. }
  121. static int dsi_dev_remove(struct platform_device *pdev)
  122. {
  123. DBG("");
  124. component_del(&pdev->dev, &dsi_ops);
  125. return 0;
  126. }
  127. static const struct of_device_id dt_match[] = {
  128. { .compatible = "qcom,mdss-dsi-ctrl" },
  129. {}
  130. };
  131. static struct platform_driver dsi_driver = {
  132. .probe = dsi_dev_probe,
  133. .remove = dsi_dev_remove,
  134. .driver = {
  135. .name = "msm_dsi",
  136. .of_match_table = dt_match,
  137. },
  138. };
  139. void __init msm_dsi_register(void)
  140. {
  141. DBG("");
  142. msm_dsi_phy_driver_register();
  143. platform_driver_register(&dsi_driver);
  144. }
  145. void __exit msm_dsi_unregister(void)
  146. {
  147. DBG("");
  148. msm_dsi_phy_driver_unregister();
  149. platform_driver_unregister(&dsi_driver);
  150. }
  151. int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
  152. struct drm_encoder *encoders[MSM_DSI_ENCODER_NUM])
  153. {
  154. struct msm_drm_private *priv = dev->dev_private;
  155. struct drm_bridge *ext_bridge;
  156. int ret, i;
  157. if (WARN_ON(!encoders[MSM_DSI_VIDEO_ENCODER_ID] ||
  158. !encoders[MSM_DSI_CMD_ENCODER_ID]))
  159. return -EINVAL;
  160. msm_dsi->dev = dev;
  161. ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
  162. if (ret) {
  163. dev_err(dev->dev, "failed to modeset init host: %d\n", ret);
  164. goto fail;
  165. }
  166. msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
  167. if (IS_ERR(msm_dsi->bridge)) {
  168. ret = PTR_ERR(msm_dsi->bridge);
  169. dev_err(dev->dev, "failed to create dsi bridge: %d\n", ret);
  170. msm_dsi->bridge = NULL;
  171. goto fail;
  172. }
  173. for (i = 0; i < MSM_DSI_ENCODER_NUM; i++) {
  174. encoders[i]->bridge = msm_dsi->bridge;
  175. msm_dsi->encoders[i] = encoders[i];
  176. }
  177. /*
  178. * check if the dsi encoder output is connected to a panel or an
  179. * external bridge. We create a connector only if we're connected to a
  180. * drm_panel device. When we're connected to an external bridge, we
  181. * assume that the drm_bridge driver will create the connector itself.
  182. */
  183. ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
  184. if (ext_bridge)
  185. msm_dsi->connector =
  186. msm_dsi_manager_ext_bridge_init(msm_dsi->id);
  187. else
  188. msm_dsi->connector =
  189. msm_dsi_manager_connector_init(msm_dsi->id);
  190. if (IS_ERR(msm_dsi->connector)) {
  191. ret = PTR_ERR(msm_dsi->connector);
  192. dev_err(dev->dev,
  193. "failed to create dsi connector: %d\n", ret);
  194. msm_dsi->connector = NULL;
  195. goto fail;
  196. }
  197. priv->bridges[priv->num_bridges++] = msm_dsi->bridge;
  198. priv->connectors[priv->num_connectors++] = msm_dsi->connector;
  199. return 0;
  200. fail:
  201. if (msm_dsi) {
  202. /* bridge/connector are normally destroyed by drm: */
  203. if (msm_dsi->bridge) {
  204. msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
  205. msm_dsi->bridge = NULL;
  206. }
  207. /* don't destroy connector if we didn't make it */
  208. if (msm_dsi->connector && !msm_dsi->external_bridge)
  209. msm_dsi->connector->funcs->destroy(msm_dsi->connector);
  210. msm_dsi->connector = NULL;
  211. }
  212. return ret;
  213. }