vsp1_wpf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. int ret;
  42. ret = vsp1_entity_set_streaming(&wpf->entity, enable);
  43. if (ret < 0)
  44. return ret;
  45. if (!enable) {
  46. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  47. vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
  48. VI6_WPF_SRCRPF, 0);
  49. return 0;
  50. }
  51. /* Sources. If the pipeline has a single input and BRU is not used,
  52. * configure it as the master layer. Otherwise configure all
  53. * inputs as sub-layers and select the virtual RPF as the master
  54. * layer.
  55. */
  56. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  57. struct vsp1_rwpf *input = pipe->inputs[i];
  58. if (!input)
  59. continue;
  60. srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
  61. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  62. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  63. }
  64. if (pipe->bru || pipe->num_inputs > 1)
  65. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  66. vsp1_wpf_write(wpf, VI6_WPF_SRCRPF, srcrpf);
  67. /* Destination stride. */
  68. if (!pipe->lif) {
  69. struct v4l2_pix_format_mplane *format = &wpf->format;
  70. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_Y,
  71. format->plane_fmt[0].bytesperline);
  72. if (format->num_planes > 1)
  73. vsp1_wpf_write(wpf, VI6_WPF_DSTM_STRIDE_C,
  74. format->plane_fmt[1].bytesperline);
  75. }
  76. vsp1_wpf_write(wpf, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  77. (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
  78. (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  79. vsp1_wpf_write(wpf, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  80. (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
  81. (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  82. /* Format */
  83. if (!pipe->lif) {
  84. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  85. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  86. if (fmtinfo->alpha)
  87. outfmt |= VI6_WPF_OUTFMT_PXA;
  88. if (fmtinfo->swap_yc)
  89. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  90. if (fmtinfo->swap_uv)
  91. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  92. vsp1_wpf_write(wpf, VI6_WPF_DSWAP, fmtinfo->swap);
  93. }
  94. if (wpf->entity.formats[RWPF_PAD_SINK].code !=
  95. wpf->entity.formats[RWPF_PAD_SOURCE].code)
  96. outfmt |= VI6_WPF_OUTFMT_CSC;
  97. outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
  98. vsp1_wpf_write(wpf, VI6_WPF_OUTFMT, outfmt);
  99. vsp1_mod_write(&wpf->entity, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  100. VI6_DPR_WPF_FPORCH_FP_WPFN);
  101. vsp1_mod_write(&wpf->entity, VI6_WPF_WRBCK_CTRL, 0);
  102. /* Enable interrupts */
  103. vsp1_write(vsp1, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  104. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index),
  105. VI6_WFP_IRQ_ENB_FREE);
  106. return 0;
  107. }
  108. /* -----------------------------------------------------------------------------
  109. * V4L2 Subdevice Operations
  110. */
  111. static struct v4l2_subdev_video_ops wpf_video_ops = {
  112. .s_stream = wpf_s_stream,
  113. };
  114. static struct v4l2_subdev_pad_ops wpf_pad_ops = {
  115. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  116. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  117. .get_fmt = vsp1_rwpf_get_format,
  118. .set_fmt = vsp1_rwpf_set_format,
  119. .get_selection = vsp1_rwpf_get_selection,
  120. .set_selection = vsp1_rwpf_set_selection,
  121. };
  122. static struct v4l2_subdev_ops wpf_ops = {
  123. .video = &wpf_video_ops,
  124. .pad = &wpf_pad_ops,
  125. };
  126. /* -----------------------------------------------------------------------------
  127. * Video Device Operations
  128. */
  129. static void wpf_set_memory(struct vsp1_rwpf *wpf, struct vsp1_rwpf_memory *mem)
  130. {
  131. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_Y, mem->addr[0]);
  132. if (mem->num_planes > 1)
  133. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C0, mem->addr[1]);
  134. if (mem->num_planes > 2)
  135. vsp1_wpf_write(wpf, VI6_WPF_DSTM_ADDR_C1, mem->addr[2]);
  136. }
  137. static const struct vsp1_rwpf_operations wpf_vdev_ops = {
  138. .set_memory = wpf_set_memory,
  139. };
  140. /* -----------------------------------------------------------------------------
  141. * Initialization and Cleanup
  142. */
  143. static void vsp1_wpf_destroy(struct vsp1_entity *entity)
  144. {
  145. struct vsp1_rwpf *wpf = container_of(entity, struct vsp1_rwpf, entity);
  146. vsp1_dlm_destroy(wpf->dlm);
  147. }
  148. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  149. {
  150. struct v4l2_subdev *subdev;
  151. struct vsp1_rwpf *wpf;
  152. int ret;
  153. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  154. if (wpf == NULL)
  155. return ERR_PTR(-ENOMEM);
  156. wpf->ops = &wpf_vdev_ops;
  157. wpf->max_width = WPF_MAX_WIDTH;
  158. wpf->max_height = WPF_MAX_HEIGHT;
  159. wpf->entity.destroy = vsp1_wpf_destroy;
  160. wpf->entity.type = VSP1_ENTITY_WPF;
  161. wpf->entity.index = index;
  162. ret = vsp1_entity_init(vsp1, &wpf->entity, 2);
  163. if (ret < 0)
  164. return ERR_PTR(ret);
  165. /* Initialize the display list manager if the WPF is used for display */
  166. if ((vsp1->info->features & VSP1_HAS_LIF) && index == 0) {
  167. wpf->dlm = vsp1_dlm_create(vsp1, 4);
  168. if (!wpf->dlm) {
  169. ret = -ENOMEM;
  170. goto error;
  171. }
  172. }
  173. /* Initialize the V4L2 subdev. */
  174. subdev = &wpf->entity.subdev;
  175. v4l2_subdev_init(subdev, &wpf_ops);
  176. subdev->entity.ops = &vsp1->media_ops;
  177. subdev->internal_ops = &vsp1_subdev_internal_ops;
  178. snprintf(subdev->name, sizeof(subdev->name), "%s wpf.%u",
  179. dev_name(vsp1->dev), index);
  180. v4l2_set_subdevdata(subdev, wpf);
  181. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  182. vsp1_entity_init_formats(subdev, NULL);
  183. /* Initialize the control handler. */
  184. ret = vsp1_rwpf_init_ctrls(wpf);
  185. if (ret < 0) {
  186. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  187. index);
  188. goto error;
  189. }
  190. return wpf;
  191. error:
  192. vsp1_entity_destroy(&wpf->entity);
  193. return ERR_PTR(ret);
  194. }