sti_drm_drv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <drm/drmP.h>
  7. #include <linux/component.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of_platform.h>
  12. #include <drm/drm_crtc_helper.h>
  13. #include <drm/drm_gem_cma_helper.h>
  14. #include <drm/drm_fb_cma_helper.h>
  15. #include "sti_drm_drv.h"
  16. #include "sti_drm_crtc.h"
  17. #define DRIVER_NAME "sti"
  18. #define DRIVER_DESC "STMicroelectronics SoC DRM"
  19. #define DRIVER_DATE "20140601"
  20. #define DRIVER_MAJOR 1
  21. #define DRIVER_MINOR 0
  22. #define STI_MAX_FB_HEIGHT 4096
  23. #define STI_MAX_FB_WIDTH 4096
  24. static struct drm_mode_config_funcs sti_drm_mode_config_funcs = {
  25. .fb_create = drm_fb_cma_create,
  26. };
  27. static void sti_drm_mode_config_init(struct drm_device *dev)
  28. {
  29. dev->mode_config.min_width = 0;
  30. dev->mode_config.min_height = 0;
  31. /*
  32. * set max width and height as default value.
  33. * this value would be used to check framebuffer size limitation
  34. * at drm_mode_addfb().
  35. */
  36. dev->mode_config.max_width = STI_MAX_FB_HEIGHT;
  37. dev->mode_config.max_height = STI_MAX_FB_WIDTH;
  38. dev->mode_config.funcs = &sti_drm_mode_config_funcs;
  39. }
  40. static int sti_drm_load(struct drm_device *dev, unsigned long flags)
  41. {
  42. struct sti_drm_private *private;
  43. int ret;
  44. private = kzalloc(sizeof(struct sti_drm_private), GFP_KERNEL);
  45. if (!private) {
  46. DRM_ERROR("Failed to allocate private\n");
  47. return -ENOMEM;
  48. }
  49. dev->dev_private = (void *)private;
  50. private->drm_dev = dev;
  51. drm_mode_config_init(dev);
  52. drm_kms_helper_poll_init(dev);
  53. sti_drm_mode_config_init(dev);
  54. ret = component_bind_all(dev->dev, dev);
  55. if (ret) {
  56. drm_kms_helper_poll_fini(dev);
  57. drm_mode_config_cleanup(dev);
  58. kfree(private);
  59. return ret;
  60. }
  61. drm_helper_disable_unused_functions(dev);
  62. #ifdef CONFIG_DRM_STI_FBDEV
  63. drm_fbdev_cma_init(dev, 32,
  64. dev->mode_config.num_crtc,
  65. dev->mode_config.num_connector);
  66. #endif
  67. return 0;
  68. }
  69. static const struct file_operations sti_drm_driver_fops = {
  70. .owner = THIS_MODULE,
  71. .open = drm_open,
  72. .mmap = drm_gem_cma_mmap,
  73. .poll = drm_poll,
  74. .read = drm_read,
  75. .unlocked_ioctl = drm_ioctl,
  76. #ifdef CONFIG_COMPAT
  77. .compat_ioctl = drm_compat_ioctl,
  78. #endif
  79. .release = drm_release,
  80. };
  81. static struct dma_buf *sti_drm_gem_prime_export(struct drm_device *dev,
  82. struct drm_gem_object *obj,
  83. int flags)
  84. {
  85. /* we want to be able to write in mmapped buffer */
  86. flags |= O_RDWR;
  87. return drm_gem_prime_export(dev, obj, flags);
  88. }
  89. static struct drm_driver sti_drm_driver = {
  90. .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
  91. DRIVER_GEM | DRIVER_PRIME,
  92. .load = sti_drm_load,
  93. .gem_free_object = drm_gem_cma_free_object,
  94. .gem_vm_ops = &drm_gem_cma_vm_ops,
  95. .dumb_create = drm_gem_cma_dumb_create,
  96. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  97. .dumb_destroy = drm_gem_dumb_destroy,
  98. .fops = &sti_drm_driver_fops,
  99. .get_vblank_counter = drm_vblank_count,
  100. .enable_vblank = sti_drm_crtc_enable_vblank,
  101. .disable_vblank = sti_drm_crtc_disable_vblank,
  102. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  103. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  104. .gem_prime_export = sti_drm_gem_prime_export,
  105. .gem_prime_import = drm_gem_prime_import,
  106. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  107. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  108. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  109. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  110. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  111. .name = DRIVER_NAME,
  112. .desc = DRIVER_DESC,
  113. .date = DRIVER_DATE,
  114. .major = DRIVER_MAJOR,
  115. .minor = DRIVER_MINOR,
  116. };
  117. static int compare_of(struct device *dev, void *data)
  118. {
  119. return dev->of_node == data;
  120. }
  121. static int sti_drm_bind(struct device *dev)
  122. {
  123. return drm_platform_init(&sti_drm_driver, to_platform_device(dev));
  124. }
  125. static void sti_drm_unbind(struct device *dev)
  126. {
  127. drm_put_dev(dev_get_drvdata(dev));
  128. }
  129. static const struct component_master_ops sti_drm_ops = {
  130. .bind = sti_drm_bind,
  131. .unbind = sti_drm_unbind,
  132. };
  133. static int sti_drm_master_probe(struct platform_device *pdev)
  134. {
  135. struct device *dev = &pdev->dev;
  136. struct device_node *node = dev->parent->of_node;
  137. struct device_node *child_np;
  138. struct component_match *match = NULL;
  139. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  140. child_np = of_get_next_available_child(node, NULL);
  141. while (child_np) {
  142. component_match_add(dev, &match, compare_of, child_np);
  143. of_node_put(child_np);
  144. child_np = of_get_next_available_child(node, child_np);
  145. }
  146. return component_master_add_with_match(dev, &sti_drm_ops, match);
  147. }
  148. static int sti_drm_master_remove(struct platform_device *pdev)
  149. {
  150. component_master_del(&pdev->dev, &sti_drm_ops);
  151. return 0;
  152. }
  153. static struct platform_driver sti_drm_master_driver = {
  154. .probe = sti_drm_master_probe,
  155. .remove = sti_drm_master_remove,
  156. .driver = {
  157. .name = DRIVER_NAME "__master",
  158. },
  159. };
  160. static int sti_drm_platform_probe(struct platform_device *pdev)
  161. {
  162. struct device *dev = &pdev->dev;
  163. struct device_node *node = dev->of_node;
  164. struct platform_device *master;
  165. of_platform_populate(node, NULL, NULL, dev);
  166. platform_driver_register(&sti_drm_master_driver);
  167. master = platform_device_register_resndata(dev,
  168. DRIVER_NAME "__master", -1,
  169. NULL, 0, NULL, 0);
  170. if (IS_ERR(master))
  171. return PTR_ERR(master);
  172. platform_set_drvdata(pdev, master);
  173. return 0;
  174. }
  175. static int sti_drm_platform_remove(struct platform_device *pdev)
  176. {
  177. struct platform_device *master = platform_get_drvdata(pdev);
  178. of_platform_depopulate(&pdev->dev);
  179. platform_device_unregister(master);
  180. platform_driver_unregister(&sti_drm_master_driver);
  181. return 0;
  182. }
  183. static const struct of_device_id sti_drm_dt_ids[] = {
  184. { .compatible = "st,sti-display-subsystem", },
  185. { /* end node */ },
  186. };
  187. MODULE_DEVICE_TABLE(of, sti_drm_dt_ids);
  188. static struct platform_driver sti_drm_platform_driver = {
  189. .probe = sti_drm_platform_probe,
  190. .remove = sti_drm_platform_remove,
  191. .driver = {
  192. .name = DRIVER_NAME,
  193. .of_match_table = sti_drm_dt_ids,
  194. },
  195. };
  196. module_platform_driver(sti_drm_platform_driver);
  197. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  198. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  199. MODULE_LICENSE("GPL");