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