1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 *         Tiffany Lin <tiffany.lin@mediatek.com>
6 */
7 
8 #ifndef _MTK_VCODEC_DRV_H_
9 #define _MTK_VCODEC_DRV_H_
10 
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/v4l2-mem2mem.h>
17 #include <media/videobuf2-core.h>
18 #include "mtk_vcodec_util.h"
19 
20 #define MTK_VCODEC_DRV_NAME	"mtk_vcodec_drv"
21 #define MTK_VCODEC_DEC_NAME	"mtk-vcodec-dec"
22 #define MTK_VCODEC_ENC_NAME	"mtk-vcodec-enc"
23 #define MTK_PLATFORM_STR	"platform:mt8173"
24 
25 #define MTK_VCODEC_MAX_PLANES	3
26 #define MTK_V4L2_BENCHMARK	0
27 #define WAIT_INTR_TIMEOUT_MS	1000
28 
29 /*
30  * enum mtk_hw_reg_idx - MTK hw register base index
31  */
32 enum mtk_hw_reg_idx {
33 	VDEC_SYS,
34 	VDEC_MISC,
35 	VDEC_LD,
36 	VDEC_TOP,
37 	VDEC_CM,
38 	VDEC_AD,
39 	VDEC_AV,
40 	VDEC_PP,
41 	VDEC_HWD,
42 	VDEC_HWQ,
43 	VDEC_HWB,
44 	VDEC_HWG,
45 	NUM_MAX_VDEC_REG_BASE,
46 	/* h264 encoder */
47 	VENC_SYS = NUM_MAX_VDEC_REG_BASE,
48 	/* vp8 encoder */
49 	VENC_LT_SYS,
50 	NUM_MAX_VCODEC_REG_BASE
51 };
52 
53 /*
54  * enum mtk_instance_type - The type of an MTK Vcodec instance.
55  */
56 enum mtk_instance_type {
57 	MTK_INST_DECODER		= 0,
58 	MTK_INST_ENCODER		= 1,
59 };
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 /*
79  * enum mtk_encode_param - General encoding parameters type
80  */
81 enum mtk_encode_param {
82 	MTK_ENCODE_PARAM_NONE = 0,
83 	MTK_ENCODE_PARAM_BITRATE = (1 << 0),
84 	MTK_ENCODE_PARAM_FRAMERATE = (1 << 1),
85 	MTK_ENCODE_PARAM_INTRA_PERIOD = (1 << 2),
86 	MTK_ENCODE_PARAM_FORCE_INTRA = (1 << 3),
87 	MTK_ENCODE_PARAM_GOP_SIZE = (1 << 4),
88 };
89 
90 enum mtk_fmt_type {
91 	MTK_FMT_DEC = 0,
92 	MTK_FMT_ENC = 1,
93 	MTK_FMT_FRAME = 2,
94 };
95 
96 /*
97  * struct mtk_video_fmt - Structure used to store information about pixelformats
98  */
99 struct mtk_video_fmt {
100 	u32	fourcc;
101 	enum mtk_fmt_type	type;
102 	u32	num_planes;
103 	u32	flags;
104 };
105 
106 /*
107  * struct mtk_codec_framesizes - Structure used to store information about
108  *							framesizes
109  */
110 struct mtk_codec_framesizes {
111 	u32	fourcc;
112 	struct	v4l2_frmsize_stepwise	stepwise;
113 };
114 
115 /*
116  * enum mtk_q_type - Type of queue
117  */
118 enum mtk_q_type {
119 	MTK_Q_DATA_SRC = 0,
120 	MTK_Q_DATA_DST = 1,
121 };
122 
123 /*
124  * struct mtk_q_data - Structure used to store information about queue
125  */
126 struct mtk_q_data {
127 	unsigned int	visible_width;
128 	unsigned int	visible_height;
129 	unsigned int	coded_width;
130 	unsigned int	coded_height;
131 	enum v4l2_field	field;
132 	unsigned int	bytesperline[MTK_VCODEC_MAX_PLANES];
133 	unsigned int	sizeimage[MTK_VCODEC_MAX_PLANES];
134 	const struct mtk_video_fmt	*fmt;
135 };
136 
137 /**
138  * struct mtk_enc_params - General encoding parameters
139  * @bitrate: target bitrate in bits per second
140  * @num_b_frame: number of b frames between p-frame
141  * @rc_frame: frame based rate control
142  * @rc_mb: macroblock based rate control
143  * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
144  *		  with the first frame
145  * @intra_period: I frame period
146  * @gop_size: group of picture size, it's used as the intra frame period
147  * @framerate_num: frame rate numerator. ex: framerate_num=30 and
148  *		   framerate_denom=1 means FPS is 30
149  * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
150  *		     framerate_denom=1 means FPS is 30
151  * @h264_max_qp: Max value for H.264 quantization parameter
152  * @h264_profile: V4L2 defined H.264 profile
153  * @h264_level: V4L2 defined H.264 level
154  * @force_intra: force/insert intra frame
155  */
156 struct mtk_enc_params {
157 	unsigned int	bitrate;
158 	unsigned int	num_b_frame;
159 	unsigned int	rc_frame;
160 	unsigned int	rc_mb;
161 	unsigned int	seq_hdr_mode;
162 	unsigned int	intra_period;
163 	unsigned int	gop_size;
164 	unsigned int	framerate_num;
165 	unsigned int	framerate_denom;
166 	unsigned int	h264_max_qp;
167 	unsigned int	h264_profile;
168 	unsigned int	h264_level;
169 	unsigned int	force_intra;
170 };
171 
172 /*
173  * struct mtk_vcodec_clk_info - Structure used to store clock name
174  */
175 struct mtk_vcodec_clk_info {
176 	const char	*clk_name;
177 	struct clk	*vcodec_clk;
178 };
179 
180 /*
181  * struct mtk_vcodec_clk - Structure used to store vcodec clock information
182  */
183 struct mtk_vcodec_clk {
184 	struct mtk_vcodec_clk_info	*clk_info;
185 	int	clk_num;
186 };
187 
188 /*
189  * struct mtk_vcodec_pm - Power management data structure
190  */
191 struct mtk_vcodec_pm {
192 	struct mtk_vcodec_clk	vdec_clk;
193 	struct device	*larbvdec;
194 
195 	struct mtk_vcodec_clk	venc_clk;
196 	struct device	*larbvenc;
197 	struct device	*dev;
198 	struct mtk_vcodec_dev	*mtkdev;
199 };
200 
201 /**
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.
212  */
213 struct vdec_pic_info {
214 	unsigned int pic_w;
215 	unsigned int pic_h;
216 	unsigned int buf_w;
217 	unsigned int buf_h;
218 	unsigned int fb_sz[VIDEO_MAX_PLANES];
219 	unsigned int cap_fourcc;
220 	unsigned int reserved;
221 };
222 
223 /**
224  * struct mtk_vcodec_ctx - Context (instance) private data.
225  *
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
232  *	    of the context
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
240  *
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
246  *	   finish
247  * @irq_status: irq status
248  *
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. Only
254  *		     to be used with encoder and stateful decoder.
255  * @is_flushing: set to true if flushing is in progress.
256  * @current_codec: current set input codec, in V4L2 pixel format
257  *
258  * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
259  * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
260  * @quantization: enum v4l2_quantization, colorspace quantization
261  * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
262  * @decoded_frame_cnt: number of decoded frames
263  * @lock: protect variables accessed by V4L2 threads and worker thread such as
264  *	  mtk_video_dec_buf.
265  */
266 struct mtk_vcodec_ctx {
267 	enum mtk_instance_type type;
268 	struct mtk_vcodec_dev *dev;
269 	struct list_head list;
270 
271 	struct v4l2_fh fh;
272 	struct v4l2_m2m_ctx *m2m_ctx;
273 	struct mtk_q_data q_data[2];
274 	int id;
275 	enum mtk_instance_state state;
276 	enum mtk_encode_param param_change;
277 	struct mtk_enc_params enc_params;
278 
279 	const struct vdec_common_if *dec_if;
280 	const struct venc_common_if *enc_if;
281 	void *drv_handle;
282 
283 	struct vdec_pic_info picinfo;
284 	int dpb_size;
285 
286 	int int_cond;
287 	int int_type;
288 	wait_queue_head_t queue;
289 	unsigned int irq_status;
290 
291 	struct v4l2_ctrl_handler ctrl_hdl;
292 	struct work_struct decode_work;
293 	struct work_struct encode_work;
294 	struct vdec_pic_info last_decoded_picinfo;
295 	struct v4l2_m2m_buffer empty_flush_buf;
296 	bool is_flushing;
297 
298 	u32 current_codec;
299 
300 	enum v4l2_colorspace colorspace;
301 	enum v4l2_ycbcr_encoding ycbcr_enc;
302 	enum v4l2_quantization quantization;
303 	enum v4l2_xfer_func xfer_func;
304 
305 	int decoded_frame_cnt;
306 	struct mutex lock;
307 
308 };
309 
310 enum mtk_chip {
311 	MTK_MT8173,
312 	MTK_MT8183,
313 	MTK_MT8192,
314 	MTK_MT8195,
315 };
316 
317 /**
318  * struct mtk_vcodec_dec_pdata - compatible data for each IC
319  * @init_vdec_params: init vdec params
320  * @ctrls_setup: init vcodec dec ctrls
321  * @worker: worker to start a decode job
322  * @flush_decoder: function that flushes the decoder
323  *
324  * @vdec_vb2_ops: struct vb2_ops
325  *
326  * @vdec_formats: supported video decoder formats
327  * @num_formats: count of video decoder formats
328  * @default_out_fmt: default output buffer format
329  * @default_cap_fmt: default capture buffer format
330  *
331  * @vdec_framesizes: supported video decoder frame sizes
332  * @num_framesizes: count of video decoder frame sizes
333  *
334  * @chip: chip this decoder is compatible with
335  *
336  * @uses_stateless_api: whether the decoder uses the stateless API with requests
337  */
338 
339 struct mtk_vcodec_dec_pdata {
340 	void (*init_vdec_params)(struct mtk_vcodec_ctx *ctx);
341 	int (*ctrls_setup)(struct mtk_vcodec_ctx *ctx);
342 	void (*worker)(struct work_struct *work);
343 	int (*flush_decoder)(struct mtk_vcodec_ctx *ctx);
344 
345 	struct vb2_ops *vdec_vb2_ops;
346 
347 	const struct mtk_video_fmt *vdec_formats;
348 	const int num_formats;
349 	const struct mtk_video_fmt *default_out_fmt;
350 	const struct mtk_video_fmt *default_cap_fmt;
351 
352 	const struct mtk_codec_framesizes *vdec_framesizes;
353 	const int num_framesizes;
354 
355 	enum mtk_chip chip;
356 
357 	bool uses_stateless_api;
358 };
359 
360 /**
361  * struct mtk_vcodec_enc_pdata - compatible data for each IC
362  *
363  * @chip: chip this encoder is compatible with
364  *
365  * @uses_ext: whether the encoder uses the extended firmware messaging format
366  * @min_bitrate: minimum supported encoding bitrate
367  * @max_bitrate: maximum supported encoding bitrate
368  * @capture_formats: array of supported capture formats
369  * @num_capture_formats: number of entries in capture_formats
370  * @output_formats: array of supported output formats
371  * @num_output_formats: number of entries in output_formats
372  * @core_id: stand for h264 or vp8 encode index
373  */
374 struct mtk_vcodec_enc_pdata {
375 	enum mtk_chip chip;
376 
377 	bool uses_ext;
378 	unsigned long min_bitrate;
379 	unsigned long max_bitrate;
380 	const struct mtk_video_fmt *capture_formats;
381 	size_t num_capture_formats;
382 	const struct mtk_video_fmt *output_formats;
383 	size_t num_output_formats;
384 	int core_id;
385 };
386 
387 #define MTK_ENC_CTX_IS_EXT(ctx) ((ctx)->dev->venc_pdata->uses_ext)
388 
389 /**
390  * struct mtk_vcodec_dev - driver data
391  * @v4l2_dev: V4L2 device to register video devices for.
392  * @vfd_dec: Video device for decoder
393  * @mdev_dec: Media device for decoder
394  * @vfd_enc: Video device for encoder.
395  *
396  * @m2m_dev_dec: m2m device for decoder
397  * @m2m_dev_enc: m2m device for encoder.
398  * @plat_dev: platform device
399  * @ctx_list: list of struct mtk_vcodec_ctx
400  * @irqlock: protect data access by irq handler and work thread
401  * @curr_ctx: The context that is waiting for codec hardware
402  *
403  * @reg_base: Mapped address of MTK Vcodec registers.
404  * @vdec_pdata: decoder IC-specific data
405  * @venc_pdata: encoder IC-specific data
406  *
407  * @fw_handler: used to communicate with the firmware.
408  * @id_counter: used to identify current opened instance
409  *
410  * @decode_workqueue: decode work queue
411  * @encode_workqueue: encode work queue
412  *
413  * @int_cond: used to identify interrupt condition happen
414  * @int_type: used to identify what kind of interrupt condition happen
415  * @dev_mutex: video_device lock
416  * @queue: waitqueue for waiting for completion of device commands
417  *
418  * @dec_irq: decoder irq resource
419  * @enc_irq: h264 encoder irq resource
420  *
421  * @dec_mutex: decoder hardware lock
422  * @enc_mutex: encoder hardware lock.
423  *
424  * @pm: power management control
425  * @dec_capability: used to identify decode capability, ex: 4k
426  * @enc_capability: used to identify encode capability
427  */
428 struct mtk_vcodec_dev {
429 	struct v4l2_device v4l2_dev;
430 	struct video_device *vfd_dec;
431 	struct media_device mdev_dec;
432 	struct video_device *vfd_enc;
433 
434 	struct v4l2_m2m_dev *m2m_dev_dec;
435 	struct v4l2_m2m_dev *m2m_dev_enc;
436 	struct platform_device *plat_dev;
437 	struct list_head ctx_list;
438 	spinlock_t irqlock;
439 	struct mtk_vcodec_ctx *curr_ctx;
440 	void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
441 	const struct mtk_vcodec_dec_pdata *vdec_pdata;
442 	const struct mtk_vcodec_enc_pdata *venc_pdata;
443 
444 	struct mtk_vcodec_fw *fw_handler;
445 
446 	unsigned long id_counter;
447 
448 	struct workqueue_struct *decode_workqueue;
449 	struct workqueue_struct *encode_workqueue;
450 	int int_cond;
451 	int int_type;
452 	struct mutex dev_mutex;
453 	wait_queue_head_t queue;
454 
455 	int dec_irq;
456 	int enc_irq;
457 
458 	struct mutex dec_mutex;
459 	struct mutex enc_mutex;
460 
461 	struct mtk_vcodec_pm pm;
462 	unsigned int dec_capability;
463 	unsigned int enc_capability;
464 };
465 
fh_to_ctx(struct v4l2_fh * fh)466 static inline struct mtk_vcodec_ctx *fh_to_ctx(struct v4l2_fh *fh)
467 {
468 	return container_of(fh, struct mtk_vcodec_ctx, fh);
469 }
470 
ctrl_to_ctx(struct v4l2_ctrl * ctrl)471 static inline struct mtk_vcodec_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
472 {
473 	return container_of(ctrl->handler, struct mtk_vcodec_ctx, ctrl_hdl);
474 }
475 
476 #endif /* _MTK_VCODEC_DRV_H_ */
477