2 * Samsung S5P Multi Format Codec v 5.1
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Kamil Debski, <k.debski@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.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/videodev2.h>
22 #include <media/v4l2-event.h>
23 #include <linux/workqueue.h>
25 #include <linux/of_device.h>
26 #include <linux/of_reserved_mem.h>
27 #include <media/videobuf2-v4l2.h>
28 #include "s5p_mfc_common.h"
29 #include "s5p_mfc_ctrl.h"
30 #include "s5p_mfc_debug.h"
31 #include "s5p_mfc_dec.h"
32 #include "s5p_mfc_enc.h"
33 #include "s5p_mfc_intr.h"
34 #include "s5p_mfc_iommu.h"
35 #include "s5p_mfc_opr.h"
36 #include "s5p_mfc_cmd.h"
37 #include "s5p_mfc_pm.h"
39 #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
40 #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
43 module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
44 MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
46 static char *mfc_mem_size;
47 module_param_named(mem, mfc_mem_size, charp, 0644);
48 MODULE_PARM_DESC(mem, "Preallocated memory size for the firmware and context buffers");
50 /* Helper functions for interrupt processing */
52 /* Remove from hw execution round robin */
53 void clear_work_bit(struct s5p_mfc_ctx *ctx)
55 struct s5p_mfc_dev *dev = ctx->dev;
57 spin_lock(&dev->condlock);
58 __clear_bit(ctx->num, &dev->ctx_work_bits);
59 spin_unlock(&dev->condlock);
62 /* Add to hw execution round robin */
63 void set_work_bit(struct s5p_mfc_ctx *ctx)
65 struct s5p_mfc_dev *dev = ctx->dev;
67 spin_lock(&dev->condlock);
68 __set_bit(ctx->num, &dev->ctx_work_bits);
69 spin_unlock(&dev->condlock);
72 /* Remove from hw execution round robin */
73 void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
75 struct s5p_mfc_dev *dev = ctx->dev;
78 spin_lock_irqsave(&dev->condlock, flags);
79 __clear_bit(ctx->num, &dev->ctx_work_bits);
80 spin_unlock_irqrestore(&dev->condlock, flags);
83 /* Add to hw execution round robin */
84 void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
86 struct s5p_mfc_dev *dev = ctx->dev;
89 spin_lock_irqsave(&dev->condlock, flags);
90 __set_bit(ctx->num, &dev->ctx_work_bits);
91 spin_unlock_irqrestore(&dev->condlock, flags);
94 int s5p_mfc_get_new_ctx(struct s5p_mfc_dev *dev)
99 spin_lock_irqsave(&dev->condlock, flags);
102 ctx = (ctx + 1) % MFC_NUM_CONTEXTS;
103 if (ctx == dev->curr_ctx) {
104 if (!test_bit(ctx, &dev->ctx_work_bits))
108 } while (!test_bit(ctx, &dev->ctx_work_bits));
109 spin_unlock_irqrestore(&dev->condlock, flags);
114 /* Wake up context wait_queue */
115 static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
119 ctx->int_type = reason;
121 wake_up(&ctx->queue);
124 /* Wake up device wait_queue */
125 static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
129 dev->int_type = reason;
131 wake_up(&dev->queue);
134 void s5p_mfc_cleanup_queue(struct list_head *lh, struct vb2_queue *vq)
136 struct s5p_mfc_buf *b;
139 while (!list_empty(lh)) {
140 b = list_entry(lh->next, struct s5p_mfc_buf, list);
141 for (i = 0; i < b->b->vb2_buf.num_planes; i++)
142 vb2_set_plane_payload(&b->b->vb2_buf, i, 0);
143 vb2_buffer_done(&b->b->vb2_buf, VB2_BUF_STATE_ERROR);
148 static void s5p_mfc_watchdog(struct timer_list *t)
150 struct s5p_mfc_dev *dev = from_timer(dev, t, watchdog_timer);
152 if (test_bit(0, &dev->hw_lock))
153 atomic_inc(&dev->watchdog_cnt);
154 if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
155 /* This means that hw is busy and no interrupts were
156 * generated by hw for the Nth time of running this
157 * watchdog timer. This usually means a serious hw
158 * error. Now it is time to kill all instances and
160 mfc_err("Time out during waiting for HW\n");
161 schedule_work(&dev->watchdog_work);
163 dev->watchdog_timer.expires = jiffies +
164 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
165 add_timer(&dev->watchdog_timer);
168 static void s5p_mfc_watchdog_worker(struct work_struct *work)
170 struct s5p_mfc_dev *dev;
171 struct s5p_mfc_ctx *ctx;
176 dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
178 mfc_err("Driver timeout error handling\n");
179 /* Lock the mutex that protects open and release.
180 * This is necessary as they may load and unload firmware. */
181 mutex_locked = mutex_trylock(&dev->mfc_mutex);
183 mfc_err("Error: some instance may be closing/opening\n");
184 spin_lock_irqsave(&dev->irqlock, flags);
188 for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
192 ctx->state = MFCINST_ERROR;
193 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
194 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
196 wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
198 clear_bit(0, &dev->hw_lock);
199 spin_unlock_irqrestore(&dev->irqlock, flags);
202 s5p_mfc_deinit_hw(dev);
204 /* Double check if there is at least one instance running.
205 * If no instance is in memory than no firmware should be present */
206 if (dev->num_inst > 0) {
207 ret = s5p_mfc_load_firmware(dev);
209 mfc_err("Failed to reload FW\n");
213 ret = s5p_mfc_init_hw(dev);
216 mfc_err("Failed to reinit FW\n");
220 mutex_unlock(&dev->mfc_mutex);
223 static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
225 struct s5p_mfc_buf *dst_buf;
226 struct s5p_mfc_dev *dev = ctx->dev;
228 ctx->state = MFCINST_FINISHED;
230 while (!list_empty(&ctx->dst_queue)) {
231 dst_buf = list_entry(ctx->dst_queue.next,
232 struct s5p_mfc_buf, list);
233 mfc_debug(2, "Cleaning up buffer: %d\n",
234 dst_buf->b->vb2_buf.index);
235 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
236 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
237 list_del(&dst_buf->list);
238 dst_buf->flags |= MFC_BUF_FLAG_EOS;
239 ctx->dst_queue_cnt--;
240 dst_buf->b->sequence = (ctx->sequence++);
242 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
243 s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
244 dst_buf->b->field = V4L2_FIELD_NONE;
246 dst_buf->b->field = V4L2_FIELD_INTERLACED;
247 dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
249 ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
250 vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
254 static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
256 struct s5p_mfc_dev *dev = ctx->dev;
257 struct s5p_mfc_buf *dst_buf, *src_buf;
259 unsigned int frame_type;
261 /* Make sure we actually have a new frame before continuing. */
262 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
263 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
265 dec_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
267 /* Copy timestamp / timecode from decoded src to dst and set
268 appropriate flags. */
269 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
270 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
271 u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
273 if (addr == dec_y_addr) {
274 dst_buf->b->timecode = src_buf->b->timecode;
275 dst_buf->b->vb2_buf.timestamp =
276 src_buf->b->vb2_buf.timestamp;
278 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
281 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
282 switch (frame_type) {
283 case S5P_FIMV_DECODE_FRAME_I_FRAME:
285 V4L2_BUF_FLAG_KEYFRAME;
287 case S5P_FIMV_DECODE_FRAME_P_FRAME:
289 V4L2_BUF_FLAG_PFRAME;
291 case S5P_FIMV_DECODE_FRAME_B_FRAME:
293 V4L2_BUF_FLAG_BFRAME;
296 /* Don't know how to handle
297 S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
298 mfc_debug(2, "Unexpected frame type: %d\n",
306 static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
308 struct s5p_mfc_dev *dev = ctx->dev;
309 struct s5p_mfc_buf *dst_buf;
311 unsigned int frame_type;
313 dspl_y_addr = (u32)s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
314 if (IS_MFCV6_PLUS(dev))
315 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
316 get_disp_frame_type, ctx);
318 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
319 get_dec_frame_type, dev);
321 /* If frame is same as previous then skip and do not dequeue */
322 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
323 if (!ctx->after_packed_pb)
325 ctx->after_packed_pb = 0;
329 /* The MFC returns address of the buffer, now we have to
330 * check which videobuf does it correspond to */
331 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
332 u32 addr = (u32)vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0);
334 /* Check if this is the buffer we're looking for */
335 if (addr == dspl_y_addr) {
336 list_del(&dst_buf->list);
337 ctx->dst_queue_cnt--;
338 dst_buf->b->sequence = ctx->sequence;
339 if (s5p_mfc_hw_call(dev->mfc_ops,
340 get_pic_type_top, ctx) ==
341 s5p_mfc_hw_call(dev->mfc_ops,
342 get_pic_type_bot, ctx))
343 dst_buf->b->field = V4L2_FIELD_NONE;
346 V4L2_FIELD_INTERLACED;
347 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
349 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
351 clear_bit(dst_buf->b->vb2_buf.index,
354 vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
355 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
362 /* Handle frame decoding interrupt */
363 static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
364 unsigned int reason, unsigned int err)
366 struct s5p_mfc_dev *dev = ctx->dev;
367 unsigned int dst_frame_status;
368 unsigned int dec_frame_status;
369 struct s5p_mfc_buf *src_buf;
370 unsigned int res_change;
372 dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
373 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
374 dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
375 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
376 res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
377 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
378 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
379 mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
380 if (ctx->state == MFCINST_RES_CHANGE_INIT)
381 ctx->state = MFCINST_RES_CHANGE_FLUSH;
382 if (res_change == S5P_FIMV_RES_INCREASE ||
383 res_change == S5P_FIMV_RES_DECREASE) {
384 ctx->state = MFCINST_RES_CHANGE_INIT;
385 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
386 wake_up_ctx(ctx, reason, err);
387 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
389 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
392 if (ctx->dpb_flush_flag)
393 ctx->dpb_flush_flag = 0;
395 /* All frames remaining in the buffer have been extracted */
396 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
397 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
398 static const struct v4l2_event ev_src_ch = {
399 .type = V4L2_EVENT_SOURCE_CHANGE,
400 .u.src_change.changes =
401 V4L2_EVENT_SRC_CH_RESOLUTION,
404 s5p_mfc_handle_frame_all_extracted(ctx);
405 ctx->state = MFCINST_RES_CHANGE_END;
406 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
408 goto leave_handle_frame;
410 s5p_mfc_handle_frame_all_extracted(ctx);
414 if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
415 s5p_mfc_handle_frame_copy_time(ctx);
417 /* A frame has been decoded and is in the buffer */
418 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
419 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
420 s5p_mfc_handle_frame_new(ctx, err);
422 mfc_debug(2, "No frame decode\n");
424 /* Mark source buffer as complete */
425 if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
426 && !list_empty(&ctx->src_queue)) {
427 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
429 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
430 get_consumed_stream, dev);
431 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
432 ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
433 ctx->consumed_stream + STUFF_BYTE <
434 src_buf->b->vb2_buf.planes[0].bytesused) {
435 /* Run MFC again on the same buffer */
436 mfc_debug(2, "Running again the same buffer\n");
437 ctx->after_packed_pb = 1;
439 mfc_debug(2, "MFC needs next buffer\n");
440 ctx->consumed_stream = 0;
441 if (src_buf->flags & MFC_BUF_FLAG_EOS)
442 ctx->state = MFCINST_FINISHING;
443 list_del(&src_buf->list);
444 ctx->src_queue_cnt--;
445 if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
446 vb2_buffer_done(&src_buf->b->vb2_buf,
447 VB2_BUF_STATE_ERROR);
449 vb2_buffer_done(&src_buf->b->vb2_buf,
454 if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
455 || ctx->dst_queue_cnt < ctx->pb_count)
457 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
458 wake_up_ctx(ctx, reason, err);
459 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
461 /* if suspending, wake up device and do not try_run again*/
462 if (test_bit(0, &dev->enter_suspend))
463 wake_up_dev(dev, reason, err);
465 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
468 /* Error handling for interrupt */
469 static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
470 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
472 mfc_err("Interrupt Error: %08x\n", err);
475 /* Error recovery is dependent on the state of context */
476 switch (ctx->state) {
477 case MFCINST_RES_CHANGE_INIT:
478 case MFCINST_RES_CHANGE_FLUSH:
479 case MFCINST_RES_CHANGE_END:
480 case MFCINST_FINISHING:
481 case MFCINST_FINISHED:
482 case MFCINST_RUNNING:
483 /* It is highly probable that an error occurred
484 * while decoding a frame */
486 ctx->state = MFCINST_ERROR;
487 /* Mark all dst buffers as having an error */
488 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
489 /* Mark all src buffers as having an error */
490 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
491 wake_up_ctx(ctx, reason, err);
495 ctx->state = MFCINST_ERROR;
496 wake_up_ctx(ctx, reason, err);
500 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
501 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
503 wake_up_dev(dev, reason, err);
506 /* Header parsing interrupt handling */
507 static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
508 unsigned int reason, unsigned int err)
510 struct s5p_mfc_dev *dev;
515 if (ctx->c_ops->post_seq_start) {
516 if (ctx->c_ops->post_seq_start(ctx))
517 mfc_err("post_seq_start() failed\n");
519 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
521 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
524 s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
526 ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
528 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
530 if (FW_HAS_E_MIN_SCRATCH_BUF(dev))
531 ctx->scratch_buf_size = s5p_mfc_hw_call(dev->mfc_ops,
532 get_min_scratch_buf_size, dev);
533 if (ctx->img_width == 0 || ctx->img_height == 0)
534 ctx->state = MFCINST_ERROR;
536 ctx->state = MFCINST_HEAD_PARSED;
538 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
539 ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
540 !list_empty(&ctx->src_queue)) {
541 struct s5p_mfc_buf *src_buf;
542 src_buf = list_entry(ctx->src_queue.next,
543 struct s5p_mfc_buf, list);
544 if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
546 src_buf->b->vb2_buf.planes[0].bytesused)
547 ctx->head_processed = 0;
549 ctx->head_processed = 1;
551 ctx->head_processed = 1;
554 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
556 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
558 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
559 wake_up_ctx(ctx, reason, err);
562 /* Header parsing interrupt handling */
563 static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
564 unsigned int reason, unsigned int err)
566 struct s5p_mfc_buf *src_buf;
567 struct s5p_mfc_dev *dev;
572 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
573 ctx->int_type = reason;
578 ctx->state = MFCINST_RUNNING;
579 if (!ctx->dpb_flush_flag && ctx->head_processed) {
580 if (!list_empty(&ctx->src_queue)) {
581 src_buf = list_entry(ctx->src_queue.next,
582 struct s5p_mfc_buf, list);
583 list_del(&src_buf->list);
584 ctx->src_queue_cnt--;
585 vb2_buffer_done(&src_buf->b->vb2_buf,
589 ctx->dpb_flush_flag = 0;
591 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
595 wake_up(&ctx->queue);
596 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
598 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
602 wake_up(&ctx->queue);
606 static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
608 struct s5p_mfc_dev *dev = ctx->dev;
609 struct s5p_mfc_buf *mb_entry;
611 mfc_debug(2, "Stream completed\n");
613 ctx->state = MFCINST_FINISHED;
615 if (!list_empty(&ctx->dst_queue)) {
616 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
618 list_del(&mb_entry->list);
619 ctx->dst_queue_cnt--;
620 vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
621 vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
626 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
629 wake_up(&ctx->queue);
630 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
633 /* Interrupt processing */
634 static irqreturn_t s5p_mfc_irq(int irq, void *priv)
636 struct s5p_mfc_dev *dev = priv;
637 struct s5p_mfc_ctx *ctx;
642 /* Reset the timeout watchdog */
643 atomic_set(&dev->watchdog_cnt, 0);
644 spin_lock(&dev->irqlock);
645 ctx = dev->ctx[dev->curr_ctx];
646 /* Get the reason of interrupt and the error code */
647 reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
648 err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
649 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
651 case S5P_MFC_R2H_CMD_ERR_RET:
652 /* An error has occurred */
653 if (ctx->state == MFCINST_RUNNING &&
654 (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
656 err == S5P_FIMV_ERR_NO_VALID_SEQ_HDR ||
657 err == S5P_FIMV_ERR_INCOMPLETE_FRAME ||
658 err == S5P_FIMV_ERR_TIMEOUT))
659 s5p_mfc_handle_frame(ctx, reason, err);
661 s5p_mfc_handle_error(dev, ctx, reason, err);
662 clear_bit(0, &dev->enter_suspend);
665 case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
666 case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
667 case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
668 if (ctx->c_ops->post_frame_start) {
669 if (ctx->c_ops->post_frame_start(ctx))
670 mfc_err("post_frame_start() failed\n");
672 if (ctx->state == MFCINST_FINISHING &&
673 list_empty(&ctx->ref_queue)) {
674 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
675 s5p_mfc_handle_stream_complete(ctx);
678 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
679 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
681 wake_up_ctx(ctx, reason, err);
682 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
684 s5p_mfc_handle_frame(ctx, reason, err);
688 case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
689 s5p_mfc_handle_seq_done(ctx, reason, err);
692 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
693 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
694 ctx->state = MFCINST_GOT_INST;
697 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
698 ctx->inst_no = MFC_NO_INSTANCE_SET;
699 ctx->state = MFCINST_FREE;
702 case S5P_MFC_R2H_CMD_SYS_INIT_RET:
703 case S5P_MFC_R2H_CMD_FW_STATUS_RET:
704 case S5P_MFC_R2H_CMD_SLEEP_RET:
705 case S5P_MFC_R2H_CMD_WAKEUP_RET:
708 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
709 clear_bit(0, &dev->hw_lock);
710 clear_bit(0, &dev->enter_suspend);
711 wake_up_dev(dev, reason, err);
714 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
715 s5p_mfc_handle_init_buffers(ctx, reason, err);
718 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
719 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
720 ctx->int_type = reason;
722 s5p_mfc_handle_stream_complete(ctx);
725 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
726 ctx->state = MFCINST_RUNNING;
730 mfc_debug(2, "Unknown int reason\n");
731 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
733 spin_unlock(&dev->irqlock);
737 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
738 ctx->int_type = reason;
741 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
742 mfc_err("Failed to unlock hw\n");
746 wake_up(&ctx->queue);
748 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
749 spin_unlock(&dev->irqlock);
750 mfc_debug(2, "Exit via irq_cleanup_hw\n");
754 /* Open an MFC node */
755 static int s5p_mfc_open(struct file *file)
757 struct video_device *vdev = video_devdata(file);
758 struct s5p_mfc_dev *dev = video_drvdata(file);
759 struct s5p_mfc_ctx *ctx = NULL;
764 if (mutex_lock_interruptible(&dev->mfc_mutex))
766 dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
767 /* Allocate memory for context */
768 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
773 init_waitqueue_head(&ctx->queue);
774 v4l2_fh_init(&ctx->fh, vdev);
775 file->private_data = &ctx->fh;
776 v4l2_fh_add(&ctx->fh);
778 INIT_LIST_HEAD(&ctx->src_queue);
779 INIT_LIST_HEAD(&ctx->dst_queue);
780 ctx->src_queue_cnt = 0;
781 ctx->dst_queue_cnt = 0;
782 /* Get context number */
784 while (dev->ctx[ctx->num]) {
786 if (ctx->num >= MFC_NUM_CONTEXTS) {
787 mfc_debug(2, "Too many open contexts\n");
792 /* Mark context as idle */
793 clear_work_bit_irqsave(ctx);
794 dev->ctx[ctx->num] = ctx;
795 if (vdev == dev->vfd_dec) {
796 ctx->type = MFCINST_DECODER;
797 ctx->c_ops = get_dec_codec_ops();
798 s5p_mfc_dec_init(ctx);
799 /* Setup ctrl handler */
800 ret = s5p_mfc_dec_ctrls_setup(ctx);
802 mfc_err("Failed to setup mfc controls\n");
803 goto err_ctrls_setup;
805 } else if (vdev == dev->vfd_enc) {
806 ctx->type = MFCINST_ENCODER;
807 ctx->c_ops = get_enc_codec_ops();
808 /* only for encoder */
809 INIT_LIST_HEAD(&ctx->ref_queue);
810 ctx->ref_queue_cnt = 0;
811 s5p_mfc_enc_init(ctx);
812 /* Setup ctrl handler */
813 ret = s5p_mfc_enc_ctrls_setup(ctx);
815 mfc_err("Failed to setup mfc controls\n");
816 goto err_ctrls_setup;
822 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
823 ctx->inst_no = MFC_NO_INSTANCE_SET;
824 /* Load firmware if this is the first instance */
825 if (dev->num_inst == 1) {
826 dev->watchdog_timer.expires = jiffies +
827 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
828 add_timer(&dev->watchdog_timer);
829 ret = s5p_mfc_power_on();
831 mfc_err("power on failed\n");
835 ret = s5p_mfc_load_firmware(dev);
841 ret = s5p_mfc_init_hw(dev);
846 /* Init videobuf2 queue for CAPTURE */
848 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
849 q->drv_priv = &ctx->fh;
850 q->lock = &dev->mfc_mutex;
851 if (vdev == dev->vfd_dec) {
852 q->io_modes = VB2_MMAP;
853 q->ops = get_dec_queue_ops();
854 } else if (vdev == dev->vfd_enc) {
855 q->io_modes = VB2_MMAP | VB2_USERPTR;
856 q->ops = get_enc_queue_ops();
862 * We'll do mostly sequential access, so sacrifice TLB efficiency for
865 q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
866 q->mem_ops = &vb2_dma_contig_memops;
867 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
868 ret = vb2_queue_init(q);
870 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
873 /* Init videobuf2 queue for OUTPUT */
875 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
876 q->drv_priv = &ctx->fh;
877 q->lock = &dev->mfc_mutex;
878 if (vdev == dev->vfd_dec) {
879 q->io_modes = VB2_MMAP;
880 q->ops = get_dec_queue_ops();
881 } else if (vdev == dev->vfd_enc) {
882 q->io_modes = VB2_MMAP | VB2_USERPTR;
883 q->ops = get_enc_queue_ops();
888 /* One way to indicate end-of-stream for MFC is to set the
889 * bytesused == 0. However by default videobuf2 handles bytesused
890 * equal to 0 as a special case and changes its value to the size
891 * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
892 * will keep the value of bytesused intact.
894 q->allow_zero_bytesused = 1;
897 * We'll do mostly sequential access, so sacrifice TLB efficiency for
900 q->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES;
901 q->mem_ops = &vb2_dma_contig_memops;
902 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
903 ret = vb2_queue_init(q);
905 mfc_err("Failed to initialize videobuf2 queue(output)\n");
908 mutex_unlock(&dev->mfc_mutex);
911 /* Deinit when failure occurred */
913 if (dev->num_inst == 1)
914 s5p_mfc_deinit_hw(dev);
918 if (dev->num_inst == 1) {
919 if (s5p_mfc_power_off() < 0)
920 mfc_err("power off failed\n");
921 del_timer_sync(&dev->watchdog_timer);
924 s5p_mfc_dec_ctrls_delete(ctx);
926 dev->ctx[ctx->num] = NULL;
928 v4l2_fh_del(&ctx->fh);
929 v4l2_fh_exit(&ctx->fh);
933 mutex_unlock(&dev->mfc_mutex);
938 /* Release MFC context */
939 static int s5p_mfc_release(struct file *file)
941 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
942 struct s5p_mfc_dev *dev = ctx->dev;
944 /* if dev is null, do cleanup that doesn't need dev */
947 mutex_lock(&dev->mfc_mutex);
948 vb2_queue_release(&ctx->vq_src);
949 vb2_queue_release(&ctx->vq_dst);
953 /* Mark context as idle */
954 clear_work_bit_irqsave(ctx);
956 * If instance was initialised and not yet freed,
957 * return instance and free resources
959 if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
960 mfc_debug(2, "Has to free instance\n");
961 s5p_mfc_close_mfc_inst(dev, ctx);
963 /* hardware locking scheme */
964 if (dev->curr_ctx == ctx->num)
965 clear_bit(0, &dev->hw_lock);
967 if (dev->num_inst == 0) {
968 mfc_debug(2, "Last instance\n");
969 s5p_mfc_deinit_hw(dev);
970 del_timer_sync(&dev->watchdog_timer);
972 if (s5p_mfc_power_off() < 0)
973 mfc_err("Power off failed\n");
975 mfc_debug(2, "Shutting down clock\n");
980 dev->ctx[ctx->num] = NULL;
981 s5p_mfc_dec_ctrls_delete(ctx);
982 v4l2_fh_del(&ctx->fh);
983 /* vdev is gone if dev is null */
985 v4l2_fh_exit(&ctx->fh);
989 mutex_unlock(&dev->mfc_mutex);
995 static __poll_t s5p_mfc_poll(struct file *file,
996 struct poll_table_struct *wait)
998 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
999 struct s5p_mfc_dev *dev = ctx->dev;
1000 struct vb2_queue *src_q, *dst_q;
1001 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
1003 unsigned long flags;
1005 mutex_lock(&dev->mfc_mutex);
1006 src_q = &ctx->vq_src;
1007 dst_q = &ctx->vq_dst;
1009 * There has to be at least one buffer queued on each queued_list, which
1010 * means either in driver already or waiting for driver to claim it
1011 * and start processing.
1013 if ((!src_q->streaming || list_empty(&src_q->queued_list))
1014 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
1018 mutex_unlock(&dev->mfc_mutex);
1019 poll_wait(file, &ctx->fh.wait, wait);
1020 poll_wait(file, &src_q->done_wq, wait);
1021 poll_wait(file, &dst_q->done_wq, wait);
1022 mutex_lock(&dev->mfc_mutex);
1023 if (v4l2_event_pending(&ctx->fh))
1025 spin_lock_irqsave(&src_q->done_lock, flags);
1026 if (!list_empty(&src_q->done_list))
1027 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
1029 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
1030 || src_vb->state == VB2_BUF_STATE_ERROR))
1031 rc |= EPOLLOUT | EPOLLWRNORM;
1032 spin_unlock_irqrestore(&src_q->done_lock, flags);
1033 spin_lock_irqsave(&dst_q->done_lock, flags);
1034 if (!list_empty(&dst_q->done_list))
1035 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
1037 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
1038 || dst_vb->state == VB2_BUF_STATE_ERROR))
1039 rc |= EPOLLIN | EPOLLRDNORM;
1040 spin_unlock_irqrestore(&dst_q->done_lock, flags);
1042 mutex_unlock(&dev->mfc_mutex);
1047 static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
1049 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
1050 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1053 if (offset < DST_QUEUE_OFF_BASE) {
1054 mfc_debug(2, "mmaping source\n");
1055 ret = vb2_mmap(&ctx->vq_src, vma);
1056 } else { /* capture */
1057 mfc_debug(2, "mmaping destination\n");
1058 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
1059 ret = vb2_mmap(&ctx->vq_dst, vma);
1065 static const struct v4l2_file_operations s5p_mfc_fops = {
1066 .owner = THIS_MODULE,
1067 .open = s5p_mfc_open,
1068 .release = s5p_mfc_release,
1069 .poll = s5p_mfc_poll,
1070 .unlocked_ioctl = video_ioctl2,
1071 .mmap = s5p_mfc_mmap,
1074 /* DMA memory related helper functions */
1075 static void s5p_mfc_memdev_release(struct device *dev)
1077 of_reserved_mem_device_release(dev);
1080 static struct device *s5p_mfc_alloc_memdev(struct device *dev,
1081 const char *name, unsigned int idx)
1083 struct device *child;
1086 child = devm_kzalloc(dev, sizeof(*child), GFP_KERNEL);
1090 device_initialize(child);
1091 dev_set_name(child, "%s:%s", dev_name(dev), name);
1092 child->parent = dev;
1093 child->coherent_dma_mask = dev->coherent_dma_mask;
1094 child->dma_mask = dev->dma_mask;
1095 child->release = s5p_mfc_memdev_release;
1097 if (device_add(child) == 0) {
1098 ret = of_reserved_mem_device_init_by_idx(child, dev->of_node,
1109 static int s5p_mfc_configure_2port_memory(struct s5p_mfc_dev *mfc_dev)
1111 struct device *dev = &mfc_dev->plat_dev->dev;
1113 dma_addr_t bank2_dma_addr;
1114 unsigned long align_size = 1 << MFC_BASE_ALIGN_ORDER;
1118 * Create and initialize virtual devices for accessing
1119 * reserved memory regions.
1121 mfc_dev->mem_dev[BANK_L_CTX] = s5p_mfc_alloc_memdev(dev, "left",
1123 if (!mfc_dev->mem_dev[BANK_L_CTX])
1125 mfc_dev->mem_dev[BANK_R_CTX] = s5p_mfc_alloc_memdev(dev, "right",
1127 if (!mfc_dev->mem_dev[BANK_R_CTX]) {
1128 device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
1132 /* Allocate memory for firmware and initialize both banks addresses */
1133 ret = s5p_mfc_alloc_firmware(mfc_dev);
1135 device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
1136 device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
1140 mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->fw_buf.dma;
1142 bank2_virt = dma_alloc_coherent(mfc_dev->mem_dev[BANK_R_CTX],
1143 align_size, &bank2_dma_addr, GFP_KERNEL);
1145 mfc_err("Allocating bank2 base failed\n");
1146 s5p_mfc_release_firmware(mfc_dev);
1147 device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
1148 device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
1152 /* Valid buffers passed to MFC encoder with LAST_FRAME command
1153 * should not have address of bank2 - MFC will treat it as a null frame.
1154 * To avoid such situation we set bank2 address below the pool address.
1156 mfc_dev->dma_base[BANK_R_CTX] = bank2_dma_addr - align_size;
1158 dma_free_coherent(mfc_dev->mem_dev[BANK_R_CTX], align_size, bank2_virt,
1161 vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX],
1163 vb2_dma_contig_set_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX],
1169 static void s5p_mfc_unconfigure_2port_memory(struct s5p_mfc_dev *mfc_dev)
1171 device_unregister(mfc_dev->mem_dev[BANK_L_CTX]);
1172 device_unregister(mfc_dev->mem_dev[BANK_R_CTX]);
1173 vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_L_CTX]);
1174 vb2_dma_contig_clear_max_seg_size(mfc_dev->mem_dev[BANK_R_CTX]);
1177 static int s5p_mfc_configure_common_memory(struct s5p_mfc_dev *mfc_dev)
1179 struct device *dev = &mfc_dev->plat_dev->dev;
1180 unsigned long mem_size = SZ_4M;
1181 unsigned int bitmap_size;
1183 if (IS_ENABLED(CONFIG_DMA_CMA) || exynos_is_iommu_available(dev))
1187 mem_size = memparse(mfc_mem_size, NULL);
1189 bitmap_size = BITS_TO_LONGS(mem_size >> PAGE_SHIFT) * sizeof(long);
1191 mfc_dev->mem_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1192 if (!mfc_dev->mem_bitmap)
1195 mfc_dev->mem_virt = dma_alloc_coherent(dev, mem_size,
1196 &mfc_dev->mem_base, GFP_KERNEL);
1197 if (!mfc_dev->mem_virt) {
1198 kfree(mfc_dev->mem_bitmap);
1199 dev_err(dev, "failed to preallocate %ld MiB for the firmware and context buffers\n",
1200 (mem_size / SZ_1M));
1203 mfc_dev->mem_size = mem_size;
1204 mfc_dev->dma_base[BANK_L_CTX] = mfc_dev->mem_base;
1205 mfc_dev->dma_base[BANK_R_CTX] = mfc_dev->mem_base;
1208 * MFC hardware cannot handle 0 as a base address, so mark first 128K
1209 * as used (to keep required base alignment) and adjust base address
1211 if (mfc_dev->mem_base == (dma_addr_t)0) {
1212 unsigned int offset = 1 << MFC_BASE_ALIGN_ORDER;
1214 bitmap_set(mfc_dev->mem_bitmap, 0, offset >> PAGE_SHIFT);
1215 mfc_dev->dma_base[BANK_L_CTX] += offset;
1216 mfc_dev->dma_base[BANK_R_CTX] += offset;
1219 /* Firmware allocation cannot fail in this case */
1220 s5p_mfc_alloc_firmware(mfc_dev);
1222 mfc_dev->mem_dev[BANK_L_CTX] = mfc_dev->mem_dev[BANK_R_CTX] = dev;
1223 vb2_dma_contig_set_max_seg_size(dev, DMA_BIT_MASK(32));
1225 dev_info(dev, "preallocated %ld MiB buffer for the firmware and context buffers\n",
1226 (mem_size / SZ_1M));
1231 static void s5p_mfc_unconfigure_common_memory(struct s5p_mfc_dev *mfc_dev)
1233 struct device *dev = &mfc_dev->plat_dev->dev;
1235 dma_free_coherent(dev, mfc_dev->mem_size, mfc_dev->mem_virt,
1237 kfree(mfc_dev->mem_bitmap);
1238 vb2_dma_contig_clear_max_seg_size(dev);
1241 static int s5p_mfc_configure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1243 struct device *dev = &mfc_dev->plat_dev->dev;
1245 if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
1246 return s5p_mfc_configure_common_memory(mfc_dev);
1248 return s5p_mfc_configure_2port_memory(mfc_dev);
1251 static void s5p_mfc_unconfigure_dma_memory(struct s5p_mfc_dev *mfc_dev)
1253 struct device *dev = &mfc_dev->plat_dev->dev;
1255 s5p_mfc_release_firmware(mfc_dev);
1256 if (exynos_is_iommu_available(dev) || !IS_TWOPORT(mfc_dev))
1257 s5p_mfc_unconfigure_common_memory(mfc_dev);
1259 s5p_mfc_unconfigure_2port_memory(mfc_dev);
1262 /* MFC probe function */
1263 static int s5p_mfc_probe(struct platform_device *pdev)
1265 struct s5p_mfc_dev *dev;
1266 struct video_device *vfd;
1267 struct resource *res;
1270 pr_debug("%s++\n", __func__);
1271 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1275 spin_lock_init(&dev->irqlock);
1276 spin_lock_init(&dev->condlock);
1277 dev->plat_dev = pdev;
1278 if (!dev->plat_dev) {
1279 mfc_err("No platform data specified\n");
1283 dev->variant = of_device_get_match_data(&pdev->dev);
1284 if (!dev->variant) {
1285 dev_err(&pdev->dev, "Failed to get device MFC hardware variant information\n");
1289 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1290 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1291 if (IS_ERR(dev->regs_base))
1292 return PTR_ERR(dev->regs_base);
1294 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1296 dev_err(&pdev->dev, "failed to get irq resource\n");
1299 dev->irq = res->start;
1300 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1301 0, pdev->name, dev);
1303 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1307 ret = s5p_mfc_configure_dma_memory(dev);
1309 dev_err(&pdev->dev, "failed to configure DMA memory\n");
1313 ret = s5p_mfc_init_pm(dev);
1315 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1320 * Load fails if fs isn't mounted. Try loading anyway.
1321 * _open() will load it, it it fails now. Ignore failure.
1323 s5p_mfc_load_firmware(dev);
1325 mutex_init(&dev->mfc_mutex);
1326 init_waitqueue_head(&dev->queue);
1328 INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1329 atomic_set(&dev->watchdog_cnt, 0);
1330 timer_setup(&dev->watchdog_timer, s5p_mfc_watchdog, 0);
1332 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1334 goto err_v4l2_dev_reg;
1337 vfd = video_device_alloc();
1339 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1343 vfd->fops = &s5p_mfc_fops;
1344 vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
1345 vfd->release = video_device_release;
1346 vfd->lock = &dev->mfc_mutex;
1347 vfd->v4l2_dev = &dev->v4l2_dev;
1348 vfd->vfl_dir = VFL_DIR_M2M;
1349 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1351 video_set_drvdata(vfd, dev);
1354 vfd = video_device_alloc();
1356 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1360 vfd->fops = &s5p_mfc_fops;
1361 vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
1362 vfd->release = video_device_release;
1363 vfd->lock = &dev->mfc_mutex;
1364 vfd->v4l2_dev = &dev->v4l2_dev;
1365 vfd->vfl_dir = VFL_DIR_M2M;
1366 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1368 video_set_drvdata(vfd, dev);
1369 platform_set_drvdata(pdev, dev);
1371 /* Initialize HW ops and commands based on MFC version */
1372 s5p_mfc_init_hw_ops(dev);
1373 s5p_mfc_init_hw_cmds(dev);
1374 s5p_mfc_init_regs(dev);
1376 /* Register decoder and encoder */
1377 ret = video_register_device(dev->vfd_dec, VFL_TYPE_GRABBER, 0);
1379 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1382 v4l2_info(&dev->v4l2_dev,
1383 "decoder registered as /dev/video%d\n", dev->vfd_dec->num);
1385 ret = video_register_device(dev->vfd_enc, VFL_TYPE_GRABBER, 0);
1387 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1390 v4l2_info(&dev->v4l2_dev,
1391 "encoder registered as /dev/video%d\n", dev->vfd_enc->num);
1393 pr_debug("%s--\n", __func__);
1396 /* Deinit MFC if probe had failed */
1398 video_unregister_device(dev->vfd_dec);
1400 video_device_release(dev->vfd_enc);
1402 video_device_release(dev->vfd_dec);
1404 v4l2_device_unregister(&dev->v4l2_dev);
1406 s5p_mfc_final_pm(dev);
1408 s5p_mfc_unconfigure_dma_memory(dev);
1410 pr_debug("%s-- with error\n", __func__);
1415 /* Remove the driver */
1416 static int s5p_mfc_remove(struct platform_device *pdev)
1418 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1419 struct s5p_mfc_ctx *ctx;
1422 v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1425 * Clear ctx dev pointer to avoid races between s5p_mfc_remove()
1426 * and s5p_mfc_release() and s5p_mfc_release() accessing ctx->dev
1427 * after s5p_mfc_remove() is run during unbind.
1429 mutex_lock(&dev->mfc_mutex);
1430 for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
1434 /* clear ctx->dev */
1437 mutex_unlock(&dev->mfc_mutex);
1439 del_timer_sync(&dev->watchdog_timer);
1440 flush_work(&dev->watchdog_work);
1442 video_unregister_device(dev->vfd_enc);
1443 video_unregister_device(dev->vfd_dec);
1444 video_device_release(dev->vfd_enc);
1445 video_device_release(dev->vfd_dec);
1446 v4l2_device_unregister(&dev->v4l2_dev);
1447 s5p_mfc_unconfigure_dma_memory(dev);
1449 s5p_mfc_final_pm(dev);
1453 #ifdef CONFIG_PM_SLEEP
1455 static int s5p_mfc_suspend(struct device *dev)
1457 struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
1460 if (m_dev->num_inst == 0)
1463 if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1464 mfc_err("Error: going to suspend for a second time\n");
1468 /* Check if we're processing then wait if it necessary. */
1469 while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1470 /* Try and lock the HW */
1471 /* Wait on the interrupt waitqueue */
1472 ret = wait_event_interruptible_timeout(m_dev->queue,
1473 m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
1475 mfc_err("Waiting for hardware to finish timed out\n");
1476 clear_bit(0, &m_dev->enter_suspend);
1481 ret = s5p_mfc_sleep(m_dev);
1483 clear_bit(0, &m_dev->enter_suspend);
1484 clear_bit(0, &m_dev->hw_lock);
1489 static int s5p_mfc_resume(struct device *dev)
1491 struct s5p_mfc_dev *m_dev = dev_get_drvdata(dev);
1493 if (m_dev->num_inst == 0)
1495 return s5p_mfc_wakeup(m_dev);
1499 /* Power management */
1500 static const struct dev_pm_ops s5p_mfc_pm_ops = {
1501 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1504 static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1505 .h264_ctx = MFC_H264_CTX_BUF_SIZE,
1506 .non_h264_ctx = MFC_CTX_BUF_SIZE,
1507 .dsc = DESC_BUF_SIZE,
1508 .shm = SHARED_BUF_SIZE,
1511 static struct s5p_mfc_buf_size buf_size_v5 = {
1513 .cpb = MAX_CPB_SIZE,
1514 .priv = &mfc_buf_size_v5,
1517 static struct s5p_mfc_variant mfc_drvdata_v5 = {
1518 .version = MFC_VERSION,
1519 .version_bit = MFC_V5_BIT,
1520 .port_num = MFC_NUM_PORTS,
1521 .buf_size = &buf_size_v5,
1522 .fw_name[0] = "/*(DEBLOBBED)*/",
1523 .clk_names = {"mfc", "sclk_mfc"},
1525 .use_clock_gating = true,
1528 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1529 .dev_ctx = MFC_CTX_BUF_SIZE_V6,
1530 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1531 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1532 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1533 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1536 static struct s5p_mfc_buf_size buf_size_v6 = {
1537 .fw = MAX_FW_SIZE_V6,
1538 .cpb = MAX_CPB_SIZE_V6,
1539 .priv = &mfc_buf_size_v6,
1542 static struct s5p_mfc_variant mfc_drvdata_v6 = {
1543 .version = MFC_VERSION_V6,
1544 .version_bit = MFC_V6_BIT,
1545 .port_num = MFC_NUM_PORTS_V6,
1546 .buf_size = &buf_size_v6,
1547 .fw_name[0] = "/*(DEBLOBBED)*/",
1549 * v6-v2 firmware contains bug fixes and interface change
1550 * for init buffer command
1552 .fw_name[1] = "/*(DEBLOBBED)*/",
1553 .clk_names = {"mfc"},
1557 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
1558 .dev_ctx = MFC_CTX_BUF_SIZE_V7,
1559 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
1560 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
1561 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
1562 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
1565 static struct s5p_mfc_buf_size buf_size_v7 = {
1566 .fw = MAX_FW_SIZE_V7,
1567 .cpb = MAX_CPB_SIZE_V7,
1568 .priv = &mfc_buf_size_v7,
1571 static struct s5p_mfc_variant mfc_drvdata_v7 = {
1572 .version = MFC_VERSION_V7,
1573 .version_bit = MFC_V7_BIT,
1574 .port_num = MFC_NUM_PORTS_V7,
1575 .buf_size = &buf_size_v7,
1576 .fw_name[0] = "/*(DEBLOBBED)*/",
1577 .clk_names = {"mfc"},
1581 static struct s5p_mfc_variant mfc_drvdata_v7_3250 = {
1582 .version = MFC_VERSION_V7,
1583 .version_bit = MFC_V7_BIT,
1584 .port_num = MFC_NUM_PORTS_V7,
1585 .buf_size = &buf_size_v7,
1586 .fw_name[0] = "/*(DEBLOBBED)*/",
1587 .clk_names = {"mfc", "sclk_mfc"},
1591 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
1592 .dev_ctx = MFC_CTX_BUF_SIZE_V8,
1593 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
1594 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
1595 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
1596 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
1599 static struct s5p_mfc_buf_size buf_size_v8 = {
1600 .fw = MAX_FW_SIZE_V8,
1601 .cpb = MAX_CPB_SIZE_V8,
1602 .priv = &mfc_buf_size_v8,
1605 static struct s5p_mfc_variant mfc_drvdata_v8 = {
1606 .version = MFC_VERSION_V8,
1607 .version_bit = MFC_V8_BIT,
1608 .port_num = MFC_NUM_PORTS_V8,
1609 .buf_size = &buf_size_v8,
1610 .fw_name[0] = "/*(DEBLOBBED)*/",
1611 .clk_names = {"mfc"},
1615 static struct s5p_mfc_variant mfc_drvdata_v8_5433 = {
1616 .version = MFC_VERSION_V8,
1617 .version_bit = MFC_V8_BIT,
1618 .port_num = MFC_NUM_PORTS_V8,
1619 .buf_size = &buf_size_v8,
1620 .fw_name[0] = "/*(DEBLOBBED)*/",
1621 .clk_names = {"pclk", "aclk", "aclk_xiu"},
1625 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v10 = {
1626 .dev_ctx = MFC_CTX_BUF_SIZE_V10,
1627 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V10,
1628 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V10,
1629 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V10,
1630 .hevc_enc_ctx = MFC_HEVC_ENC_CTX_BUF_SIZE_V10,
1631 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V10,
1634 static struct s5p_mfc_buf_size buf_size_v10 = {
1635 .fw = MAX_FW_SIZE_V10,
1636 .cpb = MAX_CPB_SIZE_V10,
1637 .priv = &mfc_buf_size_v10,
1640 static struct s5p_mfc_variant mfc_drvdata_v10 = {
1641 .version = MFC_VERSION_V10,
1642 .version_bit = MFC_V10_BIT,
1643 .port_num = MFC_NUM_PORTS_V10,
1644 .buf_size = &buf_size_v10,
1645 .fw_name[0] = "/*(DEBLOBBED)*/",
1648 static const struct of_device_id exynos_mfc_match[] = {
1650 .compatible = "samsung,mfc-v5",
1651 .data = &mfc_drvdata_v5,
1653 .compatible = "samsung,mfc-v6",
1654 .data = &mfc_drvdata_v6,
1656 .compatible = "samsung,mfc-v7",
1657 .data = &mfc_drvdata_v7,
1659 .compatible = "samsung,exynos3250-mfc",
1660 .data = &mfc_drvdata_v7_3250,
1662 .compatible = "samsung,mfc-v8",
1663 .data = &mfc_drvdata_v8,
1665 .compatible = "samsung,exynos5433-mfc",
1666 .data = &mfc_drvdata_v8_5433,
1668 .compatible = "samsung,mfc-v10",
1669 .data = &mfc_drvdata_v10,
1673 MODULE_DEVICE_TABLE(of, exynos_mfc_match);
1675 static struct platform_driver s5p_mfc_driver = {
1676 .probe = s5p_mfc_probe,
1677 .remove = s5p_mfc_remove,
1679 .name = S5P_MFC_NAME,
1680 .pm = &s5p_mfc_pm_ops,
1681 .of_match_table = exynos_mfc_match,
1685 module_platform_driver(s5p_mfc_driver);
1687 MODULE_LICENSE("GPL");
1688 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1689 MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");