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