kirin_drm_drv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifdef CONFIG_DRM_FBDEV_EMULATION
  32. if (priv->fbdev) {
  33. drm_fbdev_cma_fini(priv->fbdev);
  34. priv->fbdev = NULL;
  35. }
  36. #endif
  37. drm_kms_helper_poll_fini(dev);
  38. drm_vblank_cleanup(dev);
  39. dc_ops->cleanup(to_platform_device(dev->dev));
  40. drm_mode_config_cleanup(dev);
  41. devm_kfree(dev->dev, priv);
  42. dev->dev_private = NULL;
  43. return 0;
  44. }
  45. #ifdef CONFIG_DRM_FBDEV_EMULATION
  46. static void kirin_fbdev_output_poll_changed(struct drm_device *dev)
  47. {
  48. struct kirin_drm_private *priv = dev->dev_private;
  49. if (priv->fbdev) {
  50. drm_fbdev_cma_hotplug_event(priv->fbdev);
  51. } else {
  52. priv->fbdev = drm_fbdev_cma_init(dev, 32,
  53. dev->mode_config.num_connector);
  54. if (IS_ERR(priv->fbdev))
  55. priv->fbdev = NULL;
  56. }
  57. }
  58. #endif
  59. static const struct drm_mode_config_funcs kirin_drm_mode_config_funcs = {
  60. .fb_create = drm_fb_cma_create,
  61. #ifdef CONFIG_DRM_FBDEV_EMULATION
  62. .output_poll_changed = kirin_fbdev_output_poll_changed,
  63. #endif
  64. .atomic_check = drm_atomic_helper_check,
  65. .atomic_commit = drm_atomic_helper_commit,
  66. };
  67. static void kirin_drm_mode_config_init(struct drm_device *dev)
  68. {
  69. dev->mode_config.min_width = 0;
  70. dev->mode_config.min_height = 0;
  71. dev->mode_config.max_width = 2048;
  72. dev->mode_config.max_height = 2048;
  73. dev->mode_config.funcs = &kirin_drm_mode_config_funcs;
  74. }
  75. static int kirin_drm_kms_init(struct drm_device *dev)
  76. {
  77. struct kirin_drm_private *priv;
  78. int ret;
  79. priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
  80. if (!priv)
  81. return -ENOMEM;
  82. dev->dev_private = priv;
  83. dev_set_drvdata(dev->dev, dev);
  84. /* dev->mode_config initialization */
  85. drm_mode_config_init(dev);
  86. kirin_drm_mode_config_init(dev);
  87. /* display controller init */
  88. ret = dc_ops->init(to_platform_device(dev->dev));
  89. if (ret)
  90. goto err_mode_config_cleanup;
  91. /* bind and init sub drivers */
  92. ret = component_bind_all(dev->dev, dev);
  93. if (ret) {
  94. DRM_ERROR("failed to bind all component.\n");
  95. goto err_dc_cleanup;
  96. }
  97. /* vblank init */
  98. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  99. if (ret) {
  100. DRM_ERROR("failed to initialize vblank.\n");
  101. goto err_unbind_all;
  102. }
  103. /* with irq_enabled = true, we can use the vblank feature. */
  104. dev->irq_enabled = true;
  105. /* reset all the states of crtc/plane/encoder/connector */
  106. drm_mode_config_reset(dev);
  107. /* init kms poll for handling hpd */
  108. drm_kms_helper_poll_init(dev);
  109. /* force detection after connectors init */
  110. (void)drm_helper_hpd_irq_event(dev);
  111. return 0;
  112. err_unbind_all:
  113. component_unbind_all(dev->dev, dev);
  114. err_dc_cleanup:
  115. dc_ops->cleanup(to_platform_device(dev->dev));
  116. err_mode_config_cleanup:
  117. drm_mode_config_cleanup(dev);
  118. devm_kfree(dev->dev, priv);
  119. dev->dev_private = NULL;
  120. return ret;
  121. }
  122. static const struct file_operations kirin_drm_fops = {
  123. .owner = THIS_MODULE,
  124. .open = drm_open,
  125. .release = drm_release,
  126. .unlocked_ioctl = drm_ioctl,
  127. .compat_ioctl = drm_compat_ioctl,
  128. .poll = drm_poll,
  129. .read = drm_read,
  130. .llseek = no_llseek,
  131. .mmap = drm_gem_cma_mmap,
  132. };
  133. static int kirin_gem_cma_dumb_create(struct drm_file *file,
  134. struct drm_device *dev,
  135. struct drm_mode_create_dumb *args)
  136. {
  137. return drm_gem_cma_dumb_create_internal(file, dev, args);
  138. }
  139. static struct drm_driver kirin_drm_driver = {
  140. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
  141. DRIVER_ATOMIC,
  142. .fops = &kirin_drm_fops,
  143. .gem_free_object_unlocked = drm_gem_cma_free_object,
  144. .gem_vm_ops = &drm_gem_cma_vm_ops,
  145. .dumb_create = kirin_gem_cma_dumb_create,
  146. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  147. .dumb_destroy = drm_gem_dumb_destroy,
  148. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  149. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  150. .gem_prime_export = drm_gem_prime_export,
  151. .gem_prime_import = drm_gem_prime_import,
  152. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  153. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  154. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  155. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  156. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  157. .name = "kirin",
  158. .desc = "Hisilicon Kirin SoCs' DRM Driver",
  159. .date = "20150718",
  160. .major = 1,
  161. .minor = 0,
  162. };
  163. static int compare_of(struct device *dev, void *data)
  164. {
  165. return dev->of_node == data;
  166. }
  167. static int kirin_drm_bind(struct device *dev)
  168. {
  169. struct drm_driver *driver = &kirin_drm_driver;
  170. struct drm_device *drm_dev;
  171. int ret;
  172. drm_dev = drm_dev_alloc(driver, dev);
  173. if (IS_ERR(drm_dev))
  174. return PTR_ERR(drm_dev);
  175. ret = kirin_drm_kms_init(drm_dev);
  176. if (ret)
  177. goto err_drm_dev_unref;
  178. ret = drm_dev_register(drm_dev, 0);
  179. if (ret)
  180. goto err_kms_cleanup;
  181. return 0;
  182. err_kms_cleanup:
  183. kirin_drm_kms_cleanup(drm_dev);
  184. err_drm_dev_unref:
  185. drm_dev_unref(drm_dev);
  186. return ret;
  187. }
  188. static void kirin_drm_unbind(struct device *dev)
  189. {
  190. struct drm_device *drm_dev = dev_get_drvdata(dev);
  191. drm_dev_unregister(drm_dev);
  192. kirin_drm_kms_cleanup(drm_dev);
  193. drm_dev_unref(drm_dev);
  194. }
  195. static const struct component_master_ops kirin_drm_ops = {
  196. .bind = kirin_drm_bind,
  197. .unbind = kirin_drm_unbind,
  198. };
  199. static struct device_node *kirin_get_remote_node(struct device_node *np)
  200. {
  201. struct device_node *endpoint, *remote;
  202. /* get the first endpoint, in our case only one remote node
  203. * is connected to display controller.
  204. */
  205. endpoint = of_graph_get_next_endpoint(np, NULL);
  206. if (!endpoint) {
  207. DRM_ERROR("no valid endpoint node\n");
  208. return ERR_PTR(-ENODEV);
  209. }
  210. remote = of_graph_get_remote_port_parent(endpoint);
  211. of_node_put(endpoint);
  212. if (!remote) {
  213. DRM_ERROR("no valid remote node\n");
  214. return ERR_PTR(-ENODEV);
  215. }
  216. if (!of_device_is_available(remote)) {
  217. DRM_ERROR("not available for remote node\n");
  218. return ERR_PTR(-ENODEV);
  219. }
  220. return remote;
  221. }
  222. static int kirin_drm_platform_probe(struct platform_device *pdev)
  223. {
  224. struct device *dev = &pdev->dev;
  225. struct device_node *np = dev->of_node;
  226. struct component_match *match = NULL;
  227. struct device_node *remote;
  228. dc_ops = (struct kirin_dc_ops *)of_device_get_match_data(dev);
  229. if (!dc_ops) {
  230. DRM_ERROR("failed to get dt id data\n");
  231. return -EINVAL;
  232. }
  233. remote = kirin_get_remote_node(np);
  234. if (IS_ERR(remote))
  235. return PTR_ERR(remote);
  236. drm_of_component_match_add(dev, &match, compare_of, remote);
  237. of_node_put(remote);
  238. return component_master_add_with_match(dev, &kirin_drm_ops, match);
  239. return 0;
  240. }
  241. static int kirin_drm_platform_remove(struct platform_device *pdev)
  242. {
  243. component_master_del(&pdev->dev, &kirin_drm_ops);
  244. dc_ops = NULL;
  245. return 0;
  246. }
  247. static const struct of_device_id kirin_drm_dt_ids[] = {
  248. { .compatible = "hisilicon,hi6220-ade",
  249. .data = &ade_dc_ops,
  250. },
  251. { /* end node */ },
  252. };
  253. MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
  254. static struct platform_driver kirin_drm_platform_driver = {
  255. .probe = kirin_drm_platform_probe,
  256. .remove = kirin_drm_platform_remove,
  257. .driver = {
  258. .name = "kirin-drm",
  259. .of_match_table = kirin_drm_dt_ids,
  260. },
  261. };
  262. module_platform_driver(kirin_drm_platform_driver);
  263. MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
  264. MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
  265. MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
  266. MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
  267. MODULE_LICENSE("GPL v2");