dsi.c 6.1 KB

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