1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Coda multi-standard codec IP
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
8 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
14 #include <linux/debugfs.h>
15 #include <linux/idr.h>
16 #include <linux/irqreturn.h>
17 #include <linux/mutex.h>
18 #include <linux/kfifo.h>
19 #include <linux/videodev2.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-fh.h>
24 #include <media/videobuf2-v4l2.h>
26 #include "coda_regs.h"
28 #define CODA_MAX_FRAMEBUFFERS 19
29 #define FMO_SLICE_SAVE_BUF_SIZE (32)
48 struct coda_video_device;
52 enum coda_product product;
53 const struct coda_codec *codecs;
54 unsigned int num_codecs;
55 const struct coda_video_device **vdevs;
56 unsigned int num_vdevs;
66 struct debugfs_blob_wrapper blob;
67 struct dentry *dentry;
71 struct v4l2_device v4l2_dev;
72 struct video_device vfd[6];
74 const struct coda_devtype *devtype;
76 struct vdoa_data *vdoa;
78 void __iomem *regs_base;
81 struct reset_control *rstc;
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;
89 struct mutex dev_mutex;
90 struct mutex coda_mutex;
91 struct workqueue_struct *workqueue;
92 struct v4l2_m2m_dev *m2m_dev;
94 struct dentry *debugfs_root;
105 struct coda_huff_tab;
113 u8 h264_disable_deblocking_filter_idc;
114 s8 h264_slice_alpha_c0_offset_div2;
115 s8 h264_slice_beta_offset_div2;
116 bool h264_constrained_intra_pred_flag;
117 s8 h264_chroma_qp_index_offset;
120 u8 mpeg2_profile_idc;
126 enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling;
128 u8 jpeg_restart_interval;
129 u8 *jpeg_qmat_tab[3];
130 int jpeg_qmat_index[3];
131 int jpeg_huff_dc_index[3];
132 int jpeg_huff_ac_index[3];
134 struct coda_huff_tab *jpeg_huff_tab;
137 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
145 bool gop_size_changed;
146 bool bitrate_changed;
147 bool framerate_changed;
148 bool h264_intra_qp_changed;
149 bool intra_refresh_changed;
150 bool slice_mode_changed;
151 bool frame_rc_enable;
155 struct coda_buffer_meta {
156 struct list_head list;
158 struct v4l2_timecode timecode;
165 /* Per-queue, driver-specific private data */
169 unsigned int bytesperline;
170 unsigned int sizeimage;
172 struct v4l2_rect rect;
175 struct coda_iram_info {
177 phys_addr_t buf_bit_use;
178 phys_addr_t buf_ip_ac_dc_use;
179 phys_addr_t buf_dbk_y_use;
180 phys_addr_t buf_dbk_c_use;
181 phys_addr_t buf_ovl_use;
182 phys_addr_t buf_btp_use;
183 phys_addr_t search_ram_paddr;
186 phys_addr_t next_paddr;
189 #define GDI_LINEAR_FRAME_MAP 0
190 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
194 struct coda_context_ops {
195 int (*queue_init)(void *priv, struct vb2_queue *src_vq,
196 struct vb2_queue *dst_vq);
197 int (*reqbufs)(struct coda_ctx *ctx, struct v4l2_requestbuffers *rb);
198 int (*start_streaming)(struct coda_ctx *ctx);
199 int (*prepare_run)(struct coda_ctx *ctx);
200 void (*finish_run)(struct coda_ctx *ctx);
201 void (*run_timeout)(struct coda_ctx *ctx);
202 void (*seq_init_work)(struct work_struct *work);
203 void (*seq_end_work)(struct work_struct *work);
204 void (*release)(struct coda_ctx *ctx);
207 struct coda_internal_frame {
208 struct coda_aux_buf buf;
209 struct coda_buffer_meta meta;
215 struct coda_dev *dev;
216 struct mutex buffer_mutex;
217 struct work_struct pic_run_work;
218 struct work_struct seq_init_work;
219 struct work_struct seq_end_work;
220 struct completion completion;
221 const struct coda_video_device *cvd;
222 const struct coda_context_ops *ops;
230 struct coda_q_data q_data[2];
231 enum coda_inst_type inst_type;
232 const struct coda_codec *codec;
233 enum v4l2_colorspace colorspace;
234 enum v4l2_xfer_func xfer_func;
235 enum v4l2_ycbcr_encoding ycbcr_enc;
236 enum v4l2_quantization quantization;
237 struct coda_params params;
238 struct v4l2_ctrl_handler ctrls;
239 struct v4l2_ctrl *h264_profile_ctrl;
240 struct v4l2_ctrl *h264_level_ctrl;
241 struct v4l2_ctrl *mpeg2_profile_ctrl;
242 struct v4l2_ctrl *mpeg2_level_ctrl;
243 struct v4l2_ctrl *mpeg4_profile_ctrl;
244 struct v4l2_ctrl *mpeg4_level_ctrl;
249 char vpu_header[3][64];
250 int vpu_header_size[3];
251 struct kfifo bitstream_fifo;
252 struct mutex bitstream_mutex;
253 struct coda_aux_buf bitstream;
255 struct coda_aux_buf parabuf;
256 struct coda_aux_buf psbuf;
257 struct coda_aux_buf slicebuf;
258 struct coda_internal_frame internal_frames[CODA_MAX_FRAMEBUFFERS];
259 struct list_head buffer_meta_list;
260 spinlock_t buffer_meta_lock;
262 struct coda_aux_buf workbuf;
263 int num_internal_frames;
266 struct coda_iram_info iram_info;
268 u32 bit_stream_param;
273 struct dentry *debugfs_entry;
276 struct vdoa_ctx *vdoa;
278 * wakeup mutex used to serialize encoder stop command and finish_run,
279 * ensures that finish_run always either flags the last returned buffer
280 * or wakes up the capture queue to signal EOS afterwards.
282 struct mutex wakeup_mutex;
285 extern int coda_debug;
287 #define coda_dbg(level, ctx, fmt, arg...) \
289 if (coda_debug >= (level)) \
290 v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
291 "%u: " fmt, (ctx)->idx, ##arg); \
294 void coda_write(struct coda_dev *dev, u32 data, u32 reg);
295 unsigned int coda_read(struct coda_dev *dev, u32 reg);
296 void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data,
297 struct vb2_v4l2_buffer *buf, unsigned int reg_y);
299 int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf,
300 size_t size, const char *name, struct dentry *parent);
301 void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf);
303 int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq,
304 struct vb2_queue *dst_vq);
305 int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq,
306 struct vb2_queue *dst_vq);
308 int coda_hw_reset(struct coda_ctx *ctx);
310 void coda_fill_bitstream(struct coda_ctx *ctx, struct list_head *buffer_list);
312 void coda_set_gdi_regs(struct coda_ctx *ctx);
314 static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx,
315 enum v4l2_buf_type type)
318 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
319 return &(ctx->q_data[V4L2_M2M_SRC]);
320 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
321 return &(ctx->q_data[V4L2_M2M_DST]);
327 const char *coda_product_name(int product);
329 int coda_check_firmware(struct coda_dev *dev);
331 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx)
333 return kfifo_len(&ctx->bitstream_fifo);
337 * The bitstream prefetcher needs to read at least 2 256 byte periods past
338 * the desired bitstream position for all data to reach the decoder.
340 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx,
343 return (int)(ctx->bitstream_fifo.kfifo.in - ALIGN(pos, 256)) > 512;
346 bool coda_bitstream_can_fetch_past(struct coda_ctx *ctx, unsigned int pos);
347 int coda_bitstream_flush(struct coda_ctx *ctx);
349 void coda_bit_stream_end_flag(struct coda_ctx *ctx);
351 void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf,
352 enum vb2_buffer_state state);
354 int coda_h264_filler_nal(int size, char *p);
355 int coda_h264_padding(int size, char *p);
356 int coda_h264_profile(int profile_idc);
357 int coda_h264_level(int level_idc);
358 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb);
359 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
360 int *size, int max_size);
362 int coda_mpeg2_profile(int profile_idc);
363 int coda_mpeg2_level(int level_idc);
364 u32 coda_mpeg2_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
365 int coda_mpeg4_profile(int profile_idc);
366 int coda_mpeg4_level(int level_idc);
367 u32 coda_mpeg4_parse_headers(struct coda_ctx *ctx, u8 *buf, u32 size);
369 void coda_update_profile_level_ctrls(struct coda_ctx *ctx, u8 profile_idc,
372 bool coda_jpeg_check_buffer(struct coda_ctx *ctx, struct vb2_buffer *vb);
373 int coda_jpeg_decode_header(struct coda_ctx *ctx, struct vb2_buffer *vb);
374 int coda_jpeg_write_tables(struct coda_ctx *ctx);
375 void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality);
377 extern const struct coda_context_ops coda_bit_encode_ops;
378 extern const struct coda_context_ops coda_bit_decode_ops;
379 extern const struct coda_context_ops coda9_jpeg_encode_ops;
380 extern const struct coda_context_ops coda9_jpeg_decode_ops;
382 irqreturn_t coda_irq_handler(int irq, void *data);
383 irqreturn_t coda9_jpeg_irq_handler(int irq, void *data);
385 #endif /* __CODA_H__ */