zx_drm_drv.c 6.5 KB

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