exynos_drm_crtc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <drm/drm_encoder.h>
  19. #include "exynos_drm_crtc.h"
  20. #include "exynos_drm_drv.h"
  21. #include "exynos_drm_plane.h"
  22. static void exynos_drm_crtc_atomic_enable(struct drm_crtc *crtc,
  23. struct drm_crtc_state *old_state)
  24. {
  25. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  26. if (exynos_crtc->ops->enable)
  27. exynos_crtc->ops->enable(exynos_crtc);
  28. drm_crtc_vblank_on(crtc);
  29. }
  30. static void exynos_drm_crtc_atomic_disable(struct drm_crtc *crtc,
  31. struct drm_crtc_state *old_state)
  32. {
  33. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  34. drm_crtc_vblank_off(crtc);
  35. if (exynos_crtc->ops->disable)
  36. exynos_crtc->ops->disable(exynos_crtc);
  37. if (crtc->state->event && !crtc->state->active) {
  38. spin_lock_irq(&crtc->dev->event_lock);
  39. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  40. spin_unlock_irq(&crtc->dev->event_lock);
  41. crtc->state->event = NULL;
  42. }
  43. }
  44. static int exynos_crtc_atomic_check(struct drm_crtc *crtc,
  45. struct drm_crtc_state *state)
  46. {
  47. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  48. if (!state->enable)
  49. return 0;
  50. if (exynos_crtc->ops->atomic_check)
  51. return exynos_crtc->ops->atomic_check(exynos_crtc, state);
  52. return 0;
  53. }
  54. static void exynos_crtc_atomic_begin(struct drm_crtc *crtc,
  55. struct drm_crtc_state *old_crtc_state)
  56. {
  57. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  58. if (exynos_crtc->ops->atomic_begin)
  59. exynos_crtc->ops->atomic_begin(exynos_crtc);
  60. }
  61. static void exynos_crtc_atomic_flush(struct drm_crtc *crtc,
  62. struct drm_crtc_state *old_crtc_state)
  63. {
  64. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  65. if (exynos_crtc->ops->atomic_flush)
  66. exynos_crtc->ops->atomic_flush(exynos_crtc);
  67. }
  68. static enum drm_mode_status exynos_crtc_mode_valid(struct drm_crtc *crtc,
  69. const struct drm_display_mode *mode)
  70. {
  71. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  72. if (exynos_crtc->ops->mode_valid)
  73. return exynos_crtc->ops->mode_valid(exynos_crtc, mode);
  74. return MODE_OK;
  75. }
  76. static const struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  77. .mode_valid = exynos_crtc_mode_valid,
  78. .atomic_check = exynos_crtc_atomic_check,
  79. .atomic_begin = exynos_crtc_atomic_begin,
  80. .atomic_flush = exynos_crtc_atomic_flush,
  81. .atomic_enable = exynos_drm_crtc_atomic_enable,
  82. .atomic_disable = exynos_drm_crtc_atomic_disable,
  83. };
  84. void exynos_crtc_handle_event(struct exynos_drm_crtc *exynos_crtc)
  85. {
  86. struct drm_crtc *crtc = &exynos_crtc->base;
  87. struct drm_pending_vblank_event *event = crtc->state->event;
  88. unsigned long flags;
  89. if (!event)
  90. return;
  91. crtc->state->event = NULL;
  92. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  93. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  94. drm_crtc_arm_vblank_event(crtc, event);
  95. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  96. }
  97. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  98. {
  99. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  100. drm_crtc_cleanup(crtc);
  101. kfree(exynos_crtc);
  102. }
  103. static int exynos_drm_crtc_enable_vblank(struct drm_crtc *crtc)
  104. {
  105. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  106. if (exynos_crtc->ops->enable_vblank)
  107. return exynos_crtc->ops->enable_vblank(exynos_crtc);
  108. return 0;
  109. }
  110. static void exynos_drm_crtc_disable_vblank(struct drm_crtc *crtc)
  111. {
  112. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  113. if (exynos_crtc->ops->disable_vblank)
  114. exynos_crtc->ops->disable_vblank(exynos_crtc);
  115. }
  116. static const struct drm_crtc_funcs exynos_crtc_funcs = {
  117. .set_config = drm_atomic_helper_set_config,
  118. .page_flip = drm_atomic_helper_page_flip,
  119. .destroy = exynos_drm_crtc_destroy,
  120. .reset = drm_atomic_helper_crtc_reset,
  121. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  122. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  123. .enable_vblank = exynos_drm_crtc_enable_vblank,
  124. .disable_vblank = exynos_drm_crtc_disable_vblank,
  125. };
  126. struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev,
  127. struct drm_plane *plane,
  128. enum exynos_drm_output_type type,
  129. const struct exynos_drm_crtc_ops *ops,
  130. void *ctx)
  131. {
  132. struct exynos_drm_crtc *exynos_crtc;
  133. struct drm_crtc *crtc;
  134. int ret;
  135. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  136. if (!exynos_crtc)
  137. return ERR_PTR(-ENOMEM);
  138. exynos_crtc->type = type;
  139. exynos_crtc->ops = ops;
  140. exynos_crtc->ctx = ctx;
  141. crtc = &exynos_crtc->base;
  142. ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL,
  143. &exynos_crtc_funcs, NULL);
  144. if (ret < 0)
  145. goto err_crtc;
  146. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  147. return exynos_crtc;
  148. err_crtc:
  149. plane->funcs->destroy(plane);
  150. kfree(exynos_crtc);
  151. return ERR_PTR(ret);
  152. }
  153. struct exynos_drm_crtc *exynos_drm_crtc_get_by_type(struct drm_device *drm_dev,
  154. enum exynos_drm_output_type out_type)
  155. {
  156. struct drm_crtc *crtc;
  157. drm_for_each_crtc(crtc, drm_dev)
  158. if (to_exynos_crtc(crtc)->type == out_type)
  159. return to_exynos_crtc(crtc);
  160. return ERR_PTR(-EPERM);
  161. }
  162. int exynos_drm_set_possible_crtcs(struct drm_encoder *encoder,
  163. enum exynos_drm_output_type out_type)
  164. {
  165. struct exynos_drm_crtc *crtc = exynos_drm_crtc_get_by_type(encoder->dev,
  166. out_type);
  167. if (IS_ERR(crtc))
  168. return PTR_ERR(crtc);
  169. encoder->possible_crtcs = drm_crtc_mask(&crtc->base);
  170. return 0;
  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. }