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>
20 #include "vsp1_pipe.h"
21 #include "vsp1_rwpf.h"
22 #include "vsp1_video.h"
24 #define RPF_MAX_WIDTH 8190
25 #define RPF_MAX_HEIGHT 8190
27 /* -----------------------------------------------------------------------------
31 static inline void vsp1_rpf_write(struct vsp1_rwpf
*rpf
,
32 struct vsp1_dl_list
*dl
, u32 reg
, u32 data
)
34 vsp1_dl_list_write(dl
, reg
+ rpf
->entity
.index
* VI6_RPF_OFFSET
, data
);
37 /* -----------------------------------------------------------------------------
38 * V4L2 Subdevice Operations
41 static const struct v4l2_subdev_ops rpf_ops
= {
42 .pad
= &vsp1_rwpf_pad_ops
,
45 /* -----------------------------------------------------------------------------
46 * VSP1 Entity Operations
49 static void rpf_configure(struct vsp1_entity
*entity
,
50 struct vsp1_pipeline
*pipe
,
51 struct vsp1_dl_list
*dl
,
52 enum vsp1_entity_params params
)
54 struct vsp1_rwpf
*rpf
= to_rwpf(&entity
->subdev
);
55 const struct vsp1_format_info
*fmtinfo
= rpf
->fmtinfo
;
56 const struct v4l2_pix_format_mplane
*format
= &rpf
->format
;
57 const struct v4l2_mbus_framefmt
*source_format
;
58 const struct v4l2_mbus_framefmt
*sink_format
;
59 unsigned int left
= 0;
64 if (params
== VSP1_ENTITY_PARAMS_RUNTIME
) {
65 vsp1_rpf_write(rpf
, dl
, VI6_RPF_VRTCOL_SET
,
66 rpf
->alpha
<< VI6_RPF_VRTCOL_SET_LAYA_SHIFT
);
67 vsp1_rpf_write(rpf
, dl
, VI6_RPF_MULT_ALPHA
, rpf
->mult_alpha
|
68 (rpf
->alpha
<< VI6_RPF_MULT_ALPHA_RATIO_SHIFT
));
70 vsp1_pipeline_propagate_alpha(pipe
, dl
, rpf
->alpha
);
74 if (params
== VSP1_ENTITY_PARAMS_PARTITION
) {
75 struct vsp1_device
*vsp1
= rpf
->entity
.vsp1
;
76 struct vsp1_rwpf_memory mem
= rpf
->mem
;
77 struct v4l2_rect crop
;
80 * Source size and crop offsets.
82 * The crop offsets correspond to the location of the crop
83 * rectangle top left corner in the plane buffer. Only two
84 * offsets are needed, as planes 2 and 3 always have identical
87 crop
= *vsp1_rwpf_get_crop(rpf
, rpf
->entity
.config
);
90 * Partition Algorithm Control
92 * The partition algorithm can split this frame into multiple
93 * slices. We must scale our partition window based on the pipe
94 * configuration to match the destination partition window.
95 * To achieve this, we adjust our crop to provide a 'sub-crop'
96 * matching the expected partition window. Only 'left' and
97 * 'width' need to be adjusted.
99 if (pipe
->partitions
> 1) {
100 crop
.width
= pipe
->partition
->rpf
.width
;
101 crop
.left
+= pipe
->partition
->rpf
.left
;
104 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRC_BSIZE
,
105 (crop
.width
<< VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT
) |
106 (crop
.height
<< VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT
));
107 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRC_ESIZE
,
108 (crop
.width
<< VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT
) |
109 (crop
.height
<< VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT
));
111 mem
.addr
[0] += crop
.top
* format
->plane_fmt
[0].bytesperline
112 + crop
.left
* fmtinfo
->bpp
[0] / 8;
114 if (format
->num_planes
> 1) {
117 offset
= crop
.top
* format
->plane_fmt
[1].bytesperline
118 + crop
.left
/ fmtinfo
->hsub
119 * fmtinfo
->bpp
[1] / 8;
120 mem
.addr
[1] += offset
;
121 mem
.addr
[2] += offset
;
125 * On Gen3 hardware the SPUVS bit has no effect on 3-planar
126 * formats. Swap the U and V planes manually in that case.
128 if (vsp1
->info
->gen
== 3 && format
->num_planes
== 3 &&
130 swap(mem
.addr
[1], mem
.addr
[2]);
132 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_Y
, mem
.addr
[0]);
133 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_C0
, mem
.addr
[1]);
134 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_C1
, mem
.addr
[2]);
139 pstride
= format
->plane_fmt
[0].bytesperline
140 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT
;
141 if (format
->num_planes
> 1)
142 pstride
|= format
->plane_fmt
[1].bytesperline
143 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT
;
145 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_PSTRIDE
, pstride
);
148 sink_format
= vsp1_entity_get_pad_format(&rpf
->entity
,
151 source_format
= vsp1_entity_get_pad_format(&rpf
->entity
,
155 infmt
= VI6_RPF_INFMT_CIPM
156 | (fmtinfo
->hwfmt
<< VI6_RPF_INFMT_RDFMT_SHIFT
);
158 if (fmtinfo
->swap_yc
)
159 infmt
|= VI6_RPF_INFMT_SPYCS
;
160 if (fmtinfo
->swap_uv
)
161 infmt
|= VI6_RPF_INFMT_SPUVS
;
163 if (sink_format
->code
!= source_format
->code
)
164 infmt
|= VI6_RPF_INFMT_CSC
;
166 vsp1_rpf_write(rpf
, dl
, VI6_RPF_INFMT
, infmt
);
167 vsp1_rpf_write(rpf
, dl
, VI6_RPF_DSWAP
, fmtinfo
->swap
);
169 /* Output location */
171 const struct v4l2_rect
*compose
;
173 compose
= vsp1_entity_get_pad_selection(pipe
->bru
,
176 V4L2_SEL_TGT_COMPOSE
);
177 left
= compose
->left
;
181 vsp1_rpf_write(rpf
, dl
, VI6_RPF_LOC
,
182 (left
<< VI6_RPF_LOC_HCOORD_SHIFT
) |
183 (top
<< VI6_RPF_LOC_VCOORD_SHIFT
));
186 * On Gen2 use the alpha channel (extended to 8 bits) when available or
187 * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
190 * The Gen3 RPF has extended alpha capability and can both multiply the
191 * alpha channel by a fixed global alpha value, and multiply the pixel
192 * components to convert the input to premultiplied alpha.
194 * As alpha premultiplication is available in the BRU for both Gen2 and
195 * Gen3 we handle it there and use the Gen3 alpha multiplier for global
196 * alpha multiplication only. This however prevents conversion to
197 * premultiplied alpha if no BRU is present in the pipeline. If that use
198 * case turns out to be useful we will revisit the implementation (for
201 * We enable alpha multiplication on Gen3 using the fixed alpha value
202 * set through the V4L2_CID_ALPHA_COMPONENT control when the input
203 * contains an alpha channel. On Gen2 the global alpha is ignored in
206 * In all cases, disable color keying.
208 vsp1_rpf_write(rpf
, dl
, VI6_RPF_ALPH_SEL
, VI6_RPF_ALPH_SEL_AEXT_EXT
|
209 (fmtinfo
->alpha
? VI6_RPF_ALPH_SEL_ASEL_PACKED
210 : VI6_RPF_ALPH_SEL_ASEL_FIXED
));
212 if (entity
->vsp1
->info
->gen
== 3) {
215 if (fmtinfo
->alpha
) {
217 * When the input contains an alpha channel enable the
218 * alpha multiplier. If the input is premultiplied we
219 * need to multiply both the alpha channel and the pixel
220 * components by the global alpha value to keep them
221 * premultiplied. Otherwise multiply the alpha channel
224 bool premultiplied
= format
->flags
225 & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA
;
227 mult
= VI6_RPF_MULT_ALPHA_A_MMD_RATIO
229 VI6_RPF_MULT_ALPHA_P_MMD_RATIO
:
230 VI6_RPF_MULT_ALPHA_P_MMD_NONE
);
233 * When the input doesn't contain an alpha channel the
234 * global alpha value is applied in the unpacking unit,
235 * the alpha multiplier isn't needed and must be
238 mult
= VI6_RPF_MULT_ALPHA_A_MMD_NONE
239 | VI6_RPF_MULT_ALPHA_P_MMD_NONE
;
242 rpf
->mult_alpha
= mult
;
245 vsp1_rpf_write(rpf
, dl
, VI6_RPF_MSK_CTRL
, 0);
246 vsp1_rpf_write(rpf
, dl
, VI6_RPF_CKEY_CTRL
, 0);
250 static void rpf_partition(struct vsp1_entity
*entity
,
251 struct vsp1_pipeline
*pipe
,
252 struct vsp1_partition
*partition
,
253 unsigned int partition_idx
,
254 struct vsp1_partition_window
*window
)
256 partition
->rpf
= *window
;
259 static const struct vsp1_entity_operations rpf_entity_ops
= {
260 .configure
= rpf_configure
,
261 .partition
= rpf_partition
,
264 /* -----------------------------------------------------------------------------
265 * Initialization and Cleanup
268 struct vsp1_rwpf
*vsp1_rpf_create(struct vsp1_device
*vsp1
, unsigned int index
)
270 struct vsp1_rwpf
*rpf
;
274 rpf
= devm_kzalloc(vsp1
->dev
, sizeof(*rpf
), GFP_KERNEL
);
276 return ERR_PTR(-ENOMEM
);
278 rpf
->max_width
= RPF_MAX_WIDTH
;
279 rpf
->max_height
= RPF_MAX_HEIGHT
;
281 rpf
->entity
.ops
= &rpf_entity_ops
;
282 rpf
->entity
.type
= VSP1_ENTITY_RPF
;
283 rpf
->entity
.index
= index
;
285 sprintf(name
, "rpf.%u", index
);
286 ret
= vsp1_entity_init(vsp1
, &rpf
->entity
, name
, 2, &rpf_ops
,
287 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER
);
291 /* Initialize the control handler. */
292 ret
= vsp1_rwpf_init_ctrls(rpf
, 0);
294 dev_err(vsp1
->dev
, "rpf%u: failed to initialize controls\n",
299 v4l2_ctrl_handler_setup(&rpf
->ctrls
);
304 vsp1_entity_destroy(&rpf
->entity
);