1/*
2
3    bttv - Bt848 frame grabber driver
4
5    bttv's *private* header file  --  nobody other than bttv itself
6    should ever include this file.
7
8    (c) 2000-2002 Gerd Knorr <kraxel@bytesex.org>
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23*/
24
25#ifndef _BTTVP_H_
26#define _BTTVP_H_
27
28#include <linux/types.h>
29#include <linux/wait.h>
30#include <linux/i2c.h>
31#include <linux/i2c-algo-bit.h>
32#include <linux/pci.h>
33#include <linux/input.h>
34#include <linux/mutex.h>
35#include <linux/scatterlist.h>
36#include <linux/device.h>
37#include <asm/io.h>
38#include <media/v4l2-common.h>
39#include <media/v4l2-ctrls.h>
40#include <media/v4l2-fh.h>
41#include <media/videobuf-dma-sg.h>
42#include <media/tveeprom.h>
43#include <media/rc-core.h>
44#include <media/ir-kbd-i2c.h>
45#include <media/tea575x.h>
46
47#include "bt848.h"
48#include "bttv.h"
49#include "btcx-risc.h"
50
51#ifdef __KERNEL__
52
53#define FORMAT_FLAGS_DITHER       0x01
54#define FORMAT_FLAGS_PACKED       0x02
55#define FORMAT_FLAGS_PLANAR       0x04
56#define FORMAT_FLAGS_RAW          0x08
57#define FORMAT_FLAGS_CrCb         0x10
58
59#define RISC_SLOT_O_VBI        4
60#define RISC_SLOT_O_FIELD      6
61#define RISC_SLOT_E_VBI       10
62#define RISC_SLOT_E_FIELD     12
63#define RISC_SLOT_LOOP        14
64
65#define RESOURCE_OVERLAY       1
66#define RESOURCE_VIDEO_STREAM  2
67#define RESOURCE_VBI           4
68#define RESOURCE_VIDEO_READ    8
69
70#define RAW_LINES            640
71#define RAW_BPL             1024
72
73#define UNSET (-1U)
74
75/* Min. value in VDELAY register. */
76#define MIN_VDELAY 2
77/* Even to get Cb first, odd for Cr. */
78#define MAX_HDELAY (0x3FF & -2)
79/* Limits scaled width, which must be a multiple of 4. */
80#define MAX_HACTIVE (0x3FF & -4)
81
82#define BTTV_NORMS    (\
83		V4L2_STD_PAL    | V4L2_STD_PAL_N | \
84		V4L2_STD_PAL_Nc | V4L2_STD_SECAM | \
85		V4L2_STD_NTSC   | V4L2_STD_PAL_M | \
86		V4L2_STD_PAL_60)
87/* ---------------------------------------------------------- */
88
89struct bttv_tvnorm {
90	int   v4l2_id;
91	char  *name;
92	u32   Fsc;
93	u16   swidth, sheight; /* scaled standard width, height */
94	u16   totalwidth;
95	u8    adelay, bdelay, iform;
96	u32   scaledtwidth;
97	u16   hdelayx1, hactivex1;
98	u16   vdelay;
99	u8    vbipack;
100	u16   vtotal;
101	int   sram;
102	/* ITU-R frame line number of the first VBI line we can
103	   capture, of the first and second field. The last possible line
104	   is determined by cropcap.bounds. */
105	u16   vbistart[2];
106	/* Horizontally this counts fCLKx1 samples following the leading
107	   edge of the horizontal sync pulse, vertically ITU-R frame line
108	   numbers of the first field times two (2, 4, 6, ... 524 or 624). */
109	struct v4l2_cropcap cropcap;
110};
111extern const struct bttv_tvnorm bttv_tvnorms[];
112
113struct bttv_format {
114	char *name;
115	int  fourcc;          /* video4linux 2      */
116	int  btformat;        /* BT848_COLOR_FMT_*  */
117	int  btswap;          /* BT848_COLOR_CTL_*  */
118	int  depth;           /* bit/pixel          */
119	int  flags;
120	int  hshift,vshift;   /* for planar modes   */
121};
122
123struct bttv_ir {
124	struct rc_dev           *dev;
125	struct timer_list       timer;
126
127	char                    name[32];
128	char                    phys[32];
129
130	/* Usual gpio signalling */
131	u32                     mask_keycode;
132	u32                     mask_keydown;
133	u32                     mask_keyup;
134	u32                     polling;
135	u32                     last_gpio;
136	int                     shift_by;
137	int                     rc5_remote_gap;
138
139	/* RC5 gpio */
140	bool			rc5_gpio;   /* Is RC5 legacy GPIO enabled? */
141	u32                     last_bit;   /* last raw bit seen */
142	u32                     code;       /* raw code under construction */
143	struct timeval          base_time;  /* time of last seen code */
144	bool                    active;     /* building raw code */
145};
146
147
148/* ---------------------------------------------------------- */
149
150struct bttv_geometry {
151	u8  vtc,crop,comb;
152	u16 width,hscale,hdelay;
153	u16 sheight,vscale,vdelay,vtotal;
154};
155
156struct bttv_buffer {
157	/* common v4l buffer stuff -- must be first */
158	struct videobuf_buffer     vb;
159
160	/* bttv specific */
161	const struct bttv_format   *fmt;
162	unsigned int               tvnorm;
163	int                        btformat;
164	int                        btswap;
165	struct bttv_geometry       geo;
166	struct btcx_riscmem        top;
167	struct btcx_riscmem        bottom;
168	struct v4l2_rect           crop;
169	unsigned int               vbi_skip[2];
170	unsigned int               vbi_count[2];
171};
172
173struct bttv_buffer_set {
174	struct bttv_buffer     *top;       /* top field buffer    */
175	struct bttv_buffer     *bottom;    /* bottom field buffer */
176	unsigned int           top_irq;
177	unsigned int           frame_irq;
178};
179
180struct bttv_overlay {
181	unsigned int           tvnorm;
182	struct v4l2_rect       w;
183	enum v4l2_field        field;
184	struct v4l2_clip       *clips;
185	int                    nclips;
186	int                    setup_ok;
187};
188
189struct bttv_vbi_fmt {
190	struct v4l2_vbi_format fmt;
191
192	/* fmt.start[] and count[] refer to this video standard. */
193	const struct bttv_tvnorm *tvnorm;
194
195	/* Earliest possible start of video capturing with this
196	   v4l2_vbi_format, in struct bttv_crop.rect units. */
197	__s32                  end;
198};
199
200/* bttv-vbi.c */
201void bttv_vbi_fmt_reset(struct bttv_vbi_fmt *f, unsigned int norm);
202
203struct bttv_crop {
204	/* A cropping rectangle in struct bttv_tvnorm.cropcap units. */
205	struct v4l2_rect       rect;
206
207	/* Scaled image size limits with this crop rect. Divide
208	   max_height, but not min_height, by two when capturing
209	   single fields. See also bttv_crop_reset() and
210	   bttv_crop_adjust() in bttv-driver.c. */
211	__s32                  min_scaled_width;
212	__s32                  min_scaled_height;
213	__s32                  max_scaled_width;
214	__s32                  max_scaled_height;
215};
216
217struct bttv_fh {
218	/* This must be the first field in this struct */
219	struct v4l2_fh		 fh;
220
221	struct bttv              *btv;
222	int resources;
223	enum v4l2_buf_type       type;
224
225	/* video capture */
226	struct videobuf_queue    cap;
227	const struct bttv_format *fmt;
228	int                      width;
229	int                      height;
230
231	/* video overlay */
232	const struct bttv_format *ovfmt;
233	struct bttv_overlay      ov;
234
235	/* Application called VIDIOC_S_CROP. */
236	int                      do_crop;
237
238	/* vbi capture */
239	struct videobuf_queue    vbi;
240	/* Current VBI capture window as seen through this fh (cannot
241	   be global for compatibility with earlier drivers). Protected
242	   by struct bttv.lock and struct bttv_fh.vbi.lock. */
243	struct bttv_vbi_fmt      vbi_fmt;
244};
245
246/* ---------------------------------------------------------- */
247/* bttv-risc.c                                                */
248
249/* risc code generators - capture */
250int bttv_risc_packed(struct bttv *btv, struct btcx_riscmem *risc,
251		     struct scatterlist *sglist,
252		     unsigned int offset, unsigned int bpl,
253		     unsigned int pitch, unsigned int skip_lines,
254		     unsigned int store_lines);
255
256/* control dma register + risc main loop */
257void bttv_set_dma(struct bttv *btv, int override);
258int bttv_risc_init_main(struct bttv *btv);
259int bttv_risc_hook(struct bttv *btv, int slot, struct btcx_riscmem *risc,
260		   int irqflags);
261
262/* capture buffer handling */
263int bttv_buffer_risc(struct bttv *btv, struct bttv_buffer *buf);
264int bttv_buffer_activate_video(struct bttv *btv,
265			       struct bttv_buffer_set *set);
266int bttv_buffer_activate_vbi(struct bttv *btv,
267			     struct bttv_buffer *vbi);
268void bttv_dma_free(struct videobuf_queue *q, struct bttv *btv,
269		   struct bttv_buffer *buf);
270
271/* overlay handling */
272int bttv_overlay_risc(struct bttv *btv, struct bttv_overlay *ov,
273		      const struct bttv_format *fmt,
274		      struct bttv_buffer *buf);
275
276
277/* ---------------------------------------------------------- */
278/* bttv-vbi.c                                                 */
279
280int bttv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
281int bttv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
282int bttv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *f);
283
284extern struct videobuf_queue_ops bttv_vbi_qops;
285
286/* ---------------------------------------------------------- */
287/* bttv-gpio.c */
288
289extern struct bus_type bttv_sub_bus_type;
290int bttv_sub_add_device(struct bttv_core *core, char *name);
291int bttv_sub_del_devices(struct bttv_core *core);
292
293/* ---------------------------------------------------------- */
294/* bttv-cards.c                                               */
295
296extern int no_overlay;
297
298/* ---------------------------------------------------------- */
299/* bttv-input.c                                               */
300
301extern void init_bttv_i2c_ir(struct bttv *btv);
302
303/* ---------------------------------------------------------- */
304/* bttv-i2c.c                                                 */
305extern int init_bttv_i2c(struct bttv *btv);
306extern int fini_bttv_i2c(struct bttv *btv);
307
308/* ---------------------------------------------------------- */
309/* bttv-driver.c                                              */
310
311/* insmod options */
312extern unsigned int bttv_verbose;
313extern unsigned int bttv_debug;
314extern unsigned int bttv_gpio;
315extern void bttv_gpio_tracking(struct bttv *btv, char *comment);
316
317#define dprintk(fmt, ...)			\
318do {						\
319	if (bttv_debug >= 1)			\
320		pr_debug(fmt, ##__VA_ARGS__);	\
321} while (0)
322#define dprintk_cont(fmt, ...)			\
323do {						\
324	if (bttv_debug >= 1)			\
325		pr_cont(fmt, ##__VA_ARGS__);	\
326} while (0)
327#define d2printk(fmt, ...)			\
328do {						\
329	if (bttv_debug >= 2)			\
330		printk(fmt, ##__VA_ARGS__);	\
331} while (0)
332
333#define BTTV_MAX_FBUF   0x208000
334#define BTTV_TIMEOUT    msecs_to_jiffies(500)    /* 0.5 seconds */
335#define BTTV_FREE_IDLE  msecs_to_jiffies(1000)   /* one second */
336
337
338struct bttv_pll_info {
339	unsigned int pll_ifreq;    /* PLL input frequency        */
340	unsigned int pll_ofreq;    /* PLL output frequency       */
341	unsigned int pll_crystal;  /* Crystal used for input     */
342	unsigned int pll_current;  /* Currently programmed ofreq */
343};
344
345/* for gpio-connected remote control */
346struct bttv_input {
347	struct input_dev      *dev;
348	char                  name[32];
349	char                  phys[32];
350	u32                   mask_keycode;
351	u32                   mask_keydown;
352};
353
354struct bttv_suspend_state {
355	u32  gpio_enable;
356	u32  gpio_data;
357	int  disabled;
358	int  loop_irq;
359	struct bttv_buffer_set video;
360	struct bttv_buffer     *vbi;
361};
362
363struct bttv_tea575x_gpio {
364	u8 data, clk, wren, most;
365};
366
367struct bttv {
368	struct bttv_core c;
369
370	/* pci device config */
371	unsigned short id;
372	unsigned char revision;
373	unsigned char __iomem *bt848_mmio;   /* pointer to mmio */
374
375	/* card configuration info */
376	unsigned int cardid;   /* pci subsystem id (bt878 based ones) */
377	unsigned int tuner_type;  /* tuner chip type */
378	unsigned int tda9887_conf;
379	unsigned int svhs, dig;
380	unsigned int has_saa6588:1;
381	struct bttv_pll_info pll;
382	int triton1;
383	int gpioirq;
384
385	int use_i2c_hw;
386
387	/* old gpio interface */
388	int shutdown;
389
390	void (*volume_gpio)(struct bttv *btv, __u16 volume);
391	void (*audio_mode_gpio)(struct bttv *btv, struct v4l2_tuner *tuner, int set);
392
393	/* new gpio interface */
394	spinlock_t gpio_lock;
395
396	/* i2c layer */
397	struct i2c_algo_bit_data   i2c_algo;
398	struct i2c_client          i2c_client;
399	int                        i2c_state, i2c_rc;
400	int                        i2c_done;
401	wait_queue_head_t          i2c_queue;
402	struct v4l2_subdev 	  *sd_msp34xx;
403	struct v4l2_subdev 	  *sd_tvaudio;
404	struct v4l2_subdev	  *sd_tda7432;
405
406	/* video4linux (1) */
407	struct video_device video_dev;
408	struct video_device radio_dev;
409	struct video_device vbi_dev;
410
411	/* controls */
412	struct v4l2_ctrl_handler   ctrl_handler;
413	struct v4l2_ctrl_handler   radio_ctrl_handler;
414
415	/* infrared remote */
416	int has_remote;
417	struct bttv_ir *remote;
418
419	/* I2C remote data */
420	struct IR_i2c_init_data    init_data;
421
422	/* locking */
423	spinlock_t s_lock;
424	struct mutex lock;
425	int resources;
426
427	/* video state */
428	unsigned int input;
429	unsigned int audio_input;
430	unsigned int mute;
431	unsigned long tv_freq;
432	unsigned int tvnorm;
433	v4l2_std_id std;
434	int hue, contrast, bright, saturation;
435	struct v4l2_framebuffer fbuf;
436	unsigned int field_count;
437
438	/* various options */
439	int opt_combfilter;
440	int opt_automute;
441	int opt_vcr_hack;
442	int opt_uv_ratio;
443
444	/* radio data/state */
445	int has_radio;
446	int has_radio_tuner;
447	int radio_user;
448	int radio_uses_msp_demodulator;
449	unsigned long radio_freq;
450
451	/* miro/pinnacle + Aimslab VHX
452	   philips matchbox (tea5757 radio tuner) support */
453	int has_tea575x;
454	struct bttv_tea575x_gpio tea_gpio;
455	struct snd_tea575x tea;
456
457	/* ISA stuff (Terratec Active Radio Upgrade) */
458	int mbox_ior;
459	int mbox_iow;
460	int mbox_csel;
461
462	/* switch status for multi-controller cards */
463	char sw_status[4];
464
465	/* risc memory management data
466	   - must acquire s_lock before changing these
467	   - only the irq handler is supported to touch top + bottom + vcurr */
468	struct btcx_riscmem     main;
469	struct bttv_buffer      *screen;    /* overlay             */
470	struct list_head        capture;    /* video capture queue */
471	struct list_head        vcapture;   /* vbi capture queue   */
472	struct bttv_buffer_set  curr;       /* active buffers      */
473	struct bttv_buffer      *cvbi;      /* active vbi buffer   */
474	int                     loop_irq;
475	int                     new_input;
476
477	unsigned long cap_ctl;
478	unsigned long dma_on;
479	struct timer_list timeout;
480	struct bttv_suspend_state state;
481
482	/* stats */
483	unsigned int errors;
484	unsigned int framedrop;
485	unsigned int irq_total;
486	unsigned int irq_me;
487
488	unsigned int users;
489	struct bttv_fh init;
490
491	/* used to make dvb-bt8xx autoloadable */
492	struct work_struct request_module_wk;
493
494	/* Default (0) and current (1) video capturing and overlay
495	   cropping parameters in bttv_tvnorm.cropcap units. Protected
496	   by bttv.lock. */
497	struct bttv_crop crop[2];
498
499	/* Earliest possible start of video capturing in
500	   bttv_tvnorm.cropcap line units. Set by check_alloc_btres()
501	   and free_btres(). Protected by bttv.lock. */
502	__s32			vbi_end;
503
504	/* Latest possible end of VBI capturing (= crop[x].rect.top when
505	   VIDEO_RESOURCES are locked). Set by check_alloc_btres()
506	   and free_btres(). Protected by bttv.lock. */
507	__s32			crop_start;
508};
509
510static inline struct bttv *to_bttv(struct v4l2_device *v4l2_dev)
511{
512	return container_of(v4l2_dev, struct bttv, c.v4l2_dev);
513}
514
515/* our devices */
516#define BTTV_MAX 32
517extern unsigned int bttv_num;
518extern struct bttv *bttvs[BTTV_MAX];
519
520static inline unsigned int bttv_muxsel(const struct bttv *btv,
521				       unsigned int input)
522{
523	return (bttv_tvcards[btv->c.type].muxsel >> (input * 2)) & 3;
524}
525
526#endif
527
528#define btwrite(dat,adr)    writel((dat), btv->bt848_mmio+(adr))
529#define btread(adr)         readl(btv->bt848_mmio+(adr))
530
531#define btand(dat,adr)      btwrite((dat) & btread(adr), adr)
532#define btor(dat,adr)       btwrite((dat) | btread(adr), adr)
533#define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
534
535#endif /* _BTTVP_H_ */
536