1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * zoran - Iomega Buz driver
4  *
5  * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
6  *
7  * based on
8  *
9  * zoran.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
10  *
11  * and
12  *
13  * bttv - Bt848 frame grabber driver
14  * Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
15  *                        & Marcus Metzler (mocm@thp.uni-koeln.de)
16  */
17 
18 #ifndef _BUZ_H_
19 #define _BUZ_H_
20 
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/videobuf2-core.h>
24 #include <media/videobuf2-v4l2.h>
25 #include <media/videobuf2-dma-contig.h>
26 
27 #define ZR_NORM_PAL 0
28 #define ZR_NORM_NTSC 1
29 #define ZR_NORM_SECAM 2
30 
31 struct zr_buffer {
32 	/* common v4l buffer stuff -- must be first */
33 	struct vb2_v4l2_buffer          vbuf;
34 	struct list_head                queue;
35 };
36 
vb2_to_zr_buffer(struct vb2_buffer * vb)37 static inline struct zr_buffer *vb2_to_zr_buffer(struct vb2_buffer *vb)
38 {
39 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
40 
41 	return container_of(vbuf, struct zr_buffer, vbuf);
42 }
43 
44 #define ZORAN_NAME    "ZORAN"	/* name of the device */
45 
46 #define ZR_DEVNAME(zr) ((zr)->name)
47 
48 #define   BUZ_MAX_WIDTH   (zr->timing->wa)
49 #define   BUZ_MAX_HEIGHT  (zr->timing->ha)
50 #define   BUZ_MIN_WIDTH    32	/* never display less than 32 pixels */
51 #define   BUZ_MIN_HEIGHT   24	/* never display less than 24 rows */
52 
53 #define BUZ_NUM_STAT_COM    4
54 #define BUZ_MASK_STAT_COM   3
55 
56 #define BUZ_MAX_FRAME     256	/* Must be a power of 2 */
57 #define BUZ_MASK_FRAME    255	/* Must be BUZ_MAX_FRAME-1 */
58 
59 #define BUZ_MAX_INPUT       16
60 
61 #if VIDEO_MAX_FRAME <= 32
62 #   define   V4L_MAX_FRAME   32
63 #elif VIDEO_MAX_FRAME <= 64
64 #   define   V4L_MAX_FRAME   64
65 #else
66 #   error   "Too many video frame buffers to handle"
67 #endif
68 #define   V4L_MASK_FRAME   (V4L_MAX_FRAME - 1)
69 
70 #define MAX_FRAME (BUZ_MAX_FRAME > VIDEO_MAX_FRAME ? BUZ_MAX_FRAME : VIDEO_MAX_FRAME)
71 
72 #include "zr36057.h"
73 
74 enum card_type {
75 	UNKNOWN = -1,
76 
77 	/* Pinnacle/Miro */
78 	DC10_OLD,		/* DC30 like */
79 	DC10_NEW,		/* DC10_PLUS like */
80 	DC10_PLUS,
81 	DC30,
82 	DC30_PLUS,
83 
84 	/* Linux Media Labs */
85 	LML33,
86 	LML33R10,
87 
88 	/* Iomega */
89 	BUZ,
90 
91 	/* AverMedia */
92 	AVS6EYES,
93 
94 	/* total number of cards */
95 	NUM_CARDS
96 };
97 
98 enum zoran_codec_mode {
99 	BUZ_MODE_IDLE,		/* nothing going on */
100 	BUZ_MODE_MOTION_COMPRESS,	/* grabbing frames */
101 	BUZ_MODE_MOTION_DECOMPRESS,	/* playing frames */
102 	BUZ_MODE_STILL_COMPRESS,	/* still frame conversion */
103 	BUZ_MODE_STILL_DECOMPRESS	/* still frame conversion */
104 };
105 
106 enum zoran_map_mode {
107 	ZORAN_MAP_MODE_NONE,
108 	ZORAN_MAP_MODE_RAW,
109 	ZORAN_MAP_MODE_JPG_REC,
110 	ZORAN_MAP_MODE_JPG_PLAY,
111 };
112 
113 enum gpio_type {
114 	ZR_GPIO_JPEG_SLEEP = 0,
115 	ZR_GPIO_JPEG_RESET,
116 	ZR_GPIO_JPEG_FRAME,
117 	ZR_GPIO_VID_DIR,
118 	ZR_GPIO_VID_EN,
119 	ZR_GPIO_VID_RESET,
120 	ZR_GPIO_CLK_SEL1,
121 	ZR_GPIO_CLK_SEL2,
122 	ZR_GPIO_MAX,
123 };
124 
125 enum gpcs_type {
126 	GPCS_JPEG_RESET = 0,
127 	GPCS_JPEG_START,
128 	GPCS_MAX,
129 };
130 
131 struct zoran_format {
132 	char *name;
133 	__u32 fourcc;
134 	int colorspace;
135 	int depth;
136 	__u32 flags;
137 	__u32 vfespfr;
138 };
139 
140 /* flags */
141 #define ZORAN_FORMAT_COMPRESSED BIT(0)
142 #define ZORAN_FORMAT_OVERLAY BIT(1)
143 #define ZORAN_FORMAT_CAPTURE BIT(2)
144 #define ZORAN_FORMAT_PLAYBACK BIT(3)
145 
146 /* v4l-capture settings */
147 struct zoran_v4l_settings {
148 	int width, height, bytesperline;	/* capture size */
149 	const struct zoran_format *format;	/* capture format */
150 };
151 
152 /* jpg-capture/-playback settings */
153 struct zoran_jpg_settings {
154 	int decimation;		/* this bit is used to set everything to default */
155 	int hor_dcm, ver_dcm, tmp_dcm;	/* capture decimation settings (tmp_dcm=1 means both fields) */
156 	int field_per_buff, odd_even;	/* field-settings (odd_even=1 (+tmp_dcm=1) means top-field-first) */
157 	int img_x, img_y, img_width, img_height;	/* crop settings (subframe capture) */
158 	struct v4l2_jpegcompression jpg_comp;	/* JPEG-specific capture settings */
159 };
160 
161 
162 struct zoran;
163 
164 /* zoran_fh contains per-open() settings */
165 struct zoran_fh {
166 	struct v4l2_fh fh;
167 	struct zoran *zr;
168 };
169 
170 struct card_info {
171 	enum card_type type;
172 	char name[32];
173 	const char *i2c_decoder;	/* i2c decoder device */
174 	const unsigned short *addrs_decoder;
175 	const char *i2c_encoder;	/* i2c encoder device */
176 	const unsigned short *addrs_encoder;
177 	u16 video_vfe, video_codec;			/* videocodec types */
178 	u16 audio_chip;					/* audio type */
179 
180 	int inputs;		/* number of video inputs */
181 	struct input {
182 		int muxsel;
183 		char name[32];
184 	} input[BUZ_MAX_INPUT];
185 
186 	v4l2_std_id norms;
187 	const struct tvnorm *tvn[3];	/* supported TV norms */
188 
189 	u32 jpeg_int;		/* JPEG interrupt */
190 	u32 vsync_int;		/* VSYNC interrupt */
191 	s8 gpio[ZR_GPIO_MAX];
192 	u8 gpcs[GPCS_MAX];
193 
194 	struct vfe_polarity vfe_pol;
195 	u8 gpio_pol[ZR_GPIO_MAX];
196 
197 	/* is the /GWS line connected? */
198 	u8 gws_not_connected;
199 
200 	/* avs6eyes mux setting */
201 	u8 input_mux;
202 
203 	void (*init)(struct zoran *zr);
204 };
205 
206 struct zoran {
207 	struct v4l2_device v4l2_dev;
208 	struct v4l2_ctrl_handler hdl;
209 	struct video_device *video_dev;
210 	struct vb2_queue vq;
211 
212 	struct i2c_adapter i2c_adapter;	/* */
213 	struct i2c_algo_bit_data i2c_algo;	/* */
214 	u32 i2cbr;
215 
216 	struct v4l2_subdev *decoder;	/* video decoder sub-device */
217 	struct v4l2_subdev *encoder;	/* video encoder sub-device */
218 
219 	struct videocodec *codec;	/* video codec */
220 	struct videocodec *vfe;	/* video front end */
221 
222 	struct mutex lock;	/* file ops serialize lock */
223 
224 	u8 initialized;		/* flag if zoran has been correctly initialized */
225 	struct card_info card;
226 	const struct tvnorm *timing;
227 
228 	unsigned short id;	/* number of this device */
229 	char name[32];		/* name of this device */
230 	struct pci_dev *pci_dev;	/* PCI device */
231 	unsigned char revision;	/* revision of zr36057 */
232 	unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
233 
234 	spinlock_t spinlock;	/* Spinlock */
235 
236 	/* Video for Linux parameters */
237 	int input;	/* card's norm and input */
238 	v4l2_std_id norm;
239 
240 	/* Current buffer params */
241 	unsigned int buffer_size;
242 
243 	struct zoran_v4l_settings v4l_settings;	/* structure with a lot of things to play with */
244 
245 	/* Buz MJPEG parameters */
246 	enum zoran_codec_mode codec_mode;	/* status of codec */
247 	struct zoran_jpg_settings jpg_settings;	/* structure with a lot of things to play with */
248 
249 	/* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
250 	/* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
251 	/* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
252 	unsigned long jpg_que_head;	/* Index where to put next buffer which is queued */
253 	unsigned long jpg_dma_head;	/* Index of next buffer which goes into stat_com */
254 	unsigned long jpg_dma_tail;	/* Index of last buffer in stat_com */
255 	unsigned long jpg_que_tail;	/* Index of last buffer in queue */
256 	unsigned long jpg_seq_num;	/* count of frames since grab/play started */
257 	unsigned long jpg_err_seq;	/* last seq_num before error */
258 	unsigned long jpg_err_shift;
259 	unsigned long jpg_queued_num;	/* count of frames queued since grab/play started */
260 	unsigned long vbseq;
261 
262 	/* zr36057's code buffer table */
263 	__le32 *stat_com;		/* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
264 
265 	/* Additional stuff for testing */
266 	unsigned int ghost_int;
267 	int intr_counter_GIRQ1;
268 	int intr_counter_GIRQ0;
269 	int intr_counter_cod_rep_irq;
270 	int intr_counter_jpeg_rep_irq;
271 	int field_counter;
272 	int irq1_in;
273 	int irq1_out;
274 	int jpeg_in;
275 	int jpeg_out;
276 	int JPEG_0;
277 	int JPEG_1;
278 	int end_event_missed;
279 	int jpeg_missed;
280 	int jpeg_error;
281 	int num_errors;
282 	int jpeg_max_missed;
283 	int jpeg_min_missed;
284 	unsigned int prepared;
285 	unsigned int queued;
286 
287 	u32 last_isr;
288 	unsigned long frame_num;
289 	int running;
290 	int buf_in_reserve;
291 
292 	dma_addr_t p_sc;
293 	__le32 *stat_comb;
294 	dma_addr_t p_scb;
295 	enum zoran_map_mode map_mode;
296 	struct list_head queued_bufs;
297 	spinlock_t queued_bufs_lock; /* Protects queued_bufs */
298 	struct zr_buffer *inuse[BUZ_NUM_STAT_COM * 2];
299 };
300 
to_zoran(struct v4l2_device * v4l2_dev)301 static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
302 {
303 	return container_of(v4l2_dev, struct zoran, v4l2_dev);
304 }
305 
306 /* There was something called _ALPHA_BUZ that used the PCI address instead of
307  * the kernel iomapped address for btread/btwrite.  */
308 #define btwrite(dat, adr)    writel((dat), zr->zr36057_mem + (adr))
309 #define btread(adr)         readl(zr->zr36057_mem + (adr))
310 
311 #define btand(dat, adr)      btwrite((dat) & btread(adr), adr)
312 #define btor(dat, adr)       btwrite((dat) | btread(adr), adr)
313 #define btaor(dat, mask, adr) btwrite((dat) | ((mask) & btread(adr)), adr)
314 
315 #endif
316 
317 int zoran_queue_init(struct zoran *zr, struct vb2_queue *vq);
318 void zoran_queue_exit(struct zoran *zr);
319 int zr_set_buf(struct zoran *zr);
320