rcar_du_vsp.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor
  3. *
  4. * Copyright (C) 2015 Renesas Electronics Corporation
  5. *
  6. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <drm/drmP.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/videodev2.h>
  22. #include <media/vsp1.h>
  23. #include "rcar_du_drv.h"
  24. #include "rcar_du_kms.h"
  25. #include "rcar_du_vsp.h"
  26. void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
  27. {
  28. const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
  29. struct rcar_du_device *rcdu = crtc->group->dev;
  30. struct rcar_du_plane_state state = {
  31. .state = {
  32. .crtc = &crtc->crtc,
  33. .crtc_x = 0,
  34. .crtc_y = 0,
  35. .crtc_w = mode->hdisplay,
  36. .crtc_h = mode->vdisplay,
  37. .src_x = 0,
  38. .src_y = 0,
  39. .src_w = mode->hdisplay << 16,
  40. .src_h = mode->vdisplay << 16,
  41. .zpos = 0,
  42. },
  43. .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
  44. .source = RCAR_DU_PLANE_VSPD1,
  45. .alpha = 255,
  46. .colorkey = 0,
  47. };
  48. if (rcdu->info->gen >= 3)
  49. state.hwindex = (crtc->index % 2) ? 2 : 0;
  50. else
  51. state.hwindex = crtc->index % 2;
  52. __rcar_du_plane_setup(crtc->group, &state);
  53. /* Ensure that the plane source configuration takes effect by requesting
  54. * a restart of the group. See rcar_du_plane_atomic_update() for a more
  55. * detailed explanation.
  56. *
  57. * TODO: Check whether this is still needed on Gen3.
  58. */
  59. crtc->group->need_restart = true;
  60. vsp1_du_setup_lif(crtc->vsp->vsp, mode->hdisplay, mode->vdisplay);
  61. }
  62. void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
  63. {
  64. vsp1_du_setup_lif(crtc->vsp->vsp, 0, 0);
  65. }
  66. void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
  67. {
  68. vsp1_du_atomic_begin(crtc->vsp->vsp);
  69. }
  70. void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
  71. {
  72. vsp1_du_atomic_flush(crtc->vsp->vsp);
  73. }
  74. /* Keep the two tables in sync. */
  75. static const u32 formats_kms[] = {
  76. DRM_FORMAT_RGB332,
  77. DRM_FORMAT_ARGB4444,
  78. DRM_FORMAT_XRGB4444,
  79. DRM_FORMAT_ARGB1555,
  80. DRM_FORMAT_XRGB1555,
  81. DRM_FORMAT_RGB565,
  82. DRM_FORMAT_BGR888,
  83. DRM_FORMAT_RGB888,
  84. DRM_FORMAT_BGRA8888,
  85. DRM_FORMAT_BGRX8888,
  86. DRM_FORMAT_ARGB8888,
  87. DRM_FORMAT_XRGB8888,
  88. DRM_FORMAT_UYVY,
  89. DRM_FORMAT_VYUY,
  90. DRM_FORMAT_YUYV,
  91. DRM_FORMAT_YVYU,
  92. DRM_FORMAT_NV12,
  93. DRM_FORMAT_NV21,
  94. DRM_FORMAT_NV16,
  95. DRM_FORMAT_NV61,
  96. DRM_FORMAT_YUV420,
  97. DRM_FORMAT_YVU420,
  98. DRM_FORMAT_YUV422,
  99. DRM_FORMAT_YVU422,
  100. DRM_FORMAT_YUV444,
  101. DRM_FORMAT_YVU444,
  102. };
  103. static const u32 formats_v4l2[] = {
  104. V4L2_PIX_FMT_RGB332,
  105. V4L2_PIX_FMT_ARGB444,
  106. V4L2_PIX_FMT_XRGB444,
  107. V4L2_PIX_FMT_ARGB555,
  108. V4L2_PIX_FMT_XRGB555,
  109. V4L2_PIX_FMT_RGB565,
  110. V4L2_PIX_FMT_RGB24,
  111. V4L2_PIX_FMT_BGR24,
  112. V4L2_PIX_FMT_ARGB32,
  113. V4L2_PIX_FMT_XRGB32,
  114. V4L2_PIX_FMT_ABGR32,
  115. V4L2_PIX_FMT_XBGR32,
  116. V4L2_PIX_FMT_UYVY,
  117. V4L2_PIX_FMT_VYUY,
  118. V4L2_PIX_FMT_YUYV,
  119. V4L2_PIX_FMT_YVYU,
  120. V4L2_PIX_FMT_NV12M,
  121. V4L2_PIX_FMT_NV21M,
  122. V4L2_PIX_FMT_NV16M,
  123. V4L2_PIX_FMT_NV61M,
  124. V4L2_PIX_FMT_YUV420M,
  125. V4L2_PIX_FMT_YVU420M,
  126. V4L2_PIX_FMT_YUV422M,
  127. V4L2_PIX_FMT_YVU422M,
  128. V4L2_PIX_FMT_YUV444M,
  129. V4L2_PIX_FMT_YVU444M,
  130. };
  131. static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
  132. {
  133. struct rcar_du_vsp_plane_state *state =
  134. to_rcar_vsp_plane_state(plane->plane.state);
  135. struct drm_framebuffer *fb = plane->plane.state->fb;
  136. struct vsp1_du_atomic_config cfg = {
  137. .pixelformat = 0,
  138. .pitch = fb->pitches[0],
  139. .alpha = state->alpha,
  140. .zpos = state->state.zpos,
  141. };
  142. unsigned int i;
  143. cfg.src.left = state->state.src_x >> 16;
  144. cfg.src.top = state->state.src_y >> 16;
  145. cfg.src.width = state->state.src_w >> 16;
  146. cfg.src.height = state->state.src_h >> 16;
  147. cfg.dst.left = state->state.crtc_x;
  148. cfg.dst.top = state->state.crtc_y;
  149. cfg.dst.width = state->state.crtc_w;
  150. cfg.dst.height = state->state.crtc_h;
  151. for (i = 0; i < state->format->planes; ++i) {
  152. struct drm_gem_cma_object *gem;
  153. gem = drm_fb_cma_get_gem_obj(fb, i);
  154. cfg.mem[i] = gem->paddr + fb->offsets[i];
  155. }
  156. for (i = 0; i < ARRAY_SIZE(formats_kms); ++i) {
  157. if (formats_kms[i] == state->format->fourcc) {
  158. cfg.pixelformat = formats_v4l2[i];
  159. break;
  160. }
  161. }
  162. vsp1_du_atomic_update(plane->vsp->vsp, plane->index, &cfg);
  163. }
  164. static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
  165. struct drm_plane_state *state)
  166. {
  167. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  168. struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
  169. struct rcar_du_device *rcdu = rplane->vsp->dev;
  170. if (!state->fb || !state->crtc) {
  171. rstate->format = NULL;
  172. return 0;
  173. }
  174. if (state->src_w >> 16 != state->crtc_w ||
  175. state->src_h >> 16 != state->crtc_h) {
  176. dev_dbg(rcdu->dev, "%s: scaling not supported\n", __func__);
  177. return -EINVAL;
  178. }
  179. rstate->format = rcar_du_format_info(state->fb->format->format);
  180. if (rstate->format == NULL) {
  181. dev_dbg(rcdu->dev, "%s: unsupported format %08x\n", __func__,
  182. state->fb->format->format);
  183. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
  188. struct drm_plane_state *old_state)
  189. {
  190. struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
  191. if (plane->state->crtc)
  192. rcar_du_vsp_plane_setup(rplane);
  193. else
  194. vsp1_du_atomic_update(rplane->vsp->vsp, rplane->index, NULL);
  195. }
  196. static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
  197. .atomic_check = rcar_du_vsp_plane_atomic_check,
  198. .atomic_update = rcar_du_vsp_plane_atomic_update,
  199. };
  200. static struct drm_plane_state *
  201. rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
  202. {
  203. struct rcar_du_vsp_plane_state *state;
  204. struct rcar_du_vsp_plane_state *copy;
  205. if (WARN_ON(!plane->state))
  206. return NULL;
  207. state = to_rcar_vsp_plane_state(plane->state);
  208. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  209. if (copy == NULL)
  210. return NULL;
  211. __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
  212. return &copy->state;
  213. }
  214. static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
  215. struct drm_plane_state *state)
  216. {
  217. __drm_atomic_helper_plane_destroy_state(state);
  218. kfree(to_rcar_vsp_plane_state(state));
  219. }
  220. static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
  221. {
  222. struct rcar_du_vsp_plane_state *state;
  223. if (plane->state) {
  224. rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
  225. plane->state = NULL;
  226. }
  227. state = kzalloc(sizeof(*state), GFP_KERNEL);
  228. if (state == NULL)
  229. return;
  230. state->alpha = 255;
  231. state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
  232. plane->state = &state->state;
  233. plane->state->plane = plane;
  234. }
  235. static int rcar_du_vsp_plane_atomic_set_property(struct drm_plane *plane,
  236. struct drm_plane_state *state, struct drm_property *property,
  237. uint64_t val)
  238. {
  239. struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
  240. struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
  241. if (property == rcdu->props.alpha)
  242. rstate->alpha = val;
  243. else
  244. return -EINVAL;
  245. return 0;
  246. }
  247. static int rcar_du_vsp_plane_atomic_get_property(struct drm_plane *plane,
  248. const struct drm_plane_state *state, struct drm_property *property,
  249. uint64_t *val)
  250. {
  251. const struct rcar_du_vsp_plane_state *rstate =
  252. container_of(state, const struct rcar_du_vsp_plane_state, state);
  253. struct rcar_du_device *rcdu = to_rcar_vsp_plane(plane)->vsp->dev;
  254. if (property == rcdu->props.alpha)
  255. *val = rstate->alpha;
  256. else
  257. return -EINVAL;
  258. return 0;
  259. }
  260. static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
  261. .update_plane = drm_atomic_helper_update_plane,
  262. .disable_plane = drm_atomic_helper_disable_plane,
  263. .reset = rcar_du_vsp_plane_reset,
  264. .set_property = drm_atomic_helper_plane_set_property,
  265. .destroy = drm_plane_cleanup,
  266. .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
  267. .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
  268. .atomic_set_property = rcar_du_vsp_plane_atomic_set_property,
  269. .atomic_get_property = rcar_du_vsp_plane_atomic_get_property,
  270. };
  271. int rcar_du_vsp_init(struct rcar_du_vsp *vsp)
  272. {
  273. struct rcar_du_device *rcdu = vsp->dev;
  274. struct platform_device *pdev;
  275. struct device_node *np;
  276. unsigned int i;
  277. int ret;
  278. /* Find the VSP device and initialize it. */
  279. np = of_parse_phandle(rcdu->dev->of_node, "vsps", vsp->index);
  280. if (!np) {
  281. dev_err(rcdu->dev, "vsps node not found\n");
  282. return -ENXIO;
  283. }
  284. pdev = of_find_device_by_node(np);
  285. of_node_put(np);
  286. if (!pdev)
  287. return -ENXIO;
  288. vsp->vsp = &pdev->dev;
  289. ret = vsp1_du_init(vsp->vsp);
  290. if (ret < 0)
  291. return ret;
  292. /* The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
  293. * 4 RPFs.
  294. */
  295. vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
  296. vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
  297. sizeof(*vsp->planes), GFP_KERNEL);
  298. if (!vsp->planes)
  299. return -ENOMEM;
  300. for (i = 0; i < vsp->num_planes; ++i) {
  301. enum drm_plane_type type = i ? DRM_PLANE_TYPE_OVERLAY
  302. : DRM_PLANE_TYPE_PRIMARY;
  303. struct rcar_du_vsp_plane *plane = &vsp->planes[i];
  304. plane->vsp = vsp;
  305. plane->index = i;
  306. ret = drm_universal_plane_init(rcdu->ddev, &plane->plane,
  307. 1 << vsp->index,
  308. &rcar_du_vsp_plane_funcs,
  309. formats_kms,
  310. ARRAY_SIZE(formats_kms), type,
  311. NULL);
  312. if (ret < 0)
  313. return ret;
  314. drm_plane_helper_add(&plane->plane,
  315. &rcar_du_vsp_plane_helper_funcs);
  316. if (type == DRM_PLANE_TYPE_PRIMARY)
  317. continue;
  318. drm_object_attach_property(&plane->plane.base,
  319. rcdu->props.alpha, 255);
  320. drm_plane_create_zpos_property(&plane->plane, 1, 1,
  321. vsp->num_planes - 1);
  322. }
  323. return 0;
  324. }