armada_drv.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2012 Russell King
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/component.h>
  10. #include <linux/module.h>
  11. #include <linux/of_graph.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_crtc_helper.h>
  14. #include <drm/drm_fb_helper.h>
  15. #include <drm/drm_of.h>
  16. #include "armada_crtc.h"
  17. #include "armada_drm.h"
  18. #include "armada_gem.h"
  19. #include "armada_fb.h"
  20. #include "armada_hw.h"
  21. #include <drm/armada_drm.h>
  22. #include "armada_ioctlP.h"
  23. static struct drm_ioctl_desc armada_ioctls[] = {
  24. DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
  25. DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
  26. DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
  27. };
  28. DEFINE_DRM_GEM_FOPS(armada_drm_fops);
  29. static struct drm_driver armada_drm_driver = {
  30. .lastclose = drm_fb_helper_lastclose,
  31. .gem_free_object_unlocked = armada_gem_free_object,
  32. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  33. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  34. .gem_prime_export = armada_gem_prime_export,
  35. .gem_prime_import = armada_gem_prime_import,
  36. .dumb_create = armada_gem_dumb_create,
  37. .gem_vm_ops = &armada_gem_vm_ops,
  38. .major = 1,
  39. .minor = 0,
  40. .name = "armada-drm",
  41. .desc = "Armada SoC DRM",
  42. .date = "20120730",
  43. .driver_features = DRIVER_GEM | DRIVER_MODESET |
  44. DRIVER_PRIME | DRIVER_ATOMIC,
  45. .ioctls = armada_ioctls,
  46. .fops = &armada_drm_fops,
  47. };
  48. static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
  49. .fb_create = armada_fb_create,
  50. .output_poll_changed = drm_fb_helper_output_poll_changed,
  51. .atomic_check = drm_atomic_helper_check,
  52. .atomic_commit = drm_atomic_helper_commit,
  53. };
  54. static int armada_drm_bind(struct device *dev)
  55. {
  56. struct armada_private *priv;
  57. struct resource *mem = NULL;
  58. int ret, n;
  59. for (n = 0; ; n++) {
  60. struct resource *r = platform_get_resource(to_platform_device(dev),
  61. IORESOURCE_MEM, n);
  62. if (!r)
  63. break;
  64. /* Resources above 64K are graphics memory */
  65. if (resource_size(r) > SZ_64K)
  66. mem = r;
  67. else
  68. return -EINVAL;
  69. }
  70. if (!mem)
  71. return -ENXIO;
  72. if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
  73. "armada-drm"))
  74. return -EBUSY;
  75. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  76. if (!priv)
  77. return -ENOMEM;
  78. /*
  79. * The drm_device structure must be at the start of
  80. * armada_private for drm_dev_put() to work correctly.
  81. */
  82. BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
  83. ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
  84. if (ret) {
  85. dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
  86. __func__, ret);
  87. kfree(priv);
  88. return ret;
  89. }
  90. priv->drm.dev_private = priv;
  91. dev_set_drvdata(dev, &priv->drm);
  92. /* Mode setting support */
  93. drm_mode_config_init(&priv->drm);
  94. priv->drm.mode_config.min_width = 320;
  95. priv->drm.mode_config.min_height = 200;
  96. /*
  97. * With vscale enabled, the maximum width is 1920 due to the
  98. * 1920 by 3 lines RAM
  99. */
  100. priv->drm.mode_config.max_width = 1920;
  101. priv->drm.mode_config.max_height = 2048;
  102. priv->drm.mode_config.preferred_depth = 24;
  103. priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
  104. drm_mm_init(&priv->linear, mem->start, resource_size(mem));
  105. mutex_init(&priv->linear_lock);
  106. ret = component_bind_all(dev, &priv->drm);
  107. if (ret)
  108. goto err_kms;
  109. ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
  110. if (ret)
  111. goto err_comp;
  112. priv->drm.irq_enabled = true;
  113. drm_mode_config_reset(&priv->drm);
  114. ret = armada_fbdev_init(&priv->drm);
  115. if (ret)
  116. goto err_comp;
  117. drm_kms_helper_poll_init(&priv->drm);
  118. ret = drm_dev_register(&priv->drm, 0);
  119. if (ret)
  120. goto err_poll;
  121. #ifdef CONFIG_DEBUG_FS
  122. armada_drm_debugfs_init(priv->drm.primary);
  123. #endif
  124. return 0;
  125. err_poll:
  126. drm_kms_helper_poll_fini(&priv->drm);
  127. armada_fbdev_fini(&priv->drm);
  128. err_comp:
  129. component_unbind_all(dev, &priv->drm);
  130. err_kms:
  131. drm_mode_config_cleanup(&priv->drm);
  132. drm_mm_takedown(&priv->linear);
  133. drm_dev_put(&priv->drm);
  134. return ret;
  135. }
  136. static void armada_drm_unbind(struct device *dev)
  137. {
  138. struct drm_device *drm = dev_get_drvdata(dev);
  139. struct armada_private *priv = drm->dev_private;
  140. drm_kms_helper_poll_fini(&priv->drm);
  141. armada_fbdev_fini(&priv->drm);
  142. drm_dev_unregister(&priv->drm);
  143. component_unbind_all(dev, &priv->drm);
  144. drm_mode_config_cleanup(&priv->drm);
  145. drm_mm_takedown(&priv->linear);
  146. drm_dev_put(&priv->drm);
  147. }
  148. static int compare_of(struct device *dev, void *data)
  149. {
  150. return dev->of_node == data;
  151. }
  152. static int compare_dev_name(struct device *dev, void *data)
  153. {
  154. const char *name = data;
  155. return !strcmp(dev_name(dev), name);
  156. }
  157. static void armada_add_endpoints(struct device *dev,
  158. struct component_match **match, struct device_node *port)
  159. {
  160. struct device_node *ep, *remote;
  161. for_each_child_of_node(port, ep) {
  162. remote = of_graph_get_remote_port_parent(ep);
  163. if (!remote || !of_device_is_available(remote)) {
  164. of_node_put(remote);
  165. continue;
  166. } else if (!of_device_is_available(remote->parent)) {
  167. dev_warn(dev, "parent device of %pOF is not available\n",
  168. remote);
  169. of_node_put(remote);
  170. continue;
  171. }
  172. drm_of_component_match_add(dev, match, compare_of, remote);
  173. of_node_put(remote);
  174. }
  175. }
  176. static const struct component_master_ops armada_master_ops = {
  177. .bind = armada_drm_bind,
  178. .unbind = armada_drm_unbind,
  179. };
  180. static int armada_drm_probe(struct platform_device *pdev)
  181. {
  182. struct component_match *match = NULL;
  183. struct device *dev = &pdev->dev;
  184. int ret;
  185. ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
  186. if (ret != -EINVAL)
  187. return ret;
  188. if (dev->platform_data) {
  189. char **devices = dev->platform_data;
  190. struct device_node *port;
  191. struct device *d;
  192. int i;
  193. for (i = 0; devices[i]; i++)
  194. component_match_add(dev, &match, compare_dev_name,
  195. devices[i]);
  196. if (i == 0) {
  197. dev_err(dev, "missing 'ports' property\n");
  198. return -ENODEV;
  199. }
  200. for (i = 0; devices[i]; i++) {
  201. d = bus_find_device_by_name(&platform_bus_type, NULL,
  202. devices[i]);
  203. if (d && d->of_node) {
  204. for_each_child_of_node(d->of_node, port)
  205. armada_add_endpoints(dev, &match, port);
  206. }
  207. put_device(d);
  208. }
  209. }
  210. return component_master_add_with_match(&pdev->dev, &armada_master_ops,
  211. match);
  212. }
  213. static int armada_drm_remove(struct platform_device *pdev)
  214. {
  215. component_master_del(&pdev->dev, &armada_master_ops);
  216. return 0;
  217. }
  218. static const struct platform_device_id armada_drm_platform_ids[] = {
  219. {
  220. .name = "armada-drm",
  221. }, {
  222. .name = "armada-510-drm",
  223. },
  224. { },
  225. };
  226. MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
  227. static struct platform_driver armada_drm_platform_driver = {
  228. .probe = armada_drm_probe,
  229. .remove = armada_drm_remove,
  230. .driver = {
  231. .name = "armada-drm",
  232. },
  233. .id_table = armada_drm_platform_ids,
  234. };
  235. static int __init armada_drm_init(void)
  236. {
  237. int ret;
  238. armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
  239. ret = platform_driver_register(&armada_lcd_platform_driver);
  240. if (ret)
  241. return ret;
  242. ret = platform_driver_register(&armada_drm_platform_driver);
  243. if (ret)
  244. platform_driver_unregister(&armada_lcd_platform_driver);
  245. return ret;
  246. }
  247. module_init(armada_drm_init);
  248. static void __exit armada_drm_exit(void)
  249. {
  250. platform_driver_unregister(&armada_drm_platform_driver);
  251. platform_driver_unregister(&armada_lcd_platform_driver);
  252. }
  253. module_exit(armada_drm_exit);
  254. MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
  255. MODULE_DESCRIPTION("Armada DRM Driver");
  256. MODULE_LICENSE("GPL");
  257. MODULE_ALIAS("platform:armada-drm");