sun4i_drv.c 12 KB

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