exynos_drm_plane.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_width = (actual_w * exynos_plane->h_ratio) >> 16;
  89. exynos_plane->src_height = (actual_h * exynos_plane->v_ratio) >> 16;
  90. exynos_plane->fb_width = fb->width;
  91. exynos_plane->fb_height = fb->height;
  92. exynos_plane->bpp = fb->bits_per_pixel;
  93. exynos_plane->pitch = fb->pitches[0];
  94. exynos_plane->pixel_format = fb->pixel_format;
  95. /* set plane range to be displayed. */
  96. exynos_plane->crtc_x = crtc_x;
  97. exynos_plane->crtc_y = crtc_y;
  98. exynos_plane->crtc_width = actual_w;
  99. exynos_plane->crtc_height = actual_h;
  100. /* set drm mode data. */
  101. exynos_plane->mode_width = mode->hdisplay;
  102. exynos_plane->mode_height = mode->vdisplay;
  103. exynos_plane->refresh = mode->vrefresh;
  104. exynos_plane->scan_flag = mode->flags;
  105. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  106. exynos_plane->crtc_x, exynos_plane->crtc_y,
  107. exynos_plane->crtc_width, exynos_plane->crtc_height);
  108. plane->crtc = crtc;
  109. }
  110. static struct drm_plane_funcs exynos_plane_funcs = {
  111. .update_plane = drm_atomic_helper_update_plane,
  112. .disable_plane = drm_atomic_helper_disable_plane,
  113. .destroy = drm_plane_cleanup,
  114. .reset = drm_atomic_helper_plane_reset,
  115. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  116. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  117. };
  118. static int exynos_plane_atomic_check(struct drm_plane *plane,
  119. struct drm_plane_state *state)
  120. {
  121. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  122. int nr;
  123. int i;
  124. if (!state->fb)
  125. return 0;
  126. nr = exynos_drm_fb_get_buf_cnt(state->fb);
  127. for (i = 0; i < nr; i++) {
  128. struct exynos_drm_gem_buf *buffer =
  129. exynos_drm_fb_buffer(state->fb, i);
  130. if (!buffer) {
  131. DRM_DEBUG_KMS("buffer is null\n");
  132. return -EFAULT;
  133. }
  134. exynos_plane->dma_addr[i] = buffer->dma_addr +
  135. state->fb->offsets[i];
  136. DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
  137. i, (unsigned long)exynos_plane->dma_addr[i]);
  138. }
  139. return 0;
  140. }
  141. static void exynos_plane_atomic_update(struct drm_plane *plane,
  142. struct drm_plane_state *old_state)
  143. {
  144. struct drm_plane_state *state = plane->state;
  145. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  146. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  147. if (!state->crtc)
  148. return;
  149. exynos_plane_mode_set(plane, state->crtc, state->fb,
  150. state->crtc_x, state->crtc_y,
  151. state->crtc_w, state->crtc_h,
  152. state->src_x >> 16, state->src_y >> 16,
  153. state->src_w >> 16, state->src_h >> 16);
  154. if (exynos_crtc->ops->win_commit)
  155. exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
  156. }
  157. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  158. struct drm_plane_state *old_state)
  159. {
  160. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  161. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  162. if (!old_state->crtc)
  163. return;
  164. if (exynos_crtc->ops->win_disable)
  165. exynos_crtc->ops->win_disable(exynos_crtc,
  166. exynos_plane->zpos);
  167. }
  168. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  169. .atomic_check = exynos_plane_atomic_check,
  170. .atomic_update = exynos_plane_atomic_update,
  171. .atomic_disable = exynos_plane_atomic_disable,
  172. };
  173. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  174. unsigned int zpos)
  175. {
  176. struct drm_device *dev = plane->dev;
  177. struct exynos_drm_private *dev_priv = dev->dev_private;
  178. struct drm_property *prop;
  179. prop = dev_priv->plane_zpos_property;
  180. if (!prop) {
  181. prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
  182. "zpos", 0, MAX_PLANE - 1);
  183. if (!prop)
  184. return;
  185. dev_priv->plane_zpos_property = prop;
  186. }
  187. drm_object_attach_property(&plane->base, prop, zpos);
  188. }
  189. int exynos_plane_init(struct drm_device *dev,
  190. struct exynos_drm_plane *exynos_plane,
  191. unsigned long possible_crtcs, enum drm_plane_type type,
  192. unsigned int zpos)
  193. {
  194. int err;
  195. err = drm_universal_plane_init(dev, &exynos_plane->base, possible_crtcs,
  196. &exynos_plane_funcs, formats,
  197. ARRAY_SIZE(formats), type);
  198. if (err) {
  199. DRM_ERROR("failed to initialize plane\n");
  200. return err;
  201. }
  202. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  203. exynos_plane->zpos = zpos;
  204. if (type == DRM_PLANE_TYPE_OVERLAY)
  205. exynos_plane_attach_zpos_property(&exynos_plane->base, zpos);
  206. return 0;
  207. }