sun4i_drv.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 free_mem_region;
  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. free_mem_region:
  118. of_reserved_mem_device_release(dev);
  119. free_drm:
  120. drm_dev_unref(drm);
  121. return ret;
  122. }
  123. static void sun4i_drv_unbind(struct device *dev)
  124. {
  125. struct drm_device *drm = dev_get_drvdata(dev);
  126. drm_dev_unregister(drm);
  127. drm_kms_helper_poll_fini(drm);
  128. sun4i_framebuffer_free(drm);
  129. drm_mode_config_cleanup(drm);
  130. of_reserved_mem_device_release(dev);
  131. drm_dev_unref(drm);
  132. }
  133. static const struct component_master_ops sun4i_drv_master_ops = {
  134. .bind = sun4i_drv_bind,
  135. .unbind = sun4i_drv_unbind,
  136. };
  137. static bool sun4i_drv_node_is_connector(struct device_node *node)
  138. {
  139. return of_device_is_compatible(node, "hdmi-connector");
  140. }
  141. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  142. {
  143. return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") ||
  144. of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  145. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  146. of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") ||
  147. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
  148. }
  149. static bool sun4i_drv_node_is_supported_frontend(struct device_node *node)
  150. {
  151. if (IS_ENABLED(CONFIG_DRM_SUN4I_BACKEND))
  152. return !!of_match_node(sun4i_frontend_of_table, node);
  153. return false;
  154. }
  155. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  156. {
  157. return !!of_match_node(sun4i_tcon_of_table, node);
  158. }
  159. static int compare_of(struct device *dev, void *data)
  160. {
  161. DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
  162. dev->of_node,
  163. data);
  164. return dev->of_node == data;
  165. }
  166. /*
  167. * The encoder drivers use drm_of_find_possible_crtcs to get upstream
  168. * crtcs from the device tree using of_graph. For the results to be
  169. * correct, encoders must be probed/bound after _all_ crtcs have been
  170. * created. The existing code uses a depth first recursive traversal
  171. * of the of_graph, which means the encoders downstream of the TCON
  172. * get add right after the first TCON. The second TCON or CRTC will
  173. * never be properly associated with encoders connected to it.
  174. *
  175. * Also, in a dual display pipeline setup, both frontends can feed
  176. * either backend, and both backends can feed either TCON, we want
  177. * all components of the same type to be added before the next type
  178. * in the pipeline. Fortunately, the pipelines are perfectly symmetric,
  179. * i.e. components of the same type are at the same depth when counted
  180. * from the frontend. The only exception is the third pipeline in
  181. * the A80 SoC, which we do not support anyway.
  182. *
  183. * Hence we can use a breadth first search traversal order to add
  184. * components. We do not need to check for duplicates. The component
  185. * matching system handles this for us.
  186. */
  187. struct endpoint_list {
  188. DECLARE_KFIFO(fifo, struct device_node *, 16);
  189. };
  190. static int sun4i_drv_add_endpoints(struct device *dev,
  191. struct endpoint_list *list,
  192. struct component_match **match,
  193. struct device_node *node)
  194. {
  195. struct device_node *port, *ep, *remote;
  196. int count = 0;
  197. /*
  198. * The frontend has been disabled in some of our old device
  199. * trees. If we find a node that is the frontend and is
  200. * disabled, we should just follow through and parse its
  201. * child, but without adding it to the component list.
  202. * Otherwise, we obviously want to add it to the list.
  203. */
  204. if (!sun4i_drv_node_is_frontend(node) &&
  205. !of_device_is_available(node))
  206. return 0;
  207. /*
  208. * The connectors will be the last nodes in our pipeline, we
  209. * can just bail out.
  210. */
  211. if (sun4i_drv_node_is_connector(node))
  212. return 0;
  213. /*
  214. * If the device is either just a regular device, or an
  215. * enabled frontend supported by the driver, we add it to our
  216. * component list.
  217. */
  218. if (!sun4i_drv_node_is_frontend(node) ||
  219. (sun4i_drv_node_is_supported_frontend(node) &&
  220. of_device_is_available(node))) {
  221. /* Add current component */
  222. DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
  223. drm_of_component_match_add(dev, match, compare_of, node);
  224. count++;
  225. }
  226. /* Inputs are listed first, then outputs */
  227. port = of_graph_get_port_by_id(node, 1);
  228. if (!port) {
  229. DRM_DEBUG_DRIVER("No output to bind\n");
  230. return count;
  231. }
  232. for_each_available_child_of_node(port, ep) {
  233. remote = of_graph_get_remote_port_parent(ep);
  234. if (!remote) {
  235. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  236. of_node_put(remote);
  237. continue;
  238. }
  239. /*
  240. * If the node is our TCON, the first port is used for
  241. * panel or bridges, and will not be part of the
  242. * component framework.
  243. */
  244. if (sun4i_drv_node_is_tcon(node)) {
  245. struct of_endpoint endpoint;
  246. if (of_graph_parse_endpoint(ep, &endpoint)) {
  247. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  248. continue;
  249. }
  250. if (!endpoint.id) {
  251. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  252. continue;
  253. }
  254. }
  255. kfifo_put(&list->fifo, remote);
  256. }
  257. return count;
  258. }
  259. static int sun4i_drv_probe(struct platform_device *pdev)
  260. {
  261. struct component_match *match = NULL;
  262. struct device_node *np = pdev->dev.of_node, *endpoint;
  263. struct endpoint_list list;
  264. int i, ret, count = 0;
  265. INIT_KFIFO(list.fifo);
  266. for (i = 0;; i++) {
  267. struct device_node *pipeline = of_parse_phandle(np,
  268. "allwinner,pipelines",
  269. i);
  270. if (!pipeline)
  271. break;
  272. kfifo_put(&list.fifo, pipeline);
  273. }
  274. while (kfifo_get(&list.fifo, &endpoint)) {
  275. /* process this endpoint */
  276. ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match,
  277. endpoint);
  278. /* sun4i_drv_add_endpoints can fail to allocate memory */
  279. if (ret < 0)
  280. return ret;
  281. count += ret;
  282. }
  283. if (count)
  284. return component_master_add_with_match(&pdev->dev,
  285. &sun4i_drv_master_ops,
  286. match);
  287. else
  288. return 0;
  289. }
  290. static int sun4i_drv_remove(struct platform_device *pdev)
  291. {
  292. return 0;
  293. }
  294. static const struct of_device_id sun4i_drv_of_table[] = {
  295. { .compatible = "allwinner,sun4i-a10-display-engine" },
  296. { .compatible = "allwinner,sun5i-a10s-display-engine" },
  297. { .compatible = "allwinner,sun5i-a13-display-engine" },
  298. { .compatible = "allwinner,sun6i-a31-display-engine" },
  299. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  300. { .compatible = "allwinner,sun7i-a20-display-engine" },
  301. { .compatible = "allwinner,sun8i-a33-display-engine" },
  302. { .compatible = "allwinner,sun8i-a83t-display-engine" },
  303. { .compatible = "allwinner,sun8i-v3s-display-engine" },
  304. { }
  305. };
  306. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  307. static struct platform_driver sun4i_drv_platform_driver = {
  308. .probe = sun4i_drv_probe,
  309. .remove = sun4i_drv_remove,
  310. .driver = {
  311. .name = "sun4i-drm",
  312. .of_match_table = sun4i_drv_of_table,
  313. },
  314. };
  315. module_platform_driver(sun4i_drv_platform_driver);
  316. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  317. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  318. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  319. MODULE_LICENSE("GPL");