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