1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
8 #ifndef _MTK_VCODEC_DRV_H_
9 #define _MTK_VCODEC_DRV_H_
11 #include <linux/platform_device.h>
12 #include <linux/videodev2.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-ioctl.h>
16 #include <media/videobuf2-core.h>
17 #include "mtk_vcodec_util.h"
19 #define MTK_VCODEC_DRV_NAME "mtk_vcodec_drv"
20 #define MTK_VCODEC_DEC_NAME "mtk-vcodec-dec"
21 #define MTK_VCODEC_ENC_NAME "mtk-vcodec-enc"
22 #define MTK_PLATFORM_STR "platform:mt8173"
24 #define MTK_VCODEC_MAX_PLANES 3
25 #define MTK_V4L2_BENCHMARK 0
26 #define WAIT_INTR_TIMEOUT_MS 1000
29 * enum mtk_hw_reg_idx - MTK hw register base index
44 NUM_MAX_VDEC_REG_BASE
,
46 VENC_SYS
= NUM_MAX_VDEC_REG_BASE
,
49 NUM_MAX_VCODEC_REG_BASE
53 * enum mtk_instance_type - The type of an MTK Vcodec instance.
55 enum mtk_instance_type
{
61 * enum mtk_instance_state - The state of an MTK Vcodec instance.
62 * @MTK_STATE_FREE - default state when instance is created
63 * @MTK_STATE_INIT - vcodec instance is initialized
64 * @MTK_STATE_HEADER - vdec had sps/pps header parsed or venc
65 * had sps/pps header encoded
66 * @MTK_STATE_FLUSH - vdec is flushing. Only used by decoder
67 * @MTK_STATE_ABORT - vcodec should be aborted
69 enum mtk_instance_state
{
78 * struct mtk_encode_param - General encoding parameters type
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),
96 * struct mtk_video_fmt - Structure used to store information about pixelformats
98 struct mtk_video_fmt
{
100 enum mtk_fmt_type type
;
106 * struct mtk_codec_framesizes - Structure used to store information about
109 struct mtk_codec_framesizes
{
111 struct v4l2_frmsize_stepwise stepwise
;
115 * struct mtk_q_type - Type of queue
123 * struct mtk_q_data - Structure used to store information about queue
126 unsigned int visible_width
;
127 unsigned int visible_height
;
128 unsigned int coded_width
;
129 unsigned int coded_height
;
130 enum v4l2_field field
;
131 unsigned int bytesperline
[MTK_VCODEC_MAX_PLANES
];
132 unsigned int sizeimage
[MTK_VCODEC_MAX_PLANES
];
133 const struct mtk_video_fmt
*fmt
;
137 * struct mtk_enc_params - General encoding parameters
138 * @bitrate: target bitrate in bits per second
139 * @num_b_frame: number of b frames between p-frame
140 * @rc_frame: frame based rate control
141 * @rc_mb: macroblock based rate control
142 * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
143 * with the first frame
144 * @intra_period: I frame period
145 * @gop_size: group of picture size, it's used as the intra frame period
146 * @framerate_num: frame rate numerator. ex: framerate_num=30 and
147 * framerate_denom=1 means FPS is 30
148 * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
149 * framerate_denom=1 means FPS is 30
150 * @h264_max_qp: Max value for H.264 quantization parameter
151 * @h264_profile: V4L2 defined H.264 profile
152 * @h264_level: V4L2 defined H.264 level
153 * @force_intra: force/insert intra frame
155 struct mtk_enc_params
{
156 unsigned int bitrate
;
157 unsigned int num_b_frame
;
158 unsigned int rc_frame
;
160 unsigned int seq_hdr_mode
;
161 unsigned int intra_period
;
162 unsigned int gop_size
;
163 unsigned int framerate_num
;
164 unsigned int framerate_denom
;
165 unsigned int h264_max_qp
;
166 unsigned int h264_profile
;
167 unsigned int h264_level
;
168 unsigned int force_intra
;
172 * struct mtk_vcodec_clk_info - Structure used to store clock name
174 struct mtk_vcodec_clk_info
{
175 const char *clk_name
;
176 struct clk
*vcodec_clk
;
180 * struct mtk_vcodec_clk - Structure used to store vcodec clock information
182 struct mtk_vcodec_clk
{
183 struct mtk_vcodec_clk_info
*clk_info
;
188 * struct mtk_vcodec_pm - Power management data structure
190 struct mtk_vcodec_pm
{
191 struct mtk_vcodec_clk vdec_clk
;
192 struct device
*larbvdec
;
194 struct mtk_vcodec_clk venc_clk
;
195 struct device
*larbvenc
;
196 struct device
*larbvenclt
;
198 struct mtk_vcodec_dev
*mtkdev
;
202 * struct vdec_pic_info - picture size information
203 * @pic_w: picture width
204 * @pic_h: picture height
205 * @buf_w: picture buffer width (64 aligned up from pic_w)
206 * @buf_h: picture buffer heiht (64 aligned up from pic_h)
207 * @fb_sz: bitstream size of each plane
208 * E.g. suppose picture size is 176x144,
209 * buffer size will be aligned to 176x160.
210 * @cap_fourcc: fourcc number(may changed when resolution change)
211 * @reserved: align struct to 64-bit in order to adjust 32-bit and 64-bit os.
213 struct vdec_pic_info
{
218 unsigned int fb_sz
[VIDEO_MAX_PLANES
];
219 unsigned int cap_fourcc
;
220 unsigned int reserved
;
224 * struct mtk_vcodec_ctx - Context (instance) private data.
226 * @type: type of the instance - decoder or encoder
227 * @dev: pointer to the mtk_vcodec_dev of the device
228 * @list: link to ctx_list of mtk_vcodec_dev
229 * @fh: struct v4l2_fh
230 * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
231 * @q_data: store information of input and output queue
233 * @id: index of the context that this structure describes
234 * @state: state of the context
235 * @param_change: indicate encode parameter type
236 * @enc_params: encoding parameters
237 * @dec_if: hooked decoder driver interface
238 * @enc_if: hoooked encoder driver interface
239 * @drv_handle: driver handle for specific decode/encode instance
241 * @picinfo: store picture info after header parsing
242 * @dpb_size: store dpb count after header parsing
243 * @int_cond: variable used by the waitqueue
244 * @int_type: type of the last interrupt
245 * @queue: waitqueue that can be used to wait for this context to
247 * @irq_status: irq status
249 * @ctrl_hdl: handler for v4l2 framework
250 * @decode_work: worker for the decoding
251 * @encode_work: worker for the encoding
252 * @last_decoded_picinfo: pic information get from latest decode
253 * @empty_flush_buf: a fake size-0 capture buffer that indicates flush
255 * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
256 * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
257 * @quantization: enum v4l2_quantization, colorspace quantization
258 * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
259 * @lock: protect variables accessed by V4L2 threads and worker thread such as
262 struct mtk_vcodec_ctx
{
263 enum mtk_instance_type type
;
264 struct mtk_vcodec_dev
*dev
;
265 struct list_head list
;
268 struct v4l2_m2m_ctx
*m2m_ctx
;
269 struct mtk_q_data q_data
[2];
271 enum mtk_instance_state state
;
272 enum mtk_encode_param param_change
;
273 struct mtk_enc_params enc_params
;
275 const struct vdec_common_if
*dec_if
;
276 const struct venc_common_if
*enc_if
;
279 struct vdec_pic_info picinfo
;
284 wait_queue_head_t queue
;
285 unsigned int irq_status
;
287 struct v4l2_ctrl_handler ctrl_hdl
;
288 struct work_struct decode_work
;
289 struct work_struct encode_work
;
290 struct vdec_pic_info last_decoded_picinfo
;
291 struct mtk_video_dec_buf
*empty_flush_buf
;
293 enum v4l2_colorspace colorspace
;
294 enum v4l2_ycbcr_encoding ycbcr_enc
;
295 enum v4l2_quantization quantization
;
296 enum v4l2_xfer_func xfer_func
;
298 int decoded_frame_cnt
;
309 * struct mtk_vcodec_enc_pdata - compatible data for each IC
311 * @chip: chip this encoder is compatible with
313 * @uses_ext: whether the encoder uses the extended firmware messaging format
314 * @has_lt_irq: whether the encoder uses the LT irq
315 * @min_birate: minimum supported encoding bitrate
316 * @max_bitrate: maximum supported encoding bitrate
317 * @capture_formats: array of supported capture formats
318 * @num_capture_formats: number of entries in capture_formats
319 * @output_formats: array of supported output formats
320 * @num_output_formats: number of entries in output_formats
322 struct mtk_vcodec_enc_pdata
{
327 unsigned long min_bitrate
;
328 unsigned long max_bitrate
;
329 const struct mtk_video_fmt
*capture_formats
;
330 size_t num_capture_formats
;
331 const struct mtk_video_fmt
*output_formats
;
332 size_t num_output_formats
;
335 #define MTK_ENC_CTX_IS_EXT(ctx) ((ctx)->dev->venc_pdata->uses_ext)
338 * struct mtk_vcodec_dev - driver data
339 * @v4l2_dev: V4L2 device to register video devices for.
340 * @vfd_dec: Video device for decoder
341 * @vfd_enc: Video device for encoder.
343 * @m2m_dev_dec: m2m device for decoder
344 * @m2m_dev_enc: m2m device for encoder.
345 * @plat_dev: platform device
346 * @ctx_list: list of struct mtk_vcodec_ctx
347 * @irqlock: protect data access by irq handler and work thread
348 * @curr_ctx: The context that is waiting for codec hardware
350 * @reg_base: Mapped address of MTK Vcodec registers.
352 * @fw_handler: used to communicate with the firmware.
353 * @id_counter: used to identify current opened instance
355 * @encode_workqueue: encode work queue
357 * @int_cond: used to identify interrupt condition happen
358 * @int_type: used to identify what kind of interrupt condition happen
359 * @dev_mutex: video_device lock
360 * @queue: waitqueue for waiting for completion of device commands
362 * @dec_irq: decoder irq resource
363 * @enc_irq: h264 encoder irq resource
364 * @enc_lt_irq: vp8 encoder irq resource
366 * @dec_mutex: decoder hardware lock
367 * @enc_mutex: encoder hardware lock.
369 * @pm: power management control
370 * @dec_capability: used to identify decode capability, ex: 4k
371 * @enc_capability: used to identify encode capability
373 struct mtk_vcodec_dev
{
374 struct v4l2_device v4l2_dev
;
375 struct video_device
*vfd_dec
;
376 struct video_device
*vfd_enc
;
378 struct v4l2_m2m_dev
*m2m_dev_dec
;
379 struct v4l2_m2m_dev
*m2m_dev_enc
;
380 struct platform_device
*plat_dev
;
381 struct list_head ctx_list
;
383 struct mtk_vcodec_ctx
*curr_ctx
;
384 void __iomem
*reg_base
[NUM_MAX_VCODEC_REG_BASE
];
385 const struct mtk_vcodec_enc_pdata
*venc_pdata
;
387 struct mtk_vcodec_fw
*fw_handler
;
389 unsigned long id_counter
;
391 struct workqueue_struct
*decode_workqueue
;
392 struct workqueue_struct
*encode_workqueue
;
395 struct mutex dev_mutex
;
396 wait_queue_head_t queue
;
402 struct mutex dec_mutex
;
403 struct mutex enc_mutex
;
405 struct mtk_vcodec_pm pm
;
406 unsigned int dec_capability
;
407 unsigned int enc_capability
;
410 static inline struct mtk_vcodec_ctx
*fh_to_ctx(struct v4l2_fh
*fh
)
412 return container_of(fh
, struct mtk_vcodec_ctx
, fh
);
415 static inline struct mtk_vcodec_ctx
*ctrl_to_ctx(struct v4l2_ctrl
*ctrl
)
417 return container_of(ctrl
->handler
, struct mtk_vcodec_ctx
, ctrl_hdl
);
420 #endif /* _MTK_VCODEC_DRV_H_ */