vsp1_rpf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * V4L2 Subdevice Core Operations
  35. */
  36. static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
  37. {
  38. struct vsp1_rwpf *rpf = to_rwpf(subdev);
  39. const struct vsp1_format_info *fmtinfo = rpf->video.fmtinfo;
  40. const struct v4l2_pix_format_mplane *format = &rpf->video.format;
  41. const struct v4l2_rect *crop = &rpf->crop;
  42. u32 pstride;
  43. u32 infmt;
  44. if (!enable)
  45. return 0;
  46. /* Source size, stride and crop offsets.
  47. *
  48. * The crop offsets correspond to the location of the crop rectangle top
  49. * left corner in the plane buffer. Only two offsets are needed, as
  50. * planes 2 and 3 always have identical strides.
  51. */
  52. vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
  53. (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  54. (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  55. vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
  56. (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  57. (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  58. rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
  59. + crop->left * fmtinfo->bpp[0] / 8;
  60. pstride = format->plane_fmt[0].bytesperline
  61. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  62. if (format->num_planes > 1) {
  63. rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
  64. + crop->left * fmtinfo->bpp[1] / 8;
  65. pstride |= format->plane_fmt[1].bytesperline
  66. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  67. }
  68. vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
  69. /* Format */
  70. infmt = VI6_RPF_INFMT_CIPM
  71. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  72. if (fmtinfo->swap_yc)
  73. infmt |= VI6_RPF_INFMT_SPYCS;
  74. if (fmtinfo->swap_uv)
  75. infmt |= VI6_RPF_INFMT_SPUVS;
  76. if (rpf->entity.formats[RWPF_PAD_SINK].code !=
  77. rpf->entity.formats[RWPF_PAD_SOURCE].code)
  78. infmt |= VI6_RPF_INFMT_CSC;
  79. vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
  80. vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
  81. /* Output location */
  82. vsp1_rpf_write(rpf, VI6_RPF_LOC,
  83. (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
  84. (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
  85. /* Disable alpha, mask and color key. Set the alpha channel to a fixed
  86. * value of 255.
  87. */
  88. vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_ASEL_FIXED);
  89. vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
  90. 255 << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
  91. vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
  92. vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
  93. return 0;
  94. }
  95. /* -----------------------------------------------------------------------------
  96. * V4L2 Subdevice Operations
  97. */
  98. static struct v4l2_subdev_video_ops rpf_video_ops = {
  99. .s_stream = rpf_s_stream,
  100. };
  101. static struct v4l2_subdev_pad_ops rpf_pad_ops = {
  102. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  103. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  104. .get_fmt = vsp1_rwpf_get_format,
  105. .set_fmt = vsp1_rwpf_set_format,
  106. .get_selection = vsp1_rwpf_get_selection,
  107. .set_selection = vsp1_rwpf_set_selection,
  108. };
  109. static struct v4l2_subdev_ops rpf_ops = {
  110. .video = &rpf_video_ops,
  111. .pad = &rpf_pad_ops,
  112. };
  113. /* -----------------------------------------------------------------------------
  114. * Video Device Operations
  115. */
  116. static void rpf_vdev_queue(struct vsp1_video *video,
  117. struct vsp1_video_buffer *buf)
  118. {
  119. struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
  120. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
  121. buf->addr[0] + rpf->offsets[0]);
  122. if (buf->buf.num_planes > 1)
  123. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
  124. buf->addr[1] + rpf->offsets[1]);
  125. if (buf->buf.num_planes > 2)
  126. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
  127. buf->addr[2] + rpf->offsets[1]);
  128. }
  129. static const struct vsp1_video_operations rpf_vdev_ops = {
  130. .queue = rpf_vdev_queue,
  131. };
  132. /* -----------------------------------------------------------------------------
  133. * Initialization and Cleanup
  134. */
  135. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  136. {
  137. struct v4l2_subdev *subdev;
  138. struct vsp1_video *video;
  139. struct vsp1_rwpf *rpf;
  140. int ret;
  141. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  142. if (rpf == NULL)
  143. return ERR_PTR(-ENOMEM);
  144. rpf->max_width = RPF_MAX_WIDTH;
  145. rpf->max_height = RPF_MAX_HEIGHT;
  146. rpf->entity.type = VSP1_ENTITY_RPF;
  147. rpf->entity.index = index;
  148. ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
  149. if (ret < 0)
  150. return ERR_PTR(ret);
  151. /* Initialize the V4L2 subdev. */
  152. subdev = &rpf->entity.subdev;
  153. v4l2_subdev_init(subdev, &rpf_ops);
  154. subdev->entity.ops = &vsp1_media_ops;
  155. subdev->internal_ops = &vsp1_subdev_internal_ops;
  156. snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
  157. dev_name(vsp1->dev), index);
  158. v4l2_set_subdevdata(subdev, rpf);
  159. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  160. vsp1_entity_init_formats(subdev, NULL);
  161. /* Initialize the video device. */
  162. video = &rpf->video;
  163. video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  164. video->vsp1 = vsp1;
  165. video->ops = &rpf_vdev_ops;
  166. ret = vsp1_video_init(video, &rpf->entity);
  167. if (ret < 0)
  168. goto error_video;
  169. /* Connect the video device to the RPF. */
  170. ret = media_entity_create_link(&rpf->video.video.entity, 0,
  171. &rpf->entity.subdev.entity,
  172. RWPF_PAD_SINK,
  173. MEDIA_LNK_FL_ENABLED |
  174. MEDIA_LNK_FL_IMMUTABLE);
  175. if (ret < 0)
  176. goto error_link;
  177. return rpf;
  178. error_link:
  179. vsp1_video_cleanup(video);
  180. error_video:
  181. media_entity_cleanup(&rpf->entity.subdev.entity);
  182. return ERR_PTR(ret);
  183. }