zx_drm_drv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright 2016 Linaro Ltd.
  3. * Copyright 2016 ZTE Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/component.h>
  12. #include <linux/list.h>
  13. #include <linux/module.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/spinlock.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <drm/drm_fb_cma_helper.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_gem_cma_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_of.h>
  25. #include <drm/drmP.h>
  26. #include "zx_drm_drv.h"
  27. #include "zx_vou.h"
  28. static const struct drm_mode_config_funcs zx_drm_mode_config_funcs = {
  29. .fb_create = drm_gem_fb_create,
  30. .output_poll_changed = drm_fb_helper_output_poll_changed,
  31. .atomic_check = drm_atomic_helper_check,
  32. .atomic_commit = drm_atomic_helper_commit,
  33. };
  34. DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
  35. static struct drm_driver zx_drm_driver = {
  36. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  37. DRIVER_ATOMIC,
  38. .lastclose = drm_fb_helper_lastclose,
  39. .gem_free_object_unlocked = drm_gem_cma_free_object,
  40. .gem_vm_ops = &drm_gem_cma_vm_ops,
  41. .dumb_create = drm_gem_cma_dumb_create,
  42. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  43. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  44. .gem_prime_export = drm_gem_prime_export,
  45. .gem_prime_import = drm_gem_prime_import,
  46. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  47. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  48. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  49. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  50. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  51. .fops = &zx_drm_fops,
  52. .name = "zx-vou",
  53. .desc = "ZTE VOU Controller DRM",
  54. .date = "20160811",
  55. .major = 1,
  56. .minor = 0,
  57. };
  58. static int zx_drm_bind(struct device *dev)
  59. {
  60. struct drm_device *drm;
  61. int ret;
  62. drm = drm_dev_alloc(&zx_drm_driver, dev);
  63. if (IS_ERR(drm))
  64. return PTR_ERR(drm);
  65. dev_set_drvdata(dev, drm);
  66. drm_mode_config_init(drm);
  67. drm->mode_config.min_width = 16;
  68. drm->mode_config.min_height = 16;
  69. drm->mode_config.max_width = 4096;
  70. drm->mode_config.max_height = 4096;
  71. drm->mode_config.funcs = &zx_drm_mode_config_funcs;
  72. ret = component_bind_all(dev, drm);
  73. if (ret) {
  74. DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
  75. goto out_unregister;
  76. }
  77. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  78. if (ret < 0) {
  79. DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
  80. goto out_unbind;
  81. }
  82. /*
  83. * We will manage irq handler on our own. In this case, irq_enabled
  84. * need to be true for using vblank core support.
  85. */
  86. drm->irq_enabled = true;
  87. drm_mode_config_reset(drm);
  88. drm_kms_helper_poll_init(drm);
  89. ret = drm_fb_cma_fbdev_init(drm, 32, 0);
  90. if (ret) {
  91. DRM_DEV_ERROR(dev, "failed to init cma fbdev: %d\n", ret);
  92. goto out_poll_fini;
  93. }
  94. ret = drm_dev_register(drm, 0);
  95. if (ret)
  96. goto out_fbdev_fini;
  97. return 0;
  98. out_fbdev_fini:
  99. drm_fb_cma_fbdev_fini(drm);
  100. out_poll_fini:
  101. drm_kms_helper_poll_fini(drm);
  102. drm_mode_config_cleanup(drm);
  103. out_unbind:
  104. component_unbind_all(dev, drm);
  105. out_unregister:
  106. dev_set_drvdata(dev, NULL);
  107. drm_dev_unref(drm);
  108. return ret;
  109. }
  110. static void zx_drm_unbind(struct device *dev)
  111. {
  112. struct drm_device *drm = dev_get_drvdata(dev);
  113. drm_dev_unregister(drm);
  114. drm_fb_cma_fbdev_fini(drm);
  115. drm_kms_helper_poll_fini(drm);
  116. drm_mode_config_cleanup(drm);
  117. component_unbind_all(dev, drm);
  118. dev_set_drvdata(dev, NULL);
  119. drm_dev_unref(drm);
  120. }
  121. static const struct component_master_ops zx_drm_master_ops = {
  122. .bind = zx_drm_bind,
  123. .unbind = zx_drm_unbind,
  124. };
  125. static int compare_of(struct device *dev, void *data)
  126. {
  127. return dev->of_node == data;
  128. }
  129. static int zx_drm_probe(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct device_node *parent = dev->of_node;
  133. struct device_node *child;
  134. struct component_match *match = NULL;
  135. int ret;
  136. ret = devm_of_platform_populate(dev);
  137. if (ret)
  138. return ret;
  139. for_each_available_child_of_node(parent, child) {
  140. component_match_add(dev, &match, compare_of, child);
  141. of_node_put(child);
  142. }
  143. return component_master_add_with_match(dev, &zx_drm_master_ops, match);
  144. }
  145. static int zx_drm_remove(struct platform_device *pdev)
  146. {
  147. component_master_del(&pdev->dev, &zx_drm_master_ops);
  148. return 0;
  149. }
  150. static const struct of_device_id zx_drm_of_match[] = {
  151. { .compatible = "zte,zx296718-vou", },
  152. { /* end */ },
  153. };
  154. MODULE_DEVICE_TABLE(of, zx_drm_of_match);
  155. static struct platform_driver zx_drm_platform_driver = {
  156. .probe = zx_drm_probe,
  157. .remove = zx_drm_remove,
  158. .driver = {
  159. .name = "zx-drm",
  160. .of_match_table = zx_drm_of_match,
  161. },
  162. };
  163. static struct platform_driver *drivers[] = {
  164. &zx_crtc_driver,
  165. &zx_hdmi_driver,
  166. &zx_tvenc_driver,
  167. &zx_vga_driver,
  168. &zx_drm_platform_driver,
  169. };
  170. static int zx_drm_init(void)
  171. {
  172. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  173. }
  174. module_init(zx_drm_init);
  175. static void zx_drm_exit(void)
  176. {
  177. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  178. }
  179. module_exit(zx_drm_exit);
  180. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  181. MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
  182. MODULE_LICENSE("GPL v2");