zx_drm_drv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_of.h>
  24. #include <drm/drmP.h>
  25. #include "zx_drm_drv.h"
  26. #include "zx_vou.h"
  27. struct zx_drm_private {
  28. struct drm_fbdev_cma *fbdev;
  29. };
  30. static void zx_drm_fb_output_poll_changed(struct drm_device *drm)
  31. {
  32. struct zx_drm_private *priv = drm->dev_private;
  33. drm_fbdev_cma_hotplug_event(priv->fbdev);
  34. }
  35. static const struct drm_mode_config_funcs zx_drm_mode_config_funcs = {
  36. .fb_create = drm_fb_cma_create,
  37. .output_poll_changed = zx_drm_fb_output_poll_changed,
  38. .atomic_check = drm_atomic_helper_check,
  39. .atomic_commit = drm_atomic_helper_commit,
  40. };
  41. static void zx_drm_lastclose(struct drm_device *drm)
  42. {
  43. struct zx_drm_private *priv = drm->dev_private;
  44. drm_fbdev_cma_restore_mode(priv->fbdev);
  45. }
  46. DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
  47. static struct drm_driver zx_drm_driver = {
  48. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  49. DRIVER_ATOMIC,
  50. .lastclose = zx_drm_lastclose,
  51. .gem_free_object = drm_gem_cma_free_object,
  52. .gem_vm_ops = &drm_gem_cma_vm_ops,
  53. .dumb_create = drm_gem_cma_dumb_create,
  54. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  55. .dumb_destroy = drm_gem_dumb_destroy,
  56. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  57. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  58. .gem_prime_export = drm_gem_prime_export,
  59. .gem_prime_import = drm_gem_prime_import,
  60. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  61. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  62. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  63. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  64. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  65. .fops = &zx_drm_fops,
  66. .name = "zx-vou",
  67. .desc = "ZTE VOU Controller DRM",
  68. .date = "20160811",
  69. .major = 1,
  70. .minor = 0,
  71. };
  72. static int zx_drm_bind(struct device *dev)
  73. {
  74. struct drm_device *drm;
  75. struct zx_drm_private *priv;
  76. int ret;
  77. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  78. if (!priv)
  79. return -ENOMEM;
  80. drm = drm_dev_alloc(&zx_drm_driver, dev);
  81. if (IS_ERR(drm))
  82. return PTR_ERR(drm);
  83. drm->dev_private = priv;
  84. dev_set_drvdata(dev, drm);
  85. drm_mode_config_init(drm);
  86. drm->mode_config.min_width = 16;
  87. drm->mode_config.min_height = 16;
  88. drm->mode_config.max_width = 4096;
  89. drm->mode_config.max_height = 4096;
  90. drm->mode_config.funcs = &zx_drm_mode_config_funcs;
  91. ret = component_bind_all(dev, drm);
  92. if (ret) {
  93. DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
  94. goto out_unregister;
  95. }
  96. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  97. if (ret < 0) {
  98. DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
  99. goto out_unbind;
  100. }
  101. /*
  102. * We will manage irq handler on our own. In this case, irq_enabled
  103. * need to be true for using vblank core support.
  104. */
  105. drm->irq_enabled = true;
  106. drm_mode_config_reset(drm);
  107. drm_kms_helper_poll_init(drm);
  108. priv->fbdev = drm_fbdev_cma_init(drm, 32,
  109. drm->mode_config.num_connector);
  110. if (IS_ERR(priv->fbdev)) {
  111. ret = PTR_ERR(priv->fbdev);
  112. DRM_DEV_ERROR(dev, "failed to init cma fbdev: %d\n", ret);
  113. priv->fbdev = NULL;
  114. goto out_poll_fini;
  115. }
  116. ret = drm_dev_register(drm, 0);
  117. if (ret)
  118. goto out_fbdev_fini;
  119. return 0;
  120. out_fbdev_fini:
  121. if (priv->fbdev) {
  122. drm_fbdev_cma_fini(priv->fbdev);
  123. priv->fbdev = NULL;
  124. }
  125. out_poll_fini:
  126. drm_kms_helper_poll_fini(drm);
  127. drm_mode_config_cleanup(drm);
  128. drm_vblank_cleanup(drm);
  129. out_unbind:
  130. component_unbind_all(dev, drm);
  131. out_unregister:
  132. dev_set_drvdata(dev, NULL);
  133. drm->dev_private = NULL;
  134. drm_dev_unref(drm);
  135. return ret;
  136. }
  137. static void zx_drm_unbind(struct device *dev)
  138. {
  139. struct drm_device *drm = dev_get_drvdata(dev);
  140. struct zx_drm_private *priv = drm->dev_private;
  141. drm_dev_unregister(drm);
  142. if (priv->fbdev) {
  143. drm_fbdev_cma_fini(priv->fbdev);
  144. priv->fbdev = NULL;
  145. }
  146. drm_kms_helper_poll_fini(drm);
  147. drm_mode_config_cleanup(drm);
  148. drm_vblank_cleanup(drm);
  149. component_unbind_all(dev, drm);
  150. dev_set_drvdata(dev, NULL);
  151. drm->dev_private = NULL;
  152. drm_dev_unref(drm);
  153. }
  154. static const struct component_master_ops zx_drm_master_ops = {
  155. .bind = zx_drm_bind,
  156. .unbind = zx_drm_unbind,
  157. };
  158. static int compare_of(struct device *dev, void *data)
  159. {
  160. return dev->of_node == data;
  161. }
  162. static int zx_drm_probe(struct platform_device *pdev)
  163. {
  164. struct device *dev = &pdev->dev;
  165. struct device_node *parent = dev->of_node;
  166. struct device_node *child;
  167. struct component_match *match = NULL;
  168. int ret;
  169. ret = of_platform_populate(parent, NULL, NULL, dev);
  170. if (ret)
  171. return ret;
  172. for_each_available_child_of_node(parent, child) {
  173. component_match_add(dev, &match, compare_of, child);
  174. of_node_put(child);
  175. }
  176. return component_master_add_with_match(dev, &zx_drm_master_ops, match);
  177. }
  178. static int zx_drm_remove(struct platform_device *pdev)
  179. {
  180. component_master_del(&pdev->dev, &zx_drm_master_ops);
  181. return 0;
  182. }
  183. static const struct of_device_id zx_drm_of_match[] = {
  184. { .compatible = "zte,zx296718-vou", },
  185. { /* end */ },
  186. };
  187. MODULE_DEVICE_TABLE(of, zx_drm_of_match);
  188. static struct platform_driver zx_drm_platform_driver = {
  189. .probe = zx_drm_probe,
  190. .remove = zx_drm_remove,
  191. .driver = {
  192. .name = "zx-drm",
  193. .of_match_table = zx_drm_of_match,
  194. },
  195. };
  196. static struct platform_driver *drivers[] = {
  197. &zx_crtc_driver,
  198. &zx_hdmi_driver,
  199. &zx_tvenc_driver,
  200. &zx_drm_platform_driver,
  201. };
  202. static int zx_drm_init(void)
  203. {
  204. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  205. }
  206. module_init(zx_drm_init);
  207. static void zx_drm_exit(void)
  208. {
  209. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  210. }
  211. module_exit(zx_drm_exit);
  212. MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
  213. MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
  214. MODULE_LICENSE("GPL v2");