sun4i_drv.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * Copyright (C) 2015 Free Electrons
  3. * Copyright (C) 2015 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/component.h>
  13. #include <linux/of_graph.h>
  14. #include <linux/of_reserved_mem.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_fb_helper.h>
  20. #include <drm/drm_of.h>
  21. #include "sun4i_drv.h"
  22. #include "sun4i_framebuffer.h"
  23. #include "sun4i_tcon.h"
  24. DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
  25. static struct drm_driver sun4i_drv_driver = {
  26. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
  27. /* Generic Operations */
  28. .fops = &sun4i_drv_fops,
  29. .name = "sun4i-drm",
  30. .desc = "Allwinner sun4i Display Engine",
  31. .date = "20150629",
  32. .major = 1,
  33. .minor = 0,
  34. /* GEM Operations */
  35. .dumb_create = drm_gem_cma_dumb_create,
  36. .gem_free_object_unlocked = drm_gem_cma_free_object,
  37. .gem_vm_ops = &drm_gem_cma_vm_ops,
  38. /* PRIME Operations */
  39. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  40. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  41. .gem_prime_import = drm_gem_prime_import,
  42. .gem_prime_export = drm_gem_prime_export,
  43. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  44. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  45. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  46. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  47. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  48. /* Frame Buffer Operations */
  49. };
  50. static void sun4i_remove_framebuffers(void)
  51. {
  52. struct apertures_struct *ap;
  53. ap = alloc_apertures(1);
  54. if (!ap)
  55. return;
  56. /* The framebuffer can be located anywhere in RAM */
  57. ap->ranges[0].base = 0;
  58. ap->ranges[0].size = ~0;
  59. drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
  60. kfree(ap);
  61. }
  62. static int sun4i_drv_bind(struct device *dev)
  63. {
  64. struct drm_device *drm;
  65. struct sun4i_drv *drv;
  66. int ret;
  67. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  68. if (IS_ERR(drm))
  69. return PTR_ERR(drm);
  70. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  71. if (!drv) {
  72. ret = -ENOMEM;
  73. goto free_drm;
  74. }
  75. drm->dev_private = drv;
  76. INIT_LIST_HEAD(&drv->engine_list);
  77. INIT_LIST_HEAD(&drv->tcon_list);
  78. ret = of_reserved_mem_device_init(dev);
  79. if (ret && ret != -ENODEV) {
  80. dev_err(drm->dev, "Couldn't claim our memory region\n");
  81. goto free_drm;
  82. }
  83. /* drm_vblank_init calls kcalloc, which can fail */
  84. ret = drm_vblank_init(drm, 1);
  85. if (ret)
  86. goto free_mem_region;
  87. drm_mode_config_init(drm);
  88. ret = component_bind_all(drm->dev, drm);
  89. if (ret) {
  90. dev_err(drm->dev, "Couldn't bind all pipelines components\n");
  91. goto cleanup_mode_config;
  92. }
  93. drm->irq_enabled = true;
  94. /* Remove early framebuffers (ie. simplefb) */
  95. sun4i_remove_framebuffers();
  96. /* Create our framebuffer */
  97. drv->fbdev = sun4i_framebuffer_init(drm);
  98. if (IS_ERR(drv->fbdev)) {
  99. dev_err(drm->dev, "Couldn't create our framebuffer\n");
  100. ret = PTR_ERR(drv->fbdev);
  101. goto cleanup_mode_config;
  102. }
  103. /* Enable connectors polling */
  104. drm_kms_helper_poll_init(drm);
  105. ret = drm_dev_register(drm, 0);
  106. if (ret)
  107. goto finish_poll;
  108. return 0;
  109. finish_poll:
  110. drm_kms_helper_poll_fini(drm);
  111. sun4i_framebuffer_free(drm);
  112. cleanup_mode_config:
  113. drm_mode_config_cleanup(drm);
  114. free_mem_region:
  115. of_reserved_mem_device_release(dev);
  116. free_drm:
  117. drm_dev_unref(drm);
  118. return ret;
  119. }
  120. static void sun4i_drv_unbind(struct device *dev)
  121. {
  122. struct drm_device *drm = dev_get_drvdata(dev);
  123. drm_dev_unregister(drm);
  124. drm_kms_helper_poll_fini(drm);
  125. sun4i_framebuffer_free(drm);
  126. drm_mode_config_cleanup(drm);
  127. of_reserved_mem_device_release(dev);
  128. drm_dev_unref(drm);
  129. }
  130. static const struct component_master_ops sun4i_drv_master_ops = {
  131. .bind = sun4i_drv_bind,
  132. .unbind = sun4i_drv_unbind,
  133. };
  134. static bool sun4i_drv_node_is_connector(struct device_node *node)
  135. {
  136. return of_device_is_compatible(node, "hdmi-connector");
  137. }
  138. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  139. {
  140. return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  141. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  142. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
  143. }
  144. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  145. {
  146. return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
  147. of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
  148. of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
  149. of_device_is_compatible(node, "allwinner,sun8i-a33-tcon") ||
  150. of_device_is_compatible(node, "allwinner,sun8i-v3s-tcon");
  151. }
  152. static int compare_of(struct device *dev, void *data)
  153. {
  154. DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
  155. dev->of_node,
  156. data);
  157. return dev->of_node == data;
  158. }
  159. static int sun4i_drv_add_endpoints(struct device *dev,
  160. struct component_match **match,
  161. struct device_node *node)
  162. {
  163. struct device_node *port, *ep, *remote;
  164. int count = 0;
  165. /*
  166. * We don't support the frontend for now, so we will never
  167. * have a device bound. Just skip over it, but we still want
  168. * the rest our pipeline to be added.
  169. */
  170. if (!sun4i_drv_node_is_frontend(node) &&
  171. !of_device_is_available(node))
  172. return 0;
  173. /*
  174. * The connectors will be the last nodes in our pipeline, we
  175. * can just bail out.
  176. */
  177. if (sun4i_drv_node_is_connector(node))
  178. return 0;
  179. if (!sun4i_drv_node_is_frontend(node)) {
  180. /* Add current component */
  181. DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
  182. drm_of_component_match_add(dev, match, compare_of, node);
  183. count++;
  184. }
  185. /* Inputs are listed first, then outputs */
  186. port = of_graph_get_port_by_id(node, 1);
  187. if (!port) {
  188. DRM_DEBUG_DRIVER("No output to bind\n");
  189. return count;
  190. }
  191. for_each_available_child_of_node(port, ep) {
  192. remote = of_graph_get_remote_port_parent(ep);
  193. if (!remote) {
  194. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  195. of_node_put(remote);
  196. continue;
  197. }
  198. /*
  199. * If the node is our TCON, the first port is used for
  200. * panel or bridges, and will not be part of the
  201. * component framework.
  202. */
  203. if (sun4i_drv_node_is_tcon(node)) {
  204. struct of_endpoint endpoint;
  205. if (of_graph_parse_endpoint(ep, &endpoint)) {
  206. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  207. continue;
  208. }
  209. if (!endpoint.id) {
  210. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  211. continue;
  212. }
  213. }
  214. /* Walk down our tree */
  215. count += sun4i_drv_add_endpoints(dev, match, remote);
  216. of_node_put(remote);
  217. }
  218. return count;
  219. }
  220. static int sun4i_drv_probe(struct platform_device *pdev)
  221. {
  222. struct component_match *match = NULL;
  223. struct device_node *np = pdev->dev.of_node;
  224. int i, count = 0;
  225. for (i = 0;; i++) {
  226. struct device_node *pipeline = of_parse_phandle(np,
  227. "allwinner,pipelines",
  228. i);
  229. if (!pipeline)
  230. break;
  231. count += sun4i_drv_add_endpoints(&pdev->dev, &match,
  232. pipeline);
  233. of_node_put(pipeline);
  234. DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
  235. count, i);
  236. }
  237. if (count)
  238. return component_master_add_with_match(&pdev->dev,
  239. &sun4i_drv_master_ops,
  240. match);
  241. else
  242. return 0;
  243. }
  244. static int sun4i_drv_remove(struct platform_device *pdev)
  245. {
  246. return 0;
  247. }
  248. static const struct of_device_id sun4i_drv_of_table[] = {
  249. { .compatible = "allwinner,sun5i-a10s-display-engine" },
  250. { .compatible = "allwinner,sun5i-a13-display-engine" },
  251. { .compatible = "allwinner,sun6i-a31-display-engine" },
  252. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  253. { .compatible = "allwinner,sun8i-a33-display-engine" },
  254. { .compatible = "allwinner,sun8i-v3s-display-engine" },
  255. { }
  256. };
  257. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  258. static struct platform_driver sun4i_drv_platform_driver = {
  259. .probe = sun4i_drv_probe,
  260. .remove = sun4i_drv_remove,
  261. .driver = {
  262. .name = "sun4i-drm",
  263. .of_match_table = sun4i_drv_of_table,
  264. },
  265. };
  266. module_platform_driver(sun4i_drv_platform_driver);
  267. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  268. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  269. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  270. MODULE_LICENSE("GPL");