vsp1_rpf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
  3. *
  4. * Copyright (C) 2013 Renesas 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 RPF_MAX_WIDTH 8190
  19. #define RPF_MAX_HEIGHT 8190
  20. /* -----------------------------------------------------------------------------
  21. * Device Access
  22. */
  23. static inline u32 vsp1_rpf_read(struct vsp1_rwpf *rpf, u32 reg)
  24. {
  25. return vsp1_read(rpf->entity.vsp1,
  26. reg + rpf->entity.index * VI6_RPF_OFFSET);
  27. }
  28. static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
  29. {
  30. vsp1_write(rpf->entity.vsp1,
  31. reg + rpf->entity.index * VI6_RPF_OFFSET, data);
  32. }
  33. /* -----------------------------------------------------------------------------
  34. * V4L2 Subdevice Core Operations
  35. */
  36. static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
  37. {
  38. struct vsp1_rwpf *rpf = to_rwpf(subdev);
  39. const struct vsp1_format_info *fmtinfo = rpf->video.fmtinfo;
  40. const struct v4l2_pix_format_mplane *format = &rpf->video.format;
  41. u32 pstride;
  42. u32 infmt;
  43. if (!enable)
  44. return 0;
  45. /* Source size and stride. Cropping isn't supported yet. */
  46. vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
  47. (format->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
  48. (format->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
  49. vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
  50. (format->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
  51. (format->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
  52. pstride = format->plane_fmt[0].bytesperline
  53. << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
  54. if (format->num_planes > 1)
  55. pstride |= format->plane_fmt[1].bytesperline
  56. << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
  57. vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
  58. /* Format */
  59. infmt = VI6_RPF_INFMT_CIPM
  60. | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
  61. if (fmtinfo->swap_yc)
  62. infmt |= VI6_RPF_INFMT_SPYCS;
  63. if (fmtinfo->swap_uv)
  64. infmt |= VI6_RPF_INFMT_SPUVS;
  65. if (rpf->entity.formats[RWPF_PAD_SINK].code !=
  66. rpf->entity.formats[RWPF_PAD_SOURCE].code)
  67. infmt |= VI6_RPF_INFMT_CSC;
  68. vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
  69. vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
  70. /* Output location. Composing isn't supported yet. */
  71. vsp1_rpf_write(rpf, VI6_RPF_LOC, 0);
  72. /* Disable alpha, mask and color key. Set the alpha channel to a fixed
  73. * value of 255.
  74. */
  75. vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_ASEL_FIXED);
  76. vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
  77. 255 << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
  78. vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
  79. vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
  80. return 0;
  81. }
  82. /* -----------------------------------------------------------------------------
  83. * V4L2 Subdevice Operations
  84. */
  85. static struct v4l2_subdev_video_ops rpf_video_ops = {
  86. .s_stream = rpf_s_stream,
  87. };
  88. static struct v4l2_subdev_pad_ops rpf_pad_ops = {
  89. .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
  90. .enum_frame_size = vsp1_rwpf_enum_frame_size,
  91. .get_fmt = vsp1_rwpf_get_format,
  92. .set_fmt = vsp1_rwpf_set_format,
  93. };
  94. static struct v4l2_subdev_ops rpf_ops = {
  95. .video = &rpf_video_ops,
  96. .pad = &rpf_pad_ops,
  97. };
  98. /* -----------------------------------------------------------------------------
  99. * Video Device Operations
  100. */
  101. static void rpf_vdev_queue(struct vsp1_video *video,
  102. struct vsp1_video_buffer *buf)
  103. {
  104. struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
  105. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y, buf->addr[0]);
  106. if (buf->buf.num_planes > 1)
  107. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0, buf->addr[1]);
  108. if (buf->buf.num_planes > 2)
  109. vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1, buf->addr[2]);
  110. }
  111. static const struct vsp1_video_operations rpf_vdev_ops = {
  112. .queue = rpf_vdev_queue,
  113. };
  114. /* -----------------------------------------------------------------------------
  115. * Initialization and Cleanup
  116. */
  117. struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
  118. {
  119. struct v4l2_subdev *subdev;
  120. struct vsp1_video *video;
  121. struct vsp1_rwpf *rpf;
  122. int ret;
  123. rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
  124. if (rpf == NULL)
  125. return ERR_PTR(-ENOMEM);
  126. rpf->max_width = RPF_MAX_WIDTH;
  127. rpf->max_height = RPF_MAX_HEIGHT;
  128. rpf->entity.type = VSP1_ENTITY_RPF;
  129. rpf->entity.index = index;
  130. rpf->entity.id = VI6_DPR_NODE_RPF(index);
  131. ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
  132. if (ret < 0)
  133. return ERR_PTR(ret);
  134. /* Initialize the V4L2 subdev. */
  135. subdev = &rpf->entity.subdev;
  136. v4l2_subdev_init(subdev, &rpf_ops);
  137. subdev->entity.ops = &vsp1_media_ops;
  138. subdev->internal_ops = &vsp1_subdev_internal_ops;
  139. snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
  140. dev_name(vsp1->dev), index);
  141. v4l2_set_subdevdata(subdev, rpf);
  142. subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  143. vsp1_entity_init_formats(subdev, NULL);
  144. /* Initialize the video device. */
  145. video = &rpf->video;
  146. video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  147. video->vsp1 = vsp1;
  148. video->ops = &rpf_vdev_ops;
  149. ret = vsp1_video_init(video, &rpf->entity);
  150. if (ret < 0)
  151. goto error_video;
  152. /* Connect the video device to the RPF. */
  153. ret = media_entity_create_link(&rpf->video.video.entity, 0,
  154. &rpf->entity.subdev.entity,
  155. RWPF_PAD_SINK,
  156. MEDIA_LNK_FL_ENABLED |
  157. MEDIA_LNK_FL_IMMUTABLE);
  158. if (ret < 0)
  159. goto error_link;
  160. return rpf;
  161. error_link:
  162. vsp1_video_cleanup(video);
  163. error_video:
  164. media_entity_cleanup(&rpf->entity.subdev.entity);
  165. return ERR_PTR(ret);
  166. }