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