blob: 4e7db8939c2be89cf9a7f580f2df92f2b9bad23c [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * zoran - Iomega Buz driver
3 *
4 * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
5 *
6 * based on
7 *
8 * zoran.0.0.3 Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9 *
10 * and
11 *
12 * bttv - Bt848 frame grabber driver
13 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
14 * & Marcus Metzler (mocm@thp.uni-koeln.de)
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31#ifndef _BUZ_H_
32#define _BUZ_H_
33
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -030034#include <media/v4l2-device.h>
Hans Verkuile56c5972015-07-20 10:18:21 -030035#include <media/v4l2-ctrls.h>
36#include <media/v4l2-fh.h>
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -030037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct zoran_sync {
39 unsigned long frame; /* number of buffer that has been free'd */
40 unsigned long length; /* number of code bytes in buffer (capture only) */
41 unsigned long seq; /* frame sequence number */
42 struct timeval timestamp; /* timestamp */
43};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ZORAN_NAME "ZORAN" /* name of the device */
47
48#define ZR_DEVNAME(zr) ((zr)->name)
49
50#define BUZ_MAX_WIDTH (zr->timing->Wa)
51#define BUZ_MAX_HEIGHT (zr->timing->Ha)
52#define BUZ_MIN_WIDTH 32 /* never display less than 32 pixels */
53#define BUZ_MIN_HEIGHT 24 /* never display less than 24 rows */
54
55#define BUZ_NUM_STAT_COM 4
56#define BUZ_MASK_STAT_COM 3
57
58#define BUZ_MAX_FRAME 256 /* Must be a power of 2 */
59#define BUZ_MASK_FRAME 255 /* Must be BUZ_MAX_FRAME-1 */
60
Martin Samuelssonfbe60da2006-04-27 10:17:00 -030061#define BUZ_MAX_INPUT 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#if VIDEO_MAX_FRAME <= 32
64# define V4L_MAX_FRAME 32
65#elif VIDEO_MAX_FRAME <= 64
66# define V4L_MAX_FRAME 64
67#else
68# error "Too many video frame buffers to handle"
69#endif
70#define V4L_MASK_FRAME (V4L_MAX_FRAME - 1)
71
Trent Piepho1159b7f2009-03-10 23:28:16 -030072#define MAX_FRAME (BUZ_MAX_FRAME > VIDEO_MAX_FRAME ? BUZ_MAX_FRAME : VIDEO_MAX_FRAME)
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include "zr36057.h"
75
76enum card_type {
77 UNKNOWN = -1,
78
79 /* Pinnacle/Miro */
80 DC10_old, /* DC30 like */
81 DC10_new, /* DC10plus like */
82 DC10plus,
83 DC30,
84 DC30plus,
85
86 /* Linux Media Labs */
87 LML33,
88 LML33R10,
89
90 /* Iomega */
91 BUZ,
92
Martin Samuelssonfbe60da2006-04-27 10:17:00 -030093 /* AverMedia */
94 AVS6EYES,
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* total number of cards */
97 NUM_CARDS
98};
99
100enum zoran_codec_mode {
101 BUZ_MODE_IDLE, /* nothing going on */
102 BUZ_MODE_MOTION_COMPRESS, /* grabbing frames */
103 BUZ_MODE_MOTION_DECOMPRESS, /* playing frames */
104 BUZ_MODE_STILL_COMPRESS, /* still frame conversion */
105 BUZ_MODE_STILL_DECOMPRESS /* still frame conversion */
106};
107
108enum zoran_buffer_state {
109 BUZ_STATE_USER, /* buffer is owned by application */
110 BUZ_STATE_PEND, /* buffer is queued in pend[] ready to feed to I/O */
111 BUZ_STATE_DMA, /* buffer is queued in dma[] for I/O */
112 BUZ_STATE_DONE /* buffer is ready to return to application */
113};
114
115enum zoran_map_mode {
116 ZORAN_MAP_MODE_RAW,
117 ZORAN_MAP_MODE_JPG_REC,
118#define ZORAN_MAP_MODE_JPG ZORAN_MAP_MODE_JPG_REC
119 ZORAN_MAP_MODE_JPG_PLAY,
120};
121
122enum gpio_type {
Mauro Carvalho Chehab61658942008-02-15 18:41:06 -0300123 ZR_GPIO_JPEG_SLEEP = 0,
124 ZR_GPIO_JPEG_RESET,
125 ZR_GPIO_JPEG_FRAME,
126 ZR_GPIO_VID_DIR,
127 ZR_GPIO_VID_EN,
128 ZR_GPIO_VID_RESET,
129 ZR_GPIO_CLK_SEL1,
130 ZR_GPIO_CLK_SEL2,
131 ZR_GPIO_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134enum gpcs_type {
135 GPCS_JPEG_RESET = 0,
136 GPCS_JPEG_START,
137 GPCS_MAX,
138};
139
140struct zoran_format {
141 char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 __u32 fourcc;
143 int colorspace;
144 int depth;
145 __u32 flags;
Trent Piepho603d6f22007-07-17 18:29:44 -0300146 __u32 vfespfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148/* flags */
149#define ZORAN_FORMAT_COMPRESSED 1<<0
150#define ZORAN_FORMAT_OVERLAY 1<<1
151#define ZORAN_FORMAT_CAPTURE 1<<2
152#define ZORAN_FORMAT_PLAYBACK 1<<3
153
154/* overlay-settings */
155struct zoran_overlay_settings {
156 int is_set;
157 int x, y, width, height; /* position */
158 int clipcount; /* position and number of clips */
159 const struct zoran_format *format; /* overlay format */
160};
161
162/* v4l-capture settings */
163struct zoran_v4l_settings {
164 int width, height, bytesperline; /* capture size */
165 const struct zoran_format *format; /* capture format */
166};
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/* jpg-capture/-playback settings */
169struct zoran_jpg_settings {
170 int decimation; /* this bit is used to set everything to default */
171 int HorDcm, VerDcm, TmpDcm; /* capture decimation settings (TmpDcm=1 means both fields) */
172 int field_per_buff, odd_even; /* field-settings (odd_even=1 (+TmpDcm=1) means top-field-first) */
173 int img_x, img_y, img_width, img_height; /* crop settings (subframe capture) */
174 struct v4l2_jpegcompression jpg_comp; /* JPEG-specific capture settings */
175};
176
Al Viro3b6456d2012-07-14 13:49:40 +0400177struct zoran_fh;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179struct zoran_mapping {
Al Viro3b6456d2012-07-14 13:49:40 +0400180 struct zoran_fh *fh;
Al Viro4ad1f702013-05-23 04:38:22 -0400181 atomic_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Trent Piepho1159b7f2009-03-10 23:28:16 -0300184struct zoran_buffer {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct zoran_mapping *map;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300186 enum zoran_buffer_state state; /* state: unused/pending/dma/done */
187 struct zoran_sync bs; /* DONE: info to return to application */
188 union {
189 struct {
190 __le32 *frag_tab; /* addresses of frag table */
191 u32 frag_tab_bus; /* same value cached to save time in ISR */
192 } jpg;
193 struct {
194 char *fbuffer; /* virtual address of frame buffer */
195 unsigned long fbuffer_phys;/* physical address of frame buffer */
196 unsigned long fbuffer_bus;/* bus address of frame buffer */
197 } v4l;
198 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201enum zoran_lock_activity {
202 ZORAN_FREE, /* free for use */
203 ZORAN_ACTIVE, /* active but unlocked */
204 ZORAN_LOCKED, /* locked */
205};
206
207/* buffer collections */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300208struct zoran_buffer_col {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 enum zoran_lock_activity active; /* feature currently in use? */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300210 unsigned int num_buffers, buffer_size;
211 struct zoran_buffer buffer[MAX_FRAME]; /* buffers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 u8 allocated; /* Flag if buffers are allocated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 u8 need_contiguous; /* Flag if contiguous buffers are needed */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300214 /* only applies to jpg buffers, raw buffers are always contiguous */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215};
216
217struct zoran;
218
219/* zoran_fh contains per-open() settings */
220struct zoran_fh {
Hans Verkuile56c5972015-07-20 10:18:21 -0300221 struct v4l2_fh fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct zoran *zr;
223
Trent Piepho1159b7f2009-03-10 23:28:16 -0300224 enum zoran_map_mode map_mode; /* Flag which bufferset will map by next mmap() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 struct zoran_overlay_settings overlay_settings;
Trent Piepho1159b7f2009-03-10 23:28:16 -0300227 u32 *overlay_mask; /* overlay mask */
228 enum zoran_lock_activity overlay_active;/* feature currently in use? */
229
230 struct zoran_buffer_col buffers; /* buffers' info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236struct card_info {
237 enum card_type type;
238 char name[32];
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300239 const char *i2c_decoder; /* i2c decoder device */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300240 const unsigned short *addrs_decoder;
241 const char *i2c_encoder; /* i2c encoder device */
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300242 const unsigned short *addrs_encoder;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 u16 video_vfe, video_codec; /* videocodec types */
244 u16 audio_chip; /* audio type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 int inputs; /* number of video inputs */
247 struct input {
248 int muxsel;
249 char name[32];
250 } input[BUZ_MAX_INPUT];
251
Hans Verkuil107063c2009-02-18 17:26:06 -0300252 v4l2_std_id norms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct tvnorm *tvn[3]; /* supported TV norms */
254
255 u32 jpeg_int; /* JPEG interrupt */
256 u32 vsync_int; /* VSYNC interrupt */
Mauro Carvalho Chehab61658942008-02-15 18:41:06 -0300257 s8 gpio[ZR_GPIO_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 u8 gpcs[GPCS_MAX];
259
260 struct vfe_polarity vfe_pol;
Mauro Carvalho Chehab61658942008-02-15 18:41:06 -0300261 u8 gpio_pol[ZR_GPIO_MAX];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300263 /* is the /GWS line connected? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 u8 gws_not_connected;
265
Martin Samuelssonfbe60da2006-04-27 10:17:00 -0300266 /* avs6eyes mux setting */
267 u8 input_mux;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 void (*init) (struct zoran * zr);
270};
271
272struct zoran {
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300273 struct v4l2_device v4l2_dev;
Hans Verkuile56c5972015-07-20 10:18:21 -0300274 struct v4l2_ctrl_handler hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 struct video_device *video_dev;
276
277 struct i2c_adapter i2c_adapter; /* */
278 struct i2c_algo_bit_data i2c_algo; /* */
279 u32 i2cbr;
280
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300281 struct v4l2_subdev *decoder; /* video decoder sub-device */
282 struct v4l2_subdev *encoder; /* video encoder sub-device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 struct videocodec *codec; /* video codec */
285 struct videocodec *vfe; /* video front end */
286
Hans Verkuil7b962d42015-07-20 10:18:20 -0300287 struct mutex lock; /* file ops serialize lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200289 u8 initialized; /* flag if zoran has been correctly initialized */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 int user; /* number of current users */
291 struct card_info card;
292 struct tvnorm *timing;
293
294 unsigned short id; /* number of this device */
295 char name[32]; /* name of this device */
296 struct pci_dev *pci_dev; /* PCI device */
297 unsigned char revision; /* revision of zr36057 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned char __iomem *zr36057_mem;/* pointer to mapped IO memory */
299
300 spinlock_t spinlock; /* Spinlock */
301
302 /* Video for Linux parameters */
Hans Verkuildebff5a2010-03-22 05:25:46 -0300303 int input; /* card's norm and input */
Hans Verkuil107063c2009-02-18 17:26:06 -0300304 v4l2_std_id norm;
Hans Verkuil7f6adea2009-02-19 17:31:17 -0300305
306 /* Current buffer params */
307 void *vbuf_base;
308 int vbuf_height, vbuf_width;
309 int vbuf_depth;
310 int vbuf_bytesperline;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct zoran_overlay_settings overlay_settings;
313 u32 *overlay_mask; /* overlay mask */
314 enum zoran_lock_activity overlay_active; /* feature currently in use? */
315
316 wait_queue_head_t v4l_capq;
317
318 int v4l_overlay_active; /* Overlay grab is activated */
319 int v4l_memgrab_active; /* Memory grab is activated */
320
321 int v4l_grab_frame; /* Frame number being currently grabbed */
322#define NO_GRAB_ACTIVE (-1)
323 unsigned long v4l_grab_seq; /* Number of frames grabbed */
324 struct zoran_v4l_settings v4l_settings; /* structure with a lot of things to play with */
325
326 /* V4L grab queue of frames pending */
327 unsigned long v4l_pend_head;
328 unsigned long v4l_pend_tail;
329 unsigned long v4l_sync_tail;
330 int v4l_pend[V4L_MAX_FRAME];
Trent Piepho1159b7f2009-03-10 23:28:16 -0300331 struct zoran_buffer_col v4l_buffers; /* V4L buffers' info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Buz MJPEG parameters */
334 enum zoran_codec_mode codec_mode; /* status of codec */
335 struct zoran_jpg_settings jpg_settings; /* structure with a lot of things to play with */
336
337 wait_queue_head_t jpg_capq; /* wait here for grab to finish */
338
339 /* grab queue counts/indices, mask with BUZ_MASK_STAT_COM before using as index */
340 /* (dma_head - dma_tail) is number active in DMA, must be <= BUZ_NUM_STAT_COM */
341 /* (value & BUZ_MASK_STAT_COM) corresponds to index in stat_com table */
342 unsigned long jpg_que_head; /* Index where to put next buffer which is queued */
343 unsigned long jpg_dma_head; /* Index of next buffer which goes into stat_com */
344 unsigned long jpg_dma_tail; /* Index of last buffer in stat_com */
345 unsigned long jpg_que_tail; /* Index of last buffer in queue */
346 unsigned long jpg_seq_num; /* count of frames since grab/play started */
347 unsigned long jpg_err_seq; /* last seq_num before error */
348 unsigned long jpg_err_shift;
349 unsigned long jpg_queued_num; /* count of frames queued since grab/play started */
350
351 /* zr36057's code buffer table */
Al Viro581a7f12008-05-21 00:32:31 -0300352 __le32 *stat_com; /* stat_com[i] is indexed by dma_head/tail & BUZ_MASK_STAT_COM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* (value & BUZ_MASK_FRAME) corresponds to index in pend[] queue */
355 int jpg_pend[BUZ_MAX_FRAME];
356
357 /* array indexed by frame number */
Trent Piepho1159b7f2009-03-10 23:28:16 -0300358 struct zoran_buffer_col jpg_buffers; /* MJPEG buffers' info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Additional stuff for testing */
361#ifdef CONFIG_PROC_FS
362 struct proc_dir_entry *zoran_proc;
363#else
364 void *zoran_proc;
365#endif
366 int testing;
367 int jpeg_error;
368 int intr_counter_GIRQ1;
369 int intr_counter_GIRQ0;
370 int intr_counter_CodRepIRQ;
371 int intr_counter_JPEGRepIRQ;
372 int field_counter;
373 int IRQ1_in;
374 int IRQ1_out;
375 int JPEG_in;
376 int JPEG_out;
377 int JPEG_0;
378 int JPEG_1;
379 int END_event_missed;
380 int JPEG_missed;
381 int JPEG_error;
382 int num_errors;
383 int JPEG_max_missed;
384 int JPEG_min_missed;
385
386 u32 last_isr;
387 unsigned long frame_num;
388
389 wait_queue_head_t test_q;
390};
391
Hans Verkuil0ab6e1c2009-02-19 16:18:23 -0300392static inline struct zoran *to_zoran(struct v4l2_device *v4l2_dev)
393{
394 return container_of(v4l2_dev, struct zoran, v4l2_dev);
395}
396
Trent Piepho5e098b62009-01-12 13:09:46 -0300397/* There was something called _ALPHA_BUZ that used the PCI address instead of
398 * the kernel iomapped address for btread/btwrite. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#define btwrite(dat,adr) writel((dat), zr->zr36057_mem+(adr))
400#define btread(adr) readl(zr->zr36057_mem+(adr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#define btand(dat,adr) btwrite((dat) & btread(adr), adr)
403#define btor(dat,adr) btwrite((dat) | btread(adr), adr)
404#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#endif