mtk_vcodec_drv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: PC Chen <pc.chen@mediatek.com>
  4. * Tiffany Lin <tiffany.lin@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #ifndef _MTK_VCODEC_DRV_H_
  16. #define _MTK_VCODEC_DRV_H_
  17. #include <linux/platform_device.h>
  18. #include <linux/videodev2.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-ioctl.h>
  22. #include <media/videobuf2-core.h>
  23. #include "mtk_vcodec_util.h"
  24. #define MTK_VCODEC_DRV_NAME "mtk_vcodec_drv"
  25. #define MTK_VCODEC_DEC_NAME "mtk-vcodec-dec"
  26. #define MTK_VCODEC_ENC_NAME "mtk-vcodec-enc"
  27. #define MTK_PLATFORM_STR "platform:mt8173"
  28. #define MTK_VCODEC_MAX_PLANES 3
  29. #define MTK_V4L2_BENCHMARK 0
  30. #define WAIT_INTR_TIMEOUT_MS 1000
  31. /**
  32. * enum mtk_hw_reg_idx - MTK hw register base index
  33. */
  34. enum mtk_hw_reg_idx {
  35. VDEC_SYS,
  36. VDEC_MISC,
  37. VDEC_LD,
  38. VDEC_TOP,
  39. VDEC_CM,
  40. VDEC_AD,
  41. VDEC_AV,
  42. VDEC_PP,
  43. VDEC_HWD,
  44. VDEC_HWQ,
  45. VDEC_HWB,
  46. VDEC_HWG,
  47. NUM_MAX_VDEC_REG_BASE,
  48. /* h264 encoder */
  49. VENC_SYS = NUM_MAX_VDEC_REG_BASE,
  50. /* vp8 encoder */
  51. VENC_LT_SYS,
  52. NUM_MAX_VCODEC_REG_BASE
  53. };
  54. /**
  55. * enum mtk_instance_type - The type of an MTK Vcodec instance.
  56. */
  57. enum mtk_instance_type {
  58. MTK_INST_DECODER = 0,
  59. MTK_INST_ENCODER = 1,
  60. };
  61. /**
  62. * enum mtk_instance_state - The state of an MTK Vcodec instance.
  63. * @MTK_STATE_FREE - default state when instance is created
  64. * @MTK_STATE_INIT - vcodec instance is initialized
  65. * @MTK_STATE_HEADER - vdec had sps/pps header parsed or venc
  66. * had sps/pps header encoded
  67. * @MTK_STATE_FLUSH - vdec is flushing. Only used by decoder
  68. * @MTK_STATE_ABORT - vcodec should be aborted
  69. */
  70. enum mtk_instance_state {
  71. MTK_STATE_FREE = 0,
  72. MTK_STATE_INIT = 1,
  73. MTK_STATE_HEADER = 2,
  74. MTK_STATE_FLUSH = 3,
  75. MTK_STATE_ABORT = 4,
  76. };
  77. /**
  78. * struct mtk_encode_param - General encoding parameters type
  79. */
  80. enum mtk_encode_param {
  81. MTK_ENCODE_PARAM_NONE = 0,
  82. MTK_ENCODE_PARAM_BITRATE = (1 << 0),
  83. MTK_ENCODE_PARAM_FRAMERATE = (1 << 1),
  84. MTK_ENCODE_PARAM_INTRA_PERIOD = (1 << 2),
  85. MTK_ENCODE_PARAM_FORCE_INTRA = (1 << 3),
  86. MTK_ENCODE_PARAM_GOP_SIZE = (1 << 4),
  87. };
  88. enum mtk_fmt_type {
  89. MTK_FMT_DEC = 0,
  90. MTK_FMT_ENC = 1,
  91. MTK_FMT_FRAME = 2,
  92. };
  93. /**
  94. * struct mtk_video_fmt - Structure used to store information about pixelformats
  95. */
  96. struct mtk_video_fmt {
  97. u32 fourcc;
  98. enum mtk_fmt_type type;
  99. u32 num_planes;
  100. };
  101. /**
  102. * struct mtk_codec_framesizes - Structure used to store information about
  103. * framesizes
  104. */
  105. struct mtk_codec_framesizes {
  106. u32 fourcc;
  107. struct v4l2_frmsize_stepwise stepwise;
  108. };
  109. /**
  110. * struct mtk_q_type - Type of queue
  111. */
  112. enum mtk_q_type {
  113. MTK_Q_DATA_SRC = 0,
  114. MTK_Q_DATA_DST = 1,
  115. };
  116. /**
  117. * struct mtk_q_data - Structure used to store information about queue
  118. */
  119. struct mtk_q_data {
  120. unsigned int visible_width;
  121. unsigned int visible_height;
  122. unsigned int coded_width;
  123. unsigned int coded_height;
  124. enum v4l2_field field;
  125. unsigned int bytesperline[MTK_VCODEC_MAX_PLANES];
  126. unsigned int sizeimage[MTK_VCODEC_MAX_PLANES];
  127. struct mtk_video_fmt *fmt;
  128. };
  129. /**
  130. * struct mtk_enc_params - General encoding parameters
  131. * @bitrate: target bitrate in bits per second
  132. * @num_b_frame: number of b frames between p-frame
  133. * @rc_frame: frame based rate control
  134. * @rc_mb: macroblock based rate control
  135. * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
  136. * with the first frame
  137. * @intra_period: I frame period
  138. * @gop_size: group of picture size, it's used as the intra frame period
  139. * @framerate_num: frame rate numerator. ex: framerate_num=30 and
  140. * framerate_denom=1 menas FPS is 30
  141. * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
  142. * framerate_denom=1 menas FPS is 30
  143. * @h264_max_qp: Max value for H.264 quantization parameter
  144. * @h264_profile: V4L2 defined H.264 profile
  145. * @h264_level: V4L2 defined H.264 level
  146. * @force_intra: force/insert intra frame
  147. */
  148. struct mtk_enc_params {
  149. unsigned int bitrate;
  150. unsigned int num_b_frame;
  151. unsigned int rc_frame;
  152. unsigned int rc_mb;
  153. unsigned int seq_hdr_mode;
  154. unsigned int intra_period;
  155. unsigned int gop_size;
  156. unsigned int framerate_num;
  157. unsigned int framerate_denom;
  158. unsigned int h264_max_qp;
  159. unsigned int h264_profile;
  160. unsigned int h264_level;
  161. unsigned int force_intra;
  162. };
  163. /**
  164. * struct mtk_vcodec_pm - Power management data structure
  165. */
  166. struct mtk_vcodec_pm {
  167. struct clk *vdec_bus_clk_src;
  168. struct clk *vencpll;
  169. struct clk *vcodecpll;
  170. struct clk *univpll_d2;
  171. struct clk *clk_cci400_sel;
  172. struct clk *vdecpll;
  173. struct clk *vdec_sel;
  174. struct clk *vencpll_d2;
  175. struct clk *venc_sel;
  176. struct clk *univpll1_d2;
  177. struct clk *venc_lt_sel;
  178. struct device *larbvdec;
  179. struct device *larbvenc;
  180. struct device *larbvenclt;
  181. struct device *dev;
  182. struct mtk_vcodec_dev *mtkdev;
  183. };
  184. /**
  185. * struct vdec_pic_info - picture size information
  186. * @pic_w: picture width
  187. * @pic_h: picture height
  188. * @buf_w: picture buffer width (64 aligned up from pic_w)
  189. * @buf_h: picture buffer heiht (64 aligned up from pic_h)
  190. * @y_bs_sz: Y bitstream size
  191. * @c_bs_sz: CbCr bitstream size
  192. * @y_len_sz: additional size required to store decompress information for y
  193. * plane
  194. * @c_len_sz: additional size required to store decompress information for cbcr
  195. * plane
  196. * E.g. suppose picture size is 176x144,
  197. * buffer size will be aligned to 176x160.
  198. */
  199. struct vdec_pic_info {
  200. unsigned int pic_w;
  201. unsigned int pic_h;
  202. unsigned int buf_w;
  203. unsigned int buf_h;
  204. unsigned int y_bs_sz;
  205. unsigned int c_bs_sz;
  206. unsigned int y_len_sz;
  207. unsigned int c_len_sz;
  208. };
  209. /**
  210. * struct mtk_vcodec_ctx - Context (instance) private data.
  211. *
  212. * @type: type of the instance - decoder or encoder
  213. * @dev: pointer to the mtk_vcodec_dev of the device
  214. * @list: link to ctx_list of mtk_vcodec_dev
  215. * @fh: struct v4l2_fh
  216. * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
  217. * @q_data: store information of input and output queue
  218. * of the context
  219. * @id: index of the context that this structure describes
  220. * @state: state of the context
  221. * @param_change: indicate encode parameter type
  222. * @enc_params: encoding parameters
  223. * @dec_if: hooked decoder driver interface
  224. * @enc_if: hoooked encoder driver interface
  225. * @drv_handle: driver handle for specific decode/encode instance
  226. *
  227. * @picinfo: store picture info after header parsing
  228. * @dpb_size: store dpb count after header parsing
  229. * @int_cond: variable used by the waitqueue
  230. * @int_type: type of the last interrupt
  231. * @queue: waitqueue that can be used to wait for this context to
  232. * finish
  233. * @irq_status: irq status
  234. *
  235. * @ctrl_hdl: handler for v4l2 framework
  236. * @decode_work: worker for the decoding
  237. * @encode_work: worker for the encoding
  238. * @last_decoded_picinfo: pic information get from latest decode
  239. * @empty_flush_buf: a fake size-0 capture buffer that indicates flush
  240. *
  241. * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
  242. * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
  243. * @quantization: enum v4l2_quantization, colorspace quantization
  244. * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
  245. * @lock: protect variables accessed by V4L2 threads and worker thread such as
  246. * mtk_video_dec_buf.
  247. */
  248. struct mtk_vcodec_ctx {
  249. enum mtk_instance_type type;
  250. struct mtk_vcodec_dev *dev;
  251. struct list_head list;
  252. struct v4l2_fh fh;
  253. struct v4l2_m2m_ctx *m2m_ctx;
  254. struct mtk_q_data q_data[2];
  255. int id;
  256. enum mtk_instance_state state;
  257. enum mtk_encode_param param_change;
  258. struct mtk_enc_params enc_params;
  259. const struct vdec_common_if *dec_if;
  260. const struct venc_common_if *enc_if;
  261. unsigned long drv_handle;
  262. struct vdec_pic_info picinfo;
  263. int dpb_size;
  264. int int_cond;
  265. int int_type;
  266. wait_queue_head_t queue;
  267. unsigned int irq_status;
  268. struct v4l2_ctrl_handler ctrl_hdl;
  269. struct work_struct decode_work;
  270. struct work_struct encode_work;
  271. struct vdec_pic_info last_decoded_picinfo;
  272. struct mtk_video_dec_buf *empty_flush_buf;
  273. enum v4l2_colorspace colorspace;
  274. enum v4l2_ycbcr_encoding ycbcr_enc;
  275. enum v4l2_quantization quantization;
  276. enum v4l2_xfer_func xfer_func;
  277. int decoded_frame_cnt;
  278. struct mutex lock;
  279. };
  280. /**
  281. * struct mtk_vcodec_dev - driver data
  282. * @v4l2_dev: V4L2 device to register video devices for.
  283. * @vfd_dec: Video device for decoder
  284. * @vfd_enc: Video device for encoder.
  285. *
  286. * @m2m_dev_dec: m2m device for decoder
  287. * @m2m_dev_enc: m2m device for encoder.
  288. * @plat_dev: platform device
  289. * @vpu_plat_dev: mtk vpu platform device
  290. * @ctx_list: list of struct mtk_vcodec_ctx
  291. * @irqlock: protect data access by irq handler and work thread
  292. * @curr_ctx: The context that is waiting for codec hardware
  293. *
  294. * @reg_base: Mapped address of MTK Vcodec registers.
  295. *
  296. * @id_counter: used to identify current opened instance
  297. *
  298. * @encode_workqueue: encode work queue
  299. *
  300. * @int_cond: used to identify interrupt condition happen
  301. * @int_type: used to identify what kind of interrupt condition happen
  302. * @dev_mutex: video_device lock
  303. * @queue: waitqueue for waiting for completion of device commands
  304. *
  305. * @dec_irq: decoder irq resource
  306. * @enc_irq: h264 encoder irq resource
  307. * @enc_lt_irq: vp8 encoder irq resource
  308. *
  309. * @dec_mutex: decoder hardware lock
  310. * @enc_mutex: encoder hardware lock.
  311. *
  312. * @pm: power management control
  313. * @dec_capability: used to identify decode capability, ex: 4k
  314. * @enc_capability: used to identify encode capability
  315. */
  316. struct mtk_vcodec_dev {
  317. struct v4l2_device v4l2_dev;
  318. struct video_device *vfd_dec;
  319. struct video_device *vfd_enc;
  320. struct v4l2_m2m_dev *m2m_dev_dec;
  321. struct v4l2_m2m_dev *m2m_dev_enc;
  322. struct platform_device *plat_dev;
  323. struct platform_device *vpu_plat_dev;
  324. struct list_head ctx_list;
  325. spinlock_t irqlock;
  326. struct mtk_vcodec_ctx *curr_ctx;
  327. void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
  328. unsigned long id_counter;
  329. struct workqueue_struct *decode_workqueue;
  330. struct workqueue_struct *encode_workqueue;
  331. int int_cond;
  332. int int_type;
  333. struct mutex dev_mutex;
  334. wait_queue_head_t queue;
  335. int dec_irq;
  336. int enc_irq;
  337. int enc_lt_irq;
  338. struct mutex dec_mutex;
  339. struct mutex enc_mutex;
  340. struct mtk_vcodec_pm pm;
  341. unsigned int dec_capability;
  342. unsigned int enc_capability;
  343. };
  344. static inline struct mtk_vcodec_ctx *fh_to_ctx(struct v4l2_fh *fh)
  345. {
  346. return container_of(fh, struct mtk_vcodec_ctx, fh);
  347. }
  348. static inline struct mtk_vcodec_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
  349. {
  350. return container_of(ctrl->handler, struct mtk_vcodec_ctx, ctrl_hdl);
  351. }
  352. #endif /* _MTK_VCODEC_DRV_H_ */