vsp1_wpf.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_pipe.h"
  18. #include "vsp1_rwpf.h"
  19. #include "vsp1_video.h"
  20. #define WPF_GEN2_MAX_WIDTH 2048U
  21. #define WPF_GEN2_MAX_HEIGHT 2048U
  22. #define WPF_GEN3_MAX_WIDTH 8190U
  23. #define WPF_GEN3_MAX_HEIGHT 8190U
  24. /* -----------------------------------------------------------------------------
  25. * Device Access
  26. */
  27. static inline void vsp1_wpf_write(struct vsp1_rwpf *wpf,
  28. struct vsp1_dl_list *dl, u32 reg, u32 data)
  29. {
  30. vsp1_dl_list_write(dl, reg + wpf->entity.index * VI6_WPF_OFFSET, data);
  31. }
  32. /* -----------------------------------------------------------------------------
  33. * V4L2 Subdevice Core Operations
  34. */
  35. static int wpf_s_stream(struct v4l2_subdev *subdev, int enable)
  36. {
  37. struct vsp1_rwpf *wpf = to_rwpf(subdev);
  38. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  39. if (enable)
  40. return 0;
  41. /* Write to registers directly when stopping the stream as there will be
  42. * no pipeline run to apply the display list.
  43. */
  44. vsp1_write(vsp1, VI6_WPF_IRQ_ENB(wpf->entity.index), 0);
  45. vsp1_write(vsp1, wpf->entity.index * VI6_WPF_OFFSET +
  46. VI6_WPF_SRCRPF, 0);
  47. return 0;
  48. }
  49. /* -----------------------------------------------------------------------------
  50. * V4L2 Subdevice Operations
  51. */
  52. static const struct v4l2_subdev_video_ops wpf_video_ops = {
  53. .s_stream = wpf_s_stream,
  54. };
  55. static const struct v4l2_subdev_ops wpf_ops = {
  56. .video = &wpf_video_ops,
  57. .pad = &vsp1_rwpf_pad_ops,
  58. };
  59. /* -----------------------------------------------------------------------------
  60. * VSP1 Entity Operations
  61. */
  62. static void vsp1_wpf_destroy(struct vsp1_entity *entity)
  63. {
  64. struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
  65. vsp1_dlm_destroy(wpf->dlm);
  66. }
  67. static void wpf_set_memory(struct vsp1_entity *entity, struct vsp1_dl_list *dl)
  68. {
  69. struct vsp1_rwpf *wpf = entity_to_rwpf(entity);
  70. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_Y, wpf->mem.addr[0]);
  71. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C0, wpf->mem.addr[1]);
  72. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_ADDR_C1, wpf->mem.addr[2]);
  73. }
  74. static void wpf_configure(struct vsp1_entity *entity,
  75. struct vsp1_pipeline *pipe,
  76. struct vsp1_dl_list *dl)
  77. {
  78. struct vsp1_rwpf *wpf = to_rwpf(&entity->subdev);
  79. struct vsp1_device *vsp1 = wpf->entity.vsp1;
  80. const struct v4l2_mbus_framefmt *source_format;
  81. const struct v4l2_mbus_framefmt *sink_format;
  82. const struct v4l2_rect *crop;
  83. unsigned int i;
  84. u32 outfmt = 0;
  85. u32 srcrpf = 0;
  86. /* Cropping */
  87. crop = vsp1_rwpf_get_crop(wpf, wpf->entity.config);
  88. vsp1_wpf_write(wpf, dl, VI6_WPF_HSZCLIP, VI6_WPF_SZCLIP_EN |
  89. (crop->left << VI6_WPF_SZCLIP_OFST_SHIFT) |
  90. (crop->width << VI6_WPF_SZCLIP_SIZE_SHIFT));
  91. vsp1_wpf_write(wpf, dl, VI6_WPF_VSZCLIP, VI6_WPF_SZCLIP_EN |
  92. (crop->top << VI6_WPF_SZCLIP_OFST_SHIFT) |
  93. (crop->height << VI6_WPF_SZCLIP_SIZE_SHIFT));
  94. /* Format */
  95. sink_format = vsp1_entity_get_pad_format(&wpf->entity,
  96. wpf->entity.config,
  97. RWPF_PAD_SINK);
  98. source_format = vsp1_entity_get_pad_format(&wpf->entity,
  99. wpf->entity.config,
  100. RWPF_PAD_SOURCE);
  101. if (!pipe->lif) {
  102. const struct v4l2_pix_format_mplane *format = &wpf->format;
  103. const struct vsp1_format_info *fmtinfo = wpf->fmtinfo;
  104. outfmt = fmtinfo->hwfmt << VI6_WPF_OUTFMT_WRFMT_SHIFT;
  105. if (fmtinfo->alpha)
  106. outfmt |= VI6_WPF_OUTFMT_PXA;
  107. if (fmtinfo->swap_yc)
  108. outfmt |= VI6_WPF_OUTFMT_SPYCS;
  109. if (fmtinfo->swap_uv)
  110. outfmt |= VI6_WPF_OUTFMT_SPUVS;
  111. /* Destination stride and byte swapping. */
  112. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_Y,
  113. format->plane_fmt[0].bytesperline);
  114. if (format->num_planes > 1)
  115. vsp1_wpf_write(wpf, dl, VI6_WPF_DSTM_STRIDE_C,
  116. format->plane_fmt[1].bytesperline);
  117. vsp1_wpf_write(wpf, dl, VI6_WPF_DSWAP, fmtinfo->swap);
  118. }
  119. if (sink_format->code != source_format->code)
  120. outfmt |= VI6_WPF_OUTFMT_CSC;
  121. outfmt |= wpf->alpha << VI6_WPF_OUTFMT_PDV_SHIFT;
  122. vsp1_wpf_write(wpf, dl, VI6_WPF_OUTFMT, outfmt);
  123. vsp1_dl_list_write(dl, VI6_DPR_WPF_FPORCH(wpf->entity.index),
  124. VI6_DPR_WPF_FPORCH_FP_WPFN);
  125. vsp1_dl_list_write(dl, VI6_WPF_WRBCK_CTRL, 0);
  126. /* Sources. If the pipeline has a single input and BRU is not used,
  127. * configure it as the master layer. Otherwise configure all
  128. * inputs as sub-layers and select the virtual RPF as the master
  129. * layer.
  130. */
  131. for (i = 0; i < vsp1->info->rpf_count; ++i) {
  132. struct vsp1_rwpf *input = pipe->inputs[i];
  133. if (!input)
  134. continue;
  135. srcrpf |= (!pipe->bru && pipe->num_inputs == 1)
  136. ? VI6_WPF_SRCRPF_RPF_ACT_MST(input->entity.index)
  137. : VI6_WPF_SRCRPF_RPF_ACT_SUB(input->entity.index);
  138. }
  139. if (pipe->bru || pipe->num_inputs > 1)
  140. srcrpf |= VI6_WPF_SRCRPF_VIRACT_MST;
  141. vsp1_wpf_write(wpf, dl, VI6_WPF_SRCRPF, srcrpf);
  142. /* Enable interrupts */
  143. vsp1_dl_list_write(dl, VI6_WPF_IRQ_STA(wpf->entity.index), 0);
  144. vsp1_dl_list_write(dl, VI6_WPF_IRQ_ENB(wpf->entity.index),
  145. VI6_WFP_IRQ_ENB_FREE);
  146. }
  147. static const struct vsp1_entity_operations wpf_entity_ops = {
  148. .destroy = vsp1_wpf_destroy,
  149. .set_memory = wpf_set_memory,
  150. .configure = wpf_configure,
  151. };
  152. /* -----------------------------------------------------------------------------
  153. * Initialization and Cleanup
  154. */
  155. struct vsp1_rwpf *vsp1_wpf_create(struct vsp1_device *vsp1, unsigned int index)
  156. {
  157. struct vsp1_rwpf *wpf;
  158. char name[6];
  159. int ret;
  160. wpf = devm_kzalloc(vsp1->dev, sizeof(*wpf), GFP_KERNEL);
  161. if (wpf == NULL)
  162. return ERR_PTR(-ENOMEM);
  163. if (vsp1->info->gen == 2) {
  164. wpf->max_width = WPF_GEN2_MAX_WIDTH;
  165. wpf->max_height = WPF_GEN2_MAX_HEIGHT;
  166. } else {
  167. wpf->max_width = WPF_GEN3_MAX_WIDTH;
  168. wpf->max_height = WPF_GEN3_MAX_HEIGHT;
  169. }
  170. wpf->entity.ops = &wpf_entity_ops;
  171. wpf->entity.type = VSP1_ENTITY_WPF;
  172. wpf->entity.index = index;
  173. sprintf(name, "wpf.%u", index);
  174. ret = vsp1_entity_init(vsp1, &wpf->entity, name, 2, &wpf_ops,
  175. MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
  176. if (ret < 0)
  177. return ERR_PTR(ret);
  178. /* Initialize the display list manager. */
  179. wpf->dlm = vsp1_dlm_create(vsp1, index, 4);
  180. if (!wpf->dlm) {
  181. ret = -ENOMEM;
  182. goto error;
  183. }
  184. /* Initialize the control handler. */
  185. ret = vsp1_rwpf_init_ctrls(wpf);
  186. if (ret < 0) {
  187. dev_err(vsp1->dev, "wpf%u: failed to initialize controls\n",
  188. index);
  189. goto error;
  190. }
  191. return wpf;
  192. error:
  193. vsp1_entity_destroy(&wpf->entity);
  194. return ERR_PTR(ret);
  195. }