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