[media] s5p-mfc: Rename IS_MFCV6 macro
[firefly-linux-kernel-4.4.55.git] / drivers / media / platform / s5p-mfc / s5p_mfc_dec.c
1 /*
2  * linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
3  *
4  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5  *              http://www.samsung.com/
6  * Kamil Debski, <k.debski@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <linux/videodev2.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/videobuf2-core.h>
27 #include "s5p_mfc_common.h"
28 #include "s5p_mfc_debug.h"
29 #include "s5p_mfc_dec.h"
30 #include "s5p_mfc_intr.h"
31 #include "s5p_mfc_opr.h"
32 #include "s5p_mfc_pm.h"
33
34 #define DEF_SRC_FMT_DEC V4L2_PIX_FMT_H264
35 #define DEF_DST_FMT_DEC V4L2_PIX_FMT_NV12MT_16X16
36
37 static struct s5p_mfc_fmt formats[] = {
38         {
39                 .name           = "4:2:0 2 Planes 16x16 Tiles",
40                 .fourcc         = V4L2_PIX_FMT_NV12MT_16X16,
41                 .codec_mode     = S5P_MFC_CODEC_NONE,
42                 .type           = MFC_FMT_RAW,
43                 .num_planes     = 2,
44         },
45         {
46                 .name           = "4:2:0 2 Planes 64x32 Tiles",
47                 .fourcc         = V4L2_PIX_FMT_NV12MT,
48                 .codec_mode     = S5P_MFC_CODEC_NONE,
49                 .type           = MFC_FMT_RAW,
50                 .num_planes     = 2,
51         },
52         {
53                 .name           = "4:2:0 2 Planes Y/CbCr",
54                 .fourcc         = V4L2_PIX_FMT_NV12M,
55                 .codec_mode     = S5P_MFC_CODEC_NONE,
56                 .type           = MFC_FMT_RAW,
57                 .num_planes     = 2,
58         },
59         {
60                 .name           = "4:2:0 2 Planes Y/CrCb",
61                 .fourcc         = V4L2_PIX_FMT_NV21M,
62                 .codec_mode     = S5P_MFC_CODEC_NONE,
63                 .type           = MFC_FMT_RAW,
64                 .num_planes     = 2,
65         },
66         {
67                 .name           = "H264 Encoded Stream",
68                 .fourcc         = V4L2_PIX_FMT_H264,
69                 .codec_mode     = S5P_MFC_CODEC_H264_DEC,
70                 .type           = MFC_FMT_DEC,
71                 .num_planes     = 1,
72         },
73         {
74                 .name           = "H264/MVC Encoded Stream",
75                 .fourcc         = V4L2_PIX_FMT_H264_MVC,
76                 .codec_mode     = S5P_MFC_CODEC_H264_MVC_DEC,
77                 .type           = MFC_FMT_DEC,
78                 .num_planes     = 1,
79         },
80         {
81                 .name           = "H263 Encoded Stream",
82                 .fourcc         = V4L2_PIX_FMT_H263,
83                 .codec_mode     = S5P_MFC_CODEC_H263_DEC,
84                 .type           = MFC_FMT_DEC,
85                 .num_planes     = 1,
86         },
87         {
88                 .name           = "MPEG1 Encoded Stream",
89                 .fourcc         = V4L2_PIX_FMT_MPEG1,
90                 .codec_mode     = S5P_MFC_CODEC_MPEG2_DEC,
91                 .type           = MFC_FMT_DEC,
92                 .num_planes     = 1,
93         },
94         {
95                 .name           = "MPEG2 Encoded Stream",
96                 .fourcc         = V4L2_PIX_FMT_MPEG2,
97                 .codec_mode     = S5P_MFC_CODEC_MPEG2_DEC,
98                 .type           = MFC_FMT_DEC,
99                 .num_planes     = 1,
100         },
101         {
102                 .name           = "MPEG4 Encoded Stream",
103                 .fourcc         = V4L2_PIX_FMT_MPEG4,
104                 .codec_mode     = S5P_MFC_CODEC_MPEG4_DEC,
105                 .type           = MFC_FMT_DEC,
106                 .num_planes     = 1,
107         },
108         {
109                 .name           = "XviD Encoded Stream",
110                 .fourcc         = V4L2_PIX_FMT_XVID,
111                 .codec_mode     = S5P_MFC_CODEC_MPEG4_DEC,
112                 .type           = MFC_FMT_DEC,
113                 .num_planes     = 1,
114         },
115         {
116                 .name           = "VC1 Encoded Stream",
117                 .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_G,
118                 .codec_mode     = S5P_MFC_CODEC_VC1_DEC,
119                 .type           = MFC_FMT_DEC,
120                 .num_planes     = 1,
121         },
122         {
123                 .name           = "VC1 RCV Encoded Stream",
124                 .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_L,
125                 .codec_mode     = S5P_MFC_CODEC_VC1RCV_DEC,
126                 .type           = MFC_FMT_DEC,
127                 .num_planes     = 1,
128         },
129         {
130                 .name           = "VP8 Encoded Stream",
131                 .fourcc         = V4L2_PIX_FMT_VP8,
132                 .codec_mode     = S5P_MFC_CODEC_VP8_DEC,
133                 .type           = MFC_FMT_DEC,
134                 .num_planes     = 1,
135         },
136 };
137
138 #define NUM_FORMATS ARRAY_SIZE(formats)
139
140 /* Find selected format description */
141 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
142 {
143         unsigned int i;
144
145         for (i = 0; i < NUM_FORMATS; i++) {
146                 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
147                     formats[i].type == t)
148                         return &formats[i];
149         }
150         return NULL;
151 }
152
153 static struct mfc_control controls[] = {
154         {
155                 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
156                 .type = V4L2_CTRL_TYPE_INTEGER,
157                 .name = "H264 Display Delay",
158                 .minimum = 0,
159                 .maximum = 16383,
160                 .step = 1,
161                 .default_value = 0,
162         },
163         {
164                 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
165                 .type = V4L2_CTRL_TYPE_BOOLEAN,
166                 .name = "H264 Display Delay Enable",
167                 .minimum = 0,
168                 .maximum = 1,
169                 .step = 1,
170                 .default_value = 0,
171         },
172         {
173                 .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
174                 .type = V4L2_CTRL_TYPE_BOOLEAN,
175                 .name = "Mpeg4 Loop Filter Enable",
176                 .minimum = 0,
177                 .maximum = 1,
178                 .step = 1,
179                 .default_value = 0,
180         },
181         {
182                 .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
183                 .type = V4L2_CTRL_TYPE_BOOLEAN,
184                 .name = "Slice Interface Enable",
185                 .minimum = 0,
186                 .maximum = 1,
187                 .step = 1,
188                 .default_value = 0,
189         },
190         {
191                 .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
192                 .type = V4L2_CTRL_TYPE_INTEGER,
193                 .name = "Minimum number of cap bufs",
194                 .minimum = 1,
195                 .maximum = 32,
196                 .step = 1,
197                 .default_value = 1,
198                 .is_volatile = 1,
199         },
200 };
201
202 #define NUM_CTRLS ARRAY_SIZE(controls)
203
204 /* Check whether a context should be run on hardware */
205 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
206 {
207         /* Context is to parse header */
208         if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
209                 return 1;
210         /* Context is to decode a frame */
211         if (ctx->src_queue_cnt >= 1 &&
212             ctx->state == MFCINST_RUNNING &&
213             ctx->dst_queue_cnt >= ctx->pb_count)
214                 return 1;
215         /* Context is to return last frame */
216         if (ctx->state == MFCINST_FINISHING &&
217             ctx->dst_queue_cnt >= ctx->pb_count)
218                 return 1;
219         /* Context is to set buffers */
220         if (ctx->src_queue_cnt >= 1 &&
221             ctx->state == MFCINST_HEAD_PARSED &&
222             ctx->capture_state == QUEUE_BUFS_MMAPED)
223                 return 1;
224         /* Resolution change */
225         if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
226                 ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
227                 ctx->dst_queue_cnt >= ctx->pb_count)
228                 return 1;
229         if (ctx->state == MFCINST_RES_CHANGE_END &&
230                 ctx->src_queue_cnt >= 1)
231                 return 1;
232         mfc_debug(2, "ctx is not ready\n");
233         return 0;
234 }
235
236 static struct s5p_mfc_codec_ops decoder_codec_ops = {
237         .pre_seq_start          = NULL,
238         .post_seq_start         = NULL,
239         .pre_frame_start        = NULL,
240         .post_frame_start       = NULL,
241 };
242
243 /* Query capabilities of the device */
244 static int vidioc_querycap(struct file *file, void *priv,
245                            struct v4l2_capability *cap)
246 {
247         struct s5p_mfc_dev *dev = video_drvdata(file);
248
249         strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
250         strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
251         cap->bus_info[0] = 0;
252         cap->version = KERNEL_VERSION(1, 0, 0);
253         /*
254          * This is only a mem-to-mem video device. The capture and output
255          * device capability flags are left only for backward compatibility
256          * and are scheduled for removal.
257          */
258         cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
259                             V4L2_CAP_VIDEO_CAPTURE_MPLANE |
260                             V4L2_CAP_VIDEO_OUTPUT_MPLANE;
261         return 0;
262 }
263
264 /* Enumerate format */
265 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
266 {
267         struct s5p_mfc_fmt *fmt;
268         int i, j = 0;
269
270         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
271                 if (mplane && formats[i].num_planes == 1)
272                         continue;
273                 else if (!mplane && formats[i].num_planes > 1)
274                         continue;
275                 if (out && formats[i].type != MFC_FMT_DEC)
276                         continue;
277                 else if (!out && formats[i].type != MFC_FMT_RAW)
278                         continue;
279
280                 if (j == f->index)
281                         break;
282                 ++j;
283         }
284         if (i == ARRAY_SIZE(formats))
285                 return -EINVAL;
286         fmt = &formats[i];
287         strlcpy(f->description, fmt->name, sizeof(f->description));
288         f->pixelformat = fmt->fourcc;
289         return 0;
290 }
291
292 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
293                                                         struct v4l2_fmtdesc *f)
294 {
295         return vidioc_enum_fmt(f, false, false);
296 }
297
298 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
299                                                         struct v4l2_fmtdesc *f)
300 {
301         return vidioc_enum_fmt(f, true, false);
302 }
303
304 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
305                                                         struct v4l2_fmtdesc *f)
306 {
307         return vidioc_enum_fmt(f, false, true);
308 }
309
310 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
311                                                         struct v4l2_fmtdesc *f)
312 {
313         return vidioc_enum_fmt(f, true, true);
314 }
315
316 /* Get format */
317 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
318 {
319         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
320         struct v4l2_pix_format_mplane *pix_mp;
321
322         mfc_debug_enter();
323         pix_mp = &f->fmt.pix_mp;
324         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
325             (ctx->state == MFCINST_GOT_INST || ctx->state ==
326                                                 MFCINST_RES_CHANGE_END)) {
327                 /* If the MFC is parsing the header,
328                  * so wait until it is finished */
329                 s5p_mfc_clean_ctx_int_flags(ctx);
330                 s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
331                                                                         0);
332         }
333         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
334             ctx->state >= MFCINST_HEAD_PARSED &&
335             ctx->state < MFCINST_ABORT) {
336                 /* This is run on CAPTURE (decode output) */
337                 /* Width and height are set to the dimensions
338                    of the movie, the buffer is bigger and
339                    further processing stages should crop to this
340                    rectangle. */
341                 pix_mp->width = ctx->buf_width;
342                 pix_mp->height = ctx->buf_height;
343                 pix_mp->field = V4L2_FIELD_NONE;
344                 pix_mp->num_planes = 2;
345                 /* Set pixelformat to the format in which MFC
346                    outputs the decoded frame */
347                 pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT;
348                 pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
349                 pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
350                 pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
351                 pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
352         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
353                 /* This is run on OUTPUT
354                    The buffer contains compressed image
355                    so width and height have no meaning */
356                 pix_mp->width = 0;
357                 pix_mp->height = 0;
358                 pix_mp->field = V4L2_FIELD_NONE;
359                 pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
360                 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
361                 pix_mp->pixelformat = ctx->src_fmt->fourcc;
362                 pix_mp->num_planes = ctx->src_fmt->num_planes;
363         } else {
364                 mfc_err("Format could not be read\n");
365                 mfc_debug(2, "%s-- with error\n", __func__);
366                 return -EINVAL;
367         }
368         mfc_debug_leave();
369         return 0;
370 }
371
372 /* Try format */
373 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
374 {
375         struct s5p_mfc_dev *dev = video_drvdata(file);
376         struct s5p_mfc_fmt *fmt;
377
378         mfc_debug(2, "Type is %d\n", f->type);
379         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
380                 fmt = find_format(f, MFC_FMT_DEC);
381                 if (!fmt) {
382                         mfc_err("Unsupported format for source.\n");
383                         return -EINVAL;
384                 }
385                 if (!IS_MFCV6_PLUS(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
386                         mfc_err("Not supported format.\n");
387                         return -EINVAL;
388                 }
389         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
390                 fmt = find_format(f, MFC_FMT_RAW);
391                 if (!fmt) {
392                         mfc_err("Unsupported format for destination.\n");
393                         return -EINVAL;
394                 }
395                 if (IS_MFCV6_PLUS(dev) &&
396                                 (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
397                         mfc_err("Not supported format.\n");
398                         return -EINVAL;
399                 } else if (!IS_MFCV6_PLUS(dev) &&
400                                 (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
401                         mfc_err("Not supported format.\n");
402                         return -EINVAL;
403                 }
404         }
405
406         return 0;
407 }
408
409 /* Set format */
410 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
411 {
412         struct s5p_mfc_dev *dev = video_drvdata(file);
413         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
414         int ret = 0;
415         struct s5p_mfc_fmt *fmt;
416         struct v4l2_pix_format_mplane *pix_mp;
417
418         mfc_debug_enter();
419         ret = vidioc_try_fmt(file, priv, f);
420         pix_mp = &f->fmt.pix_mp;
421         if (ret)
422                 return ret;
423         if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
424                 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
425                 ret = -EBUSY;
426                 goto out;
427         }
428         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
429                 fmt = find_format(f, MFC_FMT_RAW);
430                 if (!fmt) {
431                         mfc_err("Unsupported format for source.\n");
432                         return -EINVAL;
433                 }
434                 if (!IS_MFCV6_PLUS(dev) &&
435                                 (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
436                         mfc_err("Not supported format.\n");
437                         return -EINVAL;
438                 } else if (IS_MFCV6_PLUS(dev) &&
439                                 (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
440                         mfc_err("Not supported format.\n");
441                         return -EINVAL;
442                 }
443                 ctx->dst_fmt = fmt;
444                 mfc_debug_leave();
445                 return ret;
446         } else if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
447                 mfc_err("Wrong type error for S_FMT : %d", f->type);
448                 return -EINVAL;
449         }
450         fmt = find_format(f, MFC_FMT_DEC);
451         if (!fmt || fmt->codec_mode == S5P_MFC_CODEC_NONE) {
452                 mfc_err("Unknown codec\n");
453                 ret = -EINVAL;
454                 goto out;
455         }
456         if (fmt->type != MFC_FMT_DEC) {
457                 mfc_err("Wrong format selected, you should choose "
458                                         "format for decoding\n");
459                 ret = -EINVAL;
460                 goto out;
461         }
462         if (!IS_MFCV6_PLUS(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
463                 mfc_err("Not supported format.\n");
464                 return -EINVAL;
465         }
466         ctx->src_fmt = fmt;
467         ctx->codec_mode = fmt->codec_mode;
468         mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
469         pix_mp->height = 0;
470         pix_mp->width = 0;
471         if (pix_mp->plane_fmt[0].sizeimage)
472                 ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
473         else
474                 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
475                                                                 DEF_CPB_SIZE;
476         pix_mp->plane_fmt[0].bytesperline = 0;
477         ctx->state = MFCINST_INIT;
478 out:
479         mfc_debug_leave();
480         return ret;
481 }
482
483 /* Reqeust buffers */
484 static int vidioc_reqbufs(struct file *file, void *priv,
485                                           struct v4l2_requestbuffers *reqbufs)
486 {
487         struct s5p_mfc_dev *dev = video_drvdata(file);
488         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
489         int ret = 0;
490
491         if (reqbufs->memory != V4L2_MEMORY_MMAP) {
492                 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
493                 return -EINVAL;
494         }
495         if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
496                 /* Can only request buffers after an instance has been opened.*/
497                 if (ctx->state == MFCINST_INIT) {
498                         ctx->src_bufs_cnt = 0;
499                         if (reqbufs->count == 0) {
500                                 mfc_debug(2, "Freeing buffers\n");
501                                 s5p_mfc_clock_on();
502                                 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
503                                 s5p_mfc_clock_off();
504                                 return ret;
505                         }
506                         /* Decoding */
507                         if (ctx->output_state != QUEUE_FREE) {
508                                 mfc_err("Bufs have already been requested\n");
509                                 return -EINVAL;
510                         }
511                         s5p_mfc_clock_on();
512                         ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
513                         s5p_mfc_clock_off();
514                         if (ret) {
515                                 mfc_err("vb2_reqbufs on output failed\n");
516                                 return ret;
517                         }
518                         mfc_debug(2, "vb2_reqbufs: %d\n", ret);
519                         ctx->output_state = QUEUE_BUFS_REQUESTED;
520                 }
521         } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
522                 ctx->dst_bufs_cnt = 0;
523                 if (reqbufs->count == 0) {
524                         mfc_debug(2, "Freeing buffers\n");
525                         s5p_mfc_clock_on();
526                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
527                         s5p_mfc_clock_off();
528                         return ret;
529                 }
530                 if (ctx->capture_state != QUEUE_FREE) {
531                         mfc_err("Bufs have already been requested\n");
532                         return -EINVAL;
533                 }
534                 ctx->capture_state = QUEUE_BUFS_REQUESTED;
535                 s5p_mfc_clock_on();
536                 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
537                 s5p_mfc_clock_off();
538                 if (ret) {
539                         mfc_err("vb2_reqbufs on capture failed\n");
540                         return ret;
541                 }
542                 if (reqbufs->count < ctx->pb_count) {
543                         mfc_err("Not enough buffers allocated\n");
544                         reqbufs->count = 0;
545                         s5p_mfc_clock_on();
546                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
547                         s5p_mfc_clock_off();
548                         return -ENOMEM;
549                 }
550                 ctx->total_dpb_count = reqbufs->count;
551                 ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
552                 if (ret) {
553                         mfc_err("Failed to allocate decoding buffers\n");
554                         reqbufs->count = 0;
555                         s5p_mfc_clock_on();
556                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
557                         s5p_mfc_clock_off();
558                         return -ENOMEM;
559                 }
560                 if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
561                         ctx->capture_state = QUEUE_BUFS_MMAPED;
562                 } else {
563                         mfc_err("Not all buffers passed to buf_init\n");
564                         reqbufs->count = 0;
565                         s5p_mfc_clock_on();
566                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
567                         s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
568                                         ctx);
569                         s5p_mfc_clock_off();
570                         return -ENOMEM;
571                 }
572                 if (s5p_mfc_ctx_ready(ctx))
573                         set_work_bit_irqsave(ctx);
574                 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
575                 s5p_mfc_wait_for_done_ctx(ctx,
576                                         S5P_MFC_R2H_CMD_INIT_BUFFERS_RET, 0);
577         }
578         return ret;
579 }
580
581 /* Query buffer */
582 static int vidioc_querybuf(struct file *file, void *priv,
583                                                    struct v4l2_buffer *buf)
584 {
585         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
586         int ret;
587         int i;
588
589         if (buf->memory != V4L2_MEMORY_MMAP) {
590                 mfc_err("Only mmaped buffers can be used\n");
591                 return -EINVAL;
592         }
593         mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
594         if (ctx->state == MFCINST_INIT &&
595                         buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
596                 ret = vb2_querybuf(&ctx->vq_src, buf);
597         } else if (ctx->state == MFCINST_RUNNING &&
598                         buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
599                 ret = vb2_querybuf(&ctx->vq_dst, buf);
600                 for (i = 0; i < buf->length; i++)
601                         buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
602         } else {
603                 mfc_err("vidioc_querybuf called in an inappropriate state\n");
604                 ret = -EINVAL;
605         }
606         mfc_debug_leave();
607         return ret;
608 }
609
610 /* Queue a buffer */
611 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
612 {
613         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
614
615         if (ctx->state == MFCINST_ERROR) {
616                 mfc_err("Call on QBUF after unrecoverable error\n");
617                 return -EIO;
618         }
619         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
620                 return vb2_qbuf(&ctx->vq_src, buf);
621         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
622                 return vb2_qbuf(&ctx->vq_dst, buf);
623         return -EINVAL;
624 }
625
626 /* Dequeue a buffer */
627 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
628 {
629         const struct v4l2_event ev = {
630                 .type = V4L2_EVENT_EOS
631         };
632         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
633         int ret;
634
635         if (ctx->state == MFCINST_ERROR) {
636                 mfc_err("Call on DQBUF after unrecoverable error\n");
637                 return -EIO;
638         }
639         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
640                 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
641         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
642                 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
643                 if (ret == 0 && ctx->state == MFCINST_FINISHED &&
644                                 list_empty(&ctx->vq_dst.done_list))
645                         v4l2_event_queue_fh(&ctx->fh, &ev);
646         } else {
647                 ret = -EINVAL;
648         }
649         return ret;
650 }
651
652 /* Export DMA buffer */
653 static int vidioc_expbuf(struct file *file, void *priv,
654         struct v4l2_exportbuffer *eb)
655 {
656         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
657
658         if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
659                 return vb2_expbuf(&ctx->vq_src, eb);
660         if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
661                 return vb2_expbuf(&ctx->vq_dst, eb);
662         return -EINVAL;
663 }
664
665 /* Stream on */
666 static int vidioc_streamon(struct file *file, void *priv,
667                            enum v4l2_buf_type type)
668 {
669         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
670         struct s5p_mfc_dev *dev = ctx->dev;
671         int ret = -EINVAL;
672
673         mfc_debug_enter();
674         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
675
676                 if (ctx->state == MFCINST_INIT) {
677                         ctx->dst_bufs_cnt = 0;
678                         ctx->src_bufs_cnt = 0;
679                         ctx->capture_state = QUEUE_FREE;
680                         ctx->output_state = QUEUE_FREE;
681                         s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer,
682                                         ctx);
683                         s5p_mfc_hw_call(dev->mfc_ops, alloc_dec_temp_buffers,
684                                         ctx);
685                         set_work_bit_irqsave(ctx);
686                         s5p_mfc_clean_ctx_int_flags(ctx);
687                         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
688
689                         if (s5p_mfc_wait_for_done_ctx(ctx,
690                                 S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
691                                 /* Error or timeout */
692                                 mfc_err("Error getting instance from hardware\n");
693                                 s5p_mfc_hw_call(dev->mfc_ops,
694                                                 release_instance_buffer, ctx);
695                                 s5p_mfc_hw_call(dev->mfc_ops,
696                                                 release_dec_desc_buffer, ctx);
697                                 return -EIO;
698                         }
699                         mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
700                 }
701                 ret = vb2_streamon(&ctx->vq_src, type);
702                 }
703         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
704                 ret = vb2_streamon(&ctx->vq_dst, type);
705         mfc_debug_leave();
706         return ret;
707 }
708
709 /* Stream off, which equals to a pause */
710 static int vidioc_streamoff(struct file *file, void *priv,
711                             enum v4l2_buf_type type)
712 {
713         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
714
715         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
716                 return vb2_streamoff(&ctx->vq_src, type);
717         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
718                 return vb2_streamoff(&ctx->vq_dst, type);
719         return -EINVAL;
720 }
721
722 /* Set controls - v4l2 control framework */
723 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
724 {
725         struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
726
727         switch (ctrl->id) {
728         case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
729                 ctx->display_delay = ctrl->val;
730                 break;
731         case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
732                 ctx->display_delay_enable = ctrl->val;
733                 break;
734         case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
735                 ctx->loop_filter_mpeg4 = ctrl->val;
736                 break;
737         case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
738                 ctx->slice_interface = ctrl->val;
739                 break;
740         default:
741                 mfc_err("Invalid control 0x%08x\n", ctrl->id);
742                 return -EINVAL;
743         }
744         return 0;
745 }
746
747 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
748 {
749         struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
750         struct s5p_mfc_dev *dev = ctx->dev;
751
752         switch (ctrl->id) {
753         case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
754                 if (ctx->state >= MFCINST_HEAD_PARSED &&
755                     ctx->state < MFCINST_ABORT) {
756                         ctrl->val = ctx->pb_count;
757                         break;
758                 } else if (ctx->state != MFCINST_INIT) {
759                         v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
760                         return -EINVAL;
761                 }
762                 /* Should wait for the header to be parsed */
763                 s5p_mfc_clean_ctx_int_flags(ctx);
764                 s5p_mfc_wait_for_done_ctx(ctx,
765                                 S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
766                 if (ctx->state >= MFCINST_HEAD_PARSED &&
767                     ctx->state < MFCINST_ABORT) {
768                         ctrl->val = ctx->pb_count;
769                 } else {
770                         v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
771                         return -EINVAL;
772                 }
773                 break;
774         }
775         return 0;
776 }
777
778
779 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
780         .s_ctrl = s5p_mfc_dec_s_ctrl,
781         .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
782 };
783
784 /* Get cropping information */
785 static int vidioc_g_crop(struct file *file, void *priv,
786                 struct v4l2_crop *cr)
787 {
788         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
789         struct s5p_mfc_dev *dev = ctx->dev;
790         u32 left, right, top, bottom;
791
792         if (ctx->state != MFCINST_HEAD_PARSED &&
793         ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
794                                         && ctx->state != MFCINST_FINISHED) {
795                         mfc_err("Cannont set crop\n");
796                         return -EINVAL;
797                 }
798         if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
799                 left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
800                 right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
801                 left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
802                 top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
803                 bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
804                 top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
805                 cr->c.left = left;
806                 cr->c.top = top;
807                 cr->c.width = ctx->img_width - left - right;
808                 cr->c.height = ctx->img_height - top - bottom;
809                 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
810                         "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
811                         cr->c.width, cr->c.height, right, bottom,
812                         ctx->buf_width, ctx->buf_height);
813         } else {
814                 cr->c.left = 0;
815                 cr->c.top = 0;
816                 cr->c.width = ctx->img_width;
817                 cr->c.height = ctx->img_height;
818                 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
819                         "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
820                                                         ctx->buf_height);
821         }
822         return 0;
823 }
824
825 int vidioc_decoder_cmd(struct file *file, void *priv,
826                                                 struct v4l2_decoder_cmd *cmd)
827 {
828         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
829         struct s5p_mfc_dev *dev = ctx->dev;
830         struct s5p_mfc_buf *buf;
831         unsigned long flags;
832
833         switch (cmd->cmd) {
834         case V4L2_ENC_CMD_STOP:
835                 if (cmd->flags != 0)
836                         return -EINVAL;
837
838                 if (!ctx->vq_src.streaming)
839                         return -EINVAL;
840
841                 spin_lock_irqsave(&dev->irqlock, flags);
842                 if (list_empty(&ctx->src_queue)) {
843                         mfc_err("EOS: empty src queue, entering finishing state");
844                         ctx->state = MFCINST_FINISHING;
845                         if (s5p_mfc_ctx_ready(ctx))
846                                 set_work_bit_irqsave(ctx);
847                         spin_unlock_irqrestore(&dev->irqlock, flags);
848                         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
849                 } else {
850                         mfc_err("EOS: marking last buffer of stream");
851                         buf = list_entry(ctx->src_queue.prev,
852                                                 struct s5p_mfc_buf, list);
853                         if (buf->flags & MFC_BUF_FLAG_USED)
854                                 ctx->state = MFCINST_FINISHING;
855                         else
856                                 buf->flags |= MFC_BUF_FLAG_EOS;
857                         spin_unlock_irqrestore(&dev->irqlock, flags);
858                 }
859                 break;
860         default:
861                 return -EINVAL;
862         }
863         return 0;
864 }
865
866 static int vidioc_subscribe_event(struct v4l2_fh *fh,
867                                 const struct  v4l2_event_subscription *sub)
868 {
869         switch (sub->type) {
870         case V4L2_EVENT_EOS:
871                 return v4l2_event_subscribe(fh, sub, 2, NULL);
872         default:
873                 return -EINVAL;
874         }
875 }
876
877
878 /* v4l2_ioctl_ops */
879 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
880         .vidioc_querycap = vidioc_querycap,
881         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
882         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
883         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
884         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
885         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
886         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
887         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
888         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
889         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
890         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
891         .vidioc_reqbufs = vidioc_reqbufs,
892         .vidioc_querybuf = vidioc_querybuf,
893         .vidioc_qbuf = vidioc_qbuf,
894         .vidioc_dqbuf = vidioc_dqbuf,
895         .vidioc_expbuf = vidioc_expbuf,
896         .vidioc_streamon = vidioc_streamon,
897         .vidioc_streamoff = vidioc_streamoff,
898         .vidioc_g_crop = vidioc_g_crop,
899         .vidioc_decoder_cmd = vidioc_decoder_cmd,
900         .vidioc_subscribe_event = vidioc_subscribe_event,
901         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
902 };
903
904 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
905                         const struct v4l2_format *fmt, unsigned int *buf_count,
906                         unsigned int *plane_count, unsigned int psize[],
907                         void *allocators[])
908 {
909         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
910         struct s5p_mfc_dev *dev = ctx->dev;
911
912         /* Video output for decoding (source)
913          * this can be set after getting an instance */
914         if (ctx->state == MFCINST_INIT &&
915             vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
916                 /* A single plane is required for input */
917                 *plane_count = 1;
918                 if (*buf_count < 1)
919                         *buf_count = 1;
920                 if (*buf_count > MFC_MAX_BUFFERS)
921                         *buf_count = MFC_MAX_BUFFERS;
922         /* Video capture for decoding (destination)
923          * this can be set after the header was parsed */
924         } else if (ctx->state == MFCINST_HEAD_PARSED &&
925                    vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
926                 /* Output plane count is 2 - one for Y and one for CbCr */
927                 *plane_count = 2;
928                 /* Setup buffer count */
929                 if (*buf_count < ctx->pb_count)
930                         *buf_count = ctx->pb_count;
931                 if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
932                         *buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
933                 if (*buf_count > MFC_MAX_BUFFERS)
934                         *buf_count = MFC_MAX_BUFFERS;
935         } else {
936                 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
937                                                         ctx->state, vq->type);
938                 return -EINVAL;
939         }
940         mfc_debug(2, "Buffer count=%d, plane count=%d\n",
941                                                 *buf_count, *plane_count);
942         if (ctx->state == MFCINST_HEAD_PARSED &&
943             vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
944                 psize[0] = ctx->luma_size;
945                 psize[1] = ctx->chroma_size;
946
947                 if (IS_MFCV6_PLUS(dev))
948                         allocators[0] =
949                                 ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
950                 else
951                         allocators[0] =
952                                 ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
953                 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
954         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
955                    ctx->state == MFCINST_INIT) {
956                 psize[0] = ctx->dec_src_buf_size;
957                 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
958         } else {
959                 mfc_err("This video node is dedicated to decoding. Decoding not initialized\n");
960                 return -EINVAL;
961         }
962         return 0;
963 }
964
965 static void s5p_mfc_unlock(struct vb2_queue *q)
966 {
967         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
968         struct s5p_mfc_dev *dev = ctx->dev;
969
970         mutex_unlock(&dev->mfc_mutex);
971 }
972
973 static void s5p_mfc_lock(struct vb2_queue *q)
974 {
975         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
976         struct s5p_mfc_dev *dev = ctx->dev;
977
978         mutex_lock(&dev->mfc_mutex);
979 }
980
981 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
982 {
983         struct vb2_queue *vq = vb->vb2_queue;
984         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
985         unsigned int i;
986
987         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
988                 if (ctx->capture_state == QUEUE_BUFS_MMAPED)
989                         return 0;
990                 for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
991                         if (IS_ERR_OR_NULL(ERR_PTR(
992                                         vb2_dma_contig_plane_dma_addr(vb, i)))) {
993                                 mfc_err("Plane mem not allocated\n");
994                                 return -EINVAL;
995                         }
996                 }
997                 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
998                         vb2_plane_size(vb, 1) < ctx->chroma_size) {
999                         mfc_err("Plane buffer (CAPTURE) is too small\n");
1000                         return -EINVAL;
1001                 }
1002                 i = vb->v4l2_buf.index;
1003                 ctx->dst_bufs[i].b = vb;
1004                 ctx->dst_bufs[i].cookie.raw.luma =
1005                                         vb2_dma_contig_plane_dma_addr(vb, 0);
1006                 ctx->dst_bufs[i].cookie.raw.chroma =
1007                                         vb2_dma_contig_plane_dma_addr(vb, 1);
1008                 ctx->dst_bufs_cnt++;
1009         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1010                 if (IS_ERR_OR_NULL(ERR_PTR(
1011                                         vb2_dma_contig_plane_dma_addr(vb, 0)))) {
1012                         mfc_err("Plane memory not allocated\n");
1013                         return -EINVAL;
1014                 }
1015                 if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
1016                         mfc_err("Plane buffer (OUTPUT) is too small\n");
1017                         return -EINVAL;
1018                 }
1019
1020                 i = vb->v4l2_buf.index;
1021                 ctx->src_bufs[i].b = vb;
1022                 ctx->src_bufs[i].cookie.stream =
1023                                         vb2_dma_contig_plane_dma_addr(vb, 0);
1024                 ctx->src_bufs_cnt++;
1025         } else {
1026                 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1027                 return -EINVAL;
1028         }
1029         return 0;
1030 }
1031
1032 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1033 {
1034         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1035         struct s5p_mfc_dev *dev = ctx->dev;
1036
1037         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1038         if (ctx->state == MFCINST_FINISHING ||
1039                 ctx->state == MFCINST_FINISHED)
1040                 ctx->state = MFCINST_RUNNING;
1041         /* If context is ready then dev = work->data;schedule it to run */
1042         if (s5p_mfc_ctx_ready(ctx))
1043                 set_work_bit_irqsave(ctx);
1044         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1045         return 0;
1046 }
1047
1048 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1049 {
1050         unsigned long flags;
1051         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1052         struct s5p_mfc_dev *dev = ctx->dev;
1053         int aborted = 0;
1054
1055         if ((ctx->state == MFCINST_FINISHING ||
1056                 ctx->state ==  MFCINST_RUNNING) &&
1057                 dev->curr_ctx == ctx->num && dev->hw_lock) {
1058                 ctx->state = MFCINST_ABORT;
1059                 s5p_mfc_wait_for_done_ctx(ctx,
1060                                         S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
1061                 aborted = 1;
1062         }
1063         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1064                 spin_lock_irqsave(&dev->irqlock, flags);
1065                 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
1066                                 &ctx->vq_dst);
1067                 INIT_LIST_HEAD(&ctx->dst_queue);
1068                 ctx->dst_queue_cnt = 0;
1069                 ctx->dpb_flush_flag = 1;
1070                 ctx->dec_dst_flag = 0;
1071                 spin_unlock_irqrestore(&dev->irqlock, flags);
1072                 if (IS_MFCV6_PLUS(dev) && (ctx->state == MFCINST_RUNNING)) {
1073                         ctx->state = MFCINST_FLUSH;
1074                         set_work_bit_irqsave(ctx);
1075                         s5p_mfc_clean_ctx_int_flags(ctx);
1076                         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1077                         if (s5p_mfc_wait_for_done_ctx(ctx,
1078                                 S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
1079                                 mfc_err("Err flushing buffers\n");
1080                 }
1081         }
1082         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1083                 spin_lock_irqsave(&dev->irqlock, flags);
1084                 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1085                                 &ctx->vq_src);
1086                 INIT_LIST_HEAD(&ctx->src_queue);
1087                 ctx->src_queue_cnt = 0;
1088                 spin_unlock_irqrestore(&dev->irqlock, flags);
1089         }
1090         if (aborted)
1091                 ctx->state = MFCINST_RUNNING;
1092         return 0;
1093 }
1094
1095
1096 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1097 {
1098         struct vb2_queue *vq = vb->vb2_queue;
1099         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1100         struct s5p_mfc_dev *dev = ctx->dev;
1101         unsigned long flags;
1102         struct s5p_mfc_buf *mfc_buf;
1103
1104         if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1105                 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1106                 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1107                 spin_lock_irqsave(&dev->irqlock, flags);
1108                 list_add_tail(&mfc_buf->list, &ctx->src_queue);
1109                 ctx->src_queue_cnt++;
1110                 spin_unlock_irqrestore(&dev->irqlock, flags);
1111         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1112                 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1113                 mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1114                 /* Mark destination as available for use by MFC */
1115                 spin_lock_irqsave(&dev->irqlock, flags);
1116                 set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1117                 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1118                 ctx->dst_queue_cnt++;
1119                 spin_unlock_irqrestore(&dev->irqlock, flags);
1120         } else {
1121                 mfc_err("Unsupported buffer type (%d)\n", vq->type);
1122         }
1123         if (s5p_mfc_ctx_ready(ctx))
1124                 set_work_bit_irqsave(ctx);
1125         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1126 }
1127
1128 static struct vb2_ops s5p_mfc_dec_qops = {
1129         .queue_setup            = s5p_mfc_queue_setup,
1130         .wait_prepare           = s5p_mfc_unlock,
1131         .wait_finish            = s5p_mfc_lock,
1132         .buf_init               = s5p_mfc_buf_init,
1133         .start_streaming        = s5p_mfc_start_streaming,
1134         .stop_streaming         = s5p_mfc_stop_streaming,
1135         .buf_queue              = s5p_mfc_buf_queue,
1136 };
1137
1138 struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1139 {
1140         return &decoder_codec_ops;
1141 }
1142
1143 struct vb2_ops *get_dec_queue_ops(void)
1144 {
1145         return &s5p_mfc_dec_qops;
1146 }
1147
1148 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1149 {
1150         return &s5p_mfc_dec_ioctl_ops;
1151 }
1152
1153 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1154                                                 && V4L2_CTRL_DRIVER_PRIV(x))
1155
1156 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1157 {
1158         struct v4l2_ctrl_config cfg;
1159         int i;
1160
1161         v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1162         if (ctx->ctrl_handler.error) {
1163                 mfc_err("v4l2_ctrl_handler_init failed\n");
1164                 return ctx->ctrl_handler.error;
1165         }
1166
1167         for (i = 0; i < NUM_CTRLS; i++) {
1168                 if (IS_MFC51_PRIV(controls[i].id)) {
1169                         memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1170                         cfg.ops = &s5p_mfc_dec_ctrl_ops;
1171                         cfg.id = controls[i].id;
1172                         cfg.min = controls[i].minimum;
1173                         cfg.max = controls[i].maximum;
1174                         cfg.def = controls[i].default_value;
1175                         cfg.name = controls[i].name;
1176                         cfg.type = controls[i].type;
1177
1178                         cfg.step = controls[i].step;
1179                         cfg.menu_skip_mask = 0;
1180
1181                         ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1182                                         &cfg, NULL);
1183                 } else {
1184                         ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1185                                         &s5p_mfc_dec_ctrl_ops,
1186                                         controls[i].id, controls[i].minimum,
1187                                         controls[i].maximum, controls[i].step,
1188                                         controls[i].default_value);
1189                 }
1190                 if (ctx->ctrl_handler.error) {
1191                         mfc_err("Adding control (%d) failed\n", i);
1192                         return ctx->ctrl_handler.error;
1193                 }
1194                 if (controls[i].is_volatile && ctx->ctrls[i])
1195                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1196         }
1197         return 0;
1198 }
1199
1200 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1201 {
1202         int i;
1203
1204         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1205         for (i = 0; i < NUM_CTRLS; i++)
1206                 ctx->ctrls[i] = NULL;
1207 }
1208
1209 void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1210 {
1211         struct v4l2_format f;
1212         f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
1213         ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1214         f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
1215         ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1216         mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1217                         (unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);
1218 }
1219