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