]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/media/platform/coda/coda.h
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-focal-kernel.git] / drivers / media / platform / coda / coda.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
a2b3e46a
PZ
2/*
3 * Coda multi-standard codec IP
4 *
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
7 * Xavier Duret
8 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
a2b3e46a
PZ
9 */
10
9a1a8f99
PZ
11#ifndef __CODA_H__
12#define __CODA_H__
13
a2b3e46a 14#include <linux/debugfs.h>
649cfc2b 15#include <linux/idr.h>
a2b3e46a
PZ
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>
c139990e 24#include <media/videobuf2-v4l2.h>
a2b3e46a
PZ
25
26#include "coda_regs.h"
27
66487818 28#define CODA_MAX_FRAMEBUFFERS 19
a2b3e46a
PZ
29#define FMO_SLICE_SAVE_BUF_SIZE (32)
30
31enum {
32 V4L2_M2M_SRC = 0,
33 V4L2_M2M_DST = 1,
34};
35
36enum coda_inst_type {
37 CODA_INST_ENCODER,
38 CODA_INST_DECODER,
39};
40
41enum coda_product {
42 CODA_DX6 = 0xf001,
1d1ca23b 43 CODA_HX4 = 0xf00a,
a2b3e46a
PZ
44 CODA_7541 = 0xf012,
45 CODA_960 = 0xf020,
46};
47
2c11d1bd
PZ
48struct coda_video_device;
49
a2b3e46a 50struct coda_devtype {
8af7779f 51 char *firmware[3];
a2b3e46a
PZ
52 enum coda_product product;
53 const struct coda_codec *codecs;
54 unsigned int num_codecs;
2c11d1bd
PZ
55 const struct coda_video_device **vdevs;
56 unsigned int num_vdevs;
a2b3e46a
PZ
57 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;
cb1d3a33 72 struct video_device vfd[5];
a2b3e46a
PZ
73 struct platform_device *plat_dev;
74 const struct coda_devtype *devtype;
2ac7f08e 75 int firmware;
e7f3c548 76 struct vdoa_data *vdoa;
a2b3e46a
PZ
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;
649cfc2b 94 struct ida ida;
a2b3e46a
PZ
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;
75fa6e4f
PZ
114 u8 h264_disable_deblocking_filter_idc;
115 s8 h264_slice_alpha_c0_offset_div2;
116 s8 h264_slice_beta_offset_div2;
31489f6b 117 bool h264_constrained_intra_pred_flag;
3943f059 118 s8 h264_chroma_qp_index_offset;
69122271
PZ
119 u8 h264_profile_idc;
120 u8 h264_level_idc;
a2b3e46a
PZ
121 u8 mpeg4_intra_qp;
122 u8 mpeg4_inter_qp;
123 u8 gop_size;
124 int intra_refresh;
cb1d3a33
PZ
125 u8 jpeg_quality;
126 u8 jpeg_restart_interval;
127 u8 *jpeg_qmat_tab[3];
a2b3e46a
PZ
128 int codec_mode;
129 int codec_mode_aux;
130 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
131 u32 framerate;
132 u16 bitrate;
da2b3b3e
PZ
133 u16 vbv_delay;
134 u32 vbv_size;
a2b3e46a
PZ
135 u32 slice_max_bits;
136 u32 slice_max_mb;
7e68c533 137 bool force_ipicture;
a2b3e46a
PZ
138};
139
7cbb105f 140struct coda_buffer_meta {
a2b3e46a
PZ
141 struct list_head list;
142 u32 sequence;
143 struct v4l2_timecode timecode;
d6dd645e 144 u64 timestamp;
6c0f5d23
PZ
145 unsigned int start;
146 unsigned int end;
a2b3e46a
PZ
147};
148
149/* Per-queue, driver-specific private data */
150struct coda_q_data {
151 unsigned int width;
152 unsigned int height;
153 unsigned int bytesperline;
154 unsigned int sizeimage;
155 unsigned int fourcc;
156 struct v4l2_rect rect;
157};
158
159struct coda_iram_info {
160 u32 axi_sram_use;
161 phys_addr_t buf_bit_use;
162 phys_addr_t buf_ip_ac_dc_use;
163 phys_addr_t buf_dbk_y_use;
164 phys_addr_t buf_dbk_c_use;
165 phys_addr_t buf_ovl_use;
166 phys_addr_t buf_btp_use;
167 phys_addr_t search_ram_paddr;
168 int search_ram_size;
169 int remaining;
170 phys_addr_t next_paddr;
171};
172
a2b3e46a 173#define GDI_LINEAR_FRAME_MAP 0
a269e53b 174#define GDI_TILED_FRAME_MB_RASTER_MAP 1
a2b3e46a 175
a1192a17
PZ
176struct coda_ctx;
177
178struct coda_context_ops {
179 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
180 struct vb2_queue *dst_vq);
73751da6 181 int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
a1192a17
PZ
182 int (*start_streaming)(struct coda_ctx *ctx);
183 int (*prepare_run)(struct coda_ctx *ctx);
184 void (*finish_run)(struct coda_ctx *ctx);
cb778f5c 185 void (*run_timeout)(struct coda_ctx *ctx);
a1192a17
PZ
186 void (*seq_end_work)(struct work_struct *work);
187 void (*release)(struct coda_ctx *ctx);
188};
189
a2b3e46a
PZ
190struct coda_ctx {
191 struct coda_dev *dev;
192 struct mutex buffer_mutex;
a2b3e46a
PZ
193 struct work_struct pic_run_work;
194 struct work_struct seq_end_work;
195 struct completion completion;
2c11d1bd 196 const struct coda_video_device *cvd;
a1192a17 197 const struct coda_context_ops *ops;
a2b3e46a
PZ
198 int aborting;
199 int initialized;
200 int streamon_out;
201 int streamon_cap;
a2b3e46a
PZ
202 u32 qsequence;
203 u32 osequence;
204 u32 sequence_offset;
205 struct coda_q_data q_data[2];
206 enum coda_inst_type inst_type;
207 const struct coda_codec *codec;
208 enum v4l2_colorspace colorspace;
b14ac545
PZ
209 enum v4l2_xfer_func xfer_func;
210 enum v4l2_ycbcr_encoding ycbcr_enc;
211 enum v4l2_quantization quantization;
a2b3e46a
PZ
212 struct coda_params params;
213 struct v4l2_ctrl_handler ctrls;
42a68012
PZ
214 struct v4l2_ctrl *h264_profile_ctrl;
215 struct v4l2_ctrl *h264_level_ctrl;
a2b3e46a
PZ
216 struct v4l2_fh fh;
217 int gopcounter;
218 int runcounter;
219 char vpu_header[3][64];
220 int vpu_header_size[3];
221 struct kfifo bitstream_fifo;
222 struct mutex bitstream_mutex;
223 struct coda_aux_buf bitstream;
224 bool hold;
225 struct coda_aux_buf parabuf;
226 struct coda_aux_buf psbuf;
227 struct coda_aux_buf slicebuf;
228 struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS];
229 u32 frame_types[CODA_MAX_FRAMEBUFFERS];
7cbb105f 230 struct coda_buffer_meta frame_metas[CODA_MAX_FRAMEBUFFERS];
a2b3e46a 231 u32 frame_errors[CODA_MAX_FRAMEBUFFERS];
7cbb105f 232 struct list_head buffer_meta_list;
47f3fa63
PZ
233 spinlock_t buffer_meta_lock;
234 int num_metas;
a2b3e46a
PZ
235 struct coda_aux_buf workbuf;
236 int num_internal_frames;
237 int idx;
238 int reg_idx;
239 struct coda_iram_info iram_info;
a269e53b 240 int tiled_map_type;
a2b3e46a
PZ
241 u32 bit_stream_param;
242 u32 frm_dis_flg;
243 u32 frame_mem_ctrl;
244 int display_idx;
245 struct dentry *debugfs_entry;
a22496c6 246 bool use_bit;
e7f3c548
MT
247 bool use_vdoa;
248 struct vdoa_ctx *vdoa;
a2b3e46a 249};
4f4ee9ee 250
79924ca9
PZ
251extern int coda_debug;
252
8f90d15e
PZ
253#define coda_dbg(level, ctx, fmt, arg...) \
254 do { \
255 if (coda_debug >= (level)) \
256 v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
257 "%u: " fmt, (ctx)->idx, ##arg); \
258 } while (0)
259
79924ca9
PZ
260void coda_write(struct coda_dev *dev, u32 data, u32 reg);
261unsigned int coda_read(struct coda_dev *dev, u32 reg);
856d7d93 262void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
2d700715 263 struct vb2_v4l2_buffer *buf, unsigned int reg_y);
79924ca9
PZ
264
265int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
266 size_t size, const char *name, struct dentry *parent);
267void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
268
79924ca9
PZ
269int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
270 struct vb2_queue *dst_vq);
271int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
272 struct vb2_queue *dst_vq);
273
274int coda_hw_reset(struct coda_ctx *ctx);
275
331e7860 276void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
79924ca9
PZ
277
278void coda_set_gdi_regs(struct coda_ctx *ctx);
279
280static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
281 enum v4l2_buf_type type)
282{
283 switch (type) {
284 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
285 return &(ctx->q_data[V4L2_M2M_SRC]);
286 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
287 return &(ctx->q_data[V4L2_M2M_DST]);
288 default:
289 return NULL;
290 }
291}
292
293const char *coda_product_name(int product);
294
295int coda_check_firmware(struct coda_dev *dev);
296
2b768468 297static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
79924ca9
PZ
298{
299 return kfifo_len(&ctx->bitstream_fifo);
300}
301
51407c2d
PZ
302/*
303 * The bitstream prefetcher needs to read at least 2 256 byte periods past
304 * the desired bitstream position for all data to reach the decoder.
305 */
306static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
307 unsigned int pos)
308{
309 return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
310}
311
312bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
313
79924ca9
PZ
314void coda_bit_stream_end_flag(struct coda_ctx *ctx);
315
2d700715 316void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
9f2bfb33
PZ
317 enum vb2_buffer_state state);
318
0eef8940 319int coda_h264_filler_nal(int size, char *p);
4f4ee9ee 320int coda_h264_padding(int size, char *p);
69122271
PZ
321int coda_h264_profile(int profile_idc);
322int coda_h264_level(int level_idc);
323int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
0dff710d
PZ
324int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
325 int *size, int max_size);
79924ca9 326
d4de047b 327bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
cb1d3a33
PZ
328int coda_jpeg_write_tables(struct coda_ctx *ctx);
329void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
330
79924ca9
PZ
331extern const struct coda_context_ops coda_bit_encode_ops;
332extern const struct coda_context_ops coda_bit_decode_ops;
333
334irqreturn_t coda_irq_handler(int irq, void *data);
9a1a8f99
PZ
335
336#endif /* __CODA_H__ */