17 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/videodev2.h>
35 #define S5P_MFC_NAME "s5p-mfc"
36 #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
37 #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
73 spin_unlock_irqrestore(&dev->
condlock, flags);
84 spin_unlock_irqrestore(&dev->
condlock, flags);
107 static void s5p_mfc_watchdog(
unsigned long arg)
119 mfc_err(
"Time out during waiting for HW\n");
137 mfc_err(
"Driver timeout error handling\n");
142 mfc_err(
"Error: some instance may be closing/opening\n");
160 spin_unlock_irqrestore(&dev->
irqlock, flags);
166 mfc_err(
"Failed to reload FW\n");
172 mfc_err(
"Failed to reinit FW\n");
184 mfc_err(
"failed to get video_device");
187 if (vdev->
index == 0)
189 else if (vdev->
index == 1)
194 static void s5p_mfc_clear_int_flags(
struct s5p_mfc_dev *dev)
201 static void s5p_mfc_handle_frame_all_extracted(
struct s5p_mfc_ctx *ctx)
212 dst_buf->
b->v4l2_buf.index);
213 vb2_set_plane_payload(dst_buf->
b, 0, 0);
214 vb2_set_plane_payload(dst_buf->
b, 1, 0);
217 dst_buf->
b->v4l2_buf.sequence = (ctx->
sequence++);
230 static void s5p_mfc_handle_frame_copy_time(
struct s5p_mfc_ctx *ctx)
244 if (vb2_dma_contig_plane_dma_addr(dst_buf->
b, 0) == dec_y_addr) {
245 memcpy(&dst_buf->
b->v4l2_buf.timecode,
246 &src_buf->
b->v4l2_buf.timecode,
248 memcpy(&dst_buf->
b->v4l2_buf.timestamp,
249 &src_buf->
b->v4l2_buf.timestamp,
251 switch (frame_type) {
253 dst_buf->
b->v4l2_buf.flags |=
257 dst_buf->
b->v4l2_buf.flags |=
261 dst_buf->
b->v4l2_buf.flags |=
270 static void s5p_mfc_handle_frame_new(
struct s5p_mfc_ctx *ctx,
unsigned int err)
293 if (vb2_dma_contig_plane_dma_addr(dst_buf->
b, 0) == dspl_y_addr) {
296 dst_buf->
b->v4l2_buf.sequence = ctx->
sequence;
298 get_pic_type_top, ctx) ==
300 get_pic_type_bot, ctx))
303 dst_buf->
b->v4l2_buf.field =
305 vb2_set_plane_payload(dst_buf->
b, 0, ctx->
luma_size);
313 index = dst_buf->
b->v4l2_buf.index;
320 static void s5p_mfc_handle_frame(
struct s5p_mfc_ctx *ctx,
321 unsigned int reason,
unsigned int err)
324 unsigned int dst_frame_status;
327 unsigned int res_change;
336 mfc_debug(2,
"Frame Status: %x\n", dst_frame_status);
343 wake_up_ctx(ctx, reason, err);
357 s5p_mfc_handle_frame_all_extracted(ctx);
359 goto leave_handle_frame;
361 s5p_mfc_handle_frame_all_extracted(ctx);
367 s5p_mfc_handle_frame_copy_time(ctx);
372 s5p_mfc_handle_frame_new(ctx, err);
382 get_consumed_stream, dev);
385 src_buf->
b->v4l2_planes[0].bytesused) {
387 mfc_debug(2,
"Running again the same buffer\n");
390 index = src_buf->
b->v4l2_buf.index;
402 spin_unlock_irqrestore(&dev->
irqlock, flags);
407 wake_up_ctx(ctx, reason, err);
415 static void s5p_mfc_handle_error(
struct s5p_mfc_ctx *ctx,
416 unsigned int reason,
unsigned int err)
427 mfc_err(
"Interrupt Error: %08x\n", err);
429 wake_up_dev(dev, reason, err);
432 switch (ctx->
state) {
442 wake_up_ctx(ctx, reason, err);
462 spin_unlock_irqrestore(&dev->
irqlock, flags);
468 mfc_err(
"Encountered an error interrupt which had not been handled\n");
475 static void s5p_mfc_handle_seq_done(
struct s5p_mfc_ctx *ctx,
476 unsigned int reason,
unsigned int err)
483 if (ctx->
c_ops->post_seq_start) {
484 if (ctx->
c_ops->post_seq_start(ctx))
485 mfc_err(
"post_seq_start() failed\n");
511 src_buf->
b->v4l2_planes[0].bytesused)
525 wake_up_ctx(ctx, reason, err);
529 static void s5p_mfc_handle_init_buffers(
struct s5p_mfc_ctx *ctx,
530 unsigned int reason,
unsigned int err)
556 spin_unlock_irqrestore(&dev->
irqlock, flags);
577 static void s5p_mfc_handle_stream_complete(
struct s5p_mfc_ctx *ctx,
578 unsigned int reason,
unsigned int err)
585 s5p_mfc_clear_int_flags(dev);
596 vb2_set_plane_payload(mb_entry->
b, 0, 0);
626 mfc_debug(1,
"Int reason: %d (err: %08x)\n", reason, err);
633 s5p_mfc_handle_frame(ctx, reason, err);
635 s5p_mfc_handle_error(ctx, reason, err);
642 if (ctx->
c_ops->post_frame_start) {
643 if (ctx->
c_ops->post_frame_start(ctx))
644 mfc_err(
"post_frame_start() failed\n");
646 wake_up_ctx(ctx, reason, err);
652 s5p_mfc_handle_frame(ctx, reason, err);
657 s5p_mfc_handle_seq_done(ctx, reason, err);
680 wake_up_dev(dev, reason, err);
686 s5p_mfc_handle_init_buffers(ctx, reason, err);
690 s5p_mfc_handle_stream_complete(ctx, reason, err);
705 mfc_err(
"Failed to unlock hw\n");
710 mfc_debug(2,
"Exit via irq_cleanup_hw\n");
715 static int s5p_mfc_open(
struct file *file)
729 mfc_err(
"Not enough memory\n");
743 while (dev->
ctx[ctx->
num]) {
745 if (ctx->
num >= MFC_NUM_CONTEXTS) {
746 mfc_err(
"Too many open contexts\n");
761 mfc_err(
"Failed to setup mfc controls\n");
762 goto err_ctrls_setup;
774 mfc_err(
"Failed to setup mfc controls\n");
775 goto err_ctrls_setup;
820 mfc_err(
"Failed to initialize videobuf2 queue(capture)\n");
841 mfc_err(
"Failed to initialize videobuf2 queue(output)\n");
877 static int s5p_mfc_release(
struct file *file)
901 mfc_err(
"Err returning instance\n");
918 mfc_debug(2,
"Last instance - release firmware\n");
940 static unsigned int s5p_mfc_poll(
struct file *file,
964 poll_wait(file, &ctx->
fh.wait, wait);
965 poll_wait(file, &src_q->
done_wq, wait);
966 poll_wait(file, &dst_q->
done_wq, wait);
977 spin_unlock_irqrestore(&src_q->
done_lock, flags);
985 spin_unlock_irqrestore(&dst_q->
done_lock, flags);
992 static int s5p_mfc_mmap(
struct file *file,
struct vm_area_struct *vma)
1016 .open = s5p_mfc_open,
1017 .release = s5p_mfc_release,
1018 .poll = s5p_mfc_poll,
1020 .mmap = s5p_mfc_mmap,
1023 static int match_child(
struct device *dev,
void *
data)
1027 return !
strcmp(dev_name(dev), (
char *)data);
1041 dev_err(&pdev->
dev,
"Not enough memory for MFC device\n");
1049 dev_err(&pdev->
dev,
"No platform data specified\n");
1058 dev_err(&pdev->
dev,
"failed to get mfc clock source\n");
1066 dev_err(&pdev->
dev,
"Failed to obtain io memory\n");
1072 dev_err(&pdev->
dev,
"failed to get irq resource\n");
1077 ret = devm_request_irq(&pdev->
dev, dev->
irq, s5p_mfc_irq,
1080 dev_err(&pdev->
dev,
"Failed to install irq (%d)\n", ret);
1087 mfc_err(
"Mem child (L) device get failed\n");
1095 mfc_err(
"Mem child (R) device get failed\n");
1101 if (IS_ERR_OR_NULL(dev->
alloc_ctx[0])) {
1106 if (IS_ERR_OR_NULL(dev->
alloc_ctx[1])) {
1108 goto err_mem_init_ctx_1;
1115 goto err_v4l2_dev_reg;
1125 vfd->
fops = &s5p_mfc_fops,
1140 "decoder registered as /dev/video%d\n", vfd->
num);
1141 video_set_drvdata(vfd, dev);
1150 vfd->
fops = &s5p_mfc_fops,
1165 "encoder registered as /dev/video%d\n", vfd->
num);
1166 video_set_drvdata(vfd, dev);
1167 platform_set_drvdata(pdev, dev);
1200 pr_debug(
"%s-- with error\n", __func__);
1208 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1226 #ifdef CONFIG_PM_SLEEP
1228 static int s5p_mfc_suspend(
struct device *dev)
1231 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1238 mfc_err(
"Error: going to suspend for a second time\n");
1251 mfc_err(
"Waiting for hardware to finish timed out\n");
1259 static int s5p_mfc_resume(
struct device *dev)
1262 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1270 #ifdef CONFIG_PM_RUNTIME
1271 static int s5p_mfc_runtime_suspend(
struct device *dev)
1274 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1280 static int s5p_mfc_runtime_resume(
struct device *dev)
1283 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1295 static const struct dev_pm_ops s5p_mfc_pm_ops = {
1323 .mclk_name =
"sclk_mfc",
1324 .fw_name =
"s5p-mfc.fw",
1350 .mclk_name =
"aclk_333",
1351 .fw_name =
"s5p-mfc-v6.fw",
1357 .driver_data = (
unsigned long)&mfc_drvdata_v5,
1359 .name =
"s5p-mfc-v5",
1360 .driver_data = (
unsigned long)&mfc_drvdata_v5,
1362 .name =
"s5p-mfc-v6",
1363 .driver_data = (
unsigned long)&mfc_drvdata_v6,
1370 .probe = s5p_mfc_probe,
1372 .id_table = mfc_driver_ids,
1376 .pm = &s5p_mfc_pm_ops