coda.h 7.6 KB

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