coda.h 7.3 KB

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