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-core.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(debug
, 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(dev
->mfc_ops
, cleanup_queue
, &ctx
->dst_queue
,
155 s5p_mfc_hw_call(dev
->mfc_ops
, cleanup_queue
, &ctx
->src_queue
,
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
);
162 /* Double check if there is at least one instance running.
163 * If no instance is in memory than no firmware should be present */
164 if (dev
->num_inst
> 0) {
165 ret
= s5p_mfc_reload_firmware(dev
);
167 mfc_err("Failed to reload FW\n");
171 ret
= s5p_mfc_init_hw(dev
);
173 mfc_err("Failed to reinit FW\n");
177 mutex_unlock(&dev
->mfc_mutex
);
180 static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev
*dev
)
182 mfc_write(dev
, 0, S5P_FIMV_RISC_HOST_INT
);
183 mfc_write(dev
, 0, S5P_FIMV_RISC2HOST_CMD
);
184 mfc_write(dev
, 0xffff, S5P_FIMV_SI_RTN_CHID
);
187 static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx
*ctx
)
189 struct s5p_mfc_buf
*dst_buf
;
190 struct s5p_mfc_dev
*dev
= ctx
->dev
;
192 ctx
->state
= MFCINST_FINISHED
;
194 while (!list_empty(&ctx
->dst_queue
)) {
195 dst_buf
= list_entry(ctx
->dst_queue
.next
,
196 struct s5p_mfc_buf
, list
);
197 mfc_debug(2, "Cleaning up buffer: %d\n",
198 dst_buf
->b
->v4l2_buf
.index
);
199 vb2_set_plane_payload(dst_buf
->b
, 0, 0);
200 vb2_set_plane_payload(dst_buf
->b
, 1, 0);
201 list_del(&dst_buf
->list
);
202 ctx
->dst_queue_cnt
--;
203 dst_buf
->b
->v4l2_buf
.sequence
= (ctx
->sequence
++);
205 if (s5p_mfc_hw_call(dev
->mfc_ops
, get_pic_type_top
, ctx
) ==
206 s5p_mfc_hw_call(dev
->mfc_ops
, get_pic_type_bot
, ctx
))
207 dst_buf
->b
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
209 dst_buf
->b
->v4l2_buf
.field
= V4L2_FIELD_INTERLACED
;
211 ctx
->dec_dst_flag
&= ~(1 << dst_buf
->b
->v4l2_buf
.index
);
212 vb2_buffer_done(dst_buf
->b
, 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 dec_y_addr
= s5p_mfc_hw_call(dev
->mfc_ops
, get_dec_y_adr
, dev
);
224 frame_type
= s5p_mfc_hw_call(dev
->mfc_ops
, get_dec_frame_type
, dev
);
226 /* Copy timestamp / timecode from decoded src to dst and set
228 src_buf
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
, list
);
229 list_for_each_entry(dst_buf
, &ctx
->dst_queue
, list
) {
230 if (vb2_dma_contig_plane_dma_addr(dst_buf
->b
, 0) == dec_y_addr
) {
231 dst_buf
->b
->v4l2_buf
.timecode
=
232 src_buf
->b
->v4l2_buf
.timecode
;
233 dst_buf
->b
->v4l2_buf
.timestamp
=
234 src_buf
->b
->v4l2_buf
.timestamp
;
235 switch (frame_type
) {
236 case S5P_FIMV_DECODE_FRAME_I_FRAME
:
237 dst_buf
->b
->v4l2_buf
.flags
|=
238 V4L2_BUF_FLAG_KEYFRAME
;
240 case S5P_FIMV_DECODE_FRAME_P_FRAME
:
241 dst_buf
->b
->v4l2_buf
.flags
|=
242 V4L2_BUF_FLAG_PFRAME
;
244 case S5P_FIMV_DECODE_FRAME_B_FRAME
:
245 dst_buf
->b
->v4l2_buf
.flags
|=
246 V4L2_BUF_FLAG_BFRAME
;
254 static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx
*ctx
, unsigned int err
)
256 struct s5p_mfc_dev
*dev
= ctx
->dev
;
257 struct s5p_mfc_buf
*dst_buf
;
259 unsigned int frame_type
;
261 dspl_y_addr
= s5p_mfc_hw_call(dev
->mfc_ops
, get_dspl_y_adr
, dev
);
262 frame_type
= s5p_mfc_hw_call(dev
->mfc_ops
, get_disp_frame_type
, ctx
);
264 /* If frame is same as previous then skip and do not dequeue */
265 if (frame_type
== S5P_FIMV_DECODE_FRAME_SKIPPED
) {
266 if (!ctx
->after_packed_pb
)
268 ctx
->after_packed_pb
= 0;
272 /* The MFC returns address of the buffer, now we have to
273 * check which videobuf does it correspond to */
274 list_for_each_entry(dst_buf
, &ctx
->dst_queue
, list
) {
275 /* Check if this is the buffer we're looking for */
276 if (vb2_dma_contig_plane_dma_addr(dst_buf
->b
, 0) == dspl_y_addr
) {
277 list_del(&dst_buf
->list
);
278 ctx
->dst_queue_cnt
--;
279 dst_buf
->b
->v4l2_buf
.sequence
= ctx
->sequence
;
280 if (s5p_mfc_hw_call(dev
->mfc_ops
,
281 get_pic_type_top
, ctx
) ==
282 s5p_mfc_hw_call(dev
->mfc_ops
,
283 get_pic_type_bot
, ctx
))
284 dst_buf
->b
->v4l2_buf
.field
= V4L2_FIELD_NONE
;
286 dst_buf
->b
->v4l2_buf
.field
=
287 V4L2_FIELD_INTERLACED
;
288 vb2_set_plane_payload(dst_buf
->b
, 0, ctx
->luma_size
);
289 vb2_set_plane_payload(dst_buf
->b
, 1, ctx
->chroma_size
);
290 clear_bit(dst_buf
->b
->v4l2_buf
.index
,
293 vb2_buffer_done(dst_buf
->b
,
294 err
? VB2_BUF_STATE_ERROR
: VB2_BUF_STATE_DONE
);
301 /* Handle frame decoding interrupt */
302 static void s5p_mfc_handle_frame(struct s5p_mfc_ctx
*ctx
,
303 unsigned int reason
, unsigned int err
)
305 struct s5p_mfc_dev
*dev
= ctx
->dev
;
306 unsigned int dst_frame_status
;
307 struct s5p_mfc_buf
*src_buf
;
309 unsigned int res_change
;
311 dst_frame_status
= s5p_mfc_hw_call(dev
->mfc_ops
, get_dspl_status
, dev
)
312 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK
;
313 res_change
= (s5p_mfc_hw_call(dev
->mfc_ops
, get_dspl_status
, dev
)
314 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK
)
315 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT
;
316 mfc_debug(2, "Frame Status: %x\n", dst_frame_status
);
317 if (ctx
->state
== MFCINST_RES_CHANGE_INIT
)
318 ctx
->state
= MFCINST_RES_CHANGE_FLUSH
;
319 if (res_change
== S5P_FIMV_RES_INCREASE
||
320 res_change
== S5P_FIMV_RES_DECREASE
) {
321 ctx
->state
= MFCINST_RES_CHANGE_INIT
;
322 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
323 wake_up_ctx(ctx
, reason
, err
);
324 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
327 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
330 if (ctx
->dpb_flush_flag
)
331 ctx
->dpb_flush_flag
= 0;
333 spin_lock_irqsave(&dev
->irqlock
, flags
);
334 /* All frames remaining in the buffer have been extracted */
335 if (dst_frame_status
== S5P_FIMV_DEC_STATUS_DECODING_EMPTY
) {
336 if (ctx
->state
== MFCINST_RES_CHANGE_FLUSH
) {
337 s5p_mfc_handle_frame_all_extracted(ctx
);
338 ctx
->state
= MFCINST_RES_CHANGE_END
;
339 goto leave_handle_frame
;
341 s5p_mfc_handle_frame_all_extracted(ctx
);
345 if (dst_frame_status
== S5P_FIMV_DEC_STATUS_DECODING_DISPLAY
||
346 dst_frame_status
== S5P_FIMV_DEC_STATUS_DECODING_ONLY
)
347 s5p_mfc_handle_frame_copy_time(ctx
);
349 /* A frame has been decoded and is in the buffer */
350 if (dst_frame_status
== S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
||
351 dst_frame_status
== S5P_FIMV_DEC_STATUS_DECODING_DISPLAY
) {
352 s5p_mfc_handle_frame_new(ctx
, err
);
354 mfc_debug(2, "No frame decode\n");
356 /* Mark source buffer as complete */
357 if (dst_frame_status
!= S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
358 && !list_empty(&ctx
->src_queue
)) {
359 src_buf
= list_entry(ctx
->src_queue
.next
, struct s5p_mfc_buf
,
361 ctx
->consumed_stream
+= s5p_mfc_hw_call(dev
->mfc_ops
,
362 get_consumed_stream
, dev
);
363 if (ctx
->codec_mode
!= S5P_MFC_CODEC_H264_DEC
&&
364 ctx
->consumed_stream
+ STUFF_BYTE
<
365 src_buf
->b
->v4l2_planes
[0].bytesused
) {
366 /* Run MFC again on the same buffer */
367 mfc_debug(2, "Running again the same buffer\n");
368 ctx
->after_packed_pb
= 1;
370 mfc_debug(2, "MFC needs next buffer\n");
371 ctx
->consumed_stream
= 0;
372 if (src_buf
->flags
& MFC_BUF_FLAG_EOS
)
373 ctx
->state
= MFCINST_FINISHING
;
374 list_del(&src_buf
->list
);
375 ctx
->src_queue_cnt
--;
376 if (s5p_mfc_hw_call(dev
->mfc_ops
, err_dec
, err
) > 0)
377 vb2_buffer_done(src_buf
->b
, VB2_BUF_STATE_ERROR
);
379 vb2_buffer_done(src_buf
->b
, VB2_BUF_STATE_DONE
);
383 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
384 if ((ctx
->src_queue_cnt
== 0 && ctx
->state
!= MFCINST_FINISHING
)
385 || ctx
->dst_queue_cnt
< ctx
->pb_count
)
387 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
388 wake_up_ctx(ctx
, reason
, err
);
389 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
392 /* if suspending, wake up device and do not try_run again*/
393 if (test_bit(0, &dev
->enter_suspend
))
394 wake_up_dev(dev
, reason
, err
);
396 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
399 /* Error handling for interrupt */
400 static void s5p_mfc_handle_error(struct s5p_mfc_dev
*dev
,
401 struct s5p_mfc_ctx
*ctx
, unsigned int reason
, unsigned int err
)
405 mfc_err("Interrupt Error: %08x\n", err
);
408 /* Error recovery is dependent on the state of context */
409 switch (ctx
->state
) {
410 case MFCINST_RES_CHANGE_INIT
:
411 case MFCINST_RES_CHANGE_FLUSH
:
412 case MFCINST_RES_CHANGE_END
:
413 case MFCINST_FINISHING
:
414 case MFCINST_FINISHED
:
415 case MFCINST_RUNNING
:
416 /* It is highly probable that an error occurred
417 * while decoding a frame */
419 ctx
->state
= MFCINST_ERROR
;
420 /* Mark all dst buffers as having an error */
421 spin_lock_irqsave(&dev
->irqlock
, flags
);
422 s5p_mfc_hw_call(dev
->mfc_ops
, cleanup_queue
,
423 &ctx
->dst_queue
, &ctx
->vq_dst
);
424 /* Mark all src buffers as having an error */
425 s5p_mfc_hw_call(dev
->mfc_ops
, cleanup_queue
,
426 &ctx
->src_queue
, &ctx
->vq_src
);
427 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
428 wake_up_ctx(ctx
, reason
, err
);
432 ctx
->state
= MFCINST_ERROR
;
433 wake_up_ctx(ctx
, reason
, err
);
437 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
439 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
441 wake_up_dev(dev
, reason
, err
);
445 /* Header parsing interrupt handling */
446 static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx
*ctx
,
447 unsigned int reason
, unsigned int err
)
449 struct s5p_mfc_dev
*dev
;
454 if (ctx
->c_ops
->post_seq_start
) {
455 if (ctx
->c_ops
->post_seq_start(ctx
))
456 mfc_err("post_seq_start() failed\n");
458 ctx
->img_width
= s5p_mfc_hw_call(dev
->mfc_ops
, get_img_width
,
460 ctx
->img_height
= s5p_mfc_hw_call(dev
->mfc_ops
, get_img_height
,
463 s5p_mfc_hw_call(dev
->mfc_ops
, dec_calc_dpb_size
, ctx
);
465 ctx
->pb_count
= s5p_mfc_hw_call(dev
->mfc_ops
, get_dpb_count
,
467 ctx
->mv_count
= s5p_mfc_hw_call(dev
->mfc_ops
, get_mv_count
,
469 if (ctx
->img_width
== 0 || ctx
->img_height
== 0)
470 ctx
->state
= MFCINST_ERROR
;
472 ctx
->state
= MFCINST_HEAD_PARSED
;
474 if ((ctx
->codec_mode
== S5P_MFC_CODEC_H264_DEC
||
475 ctx
->codec_mode
== S5P_MFC_CODEC_H264_MVC_DEC
) &&
476 !list_empty(&ctx
->src_queue
)) {
477 struct s5p_mfc_buf
*src_buf
;
478 src_buf
= list_entry(ctx
->src_queue
.next
,
479 struct s5p_mfc_buf
, list
);
480 if (s5p_mfc_hw_call(dev
->mfc_ops
, get_consumed_stream
,
482 src_buf
->b
->v4l2_planes
[0].bytesused
)
483 ctx
->head_processed
= 0;
485 ctx
->head_processed
= 1;
487 ctx
->head_processed
= 1;
490 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
492 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
495 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
496 wake_up_ctx(ctx
, reason
, err
);
499 /* Header parsing interrupt handling */
500 static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx
*ctx
,
501 unsigned int reason
, unsigned int err
)
503 struct s5p_mfc_buf
*src_buf
;
504 struct s5p_mfc_dev
*dev
;
510 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
511 ctx
->int_type
= reason
;
516 ctx
->state
= MFCINST_RUNNING
;
517 if (!ctx
->dpb_flush_flag
&& ctx
->head_processed
) {
518 spin_lock_irqsave(&dev
->irqlock
, flags
);
519 if (!list_empty(&ctx
->src_queue
)) {
520 src_buf
= list_entry(ctx
->src_queue
.next
,
521 struct s5p_mfc_buf
, list
);
522 list_del(&src_buf
->list
);
523 ctx
->src_queue_cnt
--;
524 vb2_buffer_done(src_buf
->b
,
527 spin_unlock_irqrestore(&dev
->irqlock
, flags
);
529 ctx
->dpb_flush_flag
= 0;
531 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
536 wake_up(&ctx
->queue
);
537 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
539 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
544 wake_up(&ctx
->queue
);
548 static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx
*ctx
,
549 unsigned int reason
, unsigned int err
)
551 struct s5p_mfc_dev
*dev
= ctx
->dev
;
552 struct s5p_mfc_buf
*mb_entry
;
554 mfc_debug(2, "Stream completed\n");
556 s5p_mfc_clear_int_flags(dev
);
557 ctx
->int_type
= reason
;
559 ctx
->state
= MFCINST_FINISHED
;
561 spin_lock(&dev
->irqlock
);
562 if (!list_empty(&ctx
->dst_queue
)) {
563 mb_entry
= list_entry(ctx
->dst_queue
.next
, struct s5p_mfc_buf
,
565 list_del(&mb_entry
->list
);
566 ctx
->dst_queue_cnt
--;
567 vb2_set_plane_payload(mb_entry
->b
, 0, 0);
568 vb2_buffer_done(mb_entry
->b
, VB2_BUF_STATE_DONE
);
570 spin_unlock(&dev
->irqlock
);
574 WARN_ON(test_and_clear_bit(0, &dev
->hw_lock
) == 0);
577 wake_up(&ctx
->queue
);
578 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
581 /* Interrupt processing */
582 static irqreturn_t
s5p_mfc_irq(int irq
, void *priv
)
584 struct s5p_mfc_dev
*dev
= priv
;
585 struct s5p_mfc_ctx
*ctx
;
590 /* Reset the timeout watchdog */
591 atomic_set(&dev
->watchdog_cnt
, 0);
592 ctx
= dev
->ctx
[dev
->curr_ctx
];
593 /* Get the reason of interrupt and the error code */
594 reason
= s5p_mfc_hw_call(dev
->mfc_ops
, get_int_reason
, dev
);
595 err
= s5p_mfc_hw_call(dev
->mfc_ops
, get_int_err
, dev
);
596 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason
, err
);
598 case S5P_MFC_R2H_CMD_ERR_RET
:
599 /* An error has occurred */
600 if (ctx
->state
== MFCINST_RUNNING
&&
601 s5p_mfc_hw_call(dev
->mfc_ops
, err_dec
, err
) >=
603 s5p_mfc_handle_frame(ctx
, reason
, err
);
605 s5p_mfc_handle_error(dev
, ctx
, reason
, err
);
606 clear_bit(0, &dev
->enter_suspend
);
609 case S5P_MFC_R2H_CMD_SLICE_DONE_RET
:
610 case S5P_MFC_R2H_CMD_FIELD_DONE_RET
:
611 case S5P_MFC_R2H_CMD_FRAME_DONE_RET
:
612 if (ctx
->c_ops
->post_frame_start
) {
613 if (ctx
->c_ops
->post_frame_start(ctx
))
614 mfc_err("post_frame_start() failed\n");
615 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
616 wake_up_ctx(ctx
, reason
, err
);
617 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
620 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
622 s5p_mfc_handle_frame(ctx
, reason
, err
);
626 case S5P_MFC_R2H_CMD_SEQ_DONE_RET
:
627 s5p_mfc_handle_seq_done(ctx
, reason
, err
);
630 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET
:
631 ctx
->inst_no
= s5p_mfc_hw_call(dev
->mfc_ops
, get_inst_no
, dev
);
632 ctx
->state
= MFCINST_GOT_INST
;
634 wake_up(&ctx
->queue
);
637 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET
:
639 ctx
->state
= MFCINST_FREE
;
640 wake_up(&ctx
->queue
);
643 case S5P_MFC_R2H_CMD_SYS_INIT_RET
:
644 case S5P_MFC_R2H_CMD_FW_STATUS_RET
:
645 case S5P_MFC_R2H_CMD_SLEEP_RET
:
646 case S5P_MFC_R2H_CMD_WAKEUP_RET
:
649 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
650 wake_up_dev(dev
, reason
, err
);
651 clear_bit(0, &dev
->hw_lock
);
652 clear_bit(0, &dev
->enter_suspend
);
655 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET
:
656 s5p_mfc_handle_init_buffers(ctx
, reason
, err
);
659 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET
:
660 s5p_mfc_handle_stream_complete(ctx
, reason
, err
);
663 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET
:
665 ctx
->state
= MFCINST_RUNNING
;
666 wake_up(&ctx
->queue
);
670 mfc_debug(2, "Unknown int reason\n");
671 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
676 s5p_mfc_hw_call(dev
->mfc_ops
, clear_int_flags
, dev
);
677 ctx
->int_type
= reason
;
680 if (test_and_clear_bit(0, &dev
->hw_lock
) == 0)
681 mfc_err("Failed to unlock hw\n");
685 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
686 mfc_debug(2, "Exit via irq_cleanup_hw\n");
690 /* Open an MFC node */
691 static int s5p_mfc_open(struct file
*file
)
693 struct video_device
*vdev
= video_devdata(file
);
694 struct s5p_mfc_dev
*dev
= video_drvdata(file
);
695 struct s5p_mfc_ctx
*ctx
= NULL
;
700 if (mutex_lock_interruptible(&dev
->mfc_mutex
))
702 dev
->num_inst
++; /* It is guarded by mfc_mutex in vfd */
703 /* Allocate memory for context */
704 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
706 mfc_err("Not enough memory\n");
710 v4l2_fh_init(&ctx
->fh
, video_devdata(file
));
711 file
->private_data
= &ctx
->fh
;
712 v4l2_fh_add(&ctx
->fh
);
714 INIT_LIST_HEAD(&ctx
->src_queue
);
715 INIT_LIST_HEAD(&ctx
->dst_queue
);
716 ctx
->src_queue_cnt
= 0;
717 ctx
->dst_queue_cnt
= 0;
718 /* Get context number */
720 while (dev
->ctx
[ctx
->num
]) {
722 if (ctx
->num
>= MFC_NUM_CONTEXTS
) {
723 mfc_err("Too many open contexts\n");
728 /* Mark context as idle */
729 clear_work_bit_irqsave(ctx
);
730 dev
->ctx
[ctx
->num
] = ctx
;
731 if (vdev
== dev
->vfd_dec
) {
732 ctx
->type
= MFCINST_DECODER
;
733 ctx
->c_ops
= get_dec_codec_ops();
734 s5p_mfc_dec_init(ctx
);
735 /* Setup ctrl handler */
736 ret
= s5p_mfc_dec_ctrls_setup(ctx
);
738 mfc_err("Failed to setup mfc controls\n");
739 goto err_ctrls_setup
;
741 } else if (vdev
== dev
->vfd_enc
) {
742 ctx
->type
= MFCINST_ENCODER
;
743 ctx
->c_ops
= get_enc_codec_ops();
744 /* only for encoder */
745 INIT_LIST_HEAD(&ctx
->ref_queue
);
746 ctx
->ref_queue_cnt
= 0;
747 s5p_mfc_enc_init(ctx
);
748 /* Setup ctrl handler */
749 ret
= s5p_mfc_enc_ctrls_setup(ctx
);
751 mfc_err("Failed to setup mfc controls\n");
752 goto err_ctrls_setup
;
758 ctx
->fh
.ctrl_handler
= &ctx
->ctrl_handler
;
760 /* Load firmware if this is the first instance */
761 if (dev
->num_inst
== 1) {
762 dev
->watchdog_timer
.expires
= jiffies
+
763 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL
);
764 add_timer(&dev
->watchdog_timer
);
765 ret
= s5p_mfc_power_on();
767 mfc_err("power on failed\n");
771 ret
= s5p_mfc_load_firmware(dev
);
777 ret
= s5p_mfc_init_hw(dev
);
782 /* Init videobuf2 queue for CAPTURE */
784 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
785 q
->drv_priv
= &ctx
->fh
;
786 if (vdev
== dev
->vfd_dec
) {
787 q
->io_modes
= VB2_MMAP
;
788 q
->ops
= get_dec_queue_ops();
789 } else if (vdev
== dev
->vfd_enc
) {
790 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
791 q
->ops
= get_enc_queue_ops();
796 q
->mem_ops
= (struct vb2_mem_ops
*)&vb2_dma_contig_memops
;
797 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
798 ret
= vb2_queue_init(q
);
800 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
803 /* Init videobuf2 queue for OUTPUT */
805 q
->type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
806 q
->io_modes
= VB2_MMAP
;
807 q
->drv_priv
= &ctx
->fh
;
808 if (vdev
== dev
->vfd_dec
) {
809 q
->io_modes
= VB2_MMAP
;
810 q
->ops
= get_dec_queue_ops();
811 } else if (vdev
== dev
->vfd_enc
) {
812 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
;
813 q
->ops
= get_enc_queue_ops();
818 q
->mem_ops
= (struct vb2_mem_ops
*)&vb2_dma_contig_memops
;
819 q
->timestamp_type
= V4L2_BUF_FLAG_TIMESTAMP_COPY
;
820 ret
= vb2_queue_init(q
);
822 mfc_err("Failed to initialize videobuf2 queue(output)\n");
825 init_waitqueue_head(&ctx
->queue
);
826 mutex_unlock(&dev
->mfc_mutex
);
829 /* Deinit when failure occurred */
831 if (dev
->num_inst
== 1)
832 s5p_mfc_deinit_hw(dev
);
836 if (dev
->num_inst
== 1) {
837 if (s5p_mfc_power_off() < 0)
838 mfc_err("power off failed\n");
839 del_timer_sync(&dev
->watchdog_timer
);
842 s5p_mfc_dec_ctrls_delete(ctx
);
844 dev
->ctx
[ctx
->num
] = NULL
;
846 v4l2_fh_del(&ctx
->fh
);
847 v4l2_fh_exit(&ctx
->fh
);
851 mutex_unlock(&dev
->mfc_mutex
);
856 /* Release MFC context */
857 static int s5p_mfc_release(struct file
*file
)
859 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
860 struct s5p_mfc_dev
*dev
= ctx
->dev
;
863 mutex_lock(&dev
->mfc_mutex
);
865 vb2_queue_release(&ctx
->vq_src
);
866 vb2_queue_release(&ctx
->vq_dst
);
867 /* Mark context as idle */
868 clear_work_bit_irqsave(ctx
);
869 /* If instance was initialised then
870 * return instance and free resources */
871 if (ctx
->inst_no
!= MFC_NO_INSTANCE_SET
) {
872 mfc_debug(2, "Has to free instance\n");
873 ctx
->state
= MFCINST_RETURN_INST
;
874 set_work_bit_irqsave(ctx
);
875 s5p_mfc_clean_ctx_int_flags(ctx
);
876 s5p_mfc_hw_call(dev
->mfc_ops
, try_run
, dev
);
877 /* Wait until instance is returned or timeout occurred */
878 if (s5p_mfc_wait_for_done_ctx
879 (ctx
, S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET
, 0)) {
881 mfc_err("Err returning instance\n");
883 mfc_debug(2, "After free instance\n");
885 s5p_mfc_hw_call(dev
->mfc_ops
, release_codec_buffers
, ctx
);
886 s5p_mfc_hw_call(dev
->mfc_ops
, release_instance_buffer
, ctx
);
887 if (ctx
->type
== MFCINST_DECODER
)
888 s5p_mfc_hw_call(dev
->mfc_ops
, release_dec_desc_buffer
,
891 ctx
->inst_no
= MFC_NO_INSTANCE_SET
;
893 /* hardware locking scheme */
894 if (dev
->curr_ctx
== ctx
->num
)
895 clear_bit(0, &dev
->hw_lock
);
897 if (dev
->num_inst
== 0) {
898 mfc_debug(2, "Last instance\n");
899 s5p_mfc_deinit_hw(dev
);
900 del_timer_sync(&dev
->watchdog_timer
);
901 if (s5p_mfc_power_off() < 0)
902 mfc_err("Power off failed\n");
904 mfc_debug(2, "Shutting down clock\n");
906 dev
->ctx
[ctx
->num
] = NULL
;
907 s5p_mfc_dec_ctrls_delete(ctx
);
908 v4l2_fh_del(&ctx
->fh
);
909 v4l2_fh_exit(&ctx
->fh
);
912 mutex_unlock(&dev
->mfc_mutex
);
917 static unsigned int s5p_mfc_poll(struct file
*file
,
918 struct poll_table_struct
*wait
)
920 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
921 struct s5p_mfc_dev
*dev
= ctx
->dev
;
922 struct vb2_queue
*src_q
, *dst_q
;
923 struct vb2_buffer
*src_vb
= NULL
, *dst_vb
= NULL
;
927 mutex_lock(&dev
->mfc_mutex
);
928 src_q
= &ctx
->vq_src
;
929 dst_q
= &ctx
->vq_dst
;
931 * There has to be at least one buffer queued on each queued_list, which
932 * means either in driver already or waiting for driver to claim it
933 * and start processing.
935 if ((!src_q
->streaming
|| list_empty(&src_q
->queued_list
))
936 && (!dst_q
->streaming
|| list_empty(&dst_q
->queued_list
))) {
940 mutex_unlock(&dev
->mfc_mutex
);
941 poll_wait(file
, &ctx
->fh
.wait
, wait
);
942 poll_wait(file
, &src_q
->done_wq
, wait
);
943 poll_wait(file
, &dst_q
->done_wq
, wait
);
944 mutex_lock(&dev
->mfc_mutex
);
945 if (v4l2_event_pending(&ctx
->fh
))
947 spin_lock_irqsave(&src_q
->done_lock
, flags
);
948 if (!list_empty(&src_q
->done_list
))
949 src_vb
= list_first_entry(&src_q
->done_list
, struct vb2_buffer
,
951 if (src_vb
&& (src_vb
->state
== VB2_BUF_STATE_DONE
952 || src_vb
->state
== VB2_BUF_STATE_ERROR
))
953 rc
|= POLLOUT
| POLLWRNORM
;
954 spin_unlock_irqrestore(&src_q
->done_lock
, flags
);
955 spin_lock_irqsave(&dst_q
->done_lock
, flags
);
956 if (!list_empty(&dst_q
->done_list
))
957 dst_vb
= list_first_entry(&dst_q
->done_list
, struct vb2_buffer
,
959 if (dst_vb
&& (dst_vb
->state
== VB2_BUF_STATE_DONE
960 || dst_vb
->state
== VB2_BUF_STATE_ERROR
))
961 rc
|= POLLIN
| POLLRDNORM
;
962 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
964 mutex_unlock(&dev
->mfc_mutex
);
969 static int s5p_mfc_mmap(struct file
*file
, struct vm_area_struct
*vma
)
971 struct s5p_mfc_ctx
*ctx
= fh_to_ctx(file
->private_data
);
972 struct s5p_mfc_dev
*dev
= ctx
->dev
;
973 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
976 if (mutex_lock_interruptible(&dev
->mfc_mutex
))
978 if (offset
< DST_QUEUE_OFF_BASE
) {
979 mfc_debug(2, "mmaping source\n");
980 ret
= vb2_mmap(&ctx
->vq_src
, vma
);
981 } else { /* capture */
982 mfc_debug(2, "mmaping destination\n");
983 vma
->vm_pgoff
-= (DST_QUEUE_OFF_BASE
>> PAGE_SHIFT
);
984 ret
= vb2_mmap(&ctx
->vq_dst
, vma
);
986 mutex_unlock(&dev
->mfc_mutex
);
991 static const struct v4l2_file_operations s5p_mfc_fops
= {
992 .owner
= THIS_MODULE
,
993 .open
= s5p_mfc_open
,
994 .release
= s5p_mfc_release
,
995 .poll
= s5p_mfc_poll
,
996 .unlocked_ioctl
= video_ioctl2
,
997 .mmap
= s5p_mfc_mmap
,
1000 static int match_child(struct device
*dev
, void *data
)
1004 return !strcmp(dev_name(dev
), (char *)data
);
1007 static void *mfc_get_drv_data(struct platform_device
*pdev
);
1009 static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev
*dev
)
1011 unsigned int mem_info
[2] = { };
1013 dev
->mem_dev_l
= devm_kzalloc(&dev
->plat_dev
->dev
,
1014 sizeof(struct device
), GFP_KERNEL
);
1015 if (!dev
->mem_dev_l
) {
1016 mfc_err("Not enough memory\n");
1019 device_initialize(dev
->mem_dev_l
);
1020 of_property_read_u32_array(dev
->plat_dev
->dev
.of_node
,
1021 "samsung,mfc-l", mem_info
, 2);
1022 if (dma_declare_coherent_memory(dev
->mem_dev_l
, mem_info
[0],
1023 mem_info
[0], mem_info
[1],
1024 DMA_MEMORY_MAP
| DMA_MEMORY_EXCLUSIVE
) == 0) {
1025 mfc_err("Failed to declare coherent memory for\n"
1030 dev
->mem_dev_r
= devm_kzalloc(&dev
->plat_dev
->dev
,
1031 sizeof(struct device
), GFP_KERNEL
);
1032 if (!dev
->mem_dev_r
) {
1033 mfc_err("Not enough memory\n");
1036 device_initialize(dev
->mem_dev_r
);
1037 of_property_read_u32_array(dev
->plat_dev
->dev
.of_node
,
1038 "samsung,mfc-r", mem_info
, 2);
1039 if (dma_declare_coherent_memory(dev
->mem_dev_r
, mem_info
[0],
1040 mem_info
[0], mem_info
[1],
1041 DMA_MEMORY_MAP
| DMA_MEMORY_EXCLUSIVE
) == 0) {
1042 pr_err("Failed to declare coherent memory for\n"
1049 /* MFC probe function */
1050 static int s5p_mfc_probe(struct platform_device
*pdev
)
1052 struct s5p_mfc_dev
*dev
;
1053 struct video_device
*vfd
;
1054 struct resource
*res
;
1057 pr_debug("%s++\n", __func__
);
1058 dev
= devm_kzalloc(&pdev
->dev
, sizeof(*dev
), GFP_KERNEL
);
1060 dev_err(&pdev
->dev
, "Not enough memory for MFC device\n");
1064 spin_lock_init(&dev
->irqlock
);
1065 spin_lock_init(&dev
->condlock
);
1066 dev
->plat_dev
= pdev
;
1067 if (!dev
->plat_dev
) {
1068 dev_err(&pdev
->dev
, "No platform data specified\n");
1072 dev
->variant
= mfc_get_drv_data(pdev
);
1074 ret
= s5p_mfc_init_pm(dev
);
1076 dev_err(&pdev
->dev
, "failed to get mfc clock source\n");
1080 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1082 dev
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1083 if (IS_ERR(dev
->regs_base
))
1084 return PTR_ERR(dev
->regs_base
);
1086 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1088 dev_err(&pdev
->dev
, "failed to get irq resource\n");
1092 dev
->irq
= res
->start
;
1093 ret
= devm_request_irq(&pdev
->dev
, dev
->irq
, s5p_mfc_irq
,
1094 0, pdev
->name
, dev
);
1096 dev_err(&pdev
->dev
, "Failed to install irq (%d)\n", ret
);
1100 if (pdev
->dev
.of_node
) {
1101 ret
= s5p_mfc_alloc_memdevs(dev
);
1105 dev
->mem_dev_l
= device_find_child(&dev
->plat_dev
->dev
,
1106 "s5p-mfc-l", match_child
);
1107 if (!dev
->mem_dev_l
) {
1108 mfc_err("Mem child (L) device get failed\n");
1112 dev
->mem_dev_r
= device_find_child(&dev
->plat_dev
->dev
,
1113 "s5p-mfc-r", match_child
);
1114 if (!dev
->mem_dev_r
) {
1115 mfc_err("Mem child (R) device get failed\n");
1121 dev
->alloc_ctx
[0] = vb2_dma_contig_init_ctx(dev
->mem_dev_l
);
1122 if (IS_ERR(dev
->alloc_ctx
[0])) {
1123 ret
= PTR_ERR(dev
->alloc_ctx
[0]);
1126 dev
->alloc_ctx
[1] = vb2_dma_contig_init_ctx(dev
->mem_dev_r
);
1127 if (IS_ERR(dev
->alloc_ctx
[1])) {
1128 ret
= PTR_ERR(dev
->alloc_ctx
[1]);
1129 goto err_mem_init_ctx_1
;
1132 mutex_init(&dev
->mfc_mutex
);
1134 ret
= s5p_mfc_alloc_firmware(dev
);
1138 ret
= v4l2_device_register(&pdev
->dev
, &dev
->v4l2_dev
);
1140 goto err_v4l2_dev_reg
;
1141 init_waitqueue_head(&dev
->queue
);
1144 vfd
= video_device_alloc();
1146 v4l2_err(&dev
->v4l2_dev
, "Failed to allocate video device\n");
1150 vfd
->fops
= &s5p_mfc_fops
,
1151 vfd
->ioctl_ops
= get_dec_v4l2_ioctl_ops();
1152 vfd
->release
= video_device_release
,
1153 vfd
->lock
= &dev
->mfc_mutex
;
1154 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1155 vfd
->vfl_dir
= VFL_DIR_M2M
;
1156 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s", S5P_MFC_DEC_NAME
);
1158 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
1160 v4l2_err(&dev
->v4l2_dev
, "Failed to register video device\n");
1161 video_device_release(vfd
);
1164 v4l2_info(&dev
->v4l2_dev
,
1165 "decoder registered as /dev/video%d\n", vfd
->num
);
1166 video_set_drvdata(vfd
, dev
);
1169 vfd
= video_device_alloc();
1171 v4l2_err(&dev
->v4l2_dev
, "Failed to allocate video device\n");
1175 vfd
->fops
= &s5p_mfc_fops
,
1176 vfd
->ioctl_ops
= get_enc_v4l2_ioctl_ops();
1177 vfd
->release
= video_device_release
,
1178 vfd
->lock
= &dev
->mfc_mutex
;
1179 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1180 vfd
->vfl_dir
= VFL_DIR_M2M
;
1181 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s", S5P_MFC_ENC_NAME
);
1183 ret
= video_register_device(vfd
, VFL_TYPE_GRABBER
, 0);
1185 v4l2_err(&dev
->v4l2_dev
, "Failed to register video device\n");
1186 video_device_release(vfd
);
1189 v4l2_info(&dev
->v4l2_dev
,
1190 "encoder registered as /dev/video%d\n", vfd
->num
);
1191 video_set_drvdata(vfd
, dev
);
1192 platform_set_drvdata(pdev
, dev
);
1195 dev
->watchdog_workqueue
= create_singlethread_workqueue(S5P_MFC_NAME
);
1196 INIT_WORK(&dev
->watchdog_work
, s5p_mfc_watchdog_worker
);
1197 atomic_set(&dev
->watchdog_cnt
, 0);
1198 init_timer(&dev
->watchdog_timer
);
1199 dev
->watchdog_timer
.data
= (unsigned long)dev
;
1200 dev
->watchdog_timer
.function
= s5p_mfc_watchdog
;
1202 /* Initialize HW ops and commands based on MFC version */
1203 s5p_mfc_init_hw_ops(dev
);
1204 s5p_mfc_init_hw_cmds(dev
);
1206 pr_debug("%s--\n", __func__
);
1209 /* Deinit MFC if probe had failed */
1211 video_device_release(dev
->vfd_enc
);
1213 video_unregister_device(dev
->vfd_dec
);
1215 video_device_release(dev
->vfd_dec
);
1217 v4l2_device_unregister(&dev
->v4l2_dev
);
1219 s5p_mfc_release_firmware(dev
);
1221 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
[1]);
1223 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
[0]);
1225 s5p_mfc_final_pm(dev
);
1227 pr_debug("%s-- with error\n", __func__
);
1232 /* Remove the driver */
1233 static int s5p_mfc_remove(struct platform_device
*pdev
)
1235 struct s5p_mfc_dev
*dev
= platform_get_drvdata(pdev
);
1237 v4l2_info(&dev
->v4l2_dev
, "Removing %s\n", pdev
->name
);
1239 del_timer_sync(&dev
->watchdog_timer
);
1240 flush_workqueue(dev
->watchdog_workqueue
);
1241 destroy_workqueue(dev
->watchdog_workqueue
);
1243 video_unregister_device(dev
->vfd_enc
);
1244 video_unregister_device(dev
->vfd_dec
);
1245 v4l2_device_unregister(&dev
->v4l2_dev
);
1246 s5p_mfc_release_firmware(dev
);
1247 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
[0]);
1248 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
[1]);
1249 if (pdev
->dev
.of_node
) {
1250 put_device(dev
->mem_dev_l
);
1251 put_device(dev
->mem_dev_r
);
1254 s5p_mfc_final_pm(dev
);
1258 #ifdef CONFIG_PM_SLEEP
1260 static int s5p_mfc_suspend(struct device
*dev
)
1262 struct platform_device
*pdev
= to_platform_device(dev
);
1263 struct s5p_mfc_dev
*m_dev
= platform_get_drvdata(pdev
);
1266 if (m_dev
->num_inst
== 0)
1269 if (test_and_set_bit(0, &m_dev
->enter_suspend
) != 0) {
1270 mfc_err("Error: going to suspend for a second time\n");
1274 /* Check if we're processing then wait if it necessary. */
1275 while (test_and_set_bit(0, &m_dev
->hw_lock
) != 0) {
1276 /* Try and lock the HW */
1277 /* Wait on the interrupt waitqueue */
1278 ret
= wait_event_interruptible_timeout(m_dev
->queue
,
1279 m_dev
->int_cond
, msecs_to_jiffies(MFC_INT_TIMEOUT
));
1281 mfc_err("Waiting for hardware to finish timed out\n");
1286 return s5p_mfc_sleep(m_dev
);
1289 static int s5p_mfc_resume(struct device
*dev
)
1291 struct platform_device
*pdev
= to_platform_device(dev
);
1292 struct s5p_mfc_dev
*m_dev
= platform_get_drvdata(pdev
);
1294 if (m_dev
->num_inst
== 0)
1296 return s5p_mfc_wakeup(m_dev
);
1300 #ifdef CONFIG_PM_RUNTIME
1301 static int s5p_mfc_runtime_suspend(struct device
*dev
)
1303 struct platform_device
*pdev
= to_platform_device(dev
);
1304 struct s5p_mfc_dev
*m_dev
= platform_get_drvdata(pdev
);
1306 atomic_set(&m_dev
->pm
.power
, 0);
1310 static int s5p_mfc_runtime_resume(struct device
*dev
)
1312 struct platform_device
*pdev
= to_platform_device(dev
);
1313 struct s5p_mfc_dev
*m_dev
= platform_get_drvdata(pdev
);
1316 if (!m_dev
->alloc_ctx
)
1318 pre_power
= atomic_read(&m_dev
->pm
.power
);
1319 atomic_set(&m_dev
->pm
.power
, 1);
1324 /* Power management */
1325 static const struct dev_pm_ops s5p_mfc_pm_ops
= {
1326 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend
, s5p_mfc_resume
)
1327 SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend
, s5p_mfc_runtime_resume
,
1331 struct s5p_mfc_buf_size_v5 mfc_buf_size_v5
= {
1332 .h264_ctx
= MFC_H264_CTX_BUF_SIZE
,
1333 .non_h264_ctx
= MFC_CTX_BUF_SIZE
,
1334 .dsc
= DESC_BUF_SIZE
,
1335 .shm
= SHARED_BUF_SIZE
,
1338 struct s5p_mfc_buf_size buf_size_v5
= {
1340 .cpb
= MAX_CPB_SIZE
,
1341 .priv
= &mfc_buf_size_v5
,
1344 struct s5p_mfc_buf_align mfc_buf_align_v5
= {
1345 .base
= MFC_BASE_ALIGN_ORDER
,
1348 static struct s5p_mfc_variant mfc_drvdata_v5
= {
1349 .version
= MFC_VERSION
,
1350 .port_num
= MFC_NUM_PORTS
,
1351 .buf_size
= &buf_size_v5
,
1352 .buf_align
= &mfc_buf_align_v5
,
1353 .fw_name
= "s5p-mfc.fw",
1356 struct s5p_mfc_buf_size_v6 mfc_buf_size_v6
= {
1357 .dev_ctx
= MFC_CTX_BUF_SIZE_V6
,
1358 .h264_dec_ctx
= MFC_H264_DEC_CTX_BUF_SIZE_V6
,
1359 .other_dec_ctx
= MFC_OTHER_DEC_CTX_BUF_SIZE_V6
,
1360 .h264_enc_ctx
= MFC_H264_ENC_CTX_BUF_SIZE_V6
,
1361 .other_enc_ctx
= MFC_OTHER_ENC_CTX_BUF_SIZE_V6
,
1364 struct s5p_mfc_buf_size buf_size_v6
= {
1365 .fw
= MAX_FW_SIZE_V6
,
1366 .cpb
= MAX_CPB_SIZE_V6
,
1367 .priv
= &mfc_buf_size_v6
,
1370 struct s5p_mfc_buf_align mfc_buf_align_v6
= {
1374 static struct s5p_mfc_variant mfc_drvdata_v6
= {
1375 .version
= MFC_VERSION_V6
,
1376 .port_num
= MFC_NUM_PORTS_V6
,
1377 .buf_size
= &buf_size_v6
,
1378 .buf_align
= &mfc_buf_align_v6
,
1379 .fw_name
= "s5p-mfc-v6.fw",
1382 struct s5p_mfc_buf_size_v6 mfc_buf_size_v7
= {
1383 .dev_ctx
= MFC_CTX_BUF_SIZE_V7
,
1384 .h264_dec_ctx
= MFC_H264_DEC_CTX_BUF_SIZE_V7
,
1385 .other_dec_ctx
= MFC_OTHER_DEC_CTX_BUF_SIZE_V7
,
1386 .h264_enc_ctx
= MFC_H264_ENC_CTX_BUF_SIZE_V7
,
1387 .other_enc_ctx
= MFC_OTHER_ENC_CTX_BUF_SIZE_V7
,
1390 struct s5p_mfc_buf_size buf_size_v7
= {
1391 .fw
= MAX_FW_SIZE_V7
,
1392 .cpb
= MAX_CPB_SIZE_V7
,
1393 .priv
= &mfc_buf_size_v7
,
1396 struct s5p_mfc_buf_align mfc_buf_align_v7
= {
1400 static struct s5p_mfc_variant mfc_drvdata_v7
= {
1401 .version
= MFC_VERSION_V7
,
1402 .port_num
= MFC_NUM_PORTS_V7
,
1403 .buf_size
= &buf_size_v7
,
1404 .buf_align
= &mfc_buf_align_v7
,
1405 .fw_name
= "s5p-mfc-v7.fw",
1408 static struct platform_device_id mfc_driver_ids
[] = {
1411 .driver_data
= (unsigned long)&mfc_drvdata_v5
,
1413 .name
= "s5p-mfc-v5",
1414 .driver_data
= (unsigned long)&mfc_drvdata_v5
,
1416 .name
= "s5p-mfc-v6",
1417 .driver_data
= (unsigned long)&mfc_drvdata_v6
,
1419 .name
= "s5p-mfc-v7",
1420 .driver_data
= (unsigned long)&mfc_drvdata_v7
,
1424 MODULE_DEVICE_TABLE(platform
, mfc_driver_ids
);
1426 static const struct of_device_id exynos_mfc_match
[] = {
1428 .compatible
= "samsung,mfc-v5",
1429 .data
= &mfc_drvdata_v5
,
1431 .compatible
= "samsung,mfc-v6",
1432 .data
= &mfc_drvdata_v6
,
1434 .compatible
= "samsung,mfc-v7",
1435 .data
= &mfc_drvdata_v7
,
1439 MODULE_DEVICE_TABLE(of
, exynos_mfc_match
);
1441 static void *mfc_get_drv_data(struct platform_device
*pdev
)
1443 struct s5p_mfc_variant
*driver_data
= NULL
;
1445 if (pdev
->dev
.of_node
) {
1446 const struct of_device_id
*match
;
1447 match
= of_match_node(exynos_mfc_match
,
1450 driver_data
= (struct s5p_mfc_variant
*)match
->data
;
1452 driver_data
= (struct s5p_mfc_variant
*)
1453 platform_get_device_id(pdev
)->driver_data
;
1458 static struct platform_driver s5p_mfc_driver
= {
1459 .probe
= s5p_mfc_probe
,
1460 .remove
= s5p_mfc_remove
,
1461 .id_table
= mfc_driver_ids
,
1463 .name
= S5P_MFC_NAME
,
1464 .owner
= THIS_MODULE
,
1465 .pm
= &s5p_mfc_pm_ops
,
1466 .of_match_table
= exynos_mfc_match
,
1470 module_platform_driver(s5p_mfc_driver
);
1472 MODULE_LICENSE("GPL");
1473 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1474 MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");