exynos_drm_plane.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. static const uint32_t formats[] = {
  21. DRM_FORMAT_XRGB8888,
  22. DRM_FORMAT_ARGB8888,
  23. DRM_FORMAT_NV12,
  24. };
  25. /*
  26. * This function is to get X or Y size shown via screen. This needs length and
  27. * start position of CRTC.
  28. *
  29. * <--- length --->
  30. * CRTC ----------------
  31. * ^ start ^ end
  32. *
  33. * There are six cases from a to f.
  34. *
  35. * <----- SCREEN ----->
  36. * 0 last
  37. * ----------|------------------|----------
  38. * CRTCs
  39. * a -------
  40. * b -------
  41. * c --------------------------
  42. * d --------
  43. * e -------
  44. * f -------
  45. */
  46. static int exynos_plane_get_size(int start, unsigned length, unsigned last)
  47. {
  48. int end = start + length;
  49. int size = 0;
  50. if (start <= 0) {
  51. if (end > 0)
  52. size = min_t(unsigned, end, last);
  53. } else if (start <= last) {
  54. size = min_t(unsigned, last - start, length);
  55. }
  56. return size;
  57. }
  58. static void exynos_plane_mode_set(struct drm_plane *plane,
  59. struct drm_crtc *crtc,
  60. struct drm_framebuffer *fb,
  61. int crtc_x, int crtc_y,
  62. unsigned int crtc_w, unsigned int crtc_h,
  63. uint32_t src_x, uint32_t src_y,
  64. uint32_t src_w, uint32_t src_h)
  65. {
  66. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  67. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  68. unsigned int actual_w;
  69. unsigned int actual_h;
  70. actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
  71. actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
  72. if (crtc_x < 0) {
  73. if (actual_w)
  74. src_x -= crtc_x;
  75. crtc_x = 0;
  76. }
  77. if (crtc_y < 0) {
  78. if (actual_h)
  79. src_y -= crtc_y;
  80. crtc_y = 0;
  81. }
  82. /* set ratio */
  83. exynos_plane->h_ratio = (src_w << 16) / crtc_w;
  84. exynos_plane->v_ratio = (src_h << 16) / crtc_h;
  85. /* set drm framebuffer data. */
  86. exynos_plane->src_x = src_x;
  87. exynos_plane->src_y = src_y;
  88. exynos_plane->src_w = (actual_w * exynos_plane->h_ratio) >> 16;
  89. exynos_plane->src_h = (actual_h * exynos_plane->v_ratio) >> 16;
  90. /* set plane range to be displayed. */
  91. exynos_plane->crtc_x = crtc_x;
  92. exynos_plane->crtc_y = crtc_y;
  93. exynos_plane->crtc_w = actual_w;
  94. exynos_plane->crtc_h = actual_h;
  95. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  96. exynos_plane->crtc_x, exynos_plane->crtc_y,
  97. exynos_plane->crtc_w, exynos_plane->crtc_h);
  98. plane->crtc = crtc;
  99. }
  100. static struct drm_plane_funcs exynos_plane_funcs = {
  101. .update_plane = drm_atomic_helper_update_plane,
  102. .disable_plane = drm_atomic_helper_disable_plane,
  103. .destroy = drm_plane_cleanup,
  104. .reset = drm_atomic_helper_plane_reset,
  105. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  106. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  107. };
  108. static int exynos_plane_atomic_check(struct drm_plane *plane,
  109. struct drm_plane_state *state)
  110. {
  111. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  112. int nr;
  113. int i;
  114. if (!state->fb)
  115. return 0;
  116. nr = exynos_drm_fb_get_buf_cnt(state->fb);
  117. for (i = 0; i < nr; i++) {
  118. struct exynos_drm_gem_obj *obj =
  119. exynos_drm_fb_gem_obj(state->fb, i);
  120. if (!obj) {
  121. DRM_DEBUG_KMS("gem object is null\n");
  122. return -EFAULT;
  123. }
  124. exynos_plane->dma_addr[i] = obj->dma_addr +
  125. state->fb->offsets[i];
  126. DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
  127. i, (unsigned long)exynos_plane->dma_addr[i]);
  128. }
  129. return 0;
  130. }
  131. static void exynos_plane_atomic_update(struct drm_plane *plane,
  132. struct drm_plane_state *old_state)
  133. {
  134. struct drm_plane_state *state = plane->state;
  135. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  136. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  137. if (!state->crtc)
  138. return;
  139. exynos_plane_mode_set(plane, state->crtc, state->fb,
  140. state->crtc_x, state->crtc_y,
  141. state->crtc_w, state->crtc_h,
  142. state->src_x >> 16, state->src_y >> 16,
  143. state->src_w >> 16, state->src_h >> 16);
  144. if (exynos_crtc->ops->update_plane)
  145. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  146. }
  147. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  148. struct drm_plane_state *old_state)
  149. {
  150. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  151. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  152. if (!old_state->crtc)
  153. return;
  154. if (exynos_crtc->ops->disable_plane)
  155. exynos_crtc->ops->disable_plane(exynos_crtc,
  156. exynos_plane);
  157. }
  158. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  159. .atomic_check = exynos_plane_atomic_check,
  160. .atomic_update = exynos_plane_atomic_update,
  161. .atomic_disable = exynos_plane_atomic_disable,
  162. };
  163. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  164. unsigned int zpos)
  165. {
  166. struct drm_device *dev = plane->dev;
  167. struct exynos_drm_private *dev_priv = dev->dev_private;
  168. struct drm_property *prop;
  169. prop = dev_priv->plane_zpos_property;
  170. if (!prop) {
  171. prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
  172. "zpos", 0, MAX_PLANE - 1);
  173. if (!prop)
  174. return;
  175. dev_priv->plane_zpos_property = prop;
  176. }
  177. drm_object_attach_property(&plane->base, prop, zpos);
  178. }
  179. int exynos_plane_init(struct drm_device *dev,
  180. struct exynos_drm_plane *exynos_plane,
  181. unsigned long possible_crtcs, enum drm_plane_type type,
  182. unsigned int zpos)
  183. {
  184. int err;
  185. err = drm_universal_plane_init(dev, &exynos_plane->base, possible_crtcs,
  186. &exynos_plane_funcs, formats,
  187. ARRAY_SIZE(formats), type);
  188. if (err) {
  189. DRM_ERROR("failed to initialize plane\n");
  190. return err;
  191. }
  192. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  193. exynos_plane->zpos = zpos;
  194. if (type == DRM_PLANE_TYPE_OVERLAY)
  195. exynos_plane_attach_zpos_property(&exynos_plane->base, zpos);
  196. return 0;
  197. }