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 <media/videobuf2-v4l2.h>
26 #include "s5p_mfc_common.h"
27 #include "s5p_mfc_ctrl.h"
28 #include "s5p_mfc_debug.h"
29 #include "s5p_mfc_dec.h"
30 #include "s5p_mfc_enc.h"
31 #include "s5p_mfc_intr.h"
32 #include "s5p_mfc_opr.h"
33 #include "s5p_mfc_cmd.h"
34 #include "s5p_mfc_pm.h"
36 #define S5P_MFC_NAME "s5p-mfc"
37 #define S5P_MFC_DEC_NAME "s5p-mfc-dec"
38 #define S5P_MFC_ENC_NAME "s5p-mfc-enc"
41 module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
42 MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
44 /* Helper functions for interrupt processing */
46 /* Remove from hw execution round robin */
47 void clear_work_bit(struct s5p_mfc_ctx *ctx)
49 struct s5p_mfc_dev *dev = ctx->dev;
51 spin_lock(&dev->condlock);
52 __clear_bit(ctx->num, &dev->ctx_work_bits);
53 spin_unlock(&dev->condlock);
56 /* Add to hw execution round robin */
57 void set_work_bit(struct s5p_mfc_ctx *ctx)
59 struct s5p_mfc_dev *dev = ctx->dev;
61 spin_lock(&dev->condlock);
62 __set_bit(ctx->num, &dev->ctx_work_bits);
63 spin_unlock(&dev->condlock);
66 /* Remove from hw execution round robin */
67 void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
69 struct s5p_mfc_dev *dev = ctx->dev;
72 spin_lock_irqsave(&dev->condlock, flags);
73 __clear_bit(ctx->num, &dev->ctx_work_bits);
74 spin_unlock_irqrestore(&dev->condlock, flags);
77 /* Add to hw execution round robin */
78 void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
80 struct s5p_mfc_dev *dev = ctx->dev;
83 spin_lock_irqsave(&dev->condlock, flags);
84 __set_bit(ctx->num, &dev->ctx_work_bits);
85 spin_unlock_irqrestore(&dev->condlock, flags);
88 /* Wake up context wait_queue */
89 static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
93 ctx->int_type = reason;
98 /* Wake up device wait_queue */
99 static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
103 dev->int_type = reason;
105 wake_up(&dev->queue);
108 static void s5p_mfc_watchdog(unsigned long arg)
110 struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
112 if (test_bit(0, &dev->hw_lock))
113 atomic_inc(&dev->watchdog_cnt);
114 if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
115 /* This means that hw is busy and no interrupts were
116 * generated by hw for the Nth time of running this
117 * watchdog timer. This usually means a serious hw
118 * error. Now it is time to kill all instances and
120 mfc_err("Time out during waiting for HW\n");
121 queue_work(dev->watchdog_workqueue, &dev->watchdog_work);
123 dev->watchdog_timer.expires = jiffies +
124 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
125 add_timer(&dev->watchdog_timer);
128 static void s5p_mfc_watchdog_worker(struct work_struct *work)
130 struct s5p_mfc_dev *dev;
131 struct s5p_mfc_ctx *ctx;
136 dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
138 mfc_err("Driver timeout error handling\n");
139 /* Lock the mutex that protects open and release.
140 * This is necessary as they may load and unload firmware. */
141 mutex_locked = mutex_trylock(&dev->mfc_mutex);
143 mfc_err("Error: some instance may be closing/opening\n");
144 spin_lock_irqsave(&dev->irqlock, flags);
148 for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
152 ctx->state = MFCINST_ERROR;
153 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
154 &ctx->dst_queue, &ctx->vq_dst);
155 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
156 &ctx->src_queue, &ctx->vq_src);
158 wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
160 clear_bit(0, &dev->hw_lock);
161 spin_unlock_irqrestore(&dev->irqlock, flags);
164 s5p_mfc_deinit_hw(dev);
166 /* Double check if there is at least one instance running.
167 * If no instance is in memory than no firmware should be present */
168 if (dev->num_inst > 0) {
169 ret = s5p_mfc_load_firmware(dev);
171 mfc_err("Failed to reload FW\n");
175 ret = s5p_mfc_init_hw(dev);
178 mfc_err("Failed to reinit FW\n");
182 mutex_unlock(&dev->mfc_mutex);
185 static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
187 struct s5p_mfc_buf *dst_buf;
188 struct s5p_mfc_dev *dev = ctx->dev;
190 ctx->state = MFCINST_FINISHED;
192 while (!list_empty(&ctx->dst_queue)) {
193 dst_buf = list_entry(ctx->dst_queue.next,
194 struct s5p_mfc_buf, list);
195 mfc_debug(2, "Cleaning up buffer: %d\n",
196 dst_buf->b->vb2_buf.index);
197 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0, 0);
198 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1, 0);
199 list_del(&dst_buf->list);
200 dst_buf->flags |= MFC_BUF_FLAG_EOS;
201 ctx->dst_queue_cnt--;
202 dst_buf->b->sequence = (ctx->sequence++);
204 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
205 s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
206 dst_buf->b->field = V4L2_FIELD_NONE;
208 dst_buf->b->field = V4L2_FIELD_INTERLACED;
209 dst_buf->b->flags |= V4L2_BUF_FLAG_LAST;
211 ctx->dec_dst_flag &= ~(1 << dst_buf->b->vb2_buf.index);
212 vb2_buffer_done(&dst_buf->b->vb2_buf, VB2_BUF_STATE_DONE);
216 static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
218 struct s5p_mfc_dev *dev = ctx->dev;
219 struct s5p_mfc_buf *dst_buf, *src_buf;
221 unsigned int frame_type;
223 /* Make sure we actually have a new frame before continuing. */
224 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
225 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
227 dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
229 /* Copy timestamp / timecode from decoded src to dst and set
230 appropriate flags. */
231 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
232 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
233 if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
235 dst_buf->b->timecode =
236 src_buf->b->timecode;
237 dst_buf->b->timestamp =
238 src_buf->b->timestamp;
240 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
243 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
244 switch (frame_type) {
245 case S5P_FIMV_DECODE_FRAME_I_FRAME:
247 V4L2_BUF_FLAG_KEYFRAME;
249 case S5P_FIMV_DECODE_FRAME_P_FRAME:
251 V4L2_BUF_FLAG_PFRAME;
253 case S5P_FIMV_DECODE_FRAME_B_FRAME:
255 V4L2_BUF_FLAG_BFRAME;
258 /* Don't know how to handle
259 S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
260 mfc_debug(2, "Unexpected frame type: %d\n",
268 static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
270 struct s5p_mfc_dev *dev = ctx->dev;
271 struct s5p_mfc_buf *dst_buf;
273 unsigned int frame_type;
275 dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
276 if (IS_MFCV6_PLUS(dev))
277 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
278 get_disp_frame_type, ctx);
280 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
281 get_dec_frame_type, dev);
283 /* If frame is same as previous then skip and do not dequeue */
284 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
285 if (!ctx->after_packed_pb)
287 ctx->after_packed_pb = 0;
291 /* The MFC returns address of the buffer, now we have to
292 * check which videobuf does it correspond to */
293 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
294 /* Check if this is the buffer we're looking for */
295 if (vb2_dma_contig_plane_dma_addr(&dst_buf->b->vb2_buf, 0)
297 list_del(&dst_buf->list);
298 ctx->dst_queue_cnt--;
299 dst_buf->b->sequence = ctx->sequence;
300 if (s5p_mfc_hw_call(dev->mfc_ops,
301 get_pic_type_top, ctx) ==
302 s5p_mfc_hw_call(dev->mfc_ops,
303 get_pic_type_bot, ctx))
304 dst_buf->b->field = V4L2_FIELD_NONE;
307 V4L2_FIELD_INTERLACED;
308 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 0,
310 vb2_set_plane_payload(&dst_buf->b->vb2_buf, 1,
312 clear_bit(dst_buf->b->vb2_buf.index,
315 vb2_buffer_done(&dst_buf->b->vb2_buf, err ?
316 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
323 /* Handle frame decoding interrupt */
324 static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
325 unsigned int reason, unsigned int err)
327 struct s5p_mfc_dev *dev = ctx->dev;
328 unsigned int dst_frame_status;
329 unsigned int dec_frame_status;
330 struct s5p_mfc_buf *src_buf;
332 unsigned int res_change;
334 dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
335 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
336 dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
337 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
338 res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
339 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
340 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
341 mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
342 if (ctx->state == MFCINST_RES_CHANGE_INIT)
343 ctx->state = MFCINST_RES_CHANGE_FLUSH;
344 if (res_change == S5P_FIMV_RES_INCREASE ||
345 res_change == S5P_FIMV_RES_DECREASE) {
346 ctx->state = MFCINST_RES_CHANGE_INIT;
347 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
348 wake_up_ctx(ctx, reason, err);
349 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
351 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
354 if (ctx->dpb_flush_flag)
355 ctx->dpb_flush_flag = 0;
357 spin_lock_irqsave(&dev->irqlock, flags);
358 /* All frames remaining in the buffer have been extracted */
359 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
360 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
361 static const struct v4l2_event ev_src_ch = {
362 .type = V4L2_EVENT_SOURCE_CHANGE,
363 .u.src_change.changes =
364 V4L2_EVENT_SRC_CH_RESOLUTION,
367 s5p_mfc_handle_frame_all_extracted(ctx);
368 ctx->state = MFCINST_RES_CHANGE_END;
369 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
371 goto leave_handle_frame;
373 s5p_mfc_handle_frame_all_extracted(ctx);
377 if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
378 s5p_mfc_handle_frame_copy_time(ctx);
380 /* A frame has been decoded and is in the buffer */
381 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
382 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
383 s5p_mfc_handle_frame_new(ctx, err);
385 mfc_debug(2, "No frame decode\n");
387 /* Mark source buffer as complete */
388 if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
389 && !list_empty(&ctx->src_queue)) {
390 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
392 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
393 get_consumed_stream, dev);
394 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
395 ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
396 ctx->consumed_stream + STUFF_BYTE <
397 src_buf->b->vb2_buf.planes[0].bytesused) {
398 /* Run MFC again on the same buffer */
399 mfc_debug(2, "Running again the same buffer\n");
400 ctx->after_packed_pb = 1;
402 mfc_debug(2, "MFC needs next buffer\n");
403 ctx->consumed_stream = 0;
404 if (src_buf->flags & MFC_BUF_FLAG_EOS)
405 ctx->state = MFCINST_FINISHING;
406 list_del(&src_buf->list);
407 ctx->src_queue_cnt--;
408 if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
409 vb2_buffer_done(&src_buf->b->vb2_buf,
410 VB2_BUF_STATE_ERROR);
412 vb2_buffer_done(&src_buf->b->vb2_buf,
417 spin_unlock_irqrestore(&dev->irqlock, flags);
418 if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
419 || ctx->dst_queue_cnt < ctx->pb_count)
421 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
422 wake_up_ctx(ctx, reason, err);
423 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
425 /* if suspending, wake up device and do not try_run again*/
426 if (test_bit(0, &dev->enter_suspend))
427 wake_up_dev(dev, reason, err);
429 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
432 /* Error handling for interrupt */
433 static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
434 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
438 mfc_err("Interrupt Error: %08x\n", err);
441 /* Error recovery is dependent on the state of context */
442 switch (ctx->state) {
443 case MFCINST_RES_CHANGE_INIT:
444 case MFCINST_RES_CHANGE_FLUSH:
445 case MFCINST_RES_CHANGE_END:
446 case MFCINST_FINISHING:
447 case MFCINST_FINISHED:
448 case MFCINST_RUNNING:
449 /* It is highly probable that an error occurred
450 * while decoding a frame */
452 ctx->state = MFCINST_ERROR;
453 /* Mark all dst buffers as having an error */
454 spin_lock_irqsave(&dev->irqlock, flags);
455 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
456 &ctx->dst_queue, &ctx->vq_dst);
457 /* Mark all src buffers as having an error */
458 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
459 &ctx->src_queue, &ctx->vq_src);
460 spin_unlock_irqrestore(&dev->irqlock, flags);
461 wake_up_ctx(ctx, reason, err);
465 ctx->state = MFCINST_ERROR;
466 wake_up_ctx(ctx, reason, err);
470 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
471 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
473 wake_up_dev(dev, reason, err);
477 /* Header parsing interrupt handling */
478 static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
479 unsigned int reason, unsigned int err)
481 struct s5p_mfc_dev *dev;
486 if (ctx->c_ops->post_seq_start) {
487 if (ctx->c_ops->post_seq_start(ctx))
488 mfc_err("post_seq_start() failed\n");
490 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
492 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
495 s5p_mfc_hw_call_void(dev->mfc_ops, dec_calc_dpb_size, ctx);
497 ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
499 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
501 if (ctx->img_width == 0 || ctx->img_height == 0)
502 ctx->state = MFCINST_ERROR;
504 ctx->state = MFCINST_HEAD_PARSED;
506 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
507 ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
508 !list_empty(&ctx->src_queue)) {
509 struct s5p_mfc_buf *src_buf;
510 src_buf = list_entry(ctx->src_queue.next,
511 struct s5p_mfc_buf, list);
512 if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
514 src_buf->b->vb2_buf.planes[0].bytesused)
515 ctx->head_processed = 0;
517 ctx->head_processed = 1;
519 ctx->head_processed = 1;
522 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
524 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
526 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
527 wake_up_ctx(ctx, reason, err);
530 /* Header parsing interrupt handling */
531 static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
532 unsigned int reason, unsigned int err)
534 struct s5p_mfc_buf *src_buf;
535 struct s5p_mfc_dev *dev;
541 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
542 ctx->int_type = reason;
547 ctx->state = MFCINST_RUNNING;
548 if (!ctx->dpb_flush_flag && ctx->head_processed) {
549 spin_lock_irqsave(&dev->irqlock, flags);
550 if (!list_empty(&ctx->src_queue)) {
551 src_buf = list_entry(ctx->src_queue.next,
552 struct s5p_mfc_buf, list);
553 list_del(&src_buf->list);
554 ctx->src_queue_cnt--;
555 vb2_buffer_done(&src_buf->b->vb2_buf,
558 spin_unlock_irqrestore(&dev->irqlock, flags);
560 ctx->dpb_flush_flag = 0;
562 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
566 wake_up(&ctx->queue);
567 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
569 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
573 wake_up(&ctx->queue);
577 static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx)
579 struct s5p_mfc_dev *dev = ctx->dev;
580 struct s5p_mfc_buf *mb_entry;
582 mfc_debug(2, "Stream completed\n");
584 ctx->state = MFCINST_FINISHED;
586 spin_lock(&dev->irqlock);
587 if (!list_empty(&ctx->dst_queue)) {
588 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
590 list_del(&mb_entry->list);
591 ctx->dst_queue_cnt--;
592 vb2_set_plane_payload(&mb_entry->b->vb2_buf, 0, 0);
593 vb2_buffer_done(&mb_entry->b->vb2_buf, VB2_BUF_STATE_DONE);
595 spin_unlock(&dev->irqlock);
599 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
602 wake_up(&ctx->queue);
603 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
606 /* Interrupt processing */
607 static irqreturn_t s5p_mfc_irq(int irq, void *priv)
609 struct s5p_mfc_dev *dev = priv;
610 struct s5p_mfc_ctx *ctx;
615 /* Reset the timeout watchdog */
616 atomic_set(&dev->watchdog_cnt, 0);
617 ctx = dev->ctx[dev->curr_ctx];
618 /* Get the reason of interrupt and the error code */
619 reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
620 err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
621 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
623 case S5P_MFC_R2H_CMD_ERR_RET:
624 /* An error has occurred */
625 if (ctx->state == MFCINST_RUNNING &&
626 s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
628 s5p_mfc_handle_frame(ctx, reason, err);
630 s5p_mfc_handle_error(dev, ctx, reason, err);
631 clear_bit(0, &dev->enter_suspend);
634 case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
635 case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
636 case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
637 if (ctx->c_ops->post_frame_start) {
638 if (ctx->c_ops->post_frame_start(ctx))
639 mfc_err("post_frame_start() failed\n");
641 if (ctx->state == MFCINST_FINISHING &&
642 list_empty(&ctx->ref_queue)) {
643 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
644 s5p_mfc_handle_stream_complete(ctx);
647 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
648 wake_up_ctx(ctx, reason, err);
649 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
651 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
653 s5p_mfc_handle_frame(ctx, reason, err);
657 case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
658 s5p_mfc_handle_seq_done(ctx, reason, err);
661 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
662 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
663 ctx->state = MFCINST_GOT_INST;
665 wake_up(&ctx->queue);
668 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
670 ctx->inst_no = MFC_NO_INSTANCE_SET;
671 ctx->state = MFCINST_FREE;
672 wake_up(&ctx->queue);
675 case S5P_MFC_R2H_CMD_SYS_INIT_RET:
676 case S5P_MFC_R2H_CMD_FW_STATUS_RET:
677 case S5P_MFC_R2H_CMD_SLEEP_RET:
678 case S5P_MFC_R2H_CMD_WAKEUP_RET:
681 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
682 wake_up_dev(dev, reason, err);
683 clear_bit(0, &dev->hw_lock);
684 clear_bit(0, &dev->enter_suspend);
687 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
688 s5p_mfc_handle_init_buffers(ctx, reason, err);
691 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
692 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
693 ctx->int_type = reason;
695 s5p_mfc_handle_stream_complete(ctx);
698 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
700 ctx->state = MFCINST_RUNNING;
701 wake_up(&ctx->queue);
705 mfc_debug(2, "Unknown int reason\n");
706 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
711 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
712 ctx->int_type = reason;
715 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
716 mfc_err("Failed to unlock hw\n");
720 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
721 mfc_debug(2, "Exit via irq_cleanup_hw\n");
725 /* Open an MFC node */
726 static int s5p_mfc_open(struct file *file)
728 struct video_device *vdev = video_devdata(file);
729 struct s5p_mfc_dev *dev = video_drvdata(file);
730 struct s5p_mfc_ctx *ctx = NULL;
735 if (mutex_lock_interruptible(&dev->mfc_mutex))
737 dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
738 /* Allocate memory for context */
739 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
741 mfc_err("Not enough memory\n");
745 v4l2_fh_init(&ctx->fh, vdev);
746 file->private_data = &ctx->fh;
747 v4l2_fh_add(&ctx->fh);
749 INIT_LIST_HEAD(&ctx->src_queue);
750 INIT_LIST_HEAD(&ctx->dst_queue);
751 ctx->src_queue_cnt = 0;
752 ctx->dst_queue_cnt = 0;
753 /* Get context number */
755 while (dev->ctx[ctx->num]) {
757 if (ctx->num >= MFC_NUM_CONTEXTS) {
758 mfc_err("Too many open contexts\n");
763 /* Mark context as idle */
764 clear_work_bit_irqsave(ctx);
765 dev->ctx[ctx->num] = ctx;
766 if (vdev == dev->vfd_dec) {
767 ctx->type = MFCINST_DECODER;
768 ctx->c_ops = get_dec_codec_ops();
769 s5p_mfc_dec_init(ctx);
770 /* Setup ctrl handler */
771 ret = s5p_mfc_dec_ctrls_setup(ctx);
773 mfc_err("Failed to setup mfc controls\n");
774 goto err_ctrls_setup;
776 } else if (vdev == dev->vfd_enc) {
777 ctx->type = MFCINST_ENCODER;
778 ctx->c_ops = get_enc_codec_ops();
779 /* only for encoder */
780 INIT_LIST_HEAD(&ctx->ref_queue);
781 ctx->ref_queue_cnt = 0;
782 s5p_mfc_enc_init(ctx);
783 /* Setup ctrl handler */
784 ret = s5p_mfc_enc_ctrls_setup(ctx);
786 mfc_err("Failed to setup mfc controls\n");
787 goto err_ctrls_setup;
793 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
794 ctx->inst_no = MFC_NO_INSTANCE_SET;
795 /* Load firmware if this is the first instance */
796 if (dev->num_inst == 1) {
797 dev->watchdog_timer.expires = jiffies +
798 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
799 add_timer(&dev->watchdog_timer);
800 ret = s5p_mfc_power_on();
802 mfc_err("power on failed\n");
806 ret = s5p_mfc_load_firmware(dev);
812 ret = s5p_mfc_init_hw(dev);
817 /* Init videobuf2 queue for CAPTURE */
819 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
820 q->drv_priv = &ctx->fh;
821 q->lock = &dev->mfc_mutex;
822 if (vdev == dev->vfd_dec) {
823 q->io_modes = VB2_MMAP;
824 q->ops = get_dec_queue_ops();
825 } else if (vdev == dev->vfd_enc) {
826 q->io_modes = VB2_MMAP | VB2_USERPTR;
827 q->ops = get_enc_queue_ops();
832 q->mem_ops = &vb2_dma_contig_memops;
833 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
834 ret = vb2_queue_init(q);
836 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
839 /* Init videobuf2 queue for OUTPUT */
841 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
842 q->io_modes = VB2_MMAP;
843 q->drv_priv = &ctx->fh;
844 q->lock = &dev->mfc_mutex;
845 if (vdev == dev->vfd_dec) {
846 q->io_modes = VB2_MMAP;
847 q->ops = get_dec_queue_ops();
848 } else if (vdev == dev->vfd_enc) {
849 q->io_modes = VB2_MMAP | VB2_USERPTR;
850 q->ops = get_enc_queue_ops();
855 /* One way to indicate end-of-stream for MFC is to set the
856 * bytesused == 0. However by default videobuf2 handles bytesused
857 * equal to 0 as a special case and changes its value to the size
858 * of the buffer. Set the allow_zero_bytesused flag so that videobuf2
859 * will keep the value of bytesused intact.
861 q->allow_zero_bytesused = 1;
862 q->mem_ops = &vb2_dma_contig_memops;
863 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
864 ret = vb2_queue_init(q);
866 mfc_err("Failed to initialize videobuf2 queue(output)\n");
869 init_waitqueue_head(&ctx->queue);
870 mutex_unlock(&dev->mfc_mutex);
873 /* Deinit when failure occurred */
875 if (dev->num_inst == 1)
876 s5p_mfc_deinit_hw(dev);
880 if (dev->num_inst == 1) {
881 if (s5p_mfc_power_off() < 0)
882 mfc_err("power off failed\n");
883 del_timer_sync(&dev->watchdog_timer);
886 s5p_mfc_dec_ctrls_delete(ctx);
888 dev->ctx[ctx->num] = NULL;
890 v4l2_fh_del(&ctx->fh);
891 v4l2_fh_exit(&ctx->fh);
895 mutex_unlock(&dev->mfc_mutex);
900 /* Release MFC context */
901 static int s5p_mfc_release(struct file *file)
903 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
904 struct s5p_mfc_dev *dev = ctx->dev;
907 mutex_lock(&dev->mfc_mutex);
909 vb2_queue_release(&ctx->vq_src);
910 vb2_queue_release(&ctx->vq_dst);
911 /* Mark context as idle */
912 clear_work_bit_irqsave(ctx);
913 /* If instance was initialised and not yet freed,
914 * return instance and free resources */
915 if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
916 mfc_debug(2, "Has to free instance\n");
917 s5p_mfc_close_mfc_inst(dev, ctx);
919 /* hardware locking scheme */
920 if (dev->curr_ctx == ctx->num)
921 clear_bit(0, &dev->hw_lock);
923 if (dev->num_inst == 0) {
924 mfc_debug(2, "Last instance\n");
925 s5p_mfc_deinit_hw(dev);
926 del_timer_sync(&dev->watchdog_timer);
927 if (s5p_mfc_power_off() < 0)
928 mfc_err("Power off failed\n");
930 mfc_debug(2, "Shutting down clock\n");
932 dev->ctx[ctx->num] = NULL;
933 s5p_mfc_dec_ctrls_delete(ctx);
934 v4l2_fh_del(&ctx->fh);
935 v4l2_fh_exit(&ctx->fh);
938 mutex_unlock(&dev->mfc_mutex);
943 static unsigned int s5p_mfc_poll(struct file *file,
944 struct poll_table_struct *wait)
946 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
947 struct s5p_mfc_dev *dev = ctx->dev;
948 struct vb2_queue *src_q, *dst_q;
949 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
953 mutex_lock(&dev->mfc_mutex);
954 src_q = &ctx->vq_src;
955 dst_q = &ctx->vq_dst;
957 * There has to be at least one buffer queued on each queued_list, which
958 * means either in driver already or waiting for driver to claim it
959 * and start processing.
961 if ((!src_q->streaming || list_empty(&src_q->queued_list))
962 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
966 mutex_unlock(&dev->mfc_mutex);
967 poll_wait(file, &ctx->fh.wait, wait);
968 poll_wait(file, &src_q->done_wq, wait);
969 poll_wait(file, &dst_q->done_wq, wait);
970 mutex_lock(&dev->mfc_mutex);
971 if (v4l2_event_pending(&ctx->fh))
973 spin_lock_irqsave(&src_q->done_lock, flags);
974 if (!list_empty(&src_q->done_list))
975 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
977 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
978 || src_vb->state == VB2_BUF_STATE_ERROR))
979 rc |= POLLOUT | POLLWRNORM;
980 spin_unlock_irqrestore(&src_q->done_lock, flags);
981 spin_lock_irqsave(&dst_q->done_lock, flags);
982 if (!list_empty(&dst_q->done_list))
983 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
985 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
986 || dst_vb->state == VB2_BUF_STATE_ERROR))
987 rc |= POLLIN | POLLRDNORM;
988 spin_unlock_irqrestore(&dst_q->done_lock, flags);
990 mutex_unlock(&dev->mfc_mutex);
995 static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
997 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
998 struct s5p_mfc_dev *dev = ctx->dev;
999 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
1002 if (mutex_lock_interruptible(&dev->mfc_mutex))
1003 return -ERESTARTSYS;
1004 if (offset < DST_QUEUE_OFF_BASE) {
1005 mfc_debug(2, "mmaping source\n");
1006 ret = vb2_mmap(&ctx->vq_src, vma);
1007 } else { /* capture */
1008 mfc_debug(2, "mmaping destination\n");
1009 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
1010 ret = vb2_mmap(&ctx->vq_dst, vma);
1012 mutex_unlock(&dev->mfc_mutex);
1017 static const struct v4l2_file_operations s5p_mfc_fops = {
1018 .owner = THIS_MODULE,
1019 .open = s5p_mfc_open,
1020 .release = s5p_mfc_release,
1021 .poll = s5p_mfc_poll,
1022 .unlocked_ioctl = video_ioctl2,
1023 .mmap = s5p_mfc_mmap,
1026 static int match_child(struct device *dev, void *data)
1030 return !strcmp(dev_name(dev), (char *)data);
1033 static void s5p_mfc_memdev_release(struct device *dev)
1035 dma_release_declared_memory(dev);
1038 static void *mfc_get_drv_data(struct platform_device *pdev);
1040 static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
1042 unsigned int mem_info[2] = { };
1044 dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev,
1045 sizeof(struct device), GFP_KERNEL);
1046 if (!dev->mem_dev_l) {
1047 mfc_err("Not enough memory\n");
1051 dev_set_name(dev->mem_dev_l, "%s", "s5p-mfc-l");
1052 dev->mem_dev_l->release = s5p_mfc_memdev_release;
1053 device_initialize(dev->mem_dev_l);
1054 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1055 "samsung,mfc-l", mem_info, 2);
1056 if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0],
1057 mem_info[0], mem_info[1],
1058 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1059 mfc_err("Failed to declare coherent memory for\n"
1064 dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev,
1065 sizeof(struct device), GFP_KERNEL);
1066 if (!dev->mem_dev_r) {
1067 mfc_err("Not enough memory\n");
1071 dev_set_name(dev->mem_dev_r, "%s", "s5p-mfc-r");
1072 dev->mem_dev_r->release = s5p_mfc_memdev_release;
1073 device_initialize(dev->mem_dev_r);
1074 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1075 "samsung,mfc-r", mem_info, 2);
1076 if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0],
1077 mem_info[0], mem_info[1],
1078 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1079 pr_err("Failed to declare coherent memory for\n"
1086 /* MFC probe function */
1087 static int s5p_mfc_probe(struct platform_device *pdev)
1089 struct s5p_mfc_dev *dev;
1090 struct video_device *vfd;
1091 struct resource *res;
1094 pr_debug("%s++\n", __func__);
1095 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1097 dev_err(&pdev->dev, "Not enough memory for MFC device\n");
1101 spin_lock_init(&dev->irqlock);
1102 spin_lock_init(&dev->condlock);
1103 dev->plat_dev = pdev;
1104 if (!dev->plat_dev) {
1105 dev_err(&pdev->dev, "No platform data specified\n");
1109 dev->variant = mfc_get_drv_data(pdev);
1111 ret = s5p_mfc_init_pm(dev);
1113 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1117 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1119 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1120 if (IS_ERR(dev->regs_base))
1121 return PTR_ERR(dev->regs_base);
1123 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1125 dev_err(&pdev->dev, "failed to get irq resource\n");
1129 dev->irq = res->start;
1130 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1131 0, pdev->name, dev);
1133 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1137 if (pdev->dev.of_node) {
1138 ret = s5p_mfc_alloc_memdevs(dev);
1142 dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
1143 "s5p-mfc-l", match_child);
1144 if (!dev->mem_dev_l) {
1145 mfc_err("Mem child (L) device get failed\n");
1149 dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
1150 "s5p-mfc-r", match_child);
1151 if (!dev->mem_dev_r) {
1152 mfc_err("Mem child (R) device get failed\n");
1158 dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
1159 if (IS_ERR(dev->alloc_ctx[0])) {
1160 ret = PTR_ERR(dev->alloc_ctx[0]);
1163 dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
1164 if (IS_ERR(dev->alloc_ctx[1])) {
1165 ret = PTR_ERR(dev->alloc_ctx[1]);
1166 goto err_mem_init_ctx_1;
1169 mutex_init(&dev->mfc_mutex);
1171 ret = s5p_mfc_alloc_firmware(dev);
1175 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1177 goto err_v4l2_dev_reg;
1178 init_waitqueue_head(&dev->queue);
1181 vfd = video_device_alloc();
1183 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1187 vfd->fops = &s5p_mfc_fops;
1188 vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
1189 vfd->release = video_device_release;
1190 vfd->lock = &dev->mfc_mutex;
1191 vfd->v4l2_dev = &dev->v4l2_dev;
1192 vfd->vfl_dir = VFL_DIR_M2M;
1193 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1195 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1197 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1198 video_device_release(vfd);
1201 v4l2_info(&dev->v4l2_dev,
1202 "decoder registered as /dev/video%d\n", vfd->num);
1203 video_set_drvdata(vfd, dev);
1206 vfd = video_device_alloc();
1208 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1212 vfd->fops = &s5p_mfc_fops;
1213 vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
1214 vfd->release = video_device_release;
1215 vfd->lock = &dev->mfc_mutex;
1216 vfd->v4l2_dev = &dev->v4l2_dev;
1217 vfd->vfl_dir = VFL_DIR_M2M;
1218 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1220 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1222 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1223 video_device_release(vfd);
1226 v4l2_info(&dev->v4l2_dev,
1227 "encoder registered as /dev/video%d\n", vfd->num);
1228 video_set_drvdata(vfd, dev);
1229 platform_set_drvdata(pdev, dev);
1232 dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
1233 INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1234 atomic_set(&dev->watchdog_cnt, 0);
1235 init_timer(&dev->watchdog_timer);
1236 dev->watchdog_timer.data = (unsigned long)dev;
1237 dev->watchdog_timer.function = s5p_mfc_watchdog;
1239 /* Initialize HW ops and commands based on MFC version */
1240 s5p_mfc_init_hw_ops(dev);
1241 s5p_mfc_init_hw_cmds(dev);
1242 s5p_mfc_init_regs(dev);
1244 pr_debug("%s--\n", __func__);
1247 /* Deinit MFC if probe had failed */
1249 video_device_release(dev->vfd_enc);
1251 video_unregister_device(dev->vfd_dec);
1253 video_device_release(dev->vfd_dec);
1255 v4l2_device_unregister(&dev->v4l2_dev);
1257 s5p_mfc_release_firmware(dev);
1259 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1261 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1263 s5p_mfc_final_pm(dev);
1265 pr_debug("%s-- with error\n", __func__);
1270 /* Remove the driver */
1271 static int s5p_mfc_remove(struct platform_device *pdev)
1273 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1275 v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1277 del_timer_sync(&dev->watchdog_timer);
1278 flush_workqueue(dev->watchdog_workqueue);
1279 destroy_workqueue(dev->watchdog_workqueue);
1281 video_unregister_device(dev->vfd_enc);
1282 video_unregister_device(dev->vfd_dec);
1283 v4l2_device_unregister(&dev->v4l2_dev);
1284 s5p_mfc_release_firmware(dev);
1285 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1286 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1287 if (pdev->dev.of_node) {
1288 put_device(dev->mem_dev_l);
1289 put_device(dev->mem_dev_r);
1292 s5p_mfc_final_pm(dev);
1296 #ifdef CONFIG_PM_SLEEP
1298 static int s5p_mfc_suspend(struct device *dev)
1300 struct platform_device *pdev = to_platform_device(dev);
1301 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1304 if (m_dev->num_inst == 0)
1307 if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1308 mfc_err("Error: going to suspend for a second time\n");
1312 /* Check if we're processing then wait if it necessary. */
1313 while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1314 /* Try and lock the HW */
1315 /* Wait on the interrupt waitqueue */
1316 ret = wait_event_interruptible_timeout(m_dev->queue,
1317 m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
1319 mfc_err("Waiting for hardware to finish timed out\n");
1320 clear_bit(0, &m_dev->enter_suspend);
1325 ret = s5p_mfc_sleep(m_dev);
1327 clear_bit(0, &m_dev->enter_suspend);
1328 clear_bit(0, &m_dev->hw_lock);
1333 static int s5p_mfc_resume(struct device *dev)
1335 struct platform_device *pdev = to_platform_device(dev);
1336 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1338 if (m_dev->num_inst == 0)
1340 return s5p_mfc_wakeup(m_dev);
1345 static int s5p_mfc_runtime_suspend(struct device *dev)
1347 struct platform_device *pdev = to_platform_device(dev);
1348 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1350 atomic_set(&m_dev->pm.power, 0);
1354 static int s5p_mfc_runtime_resume(struct device *dev)
1356 struct platform_device *pdev = to_platform_device(dev);
1357 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1359 atomic_set(&m_dev->pm.power, 1);
1364 /* Power management */
1365 static const struct dev_pm_ops s5p_mfc_pm_ops = {
1366 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1367 SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
1371 static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1372 .h264_ctx = MFC_H264_CTX_BUF_SIZE,
1373 .non_h264_ctx = MFC_CTX_BUF_SIZE,
1374 .dsc = DESC_BUF_SIZE,
1375 .shm = SHARED_BUF_SIZE,
1378 static struct s5p_mfc_buf_size buf_size_v5 = {
1380 .cpb = MAX_CPB_SIZE,
1381 .priv = &mfc_buf_size_v5,
1384 static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
1385 .base = MFC_BASE_ALIGN_ORDER,
1388 static struct s5p_mfc_variant mfc_drvdata_v5 = {
1389 .version = MFC_VERSION,
1390 .version_bit = MFC_V5_BIT,
1391 .port_num = MFC_NUM_PORTS,
1392 .buf_size = &buf_size_v5,
1393 .buf_align = &mfc_buf_align_v5,
1394 .fw_name[0] = "/*(DEBLOBBED)*/",
1397 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1398 .dev_ctx = MFC_CTX_BUF_SIZE_V6,
1399 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1400 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1401 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1402 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1405 static struct s5p_mfc_buf_size buf_size_v6 = {
1406 .fw = MAX_FW_SIZE_V6,
1407 .cpb = MAX_CPB_SIZE_V6,
1408 .priv = &mfc_buf_size_v6,
1411 static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
1415 static struct s5p_mfc_variant mfc_drvdata_v6 = {
1416 .version = MFC_VERSION_V6,
1417 .version_bit = MFC_V6_BIT,
1418 .port_num = MFC_NUM_PORTS_V6,
1419 .buf_size = &buf_size_v6,
1420 .buf_align = &mfc_buf_align_v6,
1421 .fw_name[0] = "/*(DEBLOBBED)*/",
1423 * v6-v2 firmware contains bug fixes and interface change
1424 * for init buffer command
1426 .fw_name[1] = "/*(DEBLOBBED)*/",
1429 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
1430 .dev_ctx = MFC_CTX_BUF_SIZE_V7,
1431 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
1432 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
1433 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
1434 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
1437 static struct s5p_mfc_buf_size buf_size_v7 = {
1438 .fw = MAX_FW_SIZE_V7,
1439 .cpb = MAX_CPB_SIZE_V7,
1440 .priv = &mfc_buf_size_v7,
1443 static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
1447 static struct s5p_mfc_variant mfc_drvdata_v7 = {
1448 .version = MFC_VERSION_V7,
1449 .version_bit = MFC_V7_BIT,
1450 .port_num = MFC_NUM_PORTS_V7,
1451 .buf_size = &buf_size_v7,
1452 .buf_align = &mfc_buf_align_v7,
1453 .fw_name[0] = "/*(DEBLOBBED)*/",
1456 static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
1457 .dev_ctx = MFC_CTX_BUF_SIZE_V8,
1458 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
1459 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
1460 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
1461 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
1464 static struct s5p_mfc_buf_size buf_size_v8 = {
1465 .fw = MAX_FW_SIZE_V8,
1466 .cpb = MAX_CPB_SIZE_V8,
1467 .priv = &mfc_buf_size_v8,
1470 static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
1474 static struct s5p_mfc_variant mfc_drvdata_v8 = {
1475 .version = MFC_VERSION_V8,
1476 .version_bit = MFC_V8_BIT,
1477 .port_num = MFC_NUM_PORTS_V8,
1478 .buf_size = &buf_size_v8,
1479 .buf_align = &mfc_buf_align_v8,
1480 .fw_name[0] = "/*(DEBLOBBED)*/",
1483 static const struct platform_device_id mfc_driver_ids[] = {
1486 .driver_data = (unsigned long)&mfc_drvdata_v5,
1488 .name = "s5p-mfc-v5",
1489 .driver_data = (unsigned long)&mfc_drvdata_v5,
1491 .name = "s5p-mfc-v6",
1492 .driver_data = (unsigned long)&mfc_drvdata_v6,
1494 .name = "s5p-mfc-v7",
1495 .driver_data = (unsigned long)&mfc_drvdata_v7,
1497 .name = "s5p-mfc-v8",
1498 .driver_data = (unsigned long)&mfc_drvdata_v8,
1502 MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
1504 static const struct of_device_id exynos_mfc_match[] = {
1506 .compatible = "samsung,mfc-v5",
1507 .data = &mfc_drvdata_v5,
1509 .compatible = "samsung,mfc-v6",
1510 .data = &mfc_drvdata_v6,
1512 .compatible = "samsung,mfc-v7",
1513 .data = &mfc_drvdata_v7,
1515 .compatible = "samsung,mfc-v8",
1516 .data = &mfc_drvdata_v8,
1520 MODULE_DEVICE_TABLE(of, exynos_mfc_match);
1522 static void *mfc_get_drv_data(struct platform_device *pdev)
1524 struct s5p_mfc_variant *driver_data = NULL;
1526 if (pdev->dev.of_node) {
1527 const struct of_device_id *match;
1528 match = of_match_node(exynos_mfc_match,
1531 driver_data = (struct s5p_mfc_variant *)match->data;
1533 driver_data = (struct s5p_mfc_variant *)
1534 platform_get_device_id(pdev)->driver_data;
1539 static struct platform_driver s5p_mfc_driver = {
1540 .probe = s5p_mfc_probe,
1541 .remove = s5p_mfc_remove,
1542 .id_table = mfc_driver_ids,
1544 .name = S5P_MFC_NAME,
1545 .pm = &s5p_mfc_pm_ops,
1546 .of_match_table = exynos_mfc_match,
1550 module_platform_driver(s5p_mfc_driver);
1552 MODULE_LICENSE("GPL");
1553 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1554 MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");