xref: /OK3568_Linux_fs/kernel/drivers/media/platform/vsp1/vsp1_brx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * vsp1_brx.c  --  R-Car VSP1 Blend ROP Unit (BRU and BRS)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2013 Renesas Corporation
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/gfp.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <media/v4l2-subdev.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "vsp1.h"
16*4882a593Smuzhiyun #include "vsp1_brx.h"
17*4882a593Smuzhiyun #include "vsp1_dl.h"
18*4882a593Smuzhiyun #include "vsp1_pipe.h"
19*4882a593Smuzhiyun #include "vsp1_rwpf.h"
20*4882a593Smuzhiyun #include "vsp1_video.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define BRX_MIN_SIZE				1U
23*4882a593Smuzhiyun #define BRX_MAX_SIZE				8190U
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
26*4882a593Smuzhiyun  * Device Access
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
vsp1_brx_write(struct vsp1_brx * brx,struct vsp1_dl_body * dlb,u32 reg,u32 data)29*4882a593Smuzhiyun static inline void vsp1_brx_write(struct vsp1_brx *brx,
30*4882a593Smuzhiyun 				  struct vsp1_dl_body *dlb, u32 reg, u32 data)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	vsp1_dl_body_write(dlb, brx->base + reg, data);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
36*4882a593Smuzhiyun  * Controls
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
brx_s_ctrl(struct v4l2_ctrl * ctrl)39*4882a593Smuzhiyun static int brx_s_ctrl(struct v4l2_ctrl *ctrl)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	struct vsp1_brx *brx =
42*4882a593Smuzhiyun 		container_of(ctrl->handler, struct vsp1_brx, ctrls);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	switch (ctrl->id) {
45*4882a593Smuzhiyun 	case V4L2_CID_BG_COLOR:
46*4882a593Smuzhiyun 		brx->bgcolor = ctrl->val;
47*4882a593Smuzhiyun 		break;
48*4882a593Smuzhiyun 	}
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	return 0;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static const struct v4l2_ctrl_ops brx_ctrl_ops = {
54*4882a593Smuzhiyun 	.s_ctrl = brx_s_ctrl,
55*4882a593Smuzhiyun };
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
58*4882a593Smuzhiyun  * V4L2 Subdevice Operations
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /*
62*4882a593Smuzhiyun  * The BRx can't perform format conversion, all sink and source formats must be
63*4882a593Smuzhiyun  * identical. We pick the format on the first sink pad (pad 0) and propagate it
64*4882a593Smuzhiyun  * to all other pads.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun 
brx_enum_mbus_code(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)67*4882a593Smuzhiyun static int brx_enum_mbus_code(struct v4l2_subdev *subdev,
68*4882a593Smuzhiyun 			      struct v4l2_subdev_pad_config *cfg,
69*4882a593Smuzhiyun 			      struct v4l2_subdev_mbus_code_enum *code)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	static const unsigned int codes[] = {
72*4882a593Smuzhiyun 		MEDIA_BUS_FMT_ARGB8888_1X32,
73*4882a593Smuzhiyun 		MEDIA_BUS_FMT_AYUV8_1X32,
74*4882a593Smuzhiyun 	};
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	return vsp1_subdev_enum_mbus_code(subdev, cfg, code, codes,
77*4882a593Smuzhiyun 					  ARRAY_SIZE(codes));
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
brx_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)80*4882a593Smuzhiyun static int brx_enum_frame_size(struct v4l2_subdev *subdev,
81*4882a593Smuzhiyun 			       struct v4l2_subdev_pad_config *cfg,
82*4882a593Smuzhiyun 			       struct v4l2_subdev_frame_size_enum *fse)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	if (fse->index)
85*4882a593Smuzhiyun 		return -EINVAL;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	if (fse->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
88*4882a593Smuzhiyun 	    fse->code != MEDIA_BUS_FMT_AYUV8_1X32)
89*4882a593Smuzhiyun 		return -EINVAL;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	fse->min_width = BRX_MIN_SIZE;
92*4882a593Smuzhiyun 	fse->max_width = BRX_MAX_SIZE;
93*4882a593Smuzhiyun 	fse->min_height = BRX_MIN_SIZE;
94*4882a593Smuzhiyun 	fse->max_height = BRX_MAX_SIZE;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return 0;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
brx_get_compose(struct vsp1_brx * brx,struct v4l2_subdev_pad_config * cfg,unsigned int pad)99*4882a593Smuzhiyun static struct v4l2_rect *brx_get_compose(struct vsp1_brx *brx,
100*4882a593Smuzhiyun 					 struct v4l2_subdev_pad_config *cfg,
101*4882a593Smuzhiyun 					 unsigned int pad)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	return v4l2_subdev_get_try_compose(&brx->entity.subdev, cfg, pad);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
brx_try_format(struct vsp1_brx * brx,struct v4l2_subdev_pad_config * config,unsigned int pad,struct v4l2_mbus_framefmt * fmt)106*4882a593Smuzhiyun static void brx_try_format(struct vsp1_brx *brx,
107*4882a593Smuzhiyun 			   struct v4l2_subdev_pad_config *config,
108*4882a593Smuzhiyun 			   unsigned int pad, struct v4l2_mbus_framefmt *fmt)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	switch (pad) {
113*4882a593Smuzhiyun 	case BRX_PAD_SINK(0):
114*4882a593Smuzhiyun 		/* Default to YUV if the requested format is not supported. */
115*4882a593Smuzhiyun 		if (fmt->code != MEDIA_BUS_FMT_ARGB8888_1X32 &&
116*4882a593Smuzhiyun 		    fmt->code != MEDIA_BUS_FMT_AYUV8_1X32)
117*4882a593Smuzhiyun 			fmt->code = MEDIA_BUS_FMT_AYUV8_1X32;
118*4882a593Smuzhiyun 		break;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	default:
121*4882a593Smuzhiyun 		/* The BRx can't perform format conversion. */
122*4882a593Smuzhiyun 		format = vsp1_entity_get_pad_format(&brx->entity, config,
123*4882a593Smuzhiyun 						    BRX_PAD_SINK(0));
124*4882a593Smuzhiyun 		fmt->code = format->code;
125*4882a593Smuzhiyun 		break;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	fmt->width = clamp(fmt->width, BRX_MIN_SIZE, BRX_MAX_SIZE);
129*4882a593Smuzhiyun 	fmt->height = clamp(fmt->height, BRX_MIN_SIZE, BRX_MAX_SIZE);
130*4882a593Smuzhiyun 	fmt->field = V4L2_FIELD_NONE;
131*4882a593Smuzhiyun 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
brx_set_format(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)134*4882a593Smuzhiyun static int brx_set_format(struct v4l2_subdev *subdev,
135*4882a593Smuzhiyun 			  struct v4l2_subdev_pad_config *cfg,
136*4882a593Smuzhiyun 			  struct v4l2_subdev_format *fmt)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun 	struct vsp1_brx *brx = to_brx(subdev);
139*4882a593Smuzhiyun 	struct v4l2_subdev_pad_config *config;
140*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
141*4882a593Smuzhiyun 	int ret = 0;
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	mutex_lock(&brx->entity.lock);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	config = vsp1_entity_get_pad_config(&brx->entity, cfg, fmt->which);
146*4882a593Smuzhiyun 	if (!config) {
147*4882a593Smuzhiyun 		ret = -EINVAL;
148*4882a593Smuzhiyun 		goto done;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	brx_try_format(brx, config, fmt->pad, &fmt->format);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	format = vsp1_entity_get_pad_format(&brx->entity, config, fmt->pad);
154*4882a593Smuzhiyun 	*format = fmt->format;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	/* Reset the compose rectangle. */
157*4882a593Smuzhiyun 	if (fmt->pad != brx->entity.source_pad) {
158*4882a593Smuzhiyun 		struct v4l2_rect *compose;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 		compose = brx_get_compose(brx, config, fmt->pad);
161*4882a593Smuzhiyun 		compose->left = 0;
162*4882a593Smuzhiyun 		compose->top = 0;
163*4882a593Smuzhiyun 		compose->width = format->width;
164*4882a593Smuzhiyun 		compose->height = format->height;
165*4882a593Smuzhiyun 	}
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	/* Propagate the format code to all pads. */
168*4882a593Smuzhiyun 	if (fmt->pad == BRX_PAD_SINK(0)) {
169*4882a593Smuzhiyun 		unsigned int i;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		for (i = 0; i <= brx->entity.source_pad; ++i) {
172*4882a593Smuzhiyun 			format = vsp1_entity_get_pad_format(&brx->entity,
173*4882a593Smuzhiyun 							    config, i);
174*4882a593Smuzhiyun 			format->code = fmt->format.code;
175*4882a593Smuzhiyun 		}
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun done:
179*4882a593Smuzhiyun 	mutex_unlock(&brx->entity.lock);
180*4882a593Smuzhiyun 	return ret;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
brx_get_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)183*4882a593Smuzhiyun static int brx_get_selection(struct v4l2_subdev *subdev,
184*4882a593Smuzhiyun 			     struct v4l2_subdev_pad_config *cfg,
185*4882a593Smuzhiyun 			     struct v4l2_subdev_selection *sel)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct vsp1_brx *brx = to_brx(subdev);
188*4882a593Smuzhiyun 	struct v4l2_subdev_pad_config *config;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (sel->pad == brx->entity.source_pad)
191*4882a593Smuzhiyun 		return -EINVAL;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	switch (sel->target) {
194*4882a593Smuzhiyun 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
195*4882a593Smuzhiyun 		sel->r.left = 0;
196*4882a593Smuzhiyun 		sel->r.top = 0;
197*4882a593Smuzhiyun 		sel->r.width = BRX_MAX_SIZE;
198*4882a593Smuzhiyun 		sel->r.height = BRX_MAX_SIZE;
199*4882a593Smuzhiyun 		return 0;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	case V4L2_SEL_TGT_COMPOSE:
202*4882a593Smuzhiyun 		config = vsp1_entity_get_pad_config(&brx->entity, cfg,
203*4882a593Smuzhiyun 						    sel->which);
204*4882a593Smuzhiyun 		if (!config)
205*4882a593Smuzhiyun 			return -EINVAL;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 		mutex_lock(&brx->entity.lock);
208*4882a593Smuzhiyun 		sel->r = *brx_get_compose(brx, config, sel->pad);
209*4882a593Smuzhiyun 		mutex_unlock(&brx->entity.lock);
210*4882a593Smuzhiyun 		return 0;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	default:
213*4882a593Smuzhiyun 		return -EINVAL;
214*4882a593Smuzhiyun 	}
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
brx_set_selection(struct v4l2_subdev * subdev,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_selection * sel)217*4882a593Smuzhiyun static int brx_set_selection(struct v4l2_subdev *subdev,
218*4882a593Smuzhiyun 			     struct v4l2_subdev_pad_config *cfg,
219*4882a593Smuzhiyun 			     struct v4l2_subdev_selection *sel)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	struct vsp1_brx *brx = to_brx(subdev);
222*4882a593Smuzhiyun 	struct v4l2_subdev_pad_config *config;
223*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
224*4882a593Smuzhiyun 	struct v4l2_rect *compose;
225*4882a593Smuzhiyun 	int ret = 0;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	if (sel->pad == brx->entity.source_pad)
228*4882a593Smuzhiyun 		return -EINVAL;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	if (sel->target != V4L2_SEL_TGT_COMPOSE)
231*4882a593Smuzhiyun 		return -EINVAL;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	mutex_lock(&brx->entity.lock);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	config = vsp1_entity_get_pad_config(&brx->entity, cfg, sel->which);
236*4882a593Smuzhiyun 	if (!config) {
237*4882a593Smuzhiyun 		ret = -EINVAL;
238*4882a593Smuzhiyun 		goto done;
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/*
242*4882a593Smuzhiyun 	 * The compose rectangle top left corner must be inside the output
243*4882a593Smuzhiyun 	 * frame.
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	format = vsp1_entity_get_pad_format(&brx->entity, config,
246*4882a593Smuzhiyun 					    brx->entity.source_pad);
247*4882a593Smuzhiyun 	sel->r.left = clamp_t(unsigned int, sel->r.left, 0, format->width - 1);
248*4882a593Smuzhiyun 	sel->r.top = clamp_t(unsigned int, sel->r.top, 0, format->height - 1);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/*
251*4882a593Smuzhiyun 	 * Scaling isn't supported, the compose rectangle size must be identical
252*4882a593Smuzhiyun 	 * to the sink format size.
253*4882a593Smuzhiyun 	 */
254*4882a593Smuzhiyun 	format = vsp1_entity_get_pad_format(&brx->entity, config, sel->pad);
255*4882a593Smuzhiyun 	sel->r.width = format->width;
256*4882a593Smuzhiyun 	sel->r.height = format->height;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	compose = brx_get_compose(brx, config, sel->pad);
259*4882a593Smuzhiyun 	*compose = sel->r;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun done:
262*4882a593Smuzhiyun 	mutex_unlock(&brx->entity.lock);
263*4882a593Smuzhiyun 	return ret;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun static const struct v4l2_subdev_pad_ops brx_pad_ops = {
267*4882a593Smuzhiyun 	.init_cfg = vsp1_entity_init_cfg,
268*4882a593Smuzhiyun 	.enum_mbus_code = brx_enum_mbus_code,
269*4882a593Smuzhiyun 	.enum_frame_size = brx_enum_frame_size,
270*4882a593Smuzhiyun 	.get_fmt = vsp1_subdev_get_pad_format,
271*4882a593Smuzhiyun 	.set_fmt = brx_set_format,
272*4882a593Smuzhiyun 	.get_selection = brx_get_selection,
273*4882a593Smuzhiyun 	.set_selection = brx_set_selection,
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun static const struct v4l2_subdev_ops brx_ops = {
277*4882a593Smuzhiyun 	.pad    = &brx_pad_ops,
278*4882a593Smuzhiyun };
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
281*4882a593Smuzhiyun  * VSP1 Entity Operations
282*4882a593Smuzhiyun  */
283*4882a593Smuzhiyun 
brx_configure_stream(struct vsp1_entity * entity,struct vsp1_pipeline * pipe,struct vsp1_dl_list * dl,struct vsp1_dl_body * dlb)284*4882a593Smuzhiyun static void brx_configure_stream(struct vsp1_entity *entity,
285*4882a593Smuzhiyun 				 struct vsp1_pipeline *pipe,
286*4882a593Smuzhiyun 				 struct vsp1_dl_list *dl,
287*4882a593Smuzhiyun 				 struct vsp1_dl_body *dlb)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	struct vsp1_brx *brx = to_brx(&entity->subdev);
290*4882a593Smuzhiyun 	struct v4l2_mbus_framefmt *format;
291*4882a593Smuzhiyun 	unsigned int flags;
292*4882a593Smuzhiyun 	unsigned int i;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	format = vsp1_entity_get_pad_format(&brx->entity, brx->entity.config,
295*4882a593Smuzhiyun 					    brx->entity.source_pad);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/*
298*4882a593Smuzhiyun 	 * The hardware is extremely flexible but we have no userspace API to
299*4882a593Smuzhiyun 	 * expose all the parameters, nor is it clear whether we would have use
300*4882a593Smuzhiyun 	 * cases for all the supported modes. Let's just hardcode the parameters
301*4882a593Smuzhiyun 	 * to sane default values for now.
302*4882a593Smuzhiyun 	 */
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	/*
305*4882a593Smuzhiyun 	 * Disable dithering and enable color data normalization unless the
306*4882a593Smuzhiyun 	 * format at the pipeline output is premultiplied.
307*4882a593Smuzhiyun 	 */
308*4882a593Smuzhiyun 	flags = pipe->output ? pipe->output->format.flags : 0;
309*4882a593Smuzhiyun 	vsp1_brx_write(brx, dlb, VI6_BRU_INCTRL,
310*4882a593Smuzhiyun 		       flags & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA ?
311*4882a593Smuzhiyun 		       0 : VI6_BRU_INCTRL_NRM);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	/*
314*4882a593Smuzhiyun 	 * Set the background position to cover the whole output image and
315*4882a593Smuzhiyun 	 * configure its color.
316*4882a593Smuzhiyun 	 */
317*4882a593Smuzhiyun 	vsp1_brx_write(brx, dlb, VI6_BRU_VIRRPF_SIZE,
318*4882a593Smuzhiyun 		       (format->width << VI6_BRU_VIRRPF_SIZE_HSIZE_SHIFT) |
319*4882a593Smuzhiyun 		       (format->height << VI6_BRU_VIRRPF_SIZE_VSIZE_SHIFT));
320*4882a593Smuzhiyun 	vsp1_brx_write(brx, dlb, VI6_BRU_VIRRPF_LOC, 0);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	vsp1_brx_write(brx, dlb, VI6_BRU_VIRRPF_COL, brx->bgcolor |
323*4882a593Smuzhiyun 		       (0xff << VI6_BRU_VIRRPF_COL_A_SHIFT));
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/*
326*4882a593Smuzhiyun 	 * Route BRU input 1 as SRC input to the ROP unit and configure the ROP
327*4882a593Smuzhiyun 	 * unit with a NOP operation to make BRU input 1 available as the
328*4882a593Smuzhiyun 	 * Blend/ROP unit B SRC input. Only needed for BRU, the BRS has no ROP
329*4882a593Smuzhiyun 	 * unit.
330*4882a593Smuzhiyun 	 */
331*4882a593Smuzhiyun 	if (entity->type == VSP1_ENTITY_BRU)
332*4882a593Smuzhiyun 		vsp1_brx_write(brx, dlb, VI6_BRU_ROP,
333*4882a593Smuzhiyun 			       VI6_BRU_ROP_DSTSEL_BRUIN(1) |
334*4882a593Smuzhiyun 			       VI6_BRU_ROP_CROP(VI6_ROP_NOP) |
335*4882a593Smuzhiyun 			       VI6_BRU_ROP_AROP(VI6_ROP_NOP));
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	for (i = 0; i < brx->entity.source_pad; ++i) {
338*4882a593Smuzhiyun 		bool premultiplied = false;
339*4882a593Smuzhiyun 		u32 ctrl = 0;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 		/*
342*4882a593Smuzhiyun 		 * Configure all Blend/ROP units corresponding to an enabled BRx
343*4882a593Smuzhiyun 		 * input for alpha blending. Blend/ROP units corresponding to
344*4882a593Smuzhiyun 		 * disabled BRx inputs are used in ROP NOP mode to ignore the
345*4882a593Smuzhiyun 		 * SRC input.
346*4882a593Smuzhiyun 		 */
347*4882a593Smuzhiyun 		if (brx->inputs[i].rpf) {
348*4882a593Smuzhiyun 			ctrl |= VI6_BRU_CTRL_RBC;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 			premultiplied = brx->inputs[i].rpf->format.flags
351*4882a593Smuzhiyun 				      & V4L2_PIX_FMT_FLAG_PREMUL_ALPHA;
352*4882a593Smuzhiyun 		} else {
353*4882a593Smuzhiyun 			ctrl |= VI6_BRU_CTRL_CROP(VI6_ROP_NOP)
354*4882a593Smuzhiyun 			     |  VI6_BRU_CTRL_AROP(VI6_ROP_NOP);
355*4882a593Smuzhiyun 		}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		/*
358*4882a593Smuzhiyun 		 * Select the virtual RPF as the Blend/ROP unit A DST input to
359*4882a593Smuzhiyun 		 * serve as a background color.
360*4882a593Smuzhiyun 		 */
361*4882a593Smuzhiyun 		if (i == 0)
362*4882a593Smuzhiyun 			ctrl |= VI6_BRU_CTRL_DSTSEL_VRPF;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		/*
365*4882a593Smuzhiyun 		 * Route inputs 0 to 3 as SRC inputs to Blend/ROP units A to D
366*4882a593Smuzhiyun 		 * in that order. In the BRU the Blend/ROP unit B SRC is
367*4882a593Smuzhiyun 		 * hardwired to the ROP unit output, the corresponding register
368*4882a593Smuzhiyun 		 * bits must be set to 0. The BRS has no ROP unit and doesn't
369*4882a593Smuzhiyun 		 * need any special processing.
370*4882a593Smuzhiyun 		 */
371*4882a593Smuzhiyun 		if (!(entity->type == VSP1_ENTITY_BRU && i == 1))
372*4882a593Smuzhiyun 			ctrl |= VI6_BRU_CTRL_SRCSEL_BRUIN(i);
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 		vsp1_brx_write(brx, dlb, VI6_BRU_CTRL(i), ctrl);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 		/*
377*4882a593Smuzhiyun 		 * Hardcode the blending formula to
378*4882a593Smuzhiyun 		 *
379*4882a593Smuzhiyun 		 *	DSTc = DSTc * (1 - SRCa) + SRCc * SRCa
380*4882a593Smuzhiyun 		 *	DSTa = DSTa * (1 - SRCa) + SRCa
381*4882a593Smuzhiyun 		 *
382*4882a593Smuzhiyun 		 * when the SRC input isn't premultiplied, and to
383*4882a593Smuzhiyun 		 *
384*4882a593Smuzhiyun 		 *	DSTc = DSTc * (1 - SRCa) + SRCc
385*4882a593Smuzhiyun 		 *	DSTa = DSTa * (1 - SRCa) + SRCa
386*4882a593Smuzhiyun 		 *
387*4882a593Smuzhiyun 		 * otherwise.
388*4882a593Smuzhiyun 		 */
389*4882a593Smuzhiyun 		vsp1_brx_write(brx, dlb, VI6_BRU_BLD(i),
390*4882a593Smuzhiyun 			       VI6_BRU_BLD_CCMDX_255_SRC_A |
391*4882a593Smuzhiyun 			       (premultiplied ? VI6_BRU_BLD_CCMDY_COEFY :
392*4882a593Smuzhiyun 						VI6_BRU_BLD_CCMDY_SRC_A) |
393*4882a593Smuzhiyun 			       VI6_BRU_BLD_ACMDX_255_SRC_A |
394*4882a593Smuzhiyun 			       VI6_BRU_BLD_ACMDY_COEFY |
395*4882a593Smuzhiyun 			       (0xff << VI6_BRU_BLD_COEFY_SHIFT));
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun static const struct vsp1_entity_operations brx_entity_ops = {
400*4882a593Smuzhiyun 	.configure_stream = brx_configure_stream,
401*4882a593Smuzhiyun };
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /* -----------------------------------------------------------------------------
404*4882a593Smuzhiyun  * Initialization and Cleanup
405*4882a593Smuzhiyun  */
406*4882a593Smuzhiyun 
vsp1_brx_create(struct vsp1_device * vsp1,enum vsp1_entity_type type)407*4882a593Smuzhiyun struct vsp1_brx *vsp1_brx_create(struct vsp1_device *vsp1,
408*4882a593Smuzhiyun 				 enum vsp1_entity_type type)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun 	struct vsp1_brx *brx;
411*4882a593Smuzhiyun 	unsigned int num_pads;
412*4882a593Smuzhiyun 	const char *name;
413*4882a593Smuzhiyun 	int ret;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	brx = devm_kzalloc(vsp1->dev, sizeof(*brx), GFP_KERNEL);
416*4882a593Smuzhiyun 	if (brx == NULL)
417*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	brx->base = type == VSP1_ENTITY_BRU ? VI6_BRU_BASE : VI6_BRS_BASE;
420*4882a593Smuzhiyun 	brx->entity.ops = &brx_entity_ops;
421*4882a593Smuzhiyun 	brx->entity.type = type;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (type == VSP1_ENTITY_BRU) {
424*4882a593Smuzhiyun 		num_pads = vsp1->info->num_bru_inputs + 1;
425*4882a593Smuzhiyun 		name = "bru";
426*4882a593Smuzhiyun 	} else {
427*4882a593Smuzhiyun 		num_pads = 3;
428*4882a593Smuzhiyun 		name = "brs";
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	ret = vsp1_entity_init(vsp1, &brx->entity, name, num_pads, &brx_ops,
432*4882a593Smuzhiyun 			       MEDIA_ENT_F_PROC_VIDEO_COMPOSER);
433*4882a593Smuzhiyun 	if (ret < 0)
434*4882a593Smuzhiyun 		return ERR_PTR(ret);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	/* Initialize the control handler. */
437*4882a593Smuzhiyun 	v4l2_ctrl_handler_init(&brx->ctrls, 1);
438*4882a593Smuzhiyun 	v4l2_ctrl_new_std(&brx->ctrls, &brx_ctrl_ops, V4L2_CID_BG_COLOR,
439*4882a593Smuzhiyun 			  0, 0xffffff, 1, 0);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	brx->bgcolor = 0;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	brx->entity.subdev.ctrl_handler = &brx->ctrls;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (brx->ctrls.error) {
446*4882a593Smuzhiyun 		dev_err(vsp1->dev, "%s: failed to initialize controls\n", name);
447*4882a593Smuzhiyun 		ret = brx->ctrls.error;
448*4882a593Smuzhiyun 		vsp1_entity_destroy(&brx->entity);
449*4882a593Smuzhiyun 		return ERR_PTR(ret);
450*4882a593Smuzhiyun 	}
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	return brx;
453*4882a593Smuzhiyun }
454