exynos_drm_plane.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. /* set ratio */
  78. exynos_state->h_ratio = (src_w << 16) / crtc_w;
  79. exynos_state->v_ratio = (src_h << 16) / crtc_h;
  80. /* clip to visible area */
  81. actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
  82. actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
  83. if (crtc_x < 0) {
  84. if (actual_w)
  85. src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
  86. crtc_x = 0;
  87. }
  88. if (crtc_y < 0) {
  89. if (actual_h)
  90. src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
  91. crtc_y = 0;
  92. }
  93. /* set drm framebuffer data. */
  94. exynos_state->src.x = src_x;
  95. exynos_state->src.y = src_y;
  96. exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
  97. exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
  98. /* set plane range to be displayed. */
  99. exynos_state->crtc.x = crtc_x;
  100. exynos_state->crtc.y = crtc_y;
  101. exynos_state->crtc.w = actual_w;
  102. exynos_state->crtc.h = actual_h;
  103. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  104. exynos_state->crtc.x, exynos_state->crtc.y,
  105. exynos_state->crtc.w, exynos_state->crtc.h);
  106. }
  107. static void exynos_drm_plane_reset(struct drm_plane *plane)
  108. {
  109. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  110. struct exynos_drm_plane_state *exynos_state;
  111. if (plane->state) {
  112. exynos_state = to_exynos_plane_state(plane->state);
  113. if (exynos_state->base.fb)
  114. drm_framebuffer_unreference(exynos_state->base.fb);
  115. kfree(exynos_state);
  116. plane->state = NULL;
  117. }
  118. exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  119. if (exynos_state) {
  120. exynos_state->zpos = exynos_plane->config->zpos;
  121. plane->state = &exynos_state->base;
  122. plane->state->plane = plane;
  123. }
  124. }
  125. static struct drm_plane_state *
  126. exynos_drm_plane_duplicate_state(struct drm_plane *plane)
  127. {
  128. struct exynos_drm_plane_state *exynos_state;
  129. struct exynos_drm_plane_state *copy;
  130. exynos_state = to_exynos_plane_state(plane->state);
  131. copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  132. if (!copy)
  133. return NULL;
  134. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  135. copy->zpos = exynos_state->zpos;
  136. return &copy->base;
  137. }
  138. static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
  139. struct drm_plane_state *old_state)
  140. {
  141. struct exynos_drm_plane_state *old_exynos_state =
  142. to_exynos_plane_state(old_state);
  143. __drm_atomic_helper_plane_destroy_state(plane, old_state);
  144. kfree(old_exynos_state);
  145. }
  146. static int exynos_drm_plane_atomic_set_property(struct drm_plane *plane,
  147. struct drm_plane_state *state,
  148. struct drm_property *property,
  149. uint64_t val)
  150. {
  151. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  152. struct exynos_drm_plane_state *exynos_state =
  153. to_exynos_plane_state(state);
  154. struct exynos_drm_private *dev_priv = plane->dev->dev_private;
  155. const struct exynos_drm_plane_config *config = exynos_plane->config;
  156. if (property == dev_priv->plane_zpos_property &&
  157. (config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS))
  158. exynos_state->zpos = val;
  159. else
  160. return -EINVAL;
  161. return 0;
  162. }
  163. static int exynos_drm_plane_atomic_get_property(struct drm_plane *plane,
  164. const struct drm_plane_state *state,
  165. struct drm_property *property,
  166. uint64_t *val)
  167. {
  168. const struct exynos_drm_plane_state *exynos_state =
  169. container_of(state, const struct exynos_drm_plane_state, base);
  170. struct exynos_drm_private *dev_priv = plane->dev->dev_private;
  171. if (property == dev_priv->plane_zpos_property)
  172. *val = exynos_state->zpos;
  173. else
  174. return -EINVAL;
  175. return 0;
  176. }
  177. static struct drm_plane_funcs exynos_plane_funcs = {
  178. .update_plane = drm_atomic_helper_update_plane,
  179. .disable_plane = drm_atomic_helper_disable_plane,
  180. .destroy = drm_plane_cleanup,
  181. .set_property = drm_atomic_helper_plane_set_property,
  182. .reset = exynos_drm_plane_reset,
  183. .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
  184. .atomic_destroy_state = exynos_drm_plane_destroy_state,
  185. .atomic_set_property = exynos_drm_plane_atomic_set_property,
  186. .atomic_get_property = exynos_drm_plane_atomic_get_property,
  187. };
  188. static int
  189. exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
  190. struct exynos_drm_plane_state *state)
  191. {
  192. bool width_ok = false, height_ok = false;
  193. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
  194. return 0;
  195. if (state->src.w == state->crtc.w)
  196. width_ok = true;
  197. if (state->src.h == state->crtc.h)
  198. height_ok = true;
  199. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  200. state->h_ratio == (1 << 15))
  201. width_ok = true;
  202. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  203. state->v_ratio == (1 << 15))
  204. height_ok = true;
  205. if (width_ok & height_ok)
  206. return 0;
  207. DRM_DEBUG_KMS("scaling mode is not supported");
  208. return -ENOTSUPP;
  209. }
  210. static int exynos_plane_atomic_check(struct drm_plane *plane,
  211. struct drm_plane_state *state)
  212. {
  213. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  214. struct exynos_drm_plane_state *exynos_state =
  215. to_exynos_plane_state(state);
  216. int ret = 0;
  217. if (!state->crtc || !state->fb)
  218. return 0;
  219. /* translate state into exynos_state */
  220. exynos_plane_mode_set(exynos_state);
  221. ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
  222. return ret;
  223. }
  224. static void exynos_plane_atomic_update(struct drm_plane *plane,
  225. struct drm_plane_state *old_state)
  226. {
  227. struct drm_plane_state *state = plane->state;
  228. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  229. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  230. if (!state->crtc)
  231. return;
  232. plane->crtc = state->crtc;
  233. exynos_plane->pending_fb = state->fb;
  234. if (exynos_crtc->ops->update_plane)
  235. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  236. }
  237. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  238. struct drm_plane_state *old_state)
  239. {
  240. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  241. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  242. if (!old_state->crtc)
  243. return;
  244. if (exynos_crtc->ops->disable_plane)
  245. exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
  246. }
  247. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  248. .atomic_check = exynos_plane_atomic_check,
  249. .atomic_update = exynos_plane_atomic_update,
  250. .atomic_disable = exynos_plane_atomic_disable,
  251. };
  252. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  253. unsigned int zpos)
  254. {
  255. struct drm_device *dev = plane->dev;
  256. struct exynos_drm_private *dev_priv = dev->dev_private;
  257. struct drm_property *prop;
  258. prop = dev_priv->plane_zpos_property;
  259. if (!prop) {
  260. prop = drm_property_create_range(dev, 0, "zpos",
  261. 0, MAX_PLANE - 1);
  262. if (!prop)
  263. return;
  264. dev_priv->plane_zpos_property = prop;
  265. }
  266. drm_object_attach_property(&plane->base, prop, zpos);
  267. }
  268. int exynos_plane_init(struct drm_device *dev,
  269. struct exynos_drm_plane *exynos_plane,
  270. unsigned int index, unsigned long possible_crtcs,
  271. const struct exynos_drm_plane_config *config)
  272. {
  273. int err;
  274. err = drm_universal_plane_init(dev, &exynos_plane->base,
  275. possible_crtcs,
  276. &exynos_plane_funcs,
  277. config->pixel_formats,
  278. config->num_pixel_formats,
  279. config->type, NULL);
  280. if (err) {
  281. DRM_ERROR("failed to initialize plane\n");
  282. return err;
  283. }
  284. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  285. exynos_plane->index = index;
  286. exynos_plane->config = config;
  287. exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos);
  288. return 0;
  289. }