2 * TW5864 driver - common header file
4 * Copyright (C) 2016 Bluecherry, LLC <maintainers@bluecherrydvr.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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/pci.h>
18 #include <linux/videodev2.h>
19 #include <linux/notifier.h>
20 #include <linux/delay.h>
21 #include <linux/mutex.h>
23 #include <linux/interrupt.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/videobuf2-dma-sg.h>
31 #include "tw5864-reg.h"
33 #define PCI_DEVICE_ID_TECHWELL_5864 0x5864
35 #define TW5864_NORMS V4L2_STD_ALL
37 /* ----------------------------------------------------------- */
38 /* card configuration */
40 #define TW5864_INPUTS 4
42 /* The TW5864 uses 192 (16x12) detection cells in full screen for motion
43 * detection. Each detection cell is composed of 44 pixels and 20 lines for
44 * NTSC and 24 lines for PAL.
46 #define MD_CELLS_HOR 16
47 #define MD_CELLS_VERT 12
48 #define MD_CELLS (MD_CELLS_HOR * MD_CELLS_VERT)
50 #define H264_VLC_BUF_SIZE 0x80000
51 #define H264_MV_BUF_SIZE 0x2000 /* device writes 5396 bytes */
53 #define MAX_GOP_SIZE 255
54 #define GOP_SIZE MAX_GOP_SIZE
58 HD1
= 2, /* half d1 - 360x(240|288) */
63 /* ----------------------------------------------------------- */
64 /* device / file handle status */
66 struct tw5864_dev
; /* forward delclaration */
68 /* buffer for one video/vbi/ts frame */
70 struct vb2_v4l2_buffer vb
;
71 struct list_head list
;
76 struct tw5864_dma_buf
{
82 STD_NTSC
= 0, /* NTSC (M) */
83 STD_PAL
= 1, /* PAL (B, D, G, H, I) */
84 STD_SECAM
= 2, /* SECAM */
85 STD_NTSC443
= 3, /* NTSC4.43 */
86 STD_PAL_M
= 4, /* PAL (M) */
87 STD_PAL_CN
= 5, /* PAL (CN) */
88 STD_PAL_60
= 6, /* PAL 60 */
94 int nr
; /* input number */
95 struct tw5864_dev
*root
;
96 struct mutex lock
; /* used for vidq and vdev */
97 spinlock_t slock
; /* used for sync between ISR, tasklet & V4L2 API */
98 struct video_device vdev
;
99 struct v4l2_ctrl_handler hdl
;
100 struct vb2_queue vidq
;
101 struct list_head active
;
102 enum resolution resolution
;
103 unsigned int width
, height
;
104 unsigned int frame_seqno
;
105 unsigned int frame_gop_seqno
;
106 unsigned int h264_idr_pic_id
;
108 enum tw5864_vid_std std
;
109 v4l2_std_id v4l2_std
;
113 int buf_cur_space_left
;
121 u32 reg_dsp_ref_mvp_lambda
;
122 u32 reg_dsp_i4x4_weight
;
125 struct tw5864_buf
*vb
;
127 struct v4l2_ctrl
*md_threshold_grid_ctrl
;
128 u16 md_threshold_grid_values
[12 * 16];
133 * In (1/MAX_FPS) units.
134 * For max FPS (default), set to 1.
135 * For 1 FPS, set to e.g. 32.
138 unsigned long new_frame_deadline
;
141 struct tw5864_h264_frame
{
142 struct tw5864_dma_buf vlc
;
143 struct tw5864_dma_buf mv
;
146 struct tw5864_input
*input
;
149 unsigned int gop_seqno
;
152 /* global device status */
154 spinlock_t slock
; /* used for sync between ISR, tasklet & V4L2 API */
155 struct v4l2_device v4l2_dev
;
156 struct tw5864_input inputs
[TW5864_INPUTS
];
157 #define H264_BUF_CNT 4
158 struct tw5864_h264_frame h264_buf
[H264_BUF_CNT
];
159 int h264_buf_r_index
;
160 int h264_buf_w_index
;
162 struct tasklet_struct tasklet
;
165 /* Input number to check next for ready raw picture (in RR fashion) */
175 #define tw_readl(reg) readl(dev->mmio + reg)
176 #define tw_mask_readl(reg, mask) \
177 (tw_readl(reg) & (mask))
178 #define tw_mask_shift_readl(reg, mask, shift) \
179 (tw_mask_readl((reg), ((mask) << (shift))) >> (shift))
181 #define tw_writel(reg, value) writel((value), dev->mmio + reg)
182 #define tw_mask_writel(reg, mask, value) \
183 tw_writel(reg, (tw_readl(reg) & ~(mask)) | ((value) & (mask)))
184 #define tw_mask_shift_writel(reg, mask, shift, value) \
185 tw_mask_writel((reg), ((mask) << (shift)), ((value) << (shift)))
187 #define tw_setl(reg, bit) tw_writel((reg), tw_readl(reg) | (bit))
188 #define tw_clearl(reg, bit) tw_writel((reg), tw_readl(reg) & ~(bit))
190 u8
tw5864_indir_readb(struct tw5864_dev
*dev
, u16 addr
);
191 #define tw_indir_readb(addr) tw5864_indir_readb(dev, addr)
192 void tw5864_indir_writeb(struct tw5864_dev
*dev
, u16 addr
, u8 data
);
193 #define tw_indir_writeb(addr, data) tw5864_indir_writeb(dev, addr, data)
195 void tw5864_irqmask_apply(struct tw5864_dev
*dev
);
196 int tw5864_video_init(struct tw5864_dev
*dev
, int *video_nr
);
197 void tw5864_video_fini(struct tw5864_dev
*dev
);
198 void tw5864_prepare_frame_headers(struct tw5864_input
*input
);
199 void tw5864_h264_put_stream_header(u8
**buf
, size_t *space_left
, int qp
,
200 int width
, int height
);
201 void tw5864_h264_put_slice_header(u8
**buf
, size_t *space_left
,
202 unsigned int idr_pic_id
,
203 unsigned int frame_gop_seqno
,
204 int *tail_nb_bits
, u8
*tail
);
205 void tw5864_request_encoded_frame(struct tw5864_input
*input
);