blob: 03204fdd363c754e6ad9244634bd95985b05f893 [file] [log] [blame]
Kamil Debskiaf935742011-06-21 10:51:26 -03001/*
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>
Kamil Debskiaf935742011-06-21 10:51:26 -030021#include <linux/videodev2.h>
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -030022#include <media/v4l2-event.h>
Kamil Debskiaf935742011-06-21 10:51:26 -030023#include <linux/workqueue.h>
Arun Kumar Kb27a23b2012-10-25 05:24:14 -030024#include <linux/of.h>
Kamil Debskiaf935742011-06-21 10:51:26 -030025#include <media/videobuf2-core.h>
Arun Kumar K43a1ea12012-10-03 22:19:08 -030026#include "s5p_mfc_common.h"
Kamil Debskiaf935742011-06-21 10:51:26 -030027#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"
Arun Kumar K43a1ea12012-10-03 22:19:08 -030032#include "s5p_mfc_opr.h"
33#include "s5p_mfc_cmd.h"
Kamil Debskiaf935742011-06-21 10:51:26 -030034#include "s5p_mfc_pm.h"
Kamil Debskiaf935742011-06-21 10:51:26 -030035
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
Mauro Carvalho Chehab139adba2014-08-22 06:07:57 -050040int mfc_debug_level;
41module_param_named(debug, mfc_debug_level, int, S_IRUGO | S_IWUSR);
Kamil Debskiaf935742011-06-21 10:51:26 -030042MODULE_PARM_DESC(debug, "Debug level - higher value produces more verbose messages");
43
44/* Helper functions for interrupt processing */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -030045
Kamil Debskiaf935742011-06-21 10:51:26 -030046/* Remove from hw execution round robin */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -030047void clear_work_bit(struct s5p_mfc_ctx *ctx)
Kamil Debskiaf935742011-06-21 10:51:26 -030048{
49 struct s5p_mfc_dev *dev = ctx->dev;
50
51 spin_lock(&dev->condlock);
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -030052 __clear_bit(ctx->num, &dev->ctx_work_bits);
Kamil Debskiaf935742011-06-21 10:51:26 -030053 spin_unlock(&dev->condlock);
54}
55
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -030056/* Add to hw execution round robin */
57void 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 */
67void 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 */
78void 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
Kamil Debskiaf935742011-06-21 10:51:26 -030088/* Wake up context wait_queue */
89static 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 */
99static 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
Sachin Kamata13bba42012-05-10 03:32:01 -0300108static void s5p_mfc_watchdog(unsigned long arg)
Kamil Debskiaf935742011-06-21 10:51:26 -0300109{
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
128static 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;
Kamil Debskie2c3be22014-09-11 10:27:20 -0300153 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
154 &ctx->dst_queue, &ctx->vq_dst);
155 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
156 &ctx->src_queue, &ctx->vq_src);
Kamil Debskiaf935742011-06-21 10:51:26 -0300157 clear_work_bit(ctx);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300158 wake_up_ctx(ctx, S5P_MFC_R2H_CMD_ERR_RET, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300159 }
160 clear_bit(0, &dev->hw_lock);
161 spin_unlock_irqrestore(&dev->irqlock, flags);
Arun Mankuzhib16e6442014-10-21 08:07:03 -0300162
163 /* De-init MFC */
164 s5p_mfc_deinit_hw(dev);
165
Kamil Debskiaf935742011-06-21 10:51:26 -0300166 /* Double check if there is at least one instance running.
167 * If no instance is in memory than no firmware should be present */
168 if (dev->num_inst > 0) {
Arun Kumar K46075002014-05-21 06:29:29 -0300169 ret = s5p_mfc_load_firmware(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300170 if (ret) {
171 mfc_err("Failed to reload FW\n");
172 goto unlock;
173 }
174 s5p_mfc_clock_on();
175 ret = s5p_mfc_init_hw(dev);
176 if (ret)
177 mfc_err("Failed to reinit FW\n");
178 }
179unlock:
180 if (mutex_locked)
181 mutex_unlock(&dev->mfc_mutex);
182}
183
Kamil Debskiaf935742011-06-21 10:51:26 -0300184static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
185{
186 mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
187 mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
188 mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
189}
190
191static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
192{
193 struct s5p_mfc_buf *dst_buf;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300194 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300195
196 ctx->state = MFCINST_FINISHED;
197 ctx->sequence++;
198 while (!list_empty(&ctx->dst_queue)) {
199 dst_buf = list_entry(ctx->dst_queue.next,
200 struct s5p_mfc_buf, list);
201 mfc_debug(2, "Cleaning up buffer: %d\n",
202 dst_buf->b->v4l2_buf.index);
203 vb2_set_plane_payload(dst_buf->b, 0, 0);
204 vb2_set_plane_payload(dst_buf->b, 1, 0);
205 list_del(&dst_buf->list);
206 ctx->dst_queue_cnt--;
207 dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
208
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300209 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
210 s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
Kamil Debskiaf935742011-06-21 10:51:26 -0300211 dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
212 else
213 dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
214
215 ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
216 vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
217 }
218}
219
220static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
221{
222 struct s5p_mfc_dev *dev = ctx->dev;
223 struct s5p_mfc_buf *dst_buf, *src_buf;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300224 size_t dec_y_addr;
225 unsigned int frame_type;
226
Ilja Friedelbb21c542014-10-21 08:06:58 -0300227 /* Make sure we actually have a new frame before continuing. */
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300228 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
Ilja Friedelbb21c542014-10-21 08:06:58 -0300229 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED)
230 return;
231 dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300232
233 /* Copy timestamp / timecode from decoded src to dst and set
Ilja Friedelbb21c542014-10-21 08:06:58 -0300234 appropriate flags. */
Kamil Debskiaf935742011-06-21 10:51:26 -0300235 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
236 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300237 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
Kamil Debski56006012013-04-24 10:31:05 -0300238 dst_buf->b->v4l2_buf.timecode =
239 src_buf->b->v4l2_buf.timecode;
240 dst_buf->b->v4l2_buf.timestamp =
241 src_buf->b->v4l2_buf.timestamp;
Sakari Ailus309f4d62014-02-08 14:21:35 -0300242 dst_buf->b->v4l2_buf.flags &=
243 ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
244 dst_buf->b->v4l2_buf.flags |=
245 src_buf->b->v4l2_buf.flags
246 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskiaf935742011-06-21 10:51:26 -0300247 switch (frame_type) {
248 case S5P_FIMV_DECODE_FRAME_I_FRAME:
249 dst_buf->b->v4l2_buf.flags |=
250 V4L2_BUF_FLAG_KEYFRAME;
251 break;
252 case S5P_FIMV_DECODE_FRAME_P_FRAME:
253 dst_buf->b->v4l2_buf.flags |=
254 V4L2_BUF_FLAG_PFRAME;
255 break;
256 case S5P_FIMV_DECODE_FRAME_B_FRAME:
257 dst_buf->b->v4l2_buf.flags |=
258 V4L2_BUF_FLAG_BFRAME;
259 break;
Ilja Friedelbb21c542014-10-21 08:06:58 -0300260 default:
261 /* Don't know how to handle
262 S5P_FIMV_DECODE_FRAME_OTHER_FRAME. */
263 mfc_debug(2, "Unexpected frame type: %d\n",
264 frame_type);
Kamil Debskiaf935742011-06-21 10:51:26 -0300265 }
266 break;
267 }
268 }
269}
270
271static 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;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300275 size_t dspl_y_addr;
276 unsigned int frame_type;
Kamil Debskiaf935742011-06-21 10:51:26 -0300277
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300278 dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
Sjoerd Simons7c672812014-09-22 09:52:02 -0300279 if (IS_MFCV6_PLUS(dev))
280 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
281 get_disp_frame_type, ctx);
282 else
283 frame_type = s5p_mfc_hw_call(dev->mfc_ops,
284 get_dec_frame_type, dev);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300285
Kamil Debskiaf935742011-06-21 10:51:26 -0300286 /* If frame is same as previous then skip and do not dequeue */
287 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
288 if (!ctx->after_packed_pb)
289 ctx->sequence++;
290 ctx->after_packed_pb = 0;
291 return;
292 }
293 ctx->sequence++;
294 /* The MFC returns address of the buffer, now we have to
295 * check which videobuf does it correspond to */
296 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
297 /* Check if this is the buffer we're looking for */
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300298 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300299 list_del(&dst_buf->list);
300 ctx->dst_queue_cnt--;
301 dst_buf->b->v4l2_buf.sequence = ctx->sequence;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300302 if (s5p_mfc_hw_call(dev->mfc_ops,
303 get_pic_type_top, ctx) ==
304 s5p_mfc_hw_call(dev->mfc_ops,
305 get_pic_type_bot, ctx))
Kamil Debskiaf935742011-06-21 10:51:26 -0300306 dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
307 else
308 dst_buf->b->v4l2_buf.field =
309 V4L2_FIELD_INTERLACED;
310 vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
311 vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
312 clear_bit(dst_buf->b->v4l2_buf.index,
313 &ctx->dec_dst_flag);
314
315 vb2_buffer_done(dst_buf->b,
316 err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
317
Kamil Debskiaf935742011-06-21 10:51:26 -0300318 break;
319 }
320 }
321}
322
323/* Handle frame decoding interrupt */
324static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
325 unsigned int reason, unsigned int err)
326{
327 struct s5p_mfc_dev *dev = ctx->dev;
328 unsigned int dst_frame_status;
Pawel Osciaka0517f52014-05-19 09:32:57 -0300329 unsigned int dec_frame_status;
Kamil Debskiaf935742011-06-21 10:51:26 -0300330 struct s5p_mfc_buf *src_buf;
331 unsigned long flags;
332 unsigned int res_change;
333
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300334 dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
Kamil Debskiaf935742011-06-21 10:51:26 -0300335 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
Pawel Osciaka0517f52014-05-19 09:32:57 -0300336 dec_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dec_status, dev)
337 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300338 res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
339 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
340 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
Kamil Debskiaf935742011-06-21 10:51:26 -0300341 mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
342 if (ctx->state == MFCINST_RES_CHANGE_INIT)
343 ctx->state = MFCINST_RES_CHANGE_FLUSH;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300344 if (res_change == S5P_FIMV_RES_INCREASE ||
345 res_change == S5P_FIMV_RES_DECREASE) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300346 ctx->state = MFCINST_RES_CHANGE_INIT;
Kamil Debskie2c3be22014-09-11 10:27:20 -0300347 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300348 wake_up_ctx(ctx, reason, err);
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300349 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300350 s5p_mfc_clock_off();
Kamil Debskie2c3be22014-09-11 10:27:20 -0300351 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300352 return;
353 }
354 if (ctx->dpb_flush_flag)
355 ctx->dpb_flush_flag = 0;
356
357 spin_lock_irqsave(&dev->irqlock, flags);
358 /* All frames remaining in the buffer have been extracted */
359 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
360 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
Pawel Osciak0520e4c2014-05-14 03:59:43 -0300361 static const struct v4l2_event ev_src_ch = {
362 .type = V4L2_EVENT_SOURCE_CHANGE,
363 .u.src_change.changes =
364 V4L2_EVENT_SRC_CH_RESOLUTION,
365 };
366
Kamil Debskiaf935742011-06-21 10:51:26 -0300367 s5p_mfc_handle_frame_all_extracted(ctx);
368 ctx->state = MFCINST_RES_CHANGE_END;
Pawel Osciak0520e4c2014-05-14 03:59:43 -0300369 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
370
Kamil Debskiaf935742011-06-21 10:51:26 -0300371 goto leave_handle_frame;
372 } else {
373 s5p_mfc_handle_frame_all_extracted(ctx);
374 }
375 }
376
Pawel Osciaka0517f52014-05-19 09:32:57 -0300377 if (dec_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY)
Kamil Debskiaf935742011-06-21 10:51:26 -0300378 s5p_mfc_handle_frame_copy_time(ctx);
379
380 /* A frame has been decoded and is in the buffer */
381 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
382 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
383 s5p_mfc_handle_frame_new(ctx, err);
384 } else {
385 mfc_debug(2, "No frame decode\n");
386 }
387 /* Mark source buffer as complete */
388 if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
389 && !list_empty(&ctx->src_queue)) {
390 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
391 list);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300392 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
393 get_consumed_stream, dev);
394 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
Pawel Osciakf49f3ed2014-05-19 09:33:02 -0300395 ctx->codec_mode != S5P_MFC_CODEC_VP8_DEC &&
Arun Kumar Kd2a0db12012-11-14 09:26:45 -0300396 ctx->consumed_stream + STUFF_BYTE <
397 src_buf->b->v4l2_planes[0].bytesused) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300398 /* Run MFC again on the same buffer */
399 mfc_debug(2, "Running again the same buffer\n");
400 ctx->after_packed_pb = 1;
401 } else {
Kamil Debskiaf935742011-06-21 10:51:26 -0300402 mfc_debug(2, "MFC needs next buffer\n");
403 ctx->consumed_stream = 0;
Kamil Debskia34026e2013-01-11 12:29:33 -0300404 if (src_buf->flags & MFC_BUF_FLAG_EOS)
405 ctx->state = MFCINST_FINISHING;
Kamil Debskiaf935742011-06-21 10:51:26 -0300406 list_del(&src_buf->list);
407 ctx->src_queue_cnt--;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300408 if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
Kamil Debskiaf935742011-06-21 10:51:26 -0300409 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
410 else
411 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
412 }
413 }
414leave_handle_frame:
415 spin_unlock_irqrestore(&dev->irqlock, flags);
416 if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
Arun Kumar Ke9d98dd2013-04-24 09:41:53 -0300417 || ctx->dst_queue_cnt < ctx->pb_count)
Kamil Debskiaf935742011-06-21 10:51:26 -0300418 clear_work_bit(ctx);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300419 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300420 wake_up_ctx(ctx, reason, err);
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300421 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300422 s5p_mfc_clock_off();
Prathyush K76a4ddb2013-10-04 01:47:19 -0300423 /* if suspending, wake up device and do not try_run again*/
424 if (test_bit(0, &dev->enter_suspend))
425 wake_up_dev(dev, reason, err);
426 else
Kamil Debskie2c3be22014-09-11 10:27:20 -0300427 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300428}
429
430/* Error handling for interrupt */
Kamil Debski7296e252012-12-21 05:32:59 -0300431static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
432 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
Kamil Debskiaf935742011-06-21 10:51:26 -0300433{
Kamil Debskiaf935742011-06-21 10:51:26 -0300434 unsigned long flags;
435
Kamil Debskiaf935742011-06-21 10:51:26 -0300436 mfc_err("Interrupt Error: %08x\n", err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300437
Kamil Debski7296e252012-12-21 05:32:59 -0300438 if (ctx != NULL) {
439 /* Error recovery is dependent on the state of context */
440 switch (ctx->state) {
441 case MFCINST_RES_CHANGE_INIT:
442 case MFCINST_RES_CHANGE_FLUSH:
443 case MFCINST_RES_CHANGE_END:
444 case MFCINST_FINISHING:
445 case MFCINST_FINISHED:
446 case MFCINST_RUNNING:
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300447 /* It is highly probable that an error occurred
Kamil Debski7296e252012-12-21 05:32:59 -0300448 * while decoding a frame */
449 clear_work_bit(ctx);
450 ctx->state = MFCINST_ERROR;
451 /* Mark all dst buffers as having an error */
452 spin_lock_irqsave(&dev->irqlock, flags);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300453 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
Kamil Debski7296e252012-12-21 05:32:59 -0300454 &ctx->dst_queue, &ctx->vq_dst);
455 /* Mark all src buffers as having an error */
Kamil Debskie2c3be22014-09-11 10:27:20 -0300456 s5p_mfc_hw_call_void(dev->mfc_ops, cleanup_queue,
Kamil Debski7296e252012-12-21 05:32:59 -0300457 &ctx->src_queue, &ctx->vq_src);
458 spin_unlock_irqrestore(&dev->irqlock, flags);
459 wake_up_ctx(ctx, reason, err);
460 break;
461 default:
462 clear_work_bit(ctx);
463 ctx->state = MFCINST_ERROR;
464 wake_up_ctx(ctx, reason, err);
465 break;
466 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300467 }
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300468 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300469 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debski7296e252012-12-21 05:32:59 -0300470 s5p_mfc_clock_off();
471 wake_up_dev(dev, reason, err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300472 return;
473}
474
475/* Header parsing interrupt handling */
476static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
477 unsigned int reason, unsigned int err)
478{
479 struct s5p_mfc_dev *dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300480
Sachin Kamat12597622012-05-10 03:32:00 -0300481 if (ctx == NULL)
Kamil Debskiaf935742011-06-21 10:51:26 -0300482 return;
483 dev = ctx->dev;
484 if (ctx->c_ops->post_seq_start) {
485 if (ctx->c_ops->post_seq_start(ctx))
486 mfc_err("post_seq_start() failed\n");
487 } else {
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300488 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
489 dev);
490 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
491 dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300492
Kamil Debskie2c3be22014-09-11 10:27:20 -0300493 s5p_mfc_hw_call_void(dev->mfc_ops, dec_calc_dpb_size, ctx);
Arun Kumar K8f532a72012-10-03 22:19:09 -0300494
Arun Kumar Ke9d98dd2013-04-24 09:41:53 -0300495 ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300496 dev);
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300497 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
498 dev);
Julia Lawallbb869362012-01-12 17:49:30 -0300499 if (ctx->img_width == 0 || ctx->img_height == 0)
Kamil Debskiaf935742011-06-21 10:51:26 -0300500 ctx->state = MFCINST_ERROR;
501 else
502 ctx->state = MFCINST_HEAD_PARSED;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300503
504 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
505 ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
506 !list_empty(&ctx->src_queue)) {
507 struct s5p_mfc_buf *src_buf;
508 src_buf = list_entry(ctx->src_queue.next,
509 struct s5p_mfc_buf, list);
510 if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
511 dev) <
512 src_buf->b->v4l2_planes[0].bytesused)
513 ctx->head_processed = 0;
514 else
515 ctx->head_processed = 1;
516 } else {
517 ctx->head_processed = 1;
518 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300519 }
Kamil Debskie2c3be22014-09-11 10:27:20 -0300520 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300521 clear_work_bit(ctx);
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300522 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300523 s5p_mfc_clock_off();
Kamil Debskie2c3be22014-09-11 10:27:20 -0300524 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300525 wake_up_ctx(ctx, reason, err);
526}
527
528/* Header parsing interrupt handling */
529static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
530 unsigned int reason, unsigned int err)
531{
532 struct s5p_mfc_buf *src_buf;
533 struct s5p_mfc_dev *dev;
534 unsigned long flags;
535
Sachin Kamat12597622012-05-10 03:32:00 -0300536 if (ctx == NULL)
Kamil Debskiaf935742011-06-21 10:51:26 -0300537 return;
538 dev = ctx->dev;
Kamil Debskie2c3be22014-09-11 10:27:20 -0300539 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300540 ctx->int_type = reason;
541 ctx->int_err = err;
542 ctx->int_cond = 1;
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300543 clear_work_bit(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300544 if (err == 0) {
545 ctx->state = MFCINST_RUNNING;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300546 if (!ctx->dpb_flush_flag && ctx->head_processed) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300547 spin_lock_irqsave(&dev->irqlock, flags);
548 if (!list_empty(&ctx->src_queue)) {
549 src_buf = list_entry(ctx->src_queue.next,
550 struct s5p_mfc_buf, list);
551 list_del(&src_buf->list);
552 ctx->src_queue_cnt--;
553 vb2_buffer_done(src_buf->b,
554 VB2_BUF_STATE_DONE);
555 }
556 spin_unlock_irqrestore(&dev->irqlock, flags);
557 } else {
558 ctx->dpb_flush_flag = 0;
559 }
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300560 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300561
562 s5p_mfc_clock_off();
563
564 wake_up(&ctx->queue);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300565 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300566 } else {
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300567 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300568
569 s5p_mfc_clock_off();
570
571 wake_up(&ctx->queue);
572 }
573}
574
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300575static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
576 unsigned int reason, unsigned int err)
577{
578 struct s5p_mfc_dev *dev = ctx->dev;
579 struct s5p_mfc_buf *mb_entry;
580
Andrzej Hajda4130eab2013-05-28 03:26:16 -0300581 mfc_debug(2, "Stream completed\n");
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300582
583 s5p_mfc_clear_int_flags(dev);
584 ctx->int_type = reason;
585 ctx->int_err = err;
586 ctx->state = MFCINST_FINISHED;
587
588 spin_lock(&dev->irqlock);
589 if (!list_empty(&ctx->dst_queue)) {
590 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
591 list);
592 list_del(&mb_entry->list);
593 ctx->dst_queue_cnt--;
594 vb2_set_plane_payload(mb_entry->b, 0, 0);
595 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
596 }
597 spin_unlock(&dev->irqlock);
598
599 clear_work_bit(ctx);
600
Sachin Kamate8256442013-01-22 01:00:06 -0300601 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300602
603 s5p_mfc_clock_off();
604 wake_up(&ctx->queue);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300605 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300606}
607
Kamil Debskiaf935742011-06-21 10:51:26 -0300608/* Interrupt processing */
609static irqreturn_t s5p_mfc_irq(int irq, void *priv)
610{
611 struct s5p_mfc_dev *dev = priv;
612 struct s5p_mfc_ctx *ctx;
613 unsigned int reason;
614 unsigned int err;
615
616 mfc_debug_enter();
617 /* Reset the timeout watchdog */
618 atomic_set(&dev->watchdog_cnt, 0);
619 ctx = dev->ctx[dev->curr_ctx];
620 /* Get the reason of interrupt and the error code */
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300621 reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
622 err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300623 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
624 switch (reason) {
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300625 case S5P_MFC_R2H_CMD_ERR_RET:
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300626 /* An error has occurred */
Kamil Debskiaf935742011-06-21 10:51:26 -0300627 if (ctx->state == MFCINST_RUNNING &&
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300628 s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
629 dev->warn_start)
Kamil Debskiaf935742011-06-21 10:51:26 -0300630 s5p_mfc_handle_frame(ctx, reason, err);
631 else
Kamil Debski7296e252012-12-21 05:32:59 -0300632 s5p_mfc_handle_error(dev, ctx, reason, err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300633 clear_bit(0, &dev->enter_suspend);
634 break;
635
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300636 case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
637 case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
638 case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300639 if (ctx->c_ops->post_frame_start) {
640 if (ctx->c_ops->post_frame_start(ctx))
641 mfc_err("post_frame_start() failed\n");
Kamil Debskie2c3be22014-09-11 10:27:20 -0300642 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300643 wake_up_ctx(ctx, reason, err);
Pawel Osciak9a7bc6b2014-10-21 08:07:01 -0300644 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Kamil Debskiaf935742011-06-21 10:51:26 -0300645 s5p_mfc_clock_off();
Kamil Debskie2c3be22014-09-11 10:27:20 -0300646 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300647 } else {
648 s5p_mfc_handle_frame(ctx, reason, err);
649 }
650 break;
651
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300652 case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300653 s5p_mfc_handle_seq_done(ctx, reason, err);
654 break;
655
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300656 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
657 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300658 ctx->state = MFCINST_GOT_INST;
659 clear_work_bit(ctx);
660 wake_up(&ctx->queue);
661 goto irq_cleanup_hw;
662
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300663 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300664 clear_work_bit(ctx);
Pawel Osciak9d87e832014-05-19 09:33:00 -0300665 ctx->inst_no = MFC_NO_INSTANCE_SET;
Kamil Debskiaf935742011-06-21 10:51:26 -0300666 ctx->state = MFCINST_FREE;
667 wake_up(&ctx->queue);
668 goto irq_cleanup_hw;
669
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300670 case S5P_MFC_R2H_CMD_SYS_INIT_RET:
671 case S5P_MFC_R2H_CMD_FW_STATUS_RET:
672 case S5P_MFC_R2H_CMD_SLEEP_RET:
673 case S5P_MFC_R2H_CMD_WAKEUP_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300674 if (ctx)
675 clear_work_bit(ctx);
Kamil Debskie2c3be22014-09-11 10:27:20 -0300676 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300677 wake_up_dev(dev, reason, err);
678 clear_bit(0, &dev->hw_lock);
679 clear_bit(0, &dev->enter_suspend);
680 break;
681
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300682 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300683 s5p_mfc_handle_init_buffers(ctx, reason, err);
684 break;
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300685
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300686 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300687 s5p_mfc_handle_stream_complete(ctx, reason, err);
688 break;
689
Arun Kumar K8f23cc02012-11-22 06:15:55 -0300690 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
691 clear_work_bit(ctx);
692 ctx->state = MFCINST_RUNNING;
693 wake_up(&ctx->queue);
694 goto irq_cleanup_hw;
695
Kamil Debskiaf935742011-06-21 10:51:26 -0300696 default:
697 mfc_debug(2, "Unknown int reason\n");
Kamil Debskie2c3be22014-09-11 10:27:20 -0300698 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300699 }
700 mfc_debug_leave();
701 return IRQ_HANDLED;
702irq_cleanup_hw:
Kamil Debskie2c3be22014-09-11 10:27:20 -0300703 s5p_mfc_hw_call_void(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300704 ctx->int_type = reason;
705 ctx->int_err = err;
706 ctx->int_cond = 1;
707 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
708 mfc_err("Failed to unlock hw\n");
709
710 s5p_mfc_clock_off();
711
Kamil Debskie2c3be22014-09-11 10:27:20 -0300712 s5p_mfc_hw_call_void(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300713 mfc_debug(2, "Exit via irq_cleanup_hw\n");
714 return IRQ_HANDLED;
715}
716
717/* Open an MFC node */
718static int s5p_mfc_open(struct file *file)
719{
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300720 struct video_device *vdev = video_devdata(file);
Kamil Debskiaf935742011-06-21 10:51:26 -0300721 struct s5p_mfc_dev *dev = video_drvdata(file);
722 struct s5p_mfc_ctx *ctx = NULL;
723 struct vb2_queue *q;
Kamil Debskiaf935742011-06-21 10:51:26 -0300724 int ret = 0;
725
726 mfc_debug_enter();
Hans Verkuilbc738302012-06-24 07:13:33 -0300727 if (mutex_lock_interruptible(&dev->mfc_mutex))
728 return -ERESTARTSYS;
Kamil Debskiaf935742011-06-21 10:51:26 -0300729 dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
730 /* Allocate memory for context */
Sachin Kamatbae061b2012-08-17 03:22:55 -0300731 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Kamil Debskiaf935742011-06-21 10:51:26 -0300732 if (!ctx) {
733 mfc_err("Not enough memory\n");
734 ret = -ENOMEM;
735 goto err_alloc;
736 }
Zhaowei Yuan55647a92014-07-23 01:06:12 -0300737 v4l2_fh_init(&ctx->fh, vdev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300738 file->private_data = &ctx->fh;
739 v4l2_fh_add(&ctx->fh);
740 ctx->dev = dev;
741 INIT_LIST_HEAD(&ctx->src_queue);
742 INIT_LIST_HEAD(&ctx->dst_queue);
743 ctx->src_queue_cnt = 0;
744 ctx->dst_queue_cnt = 0;
745 /* Get context number */
746 ctx->num = 0;
747 while (dev->ctx[ctx->num]) {
748 ctx->num++;
749 if (ctx->num >= MFC_NUM_CONTEXTS) {
750 mfc_err("Too many open contexts\n");
751 ret = -EBUSY;
752 goto err_no_ctx;
753 }
754 }
755 /* Mark context as idle */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300756 clear_work_bit_irqsave(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300757 dev->ctx[ctx->num] = ctx;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300758 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300759 ctx->type = MFCINST_DECODER;
760 ctx->c_ops = get_dec_codec_ops();
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300761 s5p_mfc_dec_init(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300762 /* Setup ctrl handler */
763 ret = s5p_mfc_dec_ctrls_setup(ctx);
764 if (ret) {
765 mfc_err("Failed to setup mfc controls\n");
766 goto err_ctrls_setup;
767 }
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300768 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300769 ctx->type = MFCINST_ENCODER;
770 ctx->c_ops = get_enc_codec_ops();
771 /* only for encoder */
772 INIT_LIST_HEAD(&ctx->ref_queue);
773 ctx->ref_queue_cnt = 0;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300774 s5p_mfc_enc_init(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300775 /* Setup ctrl handler */
776 ret = s5p_mfc_enc_ctrls_setup(ctx);
777 if (ret) {
778 mfc_err("Failed to setup mfc controls\n");
779 goto err_ctrls_setup;
780 }
781 } else {
782 ret = -ENOENT;
783 goto err_bad_node;
784 }
785 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
Pawel Osciak9d87e832014-05-19 09:33:00 -0300786 ctx->inst_no = MFC_NO_INSTANCE_SET;
Kamil Debskiaf935742011-06-21 10:51:26 -0300787 /* Load firmware if this is the first instance */
788 if (dev->num_inst == 1) {
789 dev->watchdog_timer.expires = jiffies +
790 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
791 add_timer(&dev->watchdog_timer);
792 ret = s5p_mfc_power_on();
793 if (ret < 0) {
794 mfc_err("power on failed\n");
795 goto err_pwr_enable;
796 }
797 s5p_mfc_clock_on();
Kamil Debski2e731e42013-01-03 11:02:07 -0300798 ret = s5p_mfc_load_firmware(dev);
799 if (ret) {
800 s5p_mfc_clock_off();
801 goto err_load_fw;
802 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300803 /* Init the FW */
804 ret = s5p_mfc_init_hw(dev);
Kamil Debski2e731e42013-01-03 11:02:07 -0300805 s5p_mfc_clock_off();
Kamil Debskiaf935742011-06-21 10:51:26 -0300806 if (ret)
807 goto err_init_hw;
Kamil Debskiaf935742011-06-21 10:51:26 -0300808 }
809 /* Init videobuf2 queue for CAPTURE */
810 q = &ctx->vq_dst;
811 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
812 q->drv_priv = &ctx->fh;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300813 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300814 q->io_modes = VB2_MMAP;
815 q->ops = get_dec_queue_ops();
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300816 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300817 q->io_modes = VB2_MMAP | VB2_USERPTR;
818 q->ops = get_enc_queue_ops();
819 } else {
820 ret = -ENOENT;
821 goto err_queue_init;
822 }
Rasmus Villemoes749ae712014-10-21 11:55:35 -0300823 q->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300824 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Kamil Debskiaf935742011-06-21 10:51:26 -0300825 ret = vb2_queue_init(q);
826 if (ret) {
827 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
828 goto err_queue_init;
829 }
830 /* Init videobuf2 queue for OUTPUT */
831 q = &ctx->vq_src;
832 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
833 q->io_modes = VB2_MMAP;
834 q->drv_priv = &ctx->fh;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300835 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300836 q->io_modes = VB2_MMAP;
837 q->ops = get_dec_queue_ops();
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300838 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300839 q->io_modes = VB2_MMAP | VB2_USERPTR;
840 q->ops = get_enc_queue_ops();
841 } else {
842 ret = -ENOENT;
843 goto err_queue_init;
844 }
Rasmus Villemoes749ae712014-10-21 11:55:35 -0300845 q->mem_ops = &vb2_dma_contig_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300846 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Kamil Debskiaf935742011-06-21 10:51:26 -0300847 ret = vb2_queue_init(q);
848 if (ret) {
849 mfc_err("Failed to initialize videobuf2 queue(output)\n");
850 goto err_queue_init;
851 }
852 init_waitqueue_head(&ctx->queue);
Hans Verkuilbc738302012-06-24 07:13:33 -0300853 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300854 mfc_debug_leave();
855 return ret;
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300856 /* Deinit when failure occurred */
Kamil Debskiaf935742011-06-21 10:51:26 -0300857err_queue_init:
Kamil Debski2e731e42013-01-03 11:02:07 -0300858 if (dev->num_inst == 1)
859 s5p_mfc_deinit_hw(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300860err_init_hw:
Kamil Debski2e731e42013-01-03 11:02:07 -0300861err_load_fw:
Kamil Debskiaf935742011-06-21 10:51:26 -0300862err_pwr_enable:
863 if (dev->num_inst == 1) {
864 if (s5p_mfc_power_off() < 0)
865 mfc_err("power off failed\n");
Kamil Debski1b73ba02012-11-22 10:00:28 -0300866 del_timer_sync(&dev->watchdog_timer);
Kamil Debskiaf935742011-06-21 10:51:26 -0300867 }
868err_ctrls_setup:
869 s5p_mfc_dec_ctrls_delete(ctx);
870err_bad_node:
Kamil Debski1b73ba02012-11-22 10:00:28 -0300871 dev->ctx[ctx->num] = NULL;
Kamil Debskiaf935742011-06-21 10:51:26 -0300872err_no_ctx:
873 v4l2_fh_del(&ctx->fh);
874 v4l2_fh_exit(&ctx->fh);
875 kfree(ctx);
876err_alloc:
877 dev->num_inst--;
Hans Verkuilbc738302012-06-24 07:13:33 -0300878 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300879 mfc_debug_leave();
880 return ret;
881}
882
883/* Release MFC context */
884static int s5p_mfc_release(struct file *file)
885{
886 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
887 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300888
889 mfc_debug_enter();
Hans Verkuilbc738302012-06-24 07:13:33 -0300890 mutex_lock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300891 s5p_mfc_clock_on();
892 vb2_queue_release(&ctx->vq_src);
893 vb2_queue_release(&ctx->vq_dst);
894 /* Mark context as idle */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300895 clear_work_bit_irqsave(ctx);
Pawel Osciak9d87e832014-05-19 09:33:00 -0300896 /* If instance was initialised and not yet freed,
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300897 * return instance and free resources */
Pawel Osciak9d87e832014-05-19 09:33:00 -0300898 if (ctx->state != MFCINST_FREE && ctx->state != MFCINST_INIT) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300899 mfc_debug(2, "Has to free instance\n");
Pawel Osciak818cd912014-05-19 09:32:59 -0300900 s5p_mfc_close_mfc_inst(dev, ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300901 }
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) {
Kamil Debski2e731e42013-01-03 11:02:07 -0300907 mfc_debug(2, "Last instance\n");
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300908 s5p_mfc_deinit_hw(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300909 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();
Sachin Kamat12597622012-05-10 03:32:00 -0300915 dev->ctx[ctx->num] = NULL;
Kamil Debskiaf935742011-06-21 10:51:26 -0300916 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();
Hans Verkuilbc738302012-06-24 07:13:33 -0300921 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300922 return 0;
923}
924
925/* Poll */
926static 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
Hans Verkuilbc738302012-06-24 07:13:33 -0300936 mutex_lock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300937 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);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300950 poll_wait(file, &ctx->fh.wait, wait);
Kamil Debskiaf935742011-06-21 10:51:26 -0300951 poll_wait(file, &src_q->done_wq, wait);
952 poll_wait(file, &dst_q->done_wq, wait);
953 mutex_lock(&dev->mfc_mutex);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300954 if (v4l2_event_pending(&ctx->fh))
955 rc |= POLLPRI;
Kamil Debskiaf935742011-06-21 10:51:26 -0300956 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);
972end:
Hans Verkuilbc738302012-06-24 07:13:33 -0300973 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300974 return rc;
975}
976
977/* Mmap */
978static 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);
Hans Verkuilbc738302012-06-24 07:13:33 -0300981 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300982 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
983 int ret;
Hans Verkuilbc738302012-06-24 07:13:33 -0300984
985 if (mutex_lock_interruptible(&dev->mfc_mutex))
986 return -ERESTARTSYS;
Kamil Debskiaf935742011-06-21 10:51:26 -0300987 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 }
Hans Verkuilbc738302012-06-24 07:13:33 -0300995 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300996 return ret;
997}
998
999/* v4l2 ops */
1000static 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
1009static 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
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001016static void *mfc_get_drv_data(struct platform_device *pdev);
1017
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001018static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
1019{
Sylwester Nawrocki65fccab2013-04-10 07:17:52 -03001020 unsigned int mem_info[2] = { };
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001021
1022 dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev,
1023 sizeof(struct device), GFP_KERNEL);
1024 if (!dev->mem_dev_l) {
1025 mfc_err("Not enough memory\n");
1026 return -ENOMEM;
1027 }
1028 device_initialize(dev->mem_dev_l);
1029 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1030 "samsung,mfc-l", mem_info, 2);
1031 if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0],
1032 mem_info[0], mem_info[1],
1033 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1034 mfc_err("Failed to declare coherent memory for\n"
1035 "MFC device\n");
1036 return -ENOMEM;
1037 }
1038
1039 dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev,
1040 sizeof(struct device), GFP_KERNEL);
1041 if (!dev->mem_dev_r) {
1042 mfc_err("Not enough memory\n");
1043 return -ENOMEM;
1044 }
1045 device_initialize(dev->mem_dev_r);
1046 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1047 "samsung,mfc-r", mem_info, 2);
1048 if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0],
1049 mem_info[0], mem_info[1],
1050 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1051 pr_err("Failed to declare coherent memory for\n"
1052 "MFC device\n");
1053 return -ENOMEM;
1054 }
1055 return 0;
1056}
1057
Kamil Debskiaf935742011-06-21 10:51:26 -03001058/* MFC probe function */
Kamil Debski1e393e92011-08-08 13:12:51 -03001059static int s5p_mfc_probe(struct platform_device *pdev)
Kamil Debskiaf935742011-06-21 10:51:26 -03001060{
1061 struct s5p_mfc_dev *dev;
1062 struct video_device *vfd;
1063 struct resource *res;
1064 int ret;
1065
1066 pr_debug("%s++\n", __func__);
Sachin Kamatbae061b2012-08-17 03:22:55 -03001067 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debskiaf935742011-06-21 10:51:26 -03001068 if (!dev) {
1069 dev_err(&pdev->dev, "Not enough memory for MFC device\n");
1070 return -ENOMEM;
1071 }
1072
1073 spin_lock_init(&dev->irqlock);
1074 spin_lock_init(&dev->condlock);
1075 dev->plat_dev = pdev;
1076 if (!dev->plat_dev) {
1077 dev_err(&pdev->dev, "No platform data specified\n");
Sachin Kamatd310f472012-05-14 08:22:27 -03001078 return -ENODEV;
Kamil Debskiaf935742011-06-21 10:51:26 -03001079 }
1080
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001081 dev->variant = mfc_get_drv_data(pdev);
Arun Kumar K8f532a72012-10-03 22:19:09 -03001082
Kamil Debskiaf935742011-06-21 10:51:26 -03001083 ret = s5p_mfc_init_pm(dev);
1084 if (ret < 0) {
1085 dev_err(&pdev->dev, "failed to get mfc clock source\n");
Sachin Kamatd310f472012-05-14 08:22:27 -03001086 return ret;
Kamil Debskiaf935742011-06-21 10:51:26 -03001087 }
1088
1089 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -03001090
Thierry Redingf23999e2013-01-21 06:09:07 -03001091 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1092 if (IS_ERR(dev->regs_base))
1093 return PTR_ERR(dev->regs_base);
Kamil Debskiaf935742011-06-21 10:51:26 -03001094
1095 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1096 if (res == NULL) {
1097 dev_err(&pdev->dev, "failed to get irq resource\n");
1098 ret = -ENOENT;
Sachin Kamatd310f472012-05-14 08:22:27 -03001099 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001100 }
1101 dev->irq = res->start;
Sachin Kamatd310f472012-05-14 08:22:27 -03001102 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
Michael Opdenacker1957f0d2013-10-13 02:58:39 -03001103 0, pdev->name, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001104 if (ret) {
1105 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
Sachin Kamatd310f472012-05-14 08:22:27 -03001106 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001107 }
1108
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001109 if (pdev->dev.of_node) {
Wei Yongjund68b44e2013-04-17 23:18:19 -03001110 ret = s5p_mfc_alloc_memdevs(dev);
1111 if (ret < 0)
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001112 goto err_res;
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001113 } else {
1114 dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
1115 "s5p-mfc-l", match_child);
1116 if (!dev->mem_dev_l) {
1117 mfc_err("Mem child (L) device get failed\n");
1118 ret = -ENODEV;
1119 goto err_res;
1120 }
1121 dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
1122 "s5p-mfc-r", match_child);
1123 if (!dev->mem_dev_r) {
1124 mfc_err("Mem child (R) device get failed\n");
1125 ret = -ENODEV;
1126 goto err_res;
1127 }
Kamil Debskiaf935742011-06-21 10:51:26 -03001128 }
1129
1130 dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
Kamil Debskief89fff2013-01-03 07:06:03 -03001131 if (IS_ERR(dev->alloc_ctx[0])) {
Kamil Debskiaf935742011-06-21 10:51:26 -03001132 ret = PTR_ERR(dev->alloc_ctx[0]);
Sachin Kamatd310f472012-05-14 08:22:27 -03001133 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001134 }
1135 dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
Kamil Debskief89fff2013-01-03 07:06:03 -03001136 if (IS_ERR(dev->alloc_ctx[1])) {
Kamil Debskiaf935742011-06-21 10:51:26 -03001137 ret = PTR_ERR(dev->alloc_ctx[1]);
1138 goto err_mem_init_ctx_1;
1139 }
1140
1141 mutex_init(&dev->mfc_mutex);
1142
Kamil Debski2e731e42013-01-03 11:02:07 -03001143 ret = s5p_mfc_alloc_firmware(dev);
1144 if (ret)
1145 goto err_alloc_fw;
1146
Kamil Debskiaf935742011-06-21 10:51:26 -03001147 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1148 if (ret)
1149 goto err_v4l2_dev_reg;
1150 init_waitqueue_head(&dev->queue);
1151
1152 /* decoder */
1153 vfd = video_device_alloc();
1154 if (!vfd) {
1155 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1156 ret = -ENOMEM;
1157 goto err_dec_alloc;
1158 }
Joonyoung Shimd0ce8982014-02-20 23:22:12 -03001159 vfd->fops = &s5p_mfc_fops;
Kamil Debskiaf935742011-06-21 10:51:26 -03001160 vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
Joonyoung Shimd0ce8982014-02-20 23:22:12 -03001161 vfd->release = video_device_release;
Kamil Debskiaf935742011-06-21 10:51:26 -03001162 vfd->lock = &dev->mfc_mutex;
1163 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001164 vfd->vfl_dir = VFL_DIR_M2M;
Kamil Debskiaf935742011-06-21 10:51:26 -03001165 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1166 dev->vfd_dec = vfd;
1167 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1168 if (ret) {
1169 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1170 video_device_release(vfd);
1171 goto err_dec_reg;
1172 }
1173 v4l2_info(&dev->v4l2_dev,
1174 "decoder registered as /dev/video%d\n", vfd->num);
1175 video_set_drvdata(vfd, dev);
1176
1177 /* encoder */
1178 vfd = video_device_alloc();
1179 if (!vfd) {
1180 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1181 ret = -ENOMEM;
1182 goto err_enc_alloc;
1183 }
Joonyoung Shimd0ce8982014-02-20 23:22:12 -03001184 vfd->fops = &s5p_mfc_fops;
Kamil Debskiaf935742011-06-21 10:51:26 -03001185 vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
Joonyoung Shimd0ce8982014-02-20 23:22:12 -03001186 vfd->release = video_device_release;
Kamil Debskiaf935742011-06-21 10:51:26 -03001187 vfd->lock = &dev->mfc_mutex;
1188 vfd->v4l2_dev = &dev->v4l2_dev;
Arun Kumar Kcdcf45e2012-10-04 16:14:56 -03001189 vfd->vfl_dir = VFL_DIR_M2M;
Kamil Debskiaf935742011-06-21 10:51:26 -03001190 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1191 dev->vfd_enc = vfd;
1192 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1193 if (ret) {
1194 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1195 video_device_release(vfd);
1196 goto err_enc_reg;
1197 }
1198 v4l2_info(&dev->v4l2_dev,
1199 "encoder registered as /dev/video%d\n", vfd->num);
1200 video_set_drvdata(vfd, dev);
1201 platform_set_drvdata(pdev, dev);
1202
1203 dev->hw_lock = 0;
1204 dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
1205 INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1206 atomic_set(&dev->watchdog_cnt, 0);
1207 init_timer(&dev->watchdog_timer);
1208 dev->watchdog_timer.data = (unsigned long)dev;
1209 dev->watchdog_timer.function = s5p_mfc_watchdog;
1210
Arun Kumar K43a1ea12012-10-03 22:19:08 -03001211 /* Initialize HW ops and commands based on MFC version */
1212 s5p_mfc_init_hw_ops(dev);
1213 s5p_mfc_init_hw_cmds(dev);
Kiran AVND6a9c6f682014-05-19 09:33:05 -03001214 s5p_mfc_init_regs(dev);
Arun Kumar K43a1ea12012-10-03 22:19:08 -03001215
Kamil Debskiaf935742011-06-21 10:51:26 -03001216 pr_debug("%s--\n", __func__);
1217 return 0;
1218
1219/* Deinit MFC if probe had failed */
1220err_enc_reg:
1221 video_device_release(dev->vfd_enc);
1222err_enc_alloc:
1223 video_unregister_device(dev->vfd_dec);
1224err_dec_reg:
1225 video_device_release(dev->vfd_dec);
1226err_dec_alloc:
1227 v4l2_device_unregister(&dev->v4l2_dev);
1228err_v4l2_dev_reg:
Kamil Debski2e731e42013-01-03 11:02:07 -03001229 s5p_mfc_release_firmware(dev);
1230err_alloc_fw:
Kamil Debskiaf935742011-06-21 10:51:26 -03001231 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1232err_mem_init_ctx_1:
1233 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
Kamil Debskiaf935742011-06-21 10:51:26 -03001234err_res:
1235 s5p_mfc_final_pm(dev);
Sachin Kamatd310f472012-05-14 08:22:27 -03001236
Kamil Debskiaf935742011-06-21 10:51:26 -03001237 pr_debug("%s-- with error\n", __func__);
1238 return ret;
1239
1240}
1241
1242/* Remove the driver */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001243static int s5p_mfc_remove(struct platform_device *pdev)
Kamil Debskiaf935742011-06-21 10:51:26 -03001244{
1245 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1246
1247 v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1248
1249 del_timer_sync(&dev->watchdog_timer);
1250 flush_workqueue(dev->watchdog_workqueue);
1251 destroy_workqueue(dev->watchdog_workqueue);
1252
1253 video_unregister_device(dev->vfd_enc);
1254 video_unregister_device(dev->vfd_dec);
1255 v4l2_device_unregister(&dev->v4l2_dev);
Kamil Debski2e731e42013-01-03 11:02:07 -03001256 s5p_mfc_release_firmware(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001257 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1258 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001259 if (pdev->dev.of_node) {
1260 put_device(dev->mem_dev_l);
1261 put_device(dev->mem_dev_r);
1262 }
Kamil Debskiaf935742011-06-21 10:51:26 -03001263
Kamil Debskiaf935742011-06-21 10:51:26 -03001264 s5p_mfc_final_pm(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001265 return 0;
1266}
1267
1268#ifdef CONFIG_PM_SLEEP
1269
1270static int s5p_mfc_suspend(struct device *dev)
1271{
1272 struct platform_device *pdev = to_platform_device(dev);
1273 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1274 int ret;
1275
1276 if (m_dev->num_inst == 0)
1277 return 0;
Sachin Kamat81c9bcf2012-09-28 04:01:35 -03001278
Kamil Debskiaf935742011-06-21 10:51:26 -03001279 if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1280 mfc_err("Error: going to suspend for a second time\n");
1281 return -EIO;
1282 }
1283
1284 /* Check if we're processing then wait if it necessary. */
1285 while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1286 /* Try and lock the HW */
1287 /* Wait on the interrupt waitqueue */
1288 ret = wait_event_interruptible_timeout(m_dev->queue,
Prathyush K76a4ddb2013-10-04 01:47:19 -03001289 m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
Kamil Debskiaf935742011-06-21 10:51:26 -03001290 if (ret == 0) {
1291 mfc_err("Waiting for hardware to finish timed out\n");
Prathyush K64370992014-10-21 08:06:57 -03001292 clear_bit(0, &m_dev->enter_suspend);
Kamil Debskiaf935742011-06-21 10:51:26 -03001293 return -EIO;
1294 }
1295 }
Sachin Kamat81c9bcf2012-09-28 04:01:35 -03001296
Prathyush K64370992014-10-21 08:06:57 -03001297 ret = s5p_mfc_sleep(m_dev);
1298 if (ret) {
1299 clear_bit(0, &m_dev->enter_suspend);
1300 clear_bit(0, &m_dev->hw_lock);
1301 }
1302 return ret;
Kamil Debskiaf935742011-06-21 10:51:26 -03001303}
1304
1305static int s5p_mfc_resume(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 if (m_dev->num_inst == 0)
1311 return 0;
1312 return s5p_mfc_wakeup(m_dev);
1313}
1314#endif
1315
1316#ifdef CONFIG_PM_RUNTIME
1317static int s5p_mfc_runtime_suspend(struct device *dev)
1318{
1319 struct platform_device *pdev = to_platform_device(dev);
1320 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1321
1322 atomic_set(&m_dev->pm.power, 0);
1323 return 0;
1324}
1325
1326static int s5p_mfc_runtime_resume(struct device *dev)
1327{
1328 struct platform_device *pdev = to_platform_device(dev);
1329 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001330
1331 if (!m_dev->alloc_ctx)
1332 return 0;
Kamil Debskiaf935742011-06-21 10:51:26 -03001333 atomic_set(&m_dev->pm.power, 1);
1334 return 0;
1335}
1336#endif
1337
1338/* Power management */
1339static const struct dev_pm_ops s5p_mfc_pm_ops = {
1340 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1341 SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
1342 NULL)
1343};
1344
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001345static struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
Arun Kumar K8f532a72012-10-03 22:19:09 -03001346 .h264_ctx = MFC_H264_CTX_BUF_SIZE,
1347 .non_h264_ctx = MFC_CTX_BUF_SIZE,
1348 .dsc = DESC_BUF_SIZE,
1349 .shm = SHARED_BUF_SIZE,
1350};
1351
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001352static struct s5p_mfc_buf_size buf_size_v5 = {
Arun Kumar K8f532a72012-10-03 22:19:09 -03001353 .fw = MAX_FW_SIZE,
1354 .cpb = MAX_CPB_SIZE,
1355 .priv = &mfc_buf_size_v5,
1356};
1357
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001358static struct s5p_mfc_buf_align mfc_buf_align_v5 = {
Arun Kumar K8f532a72012-10-03 22:19:09 -03001359 .base = MFC_BASE_ALIGN_ORDER,
1360};
1361
1362static struct s5p_mfc_variant mfc_drvdata_v5 = {
1363 .version = MFC_VERSION,
Kamil Debski9aa5f002014-05-20 10:15:13 -03001364 .version_bit = MFC_V5_BIT,
Arun Kumar K8f532a72012-10-03 22:19:09 -03001365 .port_num = MFC_NUM_PORTS,
1366 .buf_size = &buf_size_v5,
1367 .buf_align = &mfc_buf_align_v5,
Arun Kumar K77ba6b72014-05-21 06:29:30 -03001368 .fw_name[0] = "s5p-mfc.fw",
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001369};
1370
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001371static struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001372 .dev_ctx = MFC_CTX_BUF_SIZE_V6,
1373 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1374 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1375 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1376 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1377};
1378
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001379static struct s5p_mfc_buf_size buf_size_v6 = {
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001380 .fw = MAX_FW_SIZE_V6,
1381 .cpb = MAX_CPB_SIZE_V6,
1382 .priv = &mfc_buf_size_v6,
1383};
1384
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001385static struct s5p_mfc_buf_align mfc_buf_align_v6 = {
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001386 .base = 0,
1387};
1388
1389static struct s5p_mfc_variant mfc_drvdata_v6 = {
1390 .version = MFC_VERSION_V6,
Kamil Debski9aa5f002014-05-20 10:15:13 -03001391 .version_bit = MFC_V6_BIT,
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001392 .port_num = MFC_NUM_PORTS_V6,
1393 .buf_size = &buf_size_v6,
1394 .buf_align = &mfc_buf_align_v6,
Arun Kumar K77ba6b72014-05-21 06:29:30 -03001395 .fw_name[0] = "s5p-mfc-v6.fw",
1396 /*
1397 * v6-v2 firmware contains bug fixes and interface change
1398 * for init buffer command
1399 */
1400 .fw_name[1] = "s5p-mfc-v6-v2.fw",
Arun Kumar K8f532a72012-10-03 22:19:09 -03001401};
1402
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001403static struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001404 .dev_ctx = MFC_CTX_BUF_SIZE_V7,
1405 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
1406 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
1407 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
1408 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
1409};
1410
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001411static struct s5p_mfc_buf_size buf_size_v7 = {
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001412 .fw = MAX_FW_SIZE_V7,
1413 .cpb = MAX_CPB_SIZE_V7,
1414 .priv = &mfc_buf_size_v7,
1415};
1416
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001417static struct s5p_mfc_buf_align mfc_buf_align_v7 = {
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001418 .base = 0,
1419};
1420
1421static struct s5p_mfc_variant mfc_drvdata_v7 = {
1422 .version = MFC_VERSION_V7,
Kamil Debski9aa5f002014-05-20 10:15:13 -03001423 .version_bit = MFC_V7_BIT,
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001424 .port_num = MFC_NUM_PORTS_V7,
1425 .buf_size = &buf_size_v7,
1426 .buf_align = &mfc_buf_align_v7,
Arun Kumar K77ba6b72014-05-21 06:29:30 -03001427 .fw_name[0] = "s5p-mfc-v7.fw",
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001428};
1429
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001430static struct s5p_mfc_buf_size_v6 mfc_buf_size_v8 = {
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001431 .dev_ctx = MFC_CTX_BUF_SIZE_V8,
1432 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V8,
1433 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V8,
Kiran AVND3e594ce72014-05-19 09:50:02 -03001434 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V8,
1435 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V8,
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001436};
1437
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001438static struct s5p_mfc_buf_size buf_size_v8 = {
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001439 .fw = MAX_FW_SIZE_V8,
1440 .cpb = MAX_CPB_SIZE_V8,
1441 .priv = &mfc_buf_size_v8,
1442};
1443
Mauro Carvalho Chehabca5ea0c2014-09-24 09:08:10 -03001444static struct s5p_mfc_buf_align mfc_buf_align_v8 = {
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001445 .base = 0,
1446};
1447
1448static struct s5p_mfc_variant mfc_drvdata_v8 = {
1449 .version = MFC_VERSION_V8,
1450 .version_bit = MFC_V8_BIT,
1451 .port_num = MFC_NUM_PORTS_V8,
1452 .buf_size = &buf_size_v8,
1453 .buf_align = &mfc_buf_align_v8,
Arun Kumar K77ba6b72014-05-21 06:29:30 -03001454 .fw_name[0] = "s5p-mfc-v8.fw",
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001455};
1456
Arun Kumar K8f532a72012-10-03 22:19:09 -03001457static struct platform_device_id mfc_driver_ids[] = {
1458 {
1459 .name = "s5p-mfc",
1460 .driver_data = (unsigned long)&mfc_drvdata_v5,
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001461 }, {
1462 .name = "s5p-mfc-v5",
1463 .driver_data = (unsigned long)&mfc_drvdata_v5,
1464 }, {
1465 .name = "s5p-mfc-v6",
1466 .driver_data = (unsigned long)&mfc_drvdata_v6,
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001467 }, {
1468 .name = "s5p-mfc-v7",
1469 .driver_data = (unsigned long)&mfc_drvdata_v7,
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001470 }, {
1471 .name = "s5p-mfc-v8",
1472 .driver_data = (unsigned long)&mfc_drvdata_v8,
Arun Kumar K8f532a72012-10-03 22:19:09 -03001473 },
1474 {},
1475};
1476MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
1477
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001478static const struct of_device_id exynos_mfc_match[] = {
1479 {
1480 .compatible = "samsung,mfc-v5",
1481 .data = &mfc_drvdata_v5,
1482 }, {
1483 .compatible = "samsung,mfc-v6",
1484 .data = &mfc_drvdata_v6,
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001485 }, {
1486 .compatible = "samsung,mfc-v7",
1487 .data = &mfc_drvdata_v7,
Kiran AVNDe2b9deb2014-05-19 09:50:01 -03001488 }, {
1489 .compatible = "samsung,mfc-v8",
1490 .data = &mfc_drvdata_v8,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001491 },
1492 {},
1493};
1494MODULE_DEVICE_TABLE(of, exynos_mfc_match);
1495
1496static void *mfc_get_drv_data(struct platform_device *pdev)
1497{
1498 struct s5p_mfc_variant *driver_data = NULL;
1499
1500 if (pdev->dev.of_node) {
1501 const struct of_device_id *match;
Sachin Kamata40a1382013-05-23 00:51:19 -03001502 match = of_match_node(exynos_mfc_match,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001503 pdev->dev.of_node);
1504 if (match)
1505 driver_data = (struct s5p_mfc_variant *)match->data;
1506 } else {
1507 driver_data = (struct s5p_mfc_variant *)
1508 platform_get_device_id(pdev)->driver_data;
1509 }
1510 return driver_data;
1511}
1512
Kamil Debski1e393e92011-08-08 13:12:51 -03001513static struct platform_driver s5p_mfc_driver = {
Arun Kumar K8f532a72012-10-03 22:19:09 -03001514 .probe = s5p_mfc_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001515 .remove = s5p_mfc_remove,
Arun Kumar K8f532a72012-10-03 22:19:09 -03001516 .id_table = mfc_driver_ids,
Kamil Debskiaf935742011-06-21 10:51:26 -03001517 .driver = {
1518 .name = S5P_MFC_NAME,
1519 .owner = THIS_MODULE,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001520 .pm = &s5p_mfc_pm_ops,
1521 .of_match_table = exynos_mfc_match,
Kamil Debskiaf935742011-06-21 10:51:26 -03001522 },
1523};
1524
Axel Lin1d6629b2012-01-10 03:21:49 -03001525module_platform_driver(s5p_mfc_driver);
Kamil Debskiaf935742011-06-21 10:51:26 -03001526
1527MODULE_LICENSE("GPL");
1528MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1529MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");
1530