exynos_drm_plane.c 6.1 KB

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