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