kirin_drm_drv.c 6.8 KB

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