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