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