sun4i_drv.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "sun4i_crtc.h"
  19. #include "sun4i_drv.h"
  20. #include "sun4i_framebuffer.h"
  21. #include "sun4i_layer.h"
  22. #include "sun4i_tcon.h"
  23. static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
  24. {
  25. struct sun4i_drv *drv = drm->dev_private;
  26. struct sun4i_tcon *tcon = drv->tcon;
  27. DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
  28. sun4i_tcon_enable_vblank(tcon, true);
  29. return 0;
  30. }
  31. static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
  32. {
  33. struct sun4i_drv *drv = drm->dev_private;
  34. struct sun4i_tcon *tcon = drv->tcon;
  35. DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
  36. sun4i_tcon_enable_vblank(tcon, false);
  37. }
  38. static const struct file_operations sun4i_drv_fops = {
  39. .owner = THIS_MODULE,
  40. .open = drm_open,
  41. .release = drm_release,
  42. .unlocked_ioctl = drm_ioctl,
  43. #ifdef CONFIG_COMPAT
  44. .compat_ioctl = drm_compat_ioctl,
  45. #endif
  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 = 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. 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 (!drm)
  101. return -ENOMEM;
  102. ret = drm_dev_set_unique(drm, dev_name(drm->dev));
  103. if (ret)
  104. goto free_drm;
  105. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  106. if (!drv) {
  107. ret = -ENOMEM;
  108. goto free_drm;
  109. }
  110. drm->dev_private = drv;
  111. drm_vblank_init(drm, 1);
  112. drm_mode_config_init(drm);
  113. ret = component_bind_all(drm->dev, drm);
  114. if (ret) {
  115. dev_err(drm->dev, "Couldn't bind all pipelines components\n");
  116. goto free_drm;
  117. }
  118. /* Create our layers */
  119. drv->layers = sun4i_layers_init(drm);
  120. if (!drv->layers) {
  121. dev_err(drm->dev, "Couldn't create the planes\n");
  122. ret = -EINVAL;
  123. goto free_drm;
  124. }
  125. /* Create our CRTC */
  126. drv->crtc = sun4i_crtc_init(drm);
  127. if (!drv->crtc) {
  128. dev_err(drm->dev, "Couldn't create the CRTC\n");
  129. ret = -EINVAL;
  130. goto free_drm;
  131. }
  132. drm->irq_enabled = true;
  133. /* Remove early framebuffers (ie. simplefb) */
  134. sun4i_remove_framebuffers();
  135. /* Create our framebuffer */
  136. drv->fbdev = sun4i_framebuffer_init(drm);
  137. if (IS_ERR(drv->fbdev)) {
  138. dev_err(drm->dev, "Couldn't create our framebuffer\n");
  139. ret = PTR_ERR(drv->fbdev);
  140. goto free_drm;
  141. }
  142. /* Enable connectors polling */
  143. drm_kms_helper_poll_init(drm);
  144. ret = drm_dev_register(drm, 0);
  145. if (ret)
  146. goto free_drm;
  147. ret = drm_connector_register_all(drm);
  148. if (ret)
  149. goto unregister_drm;
  150. return 0;
  151. unregister_drm:
  152. drm_dev_unregister(drm);
  153. free_drm:
  154. drm_dev_unref(drm);
  155. return ret;
  156. }
  157. static void sun4i_drv_unbind(struct device *dev)
  158. {
  159. struct drm_device *drm = dev_get_drvdata(dev);
  160. drm_connector_unregister_all(drm);
  161. drm_dev_unregister(drm);
  162. drm_kms_helper_poll_fini(drm);
  163. sun4i_framebuffer_free(drm);
  164. drm_vblank_cleanup(drm);
  165. drm_dev_unref(drm);
  166. }
  167. static const struct component_master_ops sun4i_drv_master_ops = {
  168. .bind = sun4i_drv_bind,
  169. .unbind = sun4i_drv_unbind,
  170. };
  171. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  172. {
  173. return of_device_is_compatible(node,
  174. "allwinner,sun5i-a13-display-frontend");
  175. }
  176. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  177. {
  178. return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon");
  179. }
  180. static int compare_of(struct device *dev, void *data)
  181. {
  182. DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
  183. of_node_full_name(dev->of_node),
  184. of_node_full_name(data));
  185. return dev->of_node == data;
  186. }
  187. static int sun4i_drv_add_endpoints(struct device *dev,
  188. struct component_match **match,
  189. struct device_node *node)
  190. {
  191. struct device_node *port, *ep, *remote;
  192. int count = 0;
  193. /*
  194. * We don't support the frontend for now, so we will never
  195. * have a device bound. Just skip over it, but we still want
  196. * the rest our pipeline to be added.
  197. */
  198. if (!sun4i_drv_node_is_frontend(node) &&
  199. !of_device_is_available(node))
  200. return 0;
  201. if (!sun4i_drv_node_is_frontend(node)) {
  202. /* Add current component */
  203. DRM_DEBUG_DRIVER("Adding component %s\n",
  204. of_node_full_name(node));
  205. component_match_add(dev, match, compare_of, node);
  206. count++;
  207. }
  208. /* Inputs are listed first, then outputs */
  209. port = of_graph_get_port_by_id(node, 1);
  210. if (!port) {
  211. DRM_DEBUG_DRIVER("No output to bind\n");
  212. return count;
  213. }
  214. for_each_available_child_of_node(port, ep) {
  215. remote = of_graph_get_remote_port_parent(ep);
  216. if (!remote) {
  217. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  218. of_node_put(remote);
  219. continue;
  220. }
  221. /*
  222. * If the node is our TCON, the first port is used for our
  223. * panel, and will not be part of the
  224. * component framework.
  225. */
  226. if (sun4i_drv_node_is_tcon(node)) {
  227. struct of_endpoint endpoint;
  228. if (of_graph_parse_endpoint(ep, &endpoint)) {
  229. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  230. continue;
  231. }
  232. if (!endpoint.id) {
  233. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  234. continue;
  235. }
  236. }
  237. /* Walk down our tree */
  238. count += sun4i_drv_add_endpoints(dev, match, remote);
  239. of_node_put(remote);
  240. }
  241. return count;
  242. }
  243. static int sun4i_drv_probe(struct platform_device *pdev)
  244. {
  245. struct component_match *match = NULL;
  246. struct device_node *np = pdev->dev.of_node;
  247. int i, count = 0;
  248. for (i = 0;; i++) {
  249. struct device_node *pipeline = of_parse_phandle(np,
  250. "allwinner,pipelines",
  251. i);
  252. if (!pipeline)
  253. break;
  254. count += sun4i_drv_add_endpoints(&pdev->dev, &match,
  255. pipeline);
  256. of_node_put(pipeline);
  257. DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
  258. count, i);
  259. }
  260. if (count)
  261. return component_master_add_with_match(&pdev->dev,
  262. &sun4i_drv_master_ops,
  263. match);
  264. else
  265. return 0;
  266. }
  267. static int sun4i_drv_remove(struct platform_device *pdev)
  268. {
  269. return 0;
  270. }
  271. static const struct of_device_id sun4i_drv_of_table[] = {
  272. { .compatible = "allwinner,sun5i-a13-display-engine" },
  273. { }
  274. };
  275. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  276. static struct platform_driver sun4i_drv_platform_driver = {
  277. .probe = sun4i_drv_probe,
  278. .remove = sun4i_drv_remove,
  279. .driver = {
  280. .name = "sun4i-drm",
  281. .of_match_table = sun4i_drv_of_table,
  282. },
  283. };
  284. module_platform_driver(sun4i_drv_platform_driver);
  285. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  286. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  287. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  288. MODULE_LICENSE("GPL");