exynos_drm_crtc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_encoder.h"
  21. #include "exynos_drm_plane.h"
  22. static void exynos_drm_crtc_enable(struct drm_crtc *crtc)
  23. {
  24. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  25. if (exynos_crtc->enabled)
  26. return;
  27. if (exynos_crtc->ops->enable)
  28. exynos_crtc->ops->enable(exynos_crtc);
  29. exynos_crtc->enabled = true;
  30. drm_crtc_vblank_on(crtc);
  31. }
  32. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  33. {
  34. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  35. if (!exynos_crtc->enabled)
  36. return;
  37. /* wait for the completion of page flip. */
  38. if (!wait_event_timeout(exynos_crtc->pending_flip_queue,
  39. (exynos_crtc->event == NULL), HZ/20))
  40. exynos_crtc->event = NULL;
  41. drm_crtc_vblank_off(crtc);
  42. if (exynos_crtc->ops->disable)
  43. exynos_crtc->ops->disable(exynos_crtc);
  44. exynos_crtc->enabled = false;
  45. }
  46. static bool
  47. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  48. const struct drm_display_mode *mode,
  49. struct drm_display_mode *adjusted_mode)
  50. {
  51. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  52. if (exynos_crtc->ops->mode_fixup)
  53. return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
  54. adjusted_mode);
  55. return true;
  56. }
  57. static void
  58. exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  59. {
  60. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  61. if (exynos_crtc->ops->commit)
  62. exynos_crtc->ops->commit(exynos_crtc);
  63. }
  64. static void exynos_crtc_atomic_begin(struct drm_crtc *crtc)
  65. {
  66. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  67. if (crtc->state->event) {
  68. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  69. exynos_crtc->event = crtc->state->event;
  70. }
  71. }
  72. static void exynos_crtc_atomic_flush(struct drm_crtc *crtc)
  73. {
  74. }
  75. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  76. .enable = exynos_drm_crtc_enable,
  77. .disable = exynos_drm_crtc_disable,
  78. .mode_fixup = exynos_drm_crtc_mode_fixup,
  79. .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
  80. .atomic_begin = exynos_crtc_atomic_begin,
  81. .atomic_flush = exynos_crtc_atomic_flush,
  82. };
  83. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  84. {
  85. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  86. struct exynos_drm_private *private = crtc->dev->dev_private;
  87. private->crtc[exynos_crtc->pipe] = NULL;
  88. drm_crtc_cleanup(crtc);
  89. kfree(exynos_crtc);
  90. }
  91. static struct drm_crtc_funcs exynos_crtc_funcs = {
  92. .set_config = drm_atomic_helper_set_config,
  93. .page_flip = drm_atomic_helper_page_flip,
  94. .destroy = exynos_drm_crtc_destroy,
  95. .reset = drm_atomic_helper_crtc_reset,
  96. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  97. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  98. };
  99. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  100. struct drm_plane *plane,
  101. int pipe,
  102. enum exynos_drm_output_type type,
  103. const struct exynos_drm_crtc_ops *ops,
  104. void *ctx)
  105. {
  106. struct exynos_drm_crtc *exynos_crtc;
  107. struct exynos_drm_private *private = drm_dev->dev_private;
  108. struct drm_crtc *crtc;
  109. int ret;
  110. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  111. if (!exynos_crtc)
  112. return ERR_PTR(-ENOMEM);
  113. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  114. exynos_crtc->pipe = pipe;
  115. exynos_crtc->type = type;
  116. exynos_crtc->ops = ops;
  117. exynos_crtc->ctx = ctx;
  118. crtc = &exynos_crtc->base;
  119. private->crtc[pipe] = crtc;
  120. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  121. &exynos_crtc_funcs);
  122. if (ret < 0)
  123. goto err_crtc;
  124. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  125. return exynos_crtc;
  126. err_crtc:
  127. plane->funcs->destroy(plane);
  128. kfree(exynos_crtc);
  129. return ERR_PTR(ret);
  130. }
  131. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  132. {
  133. struct exynos_drm_private *private = dev->dev_private;
  134. struct exynos_drm_crtc *exynos_crtc =
  135. to_exynos_crtc(private->crtc[pipe]);
  136. if (!exynos_crtc->enabled)
  137. return -EPERM;
  138. if (exynos_crtc->ops->enable_vblank)
  139. exynos_crtc->ops->enable_vblank(exynos_crtc);
  140. return 0;
  141. }
  142. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  143. {
  144. struct exynos_drm_private *private = dev->dev_private;
  145. struct exynos_drm_crtc *exynos_crtc =
  146. to_exynos_crtc(private->crtc[pipe]);
  147. if (!exynos_crtc->enabled)
  148. return;
  149. if (exynos_crtc->ops->disable_vblank)
  150. exynos_crtc->ops->disable_vblank(exynos_crtc);
  151. }
  152. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
  153. {
  154. struct exynos_drm_private *dev_priv = dev->dev_private;
  155. struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
  156. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  157. unsigned long flags;
  158. spin_lock_irqsave(&dev->event_lock, flags);
  159. if (exynos_crtc->event) {
  160. drm_send_vblank_event(dev, -1, exynos_crtc->event);
  161. drm_vblank_put(dev, pipe);
  162. wake_up(&exynos_crtc->pending_flip_queue);
  163. }
  164. exynos_crtc->event = NULL;
  165. spin_unlock_irqrestore(&dev->event_lock, flags);
  166. }
  167. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  168. {
  169. struct exynos_drm_crtc *exynos_crtc;
  170. struct drm_device *dev = fb->dev;
  171. struct drm_crtc *crtc;
  172. /*
  173. * make sure that overlay data are updated to real hardware
  174. * for all encoders.
  175. */
  176. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  177. exynos_crtc = to_exynos_crtc(crtc);
  178. /*
  179. * wait for vblank interrupt
  180. * - this makes sure that overlay data are updated to
  181. * real hardware.
  182. */
  183. if (exynos_crtc->ops->wait_for_vblank)
  184. exynos_crtc->ops->wait_for_vblank(exynos_crtc);
  185. }
  186. }
  187. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  188. unsigned int out_type)
  189. {
  190. struct drm_crtc *crtc;
  191. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  192. struct exynos_drm_crtc *exynos_crtc;
  193. exynos_crtc = to_exynos_crtc(crtc);
  194. if (exynos_crtc->type == out_type)
  195. return exynos_crtc->pipe;
  196. }
  197. return -EPERM;
  198. }
  199. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  200. {
  201. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  202. if (exynos_crtc->ops->te_handler)
  203. exynos_crtc->ops->te_handler(exynos_crtc);
  204. }