sun4i_drv.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. .dumb_destroy = drm_gem_dumb_destroy,
  37. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  38. .gem_free_object_unlocked = drm_gem_cma_free_object,
  39. .gem_vm_ops = &drm_gem_cma_vm_ops,
  40. /* PRIME Operations */
  41. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  42. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  43. .gem_prime_import = drm_gem_prime_import,
  44. .gem_prime_export = drm_gem_prime_export,
  45. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  46. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  47. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  48. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  49. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  50. /* Frame Buffer Operations */
  51. };
  52. static void sun4i_remove_framebuffers(void)
  53. {
  54. struct apertures_struct *ap;
  55. ap = alloc_apertures(1);
  56. if (!ap)
  57. return;
  58. /* The framebuffer can be located anywhere in RAM */
  59. ap->ranges[0].base = 0;
  60. ap->ranges[0].size = ~0;
  61. drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
  62. kfree(ap);
  63. }
  64. static int sun4i_drv_bind(struct device *dev)
  65. {
  66. struct drm_device *drm;
  67. struct sun4i_drv *drv;
  68. int ret;
  69. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  70. if (IS_ERR(drm))
  71. return PTR_ERR(drm);
  72. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  73. if (!drv) {
  74. ret = -ENOMEM;
  75. goto free_drm;
  76. }
  77. drm->dev_private = drv;
  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. drm_vblank_cleanup(drm);
  115. free_mem_region:
  116. of_reserved_mem_device_release(dev);
  117. free_drm:
  118. drm_dev_unref(drm);
  119. return ret;
  120. }
  121. static void sun4i_drv_unbind(struct device *dev)
  122. {
  123. struct drm_device *drm = dev_get_drvdata(dev);
  124. drm_dev_unregister(drm);
  125. drm_kms_helper_poll_fini(drm);
  126. sun4i_framebuffer_free(drm);
  127. drm_mode_config_cleanup(drm);
  128. drm_vblank_cleanup(drm);
  129. of_reserved_mem_device_release(dev);
  130. drm_dev_unref(drm);
  131. }
  132. static const struct component_master_ops sun4i_drv_master_ops = {
  133. .bind = sun4i_drv_bind,
  134. .unbind = sun4i_drv_unbind,
  135. };
  136. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  137. {
  138. return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  139. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  140. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
  141. }
  142. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  143. {
  144. return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
  145. of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
  146. of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
  147. of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
  148. }
  149. static int compare_of(struct device *dev, void *data)
  150. {
  151. DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
  152. of_node_full_name(dev->of_node),
  153. of_node_full_name(data));
  154. return dev->of_node == data;
  155. }
  156. static int sun4i_drv_add_endpoints(struct device *dev,
  157. struct component_match **match,
  158. struct device_node *node)
  159. {
  160. struct device_node *port, *ep, *remote;
  161. int count = 0;
  162. /*
  163. * We don't support the frontend for now, so we will never
  164. * have a device bound. Just skip over it, but we still want
  165. * the rest our pipeline to be added.
  166. */
  167. if (!sun4i_drv_node_is_frontend(node) &&
  168. !of_device_is_available(node))
  169. return 0;
  170. if (!sun4i_drv_node_is_frontend(node)) {
  171. /* Add current component */
  172. DRM_DEBUG_DRIVER("Adding component %s\n",
  173. of_node_full_name(node));
  174. drm_of_component_match_add(dev, match, compare_of, node);
  175. count++;
  176. }
  177. /* Inputs are listed first, then outputs */
  178. port = of_graph_get_port_by_id(node, 1);
  179. if (!port) {
  180. DRM_DEBUG_DRIVER("No output to bind\n");
  181. return count;
  182. }
  183. for_each_available_child_of_node(port, ep) {
  184. remote = of_graph_get_remote_port_parent(ep);
  185. if (!remote) {
  186. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  187. of_node_put(remote);
  188. continue;
  189. }
  190. /*
  191. * If the node is our TCON, the first port is used for
  192. * panel or bridges, and will not be part of the
  193. * component framework.
  194. */
  195. if (sun4i_drv_node_is_tcon(node)) {
  196. struct of_endpoint endpoint;
  197. if (of_graph_parse_endpoint(ep, &endpoint)) {
  198. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  199. continue;
  200. }
  201. if (!endpoint.id) {
  202. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  203. continue;
  204. }
  205. }
  206. /* Walk down our tree */
  207. count += sun4i_drv_add_endpoints(dev, match, remote);
  208. of_node_put(remote);
  209. }
  210. return count;
  211. }
  212. static int sun4i_drv_probe(struct platform_device *pdev)
  213. {
  214. struct component_match *match = NULL;
  215. struct device_node *np = pdev->dev.of_node;
  216. int i, count = 0;
  217. for (i = 0;; i++) {
  218. struct device_node *pipeline = of_parse_phandle(np,
  219. "allwinner,pipelines",
  220. i);
  221. if (!pipeline)
  222. break;
  223. count += sun4i_drv_add_endpoints(&pdev->dev, &match,
  224. pipeline);
  225. of_node_put(pipeline);
  226. DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
  227. count, i);
  228. }
  229. if (count)
  230. return component_master_add_with_match(&pdev->dev,
  231. &sun4i_drv_master_ops,
  232. match);
  233. else
  234. return 0;
  235. }
  236. static int sun4i_drv_remove(struct platform_device *pdev)
  237. {
  238. return 0;
  239. }
  240. static const struct of_device_id sun4i_drv_of_table[] = {
  241. { .compatible = "allwinner,sun5i-a13-display-engine" },
  242. { .compatible = "allwinner,sun6i-a31-display-engine" },
  243. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  244. { .compatible = "allwinner,sun8i-a33-display-engine" },
  245. { }
  246. };
  247. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  248. static struct platform_driver sun4i_drv_platform_driver = {
  249. .probe = sun4i_drv_probe,
  250. .remove = sun4i_drv_remove,
  251. .driver = {
  252. .name = "sun4i-drm",
  253. .of_match_table = sun4i_drv_of_table,
  254. },
  255. };
  256. module_platform_driver(sun4i_drv_platform_driver);
  257. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  258. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  259. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  260. MODULE_LICENSE("GPL");