exynos_drm_plane.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  3. * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/exynos_drm.h>
  13. #include <drm/drm_plane_helper.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include "exynos_drm_drv.h"
  16. #include "exynos_drm_crtc.h"
  17. #include "exynos_drm_fb.h"
  18. #include "exynos_drm_gem.h"
  19. #include "exynos_drm_plane.h"
  20. /*
  21. * This function is to get X or Y size shown via screen. This needs length and
  22. * start position of CRTC.
  23. *
  24. * <--- length --->
  25. * CRTC ----------------
  26. * ^ start ^ end
  27. *
  28. * There are six cases from a to f.
  29. *
  30. * <----- SCREEN ----->
  31. * 0 last
  32. * ----------|------------------|----------
  33. * CRTCs
  34. * a -------
  35. * b -------
  36. * c --------------------------
  37. * d --------
  38. * e -------
  39. * f -------
  40. */
  41. static int exynos_plane_get_size(int start, unsigned length, unsigned last)
  42. {
  43. int end = start + length;
  44. int size = 0;
  45. if (start <= 0) {
  46. if (end > 0)
  47. size = min_t(unsigned, end, last);
  48. } else if (start <= last) {
  49. size = min_t(unsigned, last - start, length);
  50. }
  51. return size;
  52. }
  53. static void exynos_plane_mode_set(struct drm_plane *plane,
  54. struct drm_crtc *crtc,
  55. struct drm_framebuffer *fb,
  56. int crtc_x, int crtc_y,
  57. unsigned int crtc_w, unsigned int crtc_h,
  58. uint32_t src_x, uint32_t src_y,
  59. uint32_t src_w, uint32_t src_h)
  60. {
  61. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  62. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  63. unsigned int actual_w;
  64. unsigned int actual_h;
  65. actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
  66. actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
  67. if (crtc_x < 0) {
  68. if (actual_w)
  69. src_x -= crtc_x;
  70. crtc_x = 0;
  71. }
  72. if (crtc_y < 0) {
  73. if (actual_h)
  74. src_y -= crtc_y;
  75. crtc_y = 0;
  76. }
  77. /* set ratio */
  78. exynos_plane->h_ratio = (src_w << 16) / crtc_w;
  79. exynos_plane->v_ratio = (src_h << 16) / crtc_h;
  80. /* set drm framebuffer data. */
  81. exynos_plane->src_x = src_x;
  82. exynos_plane->src_y = src_y;
  83. exynos_plane->src_w = (actual_w * exynos_plane->h_ratio) >> 16;
  84. exynos_plane->src_h = (actual_h * exynos_plane->v_ratio) >> 16;
  85. /* set plane range to be displayed. */
  86. exynos_plane->crtc_x = crtc_x;
  87. exynos_plane->crtc_y = crtc_y;
  88. exynos_plane->crtc_w = actual_w;
  89. exynos_plane->crtc_h = actual_h;
  90. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  91. exynos_plane->crtc_x, exynos_plane->crtc_y,
  92. exynos_plane->crtc_w, exynos_plane->crtc_h);
  93. plane->crtc = crtc;
  94. }
  95. static struct drm_plane_funcs exynos_plane_funcs = {
  96. .update_plane = drm_atomic_helper_update_plane,
  97. .disable_plane = drm_atomic_helper_disable_plane,
  98. .destroy = drm_plane_cleanup,
  99. .reset = drm_atomic_helper_plane_reset,
  100. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  101. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  102. };
  103. static int exynos_plane_atomic_check(struct drm_plane *plane,
  104. struct drm_plane_state *state)
  105. {
  106. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  107. int nr;
  108. int i;
  109. if (!state->fb)
  110. return 0;
  111. nr = drm_format_num_planes(state->fb->pixel_format);
  112. for (i = 0; i < nr; i++) {
  113. struct exynos_drm_gem *exynos_gem =
  114. exynos_drm_fb_gem(state->fb, i);
  115. if (!exynos_gem) {
  116. DRM_DEBUG_KMS("gem object is null\n");
  117. return -EFAULT;
  118. }
  119. exynos_plane->dma_addr[i] = exynos_gem->dma_addr +
  120. state->fb->offsets[i];
  121. DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
  122. i, (unsigned long)exynos_plane->dma_addr[i]);
  123. }
  124. return 0;
  125. }
  126. static void exynos_plane_atomic_update(struct drm_plane *plane,
  127. struct drm_plane_state *old_state)
  128. {
  129. struct drm_plane_state *state = plane->state;
  130. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  131. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  132. if (!state->crtc)
  133. return;
  134. exynos_plane_mode_set(plane, state->crtc, state->fb,
  135. state->crtc_x, state->crtc_y,
  136. state->crtc_w, state->crtc_h,
  137. state->src_x >> 16, state->src_y >> 16,
  138. state->src_w >> 16, state->src_h >> 16);
  139. exynos_plane->pending_fb = state->fb;
  140. if (exynos_crtc->ops->update_plane)
  141. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  142. }
  143. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  144. struct drm_plane_state *old_state)
  145. {
  146. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  147. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  148. if (!old_state->crtc)
  149. return;
  150. if (exynos_crtc->ops->disable_plane)
  151. exynos_crtc->ops->disable_plane(exynos_crtc,
  152. exynos_plane);
  153. }
  154. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  155. .atomic_check = exynos_plane_atomic_check,
  156. .atomic_update = exynos_plane_atomic_update,
  157. .atomic_disable = exynos_plane_atomic_disable,
  158. };
  159. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  160. unsigned int zpos)
  161. {
  162. struct drm_device *dev = plane->dev;
  163. struct exynos_drm_private *dev_priv = dev->dev_private;
  164. struct drm_property *prop;
  165. prop = dev_priv->plane_zpos_property;
  166. if (!prop) {
  167. prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
  168. "zpos", 0, MAX_PLANE - 1);
  169. if (!prop)
  170. return;
  171. dev_priv->plane_zpos_property = prop;
  172. }
  173. drm_object_attach_property(&plane->base, prop, zpos);
  174. }
  175. enum drm_plane_type exynos_plane_get_type(unsigned int zpos,
  176. unsigned int cursor_win)
  177. {
  178. if (zpos == DEFAULT_WIN)
  179. return DRM_PLANE_TYPE_PRIMARY;
  180. else if (zpos == cursor_win)
  181. return DRM_PLANE_TYPE_CURSOR;
  182. else
  183. return DRM_PLANE_TYPE_OVERLAY;
  184. }
  185. int exynos_plane_init(struct drm_device *dev,
  186. struct exynos_drm_plane *exynos_plane,
  187. unsigned long possible_crtcs, enum drm_plane_type type,
  188. const uint32_t *formats, unsigned int fcount,
  189. unsigned int zpos)
  190. {
  191. int err;
  192. err = drm_universal_plane_init(dev, &exynos_plane->base, possible_crtcs,
  193. &exynos_plane_funcs, formats, fcount,
  194. type);
  195. if (err) {
  196. DRM_ERROR("failed to initialize plane\n");
  197. return err;
  198. }
  199. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  200. exynos_plane->zpos = zpos;
  201. if (type == DRM_PLANE_TYPE_OVERLAY)
  202. exynos_plane_attach_zpos_property(&exynos_plane->base, zpos);
  203. return 0;
  204. }