exynos_drm_crtc.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 void
  36. exynos_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
  37. {
  38. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  39. if (exynos_crtc->ops->commit)
  40. exynos_crtc->ops->commit(exynos_crtc);
  41. }
  42. static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
  43. struct drm_crtc_state *state)
  44. {
  45. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  46. if (!state->enable)
  47. return 0;
  48. if (exynos_crtc->ops->atomic_check)
  49. return exynos_crtc->ops->atomic_check(exynos_crtc, state);
  50. return 0;
  51. }
  52. static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
  53. struct drm_crtc_state *old_crtc_state)
  54. {
  55. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  56. exynos_crtc->event = crtc->state->event;
  57. if (exynos_crtc->ops->atomic_begin)
  58. exynos_crtc->ops->atomic_begin(exynos_crtc);
  59. }
  60. static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
  61. struct drm_crtc_state *old_crtc_state)
  62. {
  63. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  64. if (exynos_crtc->ops->atomic_flush)
  65. exynos_crtc->ops->atomic_flush(exynos_crtc);
  66. }
  67. static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  68. .enable = exynos_drm_crtc_enable,
  69. .disable = exynos_drm_crtc_disable,
  70. .mode_set_nofb = exynos_drm_crtc_mode_set_nofb,
  71. .atomic_check = exynos_crtc_atomic_check,
  72. .atomic_begin = exynos_crtc_atomic_begin,
  73. .atomic_flush = exynos_crtc_atomic_flush,
  74. };
  75. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  76. {
  77. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  78. struct exynos_drm_private *private = crtc->dev->dev_private;
  79. private->crtc[exynos_crtc->pipe] = NULL;
  80. drm_crtc_cleanup(crtc);
  81. kfree(exynos_crtc);
  82. }
  83. static const struct drm_crtc_funcs exynos_crtc_funcs = {
  84. .set_config = drm_atomic_helper_set_config,
  85. .page_flip = drm_atomic_helper_page_flip,
  86. .destroy = exynos_drm_crtc_destroy,
  87. .reset = drm_atomic_helper_crtc_reset,
  88. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  89. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  90. };
  91. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  92. struct drm_plane *plane,
  93. int pipe,
  94. enum exynos_drm_output_type type,
  95. const struct exynos_drm_crtc_ops *ops,
  96. void *ctx)
  97. {
  98. struct exynos_drm_crtc *exynos_crtc;
  99. struct exynos_drm_private *private = drm_dev->dev_private;
  100. struct drm_crtc *crtc;
  101. int ret;
  102. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  103. if (!exynos_crtc)
  104. return ERR_PTR(-ENOMEM);
  105. exynos_crtc->pipe = pipe;
  106. exynos_crtc->type = type;
  107. exynos_crtc->ops = ops;
  108. exynos_crtc->ctx = ctx;
  109. init_waitqueue_head(&exynos_crtc->wait_update);
  110. crtc = &exynos_crtc->base;
  111. private->crtc[pipe] = crtc;
  112. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  113. &exynos_crtc_funcs, NULL);
  114. if (ret < 0)
  115. goto err_crtc;
  116. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  117. return exynos_crtc;
  118. err_crtc:
  119. plane->funcs->destroy(plane);
  120. kfree(exynos_crtc);
  121. return ERR_PTR(ret);
  122. }
  123. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, unsigned int pipe)
  124. {
  125. struct exynos_drm_private *private = dev->dev_private;
  126. struct exynos_drm_crtc *exynos_crtc =
  127. to_exynos_crtc(private->crtc[pipe]);
  128. if (exynos_crtc->ops->enable_vblank)
  129. return exynos_crtc->ops->enable_vblank(exynos_crtc);
  130. return 0;
  131. }
  132. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, unsigned int pipe)
  133. {
  134. struct exynos_drm_private *private = dev->dev_private;
  135. struct exynos_drm_crtc *exynos_crtc =
  136. to_exynos_crtc(private->crtc[pipe]);
  137. if (exynos_crtc->ops->disable_vblank)
  138. exynos_crtc->ops->disable_vblank(exynos_crtc);
  139. }
  140. void exynos_drm_crtc_wait_pending_update(struct exynos_drm_crtc *exynos_crtc)
  141. {
  142. wait_event_timeout(exynos_crtc->wait_update,
  143. (atomic_read(&exynos_crtc->pending_update) == 0),
  144. msecs_to_jiffies(50));
  145. }
  146. void exynos_drm_crtc_finish_update(struct exynos_drm_crtc *exynos_crtc,
  147. struct exynos_drm_plane *exynos_plane)
  148. {
  149. struct drm_crtc *crtc = &exynos_crtc->base;
  150. unsigned long flags;
  151. exynos_plane->pending_fb = NULL;
  152. if (atomic_dec_and_test(&exynos_crtc->pending_update))
  153. wake_up(&exynos_crtc->wait_update);
  154. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  155. if (exynos_crtc->event)
  156. drm_crtc_send_vblank_event(crtc, exynos_crtc->event);
  157. exynos_crtc->event = NULL;
  158. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  159. }
  160. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  161. enum exynos_drm_output_type out_type)
  162. {
  163. struct drm_crtc *crtc;
  164. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  165. struct exynos_drm_crtc *exynos_crtc;
  166. exynos_crtc = to_exynos_crtc(crtc);
  167. if (exynos_crtc->type == out_type)
  168. return exynos_crtc->pipe;
  169. }
  170. return -EPERM;
  171. }
  172. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  173. {
  174. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  175. if (exynos_crtc->ops->te_handler)
  176. exynos_crtc->ops->te_handler(exynos_crtc);
  177. }
  178. void exynos_drm_crtc_cancel_page_flip(struct drm_crtc *crtc,
  179. struct drm_file *file)
  180. {
  181. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  182. struct drm_pending_vblank_event *e;
  183. unsigned long flags;
  184. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  185. e = exynos_crtc->event;
  186. if (e && e->base.file_priv == file) {
  187. exynos_crtc->event = NULL;
  188. /*
  189. * event will be destroyed by core part
  190. * so below line should be removed later with core changes
  191. */
  192. e->base.destroy(&e->base);
  193. /*
  194. * event_space will be increased by core part
  195. * so below line should be removed later with core changes.
  196. */
  197. file->event_space += sizeof(e->event);
  198. atomic_dec(&exynos_crtc->pending_update);
  199. }
  200. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  201. }