vsp1_wpf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_dl.h"
  17. #include "vsp1_rwpf.h"
  18. #include "vsp1_video.h"
  19. #define WPF_MAX_WIDTH 2048
  20. #define WPF_MAX_HEIGHT 2048
  21. /* -----------------------------------------------------------------------------
  22. * Device Access
  23. */
  24. static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf, u32 reg, u32 data)
  25. {
  26. vsp1_mod_write(&wpf->entity,
  27. reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  28. }
  29. /* -----------------------------------------------------------------------------
  30. * V4L2 Subdevice Core Operations
  31. */
  32. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  33. {
  34. struct vsp1_pipeline *pipe = to_vsp1_pipeline(&subdev->entity);
  35. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  36. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  37. const struct v4l2_rect *crop = &wpf->crop;
  38. unsigned int i;
  39. u32 srcrpf = 0;
  40. u32 outfmt = 0;
  41. if (!enable) {
  42. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  43. vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
  44. VI6_WPF_SRCRPF, 0);
  45. return 0;
  46. }
  47. /* Sources. If the pipeline has a single input and BRU is not used,
  48. * configure it as the master layer. Otherwise configure all
  49. * inputs as sub-layers and select the virtual RPF as the master
  50. * layer.
  51. */
  52. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  53. struct vsp1_rwpf *input = pipe->inputs[i];
  54. if (!input)
  55. continue;
  56. srcrpf |= (!pipe->bru && 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->bru || 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->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->fmtinfo;
  81. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  82. if (fmtinfo->alpha)
  83. outfmt |= VI6_WPF_OUTFMT_PXA;
  84. if (fmtinfo->swap_yc)
  85. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  86. if (fmtinfo->swap_uv)
  87. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  88. vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
  89. }
  90. if (wpf->entity.formats[RWPF_PAD_SINK].code !=
  91. wpf->entity.formats[RWPF_PAD_SOURCE].code)
  92. outfmt |= VI6_WPF_OUTFMT_CSC;
  93. outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
  94. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
  95. vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  96. VI6_DPR_WPF_FPORCH_FP_WPFN);
  97. vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0);
  98. /* Enable interrupts */
  99. vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  100. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
  101. VI6_WFP_IRQ_ENB_FREE);
  102. return 0;
  103. }
  104. /* -----------------------------------------------------------------------------
  105. * V4L2 Subdevice Operations
  106. */
  107. static struct v4l2_subdev_video_ops wpf_video_ops = {
  108. .s_stream = wpf_s_stream,
  109. };
  110. static struct v4l2_subdev_pad_ops wpf_pad_ops = {
  111. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  112. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  113. .get_fmt = vsp1_rwpf_get_format,
  114. .set_fmt = vsp1_rwpf_set_format,
  115. .get_selection = vsp1_rwpf_get_selection,
  116. .set_selection = vsp1_rwpf_set_selection,
  117. };
  118. static struct v4l2_subdev_ops wpf_ops = {
  119. .video = &wpf_video_ops,
  120. .pad = &wpf_pad_ops,
  121. };
  122. /* -----------------------------------------------------------------------------
  123. * Video Device Operations
  124. */
  125. static void wpf_set_memory(struct vsp1_rwpf *wpf)
  126. {
  127. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, wpf->buf_addr[0]);
  128. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, wpf->buf_addr[1]);
  129. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, wpf->buf_addr[2]);
  130. }
  131. static const struct vsp1_rwpf_operations wpf_vdev_ops = {
  132. .set_memory = wpf_set_memory,
  133. };
  134. /* -----------------------------------------------------------------------------
  135. * Initialization and Cleanup
  136. */
  137. static void vsp1_wpf_destroy(struct vsp1_entity *entity)
  138. {
  139. struct vsp1_rwpf *wpf = container_of(entity, struct vsp1_rwpf, entity);
  140. vsp1_dlm_destroy(wpf->dlm);
  141. }
  142. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  143. {
  144. struct v4l2_subdev *subdev;
  145. struct vsp1_rwpf *wpf;
  146. int ret;
  147. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  148. if (wpf == NULL)
  149. return ERR_PTR(-ENOMEM);
  150. wpf->ops = &wpf_vdev_ops;
  151. wpf->max_width = WPF_MAX_WIDTH;
  152. wpf->max_height = WPF_MAX_HEIGHT;
  153. wpf->entity.destroy = vsp1_wpf_destroy;
  154. wpf->entity.type = VSP1_ENTITY_WPF;
  155. wpf->entity.index = index;
  156. ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
  157. if (ret < 0)
  158. return ERR_PTR(ret);
  159. /* Initialize the display list manager if the WPF is used for display */
  160. if ((vsp1->info->features & VSP1_HAS_LIF) && index == 0) {
  161. wpf->dlm = vsp1_dlm_create(vsp1, 4);
  162. if (!wpf->dlm) {
  163. ret = -ENOMEM;
  164. goto error;
  165. }
  166. }
  167. /* Initialize the V4L2 subdev. */
  168. subdev = &wpf->entity.subdev;
  169. v4l2_subdev_init(subdev, &wpf_ops);
  170. subdev->entity.ops = &vsp1->media_ops;
  171. subdev->internal_ops = &vsp1_subdev_internal_ops;
  172. snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
  173. dev_name(vsp1->dev), index);
  174. v4l2_set_subdevdata(subdev, wpf);
  175. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  176. vsp1_entity_init_formats(subdev, NULL);
  177. /* Initialize the control handler. */
  178. ret = vsp1_rwpf_init_ctrls(wpf);
  179. if (ret < 0) {
  180. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  181. index);
  182. goto error;
  183. }
  184. return wpf;
  185. error:
  186. vsp1_entity_destroy(&wpf->entity);
  187. return ERR_PTR(ret);
  188. }