2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: PC Chen <pc.chen@mediatek.com>
4 * Tiffany Lin <tiffany.lin@mediatek.com>
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.
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.
16 #ifndef _MTK_VCODEC_DRV_H_
17 #define _MTK_VCODEC_DRV_H_
19 #include <linux/platform_device.h>
20 #include <linux/videodev2.h>
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-ioctl.h>
24 #include <media/videobuf2-core.h>
26 #include "mtk_vcodec_util.h"
28 #define MTK_VCODEC_DRV_NAME "mtk_vcodec_drv"
29 #define MTK_VCODEC_ENC_NAME "mtk-vcodec-enc"
30 #define MTK_PLATFORM_STR "platform:mt8173"
33 #define MTK_VCODEC_MAX_PLANES 3
34 #define MTK_V4L2_BENCHMARK 0
35 #define WAIT_INTR_TIMEOUT_MS 1000
38 * enum mtk_hw_reg_idx - MTK hw register base index
53 NUM_MAX_VDEC_REG_BASE
,
55 VENC_SYS
= NUM_MAX_VDEC_REG_BASE
,
58 NUM_MAX_VCODEC_REG_BASE
62 * enum mtk_instance_type - The type of an MTK Vcodec instance.
64 enum mtk_instance_type
{
70 * enum mtk_instance_state - The state of an MTK Vcodec instance.
71 * @MTK_STATE_FREE - default state when instance is created
72 * @MTK_STATE_INIT - vcodec instance is initialized
73 * @MTK_STATE_HEADER - vdec had sps/pps header parsed or venc
74 * had sps/pps header encoded
75 * @MTK_STATE_FLUSH - vdec is flushing. Only used by decoder
76 * @MTK_STATE_ABORT - vcodec should be aborted
78 enum mtk_instance_state
{
87 * struct mtk_encode_param - General encoding parameters type
89 enum mtk_encode_param
{
90 MTK_ENCODE_PARAM_NONE
= 0,
91 MTK_ENCODE_PARAM_BITRATE
= (1 << 0),
92 MTK_ENCODE_PARAM_FRAMERATE
= (1 << 1),
93 MTK_ENCODE_PARAM_INTRA_PERIOD
= (1 << 2),
94 MTK_ENCODE_PARAM_FORCE_INTRA
= (1 << 3),
95 MTK_ENCODE_PARAM_GOP_SIZE
= (1 << 4),
105 * struct mtk_video_fmt - Structure used to store information about pixelformats
107 struct mtk_video_fmt
{
109 enum mtk_fmt_type type
;
114 * struct mtk_codec_framesizes - Structure used to store information about
117 struct mtk_codec_framesizes
{
119 struct v4l2_frmsize_stepwise stepwise
;
123 * struct mtk_q_type - Type of queue
131 * struct mtk_q_data - Structure used to store information about queue
134 unsigned int visible_width
;
135 unsigned int visible_height
;
136 unsigned int coded_width
;
137 unsigned int coded_height
;
138 enum v4l2_field field
;
139 unsigned int bytesperline
[MTK_VCODEC_MAX_PLANES
];
140 unsigned int sizeimage
[MTK_VCODEC_MAX_PLANES
];
141 struct mtk_video_fmt
*fmt
;
145 * struct mtk_enc_params - General encoding parameters
146 * @bitrate: target bitrate in bits per second
147 * @num_b_frame: number of b frames between p-frame
148 * @rc_frame: frame based rate control
149 * @rc_mb: macroblock based rate control
150 * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
151 * with the first frame
152 * @intra_period: I frame period
153 * @gop_size: group of picture size, it's used as the intra frame period
154 * @framerate_num: frame rate numerator. ex: framerate_num=30 and
155 * framerate_denom=1 menas FPS is 30
156 * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
157 * framerate_denom=1 menas FPS is 30
158 * @h264_max_qp: Max value for H.264 quantization parameter
159 * @h264_profile: V4L2 defined H.264 profile
160 * @h264_level: V4L2 defined H.264 level
161 * @force_intra: force/insert intra frame
163 struct mtk_enc_params
{
164 unsigned int bitrate
;
165 unsigned int num_b_frame
;
166 unsigned int rc_frame
;
168 unsigned int seq_hdr_mode
;
169 unsigned int intra_period
;
170 unsigned int gop_size
;
171 unsigned int framerate_num
;
172 unsigned int framerate_denom
;
173 unsigned int h264_max_qp
;
174 unsigned int h264_profile
;
175 unsigned int h264_level
;
176 unsigned int force_intra
;
180 * struct mtk_vcodec_pm - Power management data structure
182 struct mtk_vcodec_pm
{
183 struct clk
*vcodecpll
;
184 struct clk
*univpll_d2
;
185 struct clk
*clk_cci400_sel
;
187 struct clk
*vdec_sel
;
188 struct clk
*vencpll_d2
;
189 struct clk
*venc_sel
;
190 struct clk
*univpll1_d2
;
191 struct clk
*venc_lt_sel
;
192 struct device
*larbvdec
;
193 struct device
*larbvenc
;
194 struct device
*larbvenclt
;
196 struct mtk_vcodec_dev
*mtkdev
;
200 * struct mtk_vcodec_ctx - Context (instance) private data.
202 * @type: type of the instance - decoder or encoder
203 * @dev: pointer to the mtk_vcodec_dev of the device
204 * @list: link to ctx_list of mtk_vcodec_dev
205 * @fh: struct v4l2_fh
206 * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
207 * @q_data: store information of input and output queue
209 * @id: index of the context that this structure describes
210 * @state: state of the context
211 * @param_change: indicate encode parameter type
212 * @enc_params: encoding parameters
213 * @enc_if: hoooked encoder driver interface
214 * @drv_handle: driver handle for specific decode/encode instance
216 * @int_cond: variable used by the waitqueue
217 * @int_type: type of the last interrupt
218 * @queue: waitqueue that can be used to wait for this context to
220 * @irq_status: irq status
222 * @ctrl_hdl: handler for v4l2 framework
223 * @encode_work: worker for the encoding
225 * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
226 * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
227 * @quantization: enum v4l2_quantization, colorspace quantization
228 * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
230 struct mtk_vcodec_ctx
{
231 enum mtk_instance_type type
;
232 struct mtk_vcodec_dev
*dev
;
233 struct list_head list
;
236 struct v4l2_m2m_ctx
*m2m_ctx
;
237 struct mtk_q_data q_data
[2];
239 enum mtk_instance_state state
;
240 enum mtk_encode_param param_change
;
241 struct mtk_enc_params enc_params
;
243 struct venc_common_if
*enc_if
;
244 unsigned long drv_handle
;
248 wait_queue_head_t queue
;
249 unsigned int irq_status
;
251 struct v4l2_ctrl_handler ctrl_hdl
;
252 struct work_struct encode_work
;
254 enum v4l2_colorspace colorspace
;
255 enum v4l2_ycbcr_encoding ycbcr_enc
;
256 enum v4l2_quantization quantization
;
257 enum v4l2_xfer_func xfer_func
;
261 * struct mtk_vcodec_dev - driver data
262 * @v4l2_dev: V4L2 device to register video devices for.
263 * @vfd_enc: Video device for encoder.
265 * @m2m_dev_enc: m2m device for encoder.
266 * @plat_dev: platform device
267 * @vpu_plat_dev: mtk vpu platform device
268 * @ctx_list: list of struct mtk_vcodec_ctx
269 * @irqlock: protect data access by irq handler and work thread
270 * @curr_ctx: The context that is waiting for codec hardware
272 * @reg_base: Mapped address of MTK Vcodec registers.
274 * @id_counter: used to identify current opened instance
275 * @num_instances: counter of active MTK Vcodec instances
277 * @encode_workqueue: encode work queue
279 * @int_cond: used to identify interrupt condition happen
280 * @int_type: used to identify what kind of interrupt condition happen
281 * @dev_mutex: video_device lock
282 * @queue: waitqueue for waiting for completion of device commands
284 * @enc_irq: h264 encoder irq resource
285 * @enc_lt_irq: vp8 encoder irq resource
287 * @enc_mutex: encoder hardware lock.
289 * @pm: power management control
290 * @dec_capability: used to identify decode capability, ex: 4k
291 * @enc_capability: used to identify encode capability
293 struct mtk_vcodec_dev
{
294 struct v4l2_device v4l2_dev
;
295 struct video_device
*vfd_enc
;
297 struct v4l2_m2m_dev
*m2m_dev_enc
;
298 struct platform_device
*plat_dev
;
299 struct platform_device
*vpu_plat_dev
;
300 struct list_head ctx_list
;
302 struct mtk_vcodec_ctx
*curr_ctx
;
303 void __iomem
*reg_base
[NUM_MAX_VCODEC_REG_BASE
];
305 unsigned long id_counter
;
308 struct workqueue_struct
*encode_workqueue
;
312 struct mutex dev_mutex
;
313 wait_queue_head_t queue
;
318 struct mutex enc_mutex
;
320 struct mtk_vcodec_pm pm
;
321 unsigned int dec_capability
;
322 unsigned int enc_capability
;
325 static inline struct mtk_vcodec_ctx
*fh_to_ctx(struct v4l2_fh
*fh
)
327 return container_of(fh
, struct mtk_vcodec_ctx
, fh
);
330 static inline struct mtk_vcodec_ctx
*ctrl_to_ctx(struct v4l2_ctrl
*ctrl
)
332 return container_of(ctrl
->handler
, struct mtk_vcodec_ctx
, ctrl_hdl
);
335 #endif /* _MTK_VCODEC_DRV_H_ */