sprd_drm.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Unisoc Inc.
  4. */
  5. #include <linux/component.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/of_graph.h>
  10. #include <linux/of_platform.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_gem_dma_helper.h>
  15. #include <drm/drm_gem_framebuffer_helper.h>
  16. #include <drm/drm_of.h>
  17. #include <drm/drm_probe_helper.h>
  18. #include <drm/drm_vblank.h>
  19. #include "sprd_drm.h"
  20. #define DRIVER_NAME "sprd"
  21. #define DRIVER_DESC "Spreadtrum SoCs' DRM Driver"
  22. #define DRIVER_DATE "20200201"
  23. #define DRIVER_MAJOR 1
  24. #define DRIVER_MINOR 0
  25. static const struct drm_mode_config_helper_funcs sprd_drm_mode_config_helper = {
  26. .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
  27. };
  28. static const struct drm_mode_config_funcs sprd_drm_mode_config_funcs = {
  29. .fb_create = drm_gem_fb_create,
  30. .atomic_check = drm_atomic_helper_check,
  31. .atomic_commit = drm_atomic_helper_commit,
  32. };
  33. static void sprd_drm_mode_config_init(struct drm_device *drm)
  34. {
  35. drm->mode_config.min_width = 0;
  36. drm->mode_config.min_height = 0;
  37. drm->mode_config.max_width = 8192;
  38. drm->mode_config.max_height = 8192;
  39. drm->mode_config.funcs = &sprd_drm_mode_config_funcs;
  40. drm->mode_config.helper_private = &sprd_drm_mode_config_helper;
  41. }
  42. DEFINE_DRM_GEM_DMA_FOPS(sprd_drm_fops);
  43. static struct drm_driver sprd_drm_drv = {
  44. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  45. .fops = &sprd_drm_fops,
  46. /* GEM Operations */
  47. DRM_GEM_DMA_DRIVER_OPS,
  48. .name = DRIVER_NAME,
  49. .desc = DRIVER_DESC,
  50. .date = DRIVER_DATE,
  51. .major = DRIVER_MAJOR,
  52. .minor = DRIVER_MINOR,
  53. };
  54. static int sprd_drm_bind(struct device *dev)
  55. {
  56. struct platform_device *pdev = to_platform_device(dev);
  57. struct drm_device *drm;
  58. struct sprd_drm *sprd;
  59. int ret;
  60. sprd = devm_drm_dev_alloc(dev, &sprd_drm_drv, struct sprd_drm, drm);
  61. if (IS_ERR(sprd))
  62. return PTR_ERR(sprd);
  63. drm = &sprd->drm;
  64. platform_set_drvdata(pdev, drm);
  65. ret = drmm_mode_config_init(drm);
  66. if (ret)
  67. return ret;
  68. sprd_drm_mode_config_init(drm);
  69. /* bind and init sub drivers */
  70. ret = component_bind_all(drm->dev, drm);
  71. if (ret) {
  72. drm_err(drm, "failed to bind all component.\n");
  73. return ret;
  74. }
  75. /* vblank init */
  76. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  77. if (ret) {
  78. drm_err(drm, "failed to initialize vblank.\n");
  79. goto err_unbind_all;
  80. }
  81. /* reset all the states of crtc/plane/encoder/connector */
  82. drm_mode_config_reset(drm);
  83. /* init kms poll for handling hpd */
  84. drm_kms_helper_poll_init(drm);
  85. ret = drm_dev_register(drm, 0);
  86. if (ret < 0)
  87. goto err_kms_helper_poll_fini;
  88. return 0;
  89. err_kms_helper_poll_fini:
  90. drm_kms_helper_poll_fini(drm);
  91. err_unbind_all:
  92. component_unbind_all(drm->dev, drm);
  93. return ret;
  94. }
  95. static void sprd_drm_unbind(struct device *dev)
  96. {
  97. struct drm_device *drm = dev_get_drvdata(dev);
  98. drm_dev_unregister(drm);
  99. drm_kms_helper_poll_fini(drm);
  100. component_unbind_all(drm->dev, drm);
  101. }
  102. static const struct component_master_ops drm_component_ops = {
  103. .bind = sprd_drm_bind,
  104. .unbind = sprd_drm_unbind,
  105. };
  106. static int sprd_drm_probe(struct platform_device *pdev)
  107. {
  108. return drm_of_component_probe(&pdev->dev, component_compare_of, &drm_component_ops);
  109. }
  110. static int sprd_drm_remove(struct platform_device *pdev)
  111. {
  112. component_master_del(&pdev->dev, &drm_component_ops);
  113. return 0;
  114. }
  115. static void sprd_drm_shutdown(struct platform_device *pdev)
  116. {
  117. struct drm_device *drm = platform_get_drvdata(pdev);
  118. if (!drm) {
  119. dev_warn(&pdev->dev, "drm device is not available, no shutdown\n");
  120. return;
  121. }
  122. drm_atomic_helper_shutdown(drm);
  123. }
  124. static const struct of_device_id drm_match_table[] = {
  125. { .compatible = "sprd,display-subsystem", },
  126. { /* sentinel */ },
  127. };
  128. MODULE_DEVICE_TABLE(of, drm_match_table);
  129. static struct platform_driver sprd_drm_driver = {
  130. .probe = sprd_drm_probe,
  131. .remove = sprd_drm_remove,
  132. .shutdown = sprd_drm_shutdown,
  133. .driver = {
  134. .name = "sprd-drm-drv",
  135. .of_match_table = drm_match_table,
  136. },
  137. };
  138. static struct platform_driver *sprd_drm_drivers[] = {
  139. &sprd_drm_driver,
  140. &sprd_dpu_driver,
  141. &sprd_dsi_driver,
  142. };
  143. static int __init sprd_drm_init(void)
  144. {
  145. if (drm_firmware_drivers_only())
  146. return -ENODEV;
  147. return platform_register_drivers(sprd_drm_drivers,
  148. ARRAY_SIZE(sprd_drm_drivers));
  149. }
  150. static void __exit sprd_drm_exit(void)
  151. {
  152. platform_unregister_drivers(sprd_drm_drivers,
  153. ARRAY_SIZE(sprd_drm_drivers));
  154. }
  155. module_init(sprd_drm_init);
  156. module_exit(sprd_drm_exit);
  157. MODULE_AUTHOR("Leon He <[email protected]>");
  158. MODULE_AUTHOR("Kevin Tang <[email protected]>");
  159. MODULE_DESCRIPTION("Unisoc DRM KMS Master Driver");
  160. MODULE_LICENSE("GPL v2");