sun4i_drv.c 8.4 KB

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