sti_drm_drv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. return ret;
  57. drm_helper_disable_unused_functions(dev);
  58. #ifdef CONFIG_DRM_STI_FBDEV
  59. drm_fbdev_cma_init(dev, 32,
  60. dev->mode_config.num_crtc,
  61. dev->mode_config.num_connector);
  62. #endif
  63. return 0;
  64. }
  65. static const struct file_operations sti_drm_driver_fops = {
  66. .owner = THIS_MODULE,
  67. .open = drm_open,
  68. .mmap = drm_gem_cma_mmap,
  69. .poll = drm_poll,
  70. .read = drm_read,
  71. .unlocked_ioctl = drm_ioctl,
  72. #ifdef CONFIG_COMPAT
  73. .compat_ioctl = drm_compat_ioctl,
  74. #endif
  75. .release = drm_release,
  76. };
  77. static struct dma_buf *sti_drm_gem_prime_export(struct drm_device *dev,
  78. struct drm_gem_object *obj,
  79. int flags)
  80. {
  81. /* we want to be able to write in mmapped buffer */
  82. flags |= O_RDWR;
  83. return drm_gem_prime_export(dev, obj, flags);
  84. }
  85. static struct drm_driver sti_drm_driver = {
  86. .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
  87. DRIVER_GEM | DRIVER_PRIME,
  88. .load = sti_drm_load,
  89. .gem_free_object = drm_gem_cma_free_object,
  90. .gem_vm_ops = &drm_gem_cma_vm_ops,
  91. .dumb_create = drm_gem_cma_dumb_create,
  92. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  93. .dumb_destroy = drm_gem_dumb_destroy,
  94. .fops = &sti_drm_driver_fops,
  95. .get_vblank_counter = drm_vblank_count,
  96. .enable_vblank = sti_drm_crtc_enable_vblank,
  97. .disable_vblank = sti_drm_crtc_disable_vblank,
  98. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  99. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  100. .gem_prime_export = sti_drm_gem_prime_export,
  101. .gem_prime_import = drm_gem_prime_import,
  102. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  103. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  104. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  105. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  106. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  107. .name = DRIVER_NAME,
  108. .desc = DRIVER_DESC,
  109. .date = DRIVER_DATE,
  110. .major = DRIVER_MAJOR,
  111. .minor = DRIVER_MINOR,
  112. };
  113. static int compare_of(struct device *dev, void *data)
  114. {
  115. return dev->of_node == data;
  116. }
  117. static int sti_drm_bind(struct device *dev)
  118. {
  119. return drm_platform_init(&sti_drm_driver, to_platform_device(dev));
  120. }
  121. static void sti_drm_unbind(struct device *dev)
  122. {
  123. drm_put_dev(dev_get_drvdata(dev));
  124. }
  125. static const struct component_master_ops sti_drm_ops = {
  126. .bind = sti_drm_bind,
  127. .unbind = sti_drm_unbind,
  128. };
  129. static int sti_drm_master_probe(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct device_node *node = dev->parent->of_node;
  133. struct device_node *child_np;
  134. struct component_match *match = NULL;
  135. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  136. child_np = of_get_next_available_child(node, NULL);
  137. while (child_np) {
  138. component_match_add(dev, &match, compare_of, child_np);
  139. of_node_put(child_np);
  140. child_np = of_get_next_available_child(node, child_np);
  141. }
  142. return component_master_add_with_match(dev, &sti_drm_ops, match);
  143. }
  144. static int sti_drm_master_remove(struct platform_device *pdev)
  145. {
  146. component_master_del(&pdev->dev, &sti_drm_ops);
  147. return 0;
  148. }
  149. static struct platform_driver sti_drm_master_driver = {
  150. .probe = sti_drm_master_probe,
  151. .remove = sti_drm_master_remove,
  152. .driver = {
  153. .owner = THIS_MODULE,
  154. .name = DRIVER_NAME "__master",
  155. },
  156. };
  157. static int sti_drm_platform_probe(struct platform_device *pdev)
  158. {
  159. struct device *dev = &pdev->dev;
  160. struct device_node *node = dev->of_node;
  161. struct platform_device *master;
  162. of_platform_populate(node, NULL, NULL, dev);
  163. platform_driver_register(&sti_drm_master_driver);
  164. master = platform_device_register_resndata(dev,
  165. DRIVER_NAME "__master", -1,
  166. NULL, 0, NULL, 0);
  167. if (IS_ERR(master))
  168. return PTR_ERR(master);
  169. platform_set_drvdata(pdev, master);
  170. return 0;
  171. }
  172. static int sti_drm_platform_remove(struct platform_device *pdev)
  173. {
  174. struct platform_device *master = platform_get_drvdata(pdev);
  175. of_platform_depopulate(&pdev->dev);
  176. platform_device_unregister(master);
  177. platform_driver_unregister(&sti_drm_master_driver);
  178. return 0;
  179. }
  180. static const struct of_device_id sti_drm_dt_ids[] = {
  181. { .compatible = "st,sti-display-subsystem", },
  182. { /* end node */ },
  183. };
  184. MODULE_DEVICE_TABLE(of, sti_drm_dt_ids);
  185. static struct platform_driver sti_drm_platform_driver = {
  186. .probe = sti_drm_platform_probe,
  187. .remove = sti_drm_platform_remove,
  188. .driver = {
  189. .owner = THIS_MODULE,
  190. .name = DRIVER_NAME,
  191. .of_match_table = sti_drm_dt_ids,
  192. },
  193. };
  194. module_platform_driver(sti_drm_platform_driver);
  195. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  196. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  197. MODULE_LICENSE("GPL");