rcar_du_vsp.c 9.7 KB

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