vsp1_rpf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
  4. *
  5. * Copyright (C) 2013-2014 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/device.h>
  10. #include <media/v4l2-subdev.h>
  11. #include "vsp1.h"
  12. #include "vsp1_dl.h"
  13. #include "vsp1_pipe.h"
  14. #include "vsp1_rwpf.h"
  15. #include "vsp1_video.h"
  16. #define RPF_MAX_WIDTH 8190
  17. #define RPF_MAX_HEIGHT 8190
  18. /* Pre extended display list command data structure. */
  19. struct vsp1_extcmd_auto_fld_body {
  20. u32 top_y0;
  21. u32 bottom_y0;
  22. u32 top_c0;
  23. u32 bottom_c0;
  24. u32 top_c1;
  25. u32 bottom_c1;
  26. u32 reserved0;
  27. u32 reserved1;
  28. } __packed;
  29. /* -----------------------------------------------------------------------------
  30. * Device Access
  31. */
  32. static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf,
  33. struct vsp1_dl_body *dlb, u32 reg, u32 data)
  34. {
  35. vsp1_dl_body_write(dlb, reg + rpf->entity.index * VI6_RPF_OFFSET,
  36. data);
  37. }
  38. /* -----------------------------------------------------------------------------
  39. * V4L2 Subdevice Operations
  40. */
  41. static const struct v4l2_subdev_ops rpf_ops = {
  42. .pad = &vsp1_rwpf_pad_ops,
  43. };
  44. /* -----------------------------------------------------------------------------
  45. * VSP1 Entity Operations
  46. */
  47. static void rpf_configure_stream(struct vsp1_entity *entity,
  48. struct vsp1_pipeline *pipe,
  49. struct vsp1_dl_body *dlb)
  50. {
  51. struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
  52. const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
  53. const struct v4l2_pix_format_mplane *format = &rpf->format;
  54. const struct v4l2_mbus_framefmt *source_format;
  55. const struct v4l2_mbus_framefmt *sink_format;
  56. unsigned int left = 0;
  57. unsigned int top = 0;
  58. u32 pstride;
  59. u32 infmt;
  60. /* Stride */
  61. pstride = format->plane_fmt[0].bytesperline
  62. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  63. if (format->num_planes > 1)
  64. pstride |= format->plane_fmt[1].bytesperline
  65. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  66. /*
  67. * pstride has both STRIDE_Y and STRIDE_C, but multiplying the whole
  68. * of pstride by 2 is conveniently OK here as we are multiplying both
  69. * values.
  70. */
  71. if (pipe->interlaced)
  72. pstride *= 2;
  73. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_PSTRIDE, pstride);
  74. /* Format */
  75. sink_format = vsp1_entity_get_pad_format(&rpf->entity,
  76. rpf->entity.config,
  77. RWPF_PAD_SINK);
  78. source_format = vsp1_entity_get_pad_format(&rpf->entity,
  79. rpf->entity.config,
  80. RWPF_PAD_SOURCE);
  81. infmt = VI6_RPF_INFMT_CIPM
  82. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  83. if (fmtinfo->swap_yc)
  84. infmt |= VI6_RPF_INFMT_SPYCS;
  85. if (fmtinfo->swap_uv)
  86. infmt |= VI6_RPF_INFMT_SPUVS;
  87. if (sink_format->code != source_format->code)
  88. infmt |= VI6_RPF_INFMT_CSC;
  89. vsp1_rpf_write(rpf, dlb, VI6_RPF_INFMT, infmt);
  90. vsp1_rpf_write(rpf, dlb, VI6_RPF_DSWAP, fmtinfo->swap);
  91. /* Output location */
  92. if (pipe->brx) {
  93. const struct v4l2_rect *compose;
  94. compose = vsp1_entity_get_pad_selection(pipe->brx,
  95. pipe->brx->config,
  96. rpf->brx_input,
  97. V4L2_SEL_TGT_COMPOSE);
  98. left = compose->left;
  99. top = compose->top;
  100. }
  101. if (pipe->interlaced)
  102. top /= 2;
  103. vsp1_rpf_write(rpf, dlb, VI6_RPF_LOC,
  104. (left << VI6_RPF_LOC_HCOORD_SHIFT) |
  105. (top << VI6_RPF_LOC_VCOORD_SHIFT));
  106. /*
  107. * On Gen2 use the alpha channel (extended to 8 bits) when available or
  108. * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
  109. * otherwise.
  110. *
  111. * The Gen3 RPF has extended alpha capability and can both multiply the
  112. * alpha channel by a fixed global alpha value, and multiply the pixel
  113. * components to convert the input to premultiplied alpha.
  114. *
  115. * As alpha premultiplication is available in the BRx for both Gen2 and
  116. * Gen3 we handle it there and use the Gen3 alpha multiplier for global
  117. * alpha multiplication only. This however prevents conversion to
  118. * premultiplied alpha if no BRx is present in the pipeline. If that use
  119. * case turns out to be useful we will revisit the implementation (for
  120. * Gen3 only).
  121. *
  122. * We enable alpha multiplication on Gen3 using the fixed alpha value
  123. * set through the V4L2_CID_ALPHA_COMPONENT control when the input
  124. * contains an alpha channel. On Gen2 the global alpha is ignored in
  125. * that case.
  126. *
  127. * In all cases, disable color keying.
  128. */
  129. vsp1_rpf_write(rpf, dlb, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
  130. (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
  131. : VI6_RPF_ALPH_SEL_ASEL_FIXED));
  132. if (entity->vsp1->info->gen == 3) {
  133. u32 mult;
  134. if (fmtinfo->alpha) {
  135. /*
  136. * When the input contains an alpha channel enable the
  137. * alpha multiplier. If the input is premultiplied we
  138. * need to multiply both the alpha channel and the pixel
  139. * components by the global alpha value to keep them
  140. * premultiplied. Otherwise multiply the alpha channel
  141. * only.
  142. */
  143. bool premultiplied = format->flags
  144. & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
  145. mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO
  146. | (premultiplied ?
  147. VI6_RPF_MULT_ALPHA_P_MMD_RATIO :
  148. VI6_RPF_MULT_ALPHA_P_MMD_NONE);
  149. } else {
  150. /*
  151. * When the input doesn't contain an alpha channel the
  152. * global alpha value is applied in the unpacking unit,
  153. * the alpha multiplier isn't needed and must be
  154. * disabled.
  155. */
  156. mult = VI6_RPF_MULT_ALPHA_A_MMD_NONE
  157. | VI6_RPF_MULT_ALPHA_P_MMD_NONE;
  158. }
  159. rpf->mult_alpha = mult;
  160. }
  161. vsp1_rpf_write(rpf, dlb, VI6_RPF_MSK_CTRL, 0);
  162. vsp1_rpf_write(rpf, dlb, VI6_RPF_CKEY_CTRL, 0);
  163. }
  164. static void vsp1_rpf_configure_autofld(struct vsp1_rwpf *rpf,
  165. struct vsp1_dl_list *dl)
  166. {
  167. const struct v4l2_pix_format_mplane *format = &rpf->format;
  168. struct vsp1_dl_ext_cmd *cmd;
  169. struct vsp1_extcmd_auto_fld_body *auto_fld;
  170. u32 offset_y, offset_c;
  171. cmd = vsp1_dl_get_pre_cmd(dl);
  172. if (WARN_ONCE(!cmd, "Failed to obtain an autofld cmd"))
  173. return;
  174. /* Re-index our auto_fld to match the current RPF. */
  175. auto_fld = cmd->data;
  176. auto_fld = &auto_fld[rpf->entity.index];
  177. auto_fld->top_y0 = rpf->mem.addr[0];
  178. auto_fld->top_c0 = rpf->mem.addr[1];
  179. auto_fld->top_c1 = rpf->mem.addr[2];
  180. offset_y = format->plane_fmt[0].bytesperline;
  181. offset_c = format->plane_fmt[1].bytesperline;
  182. auto_fld->bottom_y0 = rpf->mem.addr[0] + offset_y;
  183. auto_fld->bottom_c0 = rpf->mem.addr[1] + offset_c;
  184. auto_fld->bottom_c1 = rpf->mem.addr[2] + offset_c;
  185. cmd->flags |= VI6_DL_EXT_AUTOFLD_INT | BIT(16 + rpf->entity.index);
  186. }
  187. static void rpf_configure_frame(struct vsp1_entity *entity,
  188. struct vsp1_pipeline *pipe,
  189. struct vsp1_dl_list *dl,
  190. struct vsp1_dl_body *dlb)
  191. {
  192. struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
  193. vsp1_rpf_write(rpf, dlb, VI6_RPF_VRTCOL_SET,
  194. rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
  195. vsp1_rpf_write(rpf, dlb, VI6_RPF_MULT_ALPHA, rpf->mult_alpha |
  196. (rpf->alpha << VI6_RPF_MULT_ALPHA_RATIO_SHIFT));
  197. vsp1_pipeline_propagate_alpha(pipe, dlb, rpf->alpha);
  198. }
  199. static void rpf_configure_partition(struct vsp1_entity *entity,
  200. struct vsp1_pipeline *pipe,
  201. struct vsp1_dl_list *dl,
  202. struct vsp1_dl_body *dlb)
  203. {
  204. struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
  205. struct vsp1_rwpf_memory mem = rpf->mem;
  206. struct vsp1_device *vsp1 = rpf->entity.vsp1;
  207. const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
  208. const struct v4l2_pix_format_mplane *format = &rpf->format;
  209. struct v4l2_rect crop;
  210. /*
  211. * Source size and crop offsets.
  212. *
  213. * The crop offsets correspond to the location of the crop
  214. * rectangle top left corner in the plane buffer. Only two
  215. * offsets are needed, as planes 2 and 3 always have identical
  216. * strides.
  217. */
  218. crop = *vsp1_rwpf_get_crop(rpf, rpf->entity.config);
  219. /*
  220. * Partition Algorithm Control
  221. *
  222. * The partition algorithm can split this frame into multiple
  223. * slices. We must scale our partition window based on the pipe
  224. * configuration to match the destination partition window.
  225. * To achieve this, we adjust our crop to provide a 'sub-crop'
  226. * matching the expected partition window. Only 'left' and
  227. * 'width' need to be adjusted.
  228. */
  229. if (pipe->partitions > 1) {
  230. crop.width = pipe->partition->rpf.width;
  231. crop.left += pipe->partition->rpf.left;
  232. }
  233. if (pipe->interlaced) {
  234. crop.height = round_down(crop.height / 2, fmtinfo->vsub);
  235. crop.top = round_down(crop.top / 2, fmtinfo->vsub);
  236. }
  237. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRC_BSIZE,
  238. (crop.width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  239. (crop.height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  240. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRC_ESIZE,
  241. (crop.width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  242. (crop.height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  243. mem.addr[0] += crop.top * format->plane_fmt[0].bytesperline
  244. + crop.left * fmtinfo->bpp[0] / 8;
  245. if (format->num_planes > 1) {
  246. unsigned int offset;
  247. offset = crop.top * format->plane_fmt[1].bytesperline
  248. + crop.left / fmtinfo->hsub
  249. * fmtinfo->bpp[1] / 8;
  250. mem.addr[1] += offset;
  251. mem.addr[2] += offset;
  252. }
  253. /*
  254. * On Gen3 hardware the SPUVS bit has no effect on 3-planar
  255. * formats. Swap the U and V planes manually in that case.
  256. */
  257. if (vsp1->info->gen == 3 && format->num_planes == 3 &&
  258. fmtinfo->swap_uv)
  259. swap(mem.addr[1], mem.addr[2]);
  260. /*
  261. * Interlaced pipelines will use the extended pre-cmd to process
  262. * SRCM_ADDR_{Y,C0,C1}
  263. */
  264. if (pipe->interlaced) {
  265. vsp1_rpf_configure_autofld(rpf, dl);
  266. } else {
  267. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_Y, mem.addr[0]);
  268. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_C0, mem.addr[1]);
  269. vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_C1, mem.addr[2]);
  270. }
  271. }
  272. static void rpf_partition(struct vsp1_entity *entity,
  273. struct vsp1_pipeline *pipe,
  274. struct vsp1_partition *partition,
  275. unsigned int partition_idx,
  276. struct vsp1_partition_window *window)
  277. {
  278. partition->rpf = *window;
  279. }
  280. static const struct vsp1_entity_operations rpf_entity_ops = {
  281. .configure_stream = rpf_configure_stream,
  282. .configure_frame = rpf_configure_frame,
  283. .configure_partition = rpf_configure_partition,
  284. .partition = rpf_partition,
  285. };
  286. /* -----------------------------------------------------------------------------
  287. * Initialization and Cleanup
  288. */
  289. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  290. {
  291. struct vsp1_rwpf *rpf;
  292. char name[6];
  293. int ret;
  294. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  295. if (rpf == NULL)
  296. return ERR_PTR(-ENOMEM);
  297. rpf->max_width = RPF_MAX_WIDTH;
  298. rpf->max_height = RPF_MAX_HEIGHT;
  299. rpf->entity.ops = &rpf_entity_ops;
  300. rpf->entity.type = VSP1_ENTITY_RPF;
  301. rpf->entity.index = index;
  302. sprintf(name, "rpf.%u", index);
  303. ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops,
  304. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  305. if (ret < 0)
  306. return ERR_PTR(ret);
  307. /* Initialize the control handler. */
  308. ret = vsp1_rwpf_init_ctrls(rpf, 0);
  309. if (ret < 0) {
  310. dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
  311. index);
  312. goto error;
  313. }
  314. v4l2_ctrl_handler_setup(&rpf->ctrls);
  315. return rpf;
  316. error:
  317. vsp1_entity_destroy(&rpf->entity);
  318. return ERR_PTR(ret);
  319. }