xref: /linux/drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc.c (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 *         Tiffany Lin <tiffany.lin@mediatek.com>
6 */
7 
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
11 #include <linux/pm_runtime.h>
12 
13 #include "mtk_vcodec_enc.h"
14 #include "venc_drv_if.h"
15 
16 #define MTK_VENC_MIN_W	160U
17 #define MTK_VENC_MIN_H	128U
18 #define MTK_VENC_HD_MAX_W	1920U
19 #define MTK_VENC_HD_MAX_H	1088U
20 #define MTK_VENC_4K_MAX_W	3840U
21 #define MTK_VENC_4K_MAX_H	2176U
22 
23 #define DFT_CFG_WIDTH	MTK_VENC_MIN_W
24 #define DFT_CFG_HEIGHT	MTK_VENC_MIN_H
25 #define MTK_MAX_CTRLS_HINT	20
26 
27 #define MTK_DEFAULT_FRAMERATE_NUM 1001
28 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
29 #define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
30 
31 static void mtk_venc_worker(struct work_struct *work);
32 
33 static const struct v4l2_frmsize_stepwise mtk_venc_hd_framesizes = {
34 	MTK_VENC_MIN_W, MTK_VENC_HD_MAX_W, 16,
35 	MTK_VENC_MIN_H, MTK_VENC_HD_MAX_H, 16,
36 };
37 
38 static const struct v4l2_frmsize_stepwise mtk_venc_4k_framesizes = {
39 	MTK_VENC_MIN_W, MTK_VENC_4K_MAX_W, 16,
40 	MTK_VENC_MIN_H, MTK_VENC_4K_MAX_H, 16,
41 };
42 
43 static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
44 {
45 	struct mtk_vcodec_enc_ctx *ctx = ctrl_to_enc_ctx(ctrl);
46 	struct mtk_enc_params *p = &ctx->enc_params;
47 	int ret = 0;
48 
49 	switch (ctrl->id) {
50 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
51 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_BITRATE_MODE val= %d", ctrl->val);
52 		if (ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) {
53 			mtk_v4l2_venc_err(ctx, "Unsupported bitrate mode =%d", ctrl->val);
54 			ret = -EINVAL;
55 		}
56 		break;
57 	case V4L2_CID_MPEG_VIDEO_BITRATE:
58 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d", ctrl->val);
59 		p->bitrate = ctrl->val;
60 		ctx->param_change |= MTK_ENCODE_PARAM_BITRATE;
61 		break;
62 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
63 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d", ctrl->val);
64 		p->num_b_frame = ctrl->val;
65 		break;
66 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
67 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d",
68 				  ctrl->val);
69 		p->rc_frame = ctrl->val;
70 		break;
71 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
72 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d", ctrl->val);
73 		p->h264_max_qp = ctrl->val;
74 		break;
75 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
76 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d", ctrl->val);
77 		p->seq_hdr_mode = ctrl->val;
78 		break;
79 	case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
80 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d", ctrl->val);
81 		p->rc_mb = ctrl->val;
82 		break;
83 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
84 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d", ctrl->val);
85 		p->h264_profile = ctrl->val;
86 		break;
87 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
88 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d", ctrl->val);
89 		p->h264_level = ctrl->val;
90 		break;
91 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
92 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d", ctrl->val);
93 		p->intra_period = ctrl->val;
94 		ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD;
95 		break;
96 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
97 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d", ctrl->val);
98 		p->gop_size = ctrl->val;
99 		ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE;
100 		break;
101 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
102 		/*
103 		 * FIXME - what vp8 profiles are actually supported?
104 		 * The ctrl is added (with only profile 0 supported) for now.
105 		 */
106 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_VP8_PROFILE val = %d", ctrl->val);
107 		break;
108 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
109 		mtk_v4l2_venc_dbg(2, ctx, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME");
110 		p->force_intra = 1;
111 		ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA;
112 		break;
113 	default:
114 		ret = -EINVAL;
115 		break;
116 	}
117 
118 	return ret;
119 }
120 
121 static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = {
122 	.s_ctrl = vidioc_venc_s_ctrl,
123 };
124 
125 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f,
126 			   const struct mtk_video_fmt *formats,
127 			   size_t num_formats)
128 {
129 	if (f->index >= num_formats)
130 		return -EINVAL;
131 
132 	f->pixelformat = formats[f->index].fourcc;
133 
134 	return 0;
135 }
136 
137 static const struct mtk_video_fmt *
138 mtk_venc_find_format(u32 fourcc, const struct mtk_vcodec_enc_pdata *pdata)
139 {
140 	const struct mtk_video_fmt *fmt;
141 	unsigned int k;
142 
143 	for (k = 0; k < pdata->num_capture_formats; k++) {
144 		fmt = &pdata->capture_formats[k];
145 		if (fmt->fourcc == fourcc)
146 			return fmt;
147 	}
148 
149 	for (k = 0; k < pdata->num_output_formats; k++) {
150 		fmt = &pdata->output_formats[k];
151 		if (fmt->fourcc == fourcc)
152 			return fmt;
153 	}
154 
155 	return NULL;
156 }
157 
158 static int vidioc_enum_framesizes(struct file *file, void *fh,
159 				  struct v4l2_frmsizeenum *fsize)
160 {
161 	const struct mtk_video_fmt *fmt;
162 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
163 
164 	if (fsize->index != 0)
165 		return -EINVAL;
166 
167 	fmt = mtk_venc_find_format(fsize->pixel_format,
168 				   ctx->dev->venc_pdata);
169 	if (!fmt)
170 		return -EINVAL;
171 
172 	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
173 
174 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
175 		fsize->stepwise = mtk_venc_4k_framesizes;
176 	else
177 		fsize->stepwise = mtk_venc_hd_framesizes;
178 
179 	return 0;
180 }
181 
182 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
183 				   struct v4l2_fmtdesc *f)
184 {
185 	const struct mtk_vcodec_enc_pdata *pdata =
186 		file_to_enc_ctx(file)->dev->venc_pdata;
187 
188 	return vidioc_enum_fmt(f, pdata->capture_formats,
189 			       pdata->num_capture_formats);
190 }
191 
192 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
193 				   struct v4l2_fmtdesc *f)
194 {
195 	const struct mtk_vcodec_enc_pdata *pdata =
196 		file_to_enc_ctx(file)->dev->venc_pdata;
197 
198 	return vidioc_enum_fmt(f, pdata->output_formats,
199 			       pdata->num_output_formats);
200 }
201 
202 static int mtk_vcodec_enc_get_chip_name(struct mtk_vcodec_enc_ctx *ctx)
203 {
204 	struct device *dev = &ctx->dev->plat_dev->dev;
205 
206 	if (of_device_is_compatible(dev->of_node, "mediatek,mt8173-vcodec-enc"))
207 		return 8173;
208 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8183-vcodec-enc"))
209 		return 8183;
210 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-enc"))
211 		return 8192;
212 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-enc"))
213 		return 8195;
214 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8188-vcodec-enc"))
215 		return 8188;
216 	else
217 		return 8173;
218 }
219 
220 static int vidioc_venc_querycap(struct file *file, void *priv,
221 				struct v4l2_capability *cap)
222 {
223 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
224 	struct device *dev = &ctx->dev->plat_dev->dev;
225 	int platform_name = mtk_vcodec_enc_get_chip_name(ctx);
226 
227 	strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));
228 	snprintf(cap->card, sizeof(cap->card), "MT%d video encoder", platform_name);
229 
230 	return 0;
231 }
232 
233 static int vidioc_venc_s_parm(struct file *file, void *priv,
234 			      struct v4l2_streamparm *a)
235 {
236 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
237 	struct v4l2_fract *timeperframe = &a->parm.output.timeperframe;
238 
239 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
240 		return -EINVAL;
241 
242 	if (timeperframe->numerator == 0 || timeperframe->denominator == 0) {
243 		timeperframe->numerator = MTK_DEFAULT_FRAMERATE_NUM;
244 		timeperframe->denominator = MTK_DEFAULT_FRAMERATE_DENOM;
245 	}
246 
247 	ctx->enc_params.framerate_num = timeperframe->denominator;
248 	ctx->enc_params.framerate_denom = timeperframe->numerator;
249 	ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE;
250 
251 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
252 
253 	return 0;
254 }
255 
256 static int vidioc_venc_g_parm(struct file *file, void *priv,
257 			      struct v4l2_streamparm *a)
258 {
259 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
260 
261 	if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
262 		return -EINVAL;
263 
264 	a->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
265 	a->parm.output.timeperframe.denominator =
266 			ctx->enc_params.framerate_num;
267 	a->parm.output.timeperframe.numerator =
268 			ctx->enc_params.framerate_denom;
269 
270 	return 0;
271 }
272 
273 static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_enc_ctx *ctx,
274 					      enum v4l2_buf_type type)
275 {
276 	if (V4L2_TYPE_IS_OUTPUT(type))
277 		return &ctx->q_data[MTK_Q_DATA_SRC];
278 
279 	return &ctx->q_data[MTK_Q_DATA_DST];
280 }
281 
282 static void vidioc_try_fmt_cap(struct v4l2_format *f)
283 {
284 	f->fmt.pix_mp.field = V4L2_FIELD_NONE;
285 	f->fmt.pix_mp.num_planes = 1;
286 	f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
287 	f->fmt.pix_mp.flags = 0;
288 }
289 
290 /* V4L2 specification suggests the driver corrects the format struct if any of
291  * the dimensions is unsupported
292  */
293 static int vidioc_try_fmt_out(struct mtk_vcodec_enc_ctx *ctx, struct v4l2_format *f,
294 			      const struct mtk_video_fmt *fmt)
295 {
296 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
297 	int tmp_w, tmp_h;
298 	unsigned int max_width, max_height;
299 
300 	pix_fmt_mp->field = V4L2_FIELD_NONE;
301 
302 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE) {
303 		max_width = MTK_VENC_4K_MAX_W;
304 		max_height = MTK_VENC_4K_MAX_H;
305 	} else {
306 		max_width = MTK_VENC_HD_MAX_W;
307 		max_height = MTK_VENC_HD_MAX_H;
308 	}
309 
310 	pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VENC_MIN_H, max_height);
311 	pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VENC_MIN_W, max_width);
312 
313 	/* find next closer width align 16, height align 32, size align
314 	 * 64 rectangle
315 	 */
316 	tmp_w = pix_fmt_mp->width;
317 	tmp_h = pix_fmt_mp->height;
318 	v4l_bound_align_image(&pix_fmt_mp->width,
319 			      MTK_VENC_MIN_W,
320 			      max_width, 4,
321 			      &pix_fmt_mp->height,
322 			      MTK_VENC_MIN_H,
323 			      max_height, 5, 6);
324 
325 	if (pix_fmt_mp->width < tmp_w && (pix_fmt_mp->width + 16) <= max_width)
326 		pix_fmt_mp->width += 16;
327 	if (pix_fmt_mp->height < tmp_h && (pix_fmt_mp->height + 32) <= max_height)
328 		pix_fmt_mp->height += 32;
329 
330 	mtk_v4l2_venc_dbg(0, ctx,
331 			  "before resize wxh=%dx%d, after resize wxh=%dx%d, sizeimage=%d %d",
332 			  tmp_w, tmp_h, pix_fmt_mp->width,
333 			  pix_fmt_mp->height,
334 			  pix_fmt_mp->plane_fmt[0].sizeimage,
335 			  pix_fmt_mp->plane_fmt[1].sizeimage);
336 
337 	pix_fmt_mp->num_planes = fmt->num_planes;
338 	pix_fmt_mp->plane_fmt[0].sizeimage =
339 			pix_fmt_mp->width * pix_fmt_mp->height +
340 			((ALIGN(pix_fmt_mp->width, 16) * 2) * 16);
341 	pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
342 
343 	if (pix_fmt_mp->num_planes == 2) {
344 		pix_fmt_mp->plane_fmt[1].sizeimage =
345 			(pix_fmt_mp->width * pix_fmt_mp->height) / 2 +
346 			(ALIGN(pix_fmt_mp->width, 16) * 16);
347 		pix_fmt_mp->plane_fmt[2].sizeimage = 0;
348 		pix_fmt_mp->plane_fmt[1].bytesperline =
349 						pix_fmt_mp->width;
350 		pix_fmt_mp->plane_fmt[2].bytesperline = 0;
351 	} else if (pix_fmt_mp->num_planes == 3) {
352 		pix_fmt_mp->plane_fmt[1].sizeimage =
353 		pix_fmt_mp->plane_fmt[2].sizeimage =
354 			(pix_fmt_mp->width * pix_fmt_mp->height) / 4 +
355 			((ALIGN(pix_fmt_mp->width, 16) / 2) * 16);
356 		pix_fmt_mp->plane_fmt[1].bytesperline =
357 			pix_fmt_mp->plane_fmt[2].bytesperline =
358 			pix_fmt_mp->width / 2;
359 	}
360 
361 	pix_fmt_mp->flags = 0;
362 
363 	return 0;
364 }
365 
366 static void mtk_venc_set_param(struct mtk_vcodec_enc_ctx *ctx,
367 			       struct venc_enc_param *param)
368 {
369 	struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC];
370 	struct mtk_enc_params *enc_params = &ctx->enc_params;
371 
372 	switch (q_data_src->fmt->fourcc) {
373 	case V4L2_PIX_FMT_YUV420M:
374 		param->input_yuv_fmt = VENC_YUV_FORMAT_I420;
375 		break;
376 	case V4L2_PIX_FMT_YVU420M:
377 		param->input_yuv_fmt = VENC_YUV_FORMAT_YV12;
378 		break;
379 	case V4L2_PIX_FMT_NV12M:
380 		param->input_yuv_fmt = VENC_YUV_FORMAT_NV12;
381 		break;
382 	case V4L2_PIX_FMT_NV21M:
383 		param->input_yuv_fmt = VENC_YUV_FORMAT_NV21;
384 		break;
385 	default:
386 		mtk_v4l2_venc_err(ctx, "Unsupported fourcc =%d", q_data_src->fmt->fourcc);
387 		break;
388 	}
389 	param->h264_profile = enc_params->h264_profile;
390 	param->h264_level = enc_params->h264_level;
391 
392 	/* Config visible resolution */
393 	param->width = q_data_src->visible_width;
394 	param->height = q_data_src->visible_height;
395 	/* Config coded resolution */
396 	param->buf_width = q_data_src->coded_width;
397 	param->buf_height = q_data_src->coded_height;
398 	param->frm_rate = enc_params->framerate_num /
399 			enc_params->framerate_denom;
400 	param->intra_period = enc_params->intra_period;
401 	param->gop_size = enc_params->gop_size;
402 	param->bitrate = enc_params->bitrate;
403 
404 	mtk_v4l2_venc_dbg(0, ctx,
405 			  "fmt 0x%x, P/L %d/%d w/h %d/%d buf %d/%d fps/bps %d/%d gop %d i_per %d",
406 			  param->input_yuv_fmt, param->h264_profile,
407 			  param->h264_level, param->width, param->height,
408 			  param->buf_width, param->buf_height,
409 			  param->frm_rate, param->bitrate,
410 			  param->gop_size, param->intra_period);
411 }
412 
413 static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
414 			     struct v4l2_format *f)
415 {
416 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
417 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
418 	struct vb2_queue *vq;
419 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
420 	int i, ret;
421 	const struct mtk_video_fmt *fmt;
422 
423 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
424 
425 	if (vb2_is_busy(vq)) {
426 		mtk_v4l2_venc_err(ctx, "queue busy");
427 		return -EBUSY;
428 	}
429 
430 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
431 	if (!fmt) {
432 		fmt = &ctx->dev->venc_pdata->capture_formats[0];
433 		f->fmt.pix.pixelformat = fmt->fourcc;
434 	}
435 
436 	q_data->fmt = fmt;
437 	vidioc_try_fmt_cap(f);
438 
439 	q_data->coded_width = f->fmt.pix_mp.width;
440 	q_data->coded_height = f->fmt.pix_mp.height;
441 	q_data->field = f->fmt.pix_mp.field;
442 
443 	for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
444 		struct v4l2_plane_pix_format	*plane_fmt;
445 
446 		plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
447 		q_data->bytesperline[i]	= plane_fmt->bytesperline;
448 		q_data->sizeimage[i] = plane_fmt->sizeimage;
449 	}
450 
451 	if (ctx->state == MTK_STATE_FREE) {
452 		ret = venc_if_init(ctx, q_data->fmt->fourcc);
453 		if (ret) {
454 			mtk_v4l2_venc_err(ctx, "venc_if_init failed=%d, codec type=%x",
455 					  ret, q_data->fmt->fourcc);
456 			return -EBUSY;
457 		}
458 		ctx->state = MTK_STATE_INIT;
459 	}
460 
461 	return 0;
462 }
463 
464 static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
465 			     struct v4l2_format *f)
466 {
467 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
468 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
469 	struct vb2_queue *vq;
470 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
471 	int ret, i;
472 	const struct mtk_video_fmt *fmt;
473 
474 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
475 
476 	if (vb2_is_busy(vq)) {
477 		mtk_v4l2_venc_err(ctx, "queue busy");
478 		return -EBUSY;
479 	}
480 
481 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
482 	if (!fmt) {
483 		fmt = &ctx->dev->venc_pdata->output_formats[0];
484 		f->fmt.pix.pixelformat = fmt->fourcc;
485 	}
486 
487 	ret = vidioc_try_fmt_out(ctx, f, fmt);
488 	if (ret)
489 		return ret;
490 
491 	q_data->fmt = fmt;
492 	q_data->visible_width = f->fmt.pix_mp.width;
493 	q_data->visible_height = f->fmt.pix_mp.height;
494 	q_data->coded_width = f->fmt.pix_mp.width;
495 	q_data->coded_height = f->fmt.pix_mp.height;
496 
497 	q_data->field = f->fmt.pix_mp.field;
498 	ctx->colorspace = f->fmt.pix_mp.colorspace;
499 	ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
500 	ctx->quantization = f->fmt.pix_mp.quantization;
501 	ctx->xfer_func = f->fmt.pix_mp.xfer_func;
502 
503 	for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
504 		struct v4l2_plane_pix_format *plane_fmt;
505 
506 		plane_fmt = &f->fmt.pix_mp.plane_fmt[i];
507 		q_data->bytesperline[i] = plane_fmt->bytesperline;
508 		q_data->sizeimage[i] = plane_fmt->sizeimage;
509 	}
510 
511 	return 0;
512 }
513 
514 static int vidioc_venc_g_fmt(struct file *file, void *priv,
515 			     struct v4l2_format *f)
516 {
517 	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
518 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
519 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, f->type);
520 	int i;
521 
522 	pix->width = q_data->coded_width;
523 	pix->height = q_data->coded_height;
524 	pix->pixelformat = q_data->fmt->fourcc;
525 	pix->field = q_data->field;
526 	pix->num_planes = q_data->fmt->num_planes;
527 	for (i = 0; i < pix->num_planes; i++) {
528 		pix->plane_fmt[i].bytesperline = q_data->bytesperline[i];
529 		pix->plane_fmt[i].sizeimage = q_data->sizeimage[i];
530 	}
531 
532 	pix->flags = 0;
533 	pix->colorspace = ctx->colorspace;
534 	pix->ycbcr_enc = ctx->ycbcr_enc;
535 	pix->quantization = ctx->quantization;
536 	pix->xfer_func = ctx->xfer_func;
537 
538 	return 0;
539 }
540 
541 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
542 					 struct v4l2_format *f)
543 {
544 	const struct mtk_video_fmt *fmt;
545 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
546 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
547 
548 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
549 	if (!fmt) {
550 		fmt = &ctx->dev->venc_pdata->capture_formats[0];
551 		f->fmt.pix.pixelformat = fmt->fourcc;
552 	}
553 	f->fmt.pix_mp.colorspace = ctx->colorspace;
554 	f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc;
555 	f->fmt.pix_mp.quantization = ctx->quantization;
556 	f->fmt.pix_mp.xfer_func = ctx->xfer_func;
557 
558 	vidioc_try_fmt_cap(f);
559 
560 	return 0;
561 }
562 
563 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
564 					 struct v4l2_format *f)
565 {
566 	const struct mtk_video_fmt *fmt;
567 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
568 	const struct mtk_vcodec_enc_pdata *pdata = ctx->dev->venc_pdata;
569 
570 	fmt = mtk_venc_find_format(f->fmt.pix.pixelformat, pdata);
571 	if (!fmt) {
572 		fmt = &ctx->dev->venc_pdata->output_formats[0];
573 		f->fmt.pix.pixelformat = fmt->fourcc;
574 	}
575 	if (!f->fmt.pix_mp.colorspace) {
576 		f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709;
577 		f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
578 		f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
579 		f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
580 	}
581 
582 	return vidioc_try_fmt_out(ctx, f, fmt);
583 }
584 
585 static int vidioc_venc_g_selection(struct file *file, void *priv,
586 				     struct v4l2_selection *s)
587 {
588 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
589 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
590 
591 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
592 		return -EINVAL;
593 
594 	switch (s->target) {
595 	case V4L2_SEL_TGT_CROP_DEFAULT:
596 	case V4L2_SEL_TGT_CROP_BOUNDS:
597 		s->r.top = 0;
598 		s->r.left = 0;
599 		s->r.width = q_data->coded_width;
600 		s->r.height = q_data->coded_height;
601 		break;
602 	case V4L2_SEL_TGT_CROP:
603 		s->r.top = 0;
604 		s->r.left = 0;
605 		s->r.width = q_data->visible_width;
606 		s->r.height = q_data->visible_height;
607 		break;
608 	default:
609 		return -EINVAL;
610 	}
611 
612 	return 0;
613 }
614 
615 static int vidioc_venc_s_selection(struct file *file, void *priv,
616 				     struct v4l2_selection *s)
617 {
618 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
619 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, s->type);
620 
621 	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
622 		return -EINVAL;
623 
624 	switch (s->target) {
625 	case V4L2_SEL_TGT_CROP:
626 		/* Only support crop from (0,0) */
627 		s->r.top = 0;
628 		s->r.left = 0;
629 		s->r.width = min(s->r.width, q_data->coded_width);
630 		s->r.height = min(s->r.height, q_data->coded_height);
631 		q_data->visible_width = s->r.width;
632 		q_data->visible_height = s->r.height;
633 		break;
634 	default:
635 		return -EINVAL;
636 	}
637 	return 0;
638 }
639 
640 static int vidioc_venc_qbuf(struct file *file, void *priv,
641 			    struct v4l2_buffer *buf)
642 {
643 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
644 
645 	if (ctx->state == MTK_STATE_ABORT) {
646 		mtk_v4l2_venc_err(ctx, "[%d] Call on QBUF after unrecoverable error",
647 				  ctx->id);
648 		return -EIO;
649 	}
650 
651 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
652 }
653 
654 static int vidioc_venc_dqbuf(struct file *file, void *priv,
655 			     struct v4l2_buffer *buf)
656 {
657 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
658 	int ret;
659 
660 	if (ctx->state == MTK_STATE_ABORT) {
661 		mtk_v4l2_venc_err(ctx, "[%d] Call on QBUF after unrecoverable error",
662 				  ctx->id);
663 		return -EIO;
664 	}
665 
666 	ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
667 	if (ret)
668 		return ret;
669 
670 	/*
671 	 * Complete flush if the user dequeued the 0-payload LAST buffer.
672 	 * We check the payload because a buffer with the LAST flag can also
673 	 * be seen during resolution changes. If we happen to be flushing at
674 	 * that time, the last buffer before the resolution changes could be
675 	 * misinterpreted for the buffer generated by the flush and terminate
676 	 * it earlier than we want.
677 	 */
678 	if (!V4L2_TYPE_IS_OUTPUT(buf->type) &&
679 	    buf->flags & V4L2_BUF_FLAG_LAST &&
680 	    buf->m.planes[0].bytesused == 0 &&
681 	    ctx->is_flushing) {
682 		/*
683 		 * Last CAPTURE buffer is dequeued, we can allow another flush
684 		 * to take place.
685 		 */
686 		ctx->is_flushing = false;
687 	}
688 
689 	return 0;
690 }
691 
692 static int vidioc_encoder_cmd(struct file *file, void *priv,
693 			      struct v4l2_encoder_cmd *cmd)
694 {
695 	struct mtk_vcodec_enc_ctx *ctx = file_to_enc_ctx(file);
696 	struct vb2_queue *src_vq, *dst_vq;
697 	int ret;
698 
699 	if (ctx->state == MTK_STATE_ABORT) {
700 		mtk_v4l2_venc_err(ctx, "[%d] Call to CMD after unrecoverable error",
701 				  ctx->id);
702 		return -EIO;
703 	}
704 
705 	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, cmd);
706 	if (ret)
707 		return ret;
708 
709 	/* Calling START or STOP is invalid if a flush is in progress */
710 	if (ctx->is_flushing)
711 		return -EBUSY;
712 
713 	mtk_v4l2_venc_dbg(1, ctx, "encoder cmd=%u", cmd->cmd);
714 
715 	dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
716 				 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
717 	switch (cmd->cmd) {
718 	case V4L2_ENC_CMD_STOP:
719 		src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
720 					 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
721 		if (!vb2_is_streaming(src_vq)) {
722 			mtk_v4l2_venc_dbg(1, ctx, "Output stream is off. No need to flush.");
723 			return 0;
724 		}
725 		if (!vb2_is_streaming(dst_vq)) {
726 			mtk_v4l2_venc_dbg(1, ctx, "Capture stream is off. No need to flush.");
727 			return 0;
728 		}
729 		ctx->is_flushing = true;
730 		v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);
731 		v4l2_m2m_try_schedule(ctx->m2m_ctx);
732 		break;
733 
734 	case V4L2_ENC_CMD_START:
735 		vb2_clear_last_buffer_dequeued(dst_vq);
736 		break;
737 
738 	default:
739 		return -EINVAL;
740 	}
741 
742 	return 0;
743 }
744 
745 const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = {
746 	.vidioc_streamon		= v4l2_m2m_ioctl_streamon,
747 	.vidioc_streamoff		= v4l2_m2m_ioctl_streamoff,
748 
749 	.vidioc_reqbufs			= v4l2_m2m_ioctl_reqbufs,
750 	.vidioc_querybuf		= v4l2_m2m_ioctl_querybuf,
751 	.vidioc_qbuf			= vidioc_venc_qbuf,
752 	.vidioc_dqbuf			= vidioc_venc_dqbuf,
753 
754 	.vidioc_querycap		= vidioc_venc_querycap,
755 	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt_vid_cap,
756 	.vidioc_enum_fmt_vid_out	= vidioc_enum_fmt_vid_out,
757 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
758 
759 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
760 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
761 	.vidioc_expbuf			= v4l2_m2m_ioctl_expbuf,
762 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
763 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
764 
765 	.vidioc_s_parm			= vidioc_venc_s_parm,
766 	.vidioc_g_parm			= vidioc_venc_g_parm,
767 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_venc_s_fmt_cap,
768 	.vidioc_s_fmt_vid_out_mplane	= vidioc_venc_s_fmt_out,
769 
770 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_venc_g_fmt,
771 	.vidioc_g_fmt_vid_out_mplane	= vidioc_venc_g_fmt,
772 
773 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
774 	.vidioc_prepare_buf		= v4l2_m2m_ioctl_prepare_buf,
775 
776 	.vidioc_g_selection		= vidioc_venc_g_selection,
777 	.vidioc_s_selection		= vidioc_venc_s_selection,
778 
779 	.vidioc_encoder_cmd		= vidioc_encoder_cmd,
780 	.vidioc_try_encoder_cmd		= v4l2_m2m_ioctl_try_encoder_cmd,
781 };
782 
783 static int vb2ops_venc_queue_setup(struct vb2_queue *vq,
784 				   unsigned int *nbuffers,
785 				   unsigned int *nplanes,
786 				   unsigned int sizes[],
787 				   struct device *alloc_devs[])
788 {
789 	struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vq);
790 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vq->type);
791 	unsigned int i;
792 
793 	if (q_data == NULL)
794 		return -EINVAL;
795 
796 	if (*nplanes) {
797 		if (*nplanes != q_data->fmt->num_planes)
798 			return -EINVAL;
799 		for (i = 0; i < *nplanes; i++)
800 			if (sizes[i] < q_data->sizeimage[i])
801 				return -EINVAL;
802 	} else {
803 		*nplanes = q_data->fmt->num_planes;
804 		for (i = 0; i < *nplanes; i++)
805 			sizes[i] = q_data->sizeimage[i];
806 	}
807 
808 	return 0;
809 }
810 
811 static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb)
812 {
813 	struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
814 	struct mtk_q_data *q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type);
815 	int i;
816 
817 	for (i = 0; i < q_data->fmt->num_planes; i++) {
818 		if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
819 			mtk_v4l2_venc_err(ctx, "data will not fit into plane %d (%lu < %d)",
820 					  i, vb2_plane_size(vb, i), q_data->sizeimage[i]);
821 			return -EINVAL;
822 		}
823 	}
824 
825 	return 0;
826 }
827 
828 static void vb2ops_venc_buf_queue(struct vb2_buffer *vb)
829 {
830 	struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
831 	struct vb2_v4l2_buffer *vb2_v4l2 =
832 			container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
833 
834 	struct mtk_video_enc_buf *mtk_buf =
835 			container_of(vb2_v4l2, struct mtk_video_enc_buf,
836 				     m2m_buf.vb);
837 
838 	if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
839 	    (ctx->param_change != MTK_ENCODE_PARAM_NONE)) {
840 		mtk_v4l2_venc_dbg(1, ctx, "[%d] Before id=%d encode parameter change %x",
841 				  ctx->id, vb2_v4l2->vb2_buf.index, ctx->param_change);
842 		mtk_buf->param_change = ctx->param_change;
843 		mtk_buf->enc_params = ctx->enc_params;
844 		ctx->param_change = MTK_ENCODE_PARAM_NONE;
845 	}
846 
847 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
848 }
849 
850 static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count)
851 {
852 	struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(q);
853 	struct venc_enc_param param;
854 	int ret;
855 	int i;
856 
857 	/* Once state turn into MTK_STATE_ABORT, we need stop_streaming
858 	  * to clear it
859 	  */
860 	if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) {
861 		ret = -EIO;
862 		goto err_start_stream;
863 	}
864 
865 	/* Do the initialization when both start_streaming have been called */
866 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
867 		if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q))
868 			return 0;
869 	} else {
870 		if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q))
871 			return 0;
872 	}
873 
874 	mtk_venc_set_param(ctx, &param);
875 	ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, &param);
876 	if (ret) {
877 		mtk_v4l2_venc_err(ctx, "venc_if_set_param failed=%d", ret);
878 		ctx->state = MTK_STATE_ABORT;
879 		goto err_start_stream;
880 	}
881 	ctx->param_change = MTK_ENCODE_PARAM_NONE;
882 
883 	if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
884 	    (ctx->enc_params.seq_hdr_mode !=
885 				V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) {
886 		ret = venc_if_set_param(ctx,
887 					VENC_SET_PARAM_PREPEND_HEADER,
888 					NULL);
889 		if (ret) {
890 			mtk_v4l2_venc_err(ctx, "venc_if_set_param failed=%d", ret);
891 			ctx->state = MTK_STATE_ABORT;
892 			goto err_start_stream;
893 		}
894 		ctx->state = MTK_STATE_HEADER;
895 	}
896 
897 	return 0;
898 
899 err_start_stream:
900 	for (i = 0; i < vb2_get_num_buffers(q); ++i) {
901 		struct vb2_buffer *buf = vb2_get_buffer(q, i);
902 
903 		/*
904 		 * FIXME: This check is not needed as only active buffers
905 		 * can be marked as done.
906 		 */
907 		if (buf && buf->state == VB2_BUF_STATE_ACTIVE) {
908 			mtk_v4l2_venc_dbg(0, ctx, "[%d] id=%d, type=%d, %d->VB2_BUF_STATE_QUEUED",
909 					  ctx->id, i, q->type, (int)buf->state);
910 			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(buf),
911 					  VB2_BUF_STATE_QUEUED);
912 		}
913 	}
914 
915 	return ret;
916 }
917 
918 static void vb2ops_venc_stop_streaming(struct vb2_queue *q)
919 {
920 	struct mtk_vcodec_enc_ctx *ctx = vb2_get_drv_priv(q);
921 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
922 	int ret;
923 
924 	mtk_v4l2_venc_dbg(2, ctx, "[%d]-> type=%d", ctx->id, q->type);
925 
926 	if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
927 		while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
928 			vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
929 			v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
930 		}
931 		/* STREAMOFF on the CAPTURE queue completes any ongoing flush */
932 		if (ctx->is_flushing) {
933 			struct v4l2_m2m_buffer *b, *n;
934 
935 			mtk_v4l2_venc_dbg(1, ctx, "STREAMOFF called while flushing");
936 			/*
937 			 * STREAMOFF could be called before the flush buffer is
938 			 * dequeued. Check whether empty flush buf is still in
939 			 * queue before removing it.
940 			 */
941 			v4l2_m2m_for_each_src_buf_safe(ctx->m2m_ctx, b, n) {
942 				if (b == &ctx->empty_flush_buf) {
943 					v4l2_m2m_src_buf_remove_by_buf(ctx->m2m_ctx, &b->vb);
944 					break;
945 				}
946 			}
947 			ctx->is_flushing = false;
948 		}
949 	} else {
950 		while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
951 			if (src_buf != &ctx->empty_flush_buf.vb)
952 				v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
953 		}
954 		if (ctx->is_flushing) {
955 			/*
956 			 * If we are in the middle of a flush, put the flush
957 			 * buffer back into the queue so the next CAPTURE
958 			 * buffer gets returned with the LAST flag set.
959 			 */
960 			v4l2_m2m_buf_queue(ctx->m2m_ctx,
961 					   &ctx->empty_flush_buf.vb);
962 		}
963 	}
964 
965 	if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
966 	     vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) ||
967 	    (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
968 	     vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) {
969 		mtk_v4l2_venc_dbg(1, ctx, "[%d]-> q type %d out=%d cap=%d",
970 				  ctx->id, q->type,
971 				  vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q),
972 				  vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q));
973 		return;
974 	}
975 
976 	/* Release the encoder if both streams are stopped. */
977 	ret = venc_if_deinit(ctx);
978 	if (ret)
979 		mtk_v4l2_venc_err(ctx, "venc_if_deinit failed=%d", ret);
980 
981 	ctx->state = MTK_STATE_FREE;
982 }
983 
984 static int vb2ops_venc_buf_out_validate(struct vb2_buffer *vb)
985 {
986 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
987 
988 	vbuf->field = V4L2_FIELD_NONE;
989 	return 0;
990 }
991 
992 static const struct vb2_ops mtk_venc_vb2_ops = {
993 	.queue_setup		= vb2ops_venc_queue_setup,
994 	.buf_out_validate	= vb2ops_venc_buf_out_validate,
995 	.buf_prepare		= vb2ops_venc_buf_prepare,
996 	.buf_queue		= vb2ops_venc_buf_queue,
997 	.start_streaming	= vb2ops_venc_start_streaming,
998 	.stop_streaming		= vb2ops_venc_stop_streaming,
999 };
1000 
1001 static int mtk_venc_encode_header(void *priv)
1002 {
1003 	struct mtk_vcodec_enc_ctx *ctx = priv;
1004 	int ret;
1005 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1006 	struct mtk_vcodec_mem bs_buf;
1007 	struct venc_done_result enc_result;
1008 
1009 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1010 	if (!dst_buf) {
1011 		mtk_v4l2_venc_dbg(1, ctx, "No dst buffer");
1012 		return -EINVAL;
1013 	}
1014 
1015 	bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1016 	bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1017 	bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1018 
1019 	mtk_v4l2_venc_dbg(1, ctx,
1020 			  "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu",
1021 			  ctx->id, dst_buf->vb2_buf.index, bs_buf.va,
1022 			  (u64)bs_buf.dma_addr, bs_buf.size);
1023 
1024 	ret = venc_if_encode(ctx,
1025 			VENC_START_OPT_ENCODE_SEQUENCE_HEADER,
1026 			NULL, &bs_buf, &enc_result);
1027 
1028 	if (ret) {
1029 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1030 		ctx->state = MTK_STATE_ABORT;
1031 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1032 		mtk_v4l2_venc_err(ctx, "venc_if_encode failed=%d", ret);
1033 		return -EINVAL;
1034 	}
1035 	src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1036 	if (src_buf) {
1037 		dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1038 		dst_buf->timecode = src_buf->timecode;
1039 	} else {
1040 		mtk_v4l2_venc_err(ctx, "No timestamp for the header buffer.");
1041 	}
1042 
1043 	ctx->state = MTK_STATE_HEADER;
1044 	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1045 	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1046 
1047 	return 0;
1048 }
1049 
1050 static int mtk_venc_param_change(struct mtk_vcodec_enc_ctx *ctx)
1051 {
1052 	struct venc_enc_param enc_prm;
1053 	struct vb2_v4l2_buffer *vb2_v4l2 = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1054 	struct mtk_video_enc_buf *mtk_buf;
1055 	int ret = 0;
1056 
1057 	/* Don't upcast the empty flush buffer */
1058 	if (vb2_v4l2 == &ctx->empty_flush_buf.vb)
1059 		return 0;
1060 
1061 	mtk_buf = container_of(vb2_v4l2, struct mtk_video_enc_buf, m2m_buf.vb);
1062 
1063 	memset(&enc_prm, 0, sizeof(enc_prm));
1064 	if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE)
1065 		return 0;
1066 
1067 	if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) {
1068 		enc_prm.bitrate = mtk_buf->enc_params.bitrate;
1069 		mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param br=%d",
1070 				  ctx->id, vb2_v4l2->vb2_buf.index, enc_prm.bitrate);
1071 		ret |= venc_if_set_param(ctx,
1072 					 VENC_SET_PARAM_ADJUST_BITRATE,
1073 					 &enc_prm);
1074 	}
1075 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) {
1076 		enc_prm.frm_rate = mtk_buf->enc_params.framerate_num /
1077 				   mtk_buf->enc_params.framerate_denom;
1078 		mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param fr=%d",
1079 				  ctx->id, vb2_v4l2->vb2_buf.index, enc_prm.frm_rate);
1080 		ret |= venc_if_set_param(ctx,
1081 					 VENC_SET_PARAM_ADJUST_FRAMERATE,
1082 					 &enc_prm);
1083 	}
1084 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) {
1085 		enc_prm.gop_size = mtk_buf->enc_params.gop_size;
1086 		mtk_v4l2_venc_dbg(1, ctx, "change param intra period=%d", enc_prm.gop_size);
1087 		ret |= venc_if_set_param(ctx,
1088 					 VENC_SET_PARAM_GOP_SIZE,
1089 					 &enc_prm);
1090 	}
1091 	if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) {
1092 		mtk_v4l2_venc_dbg(1, ctx, "[%d] id=%d, change param force I=%d",
1093 				  ctx->id, vb2_v4l2->vb2_buf.index,
1094 				  mtk_buf->enc_params.force_intra);
1095 		if (mtk_buf->enc_params.force_intra)
1096 			ret |= venc_if_set_param(ctx,
1097 						 VENC_SET_PARAM_FORCE_INTRA,
1098 						 NULL);
1099 	}
1100 
1101 	mtk_buf->param_change = MTK_ENCODE_PARAM_NONE;
1102 
1103 	if (ret) {
1104 		ctx->state = MTK_STATE_ABORT;
1105 		mtk_v4l2_venc_err(ctx, "venc_if_set_param %d failed=%d",
1106 				  mtk_buf->param_change, ret);
1107 		return -1;
1108 	}
1109 
1110 	return 0;
1111 }
1112 
1113 /*
1114  * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker()
1115  * to call v4l2_m2m_job_finish().
1116  * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock.
1117  * So this function must not try to acquire dev->dev_mutex.
1118  * This means v4l2 ioctls and mtk_venc_worker() can run at the same time.
1119  * mtk_venc_worker() should be carefully implemented to avoid bugs.
1120  */
1121 static void mtk_venc_worker(struct work_struct *work)
1122 {
1123 	struct mtk_vcodec_enc_ctx *ctx = container_of(work, struct mtk_vcodec_enc_ctx,
1124 				    encode_work);
1125 	struct vb2_v4l2_buffer *src_buf, *dst_buf;
1126 	struct venc_frm_buf frm_buf;
1127 	struct mtk_vcodec_mem bs_buf;
1128 	struct venc_done_result enc_result;
1129 	int ret, i;
1130 
1131 	/* check dst_buf, dst_buf may be removed in device_run
1132 	 * to stored encdoe header so we need check dst_buf and
1133 	 * call job_finish here to prevent recursion
1134 	 */
1135 	dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
1136 	if (!dst_buf) {
1137 		v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1138 		return;
1139 	}
1140 
1141 	src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1142 
1143 	/*
1144 	 * If we see the flush buffer, send an empty buffer with the LAST flag
1145 	 * to the client. is_flushing will be reset at the time the buffer
1146 	 * is dequeued.
1147 	 */
1148 	if (src_buf == &ctx->empty_flush_buf.vb) {
1149 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1150 		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
1151 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1152 		v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1153 		return;
1154 	}
1155 
1156 	memset(&frm_buf, 0, sizeof(frm_buf));
1157 	for (i = 0; i < src_buf->vb2_buf.num_planes ; i++) {
1158 		frm_buf.fb_addr[i].dma_addr =
1159 				vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, i);
1160 		frm_buf.fb_addr[i].size =
1161 				(size_t)src_buf->vb2_buf.planes[i].length;
1162 	}
1163 	bs_buf.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
1164 	bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
1165 	bs_buf.size = (size_t)dst_buf->vb2_buf.planes[0].length;
1166 
1167 	mtk_v4l2_venc_dbg(2, ctx,
1168 			  "Framebuf PA=%llx Size=0x%zx;PA=0x%llx Size=0x%zx;PA=0x%llx Size=%zu",
1169 			  (u64)frm_buf.fb_addr[0].dma_addr, frm_buf.fb_addr[0].size,
1170 			  (u64)frm_buf.fb_addr[1].dma_addr, frm_buf.fb_addr[1].size,
1171 			  (u64)frm_buf.fb_addr[2].dma_addr, frm_buf.fb_addr[2].size);
1172 
1173 	ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME,
1174 			     &frm_buf, &bs_buf, &enc_result);
1175 
1176 	dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
1177 	dst_buf->timecode = src_buf->timecode;
1178 
1179 	if (enc_result.is_key_frm)
1180 		dst_buf->flags |= V4L2_BUF_FLAG_KEYFRAME;
1181 
1182 	if (ret) {
1183 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1184 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1185 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1186 		mtk_v4l2_venc_err(ctx, "venc_if_encode failed=%d", ret);
1187 	} else {
1188 		v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1189 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, enc_result.bs_size);
1190 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
1191 		mtk_v4l2_venc_dbg(2, ctx, "venc_if_encode bs size=%d",
1192 				  enc_result.bs_size);
1193 	}
1194 
1195 	v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx);
1196 
1197 	mtk_v4l2_venc_dbg(1, ctx, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>",
1198 			  src_buf->vb2_buf.index, dst_buf->vb2_buf.index, ret, enc_result.bs_size);
1199 }
1200 
1201 static void m2mops_venc_device_run(void *priv)
1202 {
1203 	struct mtk_vcodec_enc_ctx *ctx = priv;
1204 
1205 	if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) &&
1206 	    (ctx->state != MTK_STATE_HEADER)) {
1207 		/* encode h264 sps/pps header */
1208 		mtk_venc_encode_header(ctx);
1209 		queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1210 		return;
1211 	}
1212 
1213 	mtk_venc_param_change(ctx);
1214 	queue_work(ctx->dev->encode_workqueue, &ctx->encode_work);
1215 }
1216 
1217 static int m2mops_venc_job_ready(void *m2m_priv)
1218 {
1219 	struct mtk_vcodec_enc_ctx *ctx = m2m_priv;
1220 
1221 	if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) {
1222 		mtk_v4l2_venc_dbg(3, ctx, "[%d]Not ready: state=0x%x.", ctx->id, ctx->state);
1223 		return 0;
1224 	}
1225 
1226 	return 1;
1227 }
1228 
1229 static void m2mops_venc_job_abort(void *priv)
1230 {
1231 	struct mtk_vcodec_enc_ctx *ctx = priv;
1232 
1233 	ctx->state = MTK_STATE_ABORT;
1234 }
1235 
1236 const struct v4l2_m2m_ops mtk_venc_m2m_ops = {
1237 	.device_run	= m2mops_venc_device_run,
1238 	.job_ready	= m2mops_venc_job_ready,
1239 	.job_abort	= m2mops_venc_job_abort,
1240 };
1241 
1242 void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_enc_ctx *ctx)
1243 {
1244 	struct mtk_q_data *q_data;
1245 
1246 	ctx->m2m_ctx->q_lock = &ctx->q_mutex;
1247 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
1248 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1249 	INIT_WORK(&ctx->encode_work, mtk_venc_worker);
1250 
1251 	ctx->colorspace = V4L2_COLORSPACE_REC709;
1252 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1253 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1254 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1255 
1256 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
1257 	memset(q_data, 0, sizeof(struct mtk_q_data));
1258 	q_data->visible_width = DFT_CFG_WIDTH;
1259 	q_data->visible_height = DFT_CFG_HEIGHT;
1260 	q_data->coded_width = DFT_CFG_WIDTH;
1261 	q_data->coded_height = DFT_CFG_HEIGHT;
1262 	q_data->field = V4L2_FIELD_NONE;
1263 
1264 	q_data->fmt = &ctx->dev->venc_pdata->output_formats[0];
1265 
1266 	v4l_bound_align_image(&q_data->coded_width,
1267 				MTK_VENC_MIN_W,
1268 				MTK_VENC_HD_MAX_W, 4,
1269 				&q_data->coded_height,
1270 				MTK_VENC_MIN_H,
1271 				MTK_VENC_HD_MAX_H, 5, 6);
1272 
1273 	if (q_data->coded_width < DFT_CFG_WIDTH &&
1274 		(q_data->coded_width + 16) <= MTK_VENC_HD_MAX_W)
1275 		q_data->coded_width += 16;
1276 	if (q_data->coded_height < DFT_CFG_HEIGHT &&
1277 		(q_data->coded_height + 32) <= MTK_VENC_HD_MAX_H)
1278 		q_data->coded_height += 32;
1279 
1280 	q_data->sizeimage[0] =
1281 		q_data->coded_width * q_data->coded_height+
1282 		((ALIGN(q_data->coded_width, 16) * 2) * 16);
1283 	q_data->bytesperline[0] = q_data->coded_width;
1284 	q_data->sizeimage[1] =
1285 		(q_data->coded_width * q_data->coded_height) / 2 +
1286 		(ALIGN(q_data->coded_width, 16) * 16);
1287 	q_data->bytesperline[1] = q_data->coded_width;
1288 
1289 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
1290 	memset(q_data, 0, sizeof(struct mtk_q_data));
1291 	q_data->coded_width = DFT_CFG_WIDTH;
1292 	q_data->coded_height = DFT_CFG_HEIGHT;
1293 	q_data->fmt = &ctx->dev->venc_pdata->capture_formats[0];
1294 	q_data->field = V4L2_FIELD_NONE;
1295 	ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
1296 		DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
1297 	ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0;
1298 
1299 	ctx->enc_params.framerate_num = MTK_DEFAULT_FRAMERATE_NUM;
1300 	ctx->enc_params.framerate_denom = MTK_DEFAULT_FRAMERATE_DENOM;
1301 }
1302 
1303 int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_enc_ctx *ctx)
1304 {
1305 	const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
1306 	struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
1307 	u8 h264_max_level;
1308 
1309 	if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
1310 		h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
1311 	else
1312 		h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
1313 
1314 	v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
1315 
1316 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
1317 			  1, 1, 1, 1);
1318 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE,
1319 			  ctx->dev->venc_pdata->min_bitrate,
1320 			  ctx->dev->venc_pdata->max_bitrate, 1, 4000000);
1321 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES,
1322 			0, 2, 1, 0);
1323 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
1324 			0, 1, 1, 1);
1325 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
1326 			0, 51, 1, 51);
1327 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD,
1328 			0, 65535, 1, 0);
1329 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1330 			0, 65535, 1, 0);
1331 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
1332 			0, 1, 1, 0);
1333 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
1334 			0, 0, 0, 0);
1335 	v4l2_ctrl_new_std_menu(handler, ops,
1336 			V4L2_CID_MPEG_VIDEO_HEADER_MODE,
1337 			V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
1338 			0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
1339 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE,
1340 			V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
1341 			~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
1342 			  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
1343 			  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
1344 			V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
1345 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
1346 			       h264_max_level,
1347 			       0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
1348 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
1349 			       V4L2_MPEG_VIDEO_VP8_PROFILE_0, 0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
1350 	v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1351 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
1352 			       ~(1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR),
1353 			       V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1354 
1355 
1356 	if (handler->error) {
1357 		mtk_v4l2_venc_err(ctx, "Init control handler fail %d", handler->error);
1358 		return handler->error;
1359 	}
1360 
1361 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1362 
1363 	return 0;
1364 }
1365 
1366 int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq,
1367 			      struct vb2_queue *dst_vq)
1368 {
1369 	struct mtk_vcodec_enc_ctx *ctx = priv;
1370 	int ret;
1371 
1372 	/* Note: VB2_USERPTR works with dma-contig because mt8173
1373 	 * support iommu
1374 	 * https://patchwork.kernel.org/patch/8335461/
1375 	 * https://patchwork.kernel.org/patch/7596181/
1376 	 */
1377 	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1378 	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1379 	src_vq->drv_priv	= ctx;
1380 	src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf);
1381 	src_vq->ops		= &mtk_venc_vb2_ops;
1382 	src_vq->mem_ops		= &vb2_dma_contig_memops;
1383 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1384 	src_vq->lock		= &ctx->q_mutex;
1385 	src_vq->dev		= &ctx->dev->plat_dev->dev;
1386 
1387 	ret = vb2_queue_init(src_vq);
1388 	if (ret)
1389 		return ret;
1390 
1391 	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1392 	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
1393 	dst_vq->drv_priv	= ctx;
1394 	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1395 	dst_vq->ops		= &mtk_venc_vb2_ops;
1396 	dst_vq->mem_ops		= &vb2_dma_contig_memops;
1397 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1398 	dst_vq->lock		= &ctx->q_mutex;
1399 	dst_vq->dev		= &ctx->dev->plat_dev->dev;
1400 
1401 	return vb2_queue_init(dst_vq);
1402 }
1403 
1404 int mtk_venc_unlock(struct mtk_vcodec_enc_ctx *ctx)
1405 {
1406 	struct mtk_vcodec_enc_dev *dev = ctx->dev;
1407 
1408 	mutex_unlock(&dev->enc_mutex);
1409 	return 0;
1410 }
1411 
1412 int mtk_venc_lock(struct mtk_vcodec_enc_ctx *ctx)
1413 {
1414 	struct mtk_vcodec_enc_dev *dev = ctx->dev;
1415 
1416 	mutex_lock(&dev->enc_mutex);
1417 	return 0;
1418 }
1419 
1420 void mtk_vcodec_enc_release(struct mtk_vcodec_enc_ctx *ctx)
1421 {
1422 	int ret = venc_if_deinit(ctx);
1423 
1424 	if (ret)
1425 		mtk_v4l2_venc_err(ctx, "venc_if_deinit failed=%d", ret);
1426 
1427 	ctx->state = MTK_STATE_FREE;
1428 }
1429