coda.h 8.0 KB

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