armada_drv.c 8.8 KB

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