sun4i_drv.c 8.6 KB

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