blob: 4b831c91ae4aff81f59574811f67ae82f1508eee [file] [log] [blame]
Philipp Zabela2b3e462014-07-23 12:28:39 -03001/*
2 * Coda multi-standard codec IP
3 *
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
6 * Xavier Duret
7 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
Philipp Zabel9a1a8f92015-01-29 14:36:00 -030015#ifndef __CODA_H__
16#define __CODA_H__
17
Philipp Zabela2b3e462014-07-23 12:28:39 -030018#include <linux/debugfs.h>
19#include <linux/irqreturn.h>
20#include <linux/mutex.h>
21#include <linux/kfifo.h>
22#include <linux/videodev2.h>
23
24#include <media/v4l2-ctrls.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-fh.h>
Junghak Sungc1399902015-09-22 10:30:29 -030027#include <media/videobuf2-v4l2.h>
Philipp Zabela2b3e462014-07-23 12:28:39 -030028
29#include "coda_regs.h"
30
31#define CODA_MAX_FRAMEBUFFERS 8
Philipp Zabela2b3e462014-07-23 12:28:39 -030032#define FMO_SLICE_SAVE_BUF_SIZE (32)
33
34enum {
35 V4L2_M2M_SRC = 0,
36 V4L2_M2M_DST = 1,
37};
38
39enum coda_inst_type {
40 CODA_INST_ENCODER,
41 CODA_INST_DECODER,
42};
43
44enum coda_product {
45 CODA_DX6 = 0xf001,
46 CODA_7541 = 0xf012,
47 CODA_960 = 0xf020,
48};
49
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030050struct coda_video_device;
51
Philipp Zabela2b3e462014-07-23 12:28:39 -030052struct coda_devtype {
Baruch Siach8af77792017-01-15 08:33:53 -020053 char *firmware[3];
Philipp Zabela2b3e462014-07-23 12:28:39 -030054 enum coda_product product;
55 const struct coda_codec *codecs;
56 unsigned int num_codecs;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030057 const struct coda_video_device **vdevs;
58 unsigned int num_vdevs;
Philipp Zabela2b3e462014-07-23 12:28:39 -030059 size_t workbuf_size;
60 size_t tempbuf_size;
61 size_t iram_size;
62};
63
64struct coda_aux_buf {
65 void *vaddr;
66 dma_addr_t paddr;
67 u32 size;
68 struct debugfs_blob_wrapper blob;
69 struct dentry *dentry;
70};
71
72struct coda_dev {
73 struct v4l2_device v4l2_dev;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -030074 struct video_device vfd[5];
Philipp Zabela2b3e462014-07-23 12:28:39 -030075 struct platform_device *plat_dev;
76 const struct coda_devtype *devtype;
Philipp Zabel2ac7f082016-02-19 07:18:57 -020077 int firmware;
Michael Trettere7f3c542017-01-20 12:00:24 -020078 struct vdoa_data *vdoa;
Philipp Zabela2b3e462014-07-23 12:28:39 -030079
80 void __iomem *regs_base;
81 struct clk *clk_per;
82 struct clk *clk_ahb;
83 struct reset_control *rstc;
84
85 struct coda_aux_buf codebuf;
86 struct coda_aux_buf tempbuf;
87 struct coda_aux_buf workbuf;
88 struct gen_pool *iram_pool;
89 struct coda_aux_buf iram;
90
91 spinlock_t irqlock;
92 struct mutex dev_mutex;
93 struct mutex coda_mutex;
94 struct workqueue_struct *workqueue;
95 struct v4l2_m2m_dev *m2m_dev;
Philipp Zabela2b3e462014-07-23 12:28:39 -030096 struct list_head instances;
97 unsigned long instance_mask;
98 struct dentry *debugfs_root;
99};
100
101struct coda_codec {
102 u32 mode;
103 u32 src_fourcc;
104 u32 dst_fourcc;
105 u32 max_w;
106 u32 max_h;
107};
108
109struct coda_huff_tab;
110
111struct coda_params {
112 u8 rot_mode;
113 u8 h264_intra_qp;
114 u8 h264_inter_qp;
115 u8 h264_min_qp;
116 u8 h264_max_qp;
117 u8 h264_deblk_enabled;
118 u8 h264_deblk_alpha;
119 u8 h264_deblk_beta;
120 u8 mpeg4_intra_qp;
121 u8 mpeg4_inter_qp;
122 u8 gop_size;
123 int intra_refresh;
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300124 u8 jpeg_quality;
125 u8 jpeg_restart_interval;
126 u8 *jpeg_qmat_tab[3];
Philipp Zabela2b3e462014-07-23 12:28:39 -0300127 int codec_mode;
128 int codec_mode_aux;
129 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
130 u32 framerate;
131 u16 bitrate;
Philipp Zabelda2b3b32015-07-10 10:37:52 -0300132 u16 vbv_delay;
133 u32 vbv_size;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300134 u32 slice_max_bits;
135 u32 slice_max_mb;
136};
137
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300138struct coda_buffer_meta {
Philipp Zabela2b3e462014-07-23 12:28:39 -0300139 struct list_head list;
140 u32 sequence;
141 struct v4l2_timecode timecode;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200142 u64 timestamp;
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300143 u32 start;
144 u32 end;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300145};
146
147/* Per-queue, driver-specific private data */
148struct coda_q_data {
149 unsigned int width;
150 unsigned int height;
151 unsigned int bytesperline;
152 unsigned int sizeimage;
153 unsigned int fourcc;
154 struct v4l2_rect rect;
155};
156
157struct coda_iram_info {
158 u32 axi_sram_use;
159 phys_addr_t buf_bit_use;
160 phys_addr_t buf_ip_ac_dc_use;
161 phys_addr_t buf_dbk_y_use;
162 phys_addr_t buf_dbk_c_use;
163 phys_addr_t buf_ovl_use;
164 phys_addr_t buf_btp_use;
165 phys_addr_t search_ram_paddr;
166 int search_ram_size;
167 int remaining;
168 phys_addr_t next_paddr;
169};
170
Philipp Zabela2b3e462014-07-23 12:28:39 -0300171#define GDI_LINEAR_FRAME_MAP 0
Philipp Zabela269e532015-07-16 13:19:38 -0300172#define GDI_TILED_FRAME_MB_RASTER_MAP 1
Philipp Zabela2b3e462014-07-23 12:28:39 -0300173
Philipp Zabela1192a12014-07-23 12:28:40 -0300174struct coda_ctx;
175
176struct coda_context_ops {
177 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
178 struct vb2_queue *dst_vq);
Philipp Zabel73751da2015-03-24 14:30:51 -0300179 int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
Philipp Zabela1192a12014-07-23 12:28:40 -0300180 int (*start_streaming)(struct coda_ctx *ctx);
181 int (*prepare_run)(struct coda_ctx *ctx);
182 void (*finish_run)(struct coda_ctx *ctx);
183 void (*seq_end_work)(struct work_struct *work);
184 void (*release)(struct coda_ctx *ctx);
185};
186
Philipp Zabela2b3e462014-07-23 12:28:39 -0300187struct coda_ctx {
188 struct coda_dev *dev;
189 struct mutex buffer_mutex;
190 struct list_head list;
191 struct work_struct pic_run_work;
192 struct work_struct seq_end_work;
193 struct completion completion;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300194 const struct coda_video_device *cvd;
Philipp Zabela1192a12014-07-23 12:28:40 -0300195 const struct coda_context_ops *ops;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300196 int aborting;
197 int initialized;
198 int streamon_out;
199 int streamon_cap;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300200 u32 qsequence;
201 u32 osequence;
202 u32 sequence_offset;
203 struct coda_q_data q_data[2];
204 enum coda_inst_type inst_type;
205 const struct coda_codec *codec;
206 enum v4l2_colorspace colorspace;
207 struct coda_params params;
208 struct v4l2_ctrl_handler ctrls;
209 struct v4l2_fh fh;
210 int gopcounter;
211 int runcounter;
212 char vpu_header[3][64];
213 int vpu_header_size[3];
214 struct kfifo bitstream_fifo;
215 struct mutex bitstream_mutex;
216 struct coda_aux_buf bitstream;
217 bool hold;
218 struct coda_aux_buf parabuf;
219 struct coda_aux_buf psbuf;
220 struct coda_aux_buf slicebuf;
221 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
222 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300223 struct coda_buffer_meta frame_metas[CODA_MAX_FRAMEBUFFERS];
Philipp Zabela2b3e462014-07-23 12:28:39 -0300224 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
Philipp Zabel7cbb1052014-10-02 14:08:32 -0300225 struct list_head buffer_meta_list;
Philipp Zabel47f3fa62015-07-09 07:10:21 -0300226 spinlock_t buffer_meta_lock;
227 int num_metas;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300228 struct coda_aux_buf workbuf;
229 int num_internal_frames;
230 int idx;
231 int reg_idx;
232 struct coda_iram_info iram_info;
Philipp Zabela269e532015-07-16 13:19:38 -0300233 int tiled_map_type;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300234 u32 bit_stream_param;
235 u32 frm_dis_flg;
236 u32 frame_mem_ctrl;
237 int display_idx;
238 struct dentry *debugfs_entry;
Philipp Zabela22496c2015-01-23 13:51:33 -0300239 bool use_bit;
Michael Trettere7f3c542017-01-20 12:00:24 -0200240 bool use_vdoa;
241 struct vdoa_ctx *vdoa;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300242};
Philipp Zabel4f4ee9e2014-07-23 12:28:44 -0300243
Philipp Zabel79924ca2014-07-23 12:28:45 -0300244extern int coda_debug;
245
246void coda_write(struct coda_dev *dev, u32 data, u32 reg);
247unsigned int coda_read(struct coda_dev *dev, u32 reg);
Philipp Zabel856d7d92014-09-29 09:53:44 -0300248void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
Junghak Sung2d700712015-09-22 10:30:30 -0300249 struct vb2_v4l2_buffer *buf, unsigned int reg_y);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300250
251int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
252 size_t size, const char *name, struct dentry *parent);
253void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
254
Philipp Zabel79924ca2014-07-23 12:28:45 -0300255int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
256 struct vb2_queue *dst_vq);
257int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
258 struct vb2_queue *dst_vq);
259
260int coda_hw_reset(struct coda_ctx *ctx);
261
Philipp Zabel582d88722015-03-24 14:30:57 -0300262void coda_fill_bitstream(struct coda_ctx *ctx, bool streaming);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300263
264void coda_set_gdi_regs(struct coda_ctx *ctx);
265
266static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
267 enum v4l2_buf_type type)
268{
269 switch (type) {
270 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
271 return &(ctx->q_data[V4L2_M2M_SRC]);
272 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
273 return &(ctx->q_data[V4L2_M2M_DST]);
274 default:
275 return NULL;
276 }
277}
278
279const char *coda_product_name(int product);
280
281int coda_check_firmware(struct coda_dev *dev);
282
Philipp Zabel2b768462015-03-24 14:30:49 -0300283static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
Philipp Zabel79924ca2014-07-23 12:28:45 -0300284{
285 return kfifo_len(&ctx->bitstream_fifo);
286}
287
288void coda_bit_stream_end_flag(struct coda_ctx *ctx);
289
Junghak Sung2d700712015-09-22 10:30:30 -0300290void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
Philipp Zabel9f2bfb32015-05-04 07:51:07 -0300291 enum vb2_buffer_state state);
292
Philipp Zabel4f4ee9e2014-07-23 12:28:44 -0300293int coda_h264_padding(int size, char *p);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300294
Philipp Zabeld4de0472015-12-02 14:58:51 -0200295bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
Philipp Zabelcb1d3a32014-10-02 14:08:31 -0300296int coda_jpeg_write_tables(struct coda_ctx *ctx);
297void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
298
Philipp Zabel79924ca2014-07-23 12:28:45 -0300299extern const struct coda_context_ops coda_bit_encode_ops;
300extern const struct coda_context_ops coda_bit_decode_ops;
301
302irqreturn_t coda_irq_handler(int irq, void *data);
Philipp Zabel9a1a8f92015-01-29 14:36:00 -0300303
304#endif /* __CODA_H__ */