vsp1_rpf.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if (format->num_planes > 1) {
  90. rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
  91. + crop->left * fmtinfo->bpp[1] / 8;
  92. pstride |= format->plane_fmt[1].bytesperline
  93. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  94. }
  95. vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
  96. /* Format */
  97. infmt = VI6_RPF_INFMT_CIPM
  98. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  99. if (fmtinfo->swap_yc)
  100. infmt |= VI6_RPF_INFMT_SPYCS;
  101. if (fmtinfo->swap_uv)
  102. infmt |= VI6_RPF_INFMT_SPUVS;
  103. if (rpf->entity.formats[RWPF_PAD_SINK].code !=
  104. rpf->entity.formats[RWPF_PAD_SOURCE].code)
  105. infmt |= VI6_RPF_INFMT_CSC;
  106. vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
  107. vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
  108. /* Output location */
  109. vsp1_rpf_write(rpf, VI6_RPF_LOC,
  110. (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
  111. (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
  112. /* Use the alpha channel (extended to 8 bits) when available or an
  113. * alpha value set through the V4L2_CID_ALPHA_COMPONENT control
  114. * otherwise. Disable color keying.
  115. */
  116. vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
  117. (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
  118. : VI6_RPF_ALPH_SEL_ASEL_FIXED));
  119. vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
  120. vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
  121. return 0;
  122. }
  123. /* -----------------------------------------------------------------------------
  124. * V4L2 Subdevice Operations
  125. */
  126. static struct v4l2_subdev_video_ops rpf_video_ops = {
  127. .s_stream = rpf_s_stream,
  128. };
  129. static struct v4l2_subdev_pad_ops rpf_pad_ops = {
  130. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  131. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  132. .get_fmt = vsp1_rwpf_get_format,
  133. .set_fmt = vsp1_rwpf_set_format,
  134. .get_selection = vsp1_rwpf_get_selection,
  135. .set_selection = vsp1_rwpf_set_selection,
  136. };
  137. static struct v4l2_subdev_ops rpf_ops = {
  138. .video = &rpf_video_ops,
  139. .pad = &rpf_pad_ops,
  140. };
  141. /* -----------------------------------------------------------------------------
  142. * Video Device Operations
  143. */
  144. static void rpf_vdev_queue(struct vsp1_video *video,
  145. struct vsp1_video_buffer *buf)
  146. {
  147. struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
  148. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
  149. buf->addr[0] + rpf->offsets[0]);
  150. if (buf->buf.num_planes > 1)
  151. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
  152. buf->addr[1] + rpf->offsets[1]);
  153. if (buf->buf.num_planes > 2)
  154. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
  155. buf->addr[2] + rpf->offsets[1]);
  156. }
  157. static const struct vsp1_video_operations rpf_vdev_ops = {
  158. .queue = rpf_vdev_queue,
  159. };
  160. /* -----------------------------------------------------------------------------
  161. * Initialization and Cleanup
  162. */
  163. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  164. {
  165. struct v4l2_subdev *subdev;
  166. struct vsp1_video *video;
  167. struct vsp1_rwpf *rpf;
  168. int ret;
  169. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  170. if (rpf == NULL)
  171. return ERR_PTR(-ENOMEM);
  172. rpf->max_width = RPF_MAX_WIDTH;
  173. rpf->max_height = RPF_MAX_HEIGHT;
  174. rpf->entity.type = VSP1_ENTITY_RPF;
  175. rpf->entity.index = index;
  176. ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
  177. if (ret < 0)
  178. return ERR_PTR(ret);
  179. /* Initialize the V4L2 subdev. */
  180. subdev = &rpf->entity.subdev;
  181. v4l2_subdev_init(subdev, &rpf_ops);
  182. subdev->entity.ops = &vsp1_media_ops;
  183. subdev->internal_ops = &vsp1_subdev_internal_ops;
  184. snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
  185. dev_name(vsp1->dev), index);
  186. v4l2_set_subdevdata(subdev, rpf);
  187. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  188. vsp1_entity_init_formats(subdev, NULL);
  189. /* Initialize the control handler. */
  190. v4l2_ctrl_handler_init(&rpf->ctrls, 1);
  191. v4l2_ctrl_new_std(&rpf->ctrls, &rpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
  192. 0, 255, 1, 255);
  193. rpf->entity.subdev.ctrl_handler = &rpf->ctrls;
  194. if (rpf->ctrls.error) {
  195. dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
  196. index);
  197. ret = rpf->ctrls.error;
  198. goto error;
  199. }
  200. /* Initialize the video device. */
  201. video = &rpf->video;
  202. video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  203. video->vsp1 = vsp1;
  204. video->ops = &rpf_vdev_ops;
  205. ret = vsp1_video_init(video, &rpf->entity);
  206. if (ret < 0)
  207. goto error;
  208. rpf->entity.video = video;
  209. /* Connect the video device to the RPF. */
  210. ret = media_entity_create_link(&rpf->video.video.entity, 0,
  211. &rpf->entity.subdev.entity,
  212. RWPF_PAD_SINK,
  213. MEDIA_LNK_FL_ENABLED |
  214. MEDIA_LNK_FL_IMMUTABLE);
  215. if (ret < 0)
  216. goto error;
  217. return rpf;
  218. error:
  219. vsp1_entity_destroy(&rpf->entity);
  220. return ERR_PTR(ret);
  221. }