summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/aml_vcodec_drv.h (plain)
blob: eff017fafb537ce767d291f50859cd49c86eb7e5
1/*
2* Copyright (C) 2017 Amlogic, Inc. All rights reserved.
3*
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful, but WITHOUT
10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12* more details.
13*
14* You should have received a copy of the GNU General Public License along
15* with this program; if not, write to the Free Software Foundation, Inc.,
16* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*
18* Description:
19*/
20#ifndef _AML_VCODEC_DRV_H_
21#define _AML_VCODEC_DRV_H_
22
23#include <linux/platform_device.h>
24#include <linux/videodev2.h>
25#include <media/v4l2-ctrls.h>
26#include <media/v4l2-device.h>
27#include <media/v4l2-ioctl.h>
28#include <media/videobuf2-core.h>
29#include <linux/amlogic/media/vfm/vframe.h>
30#include "aml_vcodec_util.h"
31
32#define AML_VCODEC_DRV_NAME "aml_vcodec_drv"
33#define AML_VCODEC_DEC_NAME "aml-vcodec-dec"
34#define AML_VCODEC_ENC_NAME "aml-vcodec-enc"
35#define AML_PLATFORM_STR "platform:amlogic"
36
37#define AML_VCODEC_MAX_PLANES 3
38#define AML_V4L2_BENCHMARK 0
39#define WAIT_INTR_TIMEOUT_MS 1000
40
41/* codec types of get/set parms. */
42#define V4L2_CONFIG_PARM_ENCODE (0)
43#define V4L2_CONFIG_PARM_DECODE (1)
44
45/* types of decode parms. */
46#define V4L2_CONFIG_PARM_DECODE_CFGINFO (1 << 0)
47#define V4L2_CONFIG_PARM_DECODE_PSINFO (1 << 1)
48#define V4L2_CONFIG_PARM_DECODE_HDRINFO (1 << 2)
49#define V4L2_CONFIG_PARM_DECODE_CNTINFO (1 << 3)
50
51/* amlogic event define. */
52/* #define V4L2_EVENT_SRC_CH_RESOLUTION (1 << 0) */
53#define V4L2_EVENT_SRC_CH_HDRINFO (1 << 1)
54#define V4L2_EVENT_SRC_CH_PSINFO (1 << 2)
55#define V4L2_EVENT_SRC_CH_CNTINFO (1 << 3)
56
57/* exception handing */
58#define V4L2_EVENT_REQUEST_RESET (1 << 8)
59#define V4L2_EVENT_REQUEST_EXIT (1 << 9)
60
61
62
63/**
64 * enum aml_hw_reg_idx - AML hw register base index
65 */
66enum aml_hw_reg_idx {
67 VDEC_SYS,
68 VDEC_MISC,
69 VDEC_LD,
70 VDEC_TOP,
71 VDEC_CM,
72 VDEC_AD,
73 VDEC_AV,
74 VDEC_PP,
75 VDEC_HWD,
76 VDEC_HWQ,
77 VDEC_HWB,
78 VDEC_HWG,
79 NUM_MAX_VDEC_REG_BASE,
80 /* h264 encoder */
81 VENC_SYS = NUM_MAX_VDEC_REG_BASE,
82 /* vp8 encoder */
83 VENC_LT_SYS,
84 NUM_MAX_VCODEC_REG_BASE
85};
86
87/**
88 * enum aml_instance_type - The type of an AML Vcodec instance.
89 */
90enum aml_instance_type {
91 AML_INST_DECODER = 0,
92 AML_INST_ENCODER = 1,
93};
94
95/**
96 * enum aml_instance_state - The state of an AML Vcodec instance.
97 * @AML_STATE_IDLE - default state when instance is created
98 * @AML_STATE_INIT - vcodec instance is initialized
99 * @AML_STATE_PROBE - vdec/venc had sps/pps header parsed/encoded
100 * @AML_STATE_ACTIVE - vdec is ready for work.
101 * @AML_STATE_FLUSHING - vdec is flushing. Only used by decoder
102 * @AML_STATE_FLUSHED - decoder has transacted the last frame.
103 * @AML_STATE_RESET - decoder has be reset after flush.
104 * @AML_STATE_ABORT - vcodec should be aborted
105 */
106enum aml_instance_state {
107 AML_STATE_IDLE,
108 AML_STATE_INIT,
109 AML_STATE_PROBE,
110 AML_STATE_READY,
111 AML_STATE_ACTIVE,
112 AML_STATE_FLUSHING,
113 AML_STATE_FLUSHED,
114 AML_STATE_RESET,
115 AML_STATE_ABORT,
116};
117
118/**
119 * struct aml_encode_param - General encoding parameters type
120 */
121enum aml_encode_param {
122 AML_ENCODE_PARAM_NONE = 0,
123 AML_ENCODE_PARAM_BITRATE = (1 << 0),
124 AML_ENCODE_PARAM_FRAMERATE = (1 << 1),
125 AML_ENCODE_PARAM_INTRA_PERIOD = (1 << 2),
126 AML_ENCODE_PARAM_FORCE_INTRA = (1 << 3),
127 AML_ENCODE_PARAM_GOP_SIZE = (1 << 4),
128};
129
130enum aml_fmt_type {
131 AML_FMT_DEC = 0,
132 AML_FMT_ENC = 1,
133 AML_FMT_FRAME = 2,
134};
135
136/**
137 * struct aml_video_fmt - Structure used to store information about pixelformats
138 */
139struct aml_video_fmt {
140 u32 fourcc;
141 enum aml_fmt_type type;
142 u32 num_planes;
143};
144
145/**
146 * struct aml_codec_framesizes - Structure used to store information about
147 * framesizes
148 */
149struct aml_codec_framesizes {
150 u32 fourcc;
151 struct v4l2_frmsize_stepwise stepwise;
152};
153
154/**
155 * struct aml_q_type - Type of queue
156 */
157enum aml_q_type {
158 AML_Q_DATA_SRC = 0,
159 AML_Q_DATA_DST = 1,
160};
161
162
163/**
164 * struct aml_q_data - Structure used to store information about queue
165 */
166struct aml_q_data {
167 unsigned int visible_width;
168 unsigned int visible_height;
169 unsigned int coded_width;
170 unsigned int coded_height;
171 enum v4l2_field field;
172 unsigned int bytesperline[AML_VCODEC_MAX_PLANES];
173 unsigned int sizeimage[AML_VCODEC_MAX_PLANES];
174 struct aml_video_fmt *fmt;
175 bool resolution_changed;
176};
177
178/**
179 * struct aml_enc_params - General encoding parameters
180 * @bitrate: target bitrate in bits per second
181 * @num_b_frame: number of b frames between p-frame
182 * @rc_frame: frame based rate control
183 * @rc_mb: macroblock based rate control
184 * @seq_hdr_mode: H.264 sequence header is encoded separately or joined
185 * with the first frame
186 * @intra_period: I frame period
187 * @gop_size: group of picture size, it's used as the intra frame period
188 * @framerate_num: frame rate numerator. ex: framerate_num=30 and
189 * framerate_denom=1 menas FPS is 30
190 * @framerate_denom: frame rate denominator. ex: framerate_num=30 and
191 * framerate_denom=1 menas FPS is 30
192 * @h264_max_qp: Max value for H.264 quantization parameter
193 * @h264_profile: V4L2 defined H.264 profile
194 * @h264_level: V4L2 defined H.264 level
195 * @force_intra: force/insert intra frame
196 */
197struct aml_enc_params {
198 unsigned int bitrate;
199 unsigned int num_b_frame;
200 unsigned int rc_frame;
201 unsigned int rc_mb;
202 unsigned int seq_hdr_mode;
203 unsigned int intra_period;
204 unsigned int gop_size;
205 unsigned int framerate_num;
206 unsigned int framerate_denom;
207 unsigned int h264_max_qp;
208 unsigned int h264_profile;
209 unsigned int h264_level;
210 unsigned int force_intra;
211};
212
213/**
214 * struct aml_vcodec_pm - Power management data structure
215 */
216struct aml_vcodec_pm {
217 struct clk *vdec_bus_clk_src;
218 struct clk *vencpll;
219
220 struct clk *vcodecpll;
221 struct clk *univpll_d2;
222 struct clk *clk_cci400_sel;
223 struct clk *vdecpll;
224 struct clk *vdec_sel;
225 struct clk *vencpll_d2;
226 struct clk *venc_sel;
227 struct clk *univpll1_d2;
228 struct clk *venc_lt_sel;
229 struct device *larbvdec;
230 struct device *larbvenc;
231 struct device *larbvenclt;
232 struct device *dev;
233 struct aml_vcodec_dev *amldev;
234};
235
236/**
237 * struct vdec_pic_info - picture size information
238 * @visible_width: picture width
239 * @visible_height: picture height
240 * @coded_width: picture buffer width (64 aligned up from pic_w)
241 * @coded_height: picture buffer heiht (64 aligned up from pic_h)
242 * @y_bs_sz: Y bitstream size
243 * @c_bs_sz: CbCr bitstream size
244 * @y_len_sz: additional size required to store decompress information for y
245 * plane
246 * @c_len_sz: additional size required to store decompress information for cbcr
247 * plane
248 * E.g. suppose picture size is 176x144,
249 * buffer size will be aligned to 176x160.
250 */
251struct vdec_pic_info {
252 unsigned int visible_width;
253 unsigned int visible_height;
254 unsigned int coded_width;
255 unsigned int coded_height;
256 unsigned int y_bs_sz;
257 unsigned int c_bs_sz;
258 unsigned int y_len_sz;
259 unsigned int c_len_sz;
260 int profile_idc;
261};
262
263struct aml_vdec_cfg_infos {
264 u32 double_write_mode;
265 u32 init_width;
266 u32 init_height;
267 u32 ref_buf_margin;
268 u32 canvas_mem_mode;
269 u32 canvas_mem_endian;
270};
271
272struct aml_vdec_hdr_infos {
273 /*
274 * bit 29 : present_flag
275 * bit 28-26: video_format "component", "PAL", "NTSC", "SECAM", "MAC", "unspecified"
276 * bit 25 : range "limited", "full_range"
277 * bit 24 : color_description_present_flag
278 * bit 23-16: color_primaries "unknown", "bt709", "undef", "bt601",
279 * "bt470m", "bt470bg", "smpte170m", "smpte240m", "film", "bt2020"
280 * bit 15-8 : transfer_characteristic unknown", "bt709", "undef", "bt601",
281 * "bt470m", "bt470bg", "smpte170m", "smpte240m",
282 * "linear", "log100", "log316", "iec61966-2-4",
283 * "bt1361e", "iec61966-2-1", "bt2020-10", "bt2020-12",
284 * "smpte-st-2084", "smpte-st-428"
285 * bit 7-0 : matrix_coefficient "GBR", "bt709", "undef", "bt601",
286 * "fcc", "bt470bg", "smpte170m", "smpte240m",
287 * "YCgCo", "bt2020nc", "bt2020c"
288 */
289 u32 signal_type;
290 struct vframe_master_display_colour_s color_parms;
291};
292
293struct aml_vdec_ps_infos {
294 u32 visible_width;
295 u32 visible_height;
296 u32 coded_width;
297 u32 coded_height;
298 u32 profile;
299 u32 mb_width;
300 u32 mb_height;
301 u32 dpb_size;
302 u32 ref_frames;
303 u32 reorder_frames;
304};
305
306struct aml_vdec_cnt_infos {
307 u32 bit_rate;
308 u32 frame_count;
309 u32 error_frame_count;
310 u32 drop_frame_count;
311 u32 total_data;
312};
313
314struct aml_dec_params {
315 u32 parms_status;
316 struct aml_vdec_cfg_infos cfg;
317 struct aml_vdec_ps_infos ps;
318 struct aml_vdec_hdr_infos hdr;
319 struct aml_vdec_cnt_infos cnt;
320};
321
322struct v4l2_config_parm {
323 u32 type;
324 u32 length;
325 union {
326 struct aml_dec_params dec;
327 struct aml_enc_params enc;
328 u8 data[200];
329 } parm;
330 u8 buf[4096];
331};
332
333enum aml_thread_type {
334 AML_THREAD_OUTPUT,
335 AML_THREAD_CAPTURE,
336};
337
338typedef void (*aml_thread_func)(struct aml_vcodec_ctx *ctx);
339
340struct aml_vdec_thread {
341 struct list_head node;
342 spinlock_t lock;
343 struct semaphore sem;
344 struct task_struct *task;
345 enum aml_thread_type type;
346 void *priv;
347 int stop;
348
349 aml_thread_func func;
350};
351
352/**
353 * struct aml_vcodec_ctx - Context (instance) private data.
354 *
355 * @type: type of the instance - decoder or encoder
356 * @dev: pointer to the aml_vcodec_dev of the device
357 * @list: link to ctx_list of aml_vcodec_dev
358 * @fh: struct v4l2_fh
359 * @m2m_ctx: pointer to the v4l2_m2m_ctx of the context
360 * @q_data: store information of input and output queue
361 * of the context
362 * @id: index of the context that this structure describes
363 * @state: state of the context
364 * @param_change: indicate encode parameter type
365 * @enc_params: encoding parameters
366 * @dec_if: hooked decoder driver interface
367 * @enc_if: hoooked encoder driver interface
368 * @drv_handle: driver handle for specific decode/encode instance
369 *
370 * @picinfo: store picture info after header parsing
371 * @dpb_size: store dpb count after header parsing
372 * @int_cond: variable used by the waitqueue
373 * @int_type: type of the last interrupt
374 * @queue: waitqueue that can be used to wait for this context to
375 * finish
376 * @irq_status: irq status
377 *
378 * @ctrl_hdl: handler for v4l2 framework
379 * @decode_work: worker for the decoding
380 * @encode_work: worker for the encoding
381 * @last_decoded_picinfo: pic information get from latest decode
382 * @empty_flush_buf: a fake size-0 capture buffer that indicates flush
383 *
384 * @colorspace: enum v4l2_colorspace; supplemental to pixelformat
385 * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
386 * @quantization: enum v4l2_quantization, colorspace quantization
387 * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
388 * @lock: protect variables accessed by V4L2 threads and worker thread such as
389 * aml_video_dec_buf.
390 */
391struct aml_vcodec_ctx {
392 enum aml_instance_type type;
393 struct aml_vcodec_dev *dev;
394 struct list_head list;
395
396 struct v4l2_fh fh;
397 struct v4l2_m2m_ctx *m2m_ctx;
398 struct aml_vdec_adapt *ada_ctx;
399 struct aml_q_data q_data[2];
400 int id;
401 struct mutex state_lock;
402 enum aml_instance_state state;
403 enum aml_encode_param param_change;
404 struct aml_enc_params enc_params;
405
406 const struct vdec_common_if *dec_if;
407 const struct venc_common_if *enc_if;
408 unsigned long drv_handle;
409
410 struct vdec_pic_info picinfo;
411 int dpb_size;
412
413 int int_cond;
414 int int_type;
415 wait_queue_head_t queue;
416 unsigned int irq_status;
417
418 struct v4l2_ctrl_handler ctrl_hdl;
419 struct work_struct decode_work;
420 struct work_struct encode_work;
421 struct vdec_pic_info last_decoded_picinfo;
422 struct aml_video_dec_buf *empty_flush_buf;
423
424 enum v4l2_colorspace colorspace;
425 enum v4l2_ycbcr_encoding ycbcr_enc;
426 enum v4l2_quantization quantization;
427 enum v4l2_xfer_func xfer_func;
428
429 int decoded_frame_cnt;
430 struct mutex lock;
431 struct completion comp;
432 bool has_receive_eos;
433 struct list_head vdec_thread_list;
434 bool is_drm_mode;
435 bool is_stream_mode;
436 int buf_used_count;
437 bool receive_cmd_stop;
438 bool scatter_mem_enable;
439 bool param_sets_from_ucode;
440 bool v4l_codec_ready;
441 bool v4l_codec_dpb_ready;
442 wait_queue_head_t wq;
443 spinlock_t slock;
444 struct v4l2_config_parm config;
445 bool is_stream_off;
446 int reset_flag;
447 int stop_cmd;
448};
449
450/**
451 * struct aml_vcodec_dev - driver data
452 * @v4l2_dev: V4L2 device to register video devices for.
453 * @vfd_dec: Video device for decoder
454 * @vfd_enc: Video device for encoder.
455 *
456 * @m2m_dev_dec: m2m device for decoder
457 * @m2m_dev_enc: m2m device for encoder.
458 * @plat_dev: platform device
459 * @vpu_plat_dev: aml vpu platform device
460 * @alloc_ctx: VB2 allocator context
461 * (for allocations without kernel mapping).
462 * @ctx_list: list of struct aml_vcodec_ctx
463 * @irqlock: protect data access by irq handler and work thread
464 * @curr_ctx: The context that is waiting for codec hardware
465 *
466 * @reg_base: Mapped address of AML Vcodec registers.
467 *
468 * @id_counter: used to identify current opened instance
469 *
470 * @encode_workqueue: encode work queue
471 *
472 * @int_cond: used to identify interrupt condition happen
473 * @int_type: used to identify what kind of interrupt condition happen
474 * @dev_mutex: video_device lock
475 * @queue: waitqueue for waiting for completion of device commands
476 *
477 * @dec_irq: decoder irq resource
478 * @enc_irq: h264 encoder irq resource
479 * @enc_lt_irq: vp8 encoder irq resource
480 *
481 * @dec_mutex: decoder hardware lock
482 * @enc_mutex: encoder hardware lock.
483 *
484 * @pm: power management control
485 * @dec_capability: used to identify decode capability, ex: 4k
486 * @enc_capability: used to identify encode capability
487 */
488struct aml_vcodec_dev {
489 struct v4l2_device v4l2_dev;
490 struct video_device *vfd_dec;
491 struct video_device *vfd_enc;
492 struct file *filp;
493
494 struct v4l2_m2m_dev *m2m_dev_dec;
495 struct v4l2_m2m_dev *m2m_dev_enc;
496 struct platform_device *plat_dev;
497 struct platform_device *vpu_plat_dev;//??
498 struct vb2_alloc_ctx *alloc_ctx;//??
499 struct list_head ctx_list;
500 spinlock_t irqlock;
501 struct aml_vcodec_ctx *curr_ctx;
502 void __iomem *reg_base[NUM_MAX_VCODEC_REG_BASE];
503
504 unsigned long id_counter;
505
506 struct workqueue_struct *decode_workqueue;
507 struct workqueue_struct *encode_workqueue;
508 int int_cond;
509 int int_type;
510 struct mutex dev_mutex;
511 wait_queue_head_t queue;
512
513 int dec_irq;
514 int enc_irq;
515 int enc_lt_irq;
516
517 struct mutex dec_mutex;
518 struct mutex enc_mutex;
519
520 struct aml_vcodec_pm pm;
521 unsigned int dec_capability;
522 unsigned int enc_capability;
523};
524
525static inline struct aml_vcodec_ctx *fh_to_ctx(struct v4l2_fh *fh)
526{
527 return container_of(fh, struct aml_vcodec_ctx, fh);
528}
529
530static inline struct aml_vcodec_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
531{
532 return container_of(ctrl->handler, struct aml_vcodec_ctx, ctrl_hdl);
533}
534
535#endif /* _AML_VCODEC_DRV_H_ */
536