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/videodev2.h>
28 #include <linux/kdev_t.h>
29 #include <linux/input.h>
30 #include <linux/notifier.h>
31 #include <linux/delay.h>
35 #include <media/tuner.h>
36 #include <media/audiochip.h>
37 #include <media/ir-common.h>
38 #include <media/ir-kbd-i2c.h>
39 #include <media/video-buf.h>
40 #include <media/video-buf-dvb.h>
50 #include <sound/driver.h>
51 #include <sound/core.h>
52 #include <sound/pcm.h>
54 /* ----------------------------------------------------------- */
57 enum saa7134_tvaudio_mode
{
59 TVAUDIO_FM_BG_STEREO
= 2,
60 TVAUDIO_FM_SAT_STEREO
= 3,
61 TVAUDIO_FM_K_STEREO
= 4,
66 enum saa7134_audio_in
{
73 enum saa7134_video_out
{
77 /* ----------------------------------------------------------- */
80 struct saa7134_tvnorm
{
85 unsigned int sync_control
;
86 unsigned int luma_control
;
87 unsigned int chroma_ctrl1
;
88 unsigned int chroma_gain
;
89 unsigned int chroma_ctrl2
;
90 unsigned int vgate_misc
;
95 unsigned int video_v_start
;
96 unsigned int video_v_stop
;
97 unsigned int vbi_v_start_0
;
98 unsigned int vbi_v_stop_0
;
99 unsigned int src_timing
;
100 unsigned int vbi_v_start_1
;
103 struct saa7134_tvaudio
{
106 enum saa7134_tvaudio_mode mode
;
111 struct saa7134_format
{
116 unsigned int vshift
; /* vertical downsampling (for planar yuv) */
117 unsigned int hshift
; /* horizontal downsampling (for planar yuv) */
118 unsigned int bswap
:1;
119 unsigned int wswap
:1;
121 unsigned int planar
:1;
122 unsigned int uvswap
:1;
125 /* ----------------------------------------------------------- */
126 /* card configuration */
128 #define SAA7134_BOARD_NOAUTO UNSET
129 #define SAA7134_BOARD_UNKNOWN 0
130 #define SAA7134_BOARD_PROTEUS_PRO 1
131 #define SAA7134_BOARD_FLYVIDEO3000 2
132 #define SAA7134_BOARD_FLYVIDEO2000 3
133 #define SAA7134_BOARD_EMPRESS 4
134 #define SAA7134_BOARD_MONSTERTV 5
135 #define SAA7134_BOARD_MD9717 6
136 #define SAA7134_BOARD_TVSTATION_RDS 7
137 #define SAA7134_BOARD_CINERGY400 8
138 #define SAA7134_BOARD_MD5044 9
139 #define SAA7134_BOARD_KWORLD 10
140 #define SAA7134_BOARD_CINERGY600 11
141 #define SAA7134_BOARD_MD7134 12
142 #define SAA7134_BOARD_TYPHOON_90031 13
143 #define SAA7134_BOARD_ELSA 14
144 #define SAA7134_BOARD_ELSA_500TV 15
145 #define SAA7134_BOARD_ASUSTeK_TVFM7134 16
146 #define SAA7134_BOARD_VA1000POWER 17
147 #define SAA7134_BOARD_BMK_MPEX_NOTUNER 18
148 #define SAA7134_BOARD_VIDEOMATE_TV 19
149 #define SAA7134_BOARD_CRONOS_PLUS 20
150 #define SAA7134_BOARD_10MOONSTVMASTER 21
151 #define SAA7134_BOARD_MD2819 22
152 #define SAA7134_BOARD_BMK_MPEX_TUNER 23
153 #define SAA7134_BOARD_TVSTATION_DVR 24
154 #define SAA7134_BOARD_ASUSTEK_TVFM7133 25
155 #define SAA7134_BOARD_PINNACLE_PCTV_STEREO 26
156 #define SAA7134_BOARD_MANLI_MTV002 27
157 #define SAA7134_BOARD_MANLI_MTV001 28
158 #define SAA7134_BOARD_TG3000TV 29
159 #define SAA7134_BOARD_ECS_TVP3XP 30
160 #define SAA7134_BOARD_ECS_TVP3XP_4CB5 31
161 #define SAA7134_BOARD_AVACSSMARTTV 32
162 #define SAA7134_BOARD_AVERMEDIA_DVD_EZMAKER 33
163 #define SAA7134_BOARD_NOVAC_PRIMETV7133 34
164 #define SAA7134_BOARD_AVERMEDIA_STUDIO_305 35
165 #define SAA7134_BOARD_UPMOST_PURPLE_TV 36
166 #define SAA7134_BOARD_ITEMS_MTV005 37
167 #define SAA7134_BOARD_CINERGY200 38
168 #define SAA7134_BOARD_FLYTVPLATINUM_MINI 39
169 #define SAA7134_BOARD_VIDEOMATE_TV_PVR 40
170 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUS 41
171 #define SAA7134_BOARD_SABRENT_SBTTVFM 42
172 #define SAA7134_BOARD_ZOLID_XPERT_TV7134 43
173 #define SAA7134_BOARD_EMPIRE_PCI_TV_RADIO_LE 44
174 #define SAA7134_BOARD_AVERMEDIA_STUDIO_307 45
175 #define SAA7134_BOARD_AVERMEDIA_CARDBUS 46
176 #define SAA7134_BOARD_CINERGY400_CARDBUS 47
177 #define SAA7134_BOARD_CINERGY600_MK3 48
178 #define SAA7134_BOARD_VIDEOMATE_GOLD_PLUS 49
179 #define SAA7134_BOARD_PINNACLE_300I_DVBT_PAL 50
180 #define SAA7134_BOARD_PROVIDEO_PV952 51
181 #define SAA7134_BOARD_AVERMEDIA_305 52
182 #define SAA7134_BOARD_ASUSTeK_TVFM7135 53
183 #define SAA7134_BOARD_FLYTVPLATINUM_FM 54
184 #define SAA7134_BOARD_FLYDVBTDUO 55
185 #define SAA7134_BOARD_AVERMEDIA_307 56
186 #define SAA7134_BOARD_AVERMEDIA_GO_007_FM 57
187 #define SAA7134_BOARD_ADS_INSTANT_TV 58
188 #define SAA7134_BOARD_KWORLD_VSTREAM_XPERT 59
189 #define SAA7134_BOARD_THYPHOON_DVBT_DUO_CARDBUS 60
190 #define SAA7134_BOARD_PHILIPS_TOUGH 61
191 #define SAA7134_BOARD_VIDEOMATE_TV_GOLD_PLUSII 62
192 #define SAA7134_BOARD_KWORLD_XPERT 63
193 #define SAA7134_BOARD_FLYTV_DIGIMATRIX 64
194 #define SAA7134_BOARD_KWORLD_TERMINATOR 65
195 #define SAA7134_BOARD_YUAN_TUN900 66
196 #define SAA7134_BOARD_BEHOLD_409FM 67
197 #define SAA7134_BOARD_GOTVIEW_7135 68
198 #define SAA7134_BOARD_PHILIPS_EUROPA 69
199 #define SAA7134_BOARD_VIDEOMATE_DVBT_300 70
200 #define SAA7134_BOARD_VIDEOMATE_DVBT_200 71
201 #define SAA7134_BOARD_RTD_VFG7350 72
202 #define SAA7134_BOARD_RTD_VFG7330 73
203 #define SAA7134_BOARD_FLYTVPLATINUM_MINI2 74
204 #define SAA7134_BOARD_AVERMEDIA_AVERTVHD_A180 75
205 #define SAA7134_BOARD_MONSTERTV_MOBILE 76
206 #define SAA7134_BOARD_PINNACLE_PCTV_110i 77
207 #define SAA7134_BOARD_ASUSTeK_P7131_DUAL 78
208 #define SAA7134_BOARD_SEDNA_PC_TV_CARDBUS 79
209 #define SAA7134_BOARD_ASUSTEK_DIGIMATRIX_TV 80
210 #define SAA7134_BOARD_PHILIPS_TIGER 81
211 #define SAA7134_BOARD_MSI_TVATANYWHERE_PLUS 82
213 #define SAA7134_MAXBOARDS 8
214 #define SAA7134_INPUT_MAX 8
216 /* ----------------------------------------------------------- */
217 /* Since we support 2 remote types, lets tell them apart */
219 #define SAA7134_REMOTE_GPIO 1
220 #define SAA7134_REMOTE_I2C 2
222 /* ----------------------------------------------------------- */
223 /* Video Output Port Register Initialization Options */
225 #define SET_T_CODE_POLARITY_NON_INVERTED (1 << 0)
226 #define SET_CLOCK_NOT_DELAYED (1 << 1)
227 #define SET_CLOCK_INVERTED (1 << 2)
228 #define SET_VSYNC_OFF (1 << 3)
230 struct saa7134_input
{
233 enum saa7134_audio_in amux
;
238 enum saa7134_mpeg_type
{
240 SAA7134_MPEG_EMPRESS
,
244 struct saa7134_board
{
246 unsigned int audio_clock
;
248 /* input switching */
249 unsigned int gpiomask
;
250 struct saa7134_input inputs
[SAA7134_INPUT_MAX
];
251 struct saa7134_input radio
;
252 struct saa7134_input mute
;
255 unsigned int tuner_type
;
256 unsigned int radio_type
;
257 unsigned char tuner_addr
;
258 unsigned char radio_addr
;
260 unsigned int tda9887_conf
;
263 enum saa7134_video_out video_out
;
264 enum saa7134_mpeg_type mpeg
;
265 unsigned int vid_port_opts
;
268 #define card_has_radio(dev) (NULL != saa7134_boards[dev->board].radio.name)
269 #define card_is_empress(dev) (SAA7134_MPEG_EMPRESS == saa7134_boards[dev->board].mpeg)
270 #define card_is_dvb(dev) (SAA7134_MPEG_DVB == saa7134_boards[dev->board].mpeg)
271 #define card_has_mpeg(dev) (SAA7134_MPEG_UNUSED != saa7134_boards[dev->board].mpeg)
272 #define card(dev) (saa7134_boards[dev->board])
273 #define card_in(dev,n) (saa7134_boards[dev->board].inputs[n])
275 /* ----------------------------------------------------------- */
276 /* device / file handle status */
278 #define RESOURCE_OVERLAY 1
279 #define RESOURCE_VIDEO 2
280 #define RESOURCE_VBI 4
282 #define INTERLACE_AUTO 0
283 #define INTERLACE_ON 1
284 #define INTERLACE_OFF 2
286 #define BUFFER_TIMEOUT (HZ/2) /* 0.5 seconds */
291 /* saa7134 page table */
292 struct saa7134_pgtable
{
298 /* tvaudio thread status */
299 struct saa7134_thread
{
301 struct completion exit
;
302 wait_queue_head_t wq
;
303 unsigned int shutdown
;
309 /* buffer for one video/vbi/ts frame */
311 /* common v4l buffer stuff -- must be first */
312 struct videobuf_buffer vb
;
314 /* saa7134 specific */
315 struct saa7134_format
*fmt
;
316 unsigned int top_seen
;
317 int (*activate
)(struct saa7134_dev
*dev
,
318 struct saa7134_buf
*buf
,
319 struct saa7134_buf
*next
);
322 struct saa7134_pgtable
*pt
;
325 struct saa7134_dmaqueue
{
326 struct saa7134_dev
*dev
;
327 struct saa7134_buf
*curr
;
328 struct list_head queue
;
329 struct timer_list timeout
;
330 unsigned int need_two
;
333 /* video filehandle status */
335 struct saa7134_dev
*dev
;
337 enum v4l2_buf_type type
;
338 unsigned int resources
;
339 #ifdef VIDIOC_G_PRIORITY
340 enum v4l2_priority prio
;
344 struct v4l2_window win
;
345 struct v4l2_clip clips
[8];
349 struct saa7134_format
*fmt
;
350 unsigned int width
,height
;
351 struct videobuf_queue cap
;
352 struct saa7134_pgtable pt_cap
;
355 struct videobuf_queue vbi
;
356 struct saa7134_pgtable pt_vbi
;
359 /* dmasound dsp status */
360 struct saa7134_dmasound
{
361 struct semaphore lock
;
364 unsigned int users_dsp
;
367 enum saa7134_audio_in input
;
375 unsigned int channels
;
376 unsigned int recording_on
;
377 unsigned int dma_running
;
379 unsigned int blksize
;
380 unsigned int bufsize
;
381 struct saa7134_pgtable pt
;
382 struct videobuf_dmabuf dma
;
383 wait_queue_head_t wq
;
384 unsigned int dma_blk
;
385 unsigned int read_offset
;
386 unsigned int read_count
;
388 snd_pcm_substream_t
*substream
;
393 struct input_dev
*dev
;
394 struct ir_input_state ir
;
402 struct timer_list timer
;
408 struct saa7134_pgtable pt_ts
;
414 struct saa7134_mpeg_ops
{
415 enum saa7134_mpeg_type type
;
416 struct list_head next
;
417 int (*init
)(struct saa7134_dev
*dev
);
418 int (*fini
)(struct saa7134_dev
*dev
);
419 void (*signal_change
)(struct saa7134_dev
*dev
);
422 /* global device status */
424 struct list_head devlist
;
425 struct semaphore lock
;
427 #ifdef VIDIOC_G_PRIORITY
428 struct v4l2_prio_state prio
;
431 /* various device info */
432 unsigned int resources
;
433 struct video_device
*video_dev
;
434 struct video_device
*radio_dev
;
435 struct video_device
*vbi_dev
;
436 struct saa7134_dmasound dmasound
;
438 /* infrared remote */
440 struct saa7134_ir
*remote
;
446 unsigned char pci_rev
,pci_lat
;
447 __u32 __iomem
*lmmio
;
452 unsigned int tuner_type
;
453 unsigned int radio_type
;
454 unsigned char tuner_addr
;
455 unsigned char radio_addr
;
457 unsigned int tda9887_conf
;
458 unsigned int gpio_value
;
461 struct i2c_adapter i2c_adap
;
462 struct i2c_client i2c_client
;
463 unsigned char eedata
[128];
466 struct v4l2_framebuffer ovbuf
;
467 struct saa7134_format
*ovfmt
;
468 unsigned int ovenable
;
469 enum v4l2_field ovfield
;
471 /* video+ts+vbi capture */
472 struct saa7134_dmaqueue video_q
;
473 struct saa7134_dmaqueue vbi_q
;
474 unsigned int video_fieldcount
;
475 unsigned int vbi_fieldcount
;
477 /* various v4l controls */
478 struct saa7134_tvnorm
*tvnorm
; /* video */
479 struct saa7134_tvaudio
*tvaudio
;
480 unsigned int ctl_input
;
486 int ctl_mute
; /* audio */
488 int ctl_invert
; /* private */
495 struct v4l2_rect crop_bounds
;
496 struct v4l2_rect crop_defrect
;
497 struct v4l2_rect crop_current
;
499 /* other global state info */
500 unsigned int automute
;
501 struct saa7134_thread thread
;
502 struct saa7134_input
*input
;
503 struct saa7134_input
*hw_input
;
504 unsigned int hw_mute
;
509 struct saa7134_ts ts
;
510 struct saa7134_dmaqueue ts_q
;
511 struct saa7134_mpeg_ops
*mops
;
513 /* SAA7134_MPEG_EMPRESS only */
514 struct video_device
*empress_dev
;
515 struct videobuf_queue empress_tsq
;
516 unsigned int empress_users
;
517 struct work_struct empress_workqueue
;
520 /* SAA7134_MPEG_DVB only */
521 struct videobuf_dvb dvb
;
524 /* ----------------------------------------------------------- */
526 #define saa_readl(reg) readl(dev->lmmio + (reg))
527 #define saa_writel(reg,value) writel((value), dev->lmmio + (reg));
528 #define saa_andorl(reg,mask,value) \
529 writel((readl(dev->lmmio+(reg)) & ~(mask)) |\
530 ((value) & (mask)), dev->lmmio+(reg))
531 #define saa_setl(reg,bit) saa_andorl((reg),(bit),(bit))
532 #define saa_clearl(reg,bit) saa_andorl((reg),(bit),0)
534 #define saa_readb(reg) readb(dev->bmmio + (reg))
535 #define saa_writeb(reg,value) writeb((value), dev->bmmio + (reg));
536 #define saa_andorb(reg,mask,value) \
537 writeb((readb(dev->bmmio+(reg)) & ~(mask)) |\
538 ((value) & (mask)), dev->bmmio+(reg))
539 #define saa_setb(reg,bit) saa_andorb((reg),(bit),(bit))
540 #define saa_clearb(reg,bit) saa_andorb((reg),(bit),0)
542 #define saa_wait(us) { udelay(us); }
544 /* ----------------------------------------------------------- */
547 extern struct list_head saa7134_devlist
;
549 void saa7134_print_ioctl(char *name
, unsigned int cmd
);
550 void saa7134_track_gpio(struct saa7134_dev
*dev
, char *msg
);
552 #define SAA7134_PGTABLE_SIZE 4096
554 int saa7134_pgtable_alloc(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
);
555 int saa7134_pgtable_build(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
,
556 struct scatterlist
*list
, unsigned int length
,
557 unsigned int startpage
);
558 void saa7134_pgtable_free(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
);
560 int saa7134_buffer_count(unsigned int size
, unsigned int count
);
561 int saa7134_buffer_startpage(struct saa7134_buf
*buf
);
562 unsigned long saa7134_buffer_base(struct saa7134_buf
*buf
);
564 int saa7134_buffer_queue(struct saa7134_dev
*dev
, struct saa7134_dmaqueue
*q
,
565 struct saa7134_buf
*buf
);
566 void saa7134_buffer_finish(struct saa7134_dev
*dev
, struct saa7134_dmaqueue
*q
,
568 void saa7134_buffer_next(struct saa7134_dev
*dev
, struct saa7134_dmaqueue
*q
);
569 void saa7134_buffer_timeout(unsigned long data
);
570 void saa7134_dma_free(struct saa7134_dev
*dev
,struct saa7134_buf
*buf
);
572 int saa7134_set_dmabits(struct saa7134_dev
*dev
);
574 extern int (*dmasound_init
)(struct saa7134_dev
*dev
);
575 extern int (*dmasound_exit
)(struct saa7134_dev
*dev
);
578 /* ----------------------------------------------------------- */
579 /* saa7134-cards.c */
581 extern struct saa7134_board saa7134_boards
[];
582 extern const unsigned int saa7134_bcount
;
583 extern struct pci_device_id __devinitdata saa7134_pci_tbl
[];
585 extern int saa7134_board_init1(struct saa7134_dev
*dev
);
586 extern int saa7134_board_init2(struct saa7134_dev
*dev
);
589 /* ----------------------------------------------------------- */
592 int saa7134_i2c_register(struct saa7134_dev
*dev
);
593 int saa7134_i2c_unregister(struct saa7134_dev
*dev
);
594 void saa7134_i2c_call_clients(struct saa7134_dev
*dev
,
595 unsigned int cmd
, void *arg
);
598 /* ----------------------------------------------------------- */
599 /* saa7134-video.c */
601 extern struct video_device saa7134_video_template
;
602 extern struct video_device saa7134_radio_template
;
604 int saa7134_common_ioctl(struct saa7134_dev
*dev
,
605 unsigned int cmd
, void *arg
);
607 int saa7134_video_init1(struct saa7134_dev
*dev
);
608 int saa7134_video_init2(struct saa7134_dev
*dev
);
609 int saa7134_video_fini(struct saa7134_dev
*dev
);
610 void saa7134_irq_video_intl(struct saa7134_dev
*dev
);
611 void saa7134_irq_video_done(struct saa7134_dev
*dev
, unsigned long status
);
614 /* ----------------------------------------------------------- */
617 #define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
619 extern struct videobuf_queue_ops saa7134_ts_qops
;
621 int saa7134_ts_init1(struct saa7134_dev
*dev
);
622 int saa7134_ts_fini(struct saa7134_dev
*dev
);
623 void saa7134_irq_ts_done(struct saa7134_dev
*dev
, unsigned long status
);
625 int saa7134_ts_register(struct saa7134_mpeg_ops
*ops
);
626 void saa7134_ts_unregister(struct saa7134_mpeg_ops
*ops
);
628 /* ----------------------------------------------------------- */
631 extern struct videobuf_queue_ops saa7134_vbi_qops
;
632 extern struct video_device saa7134_vbi_template
;
634 int saa7134_vbi_init1(struct saa7134_dev
*dev
);
635 int saa7134_vbi_fini(struct saa7134_dev
*dev
);
636 void saa7134_irq_vbi_done(struct saa7134_dev
*dev
, unsigned long status
);
639 /* ----------------------------------------------------------- */
640 /* saa7134-tvaudio.c */
642 int saa7134_tvaudio_rx2mode(u32 rx
);
644 void saa7134_tvaudio_setmute(struct saa7134_dev
*dev
);
645 void saa7134_tvaudio_setinput(struct saa7134_dev
*dev
,
646 struct saa7134_input
*in
);
647 void saa7134_tvaudio_setvolume(struct saa7134_dev
*dev
, int level
);
648 int saa7134_tvaudio_getstereo(struct saa7134_dev
*dev
);
650 int saa7134_tvaudio_init2(struct saa7134_dev
*dev
);
651 int saa7134_tvaudio_fini(struct saa7134_dev
*dev
);
652 int saa7134_tvaudio_do_scan(struct saa7134_dev
*dev
);
654 int saa_dsp_writel(struct saa7134_dev
*dev
, int reg
, u32 value
);
656 /* ----------------------------------------------------------- */
659 extern struct file_operations saa7134_dsp_fops
;
660 extern struct file_operations saa7134_mixer_fops
;
662 int saa7134_oss_init1(struct saa7134_dev
*dev
);
663 int saa7134_oss_fini(struct saa7134_dev
*dev
);
664 void saa7134_irq_oss_done(struct saa7134_dev
*dev
, unsigned long status
);
666 /* ----------------------------------------------------------- */
667 /* saa7134-input.c */
669 int saa7134_input_init1(struct saa7134_dev
*dev
);
670 void saa7134_input_fini(struct saa7134_dev
*dev
);
671 void saa7134_input_irq(struct saa7134_dev
*dev
);
672 void saa7134_set_i2c_ir(struct saa7134_dev
*dev
, struct IR_i2c
*ir
);