sun4i_drv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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/kfifo.h>
  14. #include <linux/of_graph.h>
  15. #include <linux/of_reserved_mem.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/drm_fb_cma_helper.h>
  19. #include <drm/drm_gem_cma_helper.h>
  20. #include <drm/drm_fb_helper.h>
  21. #include <drm/drm_of.h>
  22. #include "sun4i_drv.h"
  23. #include "sun4i_frontend.h"
  24. #include "sun4i_framebuffer.h"
  25. #include "sun4i_tcon.h"
  26. DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
  27. static struct drm_driver sun4i_drv_driver = {
  28. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
  29. /* Generic Operations */
  30. .lastclose = drm_fb_helper_lastclose,
  31. .fops = &sun4i_drv_fops,
  32. .name = "sun4i-drm",
  33. .desc = "Allwinner sun4i Display Engine",
  34. .date = "20150629",
  35. .major = 1,
  36. .minor = 0,
  37. /* GEM Operations */
  38. .dumb_create = drm_gem_cma_dumb_create,
  39. .gem_free_object_unlocked = drm_gem_cma_free_object,
  40. .gem_vm_ops = &drm_gem_cma_vm_ops,
  41. /* PRIME Operations */
  42. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  43. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  44. .gem_prime_import = drm_gem_prime_import,
  45. .gem_prime_export = drm_gem_prime_export,
  46. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  47. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  48. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  49. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  50. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  51. /* Frame Buffer Operations */
  52. };
  53. static void sun4i_remove_framebuffers(void)
  54. {
  55. struct apertures_struct *ap;
  56. ap = alloc_apertures(1);
  57. if (!ap)
  58. return;
  59. /* The framebuffer can be located anywhere in RAM */
  60. ap->ranges[0].base = 0;
  61. ap->ranges[0].size = ~0;
  62. drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
  63. kfree(ap);
  64. }
  65. static int sun4i_drv_bind(struct device *dev)
  66. {
  67. struct drm_device *drm;
  68. struct sun4i_drv *drv;
  69. int ret;
  70. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  71. if (IS_ERR(drm))
  72. return PTR_ERR(drm);
  73. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  74. if (!drv) {
  75. ret = -ENOMEM;
  76. goto free_drm;
  77. }
  78. drm->dev_private = drv;
  79. INIT_LIST_HEAD(&drv->frontend_list);
  80. INIT_LIST_HEAD(&drv->engine_list);
  81. INIT_LIST_HEAD(&drv->tcon_list);
  82. ret = of_reserved_mem_device_init(dev);
  83. if (ret && ret != -ENODEV) {
  84. dev_err(drm->dev, "Couldn't claim our memory region\n");
  85. goto free_drm;
  86. }
  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_vblank_init calls kcalloc, which can fail */
  94. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  95. if (ret)
  96. goto cleanup_mode_config;
  97. drm->irq_enabled = true;
  98. /* Remove early framebuffers (ie. simplefb) */
  99. sun4i_remove_framebuffers();
  100. /* Create our framebuffer */
  101. ret = sun4i_framebuffer_init(drm);
  102. if (ret) {
  103. dev_err(drm->dev, "Couldn't create our framebuffer\n");
  104. goto cleanup_mode_config;
  105. }
  106. /* Enable connectors polling */
  107. drm_kms_helper_poll_init(drm);
  108. ret = drm_dev_register(drm, 0);
  109. if (ret)
  110. goto finish_poll;
  111. return 0;
  112. finish_poll:
  113. drm_kms_helper_poll_fini(drm);
  114. sun4i_framebuffer_free(drm);
  115. cleanup_mode_config:
  116. drm_mode_config_cleanup(drm);
  117. of_reserved_mem_device_release(dev);
  118. free_drm:
  119. drm_dev_unref(drm);
  120. return ret;
  121. }
  122. static void sun4i_drv_unbind(struct device *dev)
  123. {
  124. struct drm_device *drm = dev_get_drvdata(dev);
  125. drm_dev_unregister(drm);
  126. drm_kms_helper_poll_fini(drm);
  127. sun4i_framebuffer_free(drm);
  128. drm_mode_config_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_connector(struct device_node *node)
  137. {
  138. return of_device_is_compatible(node, "hdmi-connector");
  139. }
  140. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  141. {
  142. return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") ||
  143. of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  144. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  145. of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") ||
  146. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend") ||
  147. of_device_is_compatible(node, "allwinner,sun9i-a80-display-frontend");
  148. }
  149. static bool sun4i_drv_node_is_deu(struct device_node *node)
  150. {
  151. return of_device_is_compatible(node, "allwinner,sun9i-a80-deu");
  152. }
  153. static bool sun4i_drv_node_is_supported_frontend(struct device_node *node)
  154. {
  155. if (IS_ENABLED(CONFIG_DRM_SUN4I_BACKEND))
  156. return !!of_match_node(sun4i_frontend_of_table, node);
  157. return false;
  158. }
  159. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  160. {
  161. return !!of_match_node(sun4i_tcon_of_table, node);
  162. }
  163. static int compare_of(struct device *dev, void *data)
  164. {
  165. DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
  166. dev->of_node,
  167. data);
  168. return dev->of_node == data;
  169. }
  170. /*
  171. * The encoder drivers use drm_of_find_possible_crtcs to get upstream
  172. * crtcs from the device tree using of_graph. For the results to be
  173. * correct, encoders must be probed/bound after _all_ crtcs have been
  174. * created. The existing code uses a depth first recursive traversal
  175. * of the of_graph, which means the encoders downstream of the TCON
  176. * get add right after the first TCON. The second TCON or CRTC will
  177. * never be properly associated with encoders connected to it.
  178. *
  179. * Also, in a dual display pipeline setup, both frontends can feed
  180. * either backend, and both backends can feed either TCON, we want
  181. * all components of the same type to be added before the next type
  182. * in the pipeline. Fortunately, the pipelines are perfectly symmetric,
  183. * i.e. components of the same type are at the same depth when counted
  184. * from the frontend. The only exception is the third pipeline in
  185. * the A80 SoC, which we do not support anyway.
  186. *
  187. * Hence we can use a breadth first search traversal order to add
  188. * components. We do not need to check for duplicates. The component
  189. * matching system handles this for us.
  190. */
  191. struct endpoint_list {
  192. DECLARE_KFIFO(fifo, struct device_node *, 16);
  193. };
  194. static int sun4i_drv_add_endpoints(struct device *dev,
  195. struct endpoint_list *list,
  196. struct component_match **match,
  197. struct device_node *node)
  198. {
  199. struct device_node *port, *ep, *remote;
  200. int count = 0;
  201. /*
  202. * The frontend has been disabled in some of our old device
  203. * trees. If we find a node that is the frontend and is
  204. * disabled, we should just follow through and parse its
  205. * child, but without adding it to the component list.
  206. * Otherwise, we obviously want to add it to the list.
  207. */
  208. if (!sun4i_drv_node_is_frontend(node) &&
  209. !of_device_is_available(node))
  210. return 0;
  211. /*
  212. * The connectors will be the last nodes in our pipeline, we
  213. * can just bail out.
  214. */
  215. if (sun4i_drv_node_is_connector(node))
  216. return 0;
  217. /*
  218. * If the device is either just a regular device, or an
  219. * enabled frontend supported by the driver, we add it to our
  220. * component list.
  221. */
  222. if (!(sun4i_drv_node_is_frontend(node) ||
  223. sun4i_drv_node_is_deu(node)) ||
  224. (sun4i_drv_node_is_supported_frontend(node) &&
  225. of_device_is_available(node))) {
  226. /* Add current component */
  227. DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
  228. drm_of_component_match_add(dev, match, compare_of, node);
  229. count++;
  230. }
  231. /* Inputs are listed first, then outputs */
  232. port = of_graph_get_port_by_id(node, 1);
  233. if (!port) {
  234. DRM_DEBUG_DRIVER("No output to bind\n");
  235. return count;
  236. }
  237. for_each_available_child_of_node(port, ep) {
  238. remote = of_graph_get_remote_port_parent(ep);
  239. if (!remote) {
  240. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  241. of_node_put(remote);
  242. continue;
  243. }
  244. /*
  245. * If the node is our TCON, the first port is used for
  246. * panel or bridges, and will not be part of the
  247. * component framework.
  248. */
  249. if (sun4i_drv_node_is_tcon(node)) {
  250. struct of_endpoint endpoint;
  251. if (of_graph_parse_endpoint(ep, &endpoint)) {
  252. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  253. continue;
  254. }
  255. if (!endpoint.id) {
  256. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  257. continue;
  258. }
  259. }
  260. kfifo_put(&list->fifo, remote);
  261. }
  262. return count;
  263. }
  264. static int sun4i_drv_probe(struct platform_device *pdev)
  265. {
  266. struct component_match *match = NULL;
  267. struct device_node *np = pdev->dev.of_node, *endpoint;
  268. struct endpoint_list list;
  269. int i, ret, count = 0;
  270. INIT_KFIFO(list.fifo);
  271. for (i = 0;; i++) {
  272. struct device_node *pipeline = of_parse_phandle(np,
  273. "allwinner,pipelines",
  274. i);
  275. if (!pipeline)
  276. break;
  277. kfifo_put(&list.fifo, pipeline);
  278. }
  279. while (kfifo_get(&list.fifo, &endpoint)) {
  280. /* process this endpoint */
  281. ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match,
  282. endpoint);
  283. /* sun4i_drv_add_endpoints can fail to allocate memory */
  284. if (ret < 0)
  285. return ret;
  286. count += ret;
  287. }
  288. if (count)
  289. return component_master_add_with_match(&pdev->dev,
  290. &sun4i_drv_master_ops,
  291. match);
  292. else
  293. return 0;
  294. }
  295. static int sun4i_drv_remove(struct platform_device *pdev)
  296. {
  297. return 0;
  298. }
  299. static const struct of_device_id sun4i_drv_of_table[] = {
  300. { .compatible = "allwinner,sun4i-a10-display-engine" },
  301. { .compatible = "allwinner,sun5i-a10s-display-engine" },
  302. { .compatible = "allwinner,sun5i-a13-display-engine" },
  303. { .compatible = "allwinner,sun6i-a31-display-engine" },
  304. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  305. { .compatible = "allwinner,sun7i-a20-display-engine" },
  306. { .compatible = "allwinner,sun8i-a33-display-engine" },
  307. { .compatible = "allwinner,sun8i-a83t-display-engine" },
  308. { .compatible = "allwinner,sun8i-h3-display-engine" },
  309. { .compatible = "allwinner,sun8i-v3s-display-engine" },
  310. { .compatible = "allwinner,sun9i-a80-display-engine" },
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  314. static struct platform_driver sun4i_drv_platform_driver = {
  315. .probe = sun4i_drv_probe,
  316. .remove = sun4i_drv_remove,
  317. .driver = {
  318. .name = "sun4i-drm",
  319. .of_match_table = sun4i_drv_of_table,
  320. },
  321. };
  322. module_platform_driver(sun4i_drv_platform_driver);
  323. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  324. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  325. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  326. MODULE_LICENSE("GPL");