sun4i_drv.c 9.7 KB

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