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
[5];
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
;
127 u8 jpeg_restart_interval
;
128 u8
*jpeg_qmat_tab
[3];
132 enum v4l2_mpeg_video_multi_slice_mode slice_mode
;
140 bool gop_size_changed
;
141 bool bitrate_changed
;
142 bool framerate_changed
;
143 bool h264_intra_qp_changed
;
144 bool intra_refresh_changed
;
145 bool slice_mode_changed
;
148 struct coda_buffer_meta
{
149 struct list_head list
;
151 struct v4l2_timecode timecode
;
158 /* Per-queue, driver-specific private data */
162 unsigned int bytesperline
;
163 unsigned int sizeimage
;
165 struct v4l2_rect rect
;
168 struct coda_iram_info
{
170 phys_addr_t buf_bit_use
;
171 phys_addr_t buf_ip_ac_dc_use
;
172 phys_addr_t buf_dbk_y_use
;
173 phys_addr_t buf_dbk_c_use
;
174 phys_addr_t buf_ovl_use
;
175 phys_addr_t buf_btp_use
;
176 phys_addr_t search_ram_paddr
;
179 phys_addr_t next_paddr
;
182 #define GDI_LINEAR_FRAME_MAP 0
183 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
187 struct coda_context_ops
{
188 int (*queue_init
)(void *priv
, struct vb2_queue
*src_vq
,
189 struct vb2_queue
*dst_vq
);
190 int (*reqbufs
)(struct coda_ctx
*ctx
, struct v4l2_requestbuffers
*rb
);
191 int (*start_streaming
)(struct coda_ctx
*ctx
);
192 int (*prepare_run
)(struct coda_ctx
*ctx
);
193 void (*finish_run
)(struct coda_ctx
*ctx
);
194 void (*run_timeout
)(struct coda_ctx
*ctx
);
195 void (*seq_init_work
)(struct work_struct
*work
);
196 void (*seq_end_work
)(struct work_struct
*work
);
197 void (*release
)(struct coda_ctx
*ctx
);
200 struct coda_internal_frame
{
201 struct coda_aux_buf buf
;
202 struct coda_buffer_meta meta
;
208 struct coda_dev
*dev
;
209 struct mutex buffer_mutex
;
210 struct work_struct pic_run_work
;
211 struct work_struct seq_init_work
;
212 struct work_struct seq_end_work
;
213 struct completion completion
;
214 const struct coda_video_device
*cvd
;
215 const struct coda_context_ops
*ops
;
223 struct coda_q_data q_data
[2];
224 enum coda_inst_type inst_type
;
225 const struct coda_codec
*codec
;
226 enum v4l2_colorspace colorspace
;
227 enum v4l2_xfer_func xfer_func
;
228 enum v4l2_ycbcr_encoding ycbcr_enc
;
229 enum v4l2_quantization quantization
;
230 struct coda_params params
;
231 struct v4l2_ctrl_handler ctrls
;
232 struct v4l2_ctrl
*h264_profile_ctrl
;
233 struct v4l2_ctrl
*h264_level_ctrl
;
234 struct v4l2_ctrl
*mpeg2_profile_ctrl
;
235 struct v4l2_ctrl
*mpeg2_level_ctrl
;
236 struct v4l2_ctrl
*mpeg4_profile_ctrl
;
237 struct v4l2_ctrl
*mpeg4_level_ctrl
;
241 char vpu_header
[3][64];
242 int vpu_header_size
[3];
243 struct kfifo bitstream_fifo
;
244 struct mutex bitstream_mutex
;
245 struct coda_aux_buf bitstream
;
247 struct coda_aux_buf parabuf
;
248 struct coda_aux_buf psbuf
;
249 struct coda_aux_buf slicebuf
;
250 struct coda_internal_frame internal_frames
[CODA_MAX_FRAMEBUFFERS
];
251 struct list_head buffer_meta_list
;
252 spinlock_t buffer_meta_lock
;
254 struct coda_aux_buf workbuf
;
255 int num_internal_frames
;
258 struct coda_iram_info iram_info
;
260 u32 bit_stream_param
;
265 struct dentry
*debugfs_entry
;
268 struct vdoa_ctx
*vdoa
;
270 * wakeup mutex used to serialize encoder stop command and finish_run,
271 * ensures that finish_run always either flags the last returned buffer
272 * or wakes up the capture queue to signal EOS afterwards.
274 struct mutex wakeup_mutex
;
277 extern int coda_debug
;
279 #define coda_dbg(level, ctx, fmt, arg...) \
281 if (coda_debug >= (level)) \
282 v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
283 "%u: " fmt, (ctx)->idx, ##arg); \
286 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
);
287 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
);
288 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
289 struct vb2_v4l2_buffer
*buf
, unsigned int reg_y
);
291 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
292 size_t size
, const char *name
, struct dentry
*parent
);
293 void coda_free_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
);
295 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
296 struct vb2_queue
*dst_vq
);
297 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
298 struct vb2_queue
*dst_vq
);
300 int coda_hw_reset(struct coda_ctx
*ctx
);
302 void coda_fill_bitstream(struct coda_ctx
*ctx
, struct list_head
*buffer_list
);
304 void coda_set_gdi_regs(struct coda_ctx
*ctx
);
306 static inline struct coda_q_data
*get_q_data(struct coda_ctx
*ctx
,
307 enum v4l2_buf_type type
)
310 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
311 return &(ctx
->q_data
[V4L2_M2M_SRC
]);
312 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
313 return &(ctx
->q_data
[V4L2_M2M_DST
]);
319 const char *coda_product_name(int product
);
321 int coda_check_firmware(struct coda_dev
*dev
);
323 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx
*ctx
)
325 return kfifo_len(&ctx
->bitstream_fifo
);
329 * The bitstream prefetcher needs to read at least 2 256 byte periods past
330 * the desired bitstream position for all data to reach the decoder.
332 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx
*ctx
,
335 return (int)(ctx
->bitstream_fifo
.kfifo
.in
- ALIGN(pos
, 256)) > 512;
338 bool coda_bitstream_can_fetch_past(struct coda_ctx
*ctx
, unsigned int pos
);
339 int coda_bitstream_flush(struct coda_ctx
*ctx
);
341 void coda_bit_stream_end_flag(struct coda_ctx
*ctx
);
343 void coda_m2m_buf_done(struct coda_ctx
*ctx
, struct vb2_v4l2_buffer
*buf
,
344 enum vb2_buffer_state state
);
346 int coda_h264_filler_nal(int size
, char *p
);
347 int coda_h264_padding(int size
, char *p
);
348 int coda_h264_profile(int profile_idc
);
349 int coda_h264_level(int level_idc
);
350 int coda_sps_parse_profile(struct coda_ctx
*ctx
, struct vb2_buffer
*vb
);
351 int coda_h264_sps_fixup(struct coda_ctx
*ctx
, int width
, int height
, char *buf
,
352 int *size
, int max_size
);
354 int coda_mpeg2_profile(int profile_idc
);
355 int coda_mpeg2_level(int level_idc
);
356 u32
coda_mpeg2_parse_headers(struct coda_ctx
*ctx
, u8
*buf
, u32 size
);
357 int coda_mpeg4_profile(int profile_idc
);
358 int coda_mpeg4_level(int level_idc
);
359 u32
coda_mpeg4_parse_headers(struct coda_ctx
*ctx
, u8
*buf
, u32 size
);
361 void coda_update_profile_level_ctrls(struct coda_ctx
*ctx
, u8 profile_idc
,
364 bool coda_jpeg_check_buffer(struct coda_ctx
*ctx
, struct vb2_buffer
*vb
);
365 int coda_jpeg_write_tables(struct coda_ctx
*ctx
);
366 void coda_set_jpeg_compression_quality(struct coda_ctx
*ctx
, int quality
);
368 extern const struct coda_context_ops coda_bit_encode_ops
;
369 extern const struct coda_context_ops coda_bit_decode_ops
;
370 extern const struct coda_context_ops coda9_jpeg_encode_ops
;
372 irqreturn_t
coda_irq_handler(int irq
, void *data
);
373 irqreturn_t
coda9_jpeg_irq_handler(int irq
, void *data
);
375 #endif /* __CODA_H__ */