2 * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/device.h>
16 #include <media/v4l2-subdev.h>
19 #include "vsp1_rwpf.h"
20 #include "vsp1_video.h"
22 #define RPF_MAX_WIDTH 8190
23 #define RPF_MAX_HEIGHT 8190
25 /* -----------------------------------------------------------------------------
29 static inline u32 vsp1_rpf_read(struct vsp1_rwpf *rpf, u32 reg)
31 return vsp1_read(rpf->entity.vsp1,
32 reg + rpf->entity.index * VI6_RPF_OFFSET);
35 static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf, u32 reg, u32 data)
37 vsp1_write(rpf->entity.vsp1,
38 reg + rpf->entity.index * VI6_RPF_OFFSET, data);
41 /* -----------------------------------------------------------------------------
45 static int rpf_s_ctrl(struct v4l2_ctrl *ctrl)
47 struct vsp1_rwpf *rpf =
48 container_of(ctrl->handler, struct vsp1_rwpf, ctrls);
49 struct vsp1_pipeline *pipe;
51 if (!vsp1_entity_is_streaming(&rpf->entity))
55 case V4L2_CID_ALPHA_COMPONENT:
56 vsp1_rpf_write(rpf, VI6_RPF_VRTCOL_SET,
57 ctrl->val << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
59 pipe = to_vsp1_pipeline(&rpf->entity.subdev.entity);
60 vsp1_pipeline_propagate_alpha(pipe, &rpf->entity, ctrl->val);
67 static const struct v4l2_ctrl_ops rpf_ctrl_ops = {
71 /* -----------------------------------------------------------------------------
72 * V4L2 Subdevice Core Operations
75 static int rpf_s_stream(struct v4l2_subdev *subdev, int enable)
77 struct vsp1_rwpf *rpf = to_rwpf(subdev);
78 const struct vsp1_format_info *fmtinfo = rpf->video.fmtinfo;
79 const struct v4l2_pix_format_mplane *format = &rpf->video.format;
80 const struct v4l2_rect *crop = &rpf->crop;
85 ret = vsp1_entity_set_streaming(&rpf->entity, enable);
92 /* Source size, stride and crop offsets.
94 * The crop offsets correspond to the location of the crop rectangle top
95 * left corner in the plane buffer. Only two offsets are needed, as
96 * planes 2 and 3 always have identical strides.
98 vsp1_rpf_write(rpf, VI6_RPF_SRC_BSIZE,
99 (crop->width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
100 (crop->height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
101 vsp1_rpf_write(rpf, VI6_RPF_SRC_ESIZE,
102 (crop->width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
103 (crop->height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
105 rpf->offsets[0] = crop->top * format->plane_fmt[0].bytesperline
106 + crop->left * fmtinfo->bpp[0] / 8;
107 pstride = format->plane_fmt[0].bytesperline
108 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
109 if (format->num_planes > 1) {
110 rpf->offsets[1] = crop->top * format->plane_fmt[1].bytesperline
111 + crop->left * fmtinfo->bpp[1] / 8;
112 pstride |= format->plane_fmt[1].bytesperline
113 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
116 vsp1_rpf_write(rpf, VI6_RPF_SRCM_PSTRIDE, pstride);
119 infmt = VI6_RPF_INFMT_CIPM
120 | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
122 if (fmtinfo->swap_yc)
123 infmt |= VI6_RPF_INFMT_SPYCS;
124 if (fmtinfo->swap_uv)
125 infmt |= VI6_RPF_INFMT_SPUVS;
127 if (rpf->entity.formats[RWPF_PAD_SINK].code !=
128 rpf->entity.formats[RWPF_PAD_SOURCE].code)
129 infmt |= VI6_RPF_INFMT_CSC;
131 vsp1_rpf_write(rpf, VI6_RPF_INFMT, infmt);
132 vsp1_rpf_write(rpf, VI6_RPF_DSWAP, fmtinfo->swap);
134 /* Output location */
135 vsp1_rpf_write(rpf, VI6_RPF_LOC,
136 (rpf->location.left << VI6_RPF_LOC_HCOORD_SHIFT) |
137 (rpf->location.top << VI6_RPF_LOC_VCOORD_SHIFT));
139 /* Use the alpha channel (extended to 8 bits) when available or an
140 * alpha value set through the V4L2_CID_ALPHA_COMPONENT control
141 * otherwise. Disable color keying.
143 vsp1_rpf_write(rpf, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
144 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
145 : VI6_RPF_ALPH_SEL_ASEL_FIXED));
146 vsp1_rpf_write(rpf, VI6_RPF_MSK_CTRL, 0);
147 vsp1_rpf_write(rpf, VI6_RPF_CKEY_CTRL, 0);
152 /* -----------------------------------------------------------------------------
153 * V4L2 Subdevice Operations
156 static struct v4l2_subdev_video_ops rpf_video_ops = {
157 .s_stream = rpf_s_stream,
160 static struct v4l2_subdev_pad_ops rpf_pad_ops = {
161 .enum_mbus_code = vsp1_rwpf_enum_mbus_code,
162 .enum_frame_size = vsp1_rwpf_enum_frame_size,
163 .get_fmt = vsp1_rwpf_get_format,
164 .set_fmt = vsp1_rwpf_set_format,
165 .get_selection = vsp1_rwpf_get_selection,
166 .set_selection = vsp1_rwpf_set_selection,
169 static struct v4l2_subdev_ops rpf_ops = {
170 .video = &rpf_video_ops,
174 /* -----------------------------------------------------------------------------
175 * Video Device Operations
178 static void rpf_vdev_queue(struct vsp1_video *video,
179 struct vsp1_video_buffer *buf)
181 struct vsp1_rwpf *rpf = container_of(video, struct vsp1_rwpf, video);
183 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_Y,
184 buf->addr[0] + rpf->offsets[0]);
185 if (buf->buf.num_planes > 1)
186 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C0,
187 buf->addr[1] + rpf->offsets[1]);
188 if (buf->buf.num_planes > 2)
189 vsp1_rpf_write(rpf, VI6_RPF_SRCM_ADDR_C1,
190 buf->addr[2] + rpf->offsets[1]);
193 static const struct vsp1_video_operations rpf_vdev_ops = {
194 .queue = rpf_vdev_queue,
197 /* -----------------------------------------------------------------------------
198 * Initialization and Cleanup
201 struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
203 struct v4l2_subdev *subdev;
204 struct vsp1_video *video;
205 struct vsp1_rwpf *rpf;
208 rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
210 return ERR_PTR(-ENOMEM);
212 rpf->max_width = RPF_MAX_WIDTH;
213 rpf->max_height = RPF_MAX_HEIGHT;
215 rpf->entity.type = VSP1_ENTITY_RPF;
216 rpf->entity.index = index;
218 ret = vsp1_entity_init(vsp1, &rpf->entity, 2);
222 /* Initialize the V4L2 subdev. */
223 subdev = &rpf->entity.subdev;
224 v4l2_subdev_init(subdev, &rpf_ops);
226 subdev->entity.ops = &vsp1_media_ops;
227 subdev->internal_ops = &vsp1_subdev_internal_ops;
228 snprintf(subdev->name, sizeof(subdev->name), "%s rpf.%u",
229 dev_name(vsp1->dev), index);
230 v4l2_set_subdevdata(subdev, rpf);
231 subdev->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
233 vsp1_entity_init_formats(subdev, NULL);
235 /* Initialize the control handler. */
236 v4l2_ctrl_handler_init(&rpf->ctrls, 1);
237 v4l2_ctrl_new_std(&rpf->ctrls, &rpf_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
240 rpf->entity.subdev.ctrl_handler = &rpf->ctrls;
242 if (rpf->ctrls.error) {
243 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
245 ret = rpf->ctrls.error;
249 /* Initialize the video device. */
252 video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
254 video->ops = &rpf_vdev_ops;
256 ret = vsp1_video_init(video, &rpf->entity);
260 rpf->entity.video = video;
262 /* Connect the video device to the RPF. */
263 ret = media_entity_create_link(&rpf->video.video.entity, 0,
264 &rpf->entity.subdev.entity,
266 MEDIA_LNK_FL_ENABLED |
267 MEDIA_LNK_FL_IMMUTABLE);
274 vsp1_entity_destroy(&rpf->entity);