vc4_drv.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom
  3. * Copyright (C) 2013 Red Hat
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/device.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include "drm_fb_cma_helper.h"
  17. #include "vc4_drv.h"
  18. #include "vc4_regs.h"
  19. #define DRIVER_NAME "vc4"
  20. #define DRIVER_DESC "Broadcom VC4 graphics"
  21. #define DRIVER_DATE "20140616"
  22. #define DRIVER_MAJOR 0
  23. #define DRIVER_MINOR 0
  24. #define DRIVER_PATCHLEVEL 0
  25. /* Helper function for mapping the regs on a platform device. */
  26. void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
  27. {
  28. struct resource *res;
  29. void __iomem *map;
  30. res = platform_get_resource(dev, IORESOURCE_MEM, index);
  31. map = devm_ioremap_resource(&dev->dev, res);
  32. if (IS_ERR(map)) {
  33. DRM_ERROR("Failed to map registers: %ld\n", PTR_ERR(map));
  34. return map;
  35. }
  36. return map;
  37. }
  38. static void vc4_drm_preclose(struct drm_device *dev, struct drm_file *file)
  39. {
  40. struct drm_crtc *crtc;
  41. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  42. vc4_cancel_page_flip(crtc, file);
  43. }
  44. static void vc4_lastclose(struct drm_device *dev)
  45. {
  46. struct vc4_dev *vc4 = to_vc4_dev(dev);
  47. if (vc4->fbdev)
  48. drm_fbdev_cma_restore_mode(vc4->fbdev);
  49. }
  50. static const struct file_operations vc4_drm_fops = {
  51. .owner = THIS_MODULE,
  52. .open = drm_open,
  53. .release = drm_release,
  54. .unlocked_ioctl = drm_ioctl,
  55. .mmap = drm_gem_cma_mmap,
  56. .poll = drm_poll,
  57. .read = drm_read,
  58. #ifdef CONFIG_COMPAT
  59. .compat_ioctl = drm_compat_ioctl,
  60. #endif
  61. .llseek = noop_llseek,
  62. };
  63. static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
  64. };
  65. static struct drm_driver vc4_drm_driver = {
  66. .driver_features = (DRIVER_MODESET |
  67. DRIVER_ATOMIC |
  68. DRIVER_GEM |
  69. DRIVER_PRIME),
  70. .lastclose = vc4_lastclose,
  71. .preclose = vc4_drm_preclose,
  72. .enable_vblank = vc4_enable_vblank,
  73. .disable_vblank = vc4_disable_vblank,
  74. .get_vblank_counter = drm_vblank_count,
  75. #if defined(CONFIG_DEBUG_FS)
  76. .debugfs_init = vc4_debugfs_init,
  77. .debugfs_cleanup = vc4_debugfs_cleanup,
  78. #endif
  79. .gem_free_object = drm_gem_cma_free_object,
  80. .gem_vm_ops = &drm_gem_cma_vm_ops,
  81. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  82. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  83. .gem_prime_import = drm_gem_prime_import,
  84. .gem_prime_export = drm_gem_prime_export,
  85. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  86. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  87. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  88. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  89. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  90. .dumb_create = vc4_dumb_create,
  91. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  92. .dumb_destroy = drm_gem_dumb_destroy,
  93. .ioctls = vc4_drm_ioctls,
  94. .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
  95. .fops = &vc4_drm_fops,
  96. .name = DRIVER_NAME,
  97. .desc = DRIVER_DESC,
  98. .date = DRIVER_DATE,
  99. .major = DRIVER_MAJOR,
  100. .minor = DRIVER_MINOR,
  101. .patchlevel = DRIVER_PATCHLEVEL,
  102. };
  103. static int compare_dev(struct device *dev, void *data)
  104. {
  105. return dev == data;
  106. }
  107. static void vc4_match_add_drivers(struct device *dev,
  108. struct component_match **match,
  109. struct platform_driver *const *drivers,
  110. int count)
  111. {
  112. int i;
  113. for (i = 0; i < count; i++) {
  114. struct device_driver *drv = &drivers[i]->driver;
  115. struct device *p = NULL, *d;
  116. while ((d = bus_find_device(&platform_bus_type, p, drv,
  117. (void *)platform_bus_type.match))) {
  118. put_device(p);
  119. component_match_add(dev, match, compare_dev, d);
  120. p = d;
  121. }
  122. put_device(p);
  123. }
  124. }
  125. static int vc4_drm_bind(struct device *dev)
  126. {
  127. struct platform_device *pdev = to_platform_device(dev);
  128. struct drm_device *drm;
  129. struct drm_connector *connector;
  130. struct vc4_dev *vc4;
  131. int ret = 0;
  132. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  133. vc4 = devm_kzalloc(dev, sizeof(*vc4), GFP_KERNEL);
  134. if (!vc4)
  135. return -ENOMEM;
  136. drm = drm_dev_alloc(&vc4_drm_driver, dev);
  137. if (!drm)
  138. return -ENOMEM;
  139. platform_set_drvdata(pdev, drm);
  140. vc4->dev = drm;
  141. drm->dev_private = vc4;
  142. drm_dev_set_unique(drm, dev_name(dev));
  143. drm_mode_config_init(drm);
  144. if (ret)
  145. goto unref;
  146. ret = component_bind_all(dev, drm);
  147. if (ret)
  148. goto unref;
  149. ret = drm_dev_register(drm, 0);
  150. if (ret < 0)
  151. goto unbind_all;
  152. /* Connector registration has to occur after DRM device
  153. * registration, because it creates sysfs entries based on the
  154. * DRM device.
  155. */
  156. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  157. ret = drm_connector_register(connector);
  158. if (ret)
  159. goto unregister;
  160. }
  161. vc4_kms_load(drm);
  162. return 0;
  163. unregister:
  164. drm_dev_unregister(drm);
  165. unbind_all:
  166. component_unbind_all(dev, drm);
  167. unref:
  168. drm_dev_unref(drm);
  169. return ret;
  170. }
  171. static void vc4_drm_unbind(struct device *dev)
  172. {
  173. struct platform_device *pdev = to_platform_device(dev);
  174. struct drm_device *drm = platform_get_drvdata(pdev);
  175. struct vc4_dev *vc4 = to_vc4_dev(drm);
  176. if (vc4->fbdev)
  177. drm_fbdev_cma_fini(vc4->fbdev);
  178. drm_mode_config_cleanup(drm);
  179. drm_put_dev(drm);
  180. }
  181. static const struct component_master_ops vc4_drm_ops = {
  182. .bind = vc4_drm_bind,
  183. .unbind = vc4_drm_unbind,
  184. };
  185. static struct platform_driver *const component_drivers[] = {
  186. &vc4_hdmi_driver,
  187. &vc4_crtc_driver,
  188. &vc4_hvs_driver,
  189. };
  190. static int vc4_platform_drm_probe(struct platform_device *pdev)
  191. {
  192. struct component_match *match = NULL;
  193. struct device *dev = &pdev->dev;
  194. vc4_match_add_drivers(dev, &match,
  195. component_drivers, ARRAY_SIZE(component_drivers));
  196. return component_master_add_with_match(dev, &vc4_drm_ops, match);
  197. }
  198. static int vc4_platform_drm_remove(struct platform_device *pdev)
  199. {
  200. component_master_del(&pdev->dev, &vc4_drm_ops);
  201. return 0;
  202. }
  203. static const struct of_device_id vc4_of_match[] = {
  204. { .compatible = "brcm,bcm2835-vc4", },
  205. {},
  206. };
  207. MODULE_DEVICE_TABLE(of, vc4_of_match);
  208. static struct platform_driver vc4_platform_driver = {
  209. .probe = vc4_platform_drm_probe,
  210. .remove = vc4_platform_drm_remove,
  211. .driver = {
  212. .name = "vc4-drm",
  213. .of_match_table = vc4_of_match,
  214. },
  215. };
  216. static int __init vc4_drm_register(void)
  217. {
  218. int i, ret;
  219. for (i = 0; i < ARRAY_SIZE(component_drivers); i++) {
  220. ret = platform_driver_register(component_drivers[i]);
  221. if (ret) {
  222. while (--i >= 0)
  223. platform_driver_unregister(component_drivers[i]);
  224. return ret;
  225. }
  226. }
  227. return platform_driver_register(&vc4_platform_driver);
  228. }
  229. static void __exit vc4_drm_unregister(void)
  230. {
  231. int i;
  232. for (i = ARRAY_SIZE(component_drivers) - 1; i >= 0; i--)
  233. platform_driver_unregister(component_drivers[i]);
  234. platform_driver_unregister(&vc4_platform_driver);
  235. }
  236. module_init(vc4_drm_register);
  237. module_exit(vc4_drm_unregister);
  238. MODULE_ALIAS("platform:vc4-drm");
  239. MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
  240. MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
  241. MODULE_LICENSE("GPL v2");