upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / media / video / s5p-mfc / s5p_mfc_common.h
1 /*
2  * Samsung S5P Multi Format Codec v 5.0
3  *
4  * This file contains definitions of enums and structs used by the codec
5  * driver.
6  *
7  * Copyright (c) 2010 Samsung Electronics Co., Ltd.
8  * Kamil Debski, <k.debski@samsung.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by the
12  * Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version
14  */
15
16 #ifndef S5P_MFC_COMMON_H_
17 #define S5P_MFC_COMMON_H_
18
19 #include <linux/videodev2.h>
20 #include <linux/workqueue.h>
21
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-ioctl.h>
24
25 #include <media/videobuf2-core.h>
26
27 #include "regs-mfc.h"
28
29 #if defined(CONFIG_S5P_SYSMMU_MFC_L) && defined(CONFIG_S5P_SYSMMU_MFC_R)
30 #define SYSMMU_MFC_ON
31 #endif
32
33 #define MFC_MAX_EXTRA_DPB       5
34 #define MFC_MAX_BUFFERS         32
35 #define MFC_MAX_REF_BUFS        2
36 #define MFC_FRAME_PLANES        2
37
38 #define MFC_NUM_CONTEXTS        4
39 /* Interrupt timeout */
40 #define MFC_INT_TIMEOUT         2000
41 /* Busy wait timeout */
42 #define MFC_BW_TIMEOUT          500
43 /* Watchdog interval */
44 #define MFC_WATCHDOG_INTERVAL   1000
45 /* After how many executions watchdog should assume lock up */
46 #define MFC_WATCHDOG_CNT        10
47
48 #define MFC_NO_INSTANCE_SET     -1
49
50 #define MFC_ENC_CAP_PLANE_COUNT 1
51 #define MFC_ENC_OUT_PLANE_COUNT 2
52
53 #define MFC_NAME_LEN            16
54
55 #define STUFF_BYTE              4
56 #define MFC_WORKQUEUE_LEN       32
57
58 /**
59  * enum s5p_mfc_inst_type - The type of an MFC device node.
60  */
61 enum s5p_mfc_node_type {
62         MFCNODE_INVALID = -1,
63         MFCNODE_DECODER = 0,
64         MFCNODE_ENCODER = 1,
65 };
66
67 /**
68  * enum s5p_mfc_inst_type - The type of an MFC instance.
69  */
70 enum s5p_mfc_inst_type {
71         MFCINST_INVALID = 0,
72         MFCINST_DECODER = 1,
73         MFCINST_ENCODER = 2,
74 };
75
76 /**
77  * enum s5p_mfc_inst_state - The state of an MFC instance.
78  */
79 enum s5p_mfc_inst_state {
80         MFCINST_FREE = 0,
81         MFCINST_INIT = 100,
82         MFCINST_GOT_INST,
83         MFCINST_HEAD_PARSED,
84         MFCINST_BUFS_SET,
85         MFCINST_RUNNING,
86         MFCINST_FINISHING,
87         MFCINST_FINISHED,
88         MFCINST_RETURN_INST,
89         MFCINST_ERROR,
90         MFCINST_ABORT,
91         MFCINST_RES_CHANGE_INIT,
92         MFCINST_RES_CHANGE_FLUSH,
93         MFCINST_RES_CHANGE_END,
94 };
95
96 /**
97  * enum s5p_mfc_queue_state - The state of buffer queue.
98  */
99 enum s5p_mfc_queue_state {
100         QUEUE_FREE = 0,
101         QUEUE_BUFS_REQUESTED,
102         QUEUE_BUFS_QUERIED,
103         QUEUE_BUFS_MMAPED,
104 };
105
106 struct s5p_mfc_ctx;
107
108 /**
109  * struct s5p_mfc_buf - MFC buffer
110  *
111  */
112 struct s5p_mfc_buf {
113         struct list_head list;
114         struct vb2_buffer *b;
115         union {
116                 struct {
117                         size_t luma;
118                         size_t chroma;
119                 } raw;
120                 size_t stream;
121         } cookie;
122         int used;
123 };
124
125
126 struct s5p_mfc_pm {
127         struct clk      *clock;
128 #ifdef CONFIG_ARCH_S5PV310
129         atomic_t        power;
130 #ifdef CONFIG_PM_RUNTIME
131         struct device   *device;
132 #endif
133 #endif
134 };
135
136 struct s5p_mfc_fw {
137         const struct firmware   *info;
138         int                     state;
139         int                     ver;
140 };
141
142 /**
143  * struct s5p_mfc_dev - The struct containing driver internal parameters.
144  */
145 struct s5p_mfc_dev {
146         struct v4l2_device      v4l2_dev;
147         struct video_device     *vfd_dec;
148         struct video_device     *vfd_enc;
149         struct platform_device  *plat_dev;
150
151         void __iomem            *regs_base;
152         int                     irq;
153         struct resource         *mfc_mem;
154
155         struct s5p_mfc_pm       pm;
156         struct s5p_mfc_fw       fw;
157
158         int num_inst;
159         spinlock_t irqlock;
160         spinlock_t condlock;
161
162         struct mutex mfc_mutex;
163
164         int int_cond;
165         int int_type;
166         unsigned int int_err;
167         wait_queue_head_t queue;
168
169         size_t port_a;
170         size_t port_b;
171
172         unsigned long hw_lock;
173
174         /*
175         struct clk *clock1;
176         struct clk *clock2;
177         */
178
179         struct s5p_mfc_ctx *ctx[MFC_NUM_CONTEXTS];
180         int curr_ctx;
181         unsigned long ctx_work_bits;
182
183         atomic_t watchdog_cnt;
184         struct timer_list watchdog_timer;
185         struct workqueue_struct *watchdog_workqueue;
186         struct work_struct watchdog_work;
187
188         struct vb2_alloc_ctx **alloc_ctx;
189
190         unsigned long clk_state;
191         struct work_struct work_struct;
192         struct workqueue_struct *irq_workqueue;
193 };
194
195 /**
196  *
197  */
198 struct s5p_mfc_h264_enc_params {
199         u8 num_b_frame;
200         enum v4l2_codec_mfc5x_enc_h264_profile profile;
201         u8 level;
202         enum v4l2_codec_mfc5x_enc_switch interlace;
203         enum v4l2_codec_mfc5x_enc_h264_loop_filter loop_filter_mode;
204         s8 loop_filter_alpha;
205         s8 loop_filter_beta;
206         enum v4l2_codec_mfc5x_enc_h264_entropy_mode entropy_mode;
207         u8 max_ref_pic;
208         u8 num_ref_pic_4p;
209         enum v4l2_codec_mfc5x_enc_switch _8x8_transform;
210         enum v4l2_codec_mfc5x_enc_switch rc_mb;
211         u32 rc_framerate;
212         u8 rc_frame_qp;
213         u8 rc_min_qp;
214         u8 rc_max_qp;
215         enum v4l2_codec_mfc5x_enc_switch_inv rc_mb_dark;
216         enum v4l2_codec_mfc5x_enc_switch_inv rc_mb_smooth;
217         enum v4l2_codec_mfc5x_enc_switch_inv rc_mb_static;
218         enum v4l2_codec_mfc5x_enc_switch_inv rc_mb_activity;
219         u8 rc_p_frame_qp;
220         u8 rc_b_frame_qp;
221         enum v4l2_codec_mfc5x_enc_switch ar_vui;
222         u8 ar_vui_idc;
223         u16 ext_sar_width;
224         u16 ext_sar_height;
225         enum v4l2_codec_mfc5x_enc_switch open_gop;
226         u16 open_gop_size;
227 };
228
229 /**
230  *
231  */
232 struct s5p_mfc_mpeg4_enc_params {
233         /* MPEG4 Only */
234         u8 num_b_frame;
235         enum v4l2_codec_mfc5x_enc_mpeg4_profile profile;
236         u8 level;
237         enum v4l2_codec_mfc5x_enc_switch quarter_pixel;
238         u16 vop_time_res;
239         u16 vop_frm_delta;
240         u8 rc_b_frame_qp;
241         /* Common for MPEG4, H263 */
242         u32 rc_framerate;
243         u8 rc_frame_qp;
244         u8 rc_min_qp;
245         u8 rc_max_qp;
246         u8 rc_p_frame_qp;
247 };
248
249 /**
250  *
251  */
252 struct s5p_mfc_enc_params {
253         u16 width;
254         u16 height;
255
256         u16 gop_size;
257         enum v4l2_codec_mfc5x_enc_multi_slice_mode slice_mode;
258         u16 slice_mb;
259         u32 slice_bit;
260         u16 intra_refresh_mb;
261         enum v4l2_codec_mfc5x_enc_switch pad;
262         u8 pad_luma;
263         u8 pad_cb;
264         u8 pad_cr;
265         enum v4l2_codec_mfc5x_enc_switch rc_frame;
266         u32 rc_bitrate;
267         u16 rc_reaction_coeff;
268
269         u16 vbv_buf_size;
270         enum v4l2_codec_mfc5x_enc_seq_hdr_mode seq_hdr_mode;
271         enum v4l2_codec_mfc5x_enc_frame_skip_mode frame_skip_mode;
272         enum v4l2_codec_mfc5x_enc_switch fixed_target_bit;
273
274         union {
275                 struct s5p_mfc_h264_enc_params h264;
276                 struct s5p_mfc_mpeg4_enc_params mpeg4;
277         } codec;
278 };
279
280 enum s5p_mfc_ctrl_type {
281         MFC_CTRL_TYPE_SET       = 0x1,
282         MFC_CTRL_TYPE_GET       = 0x2,
283 };
284
285 enum s5p_mfc_ctrl_mode {
286         MFC_CTRL_MODE_NONE      = 0x0,
287         MFC_CTRL_MODE_SFR       = 0x1,
288         MFC_CTRL_MODE_SHM       = 0x2,
289         MFC_CTRL_MODE_CST       = 0x4,
290 };
291
292 struct s5p_mfc_ctrl_cfg {
293         enum s5p_mfc_ctrl_type type;
294         unsigned int id;
295         /*
296         unsigned int is_dynamic;
297         */
298         unsigned int is_volatile;       /* only for MFC_CTRL_TYPE_SET */
299         unsigned int mode;
300         unsigned int addr;
301         unsigned int mask;
302         unsigned int shft;
303         unsigned int flag_mode;         /* only for MFC_CTRL_TYPE_SET */
304         unsigned int flag_addr;         /* only for MFC_CTRL_TYPE_SET */
305         unsigned int flag_shft;         /* only for MFC_CTRL_TYPE_SET */
306 };
307
308 struct s5p_mfc_ctx_ctrl {
309         struct list_head list;
310         enum s5p_mfc_ctrl_type type;
311         unsigned int id;
312         /*
313         unsigned int is_dynamic;
314         */
315         int has_new;
316         int val;
317 };
318
319 struct s5p_mfc_buf_ctrl {
320         struct list_head list;
321         unsigned int id;
322         int has_new;
323         int val;
324         unsigned int old_val;           /* only for MFC_CTRL_TYPE_SET */
325         unsigned int is_volatile;       /* only for MFC_CTRL_TYPE_SET */
326         unsigned int updated;
327         unsigned int mode;
328         unsigned int addr;
329         unsigned int mask;
330         unsigned int shft;
331         unsigned int flag_mode;         /* only for MFC_CTRL_TYPE_SET */
332         unsigned int flag_addr;         /* only for MFC_CTRL_TYPE_SET */
333         unsigned int flag_shft;         /* only for MFC_CTRL_TYPE_SET */
334 };
335
336 struct s5p_mfc_codec_ops {
337         /* initialization routines */
338         int (*alloc_ctx_buf) (struct s5p_mfc_ctx *ctx);
339         int (*alloc_desc_buf) (struct s5p_mfc_ctx *ctx);
340         int (*get_init_arg) (struct s5p_mfc_ctx *ctx, void *arg);
341         int (*pre_seq_start) (struct s5p_mfc_ctx *ctx);
342         int (*post_seq_start) (struct s5p_mfc_ctx *ctx);
343         int (*set_init_arg) (struct s5p_mfc_ctx *ctx, void *arg);
344         int (*set_codec_bufs) (struct s5p_mfc_ctx *ctx);
345         int (*set_dpbs) (struct s5p_mfc_ctx *ctx);              /* decoder */
346         /* execution routines */
347         int (*get_exe_arg) (struct s5p_mfc_ctx *ctx, void *arg);
348         int (*pre_frame_start) (struct s5p_mfc_ctx *ctx);
349         int (*post_frame_start) (struct s5p_mfc_ctx *ctx);
350         int (*multi_data_frame) (struct s5p_mfc_ctx *ctx);
351         int (*set_exe_arg) (struct s5p_mfc_ctx *ctx, void *arg);
352         /* configuration routines */
353         int (*get_codec_cfg) (struct s5p_mfc_ctx *ctx, unsigned int type, int *value);
354         int (*set_codec_cfg) (struct s5p_mfc_ctx *ctx, unsigned int type, int *value);
355         /* controls per buffer */
356         int (*init_ctx_ctrls) (struct s5p_mfc_ctx *ctx);
357         int (*cleanup_ctx_ctrls) (struct s5p_mfc_ctx *ctx);
358         int (*init_buf_ctrls) (struct s5p_mfc_ctx *ctx, enum s5p_mfc_ctrl_type type, unsigned int index);
359         int (*cleanup_buf_ctrls) (struct s5p_mfc_ctx *ctx, struct list_head *head);
360         int (*to_buf_ctrls) (struct s5p_mfc_ctx *ctx, struct list_head *head);
361         int (*to_ctx_ctrls) (struct s5p_mfc_ctx *ctx, struct list_head *head);
362         int (*set_buf_ctrls_val) (struct s5p_mfc_ctx *ctx, struct list_head *head);
363         int (*get_buf_ctrls_val) (struct s5p_mfc_ctx *ctx, struct list_head *head);
364         int (*recover_buf_ctrls_val) (struct s5p_mfc_ctx *ctx, struct list_head *head);
365 };
366
367 #define call_cop(c, op, args...)                                \
368         (((c)->c_ops->op) ?                                     \
369                 ((c)->c_ops->op(args)) : 0)
370
371 /**
372  * struct s5p_mfc_ctx - This struct contains the instance context
373  */
374 struct s5p_mfc_ctx {
375         struct s5p_mfc_dev *dev;
376         int num;
377
378         int int_cond;
379         int int_type;
380         unsigned int int_err;
381         wait_queue_head_t queue;
382
383         struct s5p_mfc_fmt *src_fmt;
384         struct s5p_mfc_fmt *dst_fmt;
385
386         struct vb2_queue vq_src;
387         struct vb2_queue vq_dst;
388
389         struct list_head src_queue;
390         struct list_head dst_queue;
391
392         unsigned int src_queue_cnt;
393         unsigned int dst_queue_cnt;
394
395         enum s5p_mfc_inst_type type;
396         enum s5p_mfc_inst_state state;
397         int inst_no;
398
399         /* Decoder parameters */
400         int img_width;
401         int img_height;
402         int buf_width;
403         int buf_height;
404         int dpb_count;
405         int total_dpb_count;
406
407         int luma_size;
408         int chroma_size;
409         int mv_size;
410
411         unsigned long consumed_stream;
412         int slice_interface;
413
414         unsigned int dpb_flush_flag;
415
416         /* Buffers */
417         void *port_a_buf;
418         size_t port_a_phys;
419         size_t port_a_size;
420
421         void *port_b_buf;
422         size_t port_b_phys;
423         size_t port_b_size;
424
425
426         enum s5p_mfc_queue_state capture_state;
427         enum s5p_mfc_queue_state output_state;
428
429 //      size_t dec_dst_buf_luma[MFC_MAX_BUFFERS];
430 //      size_t dec_dst_buf_chroma[MFC_MAX_BUFFERS];
431
432         struct s5p_mfc_buf src_bufs[MFC_MAX_BUFFERS];
433         int src_bufs_cnt;
434         struct s5p_mfc_buf dst_bufs[MFC_MAX_BUFFERS];
435         int dst_bufs_cnt;
436
437         struct list_head ctrls;
438
439         struct list_head src_ctrls[MFC_MAX_BUFFERS];
440         struct list_head dst_ctrls[MFC_MAX_BUFFERS];
441
442 //      int dec_dst_buf_cnt;
443         unsigned int sequence;
444         unsigned long dec_dst_flag;
445         size_t dec_src_buf_size;
446
447         /* Control values */
448         int codec_mode;
449         __u32 pix_format;
450         int loop_filter_mpeg4;
451         int display_delay;
452         int cacheable;
453
454         /* Buffers */
455
456         void *context_buf;
457         size_t context_phys;
458         size_t context_ofs;
459         size_t context_size;
460         //dma_addr_t context_dma;
461
462         void *desc_buf;
463         size_t desc_phys;
464
465         void *shared_buf;
466         size_t shared_phys;
467         void *shared_virt;
468         //dma_addr_t shared_dma;
469
470         void *shm_alloc;
471         void *shm;
472         size_t shm_ofs;
473
474         struct s5p_mfc_enc_params enc_params;
475
476         size_t enc_dst_buf_size;
477
478         int frame_count;
479         enum v4l2_codec_mfc5x_enc_frame_type frame_type;
480         enum v4l2_codec_mfc5x_enc_force_frame_type force_frame_type;
481
482         struct list_head ref_queue;
483         unsigned int ref_queue_cnt;
484
485         struct s5p_mfc_codec_ops *c_ops;
486 };
487
488 #define MFC_FMT_DEC     0
489 #define MFC_FMT_ENC     1
490 #define MFC_FMT_RAW     2
491
492 struct s5p_mfc_fmt {
493         char *name;
494         u32 fourcc;
495         u32 codec_mode;
496         u32 type;
497         u32 num_planes;
498 };
499
500 #endif /* S5P_MFC_COMMON_H_ */