blob: e2aac592d29f6a3be2ec81ade46a89cb7e9727ee [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
40int debug;
41module_param(debug, int, S_IRUGO | S_IWUSR);
42MODULE_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;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300153 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);
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);
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 }
175unlock:
176 if (mutex_locked)
177 mutex_unlock(&dev->mfc_mutex);
178}
179
Kamil Debskiaf935742011-06-21 10:51:26 -0300180static void s5p_mfc_clear_int_flags(struct s5p_mfc_dev *dev)
181{
182 mfc_write(dev, 0, S5P_FIMV_RISC_HOST_INT);
183 mfc_write(dev, 0, S5P_FIMV_RISC2HOST_CMD);
184 mfc_write(dev, 0xffff, S5P_FIMV_SI_RTN_CHID);
185}
186
187static void s5p_mfc_handle_frame_all_extracted(struct s5p_mfc_ctx *ctx)
188{
189 struct s5p_mfc_buf *dst_buf;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300190 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300191
192 ctx->state = MFCINST_FINISHED;
193 ctx->sequence++;
194 while (!list_empty(&ctx->dst_queue)) {
195 dst_buf = list_entry(ctx->dst_queue.next,
196 struct s5p_mfc_buf, list);
197 mfc_debug(2, "Cleaning up buffer: %d\n",
198 dst_buf->b->v4l2_buf.index);
199 vb2_set_plane_payload(dst_buf->b, 0, 0);
200 vb2_set_plane_payload(dst_buf->b, 1, 0);
201 list_del(&dst_buf->list);
202 ctx->dst_queue_cnt--;
203 dst_buf->b->v4l2_buf.sequence = (ctx->sequence++);
204
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300205 if (s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_top, ctx) ==
206 s5p_mfc_hw_call(dev->mfc_ops, get_pic_type_bot, ctx))
Kamil Debskiaf935742011-06-21 10:51:26 -0300207 dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
208 else
209 dst_buf->b->v4l2_buf.field = V4L2_FIELD_INTERLACED;
210
211 ctx->dec_dst_flag &= ~(1 << dst_buf->b->v4l2_buf.index);
212 vb2_buffer_done(dst_buf->b, VB2_BUF_STATE_DONE);
213 }
214}
215
216static void s5p_mfc_handle_frame_copy_time(struct s5p_mfc_ctx *ctx)
217{
218 struct s5p_mfc_dev *dev = ctx->dev;
219 struct s5p_mfc_buf *dst_buf, *src_buf;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300220 size_t dec_y_addr;
221 unsigned int frame_type;
222
223 dec_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dec_y_adr, dev);
224 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_dec_frame_type, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300225
226 /* Copy timestamp / timecode from decoded src to dst and set
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300227 appropriate flags */
Kamil Debskiaf935742011-06-21 10:51:26 -0300228 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf, list);
229 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300230 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dec_y_addr) {
Kamil Debski56006012013-04-24 10:31:05 -0300231 dst_buf->b->v4l2_buf.timecode =
232 src_buf->b->v4l2_buf.timecode;
233 dst_buf->b->v4l2_buf.timestamp =
234 src_buf->b->v4l2_buf.timestamp;
Kamil Debskiaf935742011-06-21 10:51:26 -0300235 switch (frame_type) {
236 case S5P_FIMV_DECODE_FRAME_I_FRAME:
237 dst_buf->b->v4l2_buf.flags |=
238 V4L2_BUF_FLAG_KEYFRAME;
239 break;
240 case S5P_FIMV_DECODE_FRAME_P_FRAME:
241 dst_buf->b->v4l2_buf.flags |=
242 V4L2_BUF_FLAG_PFRAME;
243 break;
244 case S5P_FIMV_DECODE_FRAME_B_FRAME:
245 dst_buf->b->v4l2_buf.flags |=
246 V4L2_BUF_FLAG_BFRAME;
247 break;
248 }
249 break;
250 }
251 }
252}
253
254static void s5p_mfc_handle_frame_new(struct s5p_mfc_ctx *ctx, unsigned int err)
255{
256 struct s5p_mfc_dev *dev = ctx->dev;
257 struct s5p_mfc_buf *dst_buf;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300258 size_t dspl_y_addr;
259 unsigned int frame_type;
Kamil Debskiaf935742011-06-21 10:51:26 -0300260
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300261 dspl_y_addr = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_y_adr, dev);
Arun Kumar K90c0ae52013-02-26 18:02:11 -0300262 frame_type = s5p_mfc_hw_call(dev->mfc_ops, get_disp_frame_type, ctx);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300263
Kamil Debskiaf935742011-06-21 10:51:26 -0300264 /* If frame is same as previous then skip and do not dequeue */
265 if (frame_type == S5P_FIMV_DECODE_FRAME_SKIPPED) {
266 if (!ctx->after_packed_pb)
267 ctx->sequence++;
268 ctx->after_packed_pb = 0;
269 return;
270 }
271 ctx->sequence++;
272 /* The MFC returns address of the buffer, now we have to
273 * check which videobuf does it correspond to */
274 list_for_each_entry(dst_buf, &ctx->dst_queue, list) {
275 /* Check if this is the buffer we're looking for */
Marek Szyprowskiba7fcb02011-08-29 03:20:56 -0300276 if (vb2_dma_contig_plane_dma_addr(dst_buf->b, 0) == dspl_y_addr) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300277 list_del(&dst_buf->list);
278 ctx->dst_queue_cnt--;
279 dst_buf->b->v4l2_buf.sequence = ctx->sequence;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300280 if (s5p_mfc_hw_call(dev->mfc_ops,
281 get_pic_type_top, ctx) ==
282 s5p_mfc_hw_call(dev->mfc_ops,
283 get_pic_type_bot, ctx))
Kamil Debskiaf935742011-06-21 10:51:26 -0300284 dst_buf->b->v4l2_buf.field = V4L2_FIELD_NONE;
285 else
286 dst_buf->b->v4l2_buf.field =
287 V4L2_FIELD_INTERLACED;
288 vb2_set_plane_payload(dst_buf->b, 0, ctx->luma_size);
289 vb2_set_plane_payload(dst_buf->b, 1, ctx->chroma_size);
290 clear_bit(dst_buf->b->v4l2_buf.index,
291 &ctx->dec_dst_flag);
292
293 vb2_buffer_done(dst_buf->b,
294 err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
295
Kamil Debskiaf935742011-06-21 10:51:26 -0300296 break;
297 }
298 }
299}
300
301/* Handle frame decoding interrupt */
302static void s5p_mfc_handle_frame(struct s5p_mfc_ctx *ctx,
303 unsigned int reason, unsigned int err)
304{
305 struct s5p_mfc_dev *dev = ctx->dev;
306 unsigned int dst_frame_status;
307 struct s5p_mfc_buf *src_buf;
308 unsigned long flags;
309 unsigned int res_change;
310
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300311 dst_frame_status = s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
Kamil Debskiaf935742011-06-21 10:51:26 -0300312 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300313 res_change = (s5p_mfc_hw_call(dev->mfc_ops, get_dspl_status, dev)
314 & S5P_FIMV_DEC_STATUS_RESOLUTION_MASK)
315 >> S5P_FIMV_DEC_STATUS_RESOLUTION_SHIFT;
Kamil Debskiaf935742011-06-21 10:51:26 -0300316 mfc_debug(2, "Frame Status: %x\n", dst_frame_status);
317 if (ctx->state == MFCINST_RES_CHANGE_INIT)
318 ctx->state = MFCINST_RES_CHANGE_FLUSH;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300319 if (res_change == S5P_FIMV_RES_INCREASE ||
320 res_change == S5P_FIMV_RES_DECREASE) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300321 ctx->state = MFCINST_RES_CHANGE_INIT;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300322 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300323 wake_up_ctx(ctx, reason, err);
324 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
325 BUG();
326 s5p_mfc_clock_off();
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300327 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300328 return;
329 }
330 if (ctx->dpb_flush_flag)
331 ctx->dpb_flush_flag = 0;
332
333 spin_lock_irqsave(&dev->irqlock, flags);
334 /* All frames remaining in the buffer have been extracted */
335 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_EMPTY) {
336 if (ctx->state == MFCINST_RES_CHANGE_FLUSH) {
337 s5p_mfc_handle_frame_all_extracted(ctx);
338 ctx->state = MFCINST_RES_CHANGE_END;
339 goto leave_handle_frame;
340 } else {
341 s5p_mfc_handle_frame_all_extracted(ctx);
342 }
343 }
344
345 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY ||
346 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_ONLY)
347 s5p_mfc_handle_frame_copy_time(ctx);
348
349 /* A frame has been decoded and is in the buffer */
350 if (dst_frame_status == S5P_FIMV_DEC_STATUS_DISPLAY_ONLY ||
351 dst_frame_status == S5P_FIMV_DEC_STATUS_DECODING_DISPLAY) {
352 s5p_mfc_handle_frame_new(ctx, err);
353 } else {
354 mfc_debug(2, "No frame decode\n");
355 }
356 /* Mark source buffer as complete */
357 if (dst_frame_status != S5P_FIMV_DEC_STATUS_DISPLAY_ONLY
358 && !list_empty(&ctx->src_queue)) {
359 src_buf = list_entry(ctx->src_queue.next, struct s5p_mfc_buf,
360 list);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300361 ctx->consumed_stream += s5p_mfc_hw_call(dev->mfc_ops,
362 get_consumed_stream, dev);
363 if (ctx->codec_mode != S5P_MFC_CODEC_H264_DEC &&
Arun Kumar Kd2a0db1e2012-11-14 09:26:45 -0300364 ctx->consumed_stream + STUFF_BYTE <
365 src_buf->b->v4l2_planes[0].bytesused) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300366 /* Run MFC again on the same buffer */
367 mfc_debug(2, "Running again the same buffer\n");
368 ctx->after_packed_pb = 1;
369 } else {
Kamil Debskiaf935742011-06-21 10:51:26 -0300370 mfc_debug(2, "MFC needs next buffer\n");
371 ctx->consumed_stream = 0;
Kamil Debskia34026e2013-01-11 12:29:33 -0300372 if (src_buf->flags & MFC_BUF_FLAG_EOS)
373 ctx->state = MFCINST_FINISHING;
Kamil Debskiaf935742011-06-21 10:51:26 -0300374 list_del(&src_buf->list);
375 ctx->src_queue_cnt--;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300376 if (s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) > 0)
Kamil Debskiaf935742011-06-21 10:51:26 -0300377 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_ERROR);
378 else
379 vb2_buffer_done(src_buf->b, VB2_BUF_STATE_DONE);
380 }
381 }
382leave_handle_frame:
383 spin_unlock_irqrestore(&dev->irqlock, flags);
384 if ((ctx->src_queue_cnt == 0 && ctx->state != MFCINST_FINISHING)
Arun Kumar Ke9d98dd2013-04-24 09:41:53 -0300385 || ctx->dst_queue_cnt < ctx->pb_count)
Kamil Debskiaf935742011-06-21 10:51:26 -0300386 clear_work_bit(ctx);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300387 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300388 wake_up_ctx(ctx, reason, err);
389 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
390 BUG();
391 s5p_mfc_clock_off();
Prathyush K76a4ddb2013-10-04 01:47:19 -0300392 /* if suspending, wake up device and do not try_run again*/
393 if (test_bit(0, &dev->enter_suspend))
394 wake_up_dev(dev, reason, err);
395 else
396 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300397}
398
399/* Error handling for interrupt */
Kamil Debski7296e252012-12-21 05:32:59 -0300400static void s5p_mfc_handle_error(struct s5p_mfc_dev *dev,
401 struct s5p_mfc_ctx *ctx, unsigned int reason, unsigned int err)
Kamil Debskiaf935742011-06-21 10:51:26 -0300402{
Kamil Debskiaf935742011-06-21 10:51:26 -0300403 unsigned long flags;
404
Kamil Debskiaf935742011-06-21 10:51:26 -0300405 mfc_err("Interrupt Error: %08x\n", err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300406
Kamil Debski7296e252012-12-21 05:32:59 -0300407 if (ctx != NULL) {
408 /* Error recovery is dependent on the state of context */
409 switch (ctx->state) {
410 case MFCINST_RES_CHANGE_INIT:
411 case MFCINST_RES_CHANGE_FLUSH:
412 case MFCINST_RES_CHANGE_END:
413 case MFCINST_FINISHING:
414 case MFCINST_FINISHED:
415 case MFCINST_RUNNING:
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300416 /* It is highly probable that an error occurred
Kamil Debski7296e252012-12-21 05:32:59 -0300417 * while decoding a frame */
418 clear_work_bit(ctx);
419 ctx->state = MFCINST_ERROR;
420 /* Mark all dst buffers as having an error */
421 spin_lock_irqsave(&dev->irqlock, flags);
422 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue,
423 &ctx->dst_queue, &ctx->vq_dst);
424 /* Mark all src buffers as having an error */
425 s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue,
426 &ctx->src_queue, &ctx->vq_src);
427 spin_unlock_irqrestore(&dev->irqlock, flags);
428 wake_up_ctx(ctx, reason, err);
429 break;
430 default:
431 clear_work_bit(ctx);
432 ctx->state = MFCINST_ERROR;
433 wake_up_ctx(ctx, reason, err);
434 break;
435 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300436 }
Kamil Debski7296e252012-12-21 05:32:59 -0300437 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
438 BUG();
439 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
440 s5p_mfc_clock_off();
441 wake_up_dev(dev, reason, err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300442 return;
443}
444
445/* Header parsing interrupt handling */
446static void s5p_mfc_handle_seq_done(struct s5p_mfc_ctx *ctx,
447 unsigned int reason, unsigned int err)
448{
449 struct s5p_mfc_dev *dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300450
Sachin Kamat12597622012-05-10 03:32:00 -0300451 if (ctx == NULL)
Kamil Debskiaf935742011-06-21 10:51:26 -0300452 return;
453 dev = ctx->dev;
454 if (ctx->c_ops->post_seq_start) {
455 if (ctx->c_ops->post_seq_start(ctx))
456 mfc_err("post_seq_start() failed\n");
457 } else {
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300458 ctx->img_width = s5p_mfc_hw_call(dev->mfc_ops, get_img_width,
459 dev);
460 ctx->img_height = s5p_mfc_hw_call(dev->mfc_ops, get_img_height,
461 dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300462
Arun Kumar K8f532a72012-10-03 22:19:09 -0300463 s5p_mfc_hw_call(dev->mfc_ops, dec_calc_dpb_size, ctx);
464
Arun Kumar Ke9d98dd2013-04-24 09:41:53 -0300465 ctx->pb_count = s5p_mfc_hw_call(dev->mfc_ops, get_dpb_count,
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300466 dev);
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300467 ctx->mv_count = s5p_mfc_hw_call(dev->mfc_ops, get_mv_count,
468 dev);
Julia Lawallbb869362012-01-12 17:49:30 -0300469 if (ctx->img_width == 0 || ctx->img_height == 0)
Kamil Debskiaf935742011-06-21 10:51:26 -0300470 ctx->state = MFCINST_ERROR;
471 else
472 ctx->state = MFCINST_HEAD_PARSED;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300473
474 if ((ctx->codec_mode == S5P_MFC_CODEC_H264_DEC ||
475 ctx->codec_mode == S5P_MFC_CODEC_H264_MVC_DEC) &&
476 !list_empty(&ctx->src_queue)) {
477 struct s5p_mfc_buf *src_buf;
478 src_buf = list_entry(ctx->src_queue.next,
479 struct s5p_mfc_buf, list);
480 if (s5p_mfc_hw_call(dev->mfc_ops, get_consumed_stream,
481 dev) <
482 src_buf->b->v4l2_planes[0].bytesused)
483 ctx->head_processed = 0;
484 else
485 ctx->head_processed = 1;
486 } else {
487 ctx->head_processed = 1;
488 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300489 }
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300490 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300491 clear_work_bit(ctx);
492 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
493 BUG();
494 s5p_mfc_clock_off();
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300495 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300496 wake_up_ctx(ctx, reason, err);
497}
498
499/* Header parsing interrupt handling */
500static void s5p_mfc_handle_init_buffers(struct s5p_mfc_ctx *ctx,
501 unsigned int reason, unsigned int err)
502{
503 struct s5p_mfc_buf *src_buf;
504 struct s5p_mfc_dev *dev;
505 unsigned long flags;
506
Sachin Kamat12597622012-05-10 03:32:00 -0300507 if (ctx == NULL)
Kamil Debskiaf935742011-06-21 10:51:26 -0300508 return;
509 dev = ctx->dev;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300510 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300511 ctx->int_type = reason;
512 ctx->int_err = err;
513 ctx->int_cond = 1;
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300514 clear_work_bit(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300515 if (err == 0) {
516 ctx->state = MFCINST_RUNNING;
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -0300517 if (!ctx->dpb_flush_flag && ctx->head_processed) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300518 spin_lock_irqsave(&dev->irqlock, flags);
519 if (!list_empty(&ctx->src_queue)) {
520 src_buf = list_entry(ctx->src_queue.next,
521 struct s5p_mfc_buf, list);
522 list_del(&src_buf->list);
523 ctx->src_queue_cnt--;
524 vb2_buffer_done(src_buf->b,
525 VB2_BUF_STATE_DONE);
526 }
527 spin_unlock_irqrestore(&dev->irqlock, flags);
528 } else {
529 ctx->dpb_flush_flag = 0;
530 }
531 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
532 BUG();
533
534 s5p_mfc_clock_off();
535
536 wake_up(&ctx->queue);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300537 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300538 } else {
539 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
540 BUG();
541
542 s5p_mfc_clock_off();
543
544 wake_up(&ctx->queue);
545 }
546}
547
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300548static void s5p_mfc_handle_stream_complete(struct s5p_mfc_ctx *ctx,
549 unsigned int reason, unsigned int err)
550{
551 struct s5p_mfc_dev *dev = ctx->dev;
552 struct s5p_mfc_buf *mb_entry;
553
Andrzej Hajda4130eab2013-05-28 03:26:16 -0300554 mfc_debug(2, "Stream completed\n");
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300555
556 s5p_mfc_clear_int_flags(dev);
557 ctx->int_type = reason;
558 ctx->int_err = err;
559 ctx->state = MFCINST_FINISHED;
560
561 spin_lock(&dev->irqlock);
562 if (!list_empty(&ctx->dst_queue)) {
563 mb_entry = list_entry(ctx->dst_queue.next, struct s5p_mfc_buf,
564 list);
565 list_del(&mb_entry->list);
566 ctx->dst_queue_cnt--;
567 vb2_set_plane_payload(mb_entry->b, 0, 0);
568 vb2_buffer_done(mb_entry->b, VB2_BUF_STATE_DONE);
569 }
570 spin_unlock(&dev->irqlock);
571
572 clear_work_bit(ctx);
573
Sachin Kamate8256442013-01-22 01:00:06 -0300574 WARN_ON(test_and_clear_bit(0, &dev->hw_lock) == 0);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300575
576 s5p_mfc_clock_off();
577 wake_up(&ctx->queue);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300578 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300579}
580
Kamil Debskiaf935742011-06-21 10:51:26 -0300581/* Interrupt processing */
582static irqreturn_t s5p_mfc_irq(int irq, void *priv)
583{
584 struct s5p_mfc_dev *dev = priv;
585 struct s5p_mfc_ctx *ctx;
586 unsigned int reason;
587 unsigned int err;
588
589 mfc_debug_enter();
590 /* Reset the timeout watchdog */
591 atomic_set(&dev->watchdog_cnt, 0);
592 ctx = dev->ctx[dev->curr_ctx];
593 /* Get the reason of interrupt and the error code */
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300594 reason = s5p_mfc_hw_call(dev->mfc_ops, get_int_reason, dev);
595 err = s5p_mfc_hw_call(dev->mfc_ops, get_int_err, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300596 mfc_debug(1, "Int reason: %d (err: %08x)\n", reason, err);
597 switch (reason) {
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300598 case S5P_MFC_R2H_CMD_ERR_RET:
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300599 /* An error has occurred */
Kamil Debskiaf935742011-06-21 10:51:26 -0300600 if (ctx->state == MFCINST_RUNNING &&
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300601 s5p_mfc_hw_call(dev->mfc_ops, err_dec, err) >=
602 dev->warn_start)
Kamil Debskiaf935742011-06-21 10:51:26 -0300603 s5p_mfc_handle_frame(ctx, reason, err);
604 else
Kamil Debski7296e252012-12-21 05:32:59 -0300605 s5p_mfc_handle_error(dev, ctx, reason, err);
Kamil Debskiaf935742011-06-21 10:51:26 -0300606 clear_bit(0, &dev->enter_suspend);
607 break;
608
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300609 case S5P_MFC_R2H_CMD_SLICE_DONE_RET:
610 case S5P_MFC_R2H_CMD_FIELD_DONE_RET:
611 case S5P_MFC_R2H_CMD_FRAME_DONE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300612 if (ctx->c_ops->post_frame_start) {
613 if (ctx->c_ops->post_frame_start(ctx))
614 mfc_err("post_frame_start() failed\n");
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300615 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300616 wake_up_ctx(ctx, reason, err);
617 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
618 BUG();
619 s5p_mfc_clock_off();
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300620 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300621 } else {
622 s5p_mfc_handle_frame(ctx, reason, err);
623 }
624 break;
625
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300626 case S5P_MFC_R2H_CMD_SEQ_DONE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300627 s5p_mfc_handle_seq_done(ctx, reason, err);
628 break;
629
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300630 case S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET:
631 ctx->inst_no = s5p_mfc_hw_call(dev->mfc_ops, get_inst_no, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300632 ctx->state = MFCINST_GOT_INST;
633 clear_work_bit(ctx);
634 wake_up(&ctx->queue);
635 goto irq_cleanup_hw;
636
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300637 case S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300638 clear_work_bit(ctx);
639 ctx->state = MFCINST_FREE;
640 wake_up(&ctx->queue);
641 goto irq_cleanup_hw;
642
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300643 case S5P_MFC_R2H_CMD_SYS_INIT_RET:
644 case S5P_MFC_R2H_CMD_FW_STATUS_RET:
645 case S5P_MFC_R2H_CMD_SLEEP_RET:
646 case S5P_MFC_R2H_CMD_WAKEUP_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300647 if (ctx)
648 clear_work_bit(ctx);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300649 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300650 wake_up_dev(dev, reason, err);
651 clear_bit(0, &dev->hw_lock);
652 clear_bit(0, &dev->enter_suspend);
653 break;
654
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300655 case S5P_MFC_R2H_CMD_INIT_BUFFERS_RET:
Kamil Debskiaf935742011-06-21 10:51:26 -0300656 s5p_mfc_handle_init_buffers(ctx, reason, err);
657 break;
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300658
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300659 case S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET:
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300660 s5p_mfc_handle_stream_complete(ctx, reason, err);
661 break;
662
Arun Kumar K8f23cc02012-11-22 06:15:55 -0300663 case S5P_MFC_R2H_CMD_DPB_FLUSH_RET:
664 clear_work_bit(ctx);
665 ctx->state = MFCINST_RUNNING;
666 wake_up(&ctx->queue);
667 goto irq_cleanup_hw;
668
Kamil Debskiaf935742011-06-21 10:51:26 -0300669 default:
670 mfc_debug(2, "Unknown int reason\n");
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300671 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300672 }
673 mfc_debug_leave();
674 return IRQ_HANDLED;
675irq_cleanup_hw:
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300676 s5p_mfc_hw_call(dev->mfc_ops, clear_int_flags, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300677 ctx->int_type = reason;
678 ctx->int_err = err;
679 ctx->int_cond = 1;
680 if (test_and_clear_bit(0, &dev->hw_lock) == 0)
681 mfc_err("Failed to unlock hw\n");
682
683 s5p_mfc_clock_off();
684
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300685 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300686 mfc_debug(2, "Exit via irq_cleanup_hw\n");
687 return IRQ_HANDLED;
688}
689
690/* Open an MFC node */
691static int s5p_mfc_open(struct file *file)
692{
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300693 struct video_device *vdev = video_devdata(file);
Kamil Debskiaf935742011-06-21 10:51:26 -0300694 struct s5p_mfc_dev *dev = video_drvdata(file);
695 struct s5p_mfc_ctx *ctx = NULL;
696 struct vb2_queue *q;
Kamil Debskiaf935742011-06-21 10:51:26 -0300697 int ret = 0;
698
699 mfc_debug_enter();
Hans Verkuilbc738302012-06-24 07:13:33 -0300700 if (mutex_lock_interruptible(&dev->mfc_mutex))
701 return -ERESTARTSYS;
Kamil Debskiaf935742011-06-21 10:51:26 -0300702 dev->num_inst++; /* It is guarded by mfc_mutex in vfd */
703 /* Allocate memory for context */
Sachin Kamatbae061b2012-08-17 03:22:55 -0300704 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Kamil Debskiaf935742011-06-21 10:51:26 -0300705 if (!ctx) {
706 mfc_err("Not enough memory\n");
707 ret = -ENOMEM;
708 goto err_alloc;
709 }
710 v4l2_fh_init(&ctx->fh, video_devdata(file));
711 file->private_data = &ctx->fh;
712 v4l2_fh_add(&ctx->fh);
713 ctx->dev = dev;
714 INIT_LIST_HEAD(&ctx->src_queue);
715 INIT_LIST_HEAD(&ctx->dst_queue);
716 ctx->src_queue_cnt = 0;
717 ctx->dst_queue_cnt = 0;
718 /* Get context number */
719 ctx->num = 0;
720 while (dev->ctx[ctx->num]) {
721 ctx->num++;
722 if (ctx->num >= MFC_NUM_CONTEXTS) {
723 mfc_err("Too many open contexts\n");
724 ret = -EBUSY;
725 goto err_no_ctx;
726 }
727 }
728 /* Mark context as idle */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300729 clear_work_bit_irqsave(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300730 dev->ctx[ctx->num] = ctx;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300731 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300732 ctx->type = MFCINST_DECODER;
733 ctx->c_ops = get_dec_codec_ops();
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300734 s5p_mfc_dec_init(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300735 /* Setup ctrl handler */
736 ret = s5p_mfc_dec_ctrls_setup(ctx);
737 if (ret) {
738 mfc_err("Failed to setup mfc controls\n");
739 goto err_ctrls_setup;
740 }
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300741 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300742 ctx->type = MFCINST_ENCODER;
743 ctx->c_ops = get_enc_codec_ops();
744 /* only for encoder */
745 INIT_LIST_HEAD(&ctx->ref_queue);
746 ctx->ref_queue_cnt = 0;
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300747 s5p_mfc_enc_init(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300748 /* Setup ctrl handler */
749 ret = s5p_mfc_enc_ctrls_setup(ctx);
750 if (ret) {
751 mfc_err("Failed to setup mfc controls\n");
752 goto err_ctrls_setup;
753 }
754 } else {
755 ret = -ENOENT;
756 goto err_bad_node;
757 }
758 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
759 ctx->inst_no = -1;
760 /* Load firmware if this is the first instance */
761 if (dev->num_inst == 1) {
762 dev->watchdog_timer.expires = jiffies +
763 msecs_to_jiffies(MFC_WATCHDOG_INTERVAL);
764 add_timer(&dev->watchdog_timer);
765 ret = s5p_mfc_power_on();
766 if (ret < 0) {
767 mfc_err("power on failed\n");
768 goto err_pwr_enable;
769 }
770 s5p_mfc_clock_on();
Kamil Debski2e731e42013-01-03 11:02:07 -0300771 ret = s5p_mfc_load_firmware(dev);
772 if (ret) {
773 s5p_mfc_clock_off();
774 goto err_load_fw;
775 }
Kamil Debskiaf935742011-06-21 10:51:26 -0300776 /* Init the FW */
777 ret = s5p_mfc_init_hw(dev);
Kamil Debski2e731e42013-01-03 11:02:07 -0300778 s5p_mfc_clock_off();
Kamil Debskiaf935742011-06-21 10:51:26 -0300779 if (ret)
780 goto err_init_hw;
Kamil Debskiaf935742011-06-21 10:51:26 -0300781 }
782 /* Init videobuf2 queue for CAPTURE */
783 q = &ctx->vq_dst;
784 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
785 q->drv_priv = &ctx->fh;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300786 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300787 q->io_modes = VB2_MMAP;
788 q->ops = get_dec_queue_ops();
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300789 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300790 q->io_modes = VB2_MMAP | VB2_USERPTR;
791 q->ops = get_enc_queue_ops();
792 } else {
793 ret = -ENOENT;
794 goto err_queue_init;
795 }
796 q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300797 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Kamil Debskiaf935742011-06-21 10:51:26 -0300798 ret = vb2_queue_init(q);
799 if (ret) {
800 mfc_err("Failed to initialize videobuf2 queue(capture)\n");
801 goto err_queue_init;
802 }
803 /* Init videobuf2 queue for OUTPUT */
804 q = &ctx->vq_src;
805 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
806 q->io_modes = VB2_MMAP;
807 q->drv_priv = &ctx->fh;
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300808 if (vdev == dev->vfd_dec) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300809 q->io_modes = VB2_MMAP;
810 q->ops = get_dec_queue_ops();
Marek Szyprowskib80cb8d2013-12-03 10:12:51 -0300811 } else if (vdev == dev->vfd_enc) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300812 q->io_modes = VB2_MMAP | VB2_USERPTR;
813 q->ops = get_enc_queue_ops();
814 } else {
815 ret = -ENOENT;
816 goto err_queue_init;
817 }
818 q->mem_ops = (struct vb2_mem_ops *)&vb2_dma_contig_memops;
Kamil Debski6aa69f92013-01-25 06:29:57 -0300819 q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Kamil Debskiaf935742011-06-21 10:51:26 -0300820 ret = vb2_queue_init(q);
821 if (ret) {
822 mfc_err("Failed to initialize videobuf2 queue(output)\n");
823 goto err_queue_init;
824 }
825 init_waitqueue_head(&ctx->queue);
Hans Verkuilbc738302012-06-24 07:13:33 -0300826 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300827 mfc_debug_leave();
828 return ret;
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300829 /* Deinit when failure occurred */
Kamil Debskiaf935742011-06-21 10:51:26 -0300830err_queue_init:
Kamil Debski2e731e42013-01-03 11:02:07 -0300831 if (dev->num_inst == 1)
832 s5p_mfc_deinit_hw(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300833err_init_hw:
Kamil Debski2e731e42013-01-03 11:02:07 -0300834err_load_fw:
Kamil Debskiaf935742011-06-21 10:51:26 -0300835err_pwr_enable:
836 if (dev->num_inst == 1) {
837 if (s5p_mfc_power_off() < 0)
838 mfc_err("power off failed\n");
Kamil Debski1b73ba02012-11-22 10:00:28 -0300839 del_timer_sync(&dev->watchdog_timer);
Kamil Debskiaf935742011-06-21 10:51:26 -0300840 }
841err_ctrls_setup:
842 s5p_mfc_dec_ctrls_delete(ctx);
843err_bad_node:
Kamil Debski1b73ba02012-11-22 10:00:28 -0300844 dev->ctx[ctx->num] = NULL;
Kamil Debskiaf935742011-06-21 10:51:26 -0300845err_no_ctx:
846 v4l2_fh_del(&ctx->fh);
847 v4l2_fh_exit(&ctx->fh);
848 kfree(ctx);
849err_alloc:
850 dev->num_inst--;
Hans Verkuilbc738302012-06-24 07:13:33 -0300851 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300852 mfc_debug_leave();
853 return ret;
854}
855
856/* Release MFC context */
857static int s5p_mfc_release(struct file *file)
858{
859 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
860 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300861
862 mfc_debug_enter();
Hans Verkuilbc738302012-06-24 07:13:33 -0300863 mutex_lock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300864 s5p_mfc_clock_on();
865 vb2_queue_release(&ctx->vq_src);
866 vb2_queue_release(&ctx->vq_dst);
867 /* Mark context as idle */
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300868 clear_work_bit_irqsave(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300869 /* If instance was initialised then
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300870 * return instance and free resources */
Kamil Debskiaf935742011-06-21 10:51:26 -0300871 if (ctx->inst_no != MFC_NO_INSTANCE_SET) {
872 mfc_debug(2, "Has to free instance\n");
873 ctx->state = MFCINST_RETURN_INST;
Andrzej Hajda7fb89ec2012-08-14 06:13:40 -0300874 set_work_bit_irqsave(ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300875 s5p_mfc_clean_ctx_int_flags(ctx);
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300876 s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300877 /* Wait until instance is returned or timeout occurred */
Kamil Debskiaf935742011-06-21 10:51:26 -0300878 if (s5p_mfc_wait_for_done_ctx
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300879 (ctx, S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET, 0)) {
Kamil Debskiaf935742011-06-21 10:51:26 -0300880 s5p_mfc_clock_off();
881 mfc_err("Err returning instance\n");
882 }
883 mfc_debug(2, "After free instance\n");
884 /* Free resources */
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300885 s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers, ctx);
886 s5p_mfc_hw_call(dev->mfc_ops, release_instance_buffer, ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300887 if (ctx->type == MFCINST_DECODER)
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300888 s5p_mfc_hw_call(dev->mfc_ops, release_dec_desc_buffer,
889 ctx);
Kamil Debskiaf935742011-06-21 10:51:26 -0300890
891 ctx->inst_no = MFC_NO_INSTANCE_SET;
892 }
893 /* hardware locking scheme */
894 if (dev->curr_ctx == ctx->num)
895 clear_bit(0, &dev->hw_lock);
896 dev->num_inst--;
897 if (dev->num_inst == 0) {
Kamil Debski2e731e42013-01-03 11:02:07 -0300898 mfc_debug(2, "Last instance\n");
Arun Kumar K43a1ea12012-10-03 22:19:08 -0300899 s5p_mfc_deinit_hw(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -0300900 del_timer_sync(&dev->watchdog_timer);
901 if (s5p_mfc_power_off() < 0)
902 mfc_err("Power off failed\n");
903 }
904 mfc_debug(2, "Shutting down clock\n");
905 s5p_mfc_clock_off();
Sachin Kamat12597622012-05-10 03:32:00 -0300906 dev->ctx[ctx->num] = NULL;
Kamil Debskiaf935742011-06-21 10:51:26 -0300907 s5p_mfc_dec_ctrls_delete(ctx);
908 v4l2_fh_del(&ctx->fh);
909 v4l2_fh_exit(&ctx->fh);
910 kfree(ctx);
911 mfc_debug_leave();
Hans Verkuilbc738302012-06-24 07:13:33 -0300912 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300913 return 0;
914}
915
916/* Poll */
917static unsigned int s5p_mfc_poll(struct file *file,
918 struct poll_table_struct *wait)
919{
920 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
921 struct s5p_mfc_dev *dev = ctx->dev;
922 struct vb2_queue *src_q, *dst_q;
923 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
924 unsigned int rc = 0;
925 unsigned long flags;
926
Hans Verkuilbc738302012-06-24 07:13:33 -0300927 mutex_lock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300928 src_q = &ctx->vq_src;
929 dst_q = &ctx->vq_dst;
930 /*
931 * There has to be at least one buffer queued on each queued_list, which
932 * means either in driver already or waiting for driver to claim it
933 * and start processing.
934 */
935 if ((!src_q->streaming || list_empty(&src_q->queued_list))
936 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
937 rc = POLLERR;
938 goto end;
939 }
940 mutex_unlock(&dev->mfc_mutex);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300941 poll_wait(file, &ctx->fh.wait, wait);
Kamil Debskiaf935742011-06-21 10:51:26 -0300942 poll_wait(file, &src_q->done_wq, wait);
943 poll_wait(file, &dst_q->done_wq, wait);
944 mutex_lock(&dev->mfc_mutex);
Andrzej Hajdaf9f715a2012-08-21 08:05:32 -0300945 if (v4l2_event_pending(&ctx->fh))
946 rc |= POLLPRI;
Kamil Debskiaf935742011-06-21 10:51:26 -0300947 spin_lock_irqsave(&src_q->done_lock, flags);
948 if (!list_empty(&src_q->done_list))
949 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
950 done_entry);
951 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
952 || src_vb->state == VB2_BUF_STATE_ERROR))
953 rc |= POLLOUT | POLLWRNORM;
954 spin_unlock_irqrestore(&src_q->done_lock, flags);
955 spin_lock_irqsave(&dst_q->done_lock, flags);
956 if (!list_empty(&dst_q->done_list))
957 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
958 done_entry);
959 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
960 || dst_vb->state == VB2_BUF_STATE_ERROR))
961 rc |= POLLIN | POLLRDNORM;
962 spin_unlock_irqrestore(&dst_q->done_lock, flags);
963end:
Hans Verkuilbc738302012-06-24 07:13:33 -0300964 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300965 return rc;
966}
967
968/* Mmap */
969static int s5p_mfc_mmap(struct file *file, struct vm_area_struct *vma)
970{
971 struct s5p_mfc_ctx *ctx = fh_to_ctx(file->private_data);
Hans Verkuilbc738302012-06-24 07:13:33 -0300972 struct s5p_mfc_dev *dev = ctx->dev;
Kamil Debskiaf935742011-06-21 10:51:26 -0300973 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
974 int ret;
Hans Verkuilbc738302012-06-24 07:13:33 -0300975
976 if (mutex_lock_interruptible(&dev->mfc_mutex))
977 return -ERESTARTSYS;
Kamil Debskiaf935742011-06-21 10:51:26 -0300978 if (offset < DST_QUEUE_OFF_BASE) {
979 mfc_debug(2, "mmaping source\n");
980 ret = vb2_mmap(&ctx->vq_src, vma);
981 } else { /* capture */
982 mfc_debug(2, "mmaping destination\n");
983 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
984 ret = vb2_mmap(&ctx->vq_dst, vma);
985 }
Hans Verkuilbc738302012-06-24 07:13:33 -0300986 mutex_unlock(&dev->mfc_mutex);
Kamil Debskiaf935742011-06-21 10:51:26 -0300987 return ret;
988}
989
990/* v4l2 ops */
991static const struct v4l2_file_operations s5p_mfc_fops = {
992 .owner = THIS_MODULE,
993 .open = s5p_mfc_open,
994 .release = s5p_mfc_release,
995 .poll = s5p_mfc_poll,
996 .unlocked_ioctl = video_ioctl2,
997 .mmap = s5p_mfc_mmap,
998};
999
1000static int match_child(struct device *dev, void *data)
1001{
1002 if (!dev_name(dev))
1003 return 0;
1004 return !strcmp(dev_name(dev), (char *)data);
1005}
1006
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001007static void *mfc_get_drv_data(struct platform_device *pdev);
1008
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001009static int s5p_mfc_alloc_memdevs(struct s5p_mfc_dev *dev)
1010{
Sylwester Nawrocki65fccab2013-04-10 07:17:52 -03001011 unsigned int mem_info[2] = { };
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001012
1013 dev->mem_dev_l = devm_kzalloc(&dev->plat_dev->dev,
1014 sizeof(struct device), GFP_KERNEL);
1015 if (!dev->mem_dev_l) {
1016 mfc_err("Not enough memory\n");
1017 return -ENOMEM;
1018 }
1019 device_initialize(dev->mem_dev_l);
1020 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1021 "samsung,mfc-l", mem_info, 2);
1022 if (dma_declare_coherent_memory(dev->mem_dev_l, mem_info[0],
1023 mem_info[0], mem_info[1],
1024 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1025 mfc_err("Failed to declare coherent memory for\n"
1026 "MFC device\n");
1027 return -ENOMEM;
1028 }
1029
1030 dev->mem_dev_r = devm_kzalloc(&dev->plat_dev->dev,
1031 sizeof(struct device), GFP_KERNEL);
1032 if (!dev->mem_dev_r) {
1033 mfc_err("Not enough memory\n");
1034 return -ENOMEM;
1035 }
1036 device_initialize(dev->mem_dev_r);
1037 of_property_read_u32_array(dev->plat_dev->dev.of_node,
1038 "samsung,mfc-r", mem_info, 2);
1039 if (dma_declare_coherent_memory(dev->mem_dev_r, mem_info[0],
1040 mem_info[0], mem_info[1],
1041 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE) == 0) {
1042 pr_err("Failed to declare coherent memory for\n"
1043 "MFC device\n");
1044 return -ENOMEM;
1045 }
1046 return 0;
1047}
1048
Kamil Debskiaf935742011-06-21 10:51:26 -03001049/* MFC probe function */
Kamil Debski1e393e92011-08-08 13:12:51 -03001050static int s5p_mfc_probe(struct platform_device *pdev)
Kamil Debskiaf935742011-06-21 10:51:26 -03001051{
1052 struct s5p_mfc_dev *dev;
1053 struct video_device *vfd;
1054 struct resource *res;
1055 int ret;
1056
1057 pr_debug("%s++\n", __func__);
Sachin Kamatbae061b2012-08-17 03:22:55 -03001058 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debskiaf935742011-06-21 10:51:26 -03001059 if (!dev) {
1060 dev_err(&pdev->dev, "Not enough memory for MFC device\n");
1061 return -ENOMEM;
1062 }
1063
1064 spin_lock_init(&dev->irqlock);
1065 spin_lock_init(&dev->condlock);
1066 dev->plat_dev = pdev;
1067 if (!dev->plat_dev) {
1068 dev_err(&pdev->dev, "No platform data specified\n");
Sachin Kamatd310f472012-05-14 08:22:27 -03001069 return -ENODEV;
Kamil Debskiaf935742011-06-21 10:51:26 -03001070 }
1071
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001072 dev->variant = mfc_get_drv_data(pdev);
Arun Kumar K8f532a72012-10-03 22:19:09 -03001073
Kamil Debskiaf935742011-06-21 10:51:26 -03001074 ret = s5p_mfc_init_pm(dev);
1075 if (ret < 0) {
1076 dev_err(&pdev->dev, "failed to get mfc clock source\n");
Sachin Kamatd310f472012-05-14 08:22:27 -03001077 return ret;
Kamil Debskiaf935742011-06-21 10:51:26 -03001078 }
1079
1080 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debskiaf935742011-06-21 10:51:26 -03001081
Thierry Redingf23999e2013-01-21 06:09:07 -03001082 dev->regs_base = devm_ioremap_resource(&pdev->dev, res);
1083 if (IS_ERR(dev->regs_base))
1084 return PTR_ERR(dev->regs_base);
Kamil Debskiaf935742011-06-21 10:51:26 -03001085
1086 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1087 if (res == NULL) {
1088 dev_err(&pdev->dev, "failed to get irq resource\n");
1089 ret = -ENOENT;
Sachin Kamatd310f472012-05-14 08:22:27 -03001090 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001091 }
1092 dev->irq = res->start;
Sachin Kamatd310f472012-05-14 08:22:27 -03001093 ret = devm_request_irq(&pdev->dev, dev->irq, s5p_mfc_irq,
Michael Opdenacker1957f0d2013-10-13 02:58:39 -03001094 0, pdev->name, dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001095 if (ret) {
1096 dev_err(&pdev->dev, "Failed to install irq (%d)\n", ret);
Sachin Kamatd310f472012-05-14 08:22:27 -03001097 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001098 }
1099
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001100 if (pdev->dev.of_node) {
Wei Yongjund68b44e2013-04-17 23:18:19 -03001101 ret = s5p_mfc_alloc_memdevs(dev);
1102 if (ret < 0)
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001103 goto err_res;
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001104 } else {
1105 dev->mem_dev_l = device_find_child(&dev->plat_dev->dev,
1106 "s5p-mfc-l", match_child);
1107 if (!dev->mem_dev_l) {
1108 mfc_err("Mem child (L) device get failed\n");
1109 ret = -ENODEV;
1110 goto err_res;
1111 }
1112 dev->mem_dev_r = device_find_child(&dev->plat_dev->dev,
1113 "s5p-mfc-r", match_child);
1114 if (!dev->mem_dev_r) {
1115 mfc_err("Mem child (R) device get failed\n");
1116 ret = -ENODEV;
1117 goto err_res;
1118 }
Kamil Debskiaf935742011-06-21 10:51:26 -03001119 }
1120
1121 dev->alloc_ctx[0] = vb2_dma_contig_init_ctx(dev->mem_dev_l);
Kamil Debskief89fff2013-01-03 07:06:03 -03001122 if (IS_ERR(dev->alloc_ctx[0])) {
Kamil Debskiaf935742011-06-21 10:51:26 -03001123 ret = PTR_ERR(dev->alloc_ctx[0]);
Sachin Kamatd310f472012-05-14 08:22:27 -03001124 goto err_res;
Kamil Debskiaf935742011-06-21 10:51:26 -03001125 }
1126 dev->alloc_ctx[1] = vb2_dma_contig_init_ctx(dev->mem_dev_r);
Kamil Debskief89fff2013-01-03 07:06:03 -03001127 if (IS_ERR(dev->alloc_ctx[1])) {
Kamil Debskiaf935742011-06-21 10:51:26 -03001128 ret = PTR_ERR(dev->alloc_ctx[1]);
1129 goto err_mem_init_ctx_1;
1130 }
1131
1132 mutex_init(&dev->mfc_mutex);
1133
Kamil Debski2e731e42013-01-03 11:02:07 -03001134 ret = s5p_mfc_alloc_firmware(dev);
1135 if (ret)
1136 goto err_alloc_fw;
1137
Kamil Debskiaf935742011-06-21 10:51:26 -03001138 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1139 if (ret)
1140 goto err_v4l2_dev_reg;
1141 init_waitqueue_head(&dev->queue);
1142
1143 /* decoder */
1144 vfd = video_device_alloc();
1145 if (!vfd) {
1146 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1147 ret = -ENOMEM;
1148 goto err_dec_alloc;
1149 }
1150 vfd->fops = &s5p_mfc_fops,
1151 vfd->ioctl_ops = get_dec_v4l2_ioctl_ops();
1152 vfd->release = video_device_release,
1153 vfd->lock = &dev->mfc_mutex;
1154 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil954f3402012-09-05 06:05:50 -03001155 vfd->vfl_dir = VFL_DIR_M2M;
Kamil Debskiaf935742011-06-21 10:51:26 -03001156 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_DEC_NAME);
1157 dev->vfd_dec = vfd;
1158 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1159 if (ret) {
1160 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1161 video_device_release(vfd);
1162 goto err_dec_reg;
1163 }
1164 v4l2_info(&dev->v4l2_dev,
1165 "decoder registered as /dev/video%d\n", vfd->num);
1166 video_set_drvdata(vfd, dev);
1167
1168 /* encoder */
1169 vfd = video_device_alloc();
1170 if (!vfd) {
1171 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
1172 ret = -ENOMEM;
1173 goto err_enc_alloc;
1174 }
1175 vfd->fops = &s5p_mfc_fops,
1176 vfd->ioctl_ops = get_enc_v4l2_ioctl_ops();
1177 vfd->release = video_device_release,
1178 vfd->lock = &dev->mfc_mutex;
1179 vfd->v4l2_dev = &dev->v4l2_dev;
Arun Kumar Kcdcf45e2012-10-04 16:14:56 -03001180 vfd->vfl_dir = VFL_DIR_M2M;
Kamil Debskiaf935742011-06-21 10:51:26 -03001181 snprintf(vfd->name, sizeof(vfd->name), "%s", S5P_MFC_ENC_NAME);
1182 dev->vfd_enc = vfd;
1183 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1184 if (ret) {
1185 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1186 video_device_release(vfd);
1187 goto err_enc_reg;
1188 }
1189 v4l2_info(&dev->v4l2_dev,
1190 "encoder registered as /dev/video%d\n", vfd->num);
1191 video_set_drvdata(vfd, dev);
1192 platform_set_drvdata(pdev, dev);
1193
1194 dev->hw_lock = 0;
1195 dev->watchdog_workqueue = create_singlethread_workqueue(S5P_MFC_NAME);
1196 INIT_WORK(&dev->watchdog_work, s5p_mfc_watchdog_worker);
1197 atomic_set(&dev->watchdog_cnt, 0);
1198 init_timer(&dev->watchdog_timer);
1199 dev->watchdog_timer.data = (unsigned long)dev;
1200 dev->watchdog_timer.function = s5p_mfc_watchdog;
1201
Arun Kumar K43a1ea12012-10-03 22:19:08 -03001202 /* Initialize HW ops and commands based on MFC version */
1203 s5p_mfc_init_hw_ops(dev);
1204 s5p_mfc_init_hw_cmds(dev);
1205
Kamil Debskiaf935742011-06-21 10:51:26 -03001206 pr_debug("%s--\n", __func__);
1207 return 0;
1208
1209/* Deinit MFC if probe had failed */
1210err_enc_reg:
1211 video_device_release(dev->vfd_enc);
1212err_enc_alloc:
1213 video_unregister_device(dev->vfd_dec);
1214err_dec_reg:
1215 video_device_release(dev->vfd_dec);
1216err_dec_alloc:
1217 v4l2_device_unregister(&dev->v4l2_dev);
1218err_v4l2_dev_reg:
Kamil Debski2e731e42013-01-03 11:02:07 -03001219 s5p_mfc_release_firmware(dev);
1220err_alloc_fw:
Kamil Debskiaf935742011-06-21 10:51:26 -03001221 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
1222err_mem_init_ctx_1:
1223 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
Kamil Debskiaf935742011-06-21 10:51:26 -03001224err_res:
1225 s5p_mfc_final_pm(dev);
Sachin Kamatd310f472012-05-14 08:22:27 -03001226
Kamil Debskiaf935742011-06-21 10:51:26 -03001227 pr_debug("%s-- with error\n", __func__);
1228 return ret;
1229
1230}
1231
1232/* Remove the driver */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001233static int s5p_mfc_remove(struct platform_device *pdev)
Kamil Debskiaf935742011-06-21 10:51:26 -03001234{
1235 struct s5p_mfc_dev *dev = platform_get_drvdata(pdev);
1236
1237 v4l2_info(&dev->v4l2_dev, "Removing %s\n", pdev->name);
1238
1239 del_timer_sync(&dev->watchdog_timer);
1240 flush_workqueue(dev->watchdog_workqueue);
1241 destroy_workqueue(dev->watchdog_workqueue);
1242
1243 video_unregister_device(dev->vfd_enc);
1244 video_unregister_device(dev->vfd_dec);
1245 v4l2_device_unregister(&dev->v4l2_dev);
Kamil Debski2e731e42013-01-03 11:02:07 -03001246 s5p_mfc_release_firmware(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001247 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[0]);
1248 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx[1]);
Arun Kumar K6e83e6e2013-01-18 15:42:34 -03001249 if (pdev->dev.of_node) {
1250 put_device(dev->mem_dev_l);
1251 put_device(dev->mem_dev_r);
1252 }
Kamil Debskiaf935742011-06-21 10:51:26 -03001253
Kamil Debskiaf935742011-06-21 10:51:26 -03001254 s5p_mfc_final_pm(dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001255 return 0;
1256}
1257
1258#ifdef CONFIG_PM_SLEEP
1259
1260static int s5p_mfc_suspend(struct device *dev)
1261{
1262 struct platform_device *pdev = to_platform_device(dev);
1263 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1264 int ret;
1265
1266 if (m_dev->num_inst == 0)
1267 return 0;
Sachin Kamat81c9bcf2012-09-28 04:01:35 -03001268
Kamil Debskiaf935742011-06-21 10:51:26 -03001269 if (test_and_set_bit(0, &m_dev->enter_suspend) != 0) {
1270 mfc_err("Error: going to suspend for a second time\n");
1271 return -EIO;
1272 }
1273
1274 /* Check if we're processing then wait if it necessary. */
1275 while (test_and_set_bit(0, &m_dev->hw_lock) != 0) {
1276 /* Try and lock the HW */
1277 /* Wait on the interrupt waitqueue */
1278 ret = wait_event_interruptible_timeout(m_dev->queue,
Prathyush K76a4ddb2013-10-04 01:47:19 -03001279 m_dev->int_cond, msecs_to_jiffies(MFC_INT_TIMEOUT));
Kamil Debskiaf935742011-06-21 10:51:26 -03001280 if (ret == 0) {
1281 mfc_err("Waiting for hardware to finish timed out\n");
1282 return -EIO;
1283 }
1284 }
Sachin Kamat81c9bcf2012-09-28 04:01:35 -03001285
1286 return s5p_mfc_sleep(m_dev);
Kamil Debskiaf935742011-06-21 10:51:26 -03001287}
1288
1289static int s5p_mfc_resume(struct device *dev)
1290{
1291 struct platform_device *pdev = to_platform_device(dev);
1292 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1293
1294 if (m_dev->num_inst == 0)
1295 return 0;
1296 return s5p_mfc_wakeup(m_dev);
1297}
1298#endif
1299
1300#ifdef CONFIG_PM_RUNTIME
1301static int s5p_mfc_runtime_suspend(struct device *dev)
1302{
1303 struct platform_device *pdev = to_platform_device(dev);
1304 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1305
1306 atomic_set(&m_dev->pm.power, 0);
1307 return 0;
1308}
1309
1310static int s5p_mfc_runtime_resume(struct device *dev)
1311{
1312 struct platform_device *pdev = to_platform_device(dev);
1313 struct s5p_mfc_dev *m_dev = platform_get_drvdata(pdev);
1314 int pre_power;
1315
1316 if (!m_dev->alloc_ctx)
1317 return 0;
1318 pre_power = atomic_read(&m_dev->pm.power);
1319 atomic_set(&m_dev->pm.power, 1);
1320 return 0;
1321}
1322#endif
1323
1324/* Power management */
1325static const struct dev_pm_ops s5p_mfc_pm_ops = {
1326 SET_SYSTEM_SLEEP_PM_OPS(s5p_mfc_suspend, s5p_mfc_resume)
1327 SET_RUNTIME_PM_OPS(s5p_mfc_runtime_suspend, s5p_mfc_runtime_resume,
1328 NULL)
1329};
1330
Arun Kumar K8f532a72012-10-03 22:19:09 -03001331struct s5p_mfc_buf_size_v5 mfc_buf_size_v5 = {
1332 .h264_ctx = MFC_H264_CTX_BUF_SIZE,
1333 .non_h264_ctx = MFC_CTX_BUF_SIZE,
1334 .dsc = DESC_BUF_SIZE,
1335 .shm = SHARED_BUF_SIZE,
1336};
1337
1338struct s5p_mfc_buf_size buf_size_v5 = {
1339 .fw = MAX_FW_SIZE,
1340 .cpb = MAX_CPB_SIZE,
1341 .priv = &mfc_buf_size_v5,
1342};
1343
1344struct s5p_mfc_buf_align mfc_buf_align_v5 = {
1345 .base = MFC_BASE_ALIGN_ORDER,
1346};
1347
1348static struct s5p_mfc_variant mfc_drvdata_v5 = {
1349 .version = MFC_VERSION,
1350 .port_num = MFC_NUM_PORTS,
1351 .buf_size = &buf_size_v5,
1352 .buf_align = &mfc_buf_align_v5,
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001353 .fw_name = "s5p-mfc.fw",
1354};
1355
1356struct s5p_mfc_buf_size_v6 mfc_buf_size_v6 = {
1357 .dev_ctx = MFC_CTX_BUF_SIZE_V6,
1358 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V6,
1359 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V6,
1360 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V6,
1361 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V6,
1362};
1363
1364struct s5p_mfc_buf_size buf_size_v6 = {
1365 .fw = MAX_FW_SIZE_V6,
1366 .cpb = MAX_CPB_SIZE_V6,
1367 .priv = &mfc_buf_size_v6,
1368};
1369
1370struct s5p_mfc_buf_align mfc_buf_align_v6 = {
1371 .base = 0,
1372};
1373
1374static struct s5p_mfc_variant mfc_drvdata_v6 = {
1375 .version = MFC_VERSION_V6,
1376 .port_num = MFC_NUM_PORTS_V6,
1377 .buf_size = &buf_size_v6,
1378 .buf_align = &mfc_buf_align_v6,
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001379 .fw_name = "s5p-mfc-v6.fw",
Arun Kumar K8f532a72012-10-03 22:19:09 -03001380};
1381
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001382struct s5p_mfc_buf_size_v6 mfc_buf_size_v7 = {
1383 .dev_ctx = MFC_CTX_BUF_SIZE_V7,
1384 .h264_dec_ctx = MFC_H264_DEC_CTX_BUF_SIZE_V7,
1385 .other_dec_ctx = MFC_OTHER_DEC_CTX_BUF_SIZE_V7,
1386 .h264_enc_ctx = MFC_H264_ENC_CTX_BUF_SIZE_V7,
1387 .other_enc_ctx = MFC_OTHER_ENC_CTX_BUF_SIZE_V7,
1388};
1389
1390struct s5p_mfc_buf_size buf_size_v7 = {
1391 .fw = MAX_FW_SIZE_V7,
1392 .cpb = MAX_CPB_SIZE_V7,
1393 .priv = &mfc_buf_size_v7,
1394};
1395
1396struct s5p_mfc_buf_align mfc_buf_align_v7 = {
1397 .base = 0,
1398};
1399
1400static struct s5p_mfc_variant mfc_drvdata_v7 = {
1401 .version = MFC_VERSION_V7,
1402 .port_num = MFC_NUM_PORTS_V7,
1403 .buf_size = &buf_size_v7,
1404 .buf_align = &mfc_buf_align_v7,
1405 .fw_name = "s5p-mfc-v7.fw",
1406};
1407
Arun Kumar K8f532a72012-10-03 22:19:09 -03001408static struct platform_device_id mfc_driver_ids[] = {
1409 {
1410 .name = "s5p-mfc",
1411 .driver_data = (unsigned long)&mfc_drvdata_v5,
Jeongtae Parkf96f3cf2012-10-03 22:19:11 -03001412 }, {
1413 .name = "s5p-mfc-v5",
1414 .driver_data = (unsigned long)&mfc_drvdata_v5,
1415 }, {
1416 .name = "s5p-mfc-v6",
1417 .driver_data = (unsigned long)&mfc_drvdata_v6,
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001418 }, {
1419 .name = "s5p-mfc-v7",
1420 .driver_data = (unsigned long)&mfc_drvdata_v7,
Arun Kumar K8f532a72012-10-03 22:19:09 -03001421 },
1422 {},
1423};
1424MODULE_DEVICE_TABLE(platform, mfc_driver_ids);
1425
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001426static const struct of_device_id exynos_mfc_match[] = {
1427 {
1428 .compatible = "samsung,mfc-v5",
1429 .data = &mfc_drvdata_v5,
1430 }, {
1431 .compatible = "samsung,mfc-v6",
1432 .data = &mfc_drvdata_v6,
Arun Kumar K5441e9d2013-07-09 01:24:38 -03001433 }, {
1434 .compatible = "samsung,mfc-v7",
1435 .data = &mfc_drvdata_v7,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001436 },
1437 {},
1438};
1439MODULE_DEVICE_TABLE(of, exynos_mfc_match);
1440
1441static void *mfc_get_drv_data(struct platform_device *pdev)
1442{
1443 struct s5p_mfc_variant *driver_data = NULL;
1444
1445 if (pdev->dev.of_node) {
1446 const struct of_device_id *match;
Sachin Kamata40a1382013-05-23 00:51:19 -03001447 match = of_match_node(exynos_mfc_match,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001448 pdev->dev.of_node);
1449 if (match)
1450 driver_data = (struct s5p_mfc_variant *)match->data;
1451 } else {
1452 driver_data = (struct s5p_mfc_variant *)
1453 platform_get_device_id(pdev)->driver_data;
1454 }
1455 return driver_data;
1456}
1457
Kamil Debski1e393e92011-08-08 13:12:51 -03001458static struct platform_driver s5p_mfc_driver = {
Arun Kumar K8f532a72012-10-03 22:19:09 -03001459 .probe = s5p_mfc_probe,
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -08001460 .remove = s5p_mfc_remove,
Arun Kumar K8f532a72012-10-03 22:19:09 -03001461 .id_table = mfc_driver_ids,
Kamil Debskiaf935742011-06-21 10:51:26 -03001462 .driver = {
1463 .name = S5P_MFC_NAME,
1464 .owner = THIS_MODULE,
Arun Kumar Kb27a23b2012-10-25 05:24:14 -03001465 .pm = &s5p_mfc_pm_ops,
1466 .of_match_table = exynos_mfc_match,
Kamil Debskiaf935742011-06-21 10:51:26 -03001467 },
1468};
1469
Axel Lin1d6629b2012-01-10 03:21:49 -03001470module_platform_driver(s5p_mfc_driver);
Kamil Debskiaf935742011-06-21 10:51:26 -03001471
1472MODULE_LICENSE("GPL");
1473MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
1474MODULE_DESCRIPTION("Samsung S5P Multi Format Codec V4L2 driver");
1475