vsp1_rpf.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. unsigned int offsets[2];
  63. struct v4l2_rect crop;
  64. /*
  65. * Source size and crop offsets.
  66. *
  67. * The crop offsets correspond to the location of the crop
  68. * rectangle top left corner in the plane buffer. Only two
  69. * offsets are needed, as planes 2 and 3 always have identical
  70. * strides.
  71. */
  72. crop = *vsp1_rwpf_get_crop(rpf, rpf->entity.config);
  73. /*
  74. * Partition Algorithm Control
  75. *
  76. * The partition algorithm can split this frame into multiple
  77. * slices. We must scale our partition window based on the pipe
  78. * configuration to match the destination partition window.
  79. * To achieve this, we adjust our crop to provide a 'sub-crop'
  80. * matching the expected partition window. Only 'left' and
  81. * 'width' need to be adjusted.
  82. */
  83. if (pipe->partitions > 1) {
  84. const struct v4l2_mbus_framefmt *output;
  85. struct vsp1_entity *wpf = &pipe->output->entity;
  86. unsigned int input_width = crop.width;
  87. /*
  88. * Scale the partition window based on the configuration
  89. * of the pipeline.
  90. */
  91. output = vsp1_entity_get_pad_format(wpf, wpf->config,
  92. RWPF_PAD_SOURCE);
  93. crop.width = pipe->partition.width * input_width
  94. / output->width;
  95. crop.left += pipe->partition.left * input_width
  96. / output->width;
  97. }
  98. vsp1_rpf_write(rpf, dl, VI6_RPF_SRC_BSIZE,
  99. (crop.width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  100. (crop.height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  101. vsp1_rpf_write(rpf, dl, VI6_RPF_SRC_ESIZE,
  102. (crop.width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  103. (crop.height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  104. offsets[0] = crop.top * format->plane_fmt[0].bytesperline
  105. + crop.left * fmtinfo->bpp[0] / 8;
  106. if (format->num_planes > 1)
  107. offsets[1] = crop.top * format->plane_fmt[1].bytesperline
  108. + crop.left / fmtinfo->hsub
  109. * fmtinfo->bpp[1] / 8;
  110. else
  111. offsets[1] = 0;
  112. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_Y,
  113. rpf->mem.addr[0] + offsets[0]);
  114. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_C0,
  115. rpf->mem.addr[1] + offsets[1]);
  116. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_ADDR_C1,
  117. rpf->mem.addr[2] + offsets[1]);
  118. return;
  119. }
  120. /* Stride */
  121. pstride = format->plane_fmt[0].bytesperline
  122. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  123. if (format->num_planes > 1)
  124. pstride |= format->plane_fmt[1].bytesperline
  125. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  126. vsp1_rpf_write(rpf, dl, VI6_RPF_SRCM_PSTRIDE, pstride);
  127. /* Format */
  128. sink_format = vsp1_entity_get_pad_format(&rpf->entity,
  129. rpf->entity.config,
  130. RWPF_PAD_SINK);
  131. source_format = vsp1_entity_get_pad_format(&rpf->entity,
  132. rpf->entity.config,
  133. RWPF_PAD_SOURCE);
  134. infmt = VI6_RPF_INFMT_CIPM
  135. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  136. if (fmtinfo->swap_yc)
  137. infmt |= VI6_RPF_INFMT_SPYCS;
  138. if (fmtinfo->swap_uv)
  139. infmt |= VI6_RPF_INFMT_SPUVS;
  140. if (sink_format->code != source_format->code)
  141. infmt |= VI6_RPF_INFMT_CSC;
  142. vsp1_rpf_write(rpf, dl, VI6_RPF_INFMT, infmt);
  143. vsp1_rpf_write(rpf, dl, VI6_RPF_DSWAP, fmtinfo->swap);
  144. /* Output location */
  145. if (pipe->bru) {
  146. const struct v4l2_rect *compose;
  147. compose = vsp1_entity_get_pad_selection(pipe->bru,
  148. pipe->bru->config,
  149. rpf->bru_input,
  150. V4L2_SEL_TGT_COMPOSE);
  151. left = compose->left;
  152. top = compose->top;
  153. }
  154. vsp1_rpf_write(rpf, dl, VI6_RPF_LOC,
  155. (left << VI6_RPF_LOC_HCOORD_SHIFT) |
  156. (top << VI6_RPF_LOC_VCOORD_SHIFT));
  157. /* On Gen2 use the alpha channel (extended to 8 bits) when available or
  158. * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
  159. * otherwise.
  160. *
  161. * The Gen3 RPF has extended alpha capability and can both multiply the
  162. * alpha channel by a fixed global alpha value, and multiply the pixel
  163. * components to convert the input to premultiplied alpha.
  164. *
  165. * As alpha premultiplication is available in the BRU for both Gen2 and
  166. * Gen3 we handle it there and use the Gen3 alpha multiplier for global
  167. * alpha multiplication only. This however prevents conversion to
  168. * premultiplied alpha if no BRU is present in the pipeline. If that use
  169. * case turns out to be useful we will revisit the implementation (for
  170. * Gen3 only).
  171. *
  172. * We enable alpha multiplication on Gen3 using the fixed alpha value
  173. * set through the V4L2_CID_ALPHA_COMPONENT control when the input
  174. * contains an alpha channel. On Gen2 the global alpha is ignored in
  175. * that case.
  176. *
  177. * In all cases, disable color keying.
  178. */
  179. vsp1_rpf_write(rpf, dl, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
  180. (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
  181. : VI6_RPF_ALPH_SEL_ASEL_FIXED));
  182. if (entity->vsp1->info->gen == 3) {
  183. u32 mult;
  184. if (fmtinfo->alpha) {
  185. /* When the input contains an alpha channel enable the
  186. * alpha multiplier. If the input is premultiplied we
  187. * need to multiply both the alpha channel and the pixel
  188. * components by the global alpha value to keep them
  189. * premultiplied. Otherwise multiply the alpha channel
  190. * only.
  191. */
  192. bool premultiplied = format->flags
  193. & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
  194. mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO
  195. | (premultiplied ?
  196. VI6_RPF_MULT_ALPHA_P_MMD_RATIO :
  197. VI6_RPF_MULT_ALPHA_P_MMD_NONE);
  198. } else {
  199. /* When the input doesn't contain an alpha channel the
  200. * global alpha value is applied in the unpacking unit,
  201. * the alpha multiplier isn't needed and must be
  202. * disabled.
  203. */
  204. mult = VI6_RPF_MULT_ALPHA_A_MMD_NONE
  205. | VI6_RPF_MULT_ALPHA_P_MMD_NONE;
  206. }
  207. rpf->mult_alpha = mult;
  208. }
  209. vsp1_rpf_write(rpf, dl, VI6_RPF_MSK_CTRL, 0);
  210. vsp1_rpf_write(rpf, dl, VI6_RPF_CKEY_CTRL, 0);
  211. }
  212. static const struct vsp1_entity_operations rpf_entity_ops = {
  213. .configure = rpf_configure,
  214. };
  215. /* -----------------------------------------------------------------------------
  216. * Initialization and Cleanup
  217. */
  218. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  219. {
  220. struct vsp1_rwpf *rpf;
  221. char name[6];
  222. int ret;
  223. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  224. if (rpf == NULL)
  225. return ERR_PTR(-ENOMEM);
  226. rpf->max_width = RPF_MAX_WIDTH;
  227. rpf->max_height = RPF_MAX_HEIGHT;
  228. rpf->entity.ops = &rpf_entity_ops;
  229. rpf->entity.type = VSP1_ENTITY_RPF;
  230. rpf->entity.index = index;
  231. sprintf(name, "rpf.%u", index);
  232. ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops,
  233. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  234. if (ret < 0)
  235. return ERR_PTR(ret);
  236. /* Initialize the control handler. */
  237. ret = vsp1_rwpf_init_ctrls(rpf, 0);
  238. if (ret < 0) {
  239. dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
  240. index);
  241. goto error;
  242. }
  243. v4l2_ctrl_handler_setup(&rpf->ctrls);
  244. return rpf;
  245. error:
  246. vsp1_entity_destroy(&rpf->entity);
  247. return ERR_PTR(ret);
  248. }