exynos_drm_crtc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* exynos_drm_crtc.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * Authors:
  6. * Inki Dae <inki.dae@samsung.com>
  7. * Joonyoung Shim <jy0922.shim@samsung.com>
  8. * Seung-Woo Kim <sw0312.kim@samsung.com>
  9. */
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_encoder.h>
  13. #include <drm/drm_probe_helper.h>
  14. #include <drm/drm_vblank.h>
  15. #include "exynos_drm_crtc.h"
  16. #include "exynos_drm_drv.h"
  17. #include "exynos_drm_plane.h"
  18. static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
  19. struct drm_crtc_state *old_state)
  20. {
  21. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  22. if (exynos_crtc->ops->atomic_enable)
  23. exynos_crtc->ops->atomic_enable(exynos_crtc);
  24. drm_crtc_vblank_on(crtc);
  25. }
  26. static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
  27. struct drm_crtc_state *old_state)
  28. {
  29. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  30. drm_crtc_vblank_off(crtc);
  31. if (exynos_crtc->ops->atomic_disable)
  32. exynos_crtc->ops->atomic_disable(exynos_crtc);
  33. if (crtc->state->event && !crtc->state->active) {
  34. spin_lock_irq(&crtc->dev->event_lock);
  35. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  36. spin_unlock_irq(&crtc->dev->event_lock);
  37. crtc->state->event = NULL;
  38. }
  39. }
  40. static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
  41. struct drm_crtc_state *state)
  42. {
  43. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  44. if (!state->enable)
  45. return 0;
  46. if (exynos_crtc->ops->atomic_check)
  47. return exynos_crtc->ops->atomic_check(exynos_crtc, state);
  48. return 0;
  49. }
  50. static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
  51. struct drm_crtc_state *old_crtc_state)
  52. {
  53. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  54. if (exynos_crtc->ops->atomic_begin)
  55. exynos_crtc->ops->atomic_begin(exynos_crtc);
  56. }
  57. static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
  58. struct drm_crtc_state *old_crtc_state)
  59. {
  60. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  61. if (exynos_crtc->ops->atomic_flush)
  62. exynos_crtc->ops->atomic_flush(exynos_crtc);
  63. }
  64. static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
  65. const struct drm_display_mode *mode)
  66. {
  67. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  68. if (exynos_crtc->ops->mode_valid)
  69. return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
  70. return MODE_OK;
  71. }
  72. static bool exynos_crtc_mode_fixup(struct drm_crtc *crtc,
  73. const struct drm_display_mode *mode,
  74. struct drm_display_mode *adjusted_mode)
  75. {
  76. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  77. if (exynos_crtc->ops->mode_fixup)
  78. return exynos_crtc->ops->mode_fixup(exynos_crtc, mode,
  79. adjusted_mode);
  80. return true;
  81. }
  82. static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  83. .mode_valid = exynos_crtc_mode_valid,
  84. .mode_fixup = exynos_crtc_mode_fixup,
  85. .atomic_check = exynos_crtc_atomic_check,
  86. .atomic_begin = exynos_crtc_atomic_begin,
  87. .atomic_flush = exynos_crtc_atomic_flush,
  88. .atomic_enable = exynos_drm_crtc_atomic_enable,
  89. .atomic_disable = exynos_drm_crtc_atomic_disable,
  90. };
  91. void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
  92. {
  93. struct drm_crtc *crtc = &exynos_crtc->base;
  94. struct drm_pending_vblank_event *event = crtc->state->event;
  95. unsigned long flags;
  96. if (!event)
  97. return;
  98. crtc->state->event = NULL;
  99. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  100. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  101. drm_crtc_arm_vblank_event(crtc, event);
  102. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  103. }
  104. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  105. {
  106. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  107. drm_crtc_cleanup(crtc);
  108. kfree(exynos_crtc);
  109. }
  110. static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
  111. {
  112. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  113. if (exynos_crtc->ops->enable_vblank)
  114. return exynos_crtc->ops->enable_vblank(exynos_crtc);
  115. return 0;
  116. }
  117. static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
  118. {
  119. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  120. if (exynos_crtc->ops->disable_vblank)
  121. exynos_crtc->ops->disable_vblank(exynos_crtc);
  122. }
  123. static const struct drm_crtc_funcs exynos_crtc_funcs = {
  124. .set_config = drm_atomic_helper_set_config,
  125. .page_flip = drm_atomic_helper_page_flip,
  126. .destroy = exynos_drm_crtc_destroy,
  127. .reset = drm_atomic_helper_crtc_reset,
  128. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  129. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  130. .enable_vblank = exynos_drm_crtc_enable_vblank,
  131. .disable_vblank = exynos_drm_crtc_disable_vblank,
  132. };
  133. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  134. struct drm_plane *plane,
  135. enum exynos_drm_output_type type,
  136. const struct exynos_drm_crtc_ops *ops,
  137. void *ctx)
  138. {
  139. struct exynos_drm_crtc *exynos_crtc;
  140. struct drm_crtc *crtc;
  141. int ret;
  142. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  143. if (!exynos_crtc)
  144. return ERR_PTR(-ENOMEM);
  145. exynos_crtc->type = type;
  146. exynos_crtc->ops = ops;
  147. exynos_crtc->ctx = ctx;
  148. crtc = &exynos_crtc->base;
  149. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  150. &exynos_crtc_funcs, NULL);
  151. if (ret < 0)
  152. goto err_crtc;
  153. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  154. return exynos_crtc;
  155. err_crtc:
  156. plane->funcs->destroy(plane);
  157. kfree(exynos_crtc);
  158. return ERR_PTR(ret);
  159. }
  160. struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
  161. enum exynos_drm_output_type out_type)
  162. {
  163. struct drm_crtc *crtc;
  164. drm_for_each_crtc(crtc, drm_dev)
  165. if (to_exynos_crtc(crtc)->type == out_type)
  166. return to_exynos_crtc(crtc);
  167. return ERR_PTR(-ENODEV);
  168. }
  169. int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
  170. enum exynos_drm_output_type out_type)
  171. {
  172. struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
  173. out_type);
  174. if (IS_ERR(crtc))
  175. return PTR_ERR(crtc);
  176. encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
  177. return 0;
  178. }
  179. void exynos_drm_crtc_te_handler(struct drm_crtc *crtc)
  180. {
  181. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  182. if (exynos_crtc->ops->te_handler)
  183. exynos_crtc->ops->te_handler(exynos_crtc);
  184. }