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