41b4ae6e66e0dc53a66be4f2eb56a514cbc87d0f
[cascardo/linux.git] / drivers / media / video / s5p-mfc / s5p_mfc.c
1 /*
2  * Samsung S5P Multi Format Codec v 5.1
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  * Kamil Debski, <k.debski@samsung.com>
6  *
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.
11  */
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.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>
24 #include <media/videobuf2-core.h>
25 #include "s5p_mfc_common.h"
26 #include "s5p_mfc_ctrl.h"
27 #include "s5p_mfc_debug.h"
28 #include "s5p_mfc_dec.h"
29 #include "s5p_mfc_enc.h"
30 #include "s5p_mfc_intr.h"
31 #include "s5p_mfc_opr.h"
32 #include "s5p_mfc_cmd.h"
33 #include "s5p_mfc_pm.h"
34
35 #define S5P_MFC_NAME            "s5p-mfc"
36 #define S5P_MFC_DEC_NAME        "s5p-mfc-dec"
37 #define S5P_MFC_ENC_NAME        "s5p-mfc-enc"
38
39 int debug;
40 module_param(debug, int, S_IRUGO | S_IWUSR);
41 MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
42
43 /* Helper functions for interrupt processing */
44
45 /* Remove from hw execution round robin */
46 void clear_work_bit(struct s5p_mfc_ctx *ctx)
47 {
48         struct s5p_mfc_dev *dev = ctx->dev;
49
50         spin_lock(&dev->condlock);
51         __clear_bit(ctx->num, &dev->ctx_work_bits);
52         spin_unlock(&dev->condlock);
53 }
54
55 /* Add to hw execution round robin */
56 void set_work_bit(struct s5p_mfc_ctx *ctx)
57 {
58         struct s5p_mfc_dev *dev = ctx->dev;
59
60         spin_lock(&dev->condlock);
61         __set_bit(ctx->num, &dev->ctx_work_bits);
62         spin_unlock(&dev->condlock);
63 }
64
65 /* Remove from hw execution round robin */
66 void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
67 {
68         struct s5p_mfc_dev *dev = ctx->dev;
69         unsigned long flags;
70
71         spin_lock_irqsave(&dev->condlock, flags);
72         __clear_bit(ctx->num, &dev->ctx_work_bits);
73         spin_unlock_irqrestore(&dev->condlock, flags);
74 }
75
76 /* Add to hw execution round robin */
77 void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx)
78 {
79         struct s5p_mfc_dev *dev = ctx->dev;
80         unsigned long flags;
81
82         spin_lock_irqsave(&dev->condlock, flags);
83         __set_bit(ctx->num, &dev->ctx_work_bits);
84         spin_unlock_irqrestore(&dev->condlock, flags);
85 }
86
87 /* Wake up context wait_queue */
88 static void wake_up_ctx(struct s5p_mfc_ctx *ctx, unsigned int reason,
89                         unsigned int err)
90 {
91         ctx->int_cond = 1;
92         ctx->int_type = reason;
93         ctx->int_err = err;
94         wake_up(&ctx->queue);
95 }
96
97 /* Wake up device wait_queue */
98 static void wake_up_dev(struct s5p_mfc_dev *dev, unsigned int reason,
99                         unsigned int err)
100 {
101         dev->int_cond = 1;
102         dev->int_type = reason;
103         dev->int_err = err;
104         wake_up(&dev->queue);
105 }
106
107 static void s5p_mfc_watchdog(unsigned long arg)
108 {
109         struct s5p_mfc_dev *dev = (struct s5p_mfc_dev *)arg;
110
111         if (test_bit(0, &dev->hw_lock))
112                 atomic_inc(&dev->watchdog_cnt);
113         if (atomic_read(&dev->watchdog_cnt) >= MFC_WATCHDOG_CNT) {
114                 /* This means that hw is busy and no interrupts were
115                  * generated by hw for the Nth time of running this
116                  * watchdog timer. This usually means a serious hw
117                  * error. Now it is time to kill all instances and
118                  * reset the MFC. */
119                 mfc_err("Time out during waiting for HW\n");
120                 queue_work(dev->watchdog_workqueue, &dev->watchdog_work);
121         }
122         dev->watchdog_timer.expires = jiffies +
123                                         msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
124         add_timer(&dev->watchdog_timer);
125 }
126
127 static void s5p_mfc_watchdog_worker(struct work_struct *work)
128 {
129         struct s5p_mfc_dev *dev;
130         struct s5p_mfc_ctx *ctx;
131         unsigned long flags;
132         int mutex_locked;
133         int i, ret;
134
135         dev = container_of(work, struct s5p_mfc_dev, watchdog_work);
136
137         mfc_err("Driver timeout error handling\n");
138         /* Lock the mutex that protects open and release.
139          * This is necessary as they may load and unload firmware. */
140         mutex_locked = mutex_trylock(&dev->mfc_mutex);
141         if (!mutex_locked)
142                 mfc_err("Error: some instance may be closing/opening\n");
143         spin_lock_irqsave(&dev->irqlock, flags);
144
145         s5p_mfc_clock_off();
146
147         for (i = 0; i < MFC_NUM_CONTEXTS; i++) {
148                 ctx = dev->ctx[i];
149                 if (!ctx)
150                         continue;
151                 ctx->state = MFCINST_ERROR;
152                 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
153                                 &ctx->vq_dst);
154                 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
155                                 &ctx->vq_src);
156                 clear_work_bit(ctx);
157                 wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
158         }
159         clear_bit(0, &dev->hw_lock);
160         spin_unlock_irqrestore(&dev->irqlock, flags);
161         /* Double check if there is at least one instance running.
162          * If no instance is in memory than no firmware should be present */
163         if (dev->num_inst > 0) {
164                 ret = s5p_mfc_reload_firmware(dev);
165                 if (ret) {
166                         mfc_err("Failed to reload FW\n");
167                         goto unlock;
168                 }
169                 s5p_mfc_clock_on();
170                 ret = s5p_mfc_init_hw(dev);
171                 if (ret)
172                         mfc_err("Failed to reinit FW\n");
173         }
174 unlock:
175         if (mutex_locked)
176                 mutex_unlock(&dev->mfc_mutex);
177 }
178
179 static enum s5p_mfc_node_type s5p_mfc_get_node_type(struct file *file)
180 {
181         struct video_device *vdev = video_devdata(file);
182
183         if (!vdev) {
184                 mfc_err("failed to get video_device");
185                 return MFCNODE_INVALID;
186         }
187         if (vdev->index == 0)
188                 return MFCNODE_DECODER;
189         else if (vdev->index == 1)
190                 return MFCNODE_ENCODER;
191         return MFCNODE_INVALID;
192 }
193
194 static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
195 {
196         mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
197         mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
198         mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
199 }
200
201 static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
202 {
203         struct s5p_mfc_buf *dst_buf;
204         struct s5p_mfc_dev *dev = ctx->dev;
205
206         ctx->state = MFCINST_FINISHED;
207         ctx->sequence++;
208         while (!list_empty(&ctx->dst_queue)) {
209                 dst_buf = list_entry(ctx->dst_queue.next,
210                                      struct s5p_mfc_buf, list);
211                 mfc_debug(2, "Cleaning up buffer: %d\n",
212                                           dst_buf->b->v4l2_buf.index);
213                 vb2_set_plane_payload(dst_buf->b, 0, 0);
214                 vb2_set_plane_payload(dst_buf->b, 1, 0);
215                 list_del(&dst_buf->list);
216                 ctx->dst_queue_cnt--;
217                 dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
218
219                 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
220                         s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
221                         dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
222                 else
223                         dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
224
225                 ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
226                 vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
227         }
228 }
229
230 static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
231 {
232         struct s5p_mfc_dev *dev = ctx->dev;
233         struct s5p_mfc_buf  *dst_buf, *src_buf;
234         size_t dec_y_addr;
235         unsigned int frame_type;
236
237         dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
238         frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
239
240         /* Copy timestamp / timecode from decoded src to dst and set
241            appropraite flags */
242         src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
243         list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
244                 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
245                         memcpy(&dst_buf->b->v4l2_buf.timecode,
246                                 &src_buf->b->v4l2_buf.timecode,
247                                 sizeof(struct v4l2_timecode));
248                         memcpy(&dst_buf->b->v4l2_buf.timestamp,
249                                 &src_buf->b->v4l2_buf.timestamp,
250                                 sizeof(struct timeval));
251                         switch (frame_type) {
252                         case S5P_FIMV_DECODE_FRAME_I_FRAME:
253                                 dst_buf->b->v4l2_buf.flags |=
254                                                 V4L2_BUF_FLAG_KEYFRAME;
255                                 break;
256                         case S5P_FIMV_DECODE_FRAME_P_FRAME:
257                                 dst_buf->b->v4l2_buf.flags |=
258                                                 V4L2_BUF_FLAG_PFRAME;
259                                 break;
260                         case S5P_FIMV_DECODE_FRAME_B_FRAME:
261                                 dst_buf->b->v4l2_buf.flags |=
262                                                 V4L2_BUF_FLAG_BFRAME;
263                                 break;
264                         }
265                         break;
266                 }
267         }
268 }
269
270 static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
271 {
272         struct s5p_mfc_dev *dev = ctx->dev;
273         struct s5p_mfc_buf  *dst_buf;
274         size_t dspl_y_addr;
275         unsigned int frame_type;
276         unsigned int index;
277
278         dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
279         frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
280
281         /* If frame is same as previous then skip and do not dequeue */
282         if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
283                 if (!ctx->after_packed_pb)
284                         ctx->sequence++;
285                 ctx->after_packed_pb = 0;
286                 return;
287         }
288         ctx->sequence++;
289         /* The MFC returns address of the buffer, now we have to
290          * check which videobuf does it correspond to */
291         list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
292                 /* Check if this is the buffer we're looking for */
293                 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
294                         list_del(&dst_buf->list);
295                         ctx->dst_queue_cnt--;
296                         dst_buf->b->v4l2_buf.sequence = ctx->sequence;
297                         if (s5p_mfc_hw_call(dev->mfc_ops,
298                                         get_pic_type_top, ctx) ==
299                                 s5p_mfc_hw_call(dev->mfc_ops,
300                                         get_pic_type_bot, ctx))
301                                 dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
302                         else
303                                 dst_buf->b->v4l2_buf.field =
304                                                         V4L2_FIELD_INTERLACED;
305                         vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
306                         vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
307                         clear_bit(dst_buf->b->v4l2_buf.index,
308                                                         &ctx->dec_dst_flag);
309
310                         vb2_buffer_done(dst_buf->b,
311                                 err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
312
313                         index = dst_buf->b->v4l2_buf.index;
314                         break;
315                 }
316         }
317 }
318
319 /* Handle frame decoding interrupt */
320 static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
321                                         unsigned int reason, unsigned int err)
322 {
323         struct s5p_mfc_dev *dev = ctx->dev;
324         unsigned int dst_frame_status;
325         struct s5p_mfc_buf *src_buf;
326         unsigned long flags;
327         unsigned int res_change;
328
329         unsigned int index;
330
331         dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
332                                 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
333         res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
334                                 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
335                                 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
336         mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
337         if (ctx->state == MFCINST_RES_CHANGE_INIT)
338                 ctx->state = MFCINST_RES_CHANGE_FLUSH;
339         if (res_change == S5P_FIMV_RES_INCREASE ||
340                 res_change == S5P_FIMV_RES_DECREASE) {
341                 ctx->state = MFCINST_RES_CHANGE_INIT;
342                 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
343                 wake_up_ctx(ctx, reason, err);
344                 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
345                         BUG();
346                 s5p_mfc_clock_off();
347                 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
348                 return;
349         }
350         if (ctx->dpb_flush_flag)
351                 ctx->dpb_flush_flag = 0;
352
353         spin_lock_irqsave(&dev->irqlock, flags);
354         /* All frames remaining in the buffer have been extracted  */
355         if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
356                 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
357                         s5p_mfc_handle_frame_all_extracted(ctx);
358                         ctx->state = MFCINST_RES_CHANGE_END;
359                         goto leave_handle_frame;
360                 } else {
361                         s5p_mfc_handle_frame_all_extracted(ctx);
362                 }
363         }
364
365         if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY ||
366                 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_ONLY)
367                 s5p_mfc_handle_frame_copy_time(ctx);
368
369         /* A frame has been decoded and is in the buffer  */
370         if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
371             dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
372                 s5p_mfc_handle_frame_new(ctx, err);
373         } else {
374                 mfc_debug(2, "No frame decode\n");
375         }
376         /* Mark source buffer as complete */
377         if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
378                 && !list_empty(&ctx->src_queue)) {
379                 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
380                                                                 list);
381                 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
382                                                 get_consumed_stream, dev);
383                 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
384                         ctx->consumed_stream + STUFF_BYTE <
385                         src_buf->b->v4l2_planes[0].bytesused) {
386                         /* Run MFC again on the same buffer */
387                         mfc_debug(2, "Running again the same buffer\n");
388                         ctx->after_packed_pb = 1;
389                 } else {
390                         index = src_buf->b->v4l2_buf.index;
391                         mfc_debug(2, "MFC needs next buffer\n");
392                         ctx->consumed_stream = 0;
393                         list_del(&src_buf->list);
394                         ctx->src_queue_cnt--;
395                         if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
396                                 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
397                         else
398                                 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
399                 }
400         }
401 leave_handle_frame:
402         spin_unlock_irqrestore(&dev->irqlock, flags);
403         if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
404                                     || ctx->dst_queue_cnt < ctx->dpb_count)
405                 clear_work_bit(ctx);
406         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
407         wake_up_ctx(ctx, reason, err);
408         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
409                 BUG();
410         s5p_mfc_clock_off();
411         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
412 }
413
414 /* Error handling for interrupt */
415 static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
416                 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
417 {
418         unsigned long flags;
419
420         mfc_err("Interrupt Error: %08x\n", err);
421
422         if (ctx != NULL) {
423                 /* Error recovery is dependent on the state of context */
424                 switch (ctx->state) {
425                 case MFCINST_RES_CHANGE_INIT:
426                 case MFCINST_RES_CHANGE_FLUSH:
427                 case MFCINST_RES_CHANGE_END:
428                 case MFCINST_FINISHING:
429                 case MFCINST_FINISHED:
430                 case MFCINST_RUNNING:
431                         /* It is higly probable that an error occured
432                          * while decoding a frame */
433                         clear_work_bit(ctx);
434                         ctx->state = MFCINST_ERROR;
435                         /* Mark all dst buffers as having an error */
436                         spin_lock_irqsave(&dev->irqlock, flags);
437                         s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue,
438                                                 &ctx->dst_queue, &ctx->vq_dst);
439                         /* Mark all src buffers as having an error */
440                         s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue,
441                                                 &ctx->src_queue, &ctx->vq_src);
442                         spin_unlock_irqrestore(&dev->irqlock, flags);
443                         wake_up_ctx(ctx, reason, err);
444                         break;
445                 default:
446                         clear_work_bit(ctx);
447                         ctx->state = MFCINST_ERROR;
448                         wake_up_ctx(ctx, reason, err);
449                         break;
450                 }
451         }
452         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
453                 BUG();
454         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
455         s5p_mfc_clock_off();
456         wake_up_dev(dev, reason, err);
457         return;
458 }
459
460 /* Header parsing interrupt handling */
461 static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
462                                  unsigned int reason, unsigned int err)
463 {
464         struct s5p_mfc_dev *dev;
465
466         if (ctx == NULL)
467                 return;
468         dev = ctx->dev;
469         if (ctx->c_ops->post_seq_start) {
470                 if (ctx->c_ops->post_seq_start(ctx))
471                         mfc_err("post_seq_start() failed\n");
472         } else {
473                 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
474                                 dev);
475                 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
476                                 dev);
477
478                 s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
479
480                 ctx->dpb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
481                                 dev);
482                 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
483                                 dev);
484                 if (ctx->img_width == 0 || ctx->img_height == 0)
485                         ctx->state = MFCINST_ERROR;
486                 else
487                         ctx->state = MFCINST_HEAD_PARSED;
488
489                 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
490                         ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
491                                 !list_empty(&ctx->src_queue)) {
492                         struct s5p_mfc_buf *src_buf;
493                         src_buf = list_entry(ctx->src_queue.next,
494                                         struct s5p_mfc_buf, list);
495                         if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
496                                                 dev) <
497                                         src_buf->b->v4l2_planes[0].bytesused)
498                                 ctx->head_processed = 0;
499                         else
500                                 ctx->head_processed = 1;
501                 } else {
502                         ctx->head_processed = 1;
503                 }
504         }
505         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
506         clear_work_bit(ctx);
507         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
508                 BUG();
509         s5p_mfc_clock_off();
510         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
511         wake_up_ctx(ctx, reason, err);
512 }
513
514 /* Header parsing interrupt handling */
515 static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
516                                  unsigned int reason, unsigned int err)
517 {
518         struct s5p_mfc_buf *src_buf;
519         struct s5p_mfc_dev *dev;
520         unsigned long flags;
521
522         if (ctx == NULL)
523                 return;
524         dev = ctx->dev;
525         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
526         ctx->int_type = reason;
527         ctx->int_err = err;
528         ctx->int_cond = 1;
529         clear_work_bit(ctx);
530         if (err == 0) {
531                 ctx->state = MFCINST_RUNNING;
532                 if (!ctx->dpb_flush_flag && ctx->head_processed) {
533                         spin_lock_irqsave(&dev->irqlock, flags);
534                         if (!list_empty(&ctx->src_queue)) {
535                                 src_buf = list_entry(ctx->src_queue.next,
536                                              struct s5p_mfc_buf, list);
537                                 list_del(&src_buf->list);
538                                 ctx->src_queue_cnt--;
539                                 vb2_buffer_done(src_buf->b,
540                                                 VB2_BUF_STATE_DONE);
541                         }
542                         spin_unlock_irqrestore(&dev->irqlock, flags);
543                 } else {
544                         ctx->dpb_flush_flag = 0;
545                 }
546                 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
547                         BUG();
548
549                 s5p_mfc_clock_off();
550
551                 wake_up(&ctx->queue);
552                 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
553         } else {
554                 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
555                         BUG();
556
557                 s5p_mfc_clock_off();
558
559                 wake_up(&ctx->queue);
560         }
561 }
562
563 static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
564                                  unsigned int reason, unsigned int err)
565 {
566         struct s5p_mfc_dev *dev = ctx->dev;
567         struct s5p_mfc_buf *mb_entry;
568
569         mfc_debug(2, "Stream completed");
570
571         s5p_mfc_clear_int_flags(dev);
572         ctx->int_type = reason;
573         ctx->int_err = err;
574         ctx->state = MFCINST_FINISHED;
575
576         spin_lock(&dev->irqlock);
577         if (!list_empty(&ctx->dst_queue)) {
578                 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
579                                                                         list);
580                 list_del(&mb_entry->list);
581                 ctx->dst_queue_cnt--;
582                 vb2_set_plane_payload(mb_entry->b, 0, 0);
583                 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
584         }
585         spin_unlock(&dev->irqlock);
586
587         clear_work_bit(ctx);
588
589         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
590                 WARN_ON(1);
591
592         s5p_mfc_clock_off();
593         wake_up(&ctx->queue);
594         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
595 }
596
597 /* Interrupt processing */
598 static irqreturn_t s5p_mfc_irq(int irq, void *priv)
599 {
600         struct s5p_mfc_dev *dev = priv;
601         struct s5p_mfc_ctx *ctx;
602         unsigned int reason;
603         unsigned int err;
604
605         mfc_debug_enter();
606         /* Reset the timeout watchdog */
607         atomic_set(&dev->watchdog_cnt, 0);
608         ctx = dev->ctx[dev->curr_ctx];
609         /* Get the reason of interrupt and the error code */
610         reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
611         err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
612         mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
613         switch (reason) {
614         case S5P_MFC_R2H_CMD_ERR_RET:
615                 /* An error has occured */
616                 if (ctx->state == MFCINST_RUNNING &&
617                         s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
618                                 dev->warn_start)
619                         s5p_mfc_handle_frame(ctx, reason, err);
620                 else
621                         s5p_mfc_handle_error(dev, ctx, reason, err);
622                 clear_bit(0, &dev->enter_suspend);
623                 break;
624
625         case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
626         case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
627         case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
628                 if (ctx->c_ops->post_frame_start) {
629                         if (ctx->c_ops->post_frame_start(ctx))
630                                 mfc_err("post_frame_start() failed\n");
631                         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
632                         wake_up_ctx(ctx, reason, err);
633                         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
634                                 BUG();
635                         s5p_mfc_clock_off();
636                         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
637                 } else {
638                         s5p_mfc_handle_frame(ctx, reason, err);
639                 }
640                 break;
641
642         case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
643                 s5p_mfc_handle_seq_done(ctx, reason, err);
644                 break;
645
646         case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
647                 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
648                 ctx->state = MFCINST_GOT_INST;
649                 clear_work_bit(ctx);
650                 wake_up(&ctx->queue);
651                 goto irq_cleanup_hw;
652
653         case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
654                 clear_work_bit(ctx);
655                 ctx->state = MFCINST_FREE;
656                 wake_up(&ctx->queue);
657                 goto irq_cleanup_hw;
658
659         case S5P_MFC_R2H_CMD_SYS_INIT_RET:
660         case S5P_MFC_R2H_CMD_FW_STATUS_RET:
661         case S5P_MFC_R2H_CMD_SLEEP_RET:
662         case S5P_MFC_R2H_CMD_WAKEUP_RET:
663                 if (ctx)
664                         clear_work_bit(ctx);
665                 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
666                 wake_up_dev(dev, reason, err);
667                 clear_bit(0, &dev->hw_lock);
668                 clear_bit(0, &dev->enter_suspend);
669                 break;
670
671         case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
672                 s5p_mfc_handle_init_buffers(ctx, reason, err);
673                 break;
674
675         case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
676                 s5p_mfc_handle_stream_complete(ctx, reason, err);
677                 break;
678
679         default:
680                 mfc_debug(2, "Unknown int reason\n");
681                 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
682         }
683         mfc_debug_leave();
684         return IRQ_HANDLED;
685 irq_cleanup_hw:
686         s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
687         ctx->int_type = reason;
688         ctx->int_err = err;
689         ctx->int_cond = 1;
690         if (test_and_clear_bit(0, &dev->hw_lock) == 0)
691                 mfc_err("Failed to unlock hw\n");
692
693         s5p_mfc_clock_off();
694
695         s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
696         mfc_debug(2, "Exit via irq_cleanup_hw\n");
697         return IRQ_HANDLED;
698 }
699
700 /* Open an MFC node */
701 static int s5p_mfc_open(struct file *file)
702 {
703         struct s5p_mfc_dev *dev = video_drvdata(file);
704         struct s5p_mfc_ctx *ctx = NULL;
705         struct vb2_queue *q;
706         int ret = 0;
707
708         mfc_debug_enter();
709         dev->num_inst++;        /* It is guarded by mfc_mutex in vfd */
710         /* Allocate memory for context */
711         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
712         if (!ctx) {
713                 mfc_err("Not enough memory\n");
714                 ret = -ENOMEM;
715                 goto err_alloc;
716         }
717         v4l2_fh_init(&ctx->fh, video_devdata(file));
718         file->private_data = &ctx->fh;
719         v4l2_fh_add(&ctx->fh);
720         ctx->dev = dev;
721         INIT_LIST_HEAD(&ctx->src_queue);
722         INIT_LIST_HEAD(&ctx->dst_queue);
723         ctx->src_queue_cnt = 0;
724         ctx->dst_queue_cnt = 0;
725         /* Get context number */
726         ctx->num = 0;
727         while (dev->ctx[ctx->num]) {
728                 ctx->num++;
729                 if (ctx->num >= MFC_NUM_CONTEXTS) {
730                         mfc_err("Too many open contexts\n");
731                         ret = -EBUSY;
732                         goto err_no_ctx;
733                 }
734         }
735         /* Mark context as idle */
736         clear_work_bit_irqsave(ctx);
737         dev->ctx[ctx->num] = ctx;
738         if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
739                 ctx->type = MFCINST_DECODER;
740                 ctx->c_ops = get_dec_codec_ops();
741                 s5p_mfc_dec_init(ctx);
742                 /* Setup ctrl handler */
743                 ret = s5p_mfc_dec_ctrls_setup(ctx);
744                 if (ret) {
745                         mfc_err("Failed to setup mfc controls\n");
746                         goto err_ctrls_setup;
747                 }
748         } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
749                 ctx->type = MFCINST_ENCODER;
750                 ctx->c_ops = get_enc_codec_ops();
751                 /* only for encoder */
752                 INIT_LIST_HEAD(&ctx->ref_queue);
753                 ctx->ref_queue_cnt = 0;
754                 s5p_mfc_enc_init(ctx);
755                 /* Setup ctrl handler */
756                 ret = s5p_mfc_enc_ctrls_setup(ctx);
757                 if (ret) {
758                         mfc_err("Failed to setup mfc controls\n");
759                         goto err_ctrls_setup;
760                 }
761         } else {
762                 ret = -ENOENT;
763                 goto err_bad_node;
764         }
765         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
766         ctx->inst_no = -1;
767         /* Load firmware if this is the first instance */
768         if (dev->num_inst == 1) {
769                 dev->watchdog_timer.expires = jiffies +
770                                         msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
771                 add_timer(&dev->watchdog_timer);
772                 ret = s5p_mfc_power_on();
773                 if (ret < 0) {
774                         mfc_err("power on failed\n");
775                         goto err_pwr_enable;
776                 }
777                 s5p_mfc_clock_on();
778                 ret = s5p_mfc_alloc_and_load_firmware(dev);
779                 if (ret)
780                         goto err_alloc_fw;
781                 /* Init the FW */
782                 ret = s5p_mfc_init_hw(dev);
783                 if (ret)
784                         goto err_init_hw;
785                 s5p_mfc_clock_off();
786         }
787         /* Init videobuf2 queue for CAPTURE */
788         q = &ctx->vq_dst;
789         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
790         q->drv_priv = &ctx->fh;
791         if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
792                 q->io_modes = VB2_MMAP;
793                 q->ops = get_dec_queue_ops();
794         } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
795                 q->io_modes = VB2_MMAP | VB2_USERPTR;
796                 q->ops = get_enc_queue_ops();
797         } else {
798                 ret = -ENOENT;
799                 goto err_queue_init;
800         }
801         q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
802         ret = vb2_queue_init(q);
803         if (ret) {
804                 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
805                 goto err_queue_init;
806         }
807         /* Init videobuf2 queue for OUTPUT */
808         q = &ctx->vq_src;
809         q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
810         q->io_modes = VB2_MMAP;
811         q->drv_priv = &ctx->fh;
812         if (s5p_mfc_get_node_type(file) == MFCNODE_DECODER) {
813                 q->io_modes = VB2_MMAP;
814                 q->ops = get_dec_queue_ops();
815         } else if (s5p_mfc_get_node_type(file) == MFCNODE_ENCODER) {
816                 q->io_modes = VB2_MMAP | VB2_USERPTR;
817                 q->ops = get_enc_queue_ops();
818         } else {
819                 ret = -ENOENT;
820                 goto err_queue_init;
821         }
822         q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
823         ret = vb2_queue_init(q);
824         if (ret) {
825                 mfc_err("Failed to initialize videobuf2 queue(output)\n");
826                 goto err_queue_init;
827         }
828         init_waitqueue_head(&ctx->queue);
829         mfc_debug_leave();
830         return ret;
831         /* Deinit when failure occured */
832 err_queue_init:
833 err_init_hw:
834         s5p_mfc_release_firmware(dev);
835 err_alloc_fw:
836         dev->ctx[ctx->num] = NULL;
837         del_timer_sync(&dev->watchdog_timer);
838         s5p_mfc_clock_off();
839 err_pwr_enable:
840         if (dev->num_inst == 1) {
841                 if (s5p_mfc_power_off() < 0)
842                         mfc_err("power off failed\n");
843                 s5p_mfc_release_firmware(dev);
844         }
845 err_ctrls_setup:
846         s5p_mfc_dec_ctrls_delete(ctx);
847 err_bad_node:
848 err_no_ctx:
849         v4l2_fh_del(&ctx->fh);
850         v4l2_fh_exit(&ctx->fh);
851         kfree(ctx);
852 err_alloc:
853         dev->num_inst--;
854         mfc_debug_leave();
855         return ret;
856 }
857
858 /* Release MFC context */
859 static int s5p_mfc_release(struct file *file)
860 {
861         struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
862         struct s5p_mfc_dev *dev = ctx->dev;
863
864         mfc_debug_enter();
865         s5p_mfc_clock_on();
866         vb2_queue_release(&ctx->vq_src);
867         vb2_queue_release(&ctx->vq_dst);
868         /* Mark context as idle */
869         clear_work_bit_irqsave(ctx);
870         /* If instance was initialised then
871          * return instance and free reosurces */
872         if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
873                 mfc_debug(2, "Has to free instance\n");
874                 ctx->state = MFCINST_RETURN_INST;
875                 set_work_bit_irqsave(ctx);
876                 s5p_mfc_clean_ctx_int_flags(ctx);
877                 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
878                 /* Wait until instance is returned or timeout occured */
879                 if (s5p_mfc_wait_for_done_ctx
880                     (ctx, S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
881                         s5p_mfc_clock_off();
882                         mfc_err("Err returning instance\n");
883                 }
884                 mfc_debug(2, "After free instance\n");
885                 /* Free resources */
886                 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
887                 s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
888                 if (ctx->type == MFCINST_DECODER)
889                         s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer,
890                                         ctx);
891
892                 ctx->inst_no = MFC_NO_INSTANCE_SET;
893         }
894         /* hardware locking scheme */
895         if (dev->curr_ctx == ctx->num)
896                 clear_bit(0, &dev->hw_lock);
897         dev->num_inst--;
898         if (dev->num_inst == 0) {
899                 mfc_debug(2, "Last instance - release firmware\n");
900                 /* reset <-> F/W release */
901                 s5p_mfc_reset(dev);
902                 s5p_mfc_deinit_hw(dev);
903                 s5p_mfc_release_firmware(dev);
904                 del_timer_sync(&dev->watchdog_timer);
905                 if (s5p_mfc_power_off() < 0)
906                         mfc_err("Power off failed\n");
907         }
908         mfc_debug(2, "Shutting down clock\n");
909         s5p_mfc_clock_off();
910         dev->ctx[ctx->num] = NULL;
911         s5p_mfc_dec_ctrls_delete(ctx);
912         v4l2_fh_del(&ctx->fh);
913         v4l2_fh_exit(&ctx->fh);
914         kfree(ctx);
915         mfc_debug_leave();
916         return 0;
917 }
918
919 /* Poll */
920 static unsigned int s5p_mfc_poll(struct file *file,
921                                  struct poll_table_struct *wait)
922 {
923         struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
924         struct vb2_queue *src_q, *dst_q;
925         struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
926         unsigned int rc = 0;
927         unsigned long flags;
928
929         src_q = &ctx->vq_src;
930         dst_q = &ctx->vq_dst;
931         /*
932          * There has to be at least one buffer queued on each queued_list, which
933          * means either in driver already or waiting for driver to claim it
934          * and start processing.
935          */
936         if ((!src_q->streaming || list_empty(&src_q->queued_list))
937                 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
938                 rc = POLLERR;
939                 goto end;
940         }
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         if (v4l2_event_pending(&ctx->fh))
945                 rc |= POLLPRI;
946         spin_lock_irqsave(&src_q->done_lock, flags);
947         if (!list_empty(&src_q->done_list))
948                 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
949                                                                 done_entry);
950         if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
951                                 || src_vb->state == VB2_BUF_STATE_ERROR))
952                 rc |= POLLOUT | POLLWRNORM;
953         spin_unlock_irqrestore(&src_q->done_lock, flags);
954         spin_lock_irqsave(&dst_q->done_lock, flags);
955         if (!list_empty(&dst_q->done_list))
956                 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
957                                                                 done_entry);
958         if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
959                                 || dst_vb->state == VB2_BUF_STATE_ERROR))
960                 rc |= POLLIN | POLLRDNORM;
961         spin_unlock_irqrestore(&dst_q->done_lock, flags);
962 end:
963         return rc;
964 }
965
966 /* Mmap */
967 static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
968 {
969         struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
970         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
971         int ret;
972
973         if (offset < DST_QUEUE_OFF_BASE) {
974                 mfc_debug(2, "mmaping source\n");
975                 ret = vb2_mmap(&ctx->vq_src, vma);
976         } else {                /* capture */
977                 mfc_debug(2, "mmaping destination\n");
978                 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
979                 ret = vb2_mmap(&ctx->vq_dst, vma);
980         }
981         return ret;
982 }
983
984 /* v4l2 ops */
985 static const struct v4l2_file_operations s5p_mfc_fops = {
986         .owner = THIS_MODULE,
987         .open = s5p_mfc_open,
988         .release = s5p_mfc_release,
989         .poll = s5p_mfc_poll,
990         .unlocked_ioctl = video_ioctl2,
991         .mmap = s5p_mfc_mmap,
992 };
993
994 static int match_child(struct device *dev, void *data)
995 {
996         if (!dev_name(dev))
997                 return 0;
998         return !strcmp(dev_name(dev), (char *)data);
999 }
1000
1001 /* MFC probe function */
1002 static int s5p_mfc_probe(struct platform_device *pdev)
1003 {
1004         struct s5p_mfc_dev *dev;
1005         struct video_device *vfd;
1006         struct resource *res;
1007         int ret;
1008
1009         pr_debug("%s++\n", __func__);
1010         dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1011         if (!dev) {
1012                 dev_err(&pdev->dev, "Not enough memory for MFC device\n");
1013                 return -ENOMEM;
1014         }
1015
1016         spin_lock_init(&dev->irqlock);
1017         spin_lock_init(&dev->condlock);
1018         dev->plat_dev = pdev;
1019         if (!dev->plat_dev) {
1020                 dev_err(&pdev->dev, "No platform data specified\n");
1021                 return -ENODEV;
1022         }
1023
1024         dev->variant = (struct s5p_mfc_variant *)
1025                 platform_get_device_id(pdev)->driver_data;
1026
1027         ret = s5p_mfc_init_pm(dev);
1028         if (ret < 0) {
1029                 dev_err(&pdev->dev, "failed to get mfc clock source\n");
1030                 return ret;
1031         }
1032
1033         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1034
1035         dev->regs_base = devm_request_and_ioremap(&pdev->dev, res);
1036         if (dev->regs_base == NULL) {
1037                 dev_err(&pdev->dev, "Failed to obtain io memory\n");
1038                 return -ENOENT;
1039         }
1040
1041         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1042         if (res == NULL) {
1043                 dev_err(&pdev->dev, "failed to get irq resource\n");
1044                 ret = -ENOENT;
1045                 goto err_res;
1046         }
1047         dev->irq = res->start;
1048         ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
1049                                         IRQF_DISABLED, pdev->name, dev);
1050         if (ret) {
1051                 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
1052                 goto err_res;
1053         }
1054
1055         dev->mem_dev_l = device_find_child(&dev->plat_dev->dev, "s5p-mfc-l",
1056                                            match_child);
1057         if (!dev->mem_dev_l) {
1058                 mfc_err("Mem child (L) device get failed\n");
1059                 ret = -ENODEV;
1060                 goto err_res;
1061         }
1062
1063         dev->mem_dev_r = device_find_child(&dev->plat_dev->dev, "s5p-mfc-r",
1064                                            match_child);
1065         if (!dev->mem_dev_r) {
1066                 mfc_err("Mem child (R) device get failed\n");
1067                 ret = -ENODEV;
1068                 goto err_res;
1069         }
1070
1071         dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
1072         if (IS_ERR_OR_NULL(dev->alloc_ctx[0])) {
1073                 ret = PTR_ERR(dev->alloc_ctx[0]);
1074                 goto err_res;
1075         }
1076         dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
1077         if (IS_ERR_OR_NULL(dev->alloc_ctx[1])) {
1078                 ret = PTR_ERR(dev->alloc_ctx[1]);
1079                 goto err_mem_init_ctx_1;
1080         }
1081
1082         mutex_init(&dev->mfc_mutex);
1083
1084         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1085         if (ret)
1086                 goto err_v4l2_dev_reg;
1087         init_waitqueue_head(&dev->queue);
1088
1089         /* decoder */
1090         vfd = video_device_alloc();
1091         if (!vfd) {
1092                 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1093                 ret = -ENOMEM;
1094                 goto err_dec_alloc;
1095         }
1096         vfd->fops       = &s5p_mfc_fops,
1097         vfd->ioctl_ops  = get_dec_v4l2_ioctl_ops();
1098         vfd->release    = video_device_release,
1099         vfd->lock       = &dev->mfc_mutex;
1100         vfd->v4l2_dev   = &dev->v4l2_dev;
1101         snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1102         dev->vfd_dec    = vfd;
1103         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1104         if (ret) {
1105                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1106                 video_device_release(vfd);
1107                 goto err_dec_reg;
1108         }
1109         v4l2_info(&dev->v4l2_dev,
1110                   "decoder registered as /dev/video%d\n", vfd->num);
1111         video_set_drvdata(vfd, dev);
1112
1113         /* encoder */
1114         vfd = video_device_alloc();
1115         if (!vfd) {
1116                 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1117                 ret = -ENOMEM;
1118                 goto err_enc_alloc;
1119         }
1120         vfd->fops       = &s5p_mfc_fops,
1121         vfd->ioctl_ops  = get_enc_v4l2_ioctl_ops();
1122         vfd->release    = video_device_release,
1123         vfd->lock       = &dev->mfc_mutex;
1124         vfd->v4l2_dev   = &dev->v4l2_dev;
1125         snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1126         dev->vfd_enc    = vfd;
1127         ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1128         if (ret) {
1129                 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1130                 video_device_release(vfd);
1131                 goto err_enc_reg;
1132         }
1133         v4l2_info(&dev->v4l2_dev,
1134                   "encoder registered as /dev/video%d\n", vfd->num);
1135         video_set_drvdata(vfd, dev);
1136         platform_set_drvdata(pdev, dev);
1137
1138         dev->hw_lock = 0;
1139         dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
1140         INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1141         atomic_set(&dev->watchdog_cnt, 0);
1142         init_timer(&dev->watchdog_timer);
1143         dev->watchdog_timer.data = (unsigned long)dev;
1144         dev->watchdog_timer.function = s5p_mfc_watchdog;
1145
1146         /* Initialize HW ops and commands based on MFC version */
1147         s5p_mfc_init_hw_ops(dev);
1148         s5p_mfc_init_hw_cmds(dev);
1149
1150         pr_debug("%s--\n", __func__);
1151         return 0;
1152
1153 /* Deinit MFC if probe had failed */
1154 err_enc_reg:
1155         video_device_release(dev->vfd_enc);
1156 err_enc_alloc:
1157         video_unregister_device(dev->vfd_dec);
1158 err_dec_reg:
1159         video_device_release(dev->vfd_dec);
1160 err_dec_alloc:
1161         v4l2_device_unregister(&dev->v4l2_dev);
1162 err_v4l2_dev_reg:
1163         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1164 err_mem_init_ctx_1:
1165         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1166 err_res:
1167         s5p_mfc_final_pm(dev);
1168
1169         pr_debug("%s-- with error\n", __func__);
1170         return ret;
1171
1172 }
1173
1174 /* Remove the driver */
1175 static int s5p_mfc_remove(struct platform_device *pdev)
1176 {
1177         struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1178
1179         v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1180
1181         del_timer_sync(&dev->watchdog_timer);
1182         flush_workqueue(dev->watchdog_workqueue);
1183         destroy_workqueue(dev->watchdog_workqueue);
1184
1185         video_unregister_device(dev->vfd_enc);
1186         video_unregister_device(dev->vfd_dec);
1187         v4l2_device_unregister(&dev->v4l2_dev);
1188         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1189         vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1190
1191         s5p_mfc_final_pm(dev);
1192         return 0;
1193 }
1194
1195 #ifdef CONFIG_PM_SLEEP
1196
1197 static int s5p_mfc_suspend(struct device *dev)
1198 {
1199         struct platform_device *pdev = to_platform_device(dev);
1200         struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1201         int ret;
1202
1203         if (m_dev->num_inst == 0)
1204                 return 0;
1205
1206         if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1207                 mfc_err("Error: going to suspend for a second time\n");
1208                 return -EIO;
1209         }
1210
1211         /* Check if we're processing then wait if it necessary. */
1212         while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1213                 /* Try and lock the HW */
1214                 /* Wait on the interrupt waitqueue */
1215                 ret = wait_event_interruptible_timeout(m_dev->queue,
1216                         m_dev->int_cond || m_dev->ctx[m_dev->curr_ctx]->int_cond,
1217                         msecs_to_jiffies(MFC_INT_TIMEOUT));
1218
1219                 if (ret == 0) {
1220                         mfc_err("Waiting for hardware to finish timed out\n");
1221                         return -EIO;
1222                 }
1223         }
1224
1225         return s5p_mfc_sleep(m_dev);
1226 }
1227
1228 static int s5p_mfc_resume(struct device *dev)
1229 {
1230         struct platform_device *pdev = to_platform_device(dev);
1231         struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1232
1233         if (m_dev->num_inst == 0)
1234                 return 0;
1235         return s5p_mfc_wakeup(m_dev);
1236 }
1237 #endif
1238
1239 #ifdef CONFIG_PM_RUNTIME
1240 static int s5p_mfc_runtime_suspend(struct device *dev)
1241 {
1242         struct platform_device *pdev = to_platform_device(dev);
1243         struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1244
1245         atomic_set(&m_dev->pm.power, 0);
1246         return 0;
1247 }
1248
1249 static int s5p_mfc_runtime_resume(struct device *dev)
1250 {
1251         struct platform_device *pdev = to_platform_device(dev);
1252         struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1253         int pre_power;
1254
1255         if (!m_dev->alloc_ctx)
1256                 return 0;
1257         pre_power = atomic_read(&m_dev->pm.power);
1258         atomic_set(&m_dev->pm.power, 1);
1259         return 0;
1260 }
1261 #endif
1262
1263 /* Power management */
1264 static const struct dev_pm_ops s5p_mfc_pm_ops = {
1265         SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1266         SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
1267                            NULL)
1268 };
1269
1270 struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1271         .h264_ctx       = MFC_H264_CTX_BUF_SIZE,
1272         .non_h264_ctx   = MFC_CTX_BUF_SIZE,
1273         .dsc            = DESC_BUF_SIZE,
1274         .shm            = SHARED_BUF_SIZE,
1275 };
1276
1277 struct s5p_mfc_buf_size buf_size_v5 = {
1278         .fw     = MAX_FW_SIZE,
1279         .cpb    = MAX_CPB_SIZE,
1280         .priv   = &mfc_buf_size_v5,
1281 };
1282
1283 struct s5p_mfc_buf_align mfc_buf_align_v5 = {
1284         .base = MFC_BASE_ALIGN_ORDER,
1285 };
1286
1287 static struct s5p_mfc_variant mfc_drvdata_v5 = {
1288         .version        = MFC_VERSION,
1289         .port_num       = MFC_NUM_PORTS,
1290         .buf_size       = &buf_size_v5,
1291         .buf_align      = &mfc_buf_align_v5,
1292         .mclk_name      = "sclk_mfc",
1293         .fw_name        = "s5p-mfc.fw",
1294 };
1295
1296 struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1297         .dev_ctx        = MFC_CTX_BUF_SIZE_V6,
1298         .h264_dec_ctx   = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1299         .other_dec_ctx  = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1300         .h264_enc_ctx   = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1301         .other_enc_ctx  = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1302 };
1303
1304 struct s5p_mfc_buf_size buf_size_v6 = {
1305         .fw     = MAX_FW_SIZE_V6,
1306         .cpb    = MAX_CPB_SIZE_V6,
1307         .priv   = &mfc_buf_size_v6,
1308 };
1309
1310 struct s5p_mfc_buf_align mfc_buf_align_v6 = {
1311         .base = 0,
1312 };
1313
1314 static struct s5p_mfc_variant mfc_drvdata_v6 = {
1315         .version        = MFC_VERSION_V6,
1316         .port_num       = MFC_NUM_PORTS_V6,
1317         .buf_size       = &buf_size_v6,
1318         .buf_align      = &mfc_buf_align_v6,
1319         .mclk_name      = "aclk_333",
1320         .fw_name        = "s5p-mfc-v6.fw",
1321 };
1322
1323 static struct platform_device_id mfc_driver_ids[] = {
1324         {
1325                 .name = "s5p-mfc",
1326                 .driver_data = (unsigned long)&mfc_drvdata_v5,
1327         }, {
1328                 .name = "s5p-mfc-v5",
1329                 .driver_data = (unsigned long)&mfc_drvdata_v5,
1330         }, {
1331                 .name = "s5p-mfc-v6",
1332                 .driver_data = (unsigned long)&mfc_drvdata_v6,
1333         },
1334         {},
1335 };
1336 MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
1337
1338 static struct platform_driver s5p_mfc_driver = {
1339         .probe          = s5p_mfc_probe,
1340         .remove         = s5p_mfc_remove,
1341         .id_table       = mfc_driver_ids,
1342         .driver = {
1343                 .name   = S5P_MFC_NAME,
1344                 .owner  = THIS_MODULE,
1345                 .pm     = &s5p_mfc_pm_ops
1346         },
1347 };
1348
1349 module_platform_driver(s5p_mfc_driver);
1350
1351 MODULE_LICENSE("GPL");
1352 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1353 MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");
1354