vsp1_wpf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * Controls
  35. */
  36. static int wpf_s_ctrl(struct v4l2_ctrl *ctrl)
  37. {
  38. struct vsp1_rwpf *wpf =
  39. container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
  40. u32 value;
  41. if (!vsp1_entity_is_streaming(&wpf->entity))
  42. return 0;
  43. switch (ctrl->id) {
  44. case V4L2_CID_ALPHA_COMPONENT:
  45. value = vsp1_wpf_read(wpf, VI6_WPF_OUTFMT);
  46. value &= ~VI6_WPF_OUTFMT_PDV_MASK;
  47. value |= ctrl->val << VI6_WPF_OUTFMT_PDV_SHIFT;
  48. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, value);
  49. break;
  50. }
  51. return 0;
  52. }
  53. static const struct v4l2_ctrl_ops wpf_ctrl_ops = {
  54. .s_ctrl = wpf_s_ctrl,
  55. };
  56. /* -----------------------------------------------------------------------------
  57. * V4L2 Subdevice Core Operations
  58. */
  59. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  60. {
  61. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
  62. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  63. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  64. const struct v4l2_rect *crop = &wpf->crop;
  65. unsigned int i;
  66. u32 srcrpf = 0;
  67. u32 outfmt = 0;
  68. int ret;
  69. ret = vsp1_entity_set_streaming(&wpf->entity, enable);
  70. if (ret < 0)
  71. return ret;
  72. if (!enable) {
  73. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  74. vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, 0);
  75. return 0;
  76. }
  77. /* Sources. If the pipeline has a single input configure it as the
  78. * master layer. Otherwise configure all inputs as sub-layers and
  79. * select the virtual RPF as the master layer.
  80. */
  81. for (i = 0; i < pipe->num_inputs; ++i) {
  82. struct vsp1_rwpf *input = pipe->inputs[i];
  83. srcrpf |= pipe->num_inputs == 1
  84. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  85. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  86. }
  87. if (pipe->num_inputs > 1)
  88. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  89. vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
  90. /* Destination stride. */
  91. if (!pipe->lif) {
  92. struct v4l2_pix_format_mplane *format = &wpf->video.format;
  93. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
  94. format->plane_fmt[0].bytesperline);
  95. if (format->num_planes > 1)
  96. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
  97. format->plane_fmt[1].bytesperline);
  98. }
  99. vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  100. (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
  101. (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  102. vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  103. (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
  104. (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  105. /* Format */
  106. if (!pipe->lif) {
  107. const struct vsp1_format_info *fmtinfo = wpf->video.fmtinfo;
  108. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  109. if (fmtinfo->alpha)
  110. outfmt |= VI6_WPF_OUTFMT_PXA;
  111. if (fmtinfo->swap_yc)
  112. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  113. if (fmtinfo->swap_uv)
  114. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  115. vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
  116. }
  117. if (wpf->entity.formats[RWPF_PAD_SINK].code !=
  118. wpf->entity.formats[RWPF_PAD_SOURCE].code)
  119. outfmt |= VI6_WPF_OUTFMT_CSC;
  120. /* Take the control handler lock to ensure that the PDV value won't be
  121. * changed behind our back by a set control operation.
  122. */
  123. mutex_lock(wpf->ctrls.lock);
  124. outfmt |= vsp1_wpf_read(wpf, VI6_WPF_OUTFMT) & VI6_WPF_OUTFMT_PDV_MASK;
  125. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
  126. mutex_unlock(wpf->ctrls.lock);
  127. vsp1_write(vsp1, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  128. VI6_DPR_WPF_FPORCH_FP_WPFN);
  129. vsp1_write(vsp1, VI6_WPF_WRBCK_CTRL, 0);
  130. /* Enable interrupts */
  131. vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  132. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
  133. VI6_WFP_IRQ_ENB_FREE);
  134. return 0;
  135. }
  136. /* -----------------------------------------------------------------------------
  137. * V4L2 Subdevice Operations
  138. */
  139. static struct v4l2_subdev_video_ops wpf_video_ops = {
  140. .s_stream = wpf_s_stream,
  141. };
  142. static struct v4l2_subdev_pad_ops wpf_pad_ops = {
  143. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  144. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  145. .get_fmt = vsp1_rwpf_get_format,
  146. .set_fmt = vsp1_rwpf_set_format,
  147. .get_selection = vsp1_rwpf_get_selection,
  148. .set_selection = vsp1_rwpf_set_selection,
  149. };
  150. static struct v4l2_subdev_ops wpf_ops = {
  151. .video = &wpf_video_ops,
  152. .pad = &wpf_pad_ops,
  153. };
  154. /* -----------------------------------------------------------------------------
  155. * Video Device Operations
  156. */
  157. static void wpf_vdev_queue(struct vsp1_video *video,
  158. struct vsp1_video_buffer *buf)
  159. {
  160. struct vsp1_rwpf *wpf = container_of(video, struct vsp1_rwpf, video);
  161. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, buf->addr[0]);
  162. if (buf->buf.num_planes > 1)
  163. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, buf->addr[1]);
  164. if (buf->buf.num_planes > 2)
  165. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, buf->addr[2]);
  166. }
  167. static const struct vsp1_video_operations wpf_vdev_ops = {
  168. .queue = wpf_vdev_queue,
  169. };
  170. /* -----------------------------------------------------------------------------
  171. * Initialization and Cleanup
  172. */
  173. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  174. {
  175. struct v4l2_subdev *subdev;
  176. struct vsp1_video *video;
  177. struct vsp1_rwpf *wpf;
  178. unsigned int flags;
  179. int ret;
  180. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  181. if (wpf == NULL)
  182. return ERR_PTR(-ENOMEM);
  183. wpf->max_width = WPF_MAX_WIDTH;
  184. wpf->max_height = WPF_MAX_HEIGHT;
  185. wpf->entity.type = VSP1_ENTITY_WPF;
  186. wpf->entity.index = index;
  187. ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
  188. if (ret < 0)
  189. return ERR_PTR(ret);
  190. /* Initialize the V4L2 subdev. */
  191. subdev = &wpf->entity.subdev;
  192. v4l2_subdev_init(subdev, &wpf_ops);
  193. subdev->entity.ops = &vsp1_media_ops;
  194. subdev->internal_ops = &vsp1_subdev_internal_ops;
  195. snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
  196. dev_name(vsp1->dev), index);
  197. v4l2_set_subdevdata(subdev, wpf);
  198. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  199. vsp1_entity_init_formats(subdev, NULL);
  200. /* Initialize the control handler. */
  201. v4l2_ctrl_handler_init(&wpf->ctrls, 1);
  202. v4l2_ctrl_new_std(&wpf->ctrls, &wpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
  203. 0, 255, 1, 255);
  204. wpf->entity.subdev.ctrl_handler = &wpf->ctrls;
  205. if (wpf->ctrls.error) {
  206. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  207. index);
  208. ret = wpf->ctrls.error;
  209. goto error;
  210. }
  211. /* Initialize the video device. */
  212. video = &wpf->video;
  213. video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  214. video->vsp1 = vsp1;
  215. video->ops = &wpf_vdev_ops;
  216. ret = vsp1_video_init(video, &wpf->entity);
  217. if (ret < 0)
  218. goto error;
  219. wpf->entity.video = video;
  220. /* Connect the video device to the WPF. All connections are immutable
  221. * except for the WPF0 source link if a LIF is present.
  222. */
  223. flags = MEDIA_LNK_FL_ENABLED;
  224. if (!(vsp1->pdata->features & VSP1_HAS_LIF) || index != 0)
  225. flags |= MEDIA_LNK_FL_IMMUTABLE;
  226. ret = media_entity_create_link(&wpf->entity.subdev.entity,
  227. RWPF_PAD_SOURCE,
  228. &wpf->video.video.entity, 0, flags);
  229. if (ret < 0)
  230. goto error;
  231. wpf->entity.sink = &wpf->video.video.entity;
  232. return wpf;
  233. error:
  234. vsp1_entity_destroy(&wpf->entity);
  235. return ERR_PTR(ret);
  236. }