sun4i_drv.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/of_graph.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include <drm/drm_gem_cma_helper.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include "sun4i_crtc.h"
  20. #include "sun4i_drv.h"
  21. #include "sun4i_framebuffer.h"
  22. #include "sun4i_layer.h"
  23. #include "sun4i_tcon.h"
  24. static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
  25. {
  26. struct sun4i_drv *drv = drm->dev_private;
  27. struct sun4i_tcon *tcon = drv->tcon;
  28. DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
  29. sun4i_tcon_enable_vblank(tcon, true);
  30. return 0;
  31. }
  32. static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
  33. {
  34. struct sun4i_drv *drv = drm->dev_private;
  35. struct sun4i_tcon *tcon = drv->tcon;
  36. DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
  37. sun4i_tcon_enable_vblank(tcon, false);
  38. }
  39. static void sun4i_drv_lastclose(struct drm_device *dev)
  40. {
  41. struct sun4i_drv *drv = dev->dev_private;
  42. drm_fbdev_cma_restore_mode(drv->fbdev);
  43. }
  44. static const struct file_operations sun4i_drv_fops = {
  45. .owner = THIS_MODULE,
  46. .open = drm_open,
  47. .release = drm_release,
  48. .unlocked_ioctl = drm_ioctl,
  49. #ifdef CONFIG_COMPAT
  50. .compat_ioctl = drm_compat_ioctl,
  51. #endif
  52. .poll = drm_poll,
  53. .read = drm_read,
  54. .llseek = no_llseek,
  55. .mmap = drm_gem_cma_mmap,
  56. };
  57. static struct drm_driver sun4i_drv_driver = {
  58. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
  59. /* Generic Operations */
  60. .lastclose = sun4i_drv_lastclose,
  61. .fops = &sun4i_drv_fops,
  62. .name = "sun4i-drm",
  63. .desc = "Allwinner sun4i Display Engine",
  64. .date = "20150629",
  65. .major = 1,
  66. .minor = 0,
  67. /* GEM Operations */
  68. .dumb_create = drm_gem_cma_dumb_create,
  69. .dumb_destroy = drm_gem_dumb_destroy,
  70. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  71. .gem_free_object_unlocked = drm_gem_cma_free_object,
  72. .gem_vm_ops = &drm_gem_cma_vm_ops,
  73. /* PRIME Operations */
  74. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  75. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  76. .gem_prime_import = drm_gem_prime_import,
  77. .gem_prime_export = drm_gem_prime_export,
  78. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  79. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  80. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  81. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  82. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  83. /* Frame Buffer Operations */
  84. /* VBlank Operations */
  85. .get_vblank_counter = drm_vblank_no_hw_counter,
  86. .enable_vblank = sun4i_drv_enable_vblank,
  87. .disable_vblank = sun4i_drv_disable_vblank,
  88. };
  89. static void sun4i_remove_framebuffers(void)
  90. {
  91. struct apertures_struct *ap;
  92. ap = alloc_apertures(1);
  93. if (!ap)
  94. return;
  95. /* The framebuffer can be located anywhere in RAM */
  96. ap->ranges[0].base = 0;
  97. ap->ranges[0].size = ~0;
  98. drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
  99. kfree(ap);
  100. }
  101. static int sun4i_drv_bind(struct device *dev)
  102. {
  103. struct drm_device *drm;
  104. struct sun4i_drv *drv;
  105. int ret;
  106. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  107. if (IS_ERR(drm))
  108. return PTR_ERR(drm);
  109. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  110. if (!drv) {
  111. ret = -ENOMEM;
  112. goto free_drm;
  113. }
  114. drm->dev_private = drv;
  115. drm_vblank_init(drm, 1);
  116. drm_mode_config_init(drm);
  117. ret = component_bind_all(drm->dev, drm);
  118. if (ret) {
  119. dev_err(drm->dev, "Couldn't bind all pipelines components\n");
  120. goto cleanup_mode_config;
  121. }
  122. /* Create our layers */
  123. drv->layers = sun4i_layers_init(drm);
  124. if (IS_ERR(drv->layers)) {
  125. dev_err(drm->dev, "Couldn't create the planes\n");
  126. ret = PTR_ERR(drv->layers);
  127. goto cleanup_mode_config;
  128. }
  129. /* Create our CRTC */
  130. drv->crtc = sun4i_crtc_init(drm);
  131. if (!drv->crtc) {
  132. dev_err(drm->dev, "Couldn't create the CRTC\n");
  133. ret = -EINVAL;
  134. goto cleanup_mode_config;
  135. }
  136. drm->irq_enabled = true;
  137. /* Remove early framebuffers (ie. simplefb) */
  138. sun4i_remove_framebuffers();
  139. /* Create our framebuffer */
  140. drv->fbdev = sun4i_framebuffer_init(drm);
  141. if (IS_ERR(drv->fbdev)) {
  142. dev_err(drm->dev, "Couldn't create our framebuffer\n");
  143. ret = PTR_ERR(drv->fbdev);
  144. goto cleanup_mode_config;
  145. }
  146. /* Enable connectors polling */
  147. drm_kms_helper_poll_init(drm);
  148. ret = drm_dev_register(drm, 0);
  149. if (ret)
  150. goto finish_poll;
  151. return 0;
  152. finish_poll:
  153. drm_kms_helper_poll_fini(drm);
  154. sun4i_framebuffer_free(drm);
  155. cleanup_mode_config:
  156. drm_mode_config_cleanup(drm);
  157. drm_vblank_cleanup(drm);
  158. free_drm:
  159. drm_dev_unref(drm);
  160. return ret;
  161. }
  162. static void sun4i_drv_unbind(struct device *dev)
  163. {
  164. struct drm_device *drm = dev_get_drvdata(dev);
  165. drm_dev_unregister(drm);
  166. drm_kms_helper_poll_fini(drm);
  167. sun4i_framebuffer_free(drm);
  168. drm_vblank_cleanup(drm);
  169. drm_dev_unref(drm);
  170. }
  171. static const struct component_master_ops sun4i_drv_master_ops = {
  172. .bind = sun4i_drv_bind,
  173. .unbind = sun4i_drv_unbind,
  174. };
  175. static bool sun4i_drv_node_is_connector(struct device_node *node)
  176. {
  177. return of_device_is_compatible(node, "hdmi-connector");
  178. }
  179. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  180. {
  181. return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
  182. of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
  183. of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
  184. }
  185. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  186. {
  187. return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
  188. of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
  189. of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
  190. of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
  191. }
  192. static int compare_of(struct device *dev, void *data)
  193. {
  194. DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
  195. of_node_full_name(dev->of_node),
  196. of_node_full_name(data));
  197. return dev->of_node == data;
  198. }
  199. static int sun4i_drv_add_endpoints(struct device *dev,
  200. struct component_match **match,
  201. struct device_node *node)
  202. {
  203. struct device_node *port, *ep, *remote;
  204. int count = 0;
  205. /*
  206. * We don't support the frontend for now, so we will never
  207. * have a device bound. Just skip over it, but we still want
  208. * the rest our pipeline to be added.
  209. */
  210. if (!sun4i_drv_node_is_frontend(node) &&
  211. !of_device_is_available(node))
  212. return 0;
  213. /*
  214. * The connectors will be the last nodes in our pipeline, we
  215. * can just bail out.
  216. */
  217. if (sun4i_drv_node_is_connector(node))
  218. return 0;
  219. if (!sun4i_drv_node_is_frontend(node)) {
  220. /* Add current component */
  221. DRM_DEBUG_DRIVER("Adding component %s\n",
  222. of_node_full_name(node));
  223. component_match_add(dev, match, compare_of, node);
  224. count++;
  225. }
  226. /* Inputs are listed first, then outputs */
  227. port = of_graph_get_port_by_id(node, 1);
  228. if (!port) {
  229. DRM_DEBUG_DRIVER("No output to bind\n");
  230. return count;
  231. }
  232. for_each_available_child_of_node(port, ep) {
  233. remote = of_graph_get_remote_port_parent(ep);
  234. if (!remote) {
  235. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  236. of_node_put(remote);
  237. continue;
  238. }
  239. /*
  240. * If the node is our TCON, the first port is used for
  241. * panel or bridges, and will not be part of the
  242. * component framework.
  243. */
  244. if (sun4i_drv_node_is_tcon(node)) {
  245. struct of_endpoint endpoint;
  246. if (of_graph_parse_endpoint(ep, &endpoint)) {
  247. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  248. continue;
  249. }
  250. if (!endpoint.id) {
  251. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  252. continue;
  253. }
  254. }
  255. /* Walk down our tree */
  256. count += sun4i_drv_add_endpoints(dev, match, remote);
  257. of_node_put(remote);
  258. }
  259. return count;
  260. }
  261. static int sun4i_drv_probe(struct platform_device *pdev)
  262. {
  263. struct component_match *match = NULL;
  264. struct device_node *np = pdev->dev.of_node;
  265. int i, count = 0;
  266. for (i = 0;; i++) {
  267. struct device_node *pipeline = of_parse_phandle(np,
  268. "allwinner,pipelines",
  269. i);
  270. if (!pipeline)
  271. break;
  272. count += sun4i_drv_add_endpoints(&pdev->dev, &match,
  273. pipeline);
  274. of_node_put(pipeline);
  275. DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
  276. count, i);
  277. }
  278. if (count)
  279. return component_master_add_with_match(&pdev->dev,
  280. &sun4i_drv_master_ops,
  281. match);
  282. else
  283. return 0;
  284. }
  285. static int sun4i_drv_remove(struct platform_device *pdev)
  286. {
  287. return 0;
  288. }
  289. static const struct of_device_id sun4i_drv_of_table[] = {
  290. { .compatible = "allwinner,sun5i-a13-display-engine" },
  291. { .compatible = "allwinner,sun6i-a31-display-engine" },
  292. { .compatible = "allwinner,sun6i-a31s-display-engine" },
  293. { .compatible = "allwinner,sun8i-a33-display-engine" },
  294. { }
  295. };
  296. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  297. static struct platform_driver sun4i_drv_platform_driver = {
  298. .probe = sun4i_drv_probe,
  299. .remove = sun4i_drv_remove,
  300. .driver = {
  301. .name = "sun4i-drm",
  302. .of_match_table = sun4i_drv_of_table,
  303. },
  304. };
  305. module_platform_driver(sun4i_drv_platform_driver);
  306. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  307. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  308. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  309. MODULE_LICENSE("GPL");