3 * v4l2 device driver for philips saa7134 based TV cards
5 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/version.h>
23 #define SAA7134_VERSION_CODE KERNEL_VERSION(0,2,14)
25 #include <linux/pci.h>
26 #include <linux/i2c.h>
27 #include <linux/videodev.h>
28 #include <linux/kdev_t.h>
29 #include <linux/input.h>
33 #include <media/tuner.h>
34 #include <media/audiochip.h>
36 #include <media/ir-common.h>
37 #include <media/video-buf.h>
38 #include <media/video-buf-dvb.h>
48 #include <sound/driver.h>
49 #include <sound/core.h>
50 #include <sound/pcm.h>
52 /* ----------------------------------------------------------- */
55 enum saa7134_tvaudio_mode {
57 TVAUDIO_FM_BG_STEREO = 2,
58 TVAUDIO_FM_SAT_STEREO = 3,
59 TVAUDIO_FM_K_STEREO = 4,
64 enum saa7134_audio_in {
71 enum saa7134_video_out {
75 /* ----------------------------------------------------------- */
78 struct saa7134_tvnorm {
83 unsigned int sync_control;
84 unsigned int luma_control;
85 unsigned int chroma_ctrl1;
86 unsigned int chroma_gain;
87 unsigned int chroma_ctrl2;
88 unsigned int vgate_misc;
93 unsigned int video_v_start;
94 unsigned int video_v_stop;
95 unsigned int vbi_v_start_0;
96 unsigned int vbi_v_stop_0;
97 unsigned int src_timing;
98 unsigned int vbi_v_start_1;
101 struct saa7134_tvaudio {
104 enum saa7134_tvaudio_mode mode;
109 struct saa7134_format {
114 unsigned int vshift; /* vertical downsampling (for planar yuv) */
115 unsigned int hshift; /* horizontal downsampling (for planar yuv) */
116 unsigned int bswap:1;
117 unsigned int wswap:1;
119 unsigned int planar:1;
120 unsigned int uvswap:1;
123 /* ----------------------------------------------------------- */
124 /* card configuration */
126 #define SAA7134_BOARD_NOAUTO UNSET
127 #define SAA7134_BOARD_UNKNOWN 0
128 #define SAA7134_BOARD_PROTEUS_PRO 1
129 #define SAA7134_BOARD_FLYVIDEO3000 2
130 #define SAA7134_BOARD_FLYVIDEO2000 3
131 #define SAA7134_BOARD_EMPRESS 4
132 #define SAA7134_BOARD_MONSTERTV 5
133 #define SAA7134_BOARD_MD9717 6
134 #define SAA7134_BOARD_TVSTATION_RDS 7
135 #define SAA7134_BOARD_CINERGY400 8
136 #define SAA7134_BOARD_MD5044 9
137 #define SAA7134_BOARD_KWORLD 10
138 #define SAA7134_BOARD_CINERGY600 11
139 #define SAA7134_BOARD_MD7134 12
140 #define SAA7134_BOARD_TYPHOON_90031 13
141 #define SAA7134_BOARD_ELSA 14
142 #define SAA7134_BOARD_ELSA_500TV 15
143 #define SAA7134_BOARD_ASUSTeK_TVFM7134 16
144 #define SAA7134_BOARD_VA1000POWER 17
145 #define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
146 #define SAA7134_BOARD_VIDEOMATE_TV 19
147 #define SAA7134_BOARD_CRONOS_PLUS 20
148 #define SAA7134_BOARD_10MOONSTVMASTER 21
149 #define SAA7134_BOARD_MD2819 22
150 #define SAA7134_BOARD_BMK_MPEX_TUNER 23
151 #define SAA7134_BOARD_TVSTATION_DVR 24
152 #define SAA7134_BOARD_ASUSTEK_TVFM7133 25
153 #define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
154 #define SAA7134_BOARD_MANLI_MTV002 27
155 #define SAA7134_BOARD_MANLI_MTV001 28
156 #define SAA7134_BOARD_TG3000TV 29
157 #define SAA7134_BOARD_ECS_TVP3XP 30
158 #define SAA7134_BOARD_ECS_TVP3XP_4CB5 31
159 #define SAA7134_BOARD_AVACSSMARTTV 32
160 #define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
161 #define SAA7134_BOARD_NOVAC_PRIMETV7133 34
162 #define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
163 #define SAA7134_BOARD_UPMOST_PURPLE_TV 36
164 #define SAA7134_BOARD_ITEMS_MTV005 37
165 #define SAA7134_BOARD_CINERGY200 38
166 #define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
167 #define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
168 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
169 #define SAA7134_BOARD_SABRENT_SBTTVFM 42
170 #define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
171 #define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
172 #define SAA7134_BOARD_AVERMEDIA_STUDIO_307 45
173 #define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
174 #define SAA7134_BOARD_CINERGY400_CARDBUS 47
175 #define SAA7134_BOARD_CINERGY600_MK3 48
176 #define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
177 #define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
178 #define SAA7134_BOARD_PROVIDEO_PV952 51
179 #define SAA7134_BOARD_AVERMEDIA_305 52
180 #define SAA7134_BOARD_ASUSTeK_TVFM7135 53
181 #define SAA7134_BOARD_FLYTVPLATINUM_FM 54
182 #define SAA7134_BOARD_FLYDVBTDUO 55
183 #define SAA7134_BOARD_AVERMEDIA_307 56
184 #define SAA7134_BOARD_AVERMEDIA_GO_007_FM 57
185 #define SAA7134_BOARD_ADS_INSTANT_TV 58
186 #define SAA7134_BOARD_KWORLD_VSTREAM_XPERT 59
187 #define SAA7134_BOARD_THYPHOON_DVBT_DUO_CARDBUS 60
188 #define SAA7134_BOARD_PHILIPS_TOUGH 61
189 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII 62
190 #define SAA7134_BOARD_KWORLD_XPERT 63
191 #define SAA7134_BOARD_FLYTV_DIGIMATRIX 64
192 #define SAA7134_BOARD_KWORLD_TERMINATOR 65
193 #define SAA7134_BOARD_YUAN_TUN900 66
194 #define SAA7134_BOARD_BEHOLD_409FM 67
195 #define SAA7134_BOARD_GOTVIEW_7135 68
196 #define SAA7134_BOARD_PHILIPS_EUROPA 69
197 #define SAA7134_BOARD_VIDEOMATE_DVBT_300 70
198 #define SAA7134_BOARD_VIDEOMATE_DVBT_200 71
199 #define SAA7134_BOARD_RTD_VFG7350 72
200 #define SAA7134_BOARD_RTD_VFG7330 73
201 #define SAA7134_BOARD_FLYTVPLATINUM_MINI2 74
203 #define SAA7134_MAXBOARDS 8
204 #define SAA7134_INPUT_MAX 8
206 /* ----------------------------------------------------------- */
207 /* Video Output Port Register Initialization Options */
209 #define SET_T_CODE_POLARITY_NON_INVERTED (1 << 0)
210 #define SET_CLOCK_NOT_DELAYED (1 << 1)
211 #define SET_CLOCK_INVERTED (1 << 2)
212 #define SET_VSYNC_OFF (1 << 3)
214 struct saa7134_input {
217 enum saa7134_audio_in amux;
222 enum saa7134_mpeg_type {
224 SAA7134_MPEG_EMPRESS,
228 struct saa7134_board {
230 unsigned int audio_clock;
232 /* input switching */
233 unsigned int gpiomask;
234 struct saa7134_input inputs[SAA7134_INPUT_MAX];
235 struct saa7134_input radio;
236 struct saa7134_input mute;
239 unsigned int tuner_type;
240 unsigned int radio_type;
241 unsigned char tuner_addr;
242 unsigned char radio_addr;
244 unsigned int tda9887_conf;
247 enum saa7134_video_out video_out;
248 enum saa7134_mpeg_type mpeg;
249 unsigned int vid_port_opts;
252 #define card_has_radio(dev) (NULL != saa7134_boards[dev->board].radio.name)
253 #define card_is_empress(dev) (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
254 #define card_is_dvb(dev) (SAA7134_MPEG_DVB == saa7134_boards[dev->board].mpeg)
255 #define card_has_mpeg(dev) (SAA7134_MPEG_UNUSED != saa7134_boards[dev->board].mpeg)
256 #define card(dev) (saa7134_boards[dev->board])
257 #define card_in(dev,n) (saa7134_boards[dev->board].inputs[n])
259 /* ----------------------------------------------------------- */
260 /* device / file handle status */
262 #define RESOURCE_OVERLAY 1
263 #define RESOURCE_VIDEO 2
264 #define RESOURCE_VBI 4
266 #define INTERLACE_AUTO 0
267 #define INTERLACE_ON 1
268 #define INTERLACE_OFF 2
270 #define BUFFER_TIMEOUT (HZ/2) /* 0.5 seconds */
275 /* saa7134 page table */
276 struct saa7134_pgtable {
282 /* tvaudio thread status */
283 struct saa7134_thread {
285 struct completion exit;
286 wait_queue_head_t wq;
287 unsigned int shutdown;
293 /* buffer for one video/vbi/ts frame */
295 /* common v4l buffer stuff -- must be first */
296 struct videobuf_buffer vb;
298 /* saa7134 specific */
299 struct saa7134_format *fmt;
300 unsigned int top_seen;
301 int (*activate)(struct saa7134_dev *dev,
302 struct saa7134_buf *buf,
303 struct saa7134_buf *next);
306 struct saa7134_pgtable *pt;
309 struct saa7134_dmaqueue {
310 struct saa7134_dev *dev;
311 struct saa7134_buf *curr;
312 struct list_head queue;
313 struct timer_list timeout;
314 unsigned int need_two;
317 /* video filehandle status */
319 struct saa7134_dev *dev;
321 enum v4l2_buf_type type;
322 unsigned int resources;
323 #ifdef VIDIOC_G_PRIORITY
324 enum v4l2_priority prio;
328 struct v4l2_window win;
329 struct v4l2_clip clips[8];
333 struct saa7134_format *fmt;
334 unsigned int width,height;
335 struct videobuf_queue cap;
336 struct saa7134_pgtable pt_cap;
339 struct videobuf_queue vbi;
340 struct saa7134_pgtable pt_vbi;
345 struct semaphore lock;
348 unsigned int users_dsp;
351 enum saa7134_audio_in input;
359 unsigned int channels;
360 unsigned int recording_on;
361 unsigned int dma_running;
363 unsigned int blksize;
364 unsigned int bufsize;
365 struct saa7134_pgtable pt;
366 struct videobuf_dmabuf dma;
367 wait_queue_head_t wq;
368 unsigned int dma_blk;
369 unsigned int read_offset;
370 unsigned int read_count;
371 snd_pcm_substream_t *substream;
376 struct input_dev *dev;
377 struct ir_input_state ir;
385 struct timer_list timer;
391 struct saa7134_pgtable pt_ts;
397 struct saa7134_mpeg_ops {
398 enum saa7134_mpeg_type type;
399 struct list_head next;
400 int (*init)(struct saa7134_dev *dev);
401 int (*fini)(struct saa7134_dev *dev);
402 void (*signal_change)(struct saa7134_dev *dev);
405 /* global device status */
407 struct list_head devlist;
408 struct semaphore lock;
410 #ifdef VIDIOC_G_PRIORITY
411 struct v4l2_prio_state prio;
414 /* various device info */
415 unsigned int resources;
416 struct video_device *video_dev;
417 struct video_device *radio_dev;
418 struct video_device *vbi_dev;
419 struct saa7134_oss oss;
421 /* infrared remote */
423 struct saa7134_ir *remote;
429 unsigned char pci_rev,pci_lat;
430 __u32 __iomem *lmmio;
435 unsigned int tuner_type;
436 unsigned int radio_type;
437 unsigned char tuner_addr;
438 unsigned char radio_addr;
440 unsigned int tda9887_conf;
441 unsigned int gpio_value;
444 struct i2c_adapter i2c_adap;
445 struct i2c_client i2c_client;
446 unsigned char eedata[128];
449 struct v4l2_framebuffer ovbuf;
450 struct saa7134_format *ovfmt;
451 unsigned int ovenable;
452 enum v4l2_field ovfield;
454 /* video+ts+vbi capture */
455 struct saa7134_dmaqueue video_q;
456 struct saa7134_dmaqueue vbi_q;
457 unsigned int video_fieldcount;
458 unsigned int vbi_fieldcount;
460 /* various v4l controls */
461 struct saa7134_tvnorm *tvnorm; /* video */
462 struct saa7134_tvaudio *tvaudio;
463 unsigned int ctl_input;
469 int ctl_mute; /* audio */
471 int ctl_invert; /* private */
478 struct v4l2_rect crop_bounds;
479 struct v4l2_rect crop_defrect;
480 struct v4l2_rect crop_current;
482 /* other global state info */
483 unsigned int automute;
484 struct saa7134_thread thread;
485 struct saa7134_input *input;
486 struct saa7134_input *hw_input;
487 unsigned int hw_mute;
492 struct saa7134_ts ts;
493 struct saa7134_dmaqueue ts_q;
494 struct saa7134_mpeg_ops *mops;
496 /* SAA7134_MPEG_EMPRESS only */
497 struct video_device *empress_dev;
498 struct videobuf_queue empress_tsq;
499 unsigned int empress_users;
500 struct work_struct empress_workqueue;
503 /* SAA7134_MPEG_DVB only */
504 struct videobuf_dvb dvb;
507 /* ----------------------------------------------------------- */
509 #define saa_readl(reg) readl(dev->lmmio + (reg))
510 #define saa_writel(reg,value) writel((value), dev->lmmio + (reg));
511 #define saa_andorl(reg,mask,value) \
512 writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
513 ((value) & (mask)), dev->lmmio+(reg))
514 #define saa_setl(reg,bit) saa_andorl((reg),(bit),(bit))
515 #define saa_clearl(reg,bit) saa_andorl((reg),(bit),0)
517 #define saa_readb(reg) readb(dev->bmmio + (reg))
518 #define saa_writeb(reg,value) writeb((value), dev->bmmio + (reg));
519 #define saa_andorb(reg,mask,value) \
520 writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
521 ((value) & (mask)), dev->bmmio+(reg))
522 #define saa_setb(reg,bit) saa_andorb((reg),(bit),(bit))
523 #define saa_clearb(reg,bit) saa_andorb((reg),(bit),0)
525 #define saa_wait(us) { udelay(us); }
527 /* ----------------------------------------------------------- */
530 extern struct list_head saa7134_devlist;
532 void saa7134_print_ioctl(char *name, unsigned int cmd);
533 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg);
535 #define SAA7134_PGTABLE_SIZE 4096
537 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt);
538 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
539 struct scatterlist *list, unsigned int length,
540 unsigned int startpage);
541 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt);
543 int saa7134_buffer_count(unsigned int size, unsigned int count);
544 int saa7134_buffer_startpage(struct saa7134_buf *buf);
545 unsigned long saa7134_buffer_base(struct saa7134_buf *buf);
547 int saa7134_buffer_queue(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
548 struct saa7134_buf *buf);
549 void saa7134_buffer_finish(struct saa7134_dev *dev, struct saa7134_dmaqueue *q,
551 void saa7134_buffer_next(struct saa7134_dev *dev, struct saa7134_dmaqueue *q);
552 void saa7134_buffer_timeout(unsigned long data);
553 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf);
555 int saa7134_set_dmabits(struct saa7134_dev *dev);
557 /* ----------------------------------------------------------- */
558 /* saa7134-cards.c */
560 extern struct saa7134_board saa7134_boards[];
561 extern const unsigned int saa7134_bcount;
562 extern struct pci_device_id __devinitdata saa7134_pci_tbl[];
564 extern int saa7134_board_init1(struct saa7134_dev *dev);
565 extern int saa7134_board_init2(struct saa7134_dev *dev);
568 /* ----------------------------------------------------------- */
571 int saa7134_i2c_register(struct saa7134_dev *dev);
572 int saa7134_i2c_unregister(struct saa7134_dev *dev);
573 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
574 unsigned int cmd, void *arg);
577 /* ----------------------------------------------------------- */
578 /* saa7134-video.c */
580 extern struct video_device saa7134_video_template;
581 extern struct video_device saa7134_radio_template;
583 int saa7134_common_ioctl(struct saa7134_dev *dev,
584 unsigned int cmd, void *arg);
586 int saa7134_video_init1(struct saa7134_dev *dev);
587 int saa7134_video_init2(struct saa7134_dev *dev);
588 int saa7134_video_fini(struct saa7134_dev *dev);
589 void saa7134_irq_video_intl(struct saa7134_dev *dev);
590 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status);
593 /* ----------------------------------------------------------- */
596 #define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
598 extern struct videobuf_queue_ops saa7134_ts_qops;
600 int saa7134_ts_init1(struct saa7134_dev *dev);
601 int saa7134_ts_fini(struct saa7134_dev *dev);
602 void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status);
604 int saa7134_ts_register(struct saa7134_mpeg_ops *ops);
605 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops);
607 /* ----------------------------------------------------------- */
610 extern struct videobuf_queue_ops saa7134_vbi_qops;
611 extern struct video_device saa7134_vbi_template;
613 int saa7134_vbi_init1(struct saa7134_dev *dev);
614 int saa7134_vbi_fini(struct saa7134_dev *dev);
615 void saa7134_irq_vbi_done(struct saa7134_dev *dev, unsigned long status);
618 /* ----------------------------------------------------------- */
619 /* saa7134-tvaudio.c */
621 int saa7134_tvaudio_rx2mode(u32 rx);
623 void saa7134_tvaudio_setmute(struct saa7134_dev *dev);
624 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
625 struct saa7134_input *in);
626 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level);
627 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev);
629 int saa7134_tvaudio_init2(struct saa7134_dev *dev);
630 int saa7134_tvaudio_fini(struct saa7134_dev *dev);
631 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev);
633 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value);
635 /* ----------------------------------------------------------- */
638 extern struct file_operations saa7134_dsp_fops;
639 extern struct file_operations saa7134_mixer_fops;
641 int saa7134_oss_init1(struct saa7134_dev *dev);
642 int saa7134_oss_fini(struct saa7134_dev *dev);
643 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status);
645 /* ----------------------------------------------------------- */
646 /* saa7134-input.c */
648 int saa7134_input_init1(struct saa7134_dev *dev);
649 void saa7134_input_fini(struct saa7134_dev *dev);
650 void saa7134_input_irq(struct saa7134_dev *dev);
652 int alsa_card_saa7134_create(struct saa7134_dev *saadev);
653 void alsa_card_saa7134_exit(void);
654 void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status);