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 unsigned int offsets
[2];
76 struct v4l2_rect crop
;
79 * Source size and crop offsets.
81 * The crop offsets correspond to the location of the crop
82 * rectangle top left corner in the plane buffer. Only two
83 * offsets are needed, as planes 2 and 3 always have identical
86 crop
= *vsp1_rwpf_get_crop(rpf
, rpf
->entity
.config
);
89 * Partition Algorithm Control
91 * The partition algorithm can split this frame into multiple
92 * slices. We must scale our partition window based on the pipe
93 * configuration to match the destination partition window.
94 * To achieve this, we adjust our crop to provide a 'sub-crop'
95 * matching the expected partition window. Only 'left' and
96 * 'width' need to be adjusted.
98 if (pipe
->partitions
> 1) {
99 const struct v4l2_mbus_framefmt
*output
;
100 struct vsp1_entity
*wpf
= &pipe
->output
->entity
;
101 unsigned int input_width
= crop
.width
;
104 * Scale the partition window based on the configuration
107 output
= vsp1_entity_get_pad_format(wpf
, wpf
->config
,
110 crop
.width
= pipe
->partition
.width
* input_width
112 crop
.left
+= pipe
->partition
.left
* input_width
116 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRC_BSIZE
,
117 (crop
.width
<< VI6_RPF_SRC_BSIZE_BHSIZE_SHIFT
) |
118 (crop
.height
<< VI6_RPF_SRC_BSIZE_BVSIZE_SHIFT
));
119 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRC_ESIZE
,
120 (crop
.width
<< VI6_RPF_SRC_ESIZE_EHSIZE_SHIFT
) |
121 (crop
.height
<< VI6_RPF_SRC_ESIZE_EVSIZE_SHIFT
));
123 offsets
[0] = crop
.top
* format
->plane_fmt
[0].bytesperline
124 + crop
.left
* fmtinfo
->bpp
[0] / 8;
126 if (format
->num_planes
> 1)
127 offsets
[1] = crop
.top
* format
->plane_fmt
[1].bytesperline
128 + crop
.left
/ fmtinfo
->hsub
129 * fmtinfo
->bpp
[1] / 8;
133 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_Y
,
134 rpf
->mem
.addr
[0] + offsets
[0]);
135 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_C0
,
136 rpf
->mem
.addr
[1] + offsets
[1]);
137 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_ADDR_C1
,
138 rpf
->mem
.addr
[2] + offsets
[1]);
143 pstride
= format
->plane_fmt
[0].bytesperline
144 << VI6_RPF_SRCM_PSTRIDE_Y_SHIFT
;
145 if (format
->num_planes
> 1)
146 pstride
|= format
->plane_fmt
[1].bytesperline
147 << VI6_RPF_SRCM_PSTRIDE_C_SHIFT
;
149 vsp1_rpf_write(rpf
, dl
, VI6_RPF_SRCM_PSTRIDE
, pstride
);
152 sink_format
= vsp1_entity_get_pad_format(&rpf
->entity
,
155 source_format
= vsp1_entity_get_pad_format(&rpf
->entity
,
159 infmt
= VI6_RPF_INFMT_CIPM
160 | (fmtinfo
->hwfmt
<< VI6_RPF_INFMT_RDFMT_SHIFT
);
162 if (fmtinfo
->swap_yc
)
163 infmt
|= VI6_RPF_INFMT_SPYCS
;
164 if (fmtinfo
->swap_uv
)
165 infmt
|= VI6_RPF_INFMT_SPUVS
;
167 if (sink_format
->code
!= source_format
->code
)
168 infmt
|= VI6_RPF_INFMT_CSC
;
170 vsp1_rpf_write(rpf
, dl
, VI6_RPF_INFMT
, infmt
);
171 vsp1_rpf_write(rpf
, dl
, VI6_RPF_DSWAP
, fmtinfo
->swap
);
173 /* Output location */
175 const struct v4l2_rect
*compose
;
177 compose
= vsp1_entity_get_pad_selection(pipe
->bru
,
180 V4L2_SEL_TGT_COMPOSE
);
181 left
= compose
->left
;
185 vsp1_rpf_write(rpf
, dl
, VI6_RPF_LOC
,
186 (left
<< VI6_RPF_LOC_HCOORD_SHIFT
) |
187 (top
<< VI6_RPF_LOC_VCOORD_SHIFT
));
189 /* On Gen2 use the alpha channel (extended to 8 bits) when available or
190 * a fixed alpha value set through the V4L2_CID_ALPHA_COMPONENT control
193 * The Gen3 RPF has extended alpha capability and can both multiply the
194 * alpha channel by a fixed global alpha value, and multiply the pixel
195 * components to convert the input to premultiplied alpha.
197 * As alpha premultiplication is available in the BRU for both Gen2 and
198 * Gen3 we handle it there and use the Gen3 alpha multiplier for global
199 * alpha multiplication only. This however prevents conversion to
200 * premultiplied alpha if no BRU is present in the pipeline. If that use
201 * case turns out to be useful we will revisit the implementation (for
204 * We enable alpha multiplication on Gen3 using the fixed alpha value
205 * set through the V4L2_CID_ALPHA_COMPONENT control when the input
206 * contains an alpha channel. On Gen2 the global alpha is ignored in
209 * In all cases, disable color keying.
211 vsp1_rpf_write(rpf
, dl
, VI6_RPF_ALPH_SEL
, VI6_RPF_ALPH_SEL_AEXT_EXT
|
212 (fmtinfo
->alpha
? VI6_RPF_ALPH_SEL_ASEL_PACKED
213 : VI6_RPF_ALPH_SEL_ASEL_FIXED
));
215 if (entity
->vsp1
->info
->gen
== 3) {
218 if (fmtinfo
->alpha
) {
219 /* When the input contains an alpha channel enable the
220 * alpha multiplier. If the input is premultiplied we
221 * need to multiply both the alpha channel and the pixel
222 * components by the global alpha value to keep them
223 * premultiplied. Otherwise multiply the alpha channel
226 bool premultiplied
= format
->flags
227 & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA
;
229 mult
= VI6_RPF_MULT_ALPHA_A_MMD_RATIO
231 VI6_RPF_MULT_ALPHA_P_MMD_RATIO
:
232 VI6_RPF_MULT_ALPHA_P_MMD_NONE
);
234 /* When the input doesn't contain an alpha channel the
235 * global alpha value is applied in the unpacking unit,
236 * the alpha multiplier isn't needed and must be
239 mult
= VI6_RPF_MULT_ALPHA_A_MMD_NONE
240 | VI6_RPF_MULT_ALPHA_P_MMD_NONE
;
243 rpf
->mult_alpha
= mult
;
246 vsp1_rpf_write(rpf
, dl
, VI6_RPF_MSK_CTRL
, 0);
247 vsp1_rpf_write(rpf
, dl
, VI6_RPF_CKEY_CTRL
, 0);
251 static const struct vsp1_entity_operations rpf_entity_ops
= {
252 .configure
= rpf_configure
,
255 /* -----------------------------------------------------------------------------
256 * Initialization and Cleanup
259 struct vsp1_rwpf
*vsp1_rpf_create(struct vsp1_device
*vsp1
, unsigned int index
)
261 struct vsp1_rwpf
*rpf
;
265 rpf
= devm_kzalloc(vsp1
->dev
, sizeof(*rpf
), GFP_KERNEL
);
267 return ERR_PTR(-ENOMEM
);
269 rpf
->max_width
= RPF_MAX_WIDTH
;
270 rpf
->max_height
= RPF_MAX_HEIGHT
;
272 rpf
->entity
.ops
= &rpf_entity_ops
;
273 rpf
->entity
.type
= VSP1_ENTITY_RPF
;
274 rpf
->entity
.index
= index
;
276 sprintf(name
, "rpf.%u", index
);
277 ret
= vsp1_entity_init(vsp1
, &rpf
->entity
, name
, 2, &rpf_ops
,
278 MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER
);
282 /* Initialize the control handler. */
283 ret
= vsp1_rwpf_init_ctrls(rpf
, 0);
285 dev_err(vsp1
->dev
, "rpf%u: failed to initialize controls\n",
290 v4l2_ctrl_handler_setup(&rpf
->ctrls
);
295 vsp1_entity_destroy(&rpf
->entity
);