armada_drv.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 int armada_drm_load(struct drm_device *dev, unsigned long flags)
  46. {
  47. struct armada_private *priv;
  48. struct resource *mem = NULL;
  49. int ret, n;
  50. for (n = 0; ; n++) {
  51. struct resource *r = platform_get_resource(dev->platformdev,
  52. IORESOURCE_MEM, n);
  53. if (!r)
  54. break;
  55. /* Resources above 64K are graphics memory */
  56. if (resource_size(r) > SZ_64K)
  57. mem = r;
  58. else
  59. return -EINVAL;
  60. }
  61. if (!mem)
  62. return -ENXIO;
  63. if (!devm_request_mem_region(dev->dev, mem->start,
  64. resource_size(mem), "armada-drm"))
  65. return -EBUSY;
  66. priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
  67. if (!priv) {
  68. DRM_ERROR("failed to allocate private\n");
  69. return -ENOMEM;
  70. }
  71. platform_set_drvdata(dev->platformdev, dev);
  72. dev->dev_private = priv;
  73. INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
  74. INIT_KFIFO(priv->fb_unref);
  75. /* Mode setting support */
  76. drm_mode_config_init(dev);
  77. dev->mode_config.min_width = 320;
  78. dev->mode_config.min_height = 200;
  79. /*
  80. * With vscale enabled, the maximum width is 1920 due to the
  81. * 1920 by 3 lines RAM
  82. */
  83. dev->mode_config.max_width = 1920;
  84. dev->mode_config.max_height = 2048;
  85. dev->mode_config.preferred_depth = 24;
  86. dev->mode_config.funcs = &armada_drm_mode_config_funcs;
  87. drm_mm_init(&priv->linear, mem->start, resource_size(mem));
  88. mutex_init(&priv->linear_lock);
  89. ret = component_bind_all(dev->dev, dev);
  90. if (ret)
  91. goto err_kms;
  92. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  93. if (ret)
  94. goto err_comp;
  95. dev->irq_enabled = true;
  96. dev->vblank_disable_allowed = 1;
  97. ret = armada_fbdev_init(dev);
  98. if (ret)
  99. goto err_comp;
  100. drm_kms_helper_poll_init(dev);
  101. return 0;
  102. err_comp:
  103. component_unbind_all(dev->dev, dev);
  104. err_kms:
  105. drm_mode_config_cleanup(dev);
  106. drm_mm_takedown(&priv->linear);
  107. flush_work(&priv->fb_unref_work);
  108. return ret;
  109. }
  110. static int armada_drm_unload(struct drm_device *dev)
  111. {
  112. struct armada_private *priv = dev->dev_private;
  113. drm_kms_helper_poll_fini(dev);
  114. armada_fbdev_fini(dev);
  115. component_unbind_all(dev->dev, dev);
  116. drm_mode_config_cleanup(dev);
  117. drm_mm_takedown(&priv->linear);
  118. flush_work(&priv->fb_unref_work);
  119. dev->dev_private = NULL;
  120. return 0;
  121. }
  122. /* These are called under the vbl_lock. */
  123. static int armada_drm_enable_vblank(struct drm_device *dev, unsigned int pipe)
  124. {
  125. struct armada_private *priv = dev->dev_private;
  126. armada_drm_crtc_enable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
  127. return 0;
  128. }
  129. static void armada_drm_disable_vblank(struct drm_device *dev, unsigned int pipe)
  130. {
  131. struct armada_private *priv = dev->dev_private;
  132. armada_drm_crtc_disable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
  133. }
  134. static struct drm_ioctl_desc armada_ioctls[] = {
  135. DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
  136. DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
  137. DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
  138. };
  139. static void armada_drm_lastclose(struct drm_device *dev)
  140. {
  141. armada_fbdev_lastclose(dev);
  142. }
  143. static const struct file_operations armada_drm_fops = {
  144. .owner = THIS_MODULE,
  145. .llseek = no_llseek,
  146. .read = drm_read,
  147. .poll = drm_poll,
  148. .unlocked_ioctl = drm_ioctl,
  149. .mmap = drm_gem_mmap,
  150. .open = drm_open,
  151. .release = drm_release,
  152. };
  153. static struct drm_driver armada_drm_driver = {
  154. .load = armada_drm_load,
  155. .lastclose = armada_drm_lastclose,
  156. .unload = armada_drm_unload,
  157. .set_busid = drm_platform_set_busid,
  158. .get_vblank_counter = drm_vblank_no_hw_counter,
  159. .enable_vblank = armada_drm_enable_vblank,
  160. .disable_vblank = armada_drm_disable_vblank,
  161. #ifdef CONFIG_DEBUG_FS
  162. .debugfs_init = armada_drm_debugfs_init,
  163. .debugfs_cleanup = armada_drm_debugfs_cleanup,
  164. #endif
  165. .gem_free_object = armada_gem_free_object,
  166. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  167. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  168. .gem_prime_export = armada_gem_prime_export,
  169. .gem_prime_import = armada_gem_prime_import,
  170. .dumb_create = armada_gem_dumb_create,
  171. .dumb_map_offset = armada_gem_dumb_map_offset,
  172. .dumb_destroy = armada_gem_dumb_destroy,
  173. .gem_vm_ops = &armada_gem_vm_ops,
  174. .major = 1,
  175. .minor = 0,
  176. .name = "armada-drm",
  177. .desc = "Armada SoC DRM",
  178. .date = "20120730",
  179. .driver_features = DRIVER_GEM | DRIVER_MODESET |
  180. DRIVER_HAVE_IRQ | DRIVER_PRIME,
  181. .ioctls = armada_ioctls,
  182. .fops = &armada_drm_fops,
  183. };
  184. static int armada_drm_bind(struct device *dev)
  185. {
  186. return drm_platform_init(&armada_drm_driver, to_platform_device(dev));
  187. }
  188. static void armada_drm_unbind(struct device *dev)
  189. {
  190. drm_put_dev(dev_get_drvdata(dev));
  191. }
  192. static int compare_of(struct device *dev, void *data)
  193. {
  194. return dev->of_node == data;
  195. }
  196. static int compare_dev_name(struct device *dev, void *data)
  197. {
  198. const char *name = data;
  199. return !strcmp(dev_name(dev), name);
  200. }
  201. static void armada_add_endpoints(struct device *dev,
  202. struct component_match **match, struct device_node *port)
  203. {
  204. struct device_node *ep, *remote;
  205. for_each_child_of_node(port, ep) {
  206. remote = of_graph_get_remote_port_parent(ep);
  207. if (!remote || !of_device_is_available(remote)) {
  208. of_node_put(remote);
  209. continue;
  210. } else if (!of_device_is_available(remote->parent)) {
  211. dev_warn(dev, "parent device of %s is not available\n",
  212. remote->full_name);
  213. of_node_put(remote);
  214. continue;
  215. }
  216. component_match_add(dev, match, compare_of, remote);
  217. of_node_put(remote);
  218. }
  219. }
  220. static const struct component_master_ops armada_master_ops = {
  221. .bind = armada_drm_bind,
  222. .unbind = armada_drm_unbind,
  223. };
  224. static int armada_drm_probe(struct platform_device *pdev)
  225. {
  226. struct component_match *match = NULL;
  227. struct device *dev = &pdev->dev;
  228. int ret;
  229. ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
  230. if (ret != -EINVAL)
  231. return ret;
  232. if (dev->platform_data) {
  233. char **devices = dev->platform_data;
  234. struct device_node *port;
  235. struct device *d;
  236. int i;
  237. for (i = 0; devices[i]; i++)
  238. component_match_add(dev, &match, compare_dev_name,
  239. devices[i]);
  240. if (i == 0) {
  241. dev_err(dev, "missing 'ports' property\n");
  242. return -ENODEV;
  243. }
  244. for (i = 0; devices[i]; i++) {
  245. d = bus_find_device_by_name(&platform_bus_type, NULL,
  246. devices[i]);
  247. if (d && d->of_node) {
  248. for_each_child_of_node(d->of_node, port)
  249. armada_add_endpoints(dev, &match, port);
  250. }
  251. put_device(d);
  252. }
  253. }
  254. return component_master_add_with_match(&pdev->dev, &armada_master_ops,
  255. match);
  256. }
  257. static int armada_drm_remove(struct platform_device *pdev)
  258. {
  259. component_master_del(&pdev->dev, &armada_master_ops);
  260. return 0;
  261. }
  262. static const struct platform_device_id armada_drm_platform_ids[] = {
  263. {
  264. .name = "armada-drm",
  265. }, {
  266. .name = "armada-510-drm",
  267. },
  268. { },
  269. };
  270. MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
  271. static struct platform_driver armada_drm_platform_driver = {
  272. .probe = armada_drm_probe,
  273. .remove = armada_drm_remove,
  274. .driver = {
  275. .name = "armada-drm",
  276. },
  277. .id_table = armada_drm_platform_ids,
  278. };
  279. static int __init armada_drm_init(void)
  280. {
  281. int ret;
  282. armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
  283. ret = platform_driver_register(&armada_lcd_platform_driver);
  284. if (ret)
  285. return ret;
  286. ret = platform_driver_register(&armada_drm_platform_driver);
  287. if (ret)
  288. platform_driver_unregister(&armada_lcd_platform_driver);
  289. return ret;
  290. }
  291. module_init(armada_drm_init);
  292. static void __exit armada_drm_exit(void)
  293. {
  294. platform_driver_unregister(&armada_drm_platform_driver);
  295. platform_driver_unregister(&armada_lcd_platform_driver);
  296. }
  297. module_exit(armada_drm_exit);
  298. MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
  299. MODULE_DESCRIPTION("Armada DRM Driver");
  300. MODULE_LICENSE("GPL");
  301. MODULE_ALIAS("platform:armada-drm");