vsp1_rpf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_rwpf.h"
  17. #include "vsp1_video.h"
  18. #define RPF_MAX_WIDTH 8190
  19. #define RPF_MAX_HEIGHT 8190
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_rpf_read(struct vsp1_rwpf *rpf, u32 reg)
  24. {
  25. return vsp1_read(rpf->entity.vsp1,
  26. reg + rpf->entity.index * VI6_RPF_OFFSET);
  27. }
  28. static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
  29. {
  30. vsp1_write(rpf->entity.vsp1,
  31. reg + rpf->entity.index * VI6_RPF_OFFSET, data);
  32. }
  33. /* -----------------------------------------------------------------------------
  34. * Controls
  35. */
  36. static int rpf_s_ctrl(struct v4l2_ctrl *ctrl)
  37. {
  38. struct vsp1_rwpf *rpf =
  39. container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  40. struct vsp1_pipeline *pipe;
  41. if (!vsp1_entity_is_streaming(&rpf->entity))
  42. return 0;
  43. switch (ctrl->id) {
  44. case V4L2_CID_ALPHA_COMPONENT:
  45. vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
  46. ctrl->val << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
  47. pipe = to_vsp1_pipeline(&rpf->entity.subdev.entity);
  48. vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, ctrl->val);
  49. break;
  50. }
  51. return 0;
  52. }
  53. static const struct v4l2_ctrl_ops rpf_ctrl_ops = {
  54. .s_ctrl = rpf_s_ctrl,
  55. };
  56. /* -----------------------------------------------------------------------------
  57. * V4L2 Subdevice Core Operations
  58. */
  59. static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
  60. {
  61. struct vsp1_rwpf *rpf = to_rwpf(subdev);
  62. const struct vsp1_format_info *fmtinfo = rpf->video.fmtinfo;
  63. const struct v4l2_pix_format_mplane *format = &rpf->video.format;
  64. const struct v4l2_rect *crop = &rpf->crop;
  65. u32 pstride;
  66. u32 infmt;
  67. int ret;
  68. ret = vsp1_entity_set_streaming(&rpf->entity, enable);
  69. if (ret < 0)
  70. return ret;
  71. if (!enable)
  72. return 0;
  73. /* Source size, stride and crop offsets.
  74. *
  75. * The crop offsets correspond to the location of the crop rectangle top
  76. * left corner in the plane buffer. Only two offsets are needed, as
  77. * planes 2 and 3 always have identical strides.
  78. */
  79. vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
  80. (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  81. (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  82. vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
  83. (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  84. (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  85. rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
  86. + crop->left * fmtinfo->bpp[0] / 8;
  87. pstride = format->plane_fmt[0].bytesperline
  88. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  89. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
  90. rpf->buf_addr[0] + rpf->offsets[0]);
  91. if (format->num_planes > 1) {
  92. rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
  93. + crop->left * fmtinfo->bpp[1] / 8;
  94. pstride |= format->plane_fmt[1].bytesperline
  95. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  96. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
  97. rpf->buf_addr[1] + rpf->offsets[1]);
  98. if (format->num_planes > 2)
  99. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
  100. rpf->buf_addr[2] + rpf->offsets[1]);
  101. }
  102. vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
  103. /* Format */
  104. infmt = VI6_RPF_INFMT_CIPM
  105. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  106. if (fmtinfo->swap_yc)
  107. infmt |= VI6_RPF_INFMT_SPYCS;
  108. if (fmtinfo->swap_uv)
  109. infmt |= VI6_RPF_INFMT_SPUVS;
  110. if (rpf->entity.formats[RWPF_PAD_SINK].code !=
  111. rpf->entity.formats[RWPF_PAD_SOURCE].code)
  112. infmt |= VI6_RPF_INFMT_CSC;
  113. vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
  114. vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
  115. /* Output location */
  116. vsp1_rpf_write(rpf, VI6_RPF_LOC,
  117. (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
  118. (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
  119. /* Use the alpha channel (extended to 8 bits) when available or an
  120. * alpha value set through the V4L2_CID_ALPHA_COMPONENT control
  121. * otherwise. Disable color keying.
  122. */
  123. vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
  124. (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
  125. : VI6_RPF_ALPH_SEL_ASEL_FIXED));
  126. vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
  127. vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
  128. return 0;
  129. }
  130. /* -----------------------------------------------------------------------------
  131. * V4L2 Subdevice Operations
  132. */
  133. static struct v4l2_subdev_video_ops rpf_video_ops = {
  134. .s_stream = rpf_s_stream,
  135. };
  136. static struct v4l2_subdev_pad_ops rpf_pad_ops = {
  137. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  138. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  139. .get_fmt = vsp1_rwpf_get_format,
  140. .set_fmt = vsp1_rwpf_set_format,
  141. .get_selection = vsp1_rwpf_get_selection,
  142. .set_selection = vsp1_rwpf_set_selection,
  143. };
  144. static struct v4l2_subdev_ops rpf_ops = {
  145. .video = &rpf_video_ops,
  146. .pad = &rpf_pad_ops,
  147. };
  148. /* -----------------------------------------------------------------------------
  149. * Video Device Operations
  150. */
  151. static void rpf_vdev_queue(struct vsp1_video *video,
  152. struct vsp1_video_buffer *buf)
  153. {
  154. struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
  155. unsigned int i;
  156. for (i = 0; i < 3; ++i)
  157. rpf->buf_addr[i] = buf->addr[i];
  158. if (!vsp1_entity_is_streaming(&rpf->entity))
  159. return;
  160. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
  161. buf->addr[0] + rpf->offsets[0]);
  162. if (buf->buf.vb2_buf.num_planes > 1)
  163. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
  164. buf->addr[1] + rpf->offsets[1]);
  165. if (buf->buf.vb2_buf.num_planes > 2)
  166. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
  167. buf->addr[2] + rpf->offsets[1]);
  168. }
  169. static const struct vsp1_video_operations rpf_vdev_ops = {
  170. .queue = rpf_vdev_queue,
  171. };
  172. /* -----------------------------------------------------------------------------
  173. * Initialization and Cleanup
  174. */
  175. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  176. {
  177. struct v4l2_subdev *subdev;
  178. struct vsp1_video *video;
  179. struct vsp1_rwpf *rpf;
  180. int ret;
  181. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  182. if (rpf == NULL)
  183. return ERR_PTR(-ENOMEM);
  184. rpf->max_width = RPF_MAX_WIDTH;
  185. rpf->max_height = RPF_MAX_HEIGHT;
  186. rpf->entity.type = VSP1_ENTITY_RPF;
  187. rpf->entity.index = index;
  188. ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
  189. if (ret < 0)
  190. return ERR_PTR(ret);
  191. /* Initialize the V4L2 subdev. */
  192. subdev = &rpf->entity.subdev;
  193. v4l2_subdev_init(subdev, &rpf_ops);
  194. subdev->entity.ops = &vsp1_media_ops;
  195. subdev->internal_ops = &vsp1_subdev_internal_ops;
  196. snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
  197. dev_name(vsp1->dev), index);
  198. v4l2_set_subdevdata(subdev, rpf);
  199. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  200. vsp1_entity_init_formats(subdev, NULL);
  201. /* Initialize the control handler. */
  202. v4l2_ctrl_handler_init(&rpf->ctrls, 1);
  203. v4l2_ctrl_new_std(&rpf->ctrls, &rpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
  204. 0, 255, 1, 255);
  205. rpf->entity.subdev.ctrl_handler = &rpf->ctrls;
  206. if (rpf->ctrls.error) {
  207. dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
  208. index);
  209. ret = rpf->ctrls.error;
  210. goto error;
  211. }
  212. /* Initialize the video device. */
  213. video = &rpf->video;
  214. video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  215. video->vsp1 = vsp1;
  216. video->ops = &rpf_vdev_ops;
  217. ret = vsp1_video_init(video, &rpf->entity);
  218. if (ret < 0)
  219. goto error;
  220. rpf->entity.video = video;
  221. /* Connect the video device to the RPF. */
  222. ret = media_entity_create_link(&rpf->video.video.entity, 0,
  223. &rpf->entity.subdev.entity,
  224. RWPF_PAD_SINK,
  225. MEDIA_LNK_FL_ENABLED |
  226. MEDIA_LNK_FL_IMMUTABLE);
  227. if (ret < 0)
  228. goto error;
  229. return rpf;
  230. error:
  231. vsp1_entity_destroy(&rpf->entity);
  232. return ERR_PTR(ret);
  233. }