dsi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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->pdev = pdev;
  68. platform_set_drvdata(pdev, msm_dsi);
  69. /* Init dsi host */
  70. ret = msm_dsi_host_init(msm_dsi);
  71. if (ret)
  72. goto destroy_dsi;
  73. /* GET dsi PHY */
  74. ret = dsi_get_phy(msm_dsi);
  75. if (ret)
  76. goto destroy_dsi;
  77. /* Register to dsi manager */
  78. ret = msm_dsi_manager_register(msm_dsi);
  79. if (ret)
  80. goto destroy_dsi;
  81. return msm_dsi;
  82. destroy_dsi:
  83. dsi_destroy(msm_dsi);
  84. return ERR_PTR(ret);
  85. }
  86. static int dsi_bind(struct device *dev, struct device *master, void *data)
  87. {
  88. struct drm_device *drm = dev_get_drvdata(master);
  89. struct msm_drm_private *priv = drm->dev_private;
  90. struct platform_device *pdev = to_platform_device(dev);
  91. struct msm_dsi *msm_dsi;
  92. DBG("");
  93. msm_dsi = dsi_init(pdev);
  94. if (IS_ERR(msm_dsi))
  95. return PTR_ERR(msm_dsi);
  96. priv->dsi[msm_dsi->id] = msm_dsi;
  97. return 0;
  98. }
  99. static void dsi_unbind(struct device *dev, struct device *master,
  100. void *data)
  101. {
  102. struct drm_device *drm = dev_get_drvdata(master);
  103. struct msm_drm_private *priv = drm->dev_private;
  104. struct msm_dsi *msm_dsi = dev_get_drvdata(dev);
  105. int id = msm_dsi->id;
  106. if (priv->dsi[id]) {
  107. dsi_destroy(msm_dsi);
  108. priv->dsi[id] = NULL;
  109. }
  110. }
  111. static const struct component_ops dsi_ops = {
  112. .bind = dsi_bind,
  113. .unbind = dsi_unbind,
  114. };
  115. static int dsi_dev_probe(struct platform_device *pdev)
  116. {
  117. return component_add(&pdev->dev, &dsi_ops);
  118. }
  119. static int dsi_dev_remove(struct platform_device *pdev)
  120. {
  121. DBG("");
  122. component_del(&pdev->dev, &dsi_ops);
  123. return 0;
  124. }
  125. static const struct of_device_id dt_match[] = {
  126. { .compatible = "qcom,mdss-dsi-ctrl" },
  127. {}
  128. };
  129. static const struct dev_pm_ops dsi_pm_ops = {
  130. SET_RUNTIME_PM_OPS(msm_dsi_runtime_suspend, msm_dsi_runtime_resume, NULL)
  131. };
  132. static struct platform_driver dsi_driver = {
  133. .probe = dsi_dev_probe,
  134. .remove = dsi_dev_remove,
  135. .driver = {
  136. .name = "msm_dsi",
  137. .of_match_table = dt_match,
  138. .pm = &dsi_pm_ops,
  139. },
  140. };
  141. void __init msm_dsi_register(void)
  142. {
  143. DBG("");
  144. msm_dsi_phy_driver_register();
  145. platform_driver_register(&dsi_driver);
  146. }
  147. void __exit msm_dsi_unregister(void)
  148. {
  149. DBG("");
  150. msm_dsi_phy_driver_unregister();
  151. platform_driver_unregister(&dsi_driver);
  152. }
  153. int msm_dsi_modeset_init(struct msm_dsi *msm_dsi, struct drm_device *dev,
  154. struct drm_encoder *encoder)
  155. {
  156. struct msm_drm_private *priv = dev->dev_private;
  157. struct drm_bridge *ext_bridge;
  158. int ret;
  159. if (WARN_ON(!encoder))
  160. return -EINVAL;
  161. msm_dsi->dev = dev;
  162. ret = msm_dsi_host_modeset_init(msm_dsi->host, dev);
  163. if (ret) {
  164. dev_err(dev->dev, "failed to modeset init host: %d\n", ret);
  165. goto fail;
  166. }
  167. msm_dsi->encoder = encoder;
  168. msm_dsi->bridge = msm_dsi_manager_bridge_init(msm_dsi->id);
  169. if (IS_ERR(msm_dsi->bridge)) {
  170. ret = PTR_ERR(msm_dsi->bridge);
  171. dev_err(dev->dev, "failed to create dsi bridge: %d\n", ret);
  172. msm_dsi->bridge = NULL;
  173. goto fail;
  174. }
  175. /*
  176. * check if the dsi encoder output is connected to a panel or an
  177. * external bridge. We create a connector only if we're connected to a
  178. * drm_panel device. When we're connected to an external bridge, we
  179. * assume that the drm_bridge driver will create the connector itself.
  180. */
  181. ext_bridge = msm_dsi_host_get_bridge(msm_dsi->host);
  182. if (ext_bridge)
  183. msm_dsi->connector =
  184. msm_dsi_manager_ext_bridge_init(msm_dsi->id);
  185. else
  186. msm_dsi->connector =
  187. msm_dsi_manager_connector_init(msm_dsi->id);
  188. if (IS_ERR(msm_dsi->connector)) {
  189. ret = PTR_ERR(msm_dsi->connector);
  190. dev_err(dev->dev,
  191. "failed to create dsi connector: %d\n", ret);
  192. msm_dsi->connector = NULL;
  193. goto fail;
  194. }
  195. priv->bridges[priv->num_bridges++] = msm_dsi->bridge;
  196. priv->connectors[priv->num_connectors++] = msm_dsi->connector;
  197. return 0;
  198. fail:
  199. if (msm_dsi) {
  200. /* bridge/connector are normally destroyed by drm: */
  201. if (msm_dsi->bridge) {
  202. msm_dsi_manager_bridge_destroy(msm_dsi->bridge);
  203. msm_dsi->bridge = NULL;
  204. }
  205. /* don't destroy connector if we didn't make it */
  206. if (msm_dsi->connector && !msm_dsi->external_bridge)
  207. msm_dsi->connector->funcs->destroy(msm_dsi->connector);
  208. msm_dsi->connector = NULL;
  209. }
  210. return ret;
  211. }