Linux 5.2-rc4
[sfrench/cifs-2.6.git] / drivers / media / platform / mtk-vcodec / mtk_vcodec_drv.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
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/videobuf2-core.h>
17 #include "mtk_vcodec_util.h"
18
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"
23
24 #define MTK_VCODEC_MAX_PLANES   3
25 #define MTK_V4L2_BENCHMARK      0
26 #define WAIT_INTR_TIMEOUT_MS    1000
27
28 /**
29  * enum mtk_hw_reg_idx - MTK hw register base index
30  */
31 enum mtk_hw_reg_idx {
32         VDEC_SYS,
33         VDEC_MISC,
34         VDEC_LD,
35         VDEC_TOP,
36         VDEC_CM,
37         VDEC_AD,
38         VDEC_AV,
39         VDEC_PP,
40         VDEC_HWD,
41         VDEC_HWQ,
42         VDEC_HWB,
43         VDEC_HWG,
44         NUM_MAX_VDEC_REG_BASE,
45         /* h264 encoder */
46         VENC_SYS = NUM_MAX_VDEC_REG_BASE,
47         /* vp8 encoder */
48         VENC_LT_SYS,
49         NUM_MAX_VCODEC_REG_BASE
50 };
51
52 /**
53  * enum mtk_instance_type - The type of an MTK Vcodec instance.
54  */
55 enum mtk_instance_type {
56         MTK_INST_DECODER                = 0,
57         MTK_INST_ENCODER                = 1,
58 };
59
60 /**
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
68  */
69 enum mtk_instance_state {
70         MTK_STATE_FREE = 0,
71         MTK_STATE_INIT = 1,
72         MTK_STATE_HEADER = 2,
73         MTK_STATE_FLUSH = 3,
74         MTK_STATE_ABORT = 4,
75 };
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
89 enum mtk_fmt_type {
90         MTK_FMT_DEC = 0,
91         MTK_FMT_ENC = 1,
92         MTK_FMT_FRAME = 2,
93 };
94
95 /**
96  * struct mtk_video_fmt - Structure used to store information about pixelformats
97  */
98 struct mtk_video_fmt {
99         u32     fourcc;
100         enum mtk_fmt_type       type;
101         u32     num_planes;
102 };
103
104 /**
105  * struct mtk_codec_framesizes - Structure used to store information about
106  *                                                      framesizes
107  */
108 struct mtk_codec_framesizes {
109         u32     fourcc;
110         struct  v4l2_frmsize_stepwise   stepwise;
111 };
112
113 /**
114  * struct mtk_q_type - Type of queue
115  */
116 enum mtk_q_type {
117         MTK_Q_DATA_SRC = 0,
118         MTK_Q_DATA_DST = 1,
119 };
120
121 /**
122  * struct mtk_q_data - Structure used to store information about queue
123  */
124 struct mtk_q_data {
125         unsigned int    visible_width;
126         unsigned int    visible_height;
127         unsigned int    coded_width;
128         unsigned int    coded_height;
129         enum v4l2_field field;
130         unsigned int    bytesperline[MTK_VCODEC_MAX_PLANES];
131         unsigned int    sizeimage[MTK_VCODEC_MAX_PLANES];
132         struct mtk_video_fmt    *fmt;
133 };
134
135 /**
136  * struct mtk_enc_params - General encoding parameters
137  * @bitrate: target bitrate in bits per second
138  * @num_b_frame: number of b frames between p-frame
139  * @rc_frame: frame based rate control
140  * @rc_mb: macroblock based rate control
141  * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
142  *                with the first frame
143  * @intra_period: I frame period
144  * @gop_size: group of picture size, it's used as the intra frame period
145  * @framerate_num: frame rate numerator. ex: framerate_num=30 and
146  *                 framerate_denom=1 means FPS is 30
147  * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
148  *                   framerate_denom=1 means FPS is 30
149  * @h264_max_qp: Max value for H.264 quantization parameter
150  * @h264_profile: V4L2 defined H.264 profile
151  * @h264_level: V4L2 defined H.264 level
152  * @force_intra: force/insert intra frame
153  */
154 struct mtk_enc_params {
155         unsigned int    bitrate;
156         unsigned int    num_b_frame;
157         unsigned int    rc_frame;
158         unsigned int    rc_mb;
159         unsigned int    seq_hdr_mode;
160         unsigned int    intra_period;
161         unsigned int    gop_size;
162         unsigned int    framerate_num;
163         unsigned int    framerate_denom;
164         unsigned int    h264_max_qp;
165         unsigned int    h264_profile;
166         unsigned int    h264_level;
167         unsigned int    force_intra;
168 };
169
170 /**
171  * struct mtk_vcodec_clk_info - Structure used to store clock name
172  */
173 struct mtk_vcodec_clk_info {
174         const char      *clk_name;
175         struct clk      *vcodec_clk;
176 };
177
178 /**
179  * struct mtk_vcodec_clk - Structure used to store vcodec clock information
180  */
181 struct mtk_vcodec_clk {
182         struct mtk_vcodec_clk_info      *clk_info;
183         int     clk_num;
184 };
185
186 /**
187  * struct mtk_vcodec_pm - Power management data structure
188  */
189 struct mtk_vcodec_pm {
190         struct mtk_vcodec_clk   vdec_clk;
191         struct device   *larbvdec;
192
193         struct mtk_vcodec_clk   venc_clk;
194         struct device   *larbvenc;
195         struct device   *larbvenclt;
196         struct device   *dev;
197         struct mtk_vcodec_dev   *mtkdev;
198 };
199
200 /**
201  * struct vdec_pic_info  - picture size information
202  * @pic_w: picture width
203  * @pic_h: picture height
204  * @buf_w: picture buffer width (64 aligned up from pic_w)
205  * @buf_h: picture buffer heiht (64 aligned up from pic_h)
206  * @fb_sz: bitstream size of each plane
207  * E.g. suppose picture size is 176x144,
208  *      buffer size will be aligned to 176x160.
209  * @cap_fourcc: fourcc number(may changed when resolution change)
210  * @reserved: align struct to 64-bit in order to adjust 32-bit and 64-bit os.
211  */
212 struct vdec_pic_info {
213         unsigned int pic_w;
214         unsigned int pic_h;
215         unsigned int buf_w;
216         unsigned int buf_h;
217         unsigned int fb_sz[VIDEO_MAX_PLANES];
218         unsigned int cap_fourcc;
219         unsigned int reserved;
220 };
221
222 /**
223  * struct mtk_vcodec_ctx - Context (instance) private data.
224  *
225  * @type: type of the instance - decoder or encoder
226  * @dev: pointer to the mtk_vcodec_dev of the device
227  * @list: link to ctx_list of mtk_vcodec_dev
228  * @fh: struct v4l2_fh
229  * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
230  * @q_data: store information of input and output queue
231  *          of the context
232  * @id: index of the context that this structure describes
233  * @state: state of the context
234  * @param_change: indicate encode parameter type
235  * @enc_params: encoding parameters
236  * @dec_if: hooked decoder driver interface
237  * @enc_if: hoooked encoder driver interface
238  * @drv_handle: driver handle for specific decode/encode instance
239  *
240  * @picinfo: store picture info after header parsing
241  * @dpb_size: store dpb count after header parsing
242  * @int_cond: variable used by the waitqueue
243  * @int_type: type of the last interrupt
244  * @queue: waitqueue that can be used to wait for this context to
245  *         finish
246  * @irq_status: irq status
247  *
248  * @ctrl_hdl: handler for v4l2 framework
249  * @decode_work: worker for the decoding
250  * @encode_work: worker for the encoding
251  * @last_decoded_picinfo: pic information get from latest decode
252  * @empty_flush_buf: a fake size-0 capture buffer that indicates flush
253  *
254  * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
255  * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
256  * @quantization: enum v4l2_quantization, colorspace quantization
257  * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
258  * @lock: protect variables accessed by V4L2 threads and worker thread such as
259  *        mtk_video_dec_buf.
260  */
261 struct mtk_vcodec_ctx {
262         enum mtk_instance_type type;
263         struct mtk_vcodec_dev *dev;
264         struct list_head list;
265
266         struct v4l2_fh fh;
267         struct v4l2_m2m_ctx *m2m_ctx;
268         struct mtk_q_data q_data[2];
269         int id;
270         enum mtk_instance_state state;
271         enum mtk_encode_param param_change;
272         struct mtk_enc_params enc_params;
273
274         const struct vdec_common_if *dec_if;
275         const struct venc_common_if *enc_if;
276         unsigned long drv_handle;
277
278         struct vdec_pic_info picinfo;
279         int dpb_size;
280
281         int int_cond;
282         int int_type;
283         wait_queue_head_t queue;
284         unsigned int irq_status;
285
286         struct v4l2_ctrl_handler ctrl_hdl;
287         struct work_struct decode_work;
288         struct work_struct encode_work;
289         struct vdec_pic_info last_decoded_picinfo;
290         struct mtk_video_dec_buf *empty_flush_buf;
291
292         enum v4l2_colorspace colorspace;
293         enum v4l2_ycbcr_encoding ycbcr_enc;
294         enum v4l2_quantization quantization;
295         enum v4l2_xfer_func xfer_func;
296
297         int decoded_frame_cnt;
298         struct mutex lock;
299
300 };
301
302 /**
303  * struct mtk_vcodec_dev - driver data
304  * @v4l2_dev: V4L2 device to register video devices for.
305  * @vfd_dec: Video device for decoder
306  * @vfd_enc: Video device for encoder.
307  *
308  * @m2m_dev_dec: m2m device for decoder
309  * @m2m_dev_enc: m2m device for encoder.
310  * @plat_dev: platform device
311  * @vpu_plat_dev: mtk vpu platform device
312  * @ctx_list: list of struct mtk_vcodec_ctx
313  * @irqlock: protect data access by irq handler and work thread
314  * @curr_ctx: The context that is waiting for codec hardware
315  *
316  * @reg_base: Mapped address of MTK Vcodec registers.
317  *
318  * @id_counter: used to identify current opened instance
319  *
320  * @encode_workqueue: encode work queue
321  *
322  * @int_cond: used to identify interrupt condition happen
323  * @int_type: used to identify what kind of interrupt condition happen
324  * @dev_mutex: video_device lock
325  * @queue: waitqueue for waiting for completion of device commands
326  *
327  * @dec_irq: decoder irq resource
328  * @enc_irq: h264 encoder irq resource
329  * @enc_lt_irq: vp8 encoder irq resource
330  *
331  * @dec_mutex: decoder hardware lock
332  * @enc_mutex: encoder hardware lock.
333  *
334  * @pm: power management control
335  * @dec_capability: used to identify decode capability, ex: 4k
336  * @enc_capability: used to identify encode capability
337  */
338 struct mtk_vcodec_dev {
339         struct v4l2_device v4l2_dev;
340         struct video_device *vfd_dec;
341         struct video_device *vfd_enc;
342
343         struct v4l2_m2m_dev *m2m_dev_dec;
344         struct v4l2_m2m_dev *m2m_dev_enc;
345         struct platform_device *plat_dev;
346         struct platform_device *vpu_plat_dev;
347         struct list_head ctx_list;
348         spinlock_t irqlock;
349         struct mtk_vcodec_ctx *curr_ctx;
350         void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
351
352         unsigned long id_counter;
353
354         struct workqueue_struct *decode_workqueue;
355         struct workqueue_struct *encode_workqueue;
356         int int_cond;
357         int int_type;
358         struct mutex dev_mutex;
359         wait_queue_head_t queue;
360
361         int dec_irq;
362         int enc_irq;
363         int enc_lt_irq;
364
365         struct mutex dec_mutex;
366         struct mutex enc_mutex;
367
368         struct mtk_vcodec_pm pm;
369         unsigned int dec_capability;
370         unsigned int enc_capability;
371 };
372
373 static inline struct mtk_vcodec_ctx *fh_to_ctx(struct v4l2_fh *fh)
374 {
375         return container_of(fh, struct mtk_vcodec_ctx, fh);
376 }
377
378 static inline struct mtk_vcodec_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
379 {
380         return container_of(ctrl->handler, struct mtk_vcodec_ctx, ctrl_hdl);
381 }
382
383 #endif /* _MTK_VCODEC_DRV_H_ */