zx_drm_drv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. .atomic_check = drm_atomic_helper_check,
  31. .atomic_commit = drm_atomic_helper_commit,
  32. };
  33. DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
  34. static struct drm_driver zx_drm_driver = {
  35. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  36. DRIVER_ATOMIC,
  37. .gem_free_object_unlocked = drm_gem_cma_free_object,
  38. .gem_vm_ops = &drm_gem_cma_vm_ops,
  39. .dumb_create = drm_gem_cma_dumb_create,
  40. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  41. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  42. .gem_prime_export = drm_gem_prime_export,
  43. .gem_prime_import = drm_gem_prime_import,
  44. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  45. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  46. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  47. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  48. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  49. .fops = &zx_drm_fops,
  50. .name = "zx-vou",
  51. .desc = "ZTE VOU Controller DRM",
  52. .date = "20160811",
  53. .major = 1,
  54. .minor = 0,
  55. };
  56. static int zx_drm_bind(struct device *dev)
  57. {
  58. struct drm_device *drm;
  59. int ret;
  60. drm = drm_dev_alloc(&zx_drm_driver, dev);
  61. if (IS_ERR(drm))
  62. return PTR_ERR(drm);
  63. dev_set_drvdata(dev, drm);
  64. drm_mode_config_init(drm);
  65. drm->mode_config.min_width = 16;
  66. drm->mode_config.min_height = 16;
  67. drm->mode_config.max_width = 4096;
  68. drm->mode_config.max_height = 4096;
  69. drm->mode_config.funcs = &zx_drm_mode_config_funcs;
  70. ret = component_bind_all(dev, drm);
  71. if (ret) {
  72. DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
  73. goto out_unregister;
  74. }
  75. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  76. if (ret < 0) {
  77. DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
  78. goto out_unbind;
  79. }
  80. /*
  81. * We will manage irq handler on our own. In this case, irq_enabled
  82. * need to be true for using vblank core support.
  83. */
  84. drm->irq_enabled = true;
  85. drm_mode_config_reset(drm);
  86. drm_kms_helper_poll_init(drm);
  87. ret = drm_dev_register(drm, 0);
  88. if (ret)
  89. goto out_poll_fini;
  90. drm_fbdev_generic_setup(drm, 32);
  91. return 0;
  92. out_poll_fini:
  93. drm_kms_helper_poll_fini(drm);
  94. drm_mode_config_cleanup(drm);
  95. out_unbind:
  96. component_unbind_all(dev, drm);
  97. out_unregister:
  98. dev_set_drvdata(dev, NULL);
  99. drm_dev_unref(drm);
  100. return ret;
  101. }
  102. static void zx_drm_unbind(struct device *dev)
  103. {
  104. struct drm_device *drm = dev_get_drvdata(dev);
  105. drm_dev_unregister(drm);
  106. drm_kms_helper_poll_fini(drm);
  107. drm_mode_config_cleanup(drm);
  108. component_unbind_all(dev, drm);
  109. dev_set_drvdata(dev, NULL);
  110. drm_dev_unref(drm);
  111. }
  112. static const struct component_master_ops zx_drm_master_ops = {
  113. .bind = zx_drm_bind,
  114. .unbind = zx_drm_unbind,
  115. };
  116. static int compare_of(struct device *dev, void *data)
  117. {
  118. return dev->of_node == data;
  119. }
  120. static int zx_drm_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. struct device_node *parent = dev->of_node;
  124. struct device_node *child;
  125. struct component_match *match = NULL;
  126. int ret;
  127. ret = devm_of_platform_populate(dev);
  128. if (ret)
  129. return ret;
  130. for_each_available_child_of_node(parent, child)
  131. component_match_add(dev, &match, compare_of, child);
  132. return component_master_add_with_match(dev, &zx_drm_master_ops, match);
  133. }
  134. static int zx_drm_remove(struct platform_device *pdev)
  135. {
  136. component_master_del(&pdev->dev, &zx_drm_master_ops);
  137. return 0;
  138. }
  139. static const struct of_device_id zx_drm_of_match[] = {
  140. { .compatible = "zte,zx296718-vou", },
  141. { /* end */ },
  142. };
  143. MODULE_DEVICE_TABLE(of, zx_drm_of_match);
  144. static struct platform_driver zx_drm_platform_driver = {
  145. .probe = zx_drm_probe,
  146. .remove = zx_drm_remove,
  147. .driver = {
  148. .name = "zx-drm",
  149. .of_match_table = zx_drm_of_match,
  150. },
  151. };
  152. static struct platform_driver *drivers[] = {
  153. &zx_crtc_driver,
  154. &zx_hdmi_driver,
  155. &zx_tvenc_driver,
  156. &zx_vga_driver,
  157. &zx_drm_platform_driver,
  158. };
  159. static int zx_drm_init(void)
  160. {
  161. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  162. }
  163. module_init(zx_drm_init);
  164. static void zx_drm_exit(void)
  165. {
  166. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  167. }
  168. module_exit(zx_drm_exit);
  169. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  170. MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
  171. MODULE_LICENSE("GPL v2");