vsp1_wpf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * vsp1_wpf.c -- R-Car VSP1 Write Pixel Formatter
  3. *
  4. * Copyright (C) 2013 Renesas 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_mbus_framefmt *format =
  43. &wpf->entity.formats[RWPF_PAD_SOURCE];
  44. unsigned int i;
  45. u32 srcrpf = 0;
  46. u32 outfmt = 0;
  47. if (!enable) {
  48. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  49. return 0;
  50. }
  51. /* Sources */
  52. for (i = 0; i < pipe->num_inputs; ++i) {
  53. struct vsp1_rwpf *input = pipe->inputs[i];
  54. srcrpf |= VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index);
  55. }
  56. vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
  57. /* Destination stride. Cropping isn't supported yet. */
  58. if (!pipe->lif) {
  59. struct v4l2_pix_format_mplane *format = &wpf->video.format;
  60. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
  61. format->plane_fmt[0].bytesperline);
  62. if (format->num_planes > 1)
  63. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
  64. format->plane_fmt[1].bytesperline);
  65. }
  66. vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP,
  67. format->width << VI6_WPF_SZCLIP_SIZE_SHIFT);
  68. vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP,
  69. format->height << VI6_WPF_SZCLIP_SIZE_SHIFT);
  70. /* Format */
  71. if (!pipe->lif) {
  72. const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
  73. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  74. if (fmtinfo->swap_yc)
  75. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  76. if (fmtinfo->swap_uv)
  77. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  78. vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
  79. }
  80. if (wpf->entity.formats[RWPF_PAD_SINK].code !=
  81. wpf->entity.formats[RWPF_PAD_SOURCE].code)
  82. outfmt |= VI6_WPF_OUTFMT_CSC;
  83. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
  84. vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  85. VI6_DPR_WPF_FPORCH_FP_WPFN);
  86. vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
  87. /* Enable interrupts */
  88. vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  89. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
  90. VI6_WFP_IRQ_ENB_FREE);
  91. return 0;
  92. }
  93. /* -----------------------------------------------------------------------------
  94. * V4L2 Subdevice Operations
  95. */
  96. static struct v4l2_subdev_video_ops wpf_video_ops = {
  97. .s_stream = wpf_s_stream,
  98. };
  99. static struct v4l2_subdev_pad_ops wpf_pad_ops = {
  100. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  101. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  102. .get_fmt = vsp1_rwpf_get_format,
  103. .set_fmt = vsp1_rwpf_set_format,
  104. };
  105. static struct v4l2_subdev_ops wpf_ops = {
  106. .video = &wpf_video_ops,
  107. .pad = &wpf_pad_ops,
  108. };
  109. /* -----------------------------------------------------------------------------
  110. * Video Device Operations
  111. */
  112. static void wpf_vdev_queue(struct vsp1_video *video,
  113. struct vsp1_video_buffer *buf)
  114. {
  115. struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
  116. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
  117. if (buf->buf.num_planes > 1)
  118. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
  119. if (buf->buf.num_planes > 2)
  120. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
  121. }
  122. static const struct vsp1_video_operations wpf_vdev_ops = {
  123. .queue = wpf_vdev_queue,
  124. };
  125. /* -----------------------------------------------------------------------------
  126. * Initialization and Cleanup
  127. */
  128. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  129. {
  130. struct v4l2_subdev *subdev;
  131. struct vsp1_video *video;
  132. struct vsp1_rwpf *wpf;
  133. unsigned int flags;
  134. int ret;
  135. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  136. if (wpf == NULL)
  137. return ERR_PTR(-ENOMEM);
  138. wpf->max_width = WPF_MAX_WIDTH;
  139. wpf->max_height = WPF_MAX_HEIGHT;
  140. wpf->entity.type = VSP1_ENTITY_WPF;
  141. wpf->entity.index = index;
  142. wpf->entity.id = VI6_DPR_NODE_WPF(index);
  143. ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
  144. if (ret < 0)
  145. return ERR_PTR(ret);
  146. /* Initialize the V4L2 subdev. */
  147. subdev = &wpf->entity.subdev;
  148. v4l2_subdev_init(subdev, &wpf_ops);
  149. subdev->entity.ops = &vsp1_media_ops;
  150. subdev->internal_ops = &vsp1_subdev_internal_ops;
  151. snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
  152. dev_name(vsp1->dev), index);
  153. v4l2_set_subdevdata(subdev, wpf);
  154. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  155. vsp1_entity_init_formats(subdev, NULL);
  156. /* Initialize the video device. */
  157. video = &wpf->video;
  158. video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  159. video->vsp1 = vsp1;
  160. video->ops = &wpf_vdev_ops;
  161. ret = vsp1_video_init(video, &wpf->entity);
  162. if (ret < 0)
  163. goto error_video;
  164. /* Connect the video device to the WPF. All connections are immutable
  165. * except for the WPF0 source link if a LIF is present.
  166. */
  167. flags = MEDIA_LNK_FL_ENABLED;
  168. if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
  169. flags |= MEDIA_LNK_FL_IMMUTABLE;
  170. ret = media_entity_create_link(&wpf->entity.subdev.entity,
  171. RWPF_PAD_SOURCE,
  172. &wpf->video.video.entity, 0, flags);
  173. if (ret < 0)
  174. goto error_link;
  175. wpf->entity.sink = &wpf->video.video.entity;
  176. return wpf;
  177. error_link:
  178. vsp1_video_cleanup(video);
  179. error_video:
  180. media_entity_cleanup(&wpf->entity.subdev.entity);
  181. return ERR_PTR(ret);
  182. }