exynos_drm_plane.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. DRM_FORMAT_NV12MT,
  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. int exynos_check_plane(struct drm_plane *plane, struct drm_framebuffer *fb)
  59. {
  60. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  61. int nr;
  62. int i;
  63. nr = exynos_drm_fb_get_buf_cnt(fb);
  64. for (i = 0; i < nr; i++) {
  65. struct exynos_drm_gem_buf *buffer = exynos_drm_fb_buffer(fb, i);
  66. if (!buffer) {
  67. DRM_DEBUG_KMS("buffer is null\n");
  68. return -EFAULT;
  69. }
  70. exynos_plane->dma_addr[i] = buffer->dma_addr;
  71. DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n",
  72. i, (unsigned long)exynos_plane->dma_addr[i]);
  73. }
  74. return 0;
  75. }
  76. void exynos_plane_mode_set(struct drm_plane *plane, struct drm_crtc *crtc,
  77. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  78. unsigned int crtc_w, unsigned int crtc_h,
  79. uint32_t src_x, uint32_t src_y,
  80. uint32_t src_w, uint32_t src_h)
  81. {
  82. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  83. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  84. unsigned int actual_w;
  85. unsigned int actual_h;
  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. exynos_plane->fb_x = src_x;
  100. exynos_plane->fb_y = src_y;
  101. exynos_plane->fb_width = fb->width;
  102. exynos_plane->fb_height = fb->height;
  103. exynos_plane->src_width = src_w;
  104. exynos_plane->src_height = src_h;
  105. exynos_plane->bpp = fb->bits_per_pixel;
  106. exynos_plane->pitch = fb->pitches[0];
  107. exynos_plane->pixel_format = fb->pixel_format;
  108. /* set plane range to be displayed. */
  109. exynos_plane->crtc_x = crtc_x;
  110. exynos_plane->crtc_y = crtc_y;
  111. exynos_plane->crtc_width = actual_w;
  112. exynos_plane->crtc_height = actual_h;
  113. /* set drm mode data. */
  114. exynos_plane->mode_width = crtc->mode.hdisplay;
  115. exynos_plane->mode_height = crtc->mode.vdisplay;
  116. exynos_plane->refresh = crtc->mode.vrefresh;
  117. exynos_plane->scan_flag = crtc->mode.flags;
  118. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  119. exynos_plane->crtc_x, exynos_plane->crtc_y,
  120. exynos_plane->crtc_width, exynos_plane->crtc_height);
  121. plane->crtc = crtc;
  122. if (exynos_crtc->ops->win_mode_set)
  123. exynos_crtc->ops->win_mode_set(exynos_crtc, exynos_plane);
  124. }
  125. void exynos_plane_dpms(struct drm_plane *plane, int mode)
  126. {
  127. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  128. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(plane->crtc);
  129. if (mode == DRM_MODE_DPMS_ON) {
  130. if (exynos_plane->enabled)
  131. return;
  132. if (exynos_crtc->ops->win_enable)
  133. exynos_crtc->ops->win_enable(exynos_crtc,
  134. exynos_plane->zpos);
  135. exynos_plane->enabled = true;
  136. } else {
  137. if (!exynos_plane->enabled)
  138. return;
  139. if (exynos_crtc->ops->win_disable)
  140. exynos_crtc->ops->win_disable(exynos_crtc,
  141. exynos_plane->zpos);
  142. exynos_plane->enabled = false;
  143. }
  144. }
  145. int
  146. exynos_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  147. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  148. unsigned int crtc_w, unsigned int crtc_h,
  149. uint32_t src_x, uint32_t src_y,
  150. uint32_t src_w, uint32_t src_h)
  151. {
  152. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  153. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  154. int ret;
  155. ret = exynos_check_plane(plane, fb);
  156. if (ret < 0)
  157. return ret;
  158. exynos_plane_mode_set(plane, crtc, fb, crtc_x, crtc_y,
  159. crtc_w, crtc_h, src_x >> 16, src_y >> 16,
  160. src_w >> 16, src_h >> 16);
  161. if (exynos_crtc->ops->win_commit)
  162. exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos);
  163. return 0;
  164. }
  165. static int exynos_disable_plane(struct drm_plane *plane)
  166. {
  167. exynos_plane_dpms(plane, DRM_MODE_DPMS_OFF);
  168. return 0;
  169. }
  170. static void exynos_plane_destroy(struct drm_plane *plane)
  171. {
  172. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  173. exynos_disable_plane(plane);
  174. drm_plane_cleanup(plane);
  175. kfree(exynos_plane);
  176. }
  177. static int exynos_plane_set_property(struct drm_plane *plane,
  178. struct drm_property *property,
  179. uint64_t val)
  180. {
  181. struct drm_device *dev = plane->dev;
  182. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  183. struct exynos_drm_private *dev_priv = dev->dev_private;
  184. if (property == dev_priv->plane_zpos_property) {
  185. exynos_plane->zpos = val;
  186. return 0;
  187. }
  188. return -EINVAL;
  189. }
  190. static struct drm_plane_funcs exynos_plane_funcs = {
  191. .update_plane = exynos_update_plane,
  192. .disable_plane = exynos_disable_plane,
  193. .destroy = exynos_plane_destroy,
  194. .set_property = exynos_plane_set_property,
  195. };
  196. static void exynos_plane_attach_zpos_property(struct drm_plane *plane)
  197. {
  198. struct drm_device *dev = plane->dev;
  199. struct exynos_drm_private *dev_priv = dev->dev_private;
  200. struct drm_property *prop;
  201. prop = dev_priv->plane_zpos_property;
  202. if (!prop) {
  203. prop = drm_property_create_range(dev, 0, "zpos", 0,
  204. MAX_PLANE - 1);
  205. if (!prop)
  206. return;
  207. dev_priv->plane_zpos_property = prop;
  208. }
  209. drm_object_attach_property(&plane->base, prop, 0);
  210. }
  211. struct drm_plane *exynos_plane_init(struct drm_device *dev,
  212. unsigned long possible_crtcs,
  213. enum drm_plane_type type)
  214. {
  215. struct exynos_drm_plane *exynos_plane;
  216. int err;
  217. exynos_plane = kzalloc(sizeof(struct exynos_drm_plane), GFP_KERNEL);
  218. if (!exynos_plane)
  219. return ERR_PTR(-ENOMEM);
  220. err = drm_universal_plane_init(dev, &exynos_plane->base, possible_crtcs,
  221. &exynos_plane_funcs, formats,
  222. ARRAY_SIZE(formats), type);
  223. if (err) {
  224. DRM_ERROR("failed to initialize plane\n");
  225. kfree(exynos_plane);
  226. return ERR_PTR(err);
  227. }
  228. if (type == DRM_PLANE_TYPE_PRIMARY)
  229. exynos_plane->zpos = DEFAULT_ZPOS;
  230. else
  231. exynos_plane_attach_zpos_property(&exynos_plane->base);
  232. return &exynos_plane->base;
  233. }