dc-drv.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2024 NXP
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/component.h>
  7. #include <linux/device.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm.h>
  15. #include <linux/pm_runtime.h>
  16. #include <drm/clients/drm_client_setup.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_drv.h>
  19. #include <drm/drm_fbdev_dma.h>
  20. #include <drm/drm_fourcc.h>
  21. #include <drm/drm_gem_dma_helper.h>
  22. #include <drm/drm_managed.h>
  23. #include <drm/drm_modeset_helper.h>
  24. #include <drm/drm_of.h>
  25. #include "dc-de.h"
  26. #include "dc-drv.h"
  27. #include "dc-pe.h"
  28. struct dc_priv {
  29. struct drm_device *drm;
  30. struct clk *clk_cfg;
  31. };
  32. DEFINE_DRM_GEM_DMA_FOPS(dc_drm_driver_fops);
  33. static struct drm_driver dc_drm_driver = {
  34. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
  35. DRM_GEM_DMA_DRIVER_OPS,
  36. DRM_FBDEV_DMA_DRIVER_OPS,
  37. .fops = &dc_drm_driver_fops,
  38. .name = "imx8-dc",
  39. .desc = "i.MX8 DC DRM graphics",
  40. .major = 1,
  41. .minor = 0,
  42. .patchlevel = 0,
  43. };
  44. static void
  45. dc_add_components(struct device *dev, struct component_match **matchptr)
  46. {
  47. struct device_node *child, *grandchild;
  48. for_each_available_child_of_node(dev->of_node, child) {
  49. /* The interrupt controller is not a component. */
  50. if (of_device_is_compatible(child, "fsl,imx8qxp-dc-intc"))
  51. continue;
  52. drm_of_component_match_add(dev, matchptr, component_compare_of,
  53. child);
  54. for_each_available_child_of_node(child, grandchild)
  55. drm_of_component_match_add(dev, matchptr,
  56. component_compare_of,
  57. grandchild);
  58. }
  59. }
  60. static int dc_drm_component_bind_all(struct dc_drm_device *dc_drm)
  61. {
  62. struct drm_device *drm = &dc_drm->base;
  63. int ret;
  64. ret = component_bind_all(drm->dev, dc_drm);
  65. if (ret)
  66. return ret;
  67. dc_de_post_bind(dc_drm);
  68. dc_pe_post_bind(dc_drm);
  69. return 0;
  70. }
  71. static void dc_drm_component_unbind_all(void *ptr)
  72. {
  73. struct dc_drm_device *dc_drm = ptr;
  74. struct drm_device *drm = &dc_drm->base;
  75. component_unbind_all(drm->dev, dc_drm);
  76. }
  77. static int dc_drm_bind(struct device *dev)
  78. {
  79. struct dc_priv *priv = dev_get_drvdata(dev);
  80. struct dc_drm_device *dc_drm;
  81. struct drm_device *drm;
  82. int ret;
  83. dc_drm = devm_drm_dev_alloc(dev, &dc_drm_driver, struct dc_drm_device,
  84. base);
  85. if (IS_ERR(dc_drm))
  86. return PTR_ERR(dc_drm);
  87. drm = &dc_drm->base;
  88. ret = dc_drm_component_bind_all(dc_drm);
  89. if (ret)
  90. return ret;
  91. ret = devm_add_action_or_reset(dev, dc_drm_component_unbind_all,
  92. dc_drm);
  93. if (ret)
  94. return ret;
  95. ret = dc_kms_init(dc_drm);
  96. if (ret)
  97. return ret;
  98. ret = drm_dev_register(drm, 0);
  99. if (ret) {
  100. dev_err(dev, "failed to register drm device: %d\n", ret);
  101. goto err;
  102. }
  103. drm_client_setup_with_fourcc(drm, DRM_FORMAT_XRGB8888);
  104. priv->drm = drm;
  105. return 0;
  106. err:
  107. dc_kms_uninit(dc_drm);
  108. return ret;
  109. }
  110. static void dc_drm_unbind(struct device *dev)
  111. {
  112. struct dc_priv *priv = dev_get_drvdata(dev);
  113. struct dc_drm_device *dc_drm = to_dc_drm_device(priv->drm);
  114. struct drm_device *drm = &dc_drm->base;
  115. priv->drm = NULL;
  116. drm_dev_unplug(drm);
  117. dc_kms_uninit(dc_drm);
  118. drm_atomic_helper_shutdown(drm);
  119. }
  120. static const struct component_master_ops dc_drm_ops = {
  121. .bind = dc_drm_bind,
  122. .unbind = dc_drm_unbind,
  123. };
  124. static int dc_probe(struct platform_device *pdev)
  125. {
  126. struct component_match *match = NULL;
  127. struct dc_priv *priv;
  128. int ret;
  129. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  130. if (!priv)
  131. return -ENOMEM;
  132. priv->clk_cfg = devm_clk_get(&pdev->dev, NULL);
  133. if (IS_ERR(priv->clk_cfg))
  134. return dev_err_probe(&pdev->dev, PTR_ERR(priv->clk_cfg),
  135. "failed to get cfg clock\n");
  136. dev_set_drvdata(&pdev->dev, priv);
  137. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  138. if (ret)
  139. return ret;
  140. ret = devm_pm_runtime_enable(&pdev->dev);
  141. if (ret)
  142. return ret;
  143. ret = devm_of_platform_populate(&pdev->dev);
  144. if (ret)
  145. return ret;
  146. dc_add_components(&pdev->dev, &match);
  147. ret = component_master_add_with_match(&pdev->dev, &dc_drm_ops, match);
  148. if (ret)
  149. return dev_err_probe(&pdev->dev, ret,
  150. "failed to add component master\n");
  151. return 0;
  152. }
  153. static void dc_remove(struct platform_device *pdev)
  154. {
  155. component_master_del(&pdev->dev, &dc_drm_ops);
  156. }
  157. static int dc_runtime_suspend(struct device *dev)
  158. {
  159. struct dc_priv *priv = dev_get_drvdata(dev);
  160. clk_disable_unprepare(priv->clk_cfg);
  161. return 0;
  162. }
  163. static int dc_runtime_resume(struct device *dev)
  164. {
  165. struct dc_priv *priv = dev_get_drvdata(dev);
  166. int ret;
  167. ret = clk_prepare_enable(priv->clk_cfg);
  168. if (ret)
  169. dev_err(dev, "failed to enable cfg clock: %d\n", ret);
  170. return ret;
  171. }
  172. static int dc_suspend(struct device *dev)
  173. {
  174. struct dc_priv *priv = dev_get_drvdata(dev);
  175. return drm_mode_config_helper_suspend(priv->drm);
  176. }
  177. static int dc_resume(struct device *dev)
  178. {
  179. struct dc_priv *priv = dev_get_drvdata(dev);
  180. return drm_mode_config_helper_resume(priv->drm);
  181. }
  182. static void dc_shutdown(struct platform_device *pdev)
  183. {
  184. struct dc_priv *priv = dev_get_drvdata(&pdev->dev);
  185. drm_atomic_helper_shutdown(priv->drm);
  186. }
  187. static const struct dev_pm_ops dc_pm_ops = {
  188. RUNTIME_PM_OPS(dc_runtime_suspend, dc_runtime_resume, NULL)
  189. SYSTEM_SLEEP_PM_OPS(dc_suspend, dc_resume)
  190. };
  191. static const struct of_device_id dc_dt_ids[] = {
  192. { .compatible = "fsl,imx8qxp-dc", },
  193. { /* sentinel */ }
  194. };
  195. MODULE_DEVICE_TABLE(of, dc_dt_ids);
  196. static struct platform_driver dc_driver = {
  197. .probe = dc_probe,
  198. .remove = dc_remove,
  199. .shutdown = dc_shutdown,
  200. .driver = {
  201. .name = "imx8-dc",
  202. .of_match_table = dc_dt_ids,
  203. .pm = pm_sleep_ptr(&dc_pm_ops),
  204. },
  205. };
  206. static struct platform_driver * const dc_drivers[] = {
  207. &dc_cf_driver,
  208. &dc_de_driver,
  209. &dc_ed_driver,
  210. &dc_fg_driver,
  211. &dc_fl_driver,
  212. &dc_fw_driver,
  213. &dc_ic_driver,
  214. &dc_lb_driver,
  215. &dc_pe_driver,
  216. &dc_tc_driver,
  217. &dc_driver,
  218. };
  219. static int __init dc_drm_init(void)
  220. {
  221. return platform_register_drivers(dc_drivers, ARRAY_SIZE(dc_drivers));
  222. }
  223. static void __exit dc_drm_exit(void)
  224. {
  225. platform_unregister_drivers(dc_drivers, ARRAY_SIZE(dc_drivers));
  226. }
  227. module_init(dc_drm_init);
  228. module_exit(dc_drm_exit);
  229. MODULE_DESCRIPTION("i.MX8 Display Controller DRM Driver");
  230. MODULE_AUTHOR("Liu Ying <victor.liu@nxp.com>");
  231. MODULE_LICENSE("GPL");