vsp1_wpf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * vsp1_wpf.c -- R-Car VSP1 Write 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 WPF_MAX_WIDTH 2048
  19. #define WPF_MAX_HEIGHT 2048
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_wpf_read(struct vsp1_rwpf *wpf, u32 reg)
  24. {
  25. return vsp1_read(wpf->entity.vsp1,
  26. reg + wpf->entity.index * VI6_WPF_OFFSET);
  27. }
  28. static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data)
  29. {
  30. vsp1_write(wpf->entity.vsp1,
  31. reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  32. }
  33. /* -----------------------------------------------------------------------------
  34. * V4L2 Subdevice Core Operations
  35. */
  36. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  37. {
  38. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  39. struct vsp1_pipeline *pipe =
  40. to_vsp1_pipeline(&wpf->entity.subdev.entity);
  41. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  42. const struct v4l2_rect *crop = &wpf->crop;
  43. unsigned int i;
  44. u32 srcrpf = 0;
  45. u32 outfmt = 0;
  46. if (!enable) {
  47. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  48. return 0;
  49. }
  50. /* Sources. If the pipeline has a single input configure it as the
  51. * master layer. Otherwise configure all inputs as sub-layers and
  52. * select the virtual RPF as the master layer.
  53. */
  54. for (i = 0; i < pipe->num_inputs; ++i) {
  55. struct vsp1_rwpf *input = pipe->inputs[i];
  56. srcrpf |= pipe->num_inputs == 1
  57. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  58. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  59. }
  60. if (pipe->num_inputs > 1)
  61. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  62. vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
  63. /* Destination stride. */
  64. if (!pipe->lif) {
  65. struct v4l2_pix_format_mplane *format = &wpf->video.format;
  66. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
  67. format->plane_fmt[0].bytesperline);
  68. if (format->num_planes > 1)
  69. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
  70. format->plane_fmt[1].bytesperline);
  71. }
  72. vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  73. (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
  74. (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  75. vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  76. (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
  77. (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  78. /* Format */
  79. if (!pipe->lif) {
  80. const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
  81. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  82. if (fmtinfo->swap_yc)
  83. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  84. if (fmtinfo->swap_uv)
  85. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  86. vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
  87. }
  88. if (wpf->entity.formats[RWPF_PAD_SINK].code !=
  89. wpf->entity.formats[RWPF_PAD_SOURCE].code)
  90. outfmt |= VI6_WPF_OUTFMT_CSC;
  91. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
  92. vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  93. VI6_DPR_WPF_FPORCH_FP_WPFN);
  94. vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
  95. /* Enable interrupts */
  96. vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  97. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
  98. VI6_WFP_IRQ_ENB_FREE);
  99. return 0;
  100. }
  101. /* -----------------------------------------------------------------------------
  102. * V4L2 Subdevice Operations
  103. */
  104. static struct v4l2_subdev_video_ops wpf_video_ops = {
  105. .s_stream = wpf_s_stream,
  106. };
  107. static struct v4l2_subdev_pad_ops wpf_pad_ops = {
  108. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  109. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  110. .get_fmt = vsp1_rwpf_get_format,
  111. .set_fmt = vsp1_rwpf_set_format,
  112. .get_selection = vsp1_rwpf_get_selection,
  113. .set_selection = vsp1_rwpf_set_selection,
  114. };
  115. static struct v4l2_subdev_ops wpf_ops = {
  116. .video = &wpf_video_ops,
  117. .pad = &wpf_pad_ops,
  118. };
  119. /* -----------------------------------------------------------------------------
  120. * Video Device Operations
  121. */
  122. static void wpf_vdev_queue(struct vsp1_video *video,
  123. struct vsp1_video_buffer *buf)
  124. {
  125. struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
  126. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
  127. if (buf->buf.num_planes > 1)
  128. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
  129. if (buf->buf.num_planes > 2)
  130. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
  131. }
  132. static const struct vsp1_video_operations wpf_vdev_ops = {
  133. .queue = wpf_vdev_queue,
  134. };
  135. /* -----------------------------------------------------------------------------
  136. * Initialization and Cleanup
  137. */
  138. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  139. {
  140. struct v4l2_subdev *subdev;
  141. struct vsp1_video *video;
  142. struct vsp1_rwpf *wpf;
  143. unsigned int flags;
  144. int ret;
  145. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  146. if (wpf == NULL)
  147. return ERR_PTR(-ENOMEM);
  148. wpf->max_width = WPF_MAX_WIDTH;
  149. wpf->max_height = WPF_MAX_HEIGHT;
  150. wpf->entity.type = VSP1_ENTITY_WPF;
  151. wpf->entity.index = index;
  152. ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
  153. if (ret < 0)
  154. return ERR_PTR(ret);
  155. /* Initialize the V4L2 subdev. */
  156. subdev = &wpf->entity.subdev;
  157. v4l2_subdev_init(subdev, &wpf_ops);
  158. subdev->entity.ops = &vsp1_media_ops;
  159. subdev->internal_ops = &vsp1_subdev_internal_ops;
  160. snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
  161. dev_name(vsp1->dev), index);
  162. v4l2_set_subdevdata(subdev, wpf);
  163. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  164. vsp1_entity_init_formats(subdev, NULL);
  165. /* Initialize the video device. */
  166. video = &wpf->video;
  167. video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  168. video->vsp1 = vsp1;
  169. video->ops = &wpf_vdev_ops;
  170. ret = vsp1_video_init(video, &wpf->entity);
  171. if (ret < 0)
  172. goto error_video;
  173. /* Connect the video device to the WPF. All connections are immutable
  174. * except for the WPF0 source link if a LIF is present.
  175. */
  176. flags = MEDIA_LNK_FL_ENABLED;
  177. if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
  178. flags |= MEDIA_LNK_FL_IMMUTABLE;
  179. ret = media_entity_create_link(&wpf->entity.subdev.entity,
  180. RWPF_PAD_SOURCE,
  181. &wpf->video.video.entity, 0, flags);
  182. if (ret < 0)
  183. goto error_link;
  184. wpf->entity.sink = &wpf->video.video.entity;
  185. return wpf;
  186. error_link:
  187. vsp1_video_cleanup(video);
  188. error_video:
  189. media_entity_cleanup(&wpf->entity.subdev.entity);
  190. return ERR_PTR(ret);
  191. }