exynos_drm_plane.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "exynos_drm_drv.h"
  14. #include "exynos_drm_encoder.h"
  15. #include "exynos_drm_fb.h"
  16. #include "exynos_drm_gem.h"
  17. #define to_exynos_plane(x) container_of(x, struct exynos_plane, base)
  18. struct exynos_plane {
  19. struct drm_plane base;
  20. struct exynos_drm_overlay overlay;
  21. bool enabled;
  22. };
  23. static const uint32_t formats[] = {
  24. DRM_FORMAT_XRGB8888,
  25. DRM_FORMAT_ARGB8888,
  26. DRM_FORMAT_NV12,
  27. DRM_FORMAT_NV12MT,
  28. };
  29. /*
  30. * This function is to get X or Y size shown via screen. This needs length and
  31. * start position of CRTC.
  32. *
  33. * <--- length --->
  34. * CRTC ----------------
  35. * ^ start ^ end
  36. *
  37. * There are six cases from a to f.
  38. *
  39. * <----- SCREEN ----->
  40. * 0 last
  41. * ----------|------------------|----------
  42. * CRTCs
  43. * a -------
  44. * b -------
  45. * c --------------------------
  46. * d --------
  47. * e -------
  48. * f -------
  49. */
  50. static int exynos_plane_get_size(int start, unsigned length, unsigned last)
  51. {
  52. int end = start + length;
  53. int size = 0;
  54. if (start <= 0) {
  55. if (end > 0)
  56. size = min_t(unsigned, end, last);
  57. } else if (start <= last) {
  58. size = min_t(unsigned, last - start, length);
  59. }
  60. return size;
  61. }
  62. int exynos_plane_mode_set(struct drm_plane *plane, struct drm_crtc *crtc,
  63. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  64. unsigned int crtc_w, unsigned int crtc_h,
  65. uint32_t src_x, uint32_t src_y,
  66. uint32_t src_w, uint32_t src_h)
  67. {
  68. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  69. struct exynos_drm_overlay *overlay = &exynos_plane->overlay;
  70. unsigned int actual_w;
  71. unsigned int actual_h;
  72. int nr;
  73. int i;
  74. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  75. nr = exynos_drm_fb_get_buf_cnt(fb);
  76. for (i = 0; i < nr; i++) {
  77. struct exynos_drm_gem_buf *buffer = exynos_drm_fb_buffer(fb, i);
  78. if (!buffer) {
  79. DRM_LOG_KMS("buffer is null\n");
  80. return -EFAULT;
  81. }
  82. overlay->dma_addr[i] = buffer->dma_addr;
  83. DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
  84. i, (unsigned long)overlay->dma_addr[i]);
  85. }
  86. actual_w = exynos_plane_get_size(crtc_x, crtc_w, crtc->mode.hdisplay);
  87. actual_h = exynos_plane_get_size(crtc_y, crtc_h, crtc->mode.vdisplay);
  88. if (crtc_x < 0) {
  89. if (actual_w)
  90. src_x -= crtc_x;
  91. crtc_x = 0;
  92. }
  93. if (crtc_y < 0) {
  94. if (actual_h)
  95. src_y -= crtc_y;
  96. crtc_y = 0;
  97. }
  98. /* set drm framebuffer data. */
  99. overlay->fb_x = src_x;
  100. overlay->fb_y = src_y;
  101. overlay->fb_width = fb->width;
  102. overlay->fb_height = fb->height;
  103. overlay->src_width = src_w;
  104. overlay->src_height = src_h;
  105. overlay->bpp = fb->bits_per_pixel;
  106. overlay->pitch = fb->pitches[0];
  107. overlay->pixel_format = fb->pixel_format;
  108. /* set overlay range to be displayed. */
  109. overlay->crtc_x = crtc_x;
  110. overlay->crtc_y = crtc_y;
  111. overlay->crtc_width = actual_w;
  112. overlay->crtc_height = actual_h;
  113. /* set drm mode data. */
  114. overlay->mode_width = crtc->mode.hdisplay;
  115. overlay->mode_height = crtc->mode.vdisplay;
  116. overlay->refresh = crtc->mode.vrefresh;
  117. overlay->scan_flag = crtc->mode.flags;
  118. DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
  119. overlay->crtc_x, overlay->crtc_y,
  120. overlay->crtc_width, overlay->crtc_height);
  121. exynos_drm_fn_encoder(crtc, overlay, exynos_drm_encoder_plane_mode_set);
  122. return 0;
  123. }
  124. void exynos_plane_commit(struct drm_plane *plane)
  125. {
  126. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  127. struct exynos_drm_overlay *overlay = &exynos_plane->overlay;
  128. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  129. exynos_drm_encoder_plane_commit);
  130. }
  131. void exynos_plane_dpms(struct drm_plane *plane, int mode)
  132. {
  133. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  134. struct exynos_drm_overlay *overlay = &exynos_plane->overlay;
  135. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  136. if (mode == DRM_MODE_DPMS_ON) {
  137. if (exynos_plane->enabled)
  138. return;
  139. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  140. exynos_drm_encoder_plane_enable);
  141. exynos_plane->enabled = true;
  142. } else {
  143. if (!exynos_plane->enabled)
  144. return;
  145. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  146. exynos_drm_encoder_plane_disable);
  147. exynos_plane->enabled = false;
  148. }
  149. }
  150. static int
  151. exynos_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  152. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  153. unsigned int crtc_w, unsigned int crtc_h,
  154. uint32_t src_x, uint32_t src_y,
  155. uint32_t src_w, uint32_t src_h)
  156. {
  157. int ret;
  158. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  159. ret = exynos_plane_mode_set(plane, crtc, fb, crtc_x, crtc_y,
  160. crtc_w, crtc_h, src_x >> 16, src_y >> 16,
  161. src_w >> 16, src_h >> 16);
  162. if (ret < 0)
  163. return ret;
  164. plane->crtc = crtc;
  165. exynos_plane_commit(plane);
  166. exynos_plane_dpms(plane, DRM_MODE_DPMS_ON);
  167. return 0;
  168. }
  169. static int exynos_disable_plane(struct drm_plane *plane)
  170. {
  171. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  172. exynos_plane_dpms(plane, DRM_MODE_DPMS_OFF);
  173. return 0;
  174. }
  175. static void exynos_plane_destroy(struct drm_plane *plane)
  176. {
  177. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  178. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  179. exynos_disable_plane(plane);
  180. drm_plane_cleanup(plane);
  181. kfree(exynos_plane);
  182. }
  183. static int exynos_plane_set_property(struct drm_plane *plane,
  184. struct drm_property *property,
  185. uint64_t val)
  186. {
  187. struct drm_device *dev = plane->dev;
  188. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  189. struct exynos_drm_private *dev_priv = dev->dev_private;
  190. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  191. if (property == dev_priv->plane_zpos_property) {
  192. exynos_plane->overlay.zpos = val;
  193. return 0;
  194. }
  195. return -EINVAL;
  196. }
  197. static struct drm_plane_funcs exynos_plane_funcs = {
  198. .update_plane = exynos_update_plane,
  199. .disable_plane = exynos_disable_plane,
  200. .destroy = exynos_plane_destroy,
  201. .set_property = exynos_plane_set_property,
  202. };
  203. static void exynos_plane_attach_zpos_property(struct drm_plane *plane)
  204. {
  205. struct drm_device *dev = plane->dev;
  206. struct exynos_drm_private *dev_priv = dev->dev_private;
  207. struct drm_property *prop;
  208. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  209. prop = dev_priv->plane_zpos_property;
  210. if (!prop) {
  211. prop = drm_property_create_range(dev, 0, "zpos", 0,
  212. MAX_PLANE - 1);
  213. if (!prop)
  214. return;
  215. dev_priv->plane_zpos_property = prop;
  216. }
  217. drm_object_attach_property(&plane->base, prop, 0);
  218. }
  219. struct drm_plane *exynos_plane_init(struct drm_device *dev,
  220. unsigned int possible_crtcs, bool priv)
  221. {
  222. struct exynos_plane *exynos_plane;
  223. int err;
  224. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  225. exynos_plane = kzalloc(sizeof(struct exynos_plane), GFP_KERNEL);
  226. if (!exynos_plane) {
  227. DRM_ERROR("failed to allocate plane\n");
  228. return NULL;
  229. }
  230. err = drm_plane_init(dev, &exynos_plane->base, possible_crtcs,
  231. &exynos_plane_funcs, formats, ARRAY_SIZE(formats),
  232. priv);
  233. if (err) {
  234. DRM_ERROR("failed to initialize plane\n");
  235. kfree(exynos_plane);
  236. return NULL;
  237. }
  238. if (priv)
  239. exynos_plane->overlay.zpos = DEFAULT_ZPOS;
  240. else
  241. exynos_plane_attach_zpos_property(&exynos_plane->base);
  242. return &exynos_plane->base;
  243. }