sti_drv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_atomic.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_gem_cma_helper.h>
  16. #include <drm/drm_fb_cma_helper.h>
  17. #include "sti_crtc.h"
  18. #include "sti_drv.h"
  19. #define DRIVER_NAME "sti"
  20. #define DRIVER_DESC "STMicroelectronics SoC DRM"
  21. #define DRIVER_DATE "20140601"
  22. #define DRIVER_MAJOR 1
  23. #define DRIVER_MINOR 0
  24. #define STI_MAX_FB_HEIGHT 4096
  25. #define STI_MAX_FB_WIDTH 4096
  26. static void sti_atomic_schedule(struct sti_private *private,
  27. struct drm_atomic_state *state)
  28. {
  29. private->commit.state = state;
  30. schedule_work(&private->commit.work);
  31. }
  32. static void sti_atomic_complete(struct sti_private *private,
  33. struct drm_atomic_state *state)
  34. {
  35. struct drm_device *drm = private->drm_dev;
  36. /*
  37. * Everything below can be run asynchronously without the need to grab
  38. * any modeset locks at all under one condition: It must be guaranteed
  39. * that the asynchronous work has either been cancelled (if the driver
  40. * supports it, which at least requires that the framebuffers get
  41. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  42. * before the new state gets committed on the software side with
  43. * drm_atomic_helper_swap_state().
  44. *
  45. * This scheme allows new atomic state updates to be prepared and
  46. * checked in parallel to the asynchronous completion of the previous
  47. * update. Which is important since compositors need to figure out the
  48. * composition of the next frame right after having submitted the
  49. * current layout.
  50. */
  51. drm_atomic_helper_commit_modeset_disables(drm, state);
  52. drm_atomic_helper_commit_planes(drm, state, false);
  53. drm_atomic_helper_commit_modeset_enables(drm, state);
  54. drm_atomic_helper_wait_for_vblanks(drm, state);
  55. drm_atomic_helper_cleanup_planes(drm, state);
  56. drm_atomic_state_free(state);
  57. }
  58. static void sti_atomic_work(struct work_struct *work)
  59. {
  60. struct sti_private *private = container_of(work,
  61. struct sti_private, commit.work);
  62. sti_atomic_complete(private, private->commit.state);
  63. }
  64. static int sti_atomic_commit(struct drm_device *drm,
  65. struct drm_atomic_state *state, bool async)
  66. {
  67. struct sti_private *private = drm->dev_private;
  68. int err;
  69. err = drm_atomic_helper_prepare_planes(drm, state);
  70. if (err)
  71. return err;
  72. /* serialize outstanding asynchronous commits */
  73. mutex_lock(&private->commit.lock);
  74. flush_work(&private->commit.work);
  75. /*
  76. * This is the point of no return - everything below never fails except
  77. * when the hw goes bonghits. Which means we can commit the new state on
  78. * the software side now.
  79. */
  80. drm_atomic_helper_swap_state(drm, state);
  81. if (async)
  82. sti_atomic_schedule(private, state);
  83. else
  84. sti_atomic_complete(private, state);
  85. mutex_unlock(&private->commit.lock);
  86. return 0;
  87. }
  88. static const struct drm_mode_config_funcs sti_mode_config_funcs = {
  89. .fb_create = drm_fb_cma_create,
  90. .atomic_check = drm_atomic_helper_check,
  91. .atomic_commit = sti_atomic_commit,
  92. };
  93. static void sti_mode_config_init(struct drm_device *dev)
  94. {
  95. dev->mode_config.min_width = 0;
  96. dev->mode_config.min_height = 0;
  97. /*
  98. * set max width and height as default value.
  99. * this value would be used to check framebuffer size limitation
  100. * at drm_mode_addfb().
  101. */
  102. dev->mode_config.max_width = STI_MAX_FB_WIDTH;
  103. dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
  104. dev->mode_config.funcs = &sti_mode_config_funcs;
  105. }
  106. static int sti_load(struct drm_device *dev, unsigned long flags)
  107. {
  108. struct sti_private *private;
  109. int ret;
  110. private = kzalloc(sizeof(*private), GFP_KERNEL);
  111. if (!private) {
  112. DRM_ERROR("Failed to allocate private\n");
  113. return -ENOMEM;
  114. }
  115. dev->dev_private = (void *)private;
  116. private->drm_dev = dev;
  117. mutex_init(&private->commit.lock);
  118. INIT_WORK(&private->commit.work, sti_atomic_work);
  119. drm_mode_config_init(dev);
  120. drm_kms_helper_poll_init(dev);
  121. sti_mode_config_init(dev);
  122. ret = component_bind_all(dev->dev, dev);
  123. if (ret) {
  124. drm_kms_helper_poll_fini(dev);
  125. drm_mode_config_cleanup(dev);
  126. kfree(private);
  127. return ret;
  128. }
  129. drm_mode_config_reset(dev);
  130. drm_fbdev_cma_init(dev, 32,
  131. dev->mode_config.num_crtc,
  132. dev->mode_config.num_connector);
  133. return 0;
  134. }
  135. static const struct file_operations sti_driver_fops = {
  136. .owner = THIS_MODULE,
  137. .open = drm_open,
  138. .mmap = drm_gem_cma_mmap,
  139. .poll = drm_poll,
  140. .read = drm_read,
  141. .unlocked_ioctl = drm_ioctl,
  142. #ifdef CONFIG_COMPAT
  143. .compat_ioctl = drm_compat_ioctl,
  144. #endif
  145. .release = drm_release,
  146. };
  147. static struct dma_buf *sti_gem_prime_export(struct drm_device *dev,
  148. struct drm_gem_object *obj,
  149. int flags)
  150. {
  151. /* we want to be able to write in mmapped buffer */
  152. flags |= O_RDWR;
  153. return drm_gem_prime_export(dev, obj, flags);
  154. }
  155. static struct drm_driver sti_driver = {
  156. .driver_features = DRIVER_HAVE_IRQ | DRIVER_MODESET |
  157. DRIVER_GEM | DRIVER_PRIME,
  158. .load = sti_load,
  159. .gem_free_object = drm_gem_cma_free_object,
  160. .gem_vm_ops = &drm_gem_cma_vm_ops,
  161. .dumb_create = drm_gem_cma_dumb_create,
  162. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  163. .dumb_destroy = drm_gem_dumb_destroy,
  164. .fops = &sti_driver_fops,
  165. .get_vblank_counter = drm_vblank_no_hw_counter,
  166. .enable_vblank = sti_crtc_enable_vblank,
  167. .disable_vblank = sti_crtc_disable_vblank,
  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 = sti_gem_prime_export,
  171. .gem_prime_import = drm_gem_prime_import,
  172. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  173. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  174. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  175. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  176. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  177. .name = DRIVER_NAME,
  178. .desc = DRIVER_DESC,
  179. .date = DRIVER_DATE,
  180. .major = DRIVER_MAJOR,
  181. .minor = DRIVER_MINOR,
  182. };
  183. static int compare_of(struct device *dev, void *data)
  184. {
  185. return dev->of_node == data;
  186. }
  187. static int sti_bind(struct device *dev)
  188. {
  189. return drm_platform_init(&sti_driver, to_platform_device(dev));
  190. }
  191. static void sti_unbind(struct device *dev)
  192. {
  193. drm_put_dev(dev_get_drvdata(dev));
  194. }
  195. static const struct component_master_ops sti_ops = {
  196. .bind = sti_bind,
  197. .unbind = sti_unbind,
  198. };
  199. static int sti_platform_probe(struct platform_device *pdev)
  200. {
  201. struct device *dev = &pdev->dev;
  202. struct device_node *node = dev->of_node;
  203. struct device_node *child_np;
  204. struct component_match *match = NULL;
  205. dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  206. of_platform_populate(node, NULL, NULL, dev);
  207. child_np = of_get_next_available_child(node, NULL);
  208. while (child_np) {
  209. component_match_add(dev, &match, compare_of, child_np);
  210. of_node_put(child_np);
  211. child_np = of_get_next_available_child(node, child_np);
  212. }
  213. return component_master_add_with_match(dev, &sti_ops, match);
  214. }
  215. static int sti_platform_remove(struct platform_device *pdev)
  216. {
  217. component_master_del(&pdev->dev, &sti_ops);
  218. of_platform_depopulate(&pdev->dev);
  219. return 0;
  220. }
  221. static const struct of_device_id sti_dt_ids[] = {
  222. { .compatible = "st,sti-display-subsystem", },
  223. { /* end node */ },
  224. };
  225. MODULE_DEVICE_TABLE(of, sti_dt_ids);
  226. static struct platform_driver sti_platform_driver = {
  227. .probe = sti_platform_probe,
  228. .remove = sti_platform_remove,
  229. .driver = {
  230. .name = DRIVER_NAME,
  231. .of_match_table = sti_dt_ids,
  232. },
  233. };
  234. static struct platform_driver * const drivers[] = {
  235. &sti_tvout_driver,
  236. &sti_vtac_driver,
  237. &sti_hqvdp_driver,
  238. &sti_hdmi_driver,
  239. &sti_hda_driver,
  240. &sti_dvo_driver,
  241. &sti_vtg_driver,
  242. &sti_compositor_driver,
  243. &sti_platform_driver,
  244. };
  245. static int sti_drm_init(void)
  246. {
  247. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  248. }
  249. module_init(sti_drm_init);
  250. static void sti_drm_exit(void)
  251. {
  252. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  253. }
  254. module_exit(sti_drm_exit);
  255. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  256. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  257. MODULE_LICENSE("GPL");