exynos_drm_plane.c 9.6 KB

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