sun4i_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Free Electrons
  4. * Copyright (C) 2015 NextThing Co
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. */
  8. #include <linux/component.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/kfifo.h>
  11. #include <linux/module.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/of_reserved_mem.h>
  14. #include <linux/platform_device.h>
  15. #include <drm/drm_aperture.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include <drm/drm_drv.h>
  18. #include <drm/drm_fbdev_dma.h>
  19. #include <drm/drm_gem_dma_helper.h>
  20. #include <drm/drm_module.h>
  21. #include <drm/drm_of.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_vblank.h>
  24. #include "sun4i_drv.h"
  25. #include "sun4i_frontend.h"
  26. #include "sun4i_framebuffer.h"
  27. #include "sun4i_tcon.h"
  28. #include "sun8i_tcon_top.h"
  29. static int drm_sun4i_gem_dumb_create(struct drm_file *file_priv,
  30. struct drm_device *drm,
  31. struct drm_mode_create_dumb *args)
  32. {
  33. /* The hardware only allows even pitches for YUV buffers. */
  34. args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), 2);
  35. return drm_gem_dma_dumb_create_internal(file_priv, drm, args);
  36. }
  37. DEFINE_DRM_GEM_DMA_FOPS(sun4i_drv_fops);
  38. static const struct drm_driver sun4i_drv_driver = {
  39. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
  40. /* Generic Operations */
  41. .fops = &sun4i_drv_fops,
  42. .name = "sun4i-drm",
  43. .desc = "Allwinner sun4i Display Engine",
  44. .date = "20150629",
  45. .major = 1,
  46. .minor = 0,
  47. /* GEM Operations */
  48. DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_sun4i_gem_dumb_create),
  49. };
  50. static int sun4i_drv_bind(struct device *dev)
  51. {
  52. struct drm_device *drm;
  53. struct sun4i_drv *drv;
  54. int ret;
  55. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  56. if (IS_ERR(drm))
  57. return PTR_ERR(drm);
  58. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  59. if (!drv) {
  60. ret = -ENOMEM;
  61. goto free_drm;
  62. }
  63. drm->dev_private = drv;
  64. INIT_LIST_HEAD(&drv->frontend_list);
  65. INIT_LIST_HEAD(&drv->engine_list);
  66. INIT_LIST_HEAD(&drv->tcon_list);
  67. ret = of_reserved_mem_device_init(dev);
  68. if (ret && ret != -ENODEV) {
  69. dev_err(drm->dev, "Couldn't claim our memory region\n");
  70. goto free_drm;
  71. }
  72. drm_mode_config_init(drm);
  73. ret = component_bind_all(drm->dev, drm);
  74. if (ret) {
  75. dev_err(drm->dev, "Couldn't bind all pipelines components\n");
  76. goto cleanup_mode_config;
  77. }
  78. /* drm_vblank_init calls kcalloc, which can fail */
  79. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  80. if (ret)
  81. goto unbind_all;
  82. /* Remove early framebuffers (ie. simplefb) */
  83. ret = drm_aperture_remove_framebuffers(&sun4i_drv_driver);
  84. if (ret)
  85. goto unbind_all;
  86. sun4i_framebuffer_init(drm);
  87. /* Enable connectors polling */
  88. drm_kms_helper_poll_init(drm);
  89. ret = drm_dev_register(drm, 0);
  90. if (ret)
  91. goto finish_poll;
  92. drm_fbdev_dma_setup(drm, 32);
  93. dev_set_drvdata(dev, drm);
  94. return 0;
  95. finish_poll:
  96. drm_kms_helper_poll_fini(drm);
  97. unbind_all:
  98. component_unbind_all(dev, NULL);
  99. cleanup_mode_config:
  100. drm_mode_config_cleanup(drm);
  101. of_reserved_mem_device_release(dev);
  102. free_drm:
  103. drm_dev_put(drm);
  104. return ret;
  105. }
  106. static void sun4i_drv_unbind(struct device *dev)
  107. {
  108. struct drm_device *drm = dev_get_drvdata(dev);
  109. dev_set_drvdata(dev, NULL);
  110. drm_dev_unregister(drm);
  111. drm_kms_helper_poll_fini(drm);
  112. drm_atomic_helper_shutdown(drm);
  113. drm_mode_config_cleanup(drm);
  114. component_unbind_all(dev, NULL);
  115. of_reserved_mem_device_release(dev);
  116. drm_dev_put(drm);
  117. }
  118. static const struct component_master_ops sun4i_drv_master_ops = {
  119. .bind = sun4i_drv_bind,
  120. .unbind = sun4i_drv_unbind,
  121. };
  122. static bool sun4i_drv_node_is_connector(struct device_node *node)
  123. {
  124. return of_device_is_compatible(node, "hdmi-connector");
  125. }
  126. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  127. {
  128. return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") ||
  129. of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  130. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  131. of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") ||
  132. of_device_is_compatible(node, "allwinner,sun8i-a23-display-frontend") ||
  133. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend") ||
  134. of_device_is_compatible(node, "allwinner,sun9i-a80-display-frontend");
  135. }
  136. static bool sun4i_drv_node_is_deu(struct device_node *node)
  137. {
  138. return of_device_is_compatible(node, "allwinner,sun9i-a80-deu");
  139. }
  140. static bool sun4i_drv_node_is_supported_frontend(struct device_node *node)
  141. {
  142. if (IS_ENABLED(CONFIG_DRM_SUN4I_BACKEND))
  143. return !!of_match_node(sun4i_frontend_of_table, node);
  144. return false;
  145. }
  146. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  147. {
  148. return !!of_match_node(sun4i_tcon_of_table, node);
  149. }
  150. static bool sun4i_drv_node_is_tcon_with_ch0(struct device_node *node)
  151. {
  152. const struct of_device_id *match;
  153. match = of_match_node(sun4i_tcon_of_table, node);
  154. if (match) {
  155. struct sun4i_tcon_quirks *quirks;
  156. quirks = (struct sun4i_tcon_quirks *)match->data;
  157. return quirks->has_channel_0;
  158. }
  159. return false;
  160. }
  161. static bool sun4i_drv_node_is_tcon_top(struct device_node *node)
  162. {
  163. return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
  164. !!of_match_node(sun8i_tcon_top_of_table, node);
  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 void sun4i_drv_traverse_endpoints(struct endpoint_list *list,
  191. struct device_node *node,
  192. int port_id)
  193. {
  194. struct device_node *ep, *remote, *port;
  195. port = of_graph_get_port_by_id(node, port_id);
  196. if (!port) {
  197. DRM_DEBUG_DRIVER("No output to bind on port %d\n", port_id);
  198. return;
  199. }
  200. for_each_available_child_of_node(port, ep) {
  201. remote = of_graph_get_remote_port_parent(ep);
  202. if (!remote) {
  203. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  204. continue;
  205. }
  206. if (sun4i_drv_node_is_tcon(node)) {
  207. /*
  208. * TCON TOP is always probed before TCON. However, TCON
  209. * points back to TCON TOP when it is source for HDMI.
  210. * We have to skip it here to prevent infinite looping
  211. * between TCON TOP and TCON.
  212. */
  213. if (sun4i_drv_node_is_tcon_top(remote)) {
  214. DRM_DEBUG_DRIVER("TCON output endpoint is TCON TOP... skipping\n");
  215. of_node_put(remote);
  216. continue;
  217. }
  218. /*
  219. * If the node is our TCON with channel 0, the first
  220. * port is used for panel or bridges, and will not be
  221. * part of the component framework.
  222. */
  223. if (sun4i_drv_node_is_tcon_with_ch0(node)) {
  224. struct of_endpoint endpoint;
  225. if (of_graph_parse_endpoint(ep, &endpoint)) {
  226. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  227. of_node_put(remote);
  228. continue;
  229. }
  230. if (!endpoint.id) {
  231. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  232. of_node_put(remote);
  233. continue;
  234. }
  235. }
  236. }
  237. kfifo_put(&list->fifo, remote);
  238. }
  239. }
  240. static int sun4i_drv_add_endpoints(struct device *dev,
  241. struct endpoint_list *list,
  242. struct component_match **match,
  243. struct device_node *node)
  244. {
  245. int count = 0;
  246. /*
  247. * The frontend has been disabled in some of our old device
  248. * trees. If we find a node that is the frontend and is
  249. * disabled, we should just follow through and parse its
  250. * child, but without adding it to the component list.
  251. * Otherwise, we obviously want to add it to the list.
  252. */
  253. if (!sun4i_drv_node_is_frontend(node) &&
  254. !of_device_is_available(node))
  255. return 0;
  256. /*
  257. * The connectors will be the last nodes in our pipeline, we
  258. * can just bail out.
  259. */
  260. if (sun4i_drv_node_is_connector(node))
  261. return 0;
  262. /*
  263. * If the device is either just a regular device, or an
  264. * enabled frontend supported by the driver, we add it to our
  265. * component list.
  266. */
  267. if (!(sun4i_drv_node_is_frontend(node) ||
  268. sun4i_drv_node_is_deu(node)) ||
  269. (sun4i_drv_node_is_supported_frontend(node) &&
  270. of_device_is_available(node))) {
  271. /* Add current component */
  272. DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
  273. drm_of_component_match_add(dev, match, component_compare_of, node);
  274. count++;
  275. }
  276. /* each node has at least one output */
  277. sun4i_drv_traverse_endpoints(list, node, 1);
  278. /* TCON TOP has second and third output */
  279. if (sun4i_drv_node_is_tcon_top(node)) {
  280. sun4i_drv_traverse_endpoints(list, node, 3);
  281. sun4i_drv_traverse_endpoints(list, node, 5);
  282. }
  283. return count;
  284. }
  285. #ifdef CONFIG_PM_SLEEP
  286. static int sun4i_drv_drm_sys_suspend(struct device *dev)
  287. {
  288. struct drm_device *drm = dev_get_drvdata(dev);
  289. return drm_mode_config_helper_suspend(drm);
  290. }
  291. static int sun4i_drv_drm_sys_resume(struct device *dev)
  292. {
  293. struct drm_device *drm = dev_get_drvdata(dev);
  294. return drm_mode_config_helper_resume(drm);
  295. }
  296. #endif
  297. static const struct dev_pm_ops sun4i_drv_drm_pm_ops = {
  298. SET_SYSTEM_SLEEP_PM_OPS(sun4i_drv_drm_sys_suspend,
  299. sun4i_drv_drm_sys_resume)
  300. };
  301. static int sun4i_drv_probe(struct platform_device *pdev)
  302. {
  303. struct component_match *match = NULL;
  304. struct device_node *np = pdev->dev.of_node, *endpoint;
  305. struct endpoint_list list;
  306. int i, ret, count = 0;
  307. INIT_KFIFO(list.fifo);
  308. /*
  309. * DE2 and DE3 cores actually supports 40-bit addresses, but
  310. * driver does not.
  311. */
  312. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  313. dma_set_max_seg_size(&pdev->dev, UINT_MAX);
  314. for (i = 0;; i++) {
  315. struct device_node *pipeline = of_parse_phandle(np,
  316. "allwinner,pipelines",
  317. i);
  318. if (!pipeline)
  319. break;
  320. kfifo_put(&list.fifo, pipeline);
  321. }
  322. while (kfifo_get(&list.fifo, &endpoint)) {
  323. /* process this endpoint */
  324. ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match,
  325. endpoint);
  326. /* sun4i_drv_add_endpoints can fail to allocate memory */
  327. if (ret < 0)
  328. return ret;
  329. count += ret;
  330. }
  331. if (count)
  332. return component_master_add_with_match(&pdev->dev,
  333. &sun4i_drv_master_ops,
  334. match);
  335. else
  336. return 0;
  337. }
  338. static void sun4i_drv_remove(struct platform_device *pdev)
  339. {
  340. component_master_del(&pdev->dev, &sun4i_drv_master_ops);
  341. }
  342. static void sun4i_drv_shutdown(struct platform_device *pdev)
  343. {
  344. drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
  345. }
  346. static const struct of_device_id sun4i_drv_of_table[] = {
  347. { .compatible = "allwinner,sun4i-a10-display-engine" },
  348. { .compatible = "allwinner,sun5i-a10s-display-engine" },
  349. { .compatible = "allwinner,sun5i-a13-display-engine" },
  350. { .compatible = "allwinner,sun6i-a31-display-engine" },
  351. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  352. { .compatible = "allwinner,sun7i-a20-display-engine" },
  353. { .compatible = "allwinner,sun8i-a23-display-engine" },
  354. { .compatible = "allwinner,sun8i-a33-display-engine" },
  355. { .compatible = "allwinner,sun8i-a83t-display-engine" },
  356. { .compatible = "allwinner,sun8i-h3-display-engine" },
  357. { .compatible = "allwinner,sun8i-r40-display-engine" },
  358. { .compatible = "allwinner,sun8i-v3s-display-engine" },
  359. { .compatible = "allwinner,sun9i-a80-display-engine" },
  360. { .compatible = "allwinner,sun20i-d1-display-engine" },
  361. { .compatible = "allwinner,sun50i-a64-display-engine" },
  362. { .compatible = "allwinner,sun50i-h6-display-engine" },
  363. { }
  364. };
  365. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  366. static struct platform_driver sun4i_drv_platform_driver = {
  367. .probe = sun4i_drv_probe,
  368. .remove_new = sun4i_drv_remove,
  369. .shutdown = sun4i_drv_shutdown,
  370. .driver = {
  371. .name = "sun4i-drm",
  372. .of_match_table = sun4i_drv_of_table,
  373. .pm = &sun4i_drv_drm_pm_ops,
  374. },
  375. };
  376. drm_module_platform_driver(sun4i_drv_platform_driver);
  377. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  378. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  379. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  380. MODULE_LICENSE("GPL");