exynos_drm_crtc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* exynos_drm_crtc.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include "exynos_drm_crtc.h"
  19. #include "exynos_drm_drv.h"
  20. #include "exynos_drm_plane.h"
  21. static void exynos_drm_crtc_enable(struct drm_crtc *crtc)
  22. {
  23. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  24. if (exynos_crtc->ops->enable)
  25. exynos_crtc->ops->enable(exynos_crtc);
  26. drm_crtc_vblank_on(crtc);
  27. }
  28. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  29. {
  30. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  31. drm_crtc_vblank_off(crtc);
  32. if (exynos_crtc->ops->disable)
  33. exynos_crtc->ops->disable(exynos_crtc);
  34. }
  35. static bool
  36. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  37. const struct drm_display_mode *mode,
  38. struct drm_display_mode *adjusted_mode)
  39. {
  40. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  41. if (exynos_crtc->ops->mode_fixup)
  42. return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
  43. adjusted_mode);
  44. return true;
  45. }
  46. static void
  47. exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  48. {
  49. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  50. if (exynos_crtc->ops->commit)
  51. exynos_crtc->ops->commit(exynos_crtc);
  52. }
  53. static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
  54. struct drm_crtc_state *old_crtc_state)
  55. {
  56. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  57. struct drm_plane *plane;
  58. exynos_crtc->event = crtc->state->event;
  59. drm_atomic_crtc_for_each_plane(plane, crtc) {
  60. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  61. if (exynos_crtc->ops->atomic_begin)
  62. exynos_crtc->ops->atomic_begin(exynos_crtc,
  63. exynos_plane);
  64. }
  65. }
  66. static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
  67. struct drm_crtc_state *old_crtc_state)
  68. {
  69. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  70. struct drm_plane *plane;
  71. drm_atomic_crtc_for_each_plane(plane, crtc) {
  72. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  73. if (exynos_crtc->ops->atomic_flush)
  74. exynos_crtc->ops->atomic_flush(exynos_crtc,
  75. exynos_plane);
  76. }
  77. }
  78. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  79. .enable = exynos_drm_crtc_enable,
  80. .disable = exynos_drm_crtc_disable,
  81. .mode_fixup = exynos_drm_crtc_mode_fixup,
  82. .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
  83. .atomic_begin = exynos_crtc_atomic_begin,
  84. .atomic_flush = exynos_crtc_atomic_flush,
  85. };
  86. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  87. {
  88. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  89. struct exynos_drm_private *private = crtc->dev->dev_private;
  90. private->crtc[exynos_crtc->pipe] = NULL;
  91. drm_crtc_cleanup(crtc);
  92. kfree(exynos_crtc);
  93. }
  94. static struct drm_crtc_funcs exynos_crtc_funcs = {
  95. .set_config = drm_atomic_helper_set_config,
  96. .page_flip = drm_atomic_helper_page_flip,
  97. .destroy = exynos_drm_crtc_destroy,
  98. .reset = drm_atomic_helper_crtc_reset,
  99. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  100. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  101. };
  102. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  103. struct drm_plane *plane,
  104. int pipe,
  105. enum exynos_drm_output_type type,
  106. const struct exynos_drm_crtc_ops *ops,
  107. void *ctx)
  108. {
  109. struct exynos_drm_crtc *exynos_crtc;
  110. struct exynos_drm_private *private = drm_dev->dev_private;
  111. struct drm_crtc *crtc;
  112. int ret;
  113. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  114. if (!exynos_crtc)
  115. return ERR_PTR(-ENOMEM);
  116. exynos_crtc->pipe = pipe;
  117. exynos_crtc->type = type;
  118. exynos_crtc->ops = ops;
  119. exynos_crtc->ctx = ctx;
  120. init_waitqueue_head(&exynos_crtc->wait_update);
  121. crtc = &exynos_crtc->base;
  122. private->crtc[pipe] = crtc;
  123. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  124. &exynos_crtc_funcs);
  125. if (ret < 0)
  126. goto err_crtc;
  127. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  128. return exynos_crtc;
  129. err_crtc:
  130. plane->funcs->destroy(plane);
  131. kfree(exynos_crtc);
  132. return ERR_PTR(ret);
  133. }
  134. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  135. {
  136. struct exynos_drm_private *private = dev->dev_private;
  137. struct exynos_drm_crtc *exynos_crtc =
  138. to_exynos_crtc(private->crtc[pipe]);
  139. if (exynos_crtc->ops->enable_vblank)
  140. return exynos_crtc->ops->enable_vblank(exynos_crtc);
  141. return 0;
  142. }
  143. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  144. {
  145. struct exynos_drm_private *private = dev->dev_private;
  146. struct exynos_drm_crtc *exynos_crtc =
  147. to_exynos_crtc(private->crtc[pipe]);
  148. if (exynos_crtc->ops->disable_vblank)
  149. exynos_crtc->ops->disable_vblank(exynos_crtc);
  150. }
  151. void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
  152. {
  153. wait_event_timeout(exynos_crtc->wait_update,
  154. (atomic_read(&exynos_crtc->pending_update) == 0),
  155. msecs_to_jiffies(50));
  156. }
  157. void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
  158. struct exynos_drm_plane *exynos_plane)
  159. {
  160. struct drm_crtc *crtc = &exynos_crtc->base;
  161. unsigned long flags;
  162. exynos_plane->pending_fb = NULL;
  163. if (atomic_dec_and_test(&exynos_crtc->pending_update))
  164. wake_up(&exynos_crtc->wait_update);
  165. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  166. if (exynos_crtc->event)
  167. drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
  168. exynos_crtc->event = NULL;
  169. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  170. }
  171. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  172. {
  173. struct exynos_drm_crtc *exynos_crtc;
  174. struct drm_device *dev = fb->dev;
  175. struct drm_crtc *crtc;
  176. /*
  177. * make sure that overlay data are updated to real hardware
  178. * for all encoders.
  179. */
  180. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  181. exynos_crtc = to_exynos_crtc(crtc);
  182. /*
  183. * wait for vblank interrupt
  184. * - this makes sure that overlay data are updated to
  185. * real hardware.
  186. */
  187. if (exynos_crtc->ops->wait_for_vblank)
  188. exynos_crtc->ops->wait_for_vblank(exynos_crtc);
  189. }
  190. }
  191. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  192. enum exynos_drm_output_type out_type)
  193. {
  194. struct drm_crtc *crtc;
  195. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  196. struct exynos_drm_crtc *exynos_crtc;
  197. exynos_crtc = to_exynos_crtc(crtc);
  198. if (exynos_crtc->type == out_type)
  199. return exynos_crtc->pipe;
  200. }
  201. return -EPERM;
  202. }
  203. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  204. {
  205. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  206. if (exynos_crtc->ops->te_handler)
  207. exynos_crtc->ops->te_handler(exynos_crtc);
  208. }