sun4i_drv.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_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_count,
  79. .enable_vblank = sun4i_drv_enable_vblank,
  80. .disable_vblank = sun4i_drv_disable_vblank,
  81. };
  82. static int sun4i_drv_bind(struct device *dev)
  83. {
  84. struct drm_device *drm;
  85. struct sun4i_drv *drv;
  86. int ret;
  87. drm = drm_dev_alloc(&sun4i_drv_driver, dev);
  88. if (!drm)
  89. return -ENOMEM;
  90. drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
  91. if (!drv) {
  92. ret = -ENOMEM;
  93. goto free_drm;
  94. }
  95. drm->dev_private = drv;
  96. drm_vblank_init(drm, 1);
  97. drm_mode_config_init(drm);
  98. ret = component_bind_all(drm->dev, drm);
  99. if (ret) {
  100. dev_err(drm->dev, "Couldn't bind all pipelines components\n");
  101. goto free_drm;
  102. }
  103. /* Create our layers */
  104. drv->layers = sun4i_layers_init(drm);
  105. if (!drv->layers) {
  106. dev_err(drm->dev, "Couldn't create the planes\n");
  107. ret = -EINVAL;
  108. goto free_drm;
  109. }
  110. /* Create our CRTC */
  111. drv->crtc = sun4i_crtc_init(drm);
  112. if (!drv->crtc) {
  113. dev_err(drm->dev, "Couldn't create the CRTC\n");
  114. ret = -EINVAL;
  115. goto free_drm;
  116. }
  117. drm->irq_enabled = true;
  118. /* Create our framebuffer */
  119. drv->fbdev = sun4i_framebuffer_init(drm);
  120. if (IS_ERR(drv->fbdev)) {
  121. dev_err(drm->dev, "Couldn't create our framebuffer\n");
  122. ret = PTR_ERR(drv->fbdev);
  123. goto free_drm;
  124. }
  125. /* Enable connectors polling */
  126. drm_kms_helper_poll_init(drm);
  127. ret = drm_dev_register(drm, 0);
  128. if (ret)
  129. goto free_drm;
  130. return 0;
  131. free_drm:
  132. drm_dev_unref(drm);
  133. return ret;
  134. }
  135. static void sun4i_drv_unbind(struct device *dev)
  136. {
  137. struct drm_device *drm = dev_get_drvdata(dev);
  138. drm_dev_unregister(drm);
  139. drm_kms_helper_poll_fini(drm);
  140. sun4i_framebuffer_free(drm);
  141. drm_vblank_cleanup(drm);
  142. drm_dev_unref(drm);
  143. }
  144. static const struct component_master_ops sun4i_drv_master_ops = {
  145. .bind = sun4i_drv_bind,
  146. .unbind = sun4i_drv_unbind,
  147. };
  148. static bool sun4i_drv_node_is_frontend(struct device_node *node)
  149. {
  150. return of_device_is_compatible(node,
  151. "allwinner,sun5i-a13-display-frontend");
  152. }
  153. static bool sun4i_drv_node_is_tcon(struct device_node *node)
  154. {
  155. return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon");
  156. }
  157. static int compare_of(struct device *dev, void *data)
  158. {
  159. DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
  160. of_node_full_name(dev->of_node),
  161. of_node_full_name(data));
  162. return dev->of_node == data;
  163. }
  164. static int sun4i_drv_add_endpoints(struct device *dev,
  165. struct component_match **match,
  166. struct device_node *node)
  167. {
  168. struct device_node *port, *ep, *remote;
  169. int count = 0;
  170. /*
  171. * We don't support the frontend for now, so we will never
  172. * have a device bound. Just skip over it, but we still want
  173. * the rest our pipeline to be added.
  174. */
  175. if (!sun4i_drv_node_is_frontend(node) &&
  176. !of_device_is_available(node))
  177. return 0;
  178. if (!sun4i_drv_node_is_frontend(node)) {
  179. /* Add current component */
  180. DRM_DEBUG_DRIVER("Adding component %s\n",
  181. of_node_full_name(node));
  182. component_match_add(dev, match, compare_of, node);
  183. count++;
  184. }
  185. /* Inputs are listed first, then outputs */
  186. port = of_graph_get_port_by_id(node, 1);
  187. if (!port) {
  188. DRM_DEBUG_DRIVER("No output to bind\n");
  189. return count;
  190. }
  191. for_each_available_child_of_node(port, ep) {
  192. remote = of_graph_get_remote_port_parent(ep);
  193. if (!remote) {
  194. DRM_DEBUG_DRIVER("Error retrieving the output node\n");
  195. of_node_put(remote);
  196. continue;
  197. }
  198. /*
  199. * If the node is our TCON, the first port is used for our
  200. * panel, and will not be part of the
  201. * component framework.
  202. */
  203. if (sun4i_drv_node_is_tcon(node)) {
  204. struct of_endpoint endpoint;
  205. if (of_graph_parse_endpoint(ep, &endpoint)) {
  206. DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
  207. continue;
  208. }
  209. if (!endpoint.id) {
  210. DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
  211. continue;
  212. }
  213. }
  214. /* Walk down our tree */
  215. count += sun4i_drv_add_endpoints(dev, match, remote);
  216. of_node_put(remote);
  217. }
  218. return count;
  219. }
  220. static int sun4i_drv_probe(struct platform_device *pdev)
  221. {
  222. struct component_match *match = NULL;
  223. struct device_node *np = pdev->dev.of_node;
  224. int i, count = 0;
  225. for (i = 0;; i++) {
  226. struct device_node *pipeline = of_parse_phandle(np,
  227. "allwinner,pipelines",
  228. i);
  229. if (!pipeline)
  230. break;
  231. count += sun4i_drv_add_endpoints(&pdev->dev, &match,
  232. pipeline);
  233. DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
  234. count, i);
  235. }
  236. if (count)
  237. return component_master_add_with_match(&pdev->dev,
  238. &sun4i_drv_master_ops,
  239. match);
  240. else
  241. return 0;
  242. }
  243. static int sun4i_drv_remove(struct platform_device *pdev)
  244. {
  245. return 0;
  246. }
  247. static const struct of_device_id sun4i_drv_of_table[] = {
  248. { .compatible = "allwinner,sun5i-a13-display-engine" },
  249. { }
  250. };
  251. MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
  252. static struct platform_driver sun4i_drv_platform_driver = {
  253. .probe = sun4i_drv_probe,
  254. .remove = sun4i_drv_remove,
  255. .driver = {
  256. .name = "sun4i-drm",
  257. .of_match_table = sun4i_drv_of_table,
  258. },
  259. };
  260. module_platform_driver(sun4i_drv_platform_driver);
  261. MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
  262. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  263. MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
  264. MODULE_LICENSE("GPL");