kirin_drm_drv.c 7.9 KB

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