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