exynos_drm_plane.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 b.
  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. overlay->vaddr[i] = buffer->kvaddr;
  84. DRM_DEBUG_KMS("buffer: %d, vaddr = 0x%lx, dma_addr = 0x%lx\n",
  85. i, (unsigned long)overlay->vaddr[i],
  86. (unsigned long)overlay->dma_addr[i]);
  87. }
  88. actual_w = exynos_plane_get_size(crtc_x, crtc_w, crtc->mode.hdisplay);
  89. actual_h = exynos_plane_get_size(crtc_y, crtc_h, crtc->mode.vdisplay);
  90. if (crtc_x < 0) {
  91. if (actual_w)
  92. src_x -= crtc_x;
  93. else
  94. src_x += crtc_w;
  95. crtc_x = 0;
  96. }
  97. if (crtc_y < 0) {
  98. if (actual_h)
  99. src_y -= crtc_y;
  100. else
  101. src_y += crtc_h;
  102. crtc_y = 0;
  103. }
  104. /* set drm framebuffer data. */
  105. overlay->fb_x = src_x;
  106. overlay->fb_y = src_y;
  107. overlay->fb_width = fb->width;
  108. overlay->fb_height = fb->height;
  109. overlay->src_width = src_w;
  110. overlay->src_height = src_h;
  111. overlay->bpp = fb->bits_per_pixel;
  112. overlay->pitch = fb->pitches[0];
  113. overlay->pixel_format = fb->pixel_format;
  114. /* set overlay range to be displayed. */
  115. overlay->crtc_x = crtc_x;
  116. overlay->crtc_y = crtc_y;
  117. overlay->crtc_width = actual_w;
  118. overlay->crtc_height = actual_h;
  119. /* set drm mode data. */
  120. overlay->mode_width = crtc->mode.hdisplay;
  121. overlay->mode_height = crtc->mode.vdisplay;
  122. overlay->refresh = crtc->mode.vrefresh;
  123. overlay->scan_flag = crtc->mode.flags;
  124. DRM_DEBUG_KMS("overlay : offset_x/y(%d,%d), width/height(%d,%d)",
  125. overlay->crtc_x, overlay->crtc_y,
  126. overlay->crtc_width, overlay->crtc_height);
  127. exynos_drm_fn_encoder(crtc, overlay, exynos_drm_encoder_plane_mode_set);
  128. return 0;
  129. }
  130. void exynos_plane_commit(struct drm_plane *plane)
  131. {
  132. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  133. struct exynos_drm_overlay *overlay = &exynos_plane->overlay;
  134. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  135. exynos_drm_encoder_plane_commit);
  136. }
  137. void exynos_plane_dpms(struct drm_plane *plane, int mode)
  138. {
  139. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  140. struct exynos_drm_overlay *overlay = &exynos_plane->overlay;
  141. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  142. if (mode == DRM_MODE_DPMS_ON) {
  143. if (exynos_plane->enabled)
  144. return;
  145. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  146. exynos_drm_encoder_plane_enable);
  147. exynos_plane->enabled = true;
  148. } else {
  149. if (!exynos_plane->enabled)
  150. return;
  151. exynos_drm_fn_encoder(plane->crtc, &overlay->zpos,
  152. exynos_drm_encoder_plane_disable);
  153. exynos_plane->enabled = false;
  154. }
  155. }
  156. static int
  157. exynos_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  158. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  159. unsigned int crtc_w, unsigned int crtc_h,
  160. uint32_t src_x, uint32_t src_y,
  161. uint32_t src_w, uint32_t src_h)
  162. {
  163. int ret;
  164. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  165. ret = exynos_plane_mode_set(plane, crtc, fb, crtc_x, crtc_y,
  166. crtc_w, crtc_h, src_x >> 16, src_y >> 16,
  167. src_w >> 16, src_h >> 16);
  168. if (ret < 0)
  169. return ret;
  170. plane->crtc = crtc;
  171. plane->fb = crtc->fb;
  172. exynos_plane_commit(plane);
  173. exynos_plane_dpms(plane, DRM_MODE_DPMS_ON);
  174. return 0;
  175. }
  176. static int exynos_disable_plane(struct drm_plane *plane)
  177. {
  178. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  179. exynos_plane_dpms(plane, DRM_MODE_DPMS_OFF);
  180. return 0;
  181. }
  182. static void exynos_plane_destroy(struct drm_plane *plane)
  183. {
  184. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  185. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  186. exynos_disable_plane(plane);
  187. drm_plane_cleanup(plane);
  188. kfree(exynos_plane);
  189. }
  190. static int exynos_plane_set_property(struct drm_plane *plane,
  191. struct drm_property *property,
  192. uint64_t val)
  193. {
  194. struct drm_device *dev = plane->dev;
  195. struct exynos_plane *exynos_plane = to_exynos_plane(plane);
  196. struct exynos_drm_private *dev_priv = dev->dev_private;
  197. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  198. if (property == dev_priv->plane_zpos_property) {
  199. exynos_plane->overlay.zpos = val;
  200. return 0;
  201. }
  202. return -EINVAL;
  203. }
  204. static struct drm_plane_funcs exynos_plane_funcs = {
  205. .update_plane = exynos_update_plane,
  206. .disable_plane = exynos_disable_plane,
  207. .destroy = exynos_plane_destroy,
  208. .set_property = exynos_plane_set_property,
  209. };
  210. static void exynos_plane_attach_zpos_property(struct drm_plane *plane)
  211. {
  212. struct drm_device *dev = plane->dev;
  213. struct exynos_drm_private *dev_priv = dev->dev_private;
  214. struct drm_property *prop;
  215. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  216. prop = dev_priv->plane_zpos_property;
  217. if (!prop) {
  218. prop = drm_property_create_range(dev, 0, "zpos", 0,
  219. MAX_PLANE - 1);
  220. if (!prop)
  221. return;
  222. dev_priv->plane_zpos_property = prop;
  223. }
  224. drm_object_attach_property(&plane->base, prop, 0);
  225. }
  226. struct drm_plane *exynos_plane_init(struct drm_device *dev,
  227. unsigned int possible_crtcs, bool priv)
  228. {
  229. struct exynos_plane *exynos_plane;
  230. int err;
  231. DRM_DEBUG_KMS("[%d] %s\n", __LINE__, __func__);
  232. exynos_plane = kzalloc(sizeof(struct exynos_plane), GFP_KERNEL);
  233. if (!exynos_plane) {
  234. DRM_ERROR("failed to allocate plane\n");
  235. return NULL;
  236. }
  237. err = drm_plane_init(dev, &exynos_plane->base, possible_crtcs,
  238. &exynos_plane_funcs, formats, ARRAY_SIZE(formats),
  239. priv);
  240. if (err) {
  241. DRM_ERROR("failed to initialize plane\n");
  242. kfree(exynos_plane);
  243. return NULL;
  244. }
  245. if (priv)
  246. exynos_plane->overlay.zpos = DEFAULT_ZPOS;
  247. else
  248. exynos_plane_attach_zpos_property(&exynos_plane->base);
  249. return &exynos_plane->base;
  250. }