exynos_drm_plane.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /*
  21. * This function is to get X or Y size shown via screen. This needs length and
  22. * start position of CRTC.
  23. *
  24. * <--- length --->
  25. * CRTC ----------------
  26. * ^ start ^ end
  27. *
  28. * There are six cases from a to f.
  29. *
  30. * <----- SCREEN ----->
  31. * 0 last
  32. * ----------|------------------|----------
  33. * CRTCs
  34. * a -------
  35. * b -------
  36. * c --------------------------
  37. * d --------
  38. * e -------
  39. * f -------
  40. */
  41. static int exynos_plane_get_size(int start, unsigned length, unsigned last)
  42. {
  43. int end = start + length;
  44. int size = 0;
  45. if (start <= 0) {
  46. if (end > 0)
  47. size = min_t(unsigned, end, last);
  48. } else if (start <= last) {
  49. size = min_t(unsigned, last - start, length);
  50. }
  51. return size;
  52. }
  53. static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
  54. {
  55. struct drm_plane_state *state = &exynos_state->base;
  56. struct drm_crtc *crtc = exynos_state->base.crtc;
  57. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  58. int crtc_x, crtc_y;
  59. unsigned int crtc_w, crtc_h;
  60. unsigned int src_x, src_y;
  61. unsigned int src_w, src_h;
  62. unsigned int actual_w;
  63. unsigned int actual_h;
  64. /*
  65. * The original src/dest coordinates are stored in exynos_state->base,
  66. * but we want to keep another copy internal to our driver that we can
  67. * clip/modify ourselves.
  68. */
  69. crtc_x = state->crtc_x;
  70. crtc_y = state->crtc_y;
  71. crtc_w = state->crtc_w;
  72. crtc_h = state->crtc_h;
  73. src_x = state->src_x >> 16;
  74. src_y = state->src_y >> 16;
  75. src_w = state->src_w >> 16;
  76. src_h = state->src_h >> 16;
  77. actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
  78. actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
  79. if (crtc_x < 0) {
  80. if (actual_w)
  81. src_x -= crtc_x;
  82. crtc_x = 0;
  83. }
  84. if (crtc_y < 0) {
  85. if (actual_h)
  86. src_y -= crtc_y;
  87. crtc_y = 0;
  88. }
  89. /* set ratio */
  90. exynos_state->h_ratio = (src_w << 16) / crtc_w;
  91. exynos_state->v_ratio = (src_h << 16) / crtc_h;
  92. /* set drm framebuffer data. */
  93. exynos_state->src.x = src_x;
  94. exynos_state->src.y = src_y;
  95. exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
  96. exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
  97. /* set plane range to be displayed. */
  98. exynos_state->crtc.x = crtc_x;
  99. exynos_state->crtc.y = crtc_y;
  100. exynos_state->crtc.w = actual_w;
  101. exynos_state->crtc.h = actual_h;
  102. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  103. exynos_state->crtc.x, exynos_state->crtc.y,
  104. exynos_state->crtc.w, exynos_state->crtc.h);
  105. }
  106. static void exynos_drm_plane_reset(struct drm_plane *plane)
  107. {
  108. struct exynos_drm_plane_state *exynos_state;
  109. if (plane->state) {
  110. exynos_state = to_exynos_plane_state(plane->state);
  111. if (exynos_state->base.fb)
  112. drm_framebuffer_unreference(exynos_state->base.fb);
  113. kfree(exynos_state);
  114. plane->state = NULL;
  115. }
  116. exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  117. if (exynos_state) {
  118. plane->state = &exynos_state->base;
  119. plane->state->plane = plane;
  120. }
  121. }
  122. static struct drm_plane_state *
  123. exynos_drm_plane_duplicate_state(struct drm_plane *plane)
  124. {
  125. struct exynos_drm_plane_state *exynos_state;
  126. struct exynos_drm_plane_state *copy;
  127. exynos_state = to_exynos_plane_state(plane->state);
  128. copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  129. if (!copy)
  130. return NULL;
  131. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  132. return &copy->base;
  133. }
  134. static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
  135. struct drm_plane_state *old_state)
  136. {
  137. struct exynos_drm_plane_state *old_exynos_state =
  138. to_exynos_plane_state(old_state);
  139. __drm_atomic_helper_plane_destroy_state(plane, old_state);
  140. kfree(old_exynos_state);
  141. }
  142. static struct drm_plane_funcs exynos_plane_funcs = {
  143. .update_plane = drm_atomic_helper_update_plane,
  144. .disable_plane = drm_atomic_helper_disable_plane,
  145. .destroy = drm_plane_cleanup,
  146. .reset = exynos_drm_plane_reset,
  147. .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
  148. .atomic_destroy_state = exynos_drm_plane_destroy_state,
  149. };
  150. static int
  151. exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
  152. struct exynos_drm_plane_state *state)
  153. {
  154. bool width_ok = false, height_ok = false;
  155. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
  156. return 0;
  157. if (state->src.w == state->crtc.w)
  158. width_ok = true;
  159. if (state->src.h == state->crtc.h)
  160. height_ok = true;
  161. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  162. state->h_ratio == (1 << 15))
  163. width_ok = true;
  164. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  165. state->v_ratio == (1 << 15))
  166. height_ok = true;
  167. if (width_ok & height_ok)
  168. return 0;
  169. DRM_DEBUG_KMS("scaling mode is not supported");
  170. return -ENOTSUPP;
  171. }
  172. static int exynos_plane_atomic_check(struct drm_plane *plane,
  173. struct drm_plane_state *state)
  174. {
  175. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  176. struct exynos_drm_plane_state *exynos_state =
  177. to_exynos_plane_state(state);
  178. int ret = 0;
  179. if (!state->crtc || !state->fb)
  180. return 0;
  181. /* translate state into exynos_state */
  182. exynos_plane_mode_set(exynos_state);
  183. ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
  184. return ret;
  185. }
  186. static void exynos_plane_atomic_update(struct drm_plane *plane,
  187. struct drm_plane_state *old_state)
  188. {
  189. struct drm_plane_state *state = plane->state;
  190. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  191. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  192. if (!state->crtc)
  193. return;
  194. plane->crtc = state->crtc;
  195. exynos_plane->pending_fb = state->fb;
  196. if (exynos_crtc->ops->update_plane)
  197. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  198. }
  199. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  200. struct drm_plane_state *old_state)
  201. {
  202. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  203. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  204. if (!old_state->crtc)
  205. return;
  206. if (exynos_crtc->ops->disable_plane)
  207. exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
  208. }
  209. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  210. .atomic_check = exynos_plane_atomic_check,
  211. .atomic_update = exynos_plane_atomic_update,
  212. .atomic_disable = exynos_plane_atomic_disable,
  213. };
  214. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  215. unsigned int zpos)
  216. {
  217. struct drm_device *dev = plane->dev;
  218. struct exynos_drm_private *dev_priv = dev->dev_private;
  219. struct drm_property *prop;
  220. prop = dev_priv->plane_zpos_property;
  221. if (!prop) {
  222. prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
  223. "zpos", 0, MAX_PLANE - 1);
  224. if (!prop)
  225. return;
  226. dev_priv->plane_zpos_property = prop;
  227. }
  228. drm_object_attach_property(&plane->base, prop, zpos);
  229. }
  230. int exynos_plane_init(struct drm_device *dev,
  231. struct exynos_drm_plane *exynos_plane,
  232. unsigned long possible_crtcs,
  233. const struct exynos_drm_plane_config *config)
  234. {
  235. int err;
  236. err = drm_universal_plane_init(dev, &exynos_plane->base,
  237. possible_crtcs,
  238. &exynos_plane_funcs,
  239. config->pixel_formats,
  240. config->num_pixel_formats,
  241. config->type);
  242. if (err) {
  243. DRM_ERROR("failed to initialize plane\n");
  244. return err;
  245. }
  246. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  247. exynos_plane->zpos = config->zpos;
  248. exynos_plane->config = config;
  249. if (config->type == DRM_PLANE_TYPE_OVERLAY)
  250. exynos_plane_attach_zpos_property(&exynos_plane->base,
  251. config->zpos);
  252. return 0;
  253. }