sun4i_drv.c 8.4 KB

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