CHROMIUM: [media] rk3288-vpu: Add V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / rk3288-vpu / rk3288_vpu_enc.c
1 /*
2  * Rockchip RK3288 VPU codec driver
3  *
4  * Copyright (C) 2014 Rockchip Electronics Co., Ltd.
5  *      Alpha Lin <Alpha.Lin@rock-chips.com>
6  *      Jeffy Chen <jeffy.chen@rock-chips.com>
7  *
8  * Copyright (C) 2014 Google, Inc.
9  *      Tomasz Figa <tfiga@chromium.org>
10  *
11  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
12  *
13  * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
14  *
15  * This software is licensed under the terms of the GNU General Public
16  * License version 2, as published by the Free Software Foundation, and
17  * may be copied, distributed, and modified under those terms.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  */
24
25 #include "rk3288_vpu_common.h"
26
27 #include <linux/clk.h>
28 #include <linux/interrupt.h>
29 #include <linux/io.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/sched.h>
33 #include <linux/version.h>
34 #include <linux/videodev2.h>
35 #include <media/v4l2-event.h>
36 #include <linux/workqueue.h>
37 #include <media/v4l2-ctrls.h>
38 #include <media/videobuf2-core.h>
39 #include <media/videobuf2-dma-sg.h>
40
41 #include "rk3288_vpu_enc.h"
42 #include "rk3288_vpu_hw.h"
43
44 #define DEF_SRC_FMT_ENC                         V4L2_PIX_FMT_NV12
45 #define DEF_DST_FMT_ENC                         V4L2_PIX_FMT_VP8
46
47 #define RK3288_ENC_MIN_WIDTH                    96U
48 #define RK3288_ENC_MAX_WIDTH                    1920U
49 #define RK3288_ENC_MIN_HEIGHT                   96U
50 #define RK3288_ENC_MAX_HEIGHT                   1088U
51
52 #define V4L2_CID_PRIVATE_RK3288_HEADER          (V4L2_CID_CUSTOM_BASE + 0)
53 #define V4L2_CID_PRIVATE_RK3288_REG_PARAMS      (V4L2_CID_CUSTOM_BASE + 1)
54 #define V4L2_CID_PRIVATE_RK3288_HW_PARAMS       (V4L2_CID_CUSTOM_BASE + 2)
55 #define V4L2_CID_PRIVATE_RK3288_RET_PARAMS      (V4L2_CID_CUSTOM_BASE + 3)
56
57 static struct rk3288_vpu_fmt formats[] = {
58         /* Source formats. */
59         {
60                 .name = "4:2:0 3 planes Y/Cb/Cr",
61                 .fourcc = V4L2_PIX_FMT_YUV420M,
62                 .codec_mode = RK_VPU_CODEC_NONE,
63                 .num_planes = 3,
64                 .depth = { 8, 4, 4 },
65                 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420P,
66         },
67         {
68                 .name = "4:2:0 2 plane Y/CbCr",
69                 .fourcc = V4L2_PIX_FMT_NV12M,
70                 .codec_mode = RK_VPU_CODEC_NONE,
71                 .num_planes = 2,
72                 .depth = { 8, 8 },
73                 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420SP,
74         },
75         {
76                 .name = "4:2:2 1 plane YUYV",
77                 .fourcc = V4L2_PIX_FMT_YUYV,
78                 .codec_mode = RK_VPU_CODEC_NONE,
79                 .num_planes = 1,
80                 .depth = { 16 },
81                 .enc_fmt = RK3288_VPU_ENC_FMT_YUYV422,
82         },
83         {
84                 .name = "4:2:2 1 plane UYVY",
85                 .fourcc = V4L2_PIX_FMT_UYVY,
86                 .codec_mode = RK_VPU_CODEC_NONE,
87                 .num_planes = 1,
88                 .depth = { 16 },
89                 .enc_fmt = RK3288_VPU_ENC_FMT_UYVY422,
90         },
91         /* Destination formats. */
92         {
93                 .name = "VP8 Encoded Stream",
94                 .fourcc = V4L2_PIX_FMT_VP8,
95                 .codec_mode = RK_VPU_CODEC_VP8E,
96                 .num_planes = 1,
97         },
98 };
99
100 static struct rk3288_vpu_fmt *find_format(u32 fourcc, bool bitstream)
101 {
102         unsigned int i;
103
104         vpu_debug_enter();
105
106         for (i = 0; i < ARRAY_SIZE(formats); i++) {
107                 if (formats[i].fourcc != fourcc)
108                         continue;
109                 if (bitstream && formats[i].codec_mode != RK_VPU_CODEC_NONE)
110                         return &formats[i];
111                 if (!bitstream && formats[i].codec_mode == RK_VPU_CODEC_NONE)
112                         return &formats[i];
113         }
114
115         return NULL;
116 }
117
118 /*
119  * Indices of controls that need to be accessed directly, i.e. through
120  * p_cur.p pointer of their v4l2_ctrl structs.
121  */
122 enum {
123         RK3288_VPU_ENC_CTRL_HEADER,
124         RK3288_VPU_ENC_CTRL_REG_PARAMS,
125         RK3288_VPU_ENC_CTRL_HW_PARAMS,
126         RK3288_VPU_ENC_CTRL_RET_PARAMS,
127 };
128
129 static struct rk3288_vpu_control controls[] = {
130         /* Private, per-frame controls. */
131         [RK3288_VPU_ENC_CTRL_HEADER] = {
132                 .id = V4L2_CID_PRIVATE_RK3288_HEADER,
133                 .type = V4L2_CTRL_TYPE_PRIVATE,
134                 .name = "Rk3288 Private Header",
135                 .elem_size = RK3288_HEADER_SIZE,
136                 .max_stores = VIDEO_MAX_FRAME,
137                 .can_store = true,
138         },
139         [RK3288_VPU_ENC_CTRL_REG_PARAMS] = {
140                 .id = V4L2_CID_PRIVATE_RK3288_REG_PARAMS,
141                 .type = V4L2_CTRL_TYPE_PRIVATE,
142                 .name = "Rk3288 Private Reg Params",
143                 .elem_size = sizeof(struct rk3288_vp8e_reg_params),
144                 .max_stores = VIDEO_MAX_FRAME,
145                 .can_store = true,
146         },
147         [RK3288_VPU_ENC_CTRL_HW_PARAMS] = {
148                 .id = V4L2_CID_PRIVATE_RK3288_HW_PARAMS,
149                 .type = V4L2_CTRL_TYPE_PRIVATE,
150                 .name = "Rk3288 Private Hw Params",
151                 .elem_size = RK3288_HW_PARAMS_SIZE,
152                 .max_stores = VIDEO_MAX_FRAME,
153                 .can_store = true,
154         },
155         [RK3288_VPU_ENC_CTRL_RET_PARAMS] = {
156                 .id = V4L2_CID_PRIVATE_RK3288_RET_PARAMS,
157                 .type = V4L2_CTRL_TYPE_PRIVATE,
158                 .name = "Rk3288 Private Ret Params",
159                 .is_volatile = true,
160                 .is_read_only = true,
161                 .max_stores = VIDEO_MAX_FRAME,
162                 .elem_size = RK3288_RET_PARAMS_SIZE,
163         },
164         /* Generic controls. (currently ignored) */
165         {
166                 .id = V4L2_CID_MPEG_VIDEO_GOP_SIZE,
167                 .type = V4L2_CTRL_TYPE_INTEGER,
168                 .minimum = 1,
169                 .maximum = 150,
170                 .step = 1,
171                 .default_value = 30,
172         },
173         {
174                 .id = V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE,
175                 .type = V4L2_CTRL_TYPE_BOOLEAN,
176                 .minimum = 0,
177                 .maximum = 1,
178                 .step = 1,
179                 .default_value = 0,
180         },
181         {
182                 .id = V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE,
183                 .type = V4L2_CTRL_TYPE_BOOLEAN,
184                 .minimum = 0,
185                 .maximum = 1,
186                 .step = 1,
187                 .default_value = 0,
188         },
189         {
190                 .id = V4L2_CID_MPEG_VIDEO_BITRATE,
191                 .type = V4L2_CTRL_TYPE_INTEGER,
192                 .minimum = 10000,
193                 .maximum = 288000000,
194                 .step = 1,
195                 .default_value = 2097152,
196         },
197         {
198                 .id = V4L2_CID_MPEG_VIDEO_H264_PROFILE,
199                 .type = V4L2_CTRL_TYPE_MENU,
200                 .minimum = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
201                 .maximum = V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
202                 .default_value = V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE,
203                 .menu_skip_mask = ~(
204                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
205                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
206                                 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)
207                         ),
208         },
209         {
210                 .id = V4L2_CID_MPEG_VIDEO_H264_LEVEL,
211                 .type = V4L2_CTRL_TYPE_MENU,
212                 .minimum = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
213                 .maximum = V4L2_MPEG_VIDEO_H264_LEVEL_4_1,
214                 .default_value = V4L2_MPEG_VIDEO_H264_LEVEL_1_0,
215         },
216         {
217                 .id = V4L2_CID_MPEG_VIDEO_H264_MAX_QP,
218                 .type = V4L2_CTRL_TYPE_INTEGER,
219                 .minimum = 0,
220                 .maximum = 51,
221                 .step = 1,
222                 .default_value = 30,
223         },
224         {
225                 .id = V4L2_CID_MPEG_VIDEO_H264_MIN_QP,
226                 .type = V4L2_CTRL_TYPE_INTEGER,
227                 .minimum = 0,
228                 .maximum = 51,
229                 .step = 1,
230                 .default_value = 18,
231         },
232         {
233                 .id = V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM,
234                 .type = V4L2_CTRL_TYPE_BOOLEAN,
235                 .minimum = 0,
236                 .maximum = 1,
237                 .step = 1,
238                 .default_value = 0,
239         },
240         {
241                 .id = V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE,
242                 .type = V4L2_CTRL_TYPE_INTEGER,
243                 .minimum = 0,
244                 .maximum = 288000,
245                 .step = 1,
246                 .default_value = 30000,
247         },
248         {
249                 .id = V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING,
250                 .type = V4L2_CTRL_TYPE_BOOLEAN,
251                 .minimum = 0,
252                 .maximum = 1,
253                 .step = 1,
254                 .default_value = 0,
255         },
256         {
257                 .id = V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME,
258                 .type = V4L2_CTRL_TYPE_BUTTON,
259         },
260         /*
261          * This hardware does not support features provided by controls
262          * below, but they are required for compatibility with certain
263          * userspace software.
264          */
265         {
266                 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF,
267                 .type = V4L2_CTRL_TYPE_INTEGER,
268                 .name = "Rate Control Reaction Coeff.",
269                 .minimum = 1,
270                 .maximum = (1 << 16) - 1,
271                 .step = 1,
272                 .default_value = 1,
273         },
274         {
275                 .id = V4L2_CID_MPEG_VIDEO_HEADER_MODE,
276                 .type = V4L2_CTRL_TYPE_MENU,
277                 .minimum = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
278                 .maximum = V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
279                 .default_value = V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE,
280                 .menu_skip_mask = 0,
281         },
282         {
283                 .id = V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT,
284                 .type = V4L2_CTRL_TYPE_BOOLEAN,
285                 .name = "Fixed Target Bit Enable",
286                 .minimum = 0,
287                 .maximum = 1,
288                 .default_value = 0,
289                 .step = 1,
290                 .menu_skip_mask = 0,
291         },
292         {
293                 .id = V4L2_CID_MPEG_VIDEO_B_FRAMES,
294                 .type = V4L2_CTRL_TYPE_INTEGER,
295                 .minimum = 0,
296                 .maximum = 2,
297                 .step = 1,
298                 .default_value = 0,
299         },
300         {
301                 .id = V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP,
302                 .type = V4L2_CTRL_TYPE_INTEGER,
303                 .minimum = 0,
304                 .maximum = 51,
305                 .step = 1,
306                 .default_value = 1,
307         },
308 };
309
310 static inline const void *get_ctrl_ptr(struct rk3288_vpu_ctx *ctx, unsigned id)
311 {
312         struct v4l2_ctrl *ctrl = ctx->ctrls[id];
313
314         return ctrl->p_cur.p;
315 }
316
317 static const char *const *rk3288_vpu_enc_get_menu(u32 id)
318 {
319         static const char *const vpu_video_frame_skip[] = {
320                 "Disabled",
321                 "Level Limit",
322                 "VBV/CPB Limit",
323                 NULL,
324         };
325
326         switch (id) {
327         case V4L2_CID_MPEG_MFC51_VIDEO_FRAME_SKIP_MODE:
328                 return vpu_video_frame_skip;
329         }
330
331         return NULL;
332 }
333
334 static int vidioc_querycap(struct file *file, void *priv,
335                            struct v4l2_capability *cap)
336 {
337         struct rk3288_vpu_dev *dev = video_drvdata(file);
338
339         vpu_debug_enter();
340
341         strlcpy(cap->driver, RK3288_VPU_ENC_NAME, sizeof(cap->driver));
342         strlcpy(cap->card, dev->pdev->name, sizeof(cap->card));
343         strlcpy(cap->bus_info, "platform:" RK3288_VPU_NAME,
344                 sizeof(cap->bus_info));
345
346         /*
347          * This is only a mem-to-mem video device. The capture and output
348          * device capability flags are left only for backward compatibility
349          * and are scheduled for removal.
350          */
351         cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
352             V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
353
354         vpu_debug_leave();
355
356         return 0;
357 }
358
359 static int vidioc_enum_framesizes(struct file *file, void *prov,
360                                   struct v4l2_frmsizeenum *fsize)
361 {
362         struct v4l2_frmsize_stepwise *s = &fsize->stepwise;
363         struct rk3288_vpu_fmt *fmt;
364
365         if (fsize->index != 0) {
366                 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
367                                 fsize->index);
368                 return -EINVAL;
369         }
370
371         fmt = find_format(fsize->pixel_format, true);
372         if (!fmt) {
373                 vpu_debug(0, "unsupported bitstream format (%08x)\n",
374                                 fsize->pixel_format);
375                 return -EINVAL;
376         }
377
378         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
379
380         s->min_width = RK3288_ENC_MIN_WIDTH;
381         s->max_width = RK3288_ENC_MAX_WIDTH;
382         s->step_width = MB_DIM;
383         s->min_height = RK3288_ENC_MIN_HEIGHT;
384         s->max_height = RK3288_ENC_MAX_HEIGHT;
385         s->step_height = MB_DIM;
386
387         return 0;
388 }
389
390 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool out)
391 {
392         struct rk3288_vpu_fmt *fmt;
393         int i, j = 0;
394
395         vpu_debug_enter();
396
397         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
398                 if (out && formats[i].codec_mode != RK_VPU_CODEC_NONE)
399                         continue;
400                 else if (!out && formats[i].codec_mode == RK_VPU_CODEC_NONE)
401                         continue;
402
403                 if (j == f->index) {
404                         fmt = &formats[i];
405                         strlcpy(f->description, fmt->name,
406                                 sizeof(f->description));
407                         f->pixelformat = fmt->fourcc;
408
409                         f->flags = 0;
410                         if (formats[i].codec_mode != RK_VPU_CODEC_NONE)
411                                 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
412
413                         vpu_debug_leave();
414
415                         return 0;
416                 }
417
418                 ++j;
419         }
420
421         vpu_debug_leave();
422
423         return -EINVAL;
424 }
425
426 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
427                                           struct v4l2_fmtdesc *f)
428 {
429         return vidioc_enum_fmt(f, false);
430 }
431
432 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
433                                           struct v4l2_fmtdesc *f)
434 {
435         return vidioc_enum_fmt(f, true);
436 }
437
438 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
439 {
440         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
441
442         vpu_debug_enter();
443
444         vpu_debug(4, "f->type = %d\n", f->type);
445
446         switch (f->type) {
447         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
448                 f->fmt.pix_mp = ctx->dst_fmt;
449                 break;
450
451         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
452                 f->fmt.pix_mp = ctx->src_fmt;
453                 break;
454
455         default:
456                 vpu_err("invalid buf type\n");
457                 return -EINVAL;
458         }
459
460         vpu_debug_leave();
461
462         return 0;
463 }
464
465 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
466 {
467         struct rk3288_vpu_fmt *fmt;
468         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
469         char str[5];
470
471         vpu_debug_enter();
472
473         switch (f->type) {
474         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
475                 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
476
477                 fmt = find_format(pix_fmt_mp->pixelformat, true);
478                 if (!fmt) {
479                         vpu_err("failed to try capture format\n");
480                         return -EINVAL;
481                 }
482
483                 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
484                         vpu_err("must be set encoding output size\n");
485                         return -EINVAL;
486                 }
487
488                 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
489                 break;
490
491         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
492                 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
493
494                 fmt = find_format(pix_fmt_mp->pixelformat, false);
495                 if (!fmt) {
496                         vpu_err("failed to try output format\n");
497                         return -EINVAL;
498                 }
499
500                 if (fmt->num_planes != pix_fmt_mp->num_planes) {
501                         vpu_err("plane number mismatches on output format\n");
502                         return -EINVAL;
503                 }
504
505                 /* Limit to hardware min/max. */
506                 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
507                                 RK3288_ENC_MIN_WIDTH, RK3288_ENC_MAX_WIDTH);
508                 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
509                                 RK3288_ENC_MIN_HEIGHT, RK3288_ENC_MAX_HEIGHT);
510                 /* Round up to macroblocks. */
511                 pix_fmt_mp->width = round_up(pix_fmt_mp->width, MB_DIM);
512                 pix_fmt_mp->height = round_up(pix_fmt_mp->height, MB_DIM);
513                 break;
514
515         default:
516                 vpu_err("invalid buf type\n");
517                 return -EINVAL;
518         }
519
520         vpu_debug_leave();
521
522         return 0;
523 }
524
525 static void calculate_plane_sizes(struct rk3288_vpu_fmt *fmt,
526                                   unsigned int w, unsigned int h,
527                                   struct v4l2_pix_format_mplane *pix_fmt_mp)
528 {
529         int i;
530
531         for (i = 0; i < fmt->num_planes; ++i) {
532                 pix_fmt_mp->plane_fmt[i].bytesperline = w * fmt->depth[i] / 8;
533                 pix_fmt_mp->plane_fmt[i].sizeimage = h *
534                                         pix_fmt_mp->plane_fmt[i].bytesperline;
535                 /*
536                  * All of multiplanar formats we support have chroma
537                  * planes subsampled by 2 vertically.
538                  */
539                 if (i != 0)
540                         pix_fmt_mp->plane_fmt[i].sizeimage /= 2;
541         }
542 }
543
544 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
545 {
546         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
547         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
548         unsigned int mb_width, mb_height;
549         struct rk3288_vpu_fmt *fmt;
550         int ret = 0;
551
552         vpu_debug_enter();
553
554         /* Change not allowed if any queue is streaming. */
555         if (vb2_is_streaming(&ctx->vq_src) || vb2_is_streaming(&ctx->vq_dst)) {
556                 ret = -EBUSY;
557                 goto out;
558         }
559
560         switch (f->type) {
561         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
562                 /*
563                  * Pixel format change is not allowed when the other queue has
564                  * buffers allocated.
565                  */
566                 if (vb2_is_busy(&ctx->vq_src)
567                     && pix_fmt_mp->pixelformat != ctx->dst_fmt.pixelformat) {
568                         ret = -EBUSY;
569                         goto out;
570                 }
571
572                 ret = vidioc_try_fmt(file, priv, f);
573                 if (ret)
574                         goto out;
575
576                 ctx->vpu_dst_fmt = find_format(pix_fmt_mp->pixelformat, true);
577                 ctx->dst_fmt = *pix_fmt_mp;
578                 break;
579
580         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
581                 /*
582                  * Pixel format change is not allowed when the other queue has
583                  * buffers allocated.
584                  */
585                 if (vb2_is_busy(&ctx->vq_dst)
586                     && pix_fmt_mp->pixelformat != ctx->src_fmt.pixelformat) {
587                         ret = -EBUSY;
588                         goto out;
589                 }
590
591                 ret = vidioc_try_fmt(file, priv, f);
592                 if (ret)
593                         goto out;
594
595                 fmt = find_format(pix_fmt_mp->pixelformat, false);
596                 ctx->vpu_src_fmt = fmt;
597
598                 mb_width = MB_WIDTH(pix_fmt_mp->width);
599                 mb_height = MB_HEIGHT(pix_fmt_mp->height);
600
601                 vpu_debug(0, "OUTPUT codec mode: %d\n", fmt->codec_mode);
602                 vpu_debug(0, "fmt - w: %d, h: %d, mb - w: %d, h: %d\n",
603                           pix_fmt_mp->width, pix_fmt_mp->height,
604                           mb_width, mb_height);
605
606                 calculate_plane_sizes(fmt, mb_width * MB_DIM,
607                                         mb_height * MB_DIM, pix_fmt_mp);
608
609                 /* Reset crop rectangle. */
610                 ctx->src_crop.width = pix_fmt_mp->width;
611                 ctx->src_crop.height = pix_fmt_mp->height;
612
613                 ctx->src_fmt = *pix_fmt_mp;
614                 break;
615
616         default:
617                 vpu_err("invalid buf type\n");
618                 return -EINVAL;
619         }
620
621 out:
622         vpu_debug_leave();
623
624         return ret;
625 }
626
627 static int vidioc_reqbufs(struct file *file, void *priv,
628                           struct v4l2_requestbuffers *reqbufs)
629 {
630         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
631         int ret;
632
633         vpu_debug_enter();
634
635         switch (reqbufs->type) {
636         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
637                 vpu_debug(4, "\n");
638
639                 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
640                 if (ret != 0) {
641                         vpu_err("error in vb2_reqbufs() for E(D)\n");
642                         goto out;
643                 }
644                 break;
645
646         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
647                 vpu_debug(4, "memory type %d\n", reqbufs->memory);
648
649                 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
650                 if (ret != 0) {
651                         vpu_err("error in vb2_reqbufs() for E(S)\n");
652                         goto out;
653                 }
654                 break;
655
656         default:
657                 vpu_err("invalid buf type\n");
658                 ret = -EINVAL;
659                 goto out;
660         }
661
662 out:
663         vpu_debug_leave();
664
665         return ret;
666 }
667
668 static int vidioc_querybuf(struct file *file, void *priv,
669                            struct v4l2_buffer *buf)
670 {
671         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
672         int ret;
673
674         vpu_debug_enter();
675
676         switch (buf->type) {
677         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
678                 ret = vb2_querybuf(&ctx->vq_dst, buf);
679                 if (ret != 0) {
680                         vpu_err("error in vb2_querybuf() for E(D)\n");
681                         goto out;
682                 }
683
684                 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
685                 break;
686
687         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
688                 ret = vb2_querybuf(&ctx->vq_src, buf);
689                 if (ret != 0) {
690                         vpu_err("error in vb2_querybuf() for E(S)\n");
691                         goto out;
692                 }
693                 break;
694
695         default:
696                 vpu_err("invalid buf type\n");
697                 ret = -EINVAL;
698                 goto out;
699         }
700
701 out:
702         vpu_debug_leave();
703
704         return ret;
705 }
706
707 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
708 {
709         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
710         int ret;
711         int i;
712
713         vpu_debug_enter();
714
715         for (i = 0; i < buf->length; i++) {
716                 vpu_debug(4, "plane[%d]->length %d bytesused %d\n",
717                           i, buf->m.planes[i].length,
718                           buf->m.planes[i].bytesused);
719         }
720
721         switch (buf->type) {
722         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
723                 ret = vb2_qbuf(&ctx->vq_src, buf);
724                 vpu_debug(4, "vb2_qbuf return %d\n", ret);
725                 break;
726
727         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
728                 ret = vb2_qbuf(&ctx->vq_dst, buf);
729                 vpu_debug(4, "vb2_qbuf return %d\n", ret);
730                 break;
731
732         default:
733                 ret = -EINVAL;
734         }
735
736         vpu_debug_leave();
737
738         return ret;
739 }
740
741 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
742 {
743         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
744         int ret;
745
746         vpu_debug_enter();
747
748         switch (buf->type) {
749         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
750                 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
751                 break;
752
753         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
754                 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
755                 break;
756
757         default:
758                 ret = -EINVAL;
759         }
760
761         vpu_debug_leave();
762
763         return ret;
764 }
765
766 static int vidioc_expbuf(struct file *file, void *priv,
767                          struct v4l2_exportbuffer *eb)
768 {
769         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
770         int ret;
771
772         vpu_debug_enter();
773
774         switch (eb->type) {
775         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
776                 ret = vb2_expbuf(&ctx->vq_src, eb);
777                 break;
778
779         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
780                 ret = vb2_expbuf(&ctx->vq_dst, eb);
781                 break;
782
783         default:
784                 ret = -EINVAL;
785         }
786
787         vpu_debug_leave();
788
789         return ret;
790 }
791
792 static int vidioc_streamon(struct file *file, void *priv,
793                            enum v4l2_buf_type type)
794 {
795         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
796         int ret;
797
798         vpu_debug_enter();
799
800         switch (type) {
801         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
802                 ret = vb2_streamon(&ctx->vq_src, type);
803                 break;
804
805         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
806                 ret = vb2_streamon(&ctx->vq_dst, type);
807                 break;
808
809         default:
810                 ret = -EINVAL;
811         }
812
813         vpu_debug_leave();
814
815         return ret;
816 }
817
818 static int vidioc_streamoff(struct file *file, void *priv,
819                             enum v4l2_buf_type type)
820 {
821         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
822         int ret;
823
824         vpu_debug_enter();
825
826         switch (type) {
827         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
828                 ret = vb2_streamoff(&ctx->vq_src, type);
829                 break;
830
831         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
832                 ret = vb2_streamoff(&ctx->vq_dst, type);
833                 break;
834
835         default:
836                 ret = -EINVAL;
837         }
838
839         vpu_debug_leave();
840
841         return ret;
842 }
843
844 static int rk3288_vpu_enc_s_ctrl(struct v4l2_ctrl *ctrl)
845 {
846         struct rk3288_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
847         struct rk3288_vpu_dev *dev = ctx->dev;
848         int ret = 0;
849
850         vpu_debug_enter();
851
852         vpu_debug(4, "ctrl id %d\n", ctrl->id);
853
854         switch (ctrl->id) {
855         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
856         case V4L2_CID_MPEG_VIDEO_BITRATE:
857         case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
858         case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
859         case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
860         case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE:
861         case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
862         case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
863         case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
864         case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE:
865         case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING:
866         case V4L2_CID_MPEG_MFC51_VIDEO_RC_REACTION_COEFF:
867         case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
868         case V4L2_CID_MPEG_MFC51_VIDEO_RC_FIXED_TARGET_BIT:
869         case V4L2_CID_MPEG_VIDEO_B_FRAMES:
870         case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
871         case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
872                 /* Ignore these controls for now. (FIXME?) */
873                 break;
874
875         case V4L2_CID_PRIVATE_RK3288_HEADER:
876         case V4L2_CID_PRIVATE_RK3288_REG_PARAMS:
877         case V4L2_CID_PRIVATE_RK3288_HW_PARAMS:
878                 /* Nothing to do here. The control is used directly. */
879                 break;
880
881         default:
882                 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
883                          ctrl->id, ctrl->val);
884                 ret = -EINVAL;
885         }
886
887         vpu_debug_leave();
888
889         return ret;
890 }
891
892 static int rk3288_vpu_enc_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
893 {
894         struct rk3288_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
895         struct rk3288_vpu_dev *dev = ctx->dev;
896         int ret = 0;
897
898         vpu_debug_enter();
899
900         vpu_debug(4, "ctrl id %d\n", ctrl->id);
901
902         switch (ctrl->id) {
903         case V4L2_CID_PRIVATE_RK3288_RET_PARAMS:
904                 memcpy(ctrl->p_new.p, ctx->run.priv_dst.cpu,
905                         RK3288_RET_PARAMS_SIZE);
906                 break;
907
908         default:
909                 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
910                          ctrl->id, ctrl->val);
911                 ret = -EINVAL;
912         }
913
914         vpu_debug_leave();
915
916         return ret;
917 }
918
919 static const struct v4l2_ctrl_ops rk3288_vpu_enc_ctrl_ops = {
920         .s_ctrl = rk3288_vpu_enc_s_ctrl,
921         .g_volatile_ctrl = rk3288_vpu_enc_g_volatile_ctrl,
922 };
923
924 static int vidioc_cropcap(struct file *file, void *priv,
925                           struct v4l2_cropcap *cap)
926 {
927         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
928         struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
929         int ret = 0;
930
931         vpu_debug_enter();
932
933         /* Crop only supported on source. */
934         if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
935                 ret = -EINVAL;
936                 goto out;
937         }
938
939         cap->bounds.left = 0;
940         cap->bounds.top = 0;
941         cap->bounds.width = fmt->width;
942         cap->bounds.height = fmt->height;
943         cap->defrect = cap->bounds;
944         cap->pixelaspect.numerator = 1;
945         cap->pixelaspect.denominator = 1;
946
947 out:
948         vpu_debug_leave();
949
950         return ret;
951 }
952
953 static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
954 {
955         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
956         int ret = 0;
957
958         vpu_debug_enter();
959
960         /* Crop only supported on source. */
961         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
962                 ret = -EINVAL;
963                 goto out;
964         }
965
966         crop->c = ctx->src_crop;
967
968 out:
969         vpu_debug_leave();
970
971         return ret;
972 }
973
974 static int vidioc_s_crop(struct file *file, void *priv,
975                          const struct v4l2_crop *crop)
976 {
977         struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
978         struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
979         const struct v4l2_rect *rect = &crop->c;
980         int ret = 0;
981
982         vpu_debug_enter();
983
984         /* Crop only supported on source. */
985         if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
986                 ret = -EINVAL;
987                 goto out;
988         }
989
990         /* Change not allowed if the queue is streaming. */
991         if (vb2_is_streaming(&ctx->vq_src)) {
992                 ret = -EBUSY;
993                 goto out;
994         }
995
996         /* We do not support offsets. */
997         if (rect->left != 0 || rect->top != 0)
998                 goto fallback;
999
1000         /* We can crop only inside right- or bottom-most macroblocks. */
1001         if (round_up(rect->width, MB_DIM) != fmt->width
1002             || round_up(rect->height, MB_DIM) != fmt->height)
1003                 goto fallback;
1004
1005         /* We support widths aligned to 4 pixels and arbitrary heights. */
1006         ctx->src_crop.width = round_up(rect->width, 4);
1007         ctx->src_crop.height = rect->height;
1008
1009         vpu_debug_leave();
1010
1011         return 0;
1012
1013 fallback:
1014         /* Default to full frame for incorrect settings. */
1015         ctx->src_crop.width = fmt->width;
1016         ctx->src_crop.height = fmt->height;
1017
1018 out:
1019         vpu_debug_leave();
1020
1021         return ret;
1022 }
1023
1024 static const struct v4l2_ioctl_ops rk3288_vpu_enc_ioctl_ops = {
1025         .vidioc_querycap = vidioc_querycap,
1026         .vidioc_enum_framesizes = vidioc_enum_framesizes,
1027         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
1028         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
1029         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1030         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1031         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1032         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1033         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1034         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1035         .vidioc_reqbufs = vidioc_reqbufs,
1036         .vidioc_querybuf = vidioc_querybuf,
1037         .vidioc_qbuf = vidioc_qbuf,
1038         .vidioc_dqbuf = vidioc_dqbuf,
1039         .vidioc_expbuf = vidioc_expbuf,
1040         .vidioc_streamon = vidioc_streamon,
1041         .vidioc_streamoff = vidioc_streamoff,
1042         .vidioc_cropcap = vidioc_cropcap,
1043         .vidioc_g_crop = vidioc_g_crop,
1044         .vidioc_s_crop = vidioc_s_crop,
1045 };
1046
1047 static int rk3288_vpu_queue_setup(struct vb2_queue *vq,
1048                                   const struct v4l2_format *fmt,
1049                                   unsigned int *buf_count,
1050                                   unsigned int *plane_count,
1051                                   unsigned int psize[], void *allocators[])
1052 {
1053         struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1054         int ret = 0;
1055         int i;
1056
1057         vpu_debug_enter();
1058
1059         switch (vq->type) {
1060         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1061                 *plane_count = ctx->vpu_dst_fmt->num_planes;
1062
1063                 if (*buf_count < 1)
1064                         *buf_count = 1;
1065
1066                 if (*buf_count > VIDEO_MAX_FRAME)
1067                         *buf_count = VIDEO_MAX_FRAME;
1068
1069                 psize[0] = ctx->dst_fmt.plane_fmt[0].sizeimage;
1070                 /* Kernel mapping necessary for bitstream post processing. */
1071                 allocators[0] = ctx->dev->alloc_ctx_vm;
1072                 vpu_debug(0, "capture psize[%d]: %d\n", 0, psize[0]);
1073                 break;
1074
1075         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1076                 *plane_count = ctx->vpu_src_fmt->num_planes;
1077
1078                 if (*buf_count < 1)
1079                         *buf_count = 1;
1080
1081                 if (*buf_count > VIDEO_MAX_FRAME)
1082                         *buf_count = VIDEO_MAX_FRAME;
1083
1084                 for (i = 0; i < ctx->vpu_src_fmt->num_planes; ++i) {
1085                         psize[i] = ctx->src_fmt.plane_fmt[i].sizeimage;
1086                         vpu_debug(0, "output psize[%d]: %d\n", i, psize[i]);
1087                         allocators[i] = ctx->dev->alloc_ctx;
1088                 }
1089                 break;
1090
1091         default:
1092                 vpu_err("invalid queue type: %d\n", vq->type);
1093                 ret = -EINVAL;
1094         }
1095
1096         vpu_debug_leave();
1097
1098         return ret;
1099 }
1100
1101 static int rk3288_vpu_buf_prepare(struct vb2_buffer *vb)
1102 {
1103         struct vb2_queue *vq = vb->vb2_queue;
1104         struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1105         int ret = 0;
1106         int i;
1107
1108         vpu_debug_enter();
1109
1110         switch (vq->type) {
1111         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1112                 vpu_debug(4, "plane size: %ld, dst size: %d\n",
1113                                 vb2_plane_size(vb, 0),
1114                                 ctx->dst_fmt.plane_fmt[0].sizeimage);
1115
1116                 if (vb2_plane_size(vb, 0)
1117                     < ctx->dst_fmt.plane_fmt[0].sizeimage) {
1118                         vpu_err("plane size is too small for capture\n");
1119                         ret = -EINVAL;
1120                 }
1121                 break;
1122
1123         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1124                 for (i = 0; i < ctx->vpu_src_fmt->num_planes; ++i) {
1125                         vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", i,
1126                                         vb2_plane_size(vb, i),
1127                                         ctx->src_fmt.plane_fmt[i].sizeimage);
1128
1129                         if (vb2_plane_size(vb, i)
1130                             < ctx->src_fmt.plane_fmt[i].sizeimage) {
1131                                 vpu_err("size of plane %d is too small for output\n",
1132                                         i);
1133                                 break;
1134                         }
1135                 }
1136
1137                 if (i != ctx->vpu_src_fmt->num_planes)
1138                         ret = -EINVAL;
1139                 break;
1140
1141         default:
1142                 vpu_err("invalid queue type: %d\n", vq->type);
1143                 ret = -EINVAL;
1144         }
1145
1146         vpu_debug_leave();
1147
1148         return ret;
1149 }
1150
1151 static void rk3288_vpu_buf_finish(struct vb2_buffer *vb)
1152 {
1153         struct vb2_queue *vq = vb->vb2_queue;
1154         struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1155
1156         vpu_debug_enter();
1157
1158         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
1159             && vb->state == VB2_BUF_STATE_DONE
1160             && ctx->vpu_dst_fmt->fourcc == V4L2_PIX_FMT_VP8) {
1161                 struct rk3288_vpu_buf *buf;
1162
1163                 buf = vb_to_buf(vb);
1164                 rk3288_vpu_vp8e_assemble_bitstream(ctx, buf);
1165         }
1166
1167         vpu_debug_leave();
1168 }
1169
1170 static int rk3288_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
1171 {
1172         int ret = 0;
1173         struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
1174         struct rk3288_vpu_dev *dev = ctx->dev;
1175         bool ready = false;
1176
1177         vpu_debug_enter();
1178
1179         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1180                 ret = rk3288_vpu_init(ctx);
1181                 if (ret < 0) {
1182                         vpu_err("rk3288_vpu_init failed\n");
1183                         return ret;
1184                 }
1185
1186                 ready = vb2_is_streaming(&ctx->vq_src);
1187         } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1188                 ready = vb2_is_streaming(&ctx->vq_dst);
1189         }
1190
1191         if (ready)
1192                 rk3288_vpu_try_context(dev, ctx);
1193
1194         vpu_debug_leave();
1195
1196         return 0;
1197 }
1198
1199 static void rk3288_vpu_stop_streaming(struct vb2_queue *q)
1200 {
1201         unsigned long flags;
1202         struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
1203         struct rk3288_vpu_dev *dev = ctx->dev;
1204         struct rk3288_vpu_buf *b;
1205         LIST_HEAD(queue);
1206         int i;
1207
1208         vpu_debug_enter();
1209
1210         spin_lock_irqsave(&dev->irqlock, flags);
1211
1212         list_del_init(&ctx->list);
1213
1214         switch (q->type) {
1215         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1216                 list_splice_init(&ctx->dst_queue, &queue);
1217                 break;
1218
1219         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1220                 list_splice_init(&ctx->src_queue, &queue);
1221                 break;
1222
1223         default:
1224                 break;
1225         }
1226
1227         spin_unlock_irqrestore(&dev->irqlock, flags);
1228
1229         wait_event(dev->run_wq, dev->current_ctx != ctx);
1230
1231         while (!list_empty(&queue)) {
1232                 b = list_first_entry(&queue, struct rk3288_vpu_buf, list);
1233                 for (i = 0; i < b->b.num_planes; i++)
1234                         vb2_set_plane_payload(&b->b, i, 0);
1235                 vb2_buffer_done(&b->b, VB2_BUF_STATE_ERROR);
1236                 list_del(&b->list);
1237         }
1238
1239         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1240                 rk3288_vpu_deinit(ctx);
1241
1242         vpu_debug_leave();
1243 }
1244
1245 static void rk3288_vpu_buf_queue(struct vb2_buffer *vb)
1246 {
1247         struct vb2_queue *vq = vb->vb2_queue;
1248         struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1249         struct rk3288_vpu_dev *dev = ctx->dev;
1250         struct rk3288_vpu_buf *vpu_buf;
1251         unsigned long flags;
1252
1253         vpu_debug_enter();
1254
1255         switch (vq->type) {
1256         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1257                 vpu_buf = vb_to_buf(vb);
1258
1259                 /* Mark destination as available for use by VPU */
1260                 spin_lock_irqsave(&dev->irqlock, flags);
1261
1262                 list_add_tail(&vpu_buf->list, &ctx->dst_queue);
1263
1264                 spin_unlock_irqrestore(&dev->irqlock, flags);
1265                 break;
1266
1267         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1268                 vpu_buf = vb_to_buf(vb);
1269
1270                 spin_lock_irqsave(&dev->irqlock, flags);
1271
1272                 list_add_tail(&vpu_buf->list, &ctx->src_queue);
1273
1274                 spin_unlock_irqrestore(&dev->irqlock, flags);
1275                 break;
1276
1277         default:
1278                 vpu_err("unsupported buffer type (%d)\n", vq->type);
1279         }
1280
1281         if (vb2_is_streaming(&ctx->vq_src) && vb2_is_streaming(&ctx->vq_dst))
1282                 rk3288_vpu_try_context(dev, ctx);
1283
1284         vpu_debug_leave();
1285 }
1286
1287 static struct vb2_ops rk3288_vpu_enc_qops = {
1288         .queue_setup = rk3288_vpu_queue_setup,
1289         .wait_prepare = vb2_ops_wait_prepare,
1290         .wait_finish = vb2_ops_wait_finish,
1291         .buf_prepare = rk3288_vpu_buf_prepare,
1292         .buf_finish = rk3288_vpu_buf_finish,
1293         .start_streaming = rk3288_vpu_start_streaming,
1294         .stop_streaming = rk3288_vpu_stop_streaming,
1295         .buf_queue = rk3288_vpu_buf_queue,
1296 };
1297
1298 struct vb2_ops *get_enc_queue_ops(void)
1299 {
1300         return &rk3288_vpu_enc_qops;
1301 }
1302
1303 const struct v4l2_ioctl_ops *get_enc_v4l2_ioctl_ops(void)
1304 {
1305         return &rk3288_vpu_enc_ioctl_ops;
1306 }
1307
1308 static void rk3288_vpu_enc_prepare_run(struct rk3288_vpu_ctx *ctx)
1309 {
1310         struct vb2_buffer *vb2_src = &ctx->run.src->b;
1311         unsigned config_store = vb2_src->v4l2_buf.config_store;
1312
1313         v4l2_ctrl_apply_store(&ctx->ctrl_handler, config_store);
1314
1315         memcpy(ctx->run.dst->vp8e.header,
1316                 get_ctrl_ptr(ctx, RK3288_VPU_ENC_CTRL_HEADER),
1317                 RK3288_HEADER_SIZE);
1318         ctx->run.vp8e.reg_params = get_ctrl_ptr(ctx,
1319                 RK3288_VPU_ENC_CTRL_REG_PARAMS);
1320         memcpy(ctx->run.priv_src.cpu,
1321                 get_ctrl_ptr(ctx, RK3288_VPU_ENC_CTRL_HW_PARAMS),
1322                 RK3288_HW_PARAMS_SIZE);
1323 }
1324
1325 static const struct rk3288_vpu_run_ops rk3288_vpu_enc_run_ops = {
1326         .prepare_run = rk3288_vpu_enc_prepare_run,
1327 };
1328
1329 int rk3288_vpu_enc_init(struct rk3288_vpu_ctx *ctx)
1330 {
1331         struct rk3288_vpu_dev *vpu = ctx->dev;
1332         int ret;
1333
1334         ctx->vpu_src_fmt = find_format(DEF_SRC_FMT_ENC, false);
1335         ctx->vpu_dst_fmt = find_format(DEF_DST_FMT_ENC, true);
1336
1337         ret = rk3288_vpu_aux_buf_alloc(vpu, &ctx->run.priv_src,
1338                                         RK3288_HW_PARAMS_SIZE);
1339         if (ret) {
1340                 vpu_err("Failed to allocate private source buffer.\n");
1341                 return ret;
1342         }
1343
1344
1345         ret = rk3288_vpu_aux_buf_alloc(vpu, &ctx->run.priv_dst,
1346                                         RK3288_RET_PARAMS_SIZE);
1347         if (ret) {
1348                 vpu_err("Failed to allocate private destination buffer.\n");
1349                 goto err_priv_src;
1350         }
1351
1352         ret = rk3288_vpu_ctrls_setup(ctx, &rk3288_vpu_enc_ctrl_ops,
1353                                         controls, ARRAY_SIZE(controls),
1354                                         rk3288_vpu_enc_get_menu);
1355         if (ret) {
1356                 vpu_err("Failed to set up controls\n");
1357                 goto err_priv_dst;
1358         }
1359
1360         ctx->run_ops = &rk3288_vpu_enc_run_ops;
1361
1362         return 0;
1363
1364 err_priv_dst:
1365         rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_dst);
1366 err_priv_src:
1367         rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_src);
1368
1369         return ret;
1370 }
1371
1372 void rk3288_vpu_enc_exit(struct rk3288_vpu_ctx *ctx)
1373 {
1374         struct rk3288_vpu_dev *vpu = ctx->dev;
1375
1376         rk3288_vpu_ctrls_delete(ctx);
1377
1378         rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_dst);
1379         rk3288_vpu_aux_buf_free(vpu, &ctx->run.priv_src);
1380 };
1381
1382 /*
1383  * WAR for encoder state corruption after decoding
1384  */
1385
1386 static const struct rk3288_vpu_run_ops dummy_encode_run_ops = {
1387         /* No ops needed for dummy encoding. */
1388 };
1389
1390 #define DUMMY_W         64
1391 #define DUMMY_H         64
1392 #define DUMMY_SRC_FMT   V4L2_PIX_FMT_YUYV
1393 #define DUMMY_DST_FMT   V4L2_PIX_FMT_VP8
1394 #define DUMMY_DST_SIZE  (32 * 1024)
1395
1396 int rk3288_vpu_enc_init_dummy_ctx(struct rk3288_vpu_dev *dev)
1397 {
1398         struct rk3288_vpu_ctx *ctx;
1399         int ret;
1400         int i;
1401
1402         ctx = devm_kzalloc(dev->dev, sizeof(*ctx), GFP_KERNEL);
1403         if (!ctx)
1404                 return -ENOMEM;
1405
1406         ctx->dev = dev;
1407
1408         ctx->vpu_src_fmt = find_format(DUMMY_SRC_FMT, false);
1409         ctx->src_fmt.width = DUMMY_W;
1410         ctx->src_fmt.height = DUMMY_H;
1411         ctx->src_fmt.pixelformat = ctx->vpu_src_fmt->fourcc;
1412         ctx->src_fmt.num_planes = ctx->vpu_src_fmt->num_planes;
1413
1414         calculate_plane_sizes(ctx->vpu_src_fmt, ctx->src_fmt.width,
1415                                 ctx->src_fmt.height, &ctx->src_fmt);
1416
1417         ctx->vpu_dst_fmt = find_format(DUMMY_DST_FMT, true);
1418         ctx->dst_fmt.width = ctx->src_fmt.width;
1419         ctx->dst_fmt.height = ctx->src_fmt.height;
1420         ctx->dst_fmt.pixelformat = ctx->vpu_dst_fmt->fourcc;
1421         ctx->dst_fmt.plane_fmt[0].sizeimage = DUMMY_DST_SIZE;
1422         ctx->dst_fmt.plane_fmt[0].bytesperline = 0;
1423         ctx->dst_fmt.num_planes = 1;
1424
1425         INIT_LIST_HEAD(&ctx->src_queue);
1426
1427         ctx->src_crop.left = 0;
1428         ctx->src_crop.top = 0;
1429         ctx->src_crop.width = ctx->src_fmt.width;
1430         ctx->src_crop.left = ctx->src_fmt.height;
1431
1432         INIT_LIST_HEAD(&ctx->dst_queue);
1433         INIT_LIST_HEAD(&ctx->list);
1434
1435         ctx->run.vp8e.reg_params = rk3288_vpu_vp8e_get_dummy_params();
1436         ctx->run_ops = &dummy_encode_run_ops;
1437
1438         ctx->run.dst = devm_kzalloc(dev->dev, sizeof(*ctx->run.dst),
1439                                         GFP_KERNEL);
1440         if (!ctx->run.dst)
1441                 return -ENOMEM;
1442
1443         ret = rk3288_vpu_aux_buf_alloc(dev, &ctx->run.priv_src,
1444                                         RK3288_HW_PARAMS_SIZE);
1445         if (ret)
1446                 return ret;
1447
1448         ret = rk3288_vpu_aux_buf_alloc(dev, &ctx->run.priv_dst,
1449                                         RK3288_RET_PARAMS_SIZE);
1450         if (ret)
1451                 goto err_free_priv_src;
1452
1453         for (i = 0; i < ctx->src_fmt.num_planes; ++i) {
1454                 ret = rk3288_vpu_aux_buf_alloc(dev, &dev->dummy_encode_src[i],
1455                                         ctx->src_fmt.plane_fmt[i].sizeimage);
1456                 if (ret)
1457                         goto err_free_src;
1458
1459                 memset(dev->dummy_encode_src[i].cpu, 0,
1460                         dev->dummy_encode_src[i].size);
1461         }
1462
1463         ret = rk3288_vpu_aux_buf_alloc(dev, &dev->dummy_encode_dst,
1464                                         ctx->dst_fmt.plane_fmt[0].sizeimage);
1465         if (ret)
1466                 goto err_free_src;
1467
1468         memset(dev->dummy_encode_dst.cpu, 0, dev->dummy_encode_dst.size);
1469
1470         ret = rk3288_vpu_init(ctx);
1471         if (ret)
1472                 goto err_free_dst;
1473
1474         dev->dummy_encode_ctx = ctx;
1475
1476         return 0;
1477
1478 err_free_dst:
1479         rk3288_vpu_aux_buf_free(dev, &dev->dummy_encode_dst);
1480 err_free_src:
1481         for (i = 0; i < ctx->src_fmt.num_planes; ++i)
1482                 if (dev->dummy_encode_src[i].cpu)
1483                         rk3288_vpu_aux_buf_free(dev, &dev->dummy_encode_src[i]);
1484         rk3288_vpu_aux_buf_free(dev, &ctx->run.priv_dst);
1485 err_free_priv_src:
1486         rk3288_vpu_aux_buf_free(dev, &ctx->run.priv_src);
1487
1488         return ret;
1489 }
1490
1491 void rk3288_vpu_enc_free_dummy_ctx(struct rk3288_vpu_dev *dev)
1492 {
1493         struct rk3288_vpu_ctx *ctx = dev->dummy_encode_ctx;
1494         int i;
1495
1496         rk3288_vpu_deinit(ctx);
1497
1498         for (i = 0; i < ctx->src_fmt.num_planes; ++i)
1499                 rk3288_vpu_aux_buf_free(dev, &dev->dummy_encode_src[i]);
1500         rk3288_vpu_aux_buf_free(dev, &dev->dummy_encode_dst);
1501         rk3288_vpu_aux_buf_free(dev, &ctx->run.priv_src);
1502         rk3288_vpu_aux_buf_free(dev, &ctx->run.priv_dst);
1503 }