1 // SPDX-License-Identifier: GPL-2.0+
3 * vsp1_rpf.c -- R-Car VSP1 Read Pixel Formatter
5 * Copyright (C) 2013-2014 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <linux/device.h>
12 #include <media/v4l2-subdev.h>
16 #include "vsp1_pipe.h"
17 #include "vsp1_rwpf.h"
18 #include "vsp1_video.h"
20 #define RPF_MAX_WIDTH 8190
21 #define RPF_MAX_HEIGHT 8190
23 /* -----------------------------------------------------------------------------
27 static inline void vsp1_rpf_write(struct vsp1_rwpf *rpf,
28 struct vsp1_dl_body *dlb, u32 reg, u32 data)
30 vsp1_dl_body_write(dlb, reg + rpf->entity.index * VI6_RPF_OFFSET,
34 /* -----------------------------------------------------------------------------
35 * V4L2 Subdevice Operations
38 static const struct v4l2_subdev_ops rpf_ops = {
39 .pad = &vsp1_rwpf_pad_ops,
42 /* -----------------------------------------------------------------------------
43 * VSP1 Entity Operations
46 static void rpf_configure_stream(struct vsp1_entity *entity,
47 struct vsp1_pipeline *pipe,
48 struct vsp1_dl_body *dlb)
50 struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
51 const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
52 const struct v4l2_pix_format_mplane *format = &rpf->format;
53 const struct v4l2_mbus_framefmt *source_format;
54 const struct v4l2_mbus_framefmt *sink_format;
55 unsigned int left = 0;
61 pstride = format->plane_fmt[0].bytesperline
62 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT;
63 if (format->num_planes > 1)
64 pstride |= format->plane_fmt[1].bytesperline
65 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT;
67 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_PSTRIDE, pstride);
70 sink_format = vsp1_entity_get_pad_format(&rpf->entity,
73 source_format = vsp1_entity_get_pad_format(&rpf->entity,
77 infmt = VI6_RPF_INFMT_CIPM
78 | (fmtinfo->hwfmt << VI6_RPF_INFMT_RDFMT_SHIFT);
81 infmt |= VI6_RPF_INFMT_SPYCS;
83 infmt |= VI6_RPF_INFMT_SPUVS;
85 if (sink_format->code != source_format->code)
86 infmt |= VI6_RPF_INFMT_CSC;
88 vsp1_rpf_write(rpf, dlb, VI6_RPF_INFMT, infmt);
89 vsp1_rpf_write(rpf, dlb, VI6_RPF_DSWAP, fmtinfo->swap);
93 const struct v4l2_rect *compose;
95 compose = vsp1_entity_get_pad_selection(pipe->brx,
98 V4L2_SEL_TGT_COMPOSE);
103 vsp1_rpf_write(rpf, dlb, VI6_RPF_LOC,
104 (left << VI6_RPF_LOC_HCOORD_SHIFT) |
105 (top << VI6_RPF_LOC_VCOORD_SHIFT));
108 * On Gen2 use the alpha channel (extended to 8 bits) when available or
109 * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
112 * The Gen3 RPF has extended alpha capability and can both multiply the
113 * alpha channel by a fixed global alpha value, and multiply the pixel
114 * components to convert the input to premultiplied alpha.
116 * As alpha premultiplication is available in the BRx for both Gen2 and
117 * Gen3 we handle it there and use the Gen3 alpha multiplier for global
118 * alpha multiplication only. This however prevents conversion to
119 * premultiplied alpha if no BRx is present in the pipeline. If that use
120 * case turns out to be useful we will revisit the implementation (for
123 * We enable alpha multiplication on Gen3 using the fixed alpha value
124 * set through the V4L2_CID_ALPHA_COMPONENT control when the input
125 * contains an alpha channel. On Gen2 the global alpha is ignored in
128 * In all cases, disable color keying.
130 vsp1_rpf_write(rpf, dlb, VI6_RPF_ALPH_SEL, VI6_RPF_ALPH_SEL_AEXT_EXT |
131 (fmtinfo->alpha ? VI6_RPF_ALPH_SEL_ASEL_PACKED
132 : VI6_RPF_ALPH_SEL_ASEL_FIXED));
134 if (entity->vsp1->info->gen == 3) {
137 if (fmtinfo->alpha) {
139 * When the input contains an alpha channel enable the
140 * alpha multiplier. If the input is premultiplied we
141 * need to multiply both the alpha channel and the pixel
142 * components by the global alpha value to keep them
143 * premultiplied. Otherwise multiply the alpha channel
146 bool premultiplied = format->flags
147 & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
149 mult = VI6_RPF_MULT_ALPHA_A_MMD_RATIO
151 VI6_RPF_MULT_ALPHA_P_MMD_RATIO :
152 VI6_RPF_MULT_ALPHA_P_MMD_NONE);
155 * When the input doesn't contain an alpha channel the
156 * global alpha value is applied in the unpacking unit,
157 * the alpha multiplier isn't needed and must be
160 mult = VI6_RPF_MULT_ALPHA_A_MMD_NONE
161 | VI6_RPF_MULT_ALPHA_P_MMD_NONE;
164 rpf->mult_alpha = mult;
167 vsp1_rpf_write(rpf, dlb, VI6_RPF_MSK_CTRL, 0);
168 vsp1_rpf_write(rpf, dlb, VI6_RPF_CKEY_CTRL, 0);
172 static void rpf_configure_frame(struct vsp1_entity *entity,
173 struct vsp1_pipeline *pipe,
174 struct vsp1_dl_list *dl,
175 struct vsp1_dl_body *dlb)
177 struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
179 vsp1_rpf_write(rpf, dlb, VI6_RPF_VRTCOL_SET,
180 rpf->alpha << VI6_RPF_VRTCOL_SET_LAYA_SHIFT);
181 vsp1_rpf_write(rpf, dlb, VI6_RPF_MULT_ALPHA, rpf->mult_alpha |
182 (rpf->alpha << VI6_RPF_MULT_ALPHA_RATIO_SHIFT));
184 vsp1_pipeline_propagate_alpha(pipe, dlb, rpf->alpha);
187 static void rpf_configure_partition(struct vsp1_entity *entity,
188 struct vsp1_pipeline *pipe,
189 struct vsp1_dl_list *dl,
190 struct vsp1_dl_body *dlb)
192 struct vsp1_rwpf *rpf = to_rwpf(&entity->subdev);
193 struct vsp1_rwpf_memory mem = rpf->mem;
194 struct vsp1_device *vsp1 = rpf->entity.vsp1;
195 const struct vsp1_format_info *fmtinfo = rpf->fmtinfo;
196 const struct v4l2_pix_format_mplane *format = &rpf->format;
197 struct v4l2_rect crop;
200 * Source size and crop offsets.
202 * The crop offsets correspond to the location of the crop
203 * rectangle top left corner in the plane buffer. Only two
204 * offsets are needed, as planes 2 and 3 always have identical
207 crop = *vsp1_rwpf_get_crop(rpf, rpf->entity.config);
210 * Partition Algorithm Control
212 * The partition algorithm can split this frame into multiple
213 * slices. We must scale our partition window based on the pipe
214 * configuration to match the destination partition window.
215 * To achieve this, we adjust our crop to provide a 'sub-crop'
216 * matching the expected partition window. Only 'left' and
217 * 'width' need to be adjusted.
219 if (pipe->partitions > 1) {
220 crop.width = pipe->partition->rpf.width;
221 crop.left += pipe->partition->rpf.left;
224 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRC_BSIZE,
225 (crop.width << VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT) |
226 (crop.height << VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT));
227 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRC_ESIZE,
228 (crop.width << VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT) |
229 (crop.height << VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT));
231 mem.addr[0] += crop.top * format->plane_fmt[0].bytesperline
232 + crop.left * fmtinfo->bpp[0] / 8;
234 if (format->num_planes > 1) {
237 offset = crop.top * format->plane_fmt[1].bytesperline
238 + crop.left / fmtinfo->hsub
239 * fmtinfo->bpp[1] / 8;
240 mem.addr[1] += offset;
241 mem.addr[2] += offset;
245 * On Gen3 hardware the SPUVS bit has no effect on 3-planar
246 * formats. Swap the U and V planes manually in that case.
248 if (vsp1->info->gen == 3 && format->num_planes == 3 &&
250 swap(mem.addr[1], mem.addr[2]);
252 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_Y, mem.addr[0]);
253 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_C0, mem.addr[1]);
254 vsp1_rpf_write(rpf, dlb, VI6_RPF_SRCM_ADDR_C1, mem.addr[2]);
257 static void rpf_partition(struct vsp1_entity *entity,
258 struct vsp1_pipeline *pipe,
259 struct vsp1_partition *partition,
260 unsigned int partition_idx,
261 struct vsp1_partition_window *window)
263 partition->rpf = *window;
266 static const struct vsp1_entity_operations rpf_entity_ops = {
267 .configure_stream = rpf_configure_stream,
268 .configure_frame = rpf_configure_frame,
269 .configure_partition = rpf_configure_partition,
270 .partition = rpf_partition,
273 /* -----------------------------------------------------------------------------
274 * Initialization and Cleanup
277 struct vsp1_rwpf *vsp1_rpf_create(struct vsp1_device *vsp1, unsigned int index)
279 struct vsp1_rwpf *rpf;
283 rpf = devm_kzalloc(vsp1->dev, sizeof(*rpf), GFP_KERNEL);
285 return ERR_PTR(-ENOMEM);
287 rpf->max_width = RPF_MAX_WIDTH;
288 rpf->max_height = RPF_MAX_HEIGHT;
290 rpf->entity.ops = &rpf_entity_ops;
291 rpf->entity.type = VSP1_ENTITY_RPF;
292 rpf->entity.index = index;
294 sprintf(name, "rpf.%u", index);
295 ret = vsp1_entity_init(vsp1, &rpf->entity, name, 2, &rpf_ops,
296 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER);
300 /* Initialize the control handler. */
301 ret = vsp1_rwpf_init_ctrls(rpf, 0);
303 dev_err(vsp1->dev, "rpf%u: failed to initialize controls\n",
308 v4l2_ctrl_handler_setup(&rpf->ctrls);
313 vsp1_entity_destroy(&rpf->entity);