zynqmp_dpsub.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ZynqMP DisplayPort Subsystem Driver
  4. *
  5. * Copyright (C) 2017 - 2020 Xilinx, Inc.
  6. *
  7. * Authors:
  8. * - Hyun Woo Kwon <hyun.kwon@xilinx.com>
  9. * - Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/module.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_bridge.h>
  21. #include <drm/drm_modeset_helper.h>
  22. #include <drm/drm_module.h>
  23. #include "zynqmp_disp.h"
  24. #include "zynqmp_dp.h"
  25. #include "zynqmp_dpsub.h"
  26. #include "zynqmp_kms.h"
  27. /* -----------------------------------------------------------------------------
  28. * Power Management
  29. */
  30. static int __maybe_unused zynqmp_dpsub_suspend(struct device *dev)
  31. {
  32. struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
  33. if (!dpsub->drm)
  34. return 0;
  35. return drm_mode_config_helper_suspend(&dpsub->drm->dev);
  36. }
  37. static int __maybe_unused zynqmp_dpsub_resume(struct device *dev)
  38. {
  39. struct zynqmp_dpsub *dpsub = dev_get_drvdata(dev);
  40. if (!dpsub->drm)
  41. return 0;
  42. return drm_mode_config_helper_resume(&dpsub->drm->dev);
  43. }
  44. static const struct dev_pm_ops zynqmp_dpsub_pm_ops = {
  45. SET_SYSTEM_SLEEP_PM_OPS(zynqmp_dpsub_suspend, zynqmp_dpsub_resume)
  46. };
  47. /* -----------------------------------------------------------------------------
  48. * Probe & Remove
  49. */
  50. static int zynqmp_dpsub_init_clocks(struct zynqmp_dpsub *dpsub)
  51. {
  52. int ret;
  53. dpsub->apb_clk = devm_clk_get(dpsub->dev, "dp_apb_clk");
  54. if (IS_ERR(dpsub->apb_clk))
  55. return PTR_ERR(dpsub->apb_clk);
  56. ret = clk_prepare_enable(dpsub->apb_clk);
  57. if (ret) {
  58. dev_err(dpsub->dev, "failed to enable the APB clock\n");
  59. return ret;
  60. }
  61. /*
  62. * Try the live PL video clock, and fall back to the PS clock if the
  63. * live PL video clock isn't valid.
  64. */
  65. dpsub->vid_clk = devm_clk_get(dpsub->dev, "dp_live_video_in_clk");
  66. if (!IS_ERR(dpsub->vid_clk))
  67. dpsub->vid_clk_from_ps = false;
  68. else if (PTR_ERR(dpsub->vid_clk) == -EPROBE_DEFER)
  69. return PTR_ERR(dpsub->vid_clk);
  70. if (IS_ERR_OR_NULL(dpsub->vid_clk)) {
  71. dpsub->vid_clk = devm_clk_get(dpsub->dev, "dp_vtc_pixel_clk_in");
  72. if (IS_ERR(dpsub->vid_clk)) {
  73. dev_err(dpsub->dev, "failed to init any video clock\n");
  74. return PTR_ERR(dpsub->vid_clk);
  75. }
  76. dpsub->vid_clk_from_ps = true;
  77. }
  78. /*
  79. * Try the live PL audio clock, and fall back to the PS clock if the
  80. * live PL audio clock isn't valid. Missing audio clock disables audio
  81. * but isn't an error.
  82. */
  83. dpsub->aud_clk = devm_clk_get(dpsub->dev, "dp_live_audio_aclk");
  84. if (!IS_ERR(dpsub->aud_clk)) {
  85. dpsub->aud_clk_from_ps = false;
  86. return 0;
  87. }
  88. dpsub->aud_clk = devm_clk_get(dpsub->dev, "dp_aud_clk");
  89. if (!IS_ERR(dpsub->aud_clk)) {
  90. dpsub->aud_clk_from_ps = true;
  91. return 0;
  92. }
  93. dev_info(dpsub->dev, "audio disabled due to missing clock\n");
  94. return 0;
  95. }
  96. static int zynqmp_dpsub_parse_dt(struct zynqmp_dpsub *dpsub)
  97. {
  98. struct device_node *np;
  99. unsigned int i;
  100. /*
  101. * For backward compatibility with old device trees that don't contain
  102. * ports, consider that only the DP output port is connected if no
  103. * ports child no exists.
  104. */
  105. np = of_get_child_by_name(dpsub->dev->of_node, "ports");
  106. of_node_put(np);
  107. if (!np) {
  108. dev_warn(dpsub->dev, "missing ports, update DT bindings\n");
  109. dpsub->connected_ports = BIT(ZYNQMP_DPSUB_PORT_OUT_DP);
  110. dpsub->dma_enabled = true;
  111. return 0;
  112. }
  113. /* Check which ports are connected. */
  114. for (i = 0; i < ZYNQMP_DPSUB_NUM_PORTS; ++i) {
  115. struct device_node *np;
  116. np = of_graph_get_remote_node(dpsub->dev->of_node, i, -1);
  117. if (np) {
  118. dpsub->connected_ports |= BIT(i);
  119. of_node_put(np);
  120. }
  121. }
  122. /* Sanity checks. */
  123. if ((dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_LIVE_VIDEO)) &&
  124. (dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_LIVE_GFX))) {
  125. dev_err(dpsub->dev, "only one live video input is supported\n");
  126. return -EINVAL;
  127. }
  128. if ((dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_LIVE_VIDEO)) ||
  129. (dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_LIVE_GFX))) {
  130. if (dpsub->vid_clk_from_ps) {
  131. dev_err(dpsub->dev,
  132. "live video input requires PL clock\n");
  133. return -EINVAL;
  134. }
  135. } else {
  136. dpsub->dma_enabled = true;
  137. }
  138. if (dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_LIVE_AUDIO))
  139. dev_warn(dpsub->dev, "live audio unsupported, ignoring\n");
  140. if ((dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_OUT_VIDEO)) ||
  141. (dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_OUT_AUDIO)))
  142. dev_warn(dpsub->dev, "output to PL unsupported, ignoring\n");
  143. if (!(dpsub->connected_ports & BIT(ZYNQMP_DPSUB_PORT_OUT_DP))) {
  144. dev_err(dpsub->dev, "DP output port not connected\n");
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. void zynqmp_dpsub_release(struct zynqmp_dpsub *dpsub)
  150. {
  151. kfree(dpsub->disp);
  152. kfree(dpsub);
  153. }
  154. static int zynqmp_dpsub_probe(struct platform_device *pdev)
  155. {
  156. struct zynqmp_dpsub *dpsub;
  157. int ret;
  158. /* Allocate private data. */
  159. dpsub = kzalloc_obj(*dpsub);
  160. if (!dpsub)
  161. return -ENOMEM;
  162. dpsub->dev = &pdev->dev;
  163. platform_set_drvdata(pdev, dpsub);
  164. ret = dma_set_mask(dpsub->dev, DMA_BIT_MASK(ZYNQMP_DISP_MAX_DMA_BIT));
  165. if (ret)
  166. return ret;
  167. dma_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
  168. /* Try the reserved memory. Proceed if there's none. */
  169. of_reserved_mem_device_init(&pdev->dev);
  170. ret = zynqmp_dpsub_init_clocks(dpsub);
  171. if (ret < 0)
  172. goto err_mem;
  173. ret = zynqmp_dpsub_parse_dt(dpsub);
  174. if (ret < 0)
  175. goto err_mem;
  176. pm_runtime_enable(&pdev->dev);
  177. /*
  178. * DP should be probed first so that the zynqmp_disp can set the output
  179. * format accordingly.
  180. */
  181. ret = zynqmp_dp_probe(dpsub);
  182. if (ret)
  183. goto err_pm;
  184. ret = zynqmp_disp_probe(dpsub);
  185. if (ret)
  186. goto err_dp;
  187. drm_bridge_add(dpsub->bridge);
  188. if (dpsub->dma_enabled) {
  189. ret = zynqmp_dpsub_drm_init(dpsub);
  190. if (ret)
  191. goto err_disp;
  192. }
  193. ret = zynqmp_audio_init(dpsub);
  194. if (ret)
  195. goto err_drm_cleanup;
  196. dev_info(&pdev->dev, "ZynqMP DisplayPort Subsystem driver probed");
  197. return 0;
  198. err_drm_cleanup:
  199. if (dpsub->drm)
  200. zynqmp_dpsub_drm_cleanup(dpsub);
  201. err_disp:
  202. drm_bridge_remove(dpsub->bridge);
  203. zynqmp_disp_remove(dpsub);
  204. err_dp:
  205. zynqmp_dp_remove(dpsub);
  206. err_pm:
  207. pm_runtime_disable(&pdev->dev);
  208. clk_disable_unprepare(dpsub->apb_clk);
  209. err_mem:
  210. of_reserved_mem_device_release(&pdev->dev);
  211. if (!dpsub->drm)
  212. zynqmp_dpsub_release(dpsub);
  213. return ret;
  214. }
  215. static void zynqmp_dpsub_remove(struct platform_device *pdev)
  216. {
  217. struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
  218. zynqmp_audio_uninit(dpsub);
  219. if (dpsub->drm)
  220. zynqmp_dpsub_drm_cleanup(dpsub);
  221. drm_bridge_remove(dpsub->bridge);
  222. zynqmp_disp_remove(dpsub);
  223. zynqmp_dp_remove(dpsub);
  224. pm_runtime_disable(&pdev->dev);
  225. clk_disable_unprepare(dpsub->apb_clk);
  226. of_reserved_mem_device_release(&pdev->dev);
  227. if (!dpsub->drm)
  228. zynqmp_dpsub_release(dpsub);
  229. }
  230. static void zynqmp_dpsub_shutdown(struct platform_device *pdev)
  231. {
  232. struct zynqmp_dpsub *dpsub = platform_get_drvdata(pdev);
  233. if (!dpsub->drm)
  234. return;
  235. drm_atomic_helper_shutdown(&dpsub->drm->dev);
  236. }
  237. static const struct of_device_id zynqmp_dpsub_of_match[] = {
  238. { .compatible = "xlnx,zynqmp-dpsub-1.7", },
  239. { /* end of table */ },
  240. };
  241. MODULE_DEVICE_TABLE(of, zynqmp_dpsub_of_match);
  242. static struct platform_driver zynqmp_dpsub_driver = {
  243. .probe = zynqmp_dpsub_probe,
  244. .remove = zynqmp_dpsub_remove,
  245. .shutdown = zynqmp_dpsub_shutdown,
  246. .driver = {
  247. .name = "zynqmp-dpsub",
  248. .pm = &zynqmp_dpsub_pm_ops,
  249. .of_match_table = zynqmp_dpsub_of_match,
  250. },
  251. };
  252. drm_module_platform_driver(zynqmp_dpsub_driver);
  253. MODULE_AUTHOR("Xilinx, Inc.");
  254. MODULE_DESCRIPTION("ZynqMP DP Subsystem Driver");
  255. MODULE_LICENSE("GPL v2");