blob: 07eaf58303efbc1896c2ec3c138557a7061c6e99 [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
15#include <linux/debugfs.h>
16#include <linux/irqreturn.h>
17#include <linux/mutex.h>
18#include <linux/kfifo.h>
19#include <linux/videodev2.h>
20
21#include <media/v4l2-ctrls.h>
22#include <media/v4l2-device.h>
23#include <media/v4l2-fh.h>
24#include <media/videobuf2-core.h>
25
26#include "coda_regs.h"
27
28#define CODA_MAX_FRAMEBUFFERS 8
29#define CODA_MAX_FRAME_SIZE 0x100000
30#define FMO_SLICE_SAVE_BUF_SIZE (32)
31
32enum {
33 V4L2_M2M_SRC = 0,
34 V4L2_M2M_DST = 1,
35};
36
37enum coda_inst_type {
38 CODA_INST_ENCODER,
39 CODA_INST_DECODER,
40};
41
42enum coda_product {
43 CODA_DX6 = 0xf001,
44 CODA_7541 = 0xf012,
45 CODA_960 = 0xf020,
46};
47
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030048struct coda_video_device;
49
Philipp Zabela2b3e462014-07-23 12:28:39 -030050struct coda_devtype {
51 char *firmware;
52 enum coda_product product;
53 const struct coda_codec *codecs;
54 unsigned int num_codecs;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030055 const struct coda_video_device **vdevs;
56 unsigned int num_vdevs;
Philipp Zabela2b3e462014-07-23 12:28:39 -030057 size_t workbuf_size;
58 size_t tempbuf_size;
59 size_t iram_size;
60};
61
62struct coda_aux_buf {
63 void *vaddr;
64 dma_addr_t paddr;
65 u32 size;
66 struct debugfs_blob_wrapper blob;
67 struct dentry *dentry;
68};
69
70struct coda_dev {
71 struct v4l2_device v4l2_dev;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -030072 struct video_device vfd[3];
Philipp Zabela2b3e462014-07-23 12:28:39 -030073 struct platform_device *plat_dev;
74 const struct coda_devtype *devtype;
75
76 void __iomem *regs_base;
77 struct clk *clk_per;
78 struct clk *clk_ahb;
79 struct reset_control *rstc;
80
81 struct coda_aux_buf codebuf;
82 struct coda_aux_buf tempbuf;
83 struct coda_aux_buf workbuf;
84 struct gen_pool *iram_pool;
85 struct coda_aux_buf iram;
86
87 spinlock_t irqlock;
88 struct mutex dev_mutex;
89 struct mutex coda_mutex;
90 struct workqueue_struct *workqueue;
91 struct v4l2_m2m_dev *m2m_dev;
92 struct vb2_alloc_ctx *alloc_ctx;
93 struct list_head instances;
94 unsigned long instance_mask;
95 struct dentry *debugfs_root;
96};
97
98struct coda_codec {
99 u32 mode;
100 u32 src_fourcc;
101 u32 dst_fourcc;
102 u32 max_w;
103 u32 max_h;
104};
105
106struct coda_huff_tab;
107
108struct coda_params {
109 u8 rot_mode;
110 u8 h264_intra_qp;
111 u8 h264_inter_qp;
112 u8 h264_min_qp;
113 u8 h264_max_qp;
114 u8 h264_deblk_enabled;
115 u8 h264_deblk_alpha;
116 u8 h264_deblk_beta;
117 u8 mpeg4_intra_qp;
118 u8 mpeg4_inter_qp;
119 u8 gop_size;
120 int intra_refresh;
121 int codec_mode;
122 int codec_mode_aux;
123 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
124 u32 framerate;
125 u16 bitrate;
126 u32 slice_max_bits;
127 u32 slice_max_mb;
128};
129
130struct coda_timestamp {
131 struct list_head list;
132 u32 sequence;
133 struct v4l2_timecode timecode;
134 struct timeval timestamp;
135};
136
137/* Per-queue, driver-specific private data */
138struct coda_q_data {
139 unsigned int width;
140 unsigned int height;
141 unsigned int bytesperline;
142 unsigned int sizeimage;
143 unsigned int fourcc;
144 struct v4l2_rect rect;
145};
146
147struct coda_iram_info {
148 u32 axi_sram_use;
149 phys_addr_t buf_bit_use;
150 phys_addr_t buf_ip_ac_dc_use;
151 phys_addr_t buf_dbk_y_use;
152 phys_addr_t buf_dbk_c_use;
153 phys_addr_t buf_ovl_use;
154 phys_addr_t buf_btp_use;
155 phys_addr_t search_ram_paddr;
156 int search_ram_size;
157 int remaining;
158 phys_addr_t next_paddr;
159};
160
161struct gdi_tiled_map {
162 int xy2ca_map[16];
163 int xy2ba_map[16];
164 int xy2ra_map[16];
165 int rbc2axi_map[32];
166 int xy2rbc_config;
167 int map_type;
168#define GDI_LINEAR_FRAME_MAP 0
169};
170
Philipp Zabela1192a12014-07-23 12:28:40 -0300171struct coda_ctx;
172
173struct coda_context_ops {
174 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
175 struct vb2_queue *dst_vq);
176 int (*start_streaming)(struct coda_ctx *ctx);
177 int (*prepare_run)(struct coda_ctx *ctx);
178 void (*finish_run)(struct coda_ctx *ctx);
179 void (*seq_end_work)(struct work_struct *work);
180 void (*release)(struct coda_ctx *ctx);
181};
182
Philipp Zabela2b3e462014-07-23 12:28:39 -0300183struct coda_ctx {
184 struct coda_dev *dev;
185 struct mutex buffer_mutex;
186 struct list_head list;
187 struct work_struct pic_run_work;
188 struct work_struct seq_end_work;
189 struct completion completion;
Philipp Zabel2c11d1b2014-10-02 14:08:28 -0300190 const struct coda_video_device *cvd;
Philipp Zabela1192a12014-07-23 12:28:40 -0300191 const struct coda_context_ops *ops;
Philipp Zabela2b3e462014-07-23 12:28:39 -0300192 int aborting;
193 int initialized;
194 int streamon_out;
195 int streamon_cap;
196 u32 isequence;
197 u32 qsequence;
198 u32 osequence;
199 u32 sequence_offset;
200 struct coda_q_data q_data[2];
201 enum coda_inst_type inst_type;
202 const struct coda_codec *codec;
203 enum v4l2_colorspace colorspace;
204 struct coda_params params;
205 struct v4l2_ctrl_handler ctrls;
206 struct v4l2_fh fh;
207 int gopcounter;
208 int runcounter;
209 char vpu_header[3][64];
210 int vpu_header_size[3];
211 struct kfifo bitstream_fifo;
212 struct mutex bitstream_mutex;
213 struct coda_aux_buf bitstream;
214 bool hold;
215 struct coda_aux_buf parabuf;
216 struct coda_aux_buf psbuf;
217 struct coda_aux_buf slicebuf;
218 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
219 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
220 struct coda_timestamp frame_timestamps[CODA_MAX_FRAMEBUFFERS];
221 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
222 struct list_head timestamp_list;
223 struct coda_aux_buf workbuf;
224 int num_internal_frames;
225 int idx;
226 int reg_idx;
227 struct coda_iram_info iram_info;
228 struct gdi_tiled_map tiled_map;
229 u32 bit_stream_param;
230 u32 frm_dis_flg;
231 u32 frame_mem_ctrl;
232 int display_idx;
233 struct dentry *debugfs_entry;
234};
Philipp Zabel4f4ee9e2014-07-23 12:28:44 -0300235
Philipp Zabel79924ca2014-07-23 12:28:45 -0300236extern int coda_debug;
237
238void coda_write(struct coda_dev *dev, u32 data, u32 reg);
239unsigned int coda_read(struct coda_dev *dev, u32 reg);
Philipp Zabel856d7d92014-09-29 09:53:44 -0300240void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
241 struct vb2_buffer *buf, unsigned int reg_y);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300242
243int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
244 size_t size, const char *name, struct dentry *parent);
245void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
246
247static inline int coda_alloc_context_buf(struct coda_ctx *ctx,
248 struct coda_aux_buf *buf, size_t size,
249 const char *name)
250{
251 return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry);
252}
253
254int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
255 struct vb2_queue *dst_vq);
256int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
257 struct vb2_queue *dst_vq);
258
259int coda_hw_reset(struct coda_ctx *ctx);
260
261void coda_fill_bitstream(struct coda_ctx *ctx);
262
263void coda_set_gdi_regs(struct coda_ctx *ctx);
264
265static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
266 enum v4l2_buf_type type)
267{
268 switch (type) {
269 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
270 return &(ctx->q_data[V4L2_M2M_SRC]);
271 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
272 return &(ctx->q_data[V4L2_M2M_DST]);
273 default:
274 return NULL;
275 }
276}
277
278const char *coda_product_name(int product);
279
280int coda_check_firmware(struct coda_dev *dev);
281
282static inline int coda_get_bitstream_payload(struct coda_ctx *ctx)
283{
284 return kfifo_len(&ctx->bitstream_fifo);
285}
286
287void coda_bit_stream_end_flag(struct coda_ctx *ctx);
288
Philipp Zabel4f4ee9e2014-07-23 12:28:44 -0300289int coda_h264_padding(int size, char *p);
Philipp Zabel79924ca2014-07-23 12:28:45 -0300290
291extern const struct coda_context_ops coda_bit_encode_ops;
292extern const struct coda_context_ops coda_bit_decode_ops;
293
294irqreturn_t coda_irq_handler(int irq, void *data);