1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2005-2006 Micronas USA Inc.
4 */
5
6/*
7 * This is the private include file for the go7007 driver.  It should not
8 * be included by anybody but the driver itself, and especially not by
9 * user-space applications.
10 */
11
12#include <media/v4l2-device.h>
13#include <media/v4l2-ctrls.h>
14#include <media/v4l2-fh.h>
15#include <media/videobuf2-v4l2.h>
16
17struct go7007;
18
19/* IDs to activate board-specific support code */
20#define GO7007_BOARDID_MATRIX_II	0
21#define GO7007_BOARDID_MATRIX_RELOAD	1
22#define GO7007_BOARDID_STAR_TREK	2
23#define GO7007_BOARDID_PCI_VOYAGER	3
24#define GO7007_BOARDID_XMEN		4
25#define GO7007_BOARDID_XMEN_II		5
26#define GO7007_BOARDID_XMEN_III		6
27#define GO7007_BOARDID_MATRIX_REV	7
28#define GO7007_BOARDID_PX_M402U		8
29#define GO7007_BOARDID_PX_TV402U	9
30#define GO7007_BOARDID_LIFEVIEW_LR192	10 /* TV Walker Ultra */
31#define GO7007_BOARDID_ENDURA		11
32#define GO7007_BOARDID_ADLINK_MPG24	12
33#define GO7007_BOARDID_SENSORAY_2250	13 /* Sensoray 2250/2251 */
34#define GO7007_BOARDID_ADS_USBAV_709    14
35
36/* Various characteristics of each board */
37#define GO7007_BOARD_HAS_AUDIO		(1<<0)
38#define GO7007_BOARD_USE_ONBOARD_I2C	(1<<1)
39#define GO7007_BOARD_HAS_TUNER		(1<<2)
40
41/* Characteristics of sensor devices */
42#define GO7007_SENSOR_VALID_POLAR	(1<<0)
43#define GO7007_SENSOR_HREF_POLAR	(1<<1)
44#define GO7007_SENSOR_VREF_POLAR	(1<<2)
45#define GO7007_SENSOR_FIELD_ID_POLAR	(1<<3)
46#define GO7007_SENSOR_BIT_WIDTH		(1<<4)
47#define GO7007_SENSOR_VALID_ENABLE	(1<<5)
48#define GO7007_SENSOR_656		(1<<6)
49#define GO7007_SENSOR_CONFIG_MASK	0x7f
50#define GO7007_SENSOR_TV		(1<<7)
51#define GO7007_SENSOR_VBI		(1<<8)
52#define GO7007_SENSOR_SCALING		(1<<9)
53#define GO7007_SENSOR_SAA7115		(1<<10)
54
55/* Characteristics of audio sensor devices */
56#define GO7007_AUDIO_I2S_MODE_1		(1)
57#define GO7007_AUDIO_I2S_MODE_2		(2)
58#define GO7007_AUDIO_I2S_MODE_3		(3)
59#define GO7007_AUDIO_BCLK_POLAR		(1<<2)
60#define GO7007_AUDIO_WORD_14		(14<<4)
61#define GO7007_AUDIO_WORD_16		(16<<4)
62#define GO7007_AUDIO_ONE_CHANNEL	(1<<11)
63#define GO7007_AUDIO_I2S_MASTER		(1<<16)
64#define GO7007_AUDIO_OKI_MODE		(1<<17)
65
66#define GO7007_CID_CUSTOM_BASE		(V4L2_CID_DETECT_CLASS_BASE + 0x1000)
67#define V4L2_CID_PIXEL_THRESHOLD0	(GO7007_CID_CUSTOM_BASE+1)
68#define V4L2_CID_MOTION_THRESHOLD0	(GO7007_CID_CUSTOM_BASE+2)
69#define V4L2_CID_MB_THRESHOLD0		(GO7007_CID_CUSTOM_BASE+3)
70#define V4L2_CID_PIXEL_THRESHOLD1	(GO7007_CID_CUSTOM_BASE+4)
71#define V4L2_CID_MOTION_THRESHOLD1	(GO7007_CID_CUSTOM_BASE+5)
72#define V4L2_CID_MB_THRESHOLD1		(GO7007_CID_CUSTOM_BASE+6)
73#define V4L2_CID_PIXEL_THRESHOLD2	(GO7007_CID_CUSTOM_BASE+7)
74#define V4L2_CID_MOTION_THRESHOLD2	(GO7007_CID_CUSTOM_BASE+8)
75#define V4L2_CID_MB_THRESHOLD2		(GO7007_CID_CUSTOM_BASE+9)
76#define V4L2_CID_PIXEL_THRESHOLD3	(GO7007_CID_CUSTOM_BASE+10)
77#define V4L2_CID_MOTION_THRESHOLD3	(GO7007_CID_CUSTOM_BASE+11)
78#define V4L2_CID_MB_THRESHOLD3		(GO7007_CID_CUSTOM_BASE+12)
79
80struct go7007_board_info {
81	unsigned int flags;
82	int hpi_buffer_cap;
83	unsigned int sensor_flags;
84	int sensor_width;
85	int sensor_height;
86	int sensor_framerate;
87	int sensor_h_offset;
88	int sensor_v_offset;
89	unsigned int audio_flags;
90	int audio_rate;
91	int audio_bclk_div;
92	int audio_main_div;
93	int num_i2c_devs;
94	struct go_i2c {
95		const char *type;
96		unsigned int is_video:1;
97		unsigned int is_audio:1;
98		int addr;
99		u32 flags;
100	} i2c_devs[5];
101	int num_inputs;
102	struct {
103		int video_input;
104		int audio_index;
105		char *name;
106	} inputs[4];
107	int video_config;
108	int num_aud_inputs;
109	struct {
110		int audio_input;
111		char *name;
112	} aud_inputs[3];
113};
114
115struct go7007_hpi_ops {
116	int (*interface_reset)(struct go7007 *go);
117	int (*write_interrupt)(struct go7007 *go, int addr, int data);
118	int (*read_interrupt)(struct go7007 *go);
119	int (*stream_start)(struct go7007 *go);
120	int (*stream_stop)(struct go7007 *go);
121	int (*send_firmware)(struct go7007 *go, u8 *data, int len);
122	int (*send_command)(struct go7007 *go, unsigned int cmd, void *arg);
123	void (*release)(struct go7007 *go);
124};
125
126/* The video buffer size must be a multiple of PAGE_SIZE */
127#define	GO7007_BUF_PAGES	(128 * 1024 / PAGE_SIZE)
128#define	GO7007_BUF_SIZE		(GO7007_BUF_PAGES << PAGE_SHIFT)
129
130struct go7007_buffer {
131	struct vb2_v4l2_buffer vb;
132	struct list_head list;
133	unsigned int frame_offset;
134	u32 modet_active;
135};
136
137#define GO7007_RATIO_1_1	0
138#define GO7007_RATIO_4_3	1
139#define GO7007_RATIO_16_9	2
140
141enum go7007_parser_state {
142	STATE_DATA,
143	STATE_00,
144	STATE_00_00,
145	STATE_00_00_01,
146	STATE_FF,
147	STATE_VBI_LEN_A,
148	STATE_VBI_LEN_B,
149	STATE_MODET_MAP,
150	STATE_UNPARSED,
151};
152
153struct go7007 {
154	struct device *dev;
155	u8 bus_info[32];
156	const struct go7007_board_info *board_info;
157	unsigned int board_id;
158	int tuner_type;
159	int channel_number; /* for multi-channel boards like Adlink PCI-MPG24 */
160	char name[64];
161	struct video_device vdev;
162	void *boot_fw;
163	unsigned boot_fw_len;
164	struct v4l2_device v4l2_dev;
165	struct v4l2_ctrl_handler hdl;
166	struct v4l2_ctrl *mpeg_video_encoding;
167	struct v4l2_ctrl *mpeg_video_gop_size;
168	struct v4l2_ctrl *mpeg_video_gop_closure;
169	struct v4l2_ctrl *mpeg_video_bitrate;
170	struct v4l2_ctrl *mpeg_video_aspect_ratio;
171	struct v4l2_ctrl *mpeg_video_b_frames;
172	struct v4l2_ctrl *mpeg_video_rep_seqheader;
173	struct v4l2_ctrl *modet_mode;
174	enum { STATUS_INIT, STATUS_ONLINE, STATUS_SHUTDOWN } status;
175	spinlock_t spinlock;
176	struct mutex hw_lock;
177	struct mutex serialize_lock;
178	int audio_enabled;
179	struct v4l2_subdev *sd_video;
180	struct v4l2_subdev *sd_audio;
181	u8 usb_buf[16];
182
183	/* Video input */
184	int input;
185	int aud_input;
186	enum { GO7007_STD_NTSC, GO7007_STD_PAL, GO7007_STD_OTHER } standard;
187	v4l2_std_id std;
188	int sensor_framerate;
189	int width;
190	int height;
191	int encoder_h_offset;
192	int encoder_v_offset;
193	unsigned int encoder_h_halve:1;
194	unsigned int encoder_v_halve:1;
195	unsigned int encoder_subsample:1;
196
197	/* Encoder config */
198	u32 format;
199	int bitrate;
200	int fps_scale;
201	int pali;
202	int aspect_ratio;
203	int gop_size;
204	unsigned int ipb:1;
205	unsigned int closed_gop:1;
206	unsigned int repeat_seqhead:1;
207	unsigned int seq_header_enable:1;
208	unsigned int gop_header_enable:1;
209	unsigned int dvd_mode:1;
210	unsigned int interlace_coding:1;
211
212	/* Motion detection */
213	unsigned int modet_enable:1;
214	struct {
215		unsigned int enable:1;
216		int pixel_threshold;
217		int motion_threshold;
218		int mb_threshold;
219	} modet[4];
220	unsigned char modet_map[1624];
221	unsigned char active_map[216];
222	u32 modet_event_status;
223
224	/* Video streaming */
225	struct mutex queue_lock;
226	struct vb2_queue vidq;
227	enum go7007_parser_state state;
228	int parse_length;
229	u16 modet_word;
230	int seen_frame;
231	u32 next_seq;
232	struct list_head vidq_active;
233	wait_queue_head_t frame_waitq;
234	struct go7007_buffer *active_buf;
235
236	/* Audio streaming */
237	void (*audio_deliver)(struct go7007 *go, u8 *buf, int length);
238	void *snd_context;
239
240	/* I2C */
241	int i2c_adapter_online;
242	struct i2c_adapter i2c_adapter;
243
244	/* HPI driver */
245	const struct go7007_hpi_ops *hpi_ops;
246	void *hpi_context;
247	int interrupt_available;
248	wait_queue_head_t interrupt_waitq;
249	unsigned short interrupt_value;
250	unsigned short interrupt_data;
251};
252
253static inline struct go7007 *to_go7007(struct v4l2_device *v4l2_dev)
254{
255	return container_of(v4l2_dev, struct go7007, v4l2_dev);
256}
257
258/* All of these must be called with the hpi_lock mutex held! */
259#define go7007_interface_reset(go) \
260			((go)->hpi_ops->interface_reset(go))
261#define	go7007_write_interrupt(go, x, y) \
262			((go)->hpi_ops->write_interrupt)((go), (x), (y))
263#define go7007_stream_start(go) \
264			((go)->hpi_ops->stream_start(go))
265#define go7007_stream_stop(go) \
266			((go)->hpi_ops->stream_stop(go))
267#define	go7007_send_firmware(go, x, y) \
268			((go)->hpi_ops->send_firmware)((go), (x), (y))
269#define go7007_write_addr(go, x, y) \
270			((go)->hpi_ops->write_interrupt)((go), (x)|0x8000, (y))
271
272/* go7007-driver.c */
273int go7007_read_addr(struct go7007 *go, u16 addr, u16 *data);
274int go7007_read_interrupt(struct go7007 *go, u16 *value, u16 *data);
275int go7007_boot_encoder(struct go7007 *go, int init_i2c);
276int go7007_reset_encoder(struct go7007 *go);
277int go7007_register_encoder(struct go7007 *go, unsigned num_i2c_devs);
278int go7007_start_encoder(struct go7007 *go);
279void go7007_parse_video_stream(struct go7007 *go, u8 *buf, int length);
280struct go7007 *go7007_alloc(const struct go7007_board_info *board,
281					struct device *dev);
282void go7007_update_board(struct go7007 *go);
283
284/* go7007-fw.c */
285int go7007_construct_fw_image(struct go7007 *go, u8 **fw, int *fwlen);
286
287/* go7007-i2c.c */
288int go7007_i2c_init(struct go7007 *go);
289int go7007_i2c_remove(struct go7007 *go);
290
291/* go7007-v4l2.c */
292int go7007_v4l2_init(struct go7007 *go);
293int go7007_v4l2_ctrl_init(struct go7007 *go);
294void go7007_v4l2_remove(struct go7007 *go);
295
296/* snd-go7007.c */
297int go7007_snd_init(struct go7007 *go);
298int go7007_snd_remove(struct go7007 *go);
299