armada_drv.c 8.2 KB

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