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 | |
| 15 | #include <linux/debugfs.h> |
| 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> |
| 24 | #include <media/videobuf2-core.h> |
| 25 | |
| 26 | #include "coda_regs.h" |
| 27 | |
| 28 | #define CODA_MAX_FRAMEBUFFERS 8 |
| 29 | #define CODA_MAX_FRAME_SIZE 0x100000 |
| 30 | #define FMO_SLICE_SAVE_BUF_SIZE (32) |
| 31 | |
| 32 | enum { |
| 33 | V4L2_M2M_SRC = 0, |
| 34 | V4L2_M2M_DST = 1, |
| 35 | }; |
| 36 | |
| 37 | enum coda_inst_type { |
| 38 | CODA_INST_ENCODER, |
| 39 | CODA_INST_DECODER, |
| 40 | }; |
| 41 | |
| 42 | enum coda_product { |
| 43 | CODA_DX6 = 0xf001, |
| 44 | CODA_7541 = 0xf012, |
| 45 | CODA_960 = 0xf020, |
| 46 | }; |
| 47 | |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 48 | struct coda_video_device; |
| 49 | |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 50 | struct coda_devtype { |
| 51 | char *firmware; |
| 52 | enum coda_product product; |
| 53 | const struct coda_codec *codecs; |
| 54 | unsigned int num_codecs; |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 55 | const struct coda_video_device **vdevs; |
| 56 | unsigned int num_vdevs; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 57 | size_t workbuf_size; |
| 58 | size_t tempbuf_size; |
| 59 | size_t iram_size; |
| 60 | }; |
| 61 | |
| 62 | struct 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 | |
| 70 | struct coda_dev { |
| 71 | struct v4l2_device v4l2_dev; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 72 | struct video_device vfd[5]; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 73 | struct platform_device *plat_dev; |
| 74 | const struct coda_devtype *devtype; |
| 75 | |
| 76 | void __iomem *regs_base; |
| 77 | struct clk *clk_per; |
| 78 | struct clk *clk_ahb; |
| 79 | struct reset_control *rstc; |
| 80 | |
| 81 | struct coda_aux_buf codebuf; |
| 82 | struct coda_aux_buf tempbuf; |
| 83 | struct coda_aux_buf workbuf; |
| 84 | struct gen_pool *iram_pool; |
| 85 | struct coda_aux_buf iram; |
| 86 | |
| 87 | spinlock_t irqlock; |
| 88 | struct mutex dev_mutex; |
| 89 | struct mutex coda_mutex; |
| 90 | struct workqueue_struct *workqueue; |
| 91 | struct v4l2_m2m_dev *m2m_dev; |
| 92 | struct vb2_alloc_ctx *alloc_ctx; |
| 93 | struct list_head instances; |
| 94 | unsigned long instance_mask; |
| 95 | struct dentry *debugfs_root; |
| 96 | }; |
| 97 | |
| 98 | struct coda_codec { |
| 99 | u32 mode; |
| 100 | u32 src_fourcc; |
| 101 | u32 dst_fourcc; |
| 102 | u32 max_w; |
| 103 | u32 max_h; |
| 104 | }; |
| 105 | |
| 106 | struct coda_huff_tab; |
| 107 | |
| 108 | struct 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; |
| 114 | u8 h264_deblk_enabled; |
| 115 | u8 h264_deblk_alpha; |
| 116 | u8 h264_deblk_beta; |
| 117 | u8 mpeg4_intra_qp; |
| 118 | u8 mpeg4_inter_qp; |
| 119 | u8 gop_size; |
| 120 | int intra_refresh; |
Philipp Zabel | cb1d3a3 | 2014-10-02 14:08:31 -0300 | [diff] [blame] | 121 | u8 jpeg_quality; |
| 122 | u8 jpeg_restart_interval; |
| 123 | u8 *jpeg_qmat_tab[3]; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 124 | int codec_mode; |
| 125 | int codec_mode_aux; |
| 126 | enum v4l2_mpeg_video_multi_slice_mode slice_mode; |
| 127 | u32 framerate; |
| 128 | u16 bitrate; |
| 129 | u32 slice_max_bits; |
| 130 | u32 slice_max_mb; |
| 131 | }; |
| 132 | |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 133 | struct coda_buffer_meta { |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 134 | struct list_head list; |
| 135 | u32 sequence; |
| 136 | struct v4l2_timecode timecode; |
| 137 | struct timeval timestamp; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 138 | u32 start; |
| 139 | u32 end; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | /* Per-queue, driver-specific private data */ |
| 143 | struct coda_q_data { |
| 144 | unsigned int width; |
| 145 | unsigned int height; |
| 146 | unsigned int bytesperline; |
| 147 | unsigned int sizeimage; |
| 148 | unsigned int fourcc; |
| 149 | struct v4l2_rect rect; |
| 150 | }; |
| 151 | |
| 152 | struct coda_iram_info { |
| 153 | u32 axi_sram_use; |
| 154 | phys_addr_t buf_bit_use; |
| 155 | phys_addr_t buf_ip_ac_dc_use; |
| 156 | phys_addr_t buf_dbk_y_use; |
| 157 | phys_addr_t buf_dbk_c_use; |
| 158 | phys_addr_t buf_ovl_use; |
| 159 | phys_addr_t buf_btp_use; |
| 160 | phys_addr_t search_ram_paddr; |
| 161 | int search_ram_size; |
| 162 | int remaining; |
| 163 | phys_addr_t next_paddr; |
| 164 | }; |
| 165 | |
| 166 | struct gdi_tiled_map { |
| 167 | int xy2ca_map[16]; |
| 168 | int xy2ba_map[16]; |
| 169 | int xy2ra_map[16]; |
| 170 | int rbc2axi_map[32]; |
| 171 | int xy2rbc_config; |
| 172 | int map_type; |
| 173 | #define GDI_LINEAR_FRAME_MAP 0 |
| 174 | }; |
| 175 | |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 176 | struct coda_ctx; |
| 177 | |
| 178 | struct coda_context_ops { |
| 179 | int (*queue_init)(void *priv, struct vb2_queue *src_vq, |
| 180 | struct vb2_queue *dst_vq); |
| 181 | int (*start_streaming)(struct coda_ctx *ctx); |
| 182 | int (*prepare_run)(struct coda_ctx *ctx); |
| 183 | void (*finish_run)(struct coda_ctx *ctx); |
| 184 | void (*seq_end_work)(struct work_struct *work); |
| 185 | void (*release)(struct coda_ctx *ctx); |
| 186 | }; |
| 187 | |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 188 | struct coda_ctx { |
| 189 | struct coda_dev *dev; |
| 190 | struct mutex buffer_mutex; |
| 191 | struct list_head list; |
| 192 | struct work_struct pic_run_work; |
| 193 | struct work_struct seq_end_work; |
| 194 | struct completion completion; |
Philipp Zabel | 2c11d1b | 2014-10-02 14:08:28 -0300 | [diff] [blame] | 195 | const struct coda_video_device *cvd; |
Philipp Zabel | a1192a1 | 2014-07-23 12:28:40 -0300 | [diff] [blame] | 196 | const struct coda_context_ops *ops; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 197 | int aborting; |
| 198 | int initialized; |
| 199 | int streamon_out; |
| 200 | int streamon_cap; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 201 | u32 qsequence; |
| 202 | u32 osequence; |
| 203 | u32 sequence_offset; |
| 204 | struct coda_q_data q_data[2]; |
| 205 | enum coda_inst_type inst_type; |
| 206 | const struct coda_codec *codec; |
| 207 | enum v4l2_colorspace colorspace; |
| 208 | struct coda_params params; |
| 209 | struct v4l2_ctrl_handler ctrls; |
| 210 | struct v4l2_fh fh; |
| 211 | int gopcounter; |
| 212 | int runcounter; |
| 213 | char vpu_header[3][64]; |
| 214 | int vpu_header_size[3]; |
| 215 | struct kfifo bitstream_fifo; |
| 216 | struct mutex bitstream_mutex; |
| 217 | struct coda_aux_buf bitstream; |
| 218 | bool hold; |
| 219 | struct coda_aux_buf parabuf; |
| 220 | struct coda_aux_buf psbuf; |
| 221 | struct coda_aux_buf slicebuf; |
| 222 | struct coda_aux_buf internal_frames[CODA_MAX_FRAMEBUFFERS]; |
| 223 | u32 frame_types[CODA_MAX_FRAMEBUFFERS]; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 224 | struct coda_buffer_meta frame_metas[CODA_MAX_FRAMEBUFFERS]; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 225 | u32 frame_errors[CODA_MAX_FRAMEBUFFERS]; |
Philipp Zabel | 7cbb105 | 2014-10-02 14:08:32 -0300 | [diff] [blame] | 226 | struct list_head buffer_meta_list; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 227 | struct coda_aux_buf workbuf; |
| 228 | int num_internal_frames; |
| 229 | int idx; |
| 230 | int reg_idx; |
| 231 | struct coda_iram_info iram_info; |
| 232 | struct gdi_tiled_map tiled_map; |
| 233 | u32 bit_stream_param; |
| 234 | u32 frm_dis_flg; |
| 235 | u32 frame_mem_ctrl; |
| 236 | int display_idx; |
| 237 | struct dentry *debugfs_entry; |
Philipp Zabel | a22496c | 2015-01-23 13:51:33 -0300 | [diff] [blame] | 238 | bool use_bit; |
Philipp Zabel | a2b3e46 | 2014-07-23 12:28:39 -0300 | [diff] [blame] | 239 | }; |
Philipp Zabel | 4f4ee9e | 2014-07-23 12:28:44 -0300 | [diff] [blame] | 240 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 241 | extern int coda_debug; |
| 242 | |
| 243 | void coda_write(struct coda_dev *dev, u32 data, u32 reg); |
| 244 | unsigned int coda_read(struct coda_dev *dev, u32 reg); |
Philipp Zabel | 856d7d9 | 2014-09-29 09:53:44 -0300 | [diff] [blame] | 245 | void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data, |
| 246 | struct vb2_buffer *buf, unsigned int reg_y); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 247 | |
| 248 | int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf, |
| 249 | size_t size, const char *name, struct dentry *parent); |
| 250 | void coda_free_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf); |
| 251 | |
| 252 | static inline int coda_alloc_context_buf(struct coda_ctx *ctx, |
| 253 | struct coda_aux_buf *buf, size_t size, |
| 254 | const char *name) |
| 255 | { |
| 256 | return coda_alloc_aux_buf(ctx->dev, buf, size, name, ctx->debugfs_entry); |
| 257 | } |
| 258 | |
| 259 | int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq, |
| 260 | struct vb2_queue *dst_vq); |
| 261 | int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq, |
| 262 | struct vb2_queue *dst_vq); |
| 263 | |
| 264 | int coda_hw_reset(struct coda_ctx *ctx); |
| 265 | |
| 266 | void coda_fill_bitstream(struct coda_ctx *ctx); |
| 267 | |
| 268 | void coda_set_gdi_regs(struct coda_ctx *ctx); |
| 269 | |
| 270 | static inline struct coda_q_data *get_q_data(struct coda_ctx *ctx, |
| 271 | enum v4l2_buf_type type) |
| 272 | { |
| 273 | switch (type) { |
| 274 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 275 | return &(ctx->q_data[V4L2_M2M_SRC]); |
| 276 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 277 | return &(ctx->q_data[V4L2_M2M_DST]); |
| 278 | default: |
| 279 | return NULL; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | const char *coda_product_name(int product); |
| 284 | |
| 285 | int coda_check_firmware(struct coda_dev *dev); |
| 286 | |
Philipp Zabel | 2b76846 | 2015-03-24 14:30:49 -0300 | [diff] [blame^] | 287 | static inline unsigned int coda_get_bitstream_payload(struct coda_ctx *ctx) |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 288 | { |
| 289 | return kfifo_len(&ctx->bitstream_fifo); |
| 290 | } |
| 291 | |
| 292 | void coda_bit_stream_end_flag(struct coda_ctx *ctx); |
| 293 | |
Philipp Zabel | 4f4ee9e | 2014-07-23 12:28:44 -0300 | [diff] [blame] | 294 | int coda_h264_padding(int size, char *p); |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 295 | |
Philipp Zabel | edc16cb | 2014-10-08 13:09:27 -0300 | [diff] [blame] | 296 | 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] | 297 | int coda_jpeg_write_tables(struct coda_ctx *ctx); |
| 298 | void coda_set_jpeg_compression_quality(struct coda_ctx *ctx, int quality); |
| 299 | |
Philipp Zabel | 79924ca | 2014-07-23 12:28:45 -0300 | [diff] [blame] | 300 | extern const struct coda_context_ops coda_bit_encode_ops; |
| 301 | extern const struct coda_context_ops coda_bit_decode_ops; |
| 302 | |
| 303 | irqreturn_t coda_irq_handler(int irq, void *data); |