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