GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / coda / coda.h
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/idr.h>
20 #include <linux/irqreturn.h>
21 #include <linux/mutex.h>
22 #include <linux/kfifo.h>
23 #include <linux/videodev2.h>
24
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-fh.h>
28 #include <media/videobuf2-v4l2.h>
29
30 #include "coda_regs.h"
31
32 #define CODA_MAX_FRAMEBUFFERS   19
33 #define FMO_SLICE_SAVE_BUF_SIZE (32)
34
35 enum {
36         V4L2_M2M_SRC = 0,
37         V4L2_M2M_DST = 1,
38 };
39
40 enum coda_inst_type {
41         CODA_INST_ENCODER,
42         CODA_INST_DECODER,
43 };
44
45 enum coda_product {
46         CODA_DX6 = 0xf001,
47         CODA_HX4 = 0xf00a,
48         CODA_7541 = 0xf012,
49         CODA_960 = 0xf020,
50 };
51
52 struct coda_video_device;
53
54 struct coda_devtype {
55         char                    *firmware[3];
56         enum coda_product       product;
57         const struct coda_codec *codecs;
58         unsigned int            num_codecs;
59         const struct coda_video_device **vdevs;
60         unsigned int            num_vdevs;
61         size_t                  workbuf_size;
62         size_t                  tempbuf_size;
63         size_t                  iram_size;
64 };
65
66 struct coda_aux_buf {
67         void                    *vaddr;
68         dma_addr_t              paddr;
69         u32                     size;
70         struct debugfs_blob_wrapper blob;
71         struct dentry           *dentry;
72 };
73
74 struct coda_dev {
75         struct v4l2_device      v4l2_dev;
76         struct video_device     vfd[5];
77         struct platform_device  *plat_dev;
78         const struct coda_devtype *devtype;
79         int                     firmware;
80         struct vdoa_data        *vdoa;
81
82         void __iomem            *regs_base;
83         struct clk              *clk_per;
84         struct clk              *clk_ahb;
85         struct reset_control    *rstc;
86
87         struct coda_aux_buf     codebuf;
88         struct coda_aux_buf     tempbuf;
89         struct coda_aux_buf     workbuf;
90         struct gen_pool         *iram_pool;
91         struct coda_aux_buf     iram;
92
93         spinlock_t              irqlock;
94         struct mutex            dev_mutex;
95         struct mutex            coda_mutex;
96         struct workqueue_struct *workqueue;
97         struct v4l2_m2m_dev     *m2m_dev;
98         struct list_head        instances;
99         struct ida              ida;
100         struct dentry           *debugfs_root;
101 };
102
103 struct coda_codec {
104         u32 mode;
105         u32 src_fourcc;
106         u32 dst_fourcc;
107         u32 max_w;
108         u32 max_h;
109 };
110
111 struct coda_huff_tab;
112
113 struct coda_params {
114         u8                      rot_mode;
115         u8                      h264_intra_qp;
116         u8                      h264_inter_qp;
117         u8                      h264_min_qp;
118         u8                      h264_max_qp;
119         u8                      h264_disable_deblocking_filter_idc;
120         s8                      h264_slice_alpha_c0_offset_div2;
121         s8                      h264_slice_beta_offset_div2;
122         u8                      h264_profile_idc;
123         u8                      h264_level_idc;
124         u8                      mpeg4_intra_qp;
125         u8                      mpeg4_inter_qp;
126         u8                      gop_size;
127         int                     intra_refresh;
128         u8                      jpeg_quality;
129         u8                      jpeg_restart_interval;
130         u8                      *jpeg_qmat_tab[3];
131         int                     codec_mode;
132         int                     codec_mode_aux;
133         enum v4l2_mpeg_video_multi_slice_mode slice_mode;
134         u32                     framerate;
135         u16                     bitrate;
136         u16                     vbv_delay;
137         u32                     vbv_size;
138         u32                     slice_max_bits;
139         u32                     slice_max_mb;
140         bool                    force_ipicture;
141 };
142
143 struct coda_buffer_meta {
144         struct list_head        list;
145         u32                     sequence;
146         struct v4l2_timecode    timecode;
147         u64                     timestamp;
148         u32                     start;
149         u32                     end;
150 };
151
152 /* Per-queue, driver-specific private data */
153 struct coda_q_data {
154         unsigned int            width;
155         unsigned int            height;
156         unsigned int            bytesperline;
157         unsigned int            sizeimage;
158         unsigned int            fourcc;
159         struct v4l2_rect        rect;
160 };
161
162 struct coda_iram_info {
163         u32             axi_sram_use;
164         phys_addr_t     buf_bit_use;
165         phys_addr_t     buf_ip_ac_dc_use;
166         phys_addr_t     buf_dbk_y_use;
167         phys_addr_t     buf_dbk_c_use;
168         phys_addr_t     buf_ovl_use;
169         phys_addr_t     buf_btp_use;
170         phys_addr_t     search_ram_paddr;
171         int             search_ram_size;
172         int             remaining;
173         phys_addr_t     next_paddr;
174 };
175
176 #define GDI_LINEAR_FRAME_MAP 0
177 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
178
179 struct coda_ctx;
180
181 struct coda_context_ops {
182         int (*queue_init)(void *priv, struct vb2_queue *src_vq,
183                           struct vb2_queue *dst_vq);
184         int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
185         int (*start_streaming)(struct coda_ctx *ctx);
186         int (*prepare_run)(struct coda_ctx *ctx);
187         void (*finish_run)(struct coda_ctx *ctx);
188         void (*run_timeout)(struct coda_ctx *ctx);
189         void (*seq_end_work)(struct work_struct *work);
190         void (*release)(struct coda_ctx *ctx);
191 };
192
193 struct coda_ctx {
194         struct coda_dev                 *dev;
195         struct mutex                    buffer_mutex;
196         struct list_head                list;
197         struct work_struct              pic_run_work;
198         struct work_struct              seq_end_work;
199         struct completion               completion;
200         const struct coda_video_device  *cvd;
201         const struct coda_context_ops   *ops;
202         int                             aborting;
203         int                             initialized;
204         int                             streamon_out;
205         int                             streamon_cap;
206         u32                             qsequence;
207         u32                             osequence;
208         u32                             sequence_offset;
209         struct coda_q_data              q_data[2];
210         enum coda_inst_type             inst_type;
211         const struct coda_codec         *codec;
212         enum v4l2_colorspace            colorspace;
213         enum v4l2_xfer_func             xfer_func;
214         enum v4l2_ycbcr_encoding        ycbcr_enc;
215         enum v4l2_quantization          quantization;
216         struct coda_params              params;
217         struct v4l2_ctrl_handler        ctrls;
218         struct v4l2_ctrl                *h264_profile_ctrl;
219         struct v4l2_ctrl                *h264_level_ctrl;
220         struct v4l2_fh                  fh;
221         int                             gopcounter;
222         int                             runcounter;
223         char                            vpu_header[3][64];
224         int                             vpu_header_size[3];
225         struct kfifo                    bitstream_fifo;
226         struct mutex                    bitstream_mutex;
227         struct coda_aux_buf             bitstream;
228         bool                            hold;
229         struct coda_aux_buf             parabuf;
230         struct coda_aux_buf             psbuf;
231         struct coda_aux_buf             slicebuf;
232         struct coda_aux_buf             internal_frames[CODA_MAX_FRAMEBUFFERS];
233         u32                             frame_types[CODA_MAX_FRAMEBUFFERS];
234         struct coda_buffer_meta         frame_metas[CODA_MAX_FRAMEBUFFERS];
235         u32                             frame_errors[CODA_MAX_FRAMEBUFFERS];
236         struct list_head                buffer_meta_list;
237         spinlock_t                      buffer_meta_lock;
238         int                             num_metas;
239         struct coda_aux_buf             workbuf;
240         int                             num_internal_frames;
241         int                             idx;
242         int                             reg_idx;
243         struct coda_iram_info           iram_info;
244         int                             tiled_map_type;
245         u32                             bit_stream_param;
246         u32                             frm_dis_flg;
247         u32                             frame_mem_ctrl;
248         int                             display_idx;
249         struct dentry                   *debugfs_entry;
250         bool                            use_bit;
251         bool                            use_vdoa;
252         struct vdoa_ctx                 *vdoa;
253 };
254
255 extern int coda_debug;
256
257 void coda_write(struct coda_dev *dev, u32 data, u32 reg);
258 unsigned int coda_read(struct coda_dev *dev, u32 reg);
259 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
260                      struct vb2_v4l2_buffer *buf, unsigned int reg_y);
261
262 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
263                        size_t size, const char *name, struct dentry *parent);
264 void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
265
266 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
267                             struct vb2_queue *dst_vq);
268 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
269                             struct vb2_queue *dst_vq);
270
271 int coda_hw_reset(struct coda_ctx *ctx);
272
273 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
274
275 void coda_set_gdi_regs(struct coda_ctx *ctx);
276
277 static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
278                                              enum v4l2_buf_type type)
279 {
280         switch (type) {
281         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
282                 return &(ctx->q_data[V4L2_M2M_SRC]);
283         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
284                 return &(ctx->q_data[V4L2_M2M_DST]);
285         default:
286                 return NULL;
287         }
288 }
289
290 const char *coda_product_name(int product);
291
292 int coda_check_firmware(struct coda_dev *dev);
293
294 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
295 {
296         return kfifo_len(&ctx->bitstream_fifo);
297 }
298
299 void coda_bit_stream_end_flag(struct coda_ctx *ctx);
300
301 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
302                        enum vb2_buffer_state state);
303
304 int coda_h264_filler_nal(int size, char *p);
305 int coda_h264_padding(int size, char *p);
306 int coda_h264_profile(int profile_idc);
307 int coda_h264_level(int level_idc);
308 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
309 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
310                         int *size, int max_size);
311
312 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
313 int coda_jpeg_write_tables(struct coda_ctx *ctx);
314 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
315
316 extern const struct coda_context_ops coda_bit_encode_ops;
317 extern const struct coda_context_ops coda_bit_decode_ops;
318
319 irqreturn_t coda_irq_handler(int irq, void *data);
320
321 #endif /* __CODA_H__ */