vsp1_wpf.c 8.0 KB

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