sun4i_drv.c 12 KB

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