kirin_drm_drv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Hisilicon Kirin SoCs drm master driver
  3. *
  4. * Copyright (c) 2016 Linaro Limited.
  5. * Copyright (c) 2014-2016 Hisilicon Limited.
  6. *
  7. * Author:
  8. * Xinliang Liu <z.liuxinliang@hisilicon.com>
  9. * Xinliang Liu <xinliang.liu@linaro.org>
  10. * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/of_platform.h>
  18. #include <linux/component.h>
  19. #include <linux/of_graph.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_gem_cma_helper.h>
  22. #include <drm/drm_fb_cma_helper.h>
  23. #include <drm/drm_gem_framebuffer_helper.h>
  24. #include <drm/drm_atomic_helper.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drm_of.h>
  27. #include "kirin_drm_drv.h"
  28. static struct kirin_dc_ops *dc_ops;
  29. static int kirin_drm_kms_cleanup(struct drm_device *dev)
  30. {
  31. struct kirin_drm_private *priv = dev->dev_private;
  32. if (priv->fbdev) {
  33. drm_fbdev_cma_fini(priv->fbdev);
  34. priv->fbdev = NULL;
  35. }
  36. drm_kms_helper_poll_fini(dev);
  37. dc_ops->cleanup(to_platform_device(dev->dev));
  38. drm_mode_config_cleanup(dev);
  39. devm_kfree(dev->dev, priv);
  40. dev->dev_private = NULL;
  41. return 0;
  42. }
  43. static void kirin_fbdev_output_poll_changed(struct drm_device *dev)
  44. {
  45. struct kirin_drm_private *priv = dev->dev_private;
  46. drm_fbdev_cma_hotplug_event(priv->fbdev);
  47. }
  48. static const struct drm_mode_config_funcs kirin_drm_mode_config_funcs = {
  49. .fb_create = drm_gem_fb_create,
  50. .output_poll_changed = kirin_fbdev_output_poll_changed,
  51. .atomic_check = drm_atomic_helper_check,
  52. .atomic_commit = drm_atomic_helper_commit,
  53. };
  54. static void kirin_drm_mode_config_init(struct drm_device *dev)
  55. {
  56. dev->mode_config.min_width = 0;
  57. dev->mode_config.min_height = 0;
  58. dev->mode_config.max_width = 2048;
  59. dev->mode_config.max_height = 2048;
  60. dev->mode_config.funcs = &kirin_drm_mode_config_funcs;
  61. }
  62. static int kirin_drm_kms_init(struct drm_device *dev)
  63. {
  64. struct kirin_drm_private *priv;
  65. int ret;
  66. priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
  67. if (!priv)
  68. return -ENOMEM;
  69. dev->dev_private = priv;
  70. dev_set_drvdata(dev->dev, dev);
  71. /* dev->mode_config initialization */
  72. drm_mode_config_init(dev);
  73. kirin_drm_mode_config_init(dev);
  74. /* display controller init */
  75. ret = dc_ops->init(to_platform_device(dev->dev));
  76. if (ret)
  77. goto err_mode_config_cleanup;
  78. /* bind and init sub drivers */
  79. ret = component_bind_all(dev->dev, dev);
  80. if (ret) {
  81. DRM_ERROR("failed to bind all component.\n");
  82. goto err_dc_cleanup;
  83. }
  84. /* vblank init */
  85. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  86. if (ret) {
  87. DRM_ERROR("failed to initialize vblank.\n");
  88. goto err_unbind_all;
  89. }
  90. /* with irq_enabled = true, we can use the vblank feature. */
  91. dev->irq_enabled = true;
  92. /* reset all the states of crtc/plane/encoder/connector */
  93. drm_mode_config_reset(dev);
  94. /* init kms poll for handling hpd */
  95. drm_kms_helper_poll_init(dev);
  96. priv->fbdev = drm_fbdev_cma_init(dev, 32,
  97. dev->mode_config.num_connector);
  98. if (IS_ERR(priv->fbdev)) {
  99. DRM_ERROR("failed to initialize fbdev.\n");
  100. ret = PTR_ERR(priv->fbdev);
  101. goto err_cleanup_poll;
  102. }
  103. return 0;
  104. err_cleanup_poll:
  105. drm_kms_helper_poll_fini(dev);
  106. err_unbind_all:
  107. component_unbind_all(dev->dev, dev);
  108. err_dc_cleanup:
  109. dc_ops->cleanup(to_platform_device(dev->dev));
  110. err_mode_config_cleanup:
  111. drm_mode_config_cleanup(dev);
  112. devm_kfree(dev->dev, priv);
  113. dev->dev_private = NULL;
  114. return ret;
  115. }
  116. DEFINE_DRM_GEM_CMA_FOPS(kirin_drm_fops);
  117. static int kirin_gem_cma_dumb_create(struct drm_file *file,
  118. struct drm_device *dev,
  119. struct drm_mode_create_dumb *args)
  120. {
  121. return drm_gem_cma_dumb_create_internal(file, dev, args);
  122. }
  123. static struct drm_driver kirin_drm_driver = {
  124. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  125. DRIVER_ATOMIC,
  126. .fops = &kirin_drm_fops,
  127. .gem_free_object_unlocked = drm_gem_cma_free_object,
  128. .gem_vm_ops = &drm_gem_cma_vm_ops,
  129. .dumb_create = kirin_gem_cma_dumb_create,
  130. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  131. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  132. .gem_prime_export = drm_gem_prime_export,
  133. .gem_prime_import = drm_gem_prime_import,
  134. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  135. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  136. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  137. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  138. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  139. .name = "kirin",
  140. .desc = "Hisilicon Kirin SoCs' DRM Driver",
  141. .date = "20150718",
  142. .major = 1,
  143. .minor = 0,
  144. };
  145. static int compare_of(struct device *dev, void *data)
  146. {
  147. return dev->of_node == data;
  148. }
  149. static int kirin_drm_bind(struct device *dev)
  150. {
  151. struct drm_driver *driver = &kirin_drm_driver;
  152. struct drm_device *drm_dev;
  153. int ret;
  154. drm_dev = drm_dev_alloc(driver, dev);
  155. if (IS_ERR(drm_dev))
  156. return PTR_ERR(drm_dev);
  157. ret = kirin_drm_kms_init(drm_dev);
  158. if (ret)
  159. goto err_drm_dev_unref;
  160. ret = drm_dev_register(drm_dev, 0);
  161. if (ret)
  162. goto err_kms_cleanup;
  163. return 0;
  164. err_kms_cleanup:
  165. kirin_drm_kms_cleanup(drm_dev);
  166. err_drm_dev_unref:
  167. drm_dev_unref(drm_dev);
  168. return ret;
  169. }
  170. static void kirin_drm_unbind(struct device *dev)
  171. {
  172. struct drm_device *drm_dev = dev_get_drvdata(dev);
  173. drm_dev_unregister(drm_dev);
  174. kirin_drm_kms_cleanup(drm_dev);
  175. drm_dev_unref(drm_dev);
  176. }
  177. static const struct component_master_ops kirin_drm_ops = {
  178. .bind = kirin_drm_bind,
  179. .unbind = kirin_drm_unbind,
  180. };
  181. static int kirin_drm_platform_probe(struct platform_device *pdev)
  182. {
  183. struct device *dev = &pdev->dev;
  184. struct device_node *np = dev->of_node;
  185. struct component_match *match = NULL;
  186. struct device_node *remote;
  187. dc_ops = (struct kirin_dc_ops *)of_device_get_match_data(dev);
  188. if (!dc_ops) {
  189. DRM_ERROR("failed to get dt id data\n");
  190. return -EINVAL;
  191. }
  192. remote = of_graph_get_remote_node(np, 0, 0);
  193. if (!remote)
  194. return -ENODEV;
  195. drm_of_component_match_add(dev, &match, compare_of, remote);
  196. of_node_put(remote);
  197. return component_master_add_with_match(dev, &kirin_drm_ops, match);
  198. return 0;
  199. }
  200. static int kirin_drm_platform_remove(struct platform_device *pdev)
  201. {
  202. component_master_del(&pdev->dev, &kirin_drm_ops);
  203. dc_ops = NULL;
  204. return 0;
  205. }
  206. static const struct of_device_id kirin_drm_dt_ids[] = {
  207. { .compatible = "hisilicon,hi6220-ade",
  208. .data = &ade_dc_ops,
  209. },
  210. { /* end node */ },
  211. };
  212. MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
  213. static struct platform_driver kirin_drm_platform_driver = {
  214. .probe = kirin_drm_platform_probe,
  215. .remove = kirin_drm_platform_remove,
  216. .driver = {
  217. .name = "kirin-drm",
  218. .of_match_table = kirin_drm_dt_ids,
  219. },
  220. };
  221. module_platform_driver(kirin_drm_platform_driver);
  222. MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
  223. MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
  224. MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
  225. MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
  226. MODULE_LICENSE("GPL v2");