sun4i_drv.c 12 KB

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