1/*
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#ifndef __CODA_H__
16#define __CODA_H__
17
18#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>
27#include <media/videobuf2-core.h>
28
29#include "coda_regs.h"
30
31#define CODA_MAX_FRAMEBUFFERS	8
32#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
50struct coda_video_device;
51
52struct coda_devtype {
53	char			*firmware;
54	enum coda_product	product;
55	const struct coda_codec	*codecs;
56	unsigned int		num_codecs;
57	const struct coda_video_device **vdevs;
58	unsigned int		num_vdevs;
59	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;
74	struct video_device	vfd[5];
75	struct platform_device	*plat_dev;
76	const struct coda_devtype *devtype;
77
78	void __iomem		*regs_base;
79	struct clk		*clk_per;
80	struct clk		*clk_ahb;
81	struct reset_control	*rstc;
82
83	struct coda_aux_buf	codebuf;
84	struct coda_aux_buf	tempbuf;
85	struct coda_aux_buf	workbuf;
86	struct gen_pool		*iram_pool;
87	struct coda_aux_buf	iram;
88
89	spinlock_t		irqlock;
90	struct mutex		dev_mutex;
91	struct mutex		coda_mutex;
92	struct workqueue_struct	*workqueue;
93	struct v4l2_m2m_dev	*m2m_dev;
94	struct vb2_alloc_ctx	*alloc_ctx;
95	struct list_head	instances;
96	unsigned long		instance_mask;
97	struct dentry		*debugfs_root;
98};
99
100struct coda_codec {
101	u32 mode;
102	u32 src_fourcc;
103	u32 dst_fourcc;
104	u32 max_w;
105	u32 max_h;
106};
107
108struct coda_huff_tab;
109
110struct coda_params {
111	u8			rot_mode;
112	u8			h264_intra_qp;
113	u8			h264_inter_qp;
114	u8			h264_min_qp;
115	u8			h264_max_qp;
116	u8			h264_deblk_enabled;
117	u8			h264_deblk_alpha;
118	u8			h264_deblk_beta;
119	u8			mpeg4_intra_qp;
120	u8			mpeg4_inter_qp;
121	u8			gop_size;
122	int			intra_refresh;
123	u8			jpeg_quality;
124	u8			jpeg_restart_interval;
125	u8			*jpeg_qmat_tab[3];
126	int			codec_mode;
127	int			codec_mode_aux;
128	enum v4l2_mpeg_video_multi_slice_mode slice_mode;
129	u32			framerate;
130	u16			bitrate;
131	u32			slice_max_bits;
132	u32			slice_max_mb;
133};
134
135struct coda_buffer_meta {
136	struct list_head	list;
137	u32			sequence;
138	struct v4l2_timecode	timecode;
139	struct timeval		timestamp;
140	u32			start;
141	u32			end;
142};
143
144/* Per-queue, driver-specific private data */
145struct coda_q_data {
146	unsigned int		width;
147	unsigned int		height;
148	unsigned int		bytesperline;
149	unsigned int		sizeimage;
150	unsigned int		fourcc;
151	struct v4l2_rect	rect;
152};
153
154struct coda_iram_info {
155	u32		axi_sram_use;
156	phys_addr_t	buf_bit_use;
157	phys_addr_t	buf_ip_ac_dc_use;
158	phys_addr_t	buf_dbk_y_use;
159	phys_addr_t	buf_dbk_c_use;
160	phys_addr_t	buf_ovl_use;
161	phys_addr_t	buf_btp_use;
162	phys_addr_t	search_ram_paddr;
163	int		search_ram_size;
164	int		remaining;
165	phys_addr_t	next_paddr;
166};
167
168struct gdi_tiled_map {
169	int xy2ca_map[16];
170	int xy2ba_map[16];
171	int xy2ra_map[16];
172	int rbc2axi_map[32];
173	int xy2rbc_config;
174	int map_type;
175#define GDI_LINEAR_FRAME_MAP 0
176};
177
178struct coda_ctx;
179
180struct coda_context_ops {
181	int (*queue_init)(void *priv, struct vb2_queue *src_vq,
182			  struct vb2_queue *dst_vq);
183	int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
184	int (*start_streaming)(struct coda_ctx *ctx);
185	int (*prepare_run)(struct coda_ctx *ctx);
186	void (*finish_run)(struct coda_ctx *ctx);
187	void (*seq_end_work)(struct work_struct *work);
188	void (*release)(struct coda_ctx *ctx);
189};
190
191struct coda_ctx {
192	struct coda_dev			*dev;
193	struct mutex			buffer_mutex;
194	struct list_head		list;
195	struct work_struct		pic_run_work;
196	struct work_struct		seq_end_work;
197	struct completion		completion;
198	const struct coda_video_device	*cvd;
199	const struct coda_context_ops	*ops;
200	int				aborting;
201	int				initialized;
202	int				streamon_out;
203	int				streamon_cap;
204	u32				qsequence;
205	u32				osequence;
206	u32				sequence_offset;
207	struct coda_q_data		q_data[2];
208	enum coda_inst_type		inst_type;
209	const struct coda_codec		*codec;
210	enum v4l2_colorspace		colorspace;
211	struct coda_params		params;
212	struct v4l2_ctrl_handler	ctrls;
213	struct v4l2_fh			fh;
214	int				gopcounter;
215	int				runcounter;
216	char				vpu_header[3][64];
217	int				vpu_header_size[3];
218	struct kfifo			bitstream_fifo;
219	struct mutex			bitstream_mutex;
220	struct coda_aux_buf		bitstream;
221	bool				hold;
222	struct coda_aux_buf		parabuf;
223	struct coda_aux_buf		psbuf;
224	struct coda_aux_buf		slicebuf;
225	struct coda_aux_buf		internal_frames[CODA_MAX_FRAMEBUFFERS];
226	u32				frame_types[CODA_MAX_FRAMEBUFFERS];
227	struct coda_buffer_meta		frame_metas[CODA_MAX_FRAMEBUFFERS];
228	u32				frame_errors[CODA_MAX_FRAMEBUFFERS];
229	struct list_head		buffer_meta_list;
230	struct coda_aux_buf		workbuf;
231	int				num_internal_frames;
232	int				idx;
233	int				reg_idx;
234	struct coda_iram_info		iram_info;
235	struct gdi_tiled_map		tiled_map;
236	u32				bit_stream_param;
237	u32				frm_dis_flg;
238	u32				frame_mem_ctrl;
239	int				display_idx;
240	struct dentry			*debugfs_entry;
241	bool				use_bit;
242};
243
244extern 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);
248void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
249		     struct vb2_buffer *buf, unsigned int reg_y);
250
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
255int 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
262void coda_fill_bitstream(struct coda_ctx *ctx, bool streaming);
263
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
283static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
284{
285	return kfifo_len(&ctx->bitstream_fifo);
286}
287
288void coda_bit_stream_end_flag(struct coda_ctx *ctx);
289
290int coda_h264_padding(int size, char *p);
291
292bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
293int coda_jpeg_write_tables(struct coda_ctx *ctx);
294void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
295
296extern const struct coda_context_ops coda_bit_encode_ops;
297extern const struct coda_context_ops coda_bit_decode_ops;
298
299irqreturn_t coda_irq_handler(int irq, void *data);
300
301#endif /* __CODA_H__ */
302