vsp1_rpf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
  3. *
  4. * Copyright (C) 2013-2014 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 <linux/device.h>
  14. #include <media/v4l2-subdev.h>
  15. #include "vsp1.h"
  16. #include "vsp1_dl.h"
  17. #include "vsp1_pipe.h"
  18. #include "vsp1_rwpf.h"
  19. #include "vsp1_video.h"
  20. #define RPF_MAX_WIDTH 8190
  21. #define RPF_MAX_HEIGHT 8190
  22. /* -----------------------------------------------------------------------------
  23. * Device Access
  24. */
  25. static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf,
  26. struct vsp1_dl_list *dl, u32 reg, u32 data)
  27. {
  28. vsp1_dl_list_write(dl, reg + rpf->entity.index * VI6_RPF_OFFSET, data);
  29. }
  30. /* -----------------------------------------------------------------------------
  31. * V4L2 Subdevice Operations
  32. */
  33. static const struct v4l2_subdev_ops rpf_ops = {
  34. .pad = &vsp1_rwpf_pad_ops,
  35. };
  36. /* -----------------------------------------------------------------------------
  37. * VSP1 Entity Operations
  38. */
  39. static void rpf_configure(struct vsp1_entity *entity,
  40. struct vsp1_pipeline *pipe,
  41. struct vsp1_dl_list *dl,
  42. enum vsp1_entity_params params)
  43. {
  44. struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
  45. const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
  46. const struct v4l2_pix_format_mplane *format = &rpf->format;
  47. const struct v4l2_mbus_framefmt *source_format;
  48. const struct v4l2_mbus_framefmt *sink_format;
  49. unsigned int left = 0;
  50. unsigned int top = 0;
  51. u32 pstride;
  52. u32 infmt;
  53. if (params == VSP1_ENTITY_PARAMS_RUNTIME) {
  54. vsp1_rpf_write(rpf, dl, VI6_RPF_VRTCOL_SET,
  55. rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
  56. vsp1_rpf_write(rpf, dl, VI6_RPF_MULT_ALPHA, rpf->mult_alpha |
  57. (rpf->alpha << VI6_RPF_MULT_ALPHA_RATIO_SHIFT));
  58. vsp1_pipeline_propagate_alpha(pipe, dl, rpf->alpha);
  59. return;
  60. }
  61. if (params == VSP1_ENTITY_PARAMS_PARTITION) {
  62. struct vsp1_device *vsp1 = rpf->entity.vsp1;
  63. struct vsp1_rwpf_memory mem = rpf->mem;
  64. struct v4l2_rect crop;
  65. /*
  66. * Source size and crop offsets.
  67. *
  68. * The crop offsets correspond to the location of the crop
  69. * rectangle top left corner in the plane buffer. Only two
  70. * offsets are needed, as planes 2 and 3 always have identical
  71. * strides.
  72. */
  73. crop = *vsp1_rwpf_get_crop(rpf, rpf->entity.config);
  74. /*
  75. * Partition Algorithm Control
  76. *
  77. * The partition algorithm can split this frame into multiple
  78. * slices. We must scale our partition window based on the pipe
  79. * configuration to match the destination partition window.
  80. * To achieve this, we adjust our crop to provide a 'sub-crop'
  81. * matching the expected partition window. Only 'left' and
  82. * 'width' need to be adjusted.
  83. */
  84. if (pipe->partitions > 1) {
  85. crop.width = pipe->partition->rpf.width;
  86. crop.left += pipe->partition->rpf.left;
  87. }
  88. vsp1_rpf_write(rpf, dl, VI6_RPF_SRC_BSIZE,
  89. (crop.width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  90. (crop.height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  91. vsp1_rpf_write(rpf, dl, VI6_RPF_SRC_ESIZE,
  92. (crop.width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  93. (crop.height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  94. mem.addr[0] += crop.top * format->plane_fmt[0].bytesperline
  95. + crop.left * fmtinfo->bpp[0] / 8;
  96. if (format->num_planes > 1) {
  97. unsigned int offset;
  98. offset = crop.top * format->plane_fmt[1].bytesperline
  99. + crop.left / fmtinfo->hsub
  100. * fmtinfo->bpp[1] / 8;
  101. mem.addr[1] += offset;
  102. mem.addr[2] += offset;
  103. }
  104. /*
  105. * On Gen3 hardware the SPUVS bit has no effect on 3-planar
  106. * formats. Swap the U and V planes manually in that case.
  107. */
  108. if (vsp1->info->gen == 3 && format->num_planes == 3 &&
  109. fmtinfo->swap_uv)
  110. swap(mem.addr[1], mem.addr[2]);
  111. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_Y, mem.addr[0]);
  112. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_C0, mem.addr[1]);
  113. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_C1, mem.addr[2]);
  114. return;
  115. }
  116. /* Stride */
  117. pstride = format->plane_fmt[0].bytesperline
  118. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  119. if (format->num_planes > 1)
  120. pstride |= format->plane_fmt[1].bytesperline
  121. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  122. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_PSTRIDE, pstride);
  123. /* Format */
  124. sink_format = vsp1_entity_get_pad_format(&rpf->entity,
  125. rpf->entity.config,
  126. RWPF_PAD_SINK);
  127. source_format = vsp1_entity_get_pad_format(&rpf->entity,
  128. rpf->entity.config,
  129. RWPF_PAD_SOURCE);
  130. infmt = VI6_RPF_INFMT_CIPM
  131. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  132. if (fmtinfo->swap_yc)
  133. infmt |= VI6_RPF_INFMT_SPYCS;
  134. if (fmtinfo->swap_uv)
  135. infmt |= VI6_RPF_INFMT_SPUVS;
  136. if (sink_format->code != source_format->code)
  137. infmt |= VI6_RPF_INFMT_CSC;
  138. vsp1_rpf_write(rpf, dl, VI6_RPF_INFMT, infmt);
  139. vsp1_rpf_write(rpf, dl, VI6_RPF_DSWAP, fmtinfo->swap);
  140. /* Output location */
  141. if (pipe->bru) {
  142. const struct v4l2_rect *compose;
  143. compose = vsp1_entity_get_pad_selection(pipe->bru,
  144. pipe->bru->config,
  145. rpf->bru_input,
  146. V4L2_SEL_TGT_COMPOSE);
  147. left = compose->left;
  148. top = compose->top;
  149. }
  150. vsp1_rpf_write(rpf, dl, VI6_RPF_LOC,
  151. (left << VI6_RPF_LOC_HCOORD_SHIFT) |
  152. (top << VI6_RPF_LOC_VCOORD_SHIFT));
  153. /*
  154. * On Gen2 use the alpha channel (extended to 8 bits) when available or
  155. * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
  156. * otherwise.
  157. *
  158. * The Gen3 RPF has extended alpha capability and can both multiply the
  159. * alpha channel by a fixed global alpha value, and multiply the pixel
  160. * components to convert the input to premultiplied alpha.
  161. *
  162. * As alpha premultiplication is available in the BRU for both Gen2 and
  163. * Gen3 we handle it there and use the Gen3 alpha multiplier for global
  164. * alpha multiplication only. This however prevents conversion to
  165. * premultiplied alpha if no BRU is present in the pipeline. If that use
  166. * case turns out to be useful we will revisit the implementation (for
  167. * Gen3 only).
  168. *
  169. * We enable alpha multiplication on Gen3 using the fixed alpha value
  170. * set through the V4L2_CID_ALPHA_COMPONENT control when the input
  171. * contains an alpha channel. On Gen2 the global alpha is ignored in
  172. * that case.
  173. *
  174. * In all cases, disable color keying.
  175. */
  176. vsp1_rpf_write(rpf, dl, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
  177. (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
  178. : VI6_RPF_ALPH_SEL_ASEL_FIXED));
  179. if (entity->vsp1->info->gen == 3) {
  180. u32 mult;
  181. if (fmtinfo->alpha) {
  182. /*
  183. * When the input contains an alpha channel enable the
  184. * alpha multiplier. If the input is premultiplied we
  185. * need to multiply both the alpha channel and the pixel
  186. * components by the global alpha value to keep them
  187. * premultiplied. Otherwise multiply the alpha channel
  188. * only.
  189. */
  190. bool premultiplied = format->flags
  191. & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
  192. mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO
  193. | (premultiplied ?
  194. VI6_RPF_MULT_ALPHA_P_MMD_RATIO :
  195. VI6_RPF_MULT_ALPHA_P_MMD_NONE);
  196. } else {
  197. /*
  198. * When the input doesn't contain an alpha channel the
  199. * global alpha value is applied in the unpacking unit,
  200. * the alpha multiplier isn't needed and must be
  201. * disabled.
  202. */
  203. mult = VI6_RPF_MULT_ALPHA_A_MMD_NONE
  204. | VI6_RPF_MULT_ALPHA_P_MMD_NONE;
  205. }
  206. rpf->mult_alpha = mult;
  207. }
  208. vsp1_rpf_write(rpf, dl, VI6_RPF_MSK_CTRL, 0);
  209. vsp1_rpf_write(rpf, dl, VI6_RPF_CKEY_CTRL, 0);
  210. }
  211. static void rpf_partition(struct vsp1_entity *entity,
  212. struct vsp1_pipeline *pipe,
  213. struct vsp1_partition *partition,
  214. unsigned int partition_idx,
  215. struct vsp1_partition_window *window)
  216. {
  217. partition->rpf = *window;
  218. }
  219. static const struct vsp1_entity_operations rpf_entity_ops = {
  220. .configure = rpf_configure,
  221. .partition = rpf_partition,
  222. };
  223. /* -----------------------------------------------------------------------------
  224. * Initialization and Cleanup
  225. */
  226. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  227. {
  228. struct vsp1_rwpf *rpf;
  229. char name[6];
  230. int ret;
  231. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  232. if (rpf == NULL)
  233. return ERR_PTR(-ENOMEM);
  234. rpf->max_width = RPF_MAX_WIDTH;
  235. rpf->max_height = RPF_MAX_HEIGHT;
  236. rpf->entity.ops = &rpf_entity_ops;
  237. rpf->entity.type = VSP1_ENTITY_RPF;
  238. rpf->entity.index = index;
  239. sprintf(name, "rpf.%u", index);
  240. ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops,
  241. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  242. if (ret < 0)
  243. return ERR_PTR(ret);
  244. /* Initialize the control handler. */
  245. ret = vsp1_rwpf_init_ctrls(rpf, 0);
  246. if (ret < 0) {
  247. dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
  248. index);
  249. goto error;
  250. }
  251. v4l2_ctrl_handler_setup(&rpf->ctrls);
  252. return rpf;
  253. error:
  254. vsp1_entity_destroy(&rpf->entity);
  255. return ERR_PTR(ret);
  256. }