2 * $Id: saa7134-core.c,v 1.28 2005/02/22 09:56:29 kraxel Exp $
4 * device driver for philips saa7134 based TV cards
7 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/kmod.h>
32 #include <linux/sound.h>
33 #include <linux/interrupt.h>
34 #include <linux/delay.h>
36 #include "saa7134-reg.h"
39 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
40 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
41 MODULE_LICENSE("GPL");
43 /* ------------------------------------------------------------------ */
45 static unsigned int irq_debug
= 0;
46 module_param(irq_debug
, int, 0644);
47 MODULE_PARM_DESC(irq_debug
,"enable debug messages [IRQ handler]");
49 static unsigned int core_debug
= 0;
50 module_param(core_debug
, int, 0644);
51 MODULE_PARM_DESC(core_debug
,"enable debug messages [core]");
53 static unsigned int gpio_tracking
= 0;
54 module_param(gpio_tracking
, int, 0644);
55 MODULE_PARM_DESC(gpio_tracking
,"enable debug messages [gpio]");
57 static unsigned int oss
= 0;
58 module_param(oss
, int, 0444);
59 MODULE_PARM_DESC(oss
,"register oss devices (default: no)");
61 static unsigned int latency
= UNSET
;
62 module_param(latency
, int, 0444);
63 MODULE_PARM_DESC(latency
,"pci latency timer");
65 static unsigned int video_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
66 static unsigned int vbi_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
67 static unsigned int radio_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
68 static unsigned int dsp_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
69 static unsigned int mixer_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
70 static unsigned int tuner
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
71 static unsigned int card
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
73 module_param_array(video_nr
, int, NULL
, 0444);
74 module_param_array(vbi_nr
, int, NULL
, 0444);
75 module_param_array(radio_nr
, int, NULL
, 0444);
76 module_param_array(dsp_nr
, int, NULL
, 0444);
77 module_param_array(mixer_nr
, int, NULL
, 0444);
78 module_param_array(tuner
, int, NULL
, 0444);
79 module_param_array(card
, int, NULL
, 0444);
81 MODULE_PARM_DESC(video_nr
, "video device number");
82 MODULE_PARM_DESC(vbi_nr
, "vbi device number");
83 MODULE_PARM_DESC(radio_nr
, "radio device number");
84 MODULE_PARM_DESC(dsp_nr
, "oss dsp device number");
85 MODULE_PARM_DESC(mixer_nr
, "oss mixer device number");
86 MODULE_PARM_DESC(tuner
, "tuner type");
87 MODULE_PARM_DESC(card
, "card type");
89 static DECLARE_MUTEX(devlist_lock
);
90 LIST_HEAD(saa7134_devlist
);
91 static LIST_HEAD(mops_list
);
92 static unsigned int saa7134_devcount
;
94 #define dprintk(fmt, arg...) if (core_debug) \
95 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
97 /* ------------------------------------------------------------------ */
98 /* debug help functions */
100 static const char *v4l1_ioctls
[] = {
101 "0", "GCAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
102 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
103 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
104 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
105 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
106 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
108 static const char *v4l2_ioctls
[] = {
109 "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
110 "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
111 "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
112 "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
113 "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
114 "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
115 "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
116 "44", "45", "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
117 "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
120 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
122 static const char *osspcm_ioctls
[] = {
123 "RESET", "SYNC", "SPEED", "STEREO", "GETBLKSIZE", "SETFMT",
124 "CHANNELS", "?", "POST", "SUBDIVIDE", "SETFRAGMENT", "GETFMTS",
125 "GETOSPACE", "GETISPACE", "NONBLOCK", "GETCAPS", "GET/SETTRIGGER",
126 "GETIPTR", "GETOPTR", "MAPINBUF", "MAPOUTBUF", "SETSYNCRO",
127 "SETDUPLEX", "GETODELAY"
129 #define OSSPCM_IOCTLS ARRAY_SIZE(v4l2_ioctls)
131 void saa7134_print_ioctl(char *name
, unsigned int cmd
)
135 switch (_IOC_DIR(cmd
)) {
136 case _IOC_NONE
: dir
= "--"; break;
137 case _IOC_READ
: dir
= "r-"; break;
138 case _IOC_WRITE
: dir
= "-w"; break;
139 case _IOC_READ
| _IOC_WRITE
: dir
= "rw"; break;
140 default: dir
= "??"; break;
142 switch (_IOC_TYPE(cmd
)) {
144 printk(KERN_DEBUG
"%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
145 name
, cmd
, dir
, (_IOC_NR(cmd
) < V4L1_IOCTLS
) ?
146 v4l1_ioctls
[_IOC_NR(cmd
)] : "???");
149 printk(KERN_DEBUG
"%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
150 name
, cmd
, dir
, (_IOC_NR(cmd
) < V4L2_IOCTLS
) ?
151 v4l2_ioctls
[_IOC_NR(cmd
)] : "???");
154 printk(KERN_DEBUG
"%s: ioctl 0x%08x (oss dsp, %s, SNDCTL_DSP_%s)\n",
155 name
, cmd
, dir
, (_IOC_NR(cmd
) < OSSPCM_IOCTLS
) ?
156 osspcm_ioctls
[_IOC_NR(cmd
)] : "???");
159 printk(KERN_DEBUG
"%s: ioctl 0x%08x (oss mixer, %s, #%d)\n",
160 name
, cmd
, dir
, _IOC_NR(cmd
));
163 printk(KERN_DEBUG
"%s: ioctl 0x%08x (???, %s, #%d)\n",
164 name
, cmd
, dir
, _IOC_NR(cmd
));
168 void saa7134_track_gpio(struct saa7134_dev
*dev
, char *msg
)
170 unsigned long mode
,status
;
174 /* rising SAA7134_GPIO_GPRESCAN reads the status */
175 saa_andorb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
,0);
176 saa_andorb(SAA7134_GPIO_GPMODE3
,SAA7134_GPIO_GPRESCAN
,SAA7134_GPIO_GPRESCAN
);
177 mode
= saa_readl(SAA7134_GPIO_GPMODE0
>> 2) & 0xfffffff;
178 status
= saa_readl(SAA7134_GPIO_GPSTATUS0
>> 2) & 0xfffffff;
180 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
181 dev
->name
, mode
, (~mode
) & status
, mode
& status
, msg
);
184 /* ------------------------------------------------------------------ */
187 static char *dec1_bits
[8] = {
188 "DCSTD0", "DCSCT1", "WIPA", "GLIMB",
189 "GLIMT", "SLTCA", "HLCK"
191 static char *dec2_bits
[8] = {
192 "RDCAP", "COPRO", "COLSTR", "TYPE3",
193 NULL
, "FIDT", "HLVLN", "INTL"
195 static char *scale1_bits
[8] = {
196 "VID_A", "VBI_A", NULL
, NULL
, "VID_B", "VBI_B"
198 static char *scale2_bits
[8] = {
199 "TRERR", "CFERR", "LDERR", "WASRST",
200 "FIDSCI", "FIDSCO", "D6^D5", "TASK"
203 static void dump_statusreg(struct saa7134_dev
*dev
, int reg
,
204 char *regname
, char **bits
)
208 value
= saa_readb(reg
);
209 printk(KERN_DEBUG
"%s: %s:", dev
->name
, regname
);
210 for (i
= 7; i
>= 0; i
--) {
213 printk(" %s=%d", bits
[i
], (value
& (1 << i
)) ? 1 : 0);
218 static void dump_statusregs(struct saa7134_dev
*dev
)
220 dump_statusreg(dev
,SAA7134_STATUS_VIDEO1
,"dec1",dec1_bits
);
221 dump_statusreg(dev
,SAA7134_STATUS_VIDEO2
,"dec2",dec2_bits
);
222 dump_statusreg(dev
,SAA7134_SCALER_STATUS0
,"scale0",scale1_bits
);
223 dump_statusreg(dev
,SAA7134_SCALER_STATUS1
,"scale1",scale2_bits
);
227 /* ----------------------------------------------------------- */
228 /* delayed request_module */
230 #ifdef CONFIG_MODULES
232 static int need_empress
;
235 static int pending_call(struct notifier_block
*self
, unsigned long state
,
238 if (module
!= THIS_MODULE
|| state
!= MODULE_STATE_LIVE
)
242 request_module("saa7134-empress");
244 request_module("saa7134-dvb");
248 static int pending_registered
;
249 static struct notifier_block pending_notifier
= {
250 .notifier_call
= pending_call
,
253 static void request_module_depend(char *name
, int *flag
)
255 switch (THIS_MODULE
->state
) {
256 case MODULE_STATE_COMING
:
257 if (!pending_registered
) {
258 register_module_notifier(&pending_notifier
);
259 pending_registered
= 1;
263 case MODULE_STATE_LIVE
:
264 request_module(name
);
274 #define request_module_depend(name,flag)
276 #endif /* CONFIG_MODULES */
278 /* ------------------------------------------------------------------ */
280 /* nr of (saa7134-)pages for the given buffer size */
281 static int saa7134_buffer_pages(int size
)
283 size
= PAGE_ALIGN(size
);
284 size
+= PAGE_SIZE
; /* for non-page-aligned buffers */
289 /* calc max # of buffers from size (must not exceed the 4MB virtual
290 * address space per DMA channel) */
291 int saa7134_buffer_count(unsigned int size
, unsigned int count
)
293 unsigned int maxcount
;
295 maxcount
= 1024 / saa7134_buffer_pages(size
);
296 if (count
> maxcount
)
301 int saa7134_buffer_startpage(struct saa7134_buf
*buf
)
303 return saa7134_buffer_pages(buf
->vb
.bsize
) * buf
->vb
.i
;
306 unsigned long saa7134_buffer_base(struct saa7134_buf
*buf
)
310 base
= saa7134_buffer_startpage(buf
) * 4096;
311 base
+= buf
->vb
.dma
.sglist
[0].offset
;
315 /* ------------------------------------------------------------------ */
317 int saa7134_pgtable_alloc(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
322 cpu
= pci_alloc_consistent(pci
, SAA7134_PGTABLE_SIZE
, &dma_addr
);
325 pt
->size
= SAA7134_PGTABLE_SIZE
;
331 int saa7134_pgtable_build(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
,
332 struct scatterlist
*list
, unsigned int length
,
333 unsigned int startpage
)
338 BUG_ON(NULL
== pt
|| NULL
== pt
->cpu
);
340 ptr
= pt
->cpu
+ startpage
;
341 for (i
= 0; i
< length
; i
++, list
++)
342 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr
++)
343 *ptr
= sg_dma_address(list
) - list
->offset
;
347 void saa7134_pgtable_free(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
351 pci_free_consistent(pci
, pt
->size
, pt
->cpu
, pt
->dma
);
355 /* ------------------------------------------------------------------ */
357 void saa7134_dma_free(struct saa7134_dev
*dev
,struct saa7134_buf
*buf
)
362 videobuf_waiton(&buf
->vb
,0,0);
363 videobuf_dma_pci_unmap(dev
->pci
, &buf
->vb
.dma
);
364 videobuf_dma_free(&buf
->vb
.dma
);
365 buf
->vb
.state
= STATE_NEEDS_INIT
;
368 /* ------------------------------------------------------------------ */
370 int saa7134_buffer_queue(struct saa7134_dev
*dev
,
371 struct saa7134_dmaqueue
*q
,
372 struct saa7134_buf
*buf
)
374 struct saa7134_buf
*next
= NULL
;
376 assert_spin_locked(&dev
->slock
);
377 dprintk("buffer_queue %p\n",buf
);
378 if (NULL
== q
->curr
) {
381 buf
->activate(dev
,buf
,NULL
);
382 } else if (list_empty(&q
->queue
)) {
383 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
384 buf
->vb
.state
= STATE_QUEUED
;
386 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
389 buf
->activate(dev
,buf
,next
);
392 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
393 buf
->vb
.state
= STATE_QUEUED
;
398 void saa7134_buffer_finish(struct saa7134_dev
*dev
,
399 struct saa7134_dmaqueue
*q
,
402 assert_spin_locked(&dev
->slock
);
403 dprintk("buffer_finish %p\n",q
->curr
);
405 /* finish current buffer */
406 q
->curr
->vb
.state
= state
;
407 do_gettimeofday(&q
->curr
->vb
.ts
);
408 wake_up(&q
->curr
->vb
.done
);
412 void saa7134_buffer_next(struct saa7134_dev
*dev
,
413 struct saa7134_dmaqueue
*q
)
415 struct saa7134_buf
*buf
,*next
= NULL
;
417 assert_spin_locked(&dev
->slock
);
418 BUG_ON(NULL
!= q
->curr
);
420 if (!list_empty(&q
->queue
)) {
421 /* activate next one from queue */
422 buf
= list_entry(q
->queue
.next
,struct saa7134_buf
,vb
.queue
);
423 dprintk("buffer_next %p [prev=%p/next=%p]\n",
424 buf
,q
->queue
.prev
,q
->queue
.next
);
425 list_del(&buf
->vb
.queue
);
426 if (!list_empty(&q
->queue
))
427 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
430 buf
->activate(dev
,buf
,next
);
431 dprintk("buffer_next #2 prev=%p/next=%p\n",
432 q
->queue
.prev
,q
->queue
.next
);
434 /* nothing to do -- just stop DMA */
435 dprintk("buffer_next %p\n",NULL
);
436 saa7134_set_dmabits(dev
);
437 del_timer(&q
->timeout
);
441 void saa7134_buffer_timeout(unsigned long data
)
443 struct saa7134_dmaqueue
*q
= (struct saa7134_dmaqueue
*)data
;
444 struct saa7134_dev
*dev
= q
->dev
;
447 spin_lock_irqsave(&dev
->slock
,flags
);
449 /* try to reset the hardware (SWRST) */
450 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
451 saa_writeb(SAA7134_REGION_ENABLE
, 0x80);
452 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
454 /* flag current buffer as failed,
455 try to start over with the next one. */
457 dprintk("timeout on %p\n",q
->curr
);
458 saa7134_buffer_finish(dev
,q
,STATE_ERROR
);
460 saa7134_buffer_next(dev
,q
);
461 spin_unlock_irqrestore(&dev
->slock
,flags
);
464 /* ------------------------------------------------------------------ */
466 int saa7134_set_dmabits(struct saa7134_dev
*dev
)
468 u32 split
, task
=0, ctrl
=0, irq
=0;
469 enum v4l2_field cap
= V4L2_FIELD_ANY
;
470 enum v4l2_field ov
= V4L2_FIELD_ANY
;
472 assert_spin_locked(&dev
->slock
);
474 /* video capture -- dma 0 + video task A */
475 if (dev
->video_q
.curr
) {
477 ctrl
|= SAA7134_MAIN_CTRL_TE0
;
478 irq
|= SAA7134_IRQ1_INTE_RA0_1
|
479 SAA7134_IRQ1_INTE_RA0_0
;
480 cap
= dev
->video_q
.curr
->vb
.field
;
483 /* video capture -- dma 1+2 (planar modes) */
484 if (dev
->video_q
.curr
&&
485 dev
->video_q
.curr
->fmt
->planar
) {
486 ctrl
|= SAA7134_MAIN_CTRL_TE4
|
487 SAA7134_MAIN_CTRL_TE5
;
490 /* screen overlay -- dma 0 + video task B */
493 ctrl
|= SAA7134_MAIN_CTRL_TE1
;
497 /* vbi capture -- dma 0 + vbi task A+B */
498 if (dev
->vbi_q
.curr
) {
500 ctrl
|= SAA7134_MAIN_CTRL_TE2
|
501 SAA7134_MAIN_CTRL_TE3
;
502 irq
|= SAA7134_IRQ1_INTE_RA0_7
|
503 SAA7134_IRQ1_INTE_RA0_6
|
504 SAA7134_IRQ1_INTE_RA0_5
|
505 SAA7134_IRQ1_INTE_RA0_4
;
508 /* audio capture -- dma 3 */
509 if (dev
->oss
.dma_running
) {
510 ctrl
|= SAA7134_MAIN_CTRL_TE6
;
511 irq
|= SAA7134_IRQ1_INTE_RA3_1
|
512 SAA7134_IRQ1_INTE_RA3_0
;
515 /* TS capture -- dma 5 */
516 if (dev
->ts_q
.curr
) {
517 ctrl
|= SAA7134_MAIN_CTRL_TE5
;
518 irq
|= SAA7134_IRQ1_INTE_RA2_3
|
519 SAA7134_IRQ1_INTE_RA2_2
|
520 SAA7134_IRQ1_INTE_RA2_1
|
521 SAA7134_IRQ1_INTE_RA2_0
;
524 /* set task conditions + field handling */
525 if (V4L2_FIELD_HAS_BOTH(cap
) || V4L2_FIELD_HAS_BOTH(ov
) || cap
== ov
) {
526 /* default config -- use full frames */
527 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
528 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
529 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x02);
530 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x02);
533 /* split fields between tasks */
534 if (V4L2_FIELD_TOP
== cap
) {
535 /* odd A, even B, repeat */
536 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
537 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0e);
539 /* odd B, even A, repeat */
540 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0e);
541 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
543 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x01);
544 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x01);
549 saa_writeb(SAA7134_REGION_ENABLE
, task
);
550 saa_writel(SAA7134_IRQ1
, irq
);
551 saa_andorl(SAA7134_MAIN_CTRL
,
552 SAA7134_MAIN_CTRL_TE0
|
553 SAA7134_MAIN_CTRL_TE1
|
554 SAA7134_MAIN_CTRL_TE2
|
555 SAA7134_MAIN_CTRL_TE3
|
556 SAA7134_MAIN_CTRL_TE4
|
557 SAA7134_MAIN_CTRL_TE5
|
558 SAA7134_MAIN_CTRL_TE6
,
560 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
561 task
, ctrl
, irq
, split
? "no" : "yes");
566 /* ------------------------------------------------------------------ */
567 /* IRQ handler + helpers */
569 static char *irqbits
[] = {
570 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
571 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
572 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
573 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
575 #define IRQBITS ARRAY_SIZE(irqbits)
577 static void print_irqstatus(struct saa7134_dev
*dev
, int loop
,
578 unsigned long report
, unsigned long status
)
582 printk(KERN_DEBUG
"%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
583 dev
->name
,loop
,jiffies
,report
,status
);
584 for (i
= 0; i
< IRQBITS
; i
++) {
585 if (!(report
& (1 << i
)))
587 printk(" %s",irqbits
[i
]);
589 if (report
& SAA7134_IRQ_REPORT_DONE_RA0
) {
590 printk(" | RA0=%s,%s,%s,%ld",
591 (status
& 0x40) ? "vbi" : "video",
592 (status
& 0x20) ? "b" : "a",
593 (status
& 0x10) ? "odd" : "even",
599 static irqreturn_t
saa7134_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
601 struct saa7134_dev
*dev
= (struct saa7134_dev
*) dev_id
;
602 unsigned long report
,status
;
603 int loop
, handled
= 0;
605 for (loop
= 0; loop
< 10; loop
++) {
606 report
= saa_readl(SAA7134_IRQ_REPORT
);
607 status
= saa_readl(SAA7134_IRQ_STATUS
);
610 printk(KERN_DEBUG
"%s/irq: no (more) work\n",
615 saa_writel(SAA7134_IRQ_REPORT
,report
);
617 print_irqstatus(dev
,loop
,report
,status
);
620 if (report
& SAA7134_IRQ_REPORT_CONF_ERR
)
621 dump_statusregs(dev
);
624 if (report
& SAA7134_IRQ_REPORT_RDCAP
/* _INTL */)
625 saa7134_irq_video_intl(dev
);
627 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
628 (status
& 0x60) == 0)
629 saa7134_irq_video_done(dev
,status
);
631 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
632 (status
& 0x40) == 0x40)
633 saa7134_irq_vbi_done(dev
,status
);
635 if ((report
& SAA7134_IRQ_REPORT_DONE_RA2
) &&
637 saa7134_irq_ts_done(dev
,status
);
639 if ((report
& SAA7134_IRQ_REPORT_DONE_RA3
))
640 saa7134_irq_oss_done(dev
,status
);
642 if ((report
& (SAA7134_IRQ_REPORT_GPIO16
|
643 SAA7134_IRQ_REPORT_GPIO18
)) &&
645 saa7134_input_irq(dev
);
649 print_irqstatus(dev
,loop
,report
,status
);
650 if (report
& SAA7134_IRQ_REPORT_PE
) {
651 /* disable all parity error */
652 printk(KERN_WARNING
"%s/irq: looping -- "
653 "clearing PE (parity error!) enable bit\n",dev
->name
);
654 saa_clearl(SAA7134_IRQ2
,SAA7134_IRQ2_INTE_PE
);
655 } else if (report
& (SAA7134_IRQ_REPORT_GPIO16
|
656 SAA7134_IRQ_REPORT_GPIO18
)) {
657 /* disable gpio IRQs */
658 printk(KERN_WARNING
"%s/irq: looping -- "
659 "clearing GPIO enable bits\n",dev
->name
);
660 saa_clearl(SAA7134_IRQ2
, (SAA7134_IRQ2_INTE_GPIO16
|
661 SAA7134_IRQ2_INTE_GPIO18
));
663 /* disable all irqs */
664 printk(KERN_WARNING
"%s/irq: looping -- "
665 "clearing all enable bits\n",dev
->name
);
666 saa_writel(SAA7134_IRQ1
,0);
667 saa_writel(SAA7134_IRQ2
,0);
672 return IRQ_RETVAL(handled
);
675 /* ------------------------------------------------------------------ */
677 /* early init (no i2c, no irq) */
678 static int saa7134_hwinit1(struct saa7134_dev
*dev
)
680 dprintk("hwinit1\n");
682 saa_writel(SAA7134_IRQ1
, 0);
683 saa_writel(SAA7134_IRQ2
, 0);
684 init_MUTEX(&dev
->lock
);
685 spin_lock_init(&dev
->slock
);
687 saa7134_track_gpio(dev
,"pre-init");
688 saa7134_video_init1(dev
);
689 saa7134_vbi_init1(dev
);
690 if (card_has_mpeg(dev
))
691 saa7134_ts_init1(dev
);
692 saa7134_input_init1(dev
);
694 switch (dev
->pci
->device
) {
695 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
696 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
697 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
698 saa7134_oss_init1(dev
);
702 /* RAM FIFO config */
703 saa_writel(SAA7134_FIFO_SIZE
, 0x08070503);
704 saa_writel(SAA7134_THRESHOULD
,0x02020202);
706 /* enable audio + video processing */
707 saa_writel(SAA7134_MAIN_CTRL
,
708 SAA7134_MAIN_CTRL_VPLLE
|
709 SAA7134_MAIN_CTRL_APLLE
|
710 SAA7134_MAIN_CTRL_EXOSC
|
711 SAA7134_MAIN_CTRL_EVFE1
|
712 SAA7134_MAIN_CTRL_EVFE2
|
713 SAA7134_MAIN_CTRL_ESFE
|
714 SAA7134_MAIN_CTRL_EBADC
|
715 SAA7134_MAIN_CTRL_EBDAC
);
717 /* enable peripheral devices */
718 saa_writeb(SAA7134_SPECIAL_MODE
, 0x01);
720 /* set vertical line numbering start (vbi needs this) */
721 saa_writeb(SAA7134_SOURCE_TIMING2
, 0x20);
726 /* late init (with i2c + irq) */
727 static int saa7134_hwinit2(struct saa7134_dev
*dev
)
729 dprintk("hwinit2\n");
731 saa7134_video_init2(dev
);
732 saa7134_tvaudio_init2(dev
);
735 saa_writel(SAA7134_IRQ1
, 0);
736 saa_writel(SAA7134_IRQ2
, dev
->irq2_mask
);
742 static int saa7134_hwfini(struct saa7134_dev
*dev
)
746 switch (dev
->pci
->device
) {
747 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
748 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
749 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
750 saa7134_oss_fini(dev
);
753 if (card_has_mpeg(dev
))
754 saa7134_ts_fini(dev
);
755 saa7134_input_fini(dev
);
756 saa7134_vbi_fini(dev
);
757 saa7134_video_fini(dev
);
758 saa7134_tvaudio_fini(dev
);
762 static void __devinit
must_configure_manually(void)
768 "saa7134: Congratulations! Your TV card vendor saved a few\n"
769 "saa7134: cents for a eeprom, thus your pci board has no\n"
770 "saa7134: subsystem ID and I can't identify it automatically\n"
772 "saa7134: I feel better now. Ok, here are the good news:\n"
773 "saa7134: You can use the card=<nr> insmod option to specify\n"
774 "saa7134: which board do you have. The list:\n");
775 for (i
= 0; i
< saa7134_bcount
; i
++) {
776 printk(KERN_WARNING
"saa7134: card=%d -> %-40.40s",
777 i
,saa7134_boards
[i
].name
);
778 for (p
= 0; saa7134_pci_tbl
[p
].driver_data
; p
++) {
779 if (saa7134_pci_tbl
[p
].driver_data
!= i
)
782 saa7134_pci_tbl
[p
].subvendor
,
783 saa7134_pci_tbl
[p
].subdevice
);
789 static struct video_device
*vdev_init(struct saa7134_dev
*dev
,
790 struct video_device
*template,
793 struct video_device
*vfd
;
795 vfd
= video_device_alloc();
800 vfd
->dev
= &dev
->pci
->dev
;
801 vfd
->release
= video_device_release
;
802 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)",
803 dev
->name
, type
, saa7134_boards
[dev
->board
].name
);
807 static void saa7134_unregister_video(struct saa7134_dev
*dev
)
809 if (dev
->video_dev
) {
810 if (-1 != dev
->video_dev
->minor
)
811 video_unregister_device(dev
->video_dev
);
813 video_device_release(dev
->video_dev
);
814 dev
->video_dev
= NULL
;
817 if (-1 != dev
->vbi_dev
->minor
)
818 video_unregister_device(dev
->vbi_dev
);
820 video_device_release(dev
->vbi_dev
);
823 if (dev
->radio_dev
) {
824 if (-1 != dev
->radio_dev
->minor
)
825 video_unregister_device(dev
->radio_dev
);
827 video_device_release(dev
->radio_dev
);
828 dev
->radio_dev
= NULL
;
832 static void mpeg_ops_attach(struct saa7134_mpeg_ops
*ops
,
833 struct saa7134_dev
*dev
)
837 if (NULL
!= dev
->mops
)
839 if (saa7134_boards
[dev
->board
].mpeg
!= ops
->type
)
841 err
= ops
->init(dev
);
847 static void mpeg_ops_detach(struct saa7134_mpeg_ops
*ops
,
848 struct saa7134_dev
*dev
)
850 if (NULL
== dev
->mops
)
852 if (dev
->mops
!= ops
)
854 dev
->mops
->fini(dev
);
858 static int __devinit
saa7134_initdev(struct pci_dev
*pci_dev
,
859 const struct pci_device_id
*pci_id
)
861 struct saa7134_dev
*dev
;
862 struct list_head
*item
;
863 struct saa7134_mpeg_ops
*mops
;
866 dev
= kmalloc(sizeof(*dev
),GFP_KERNEL
);
869 memset(dev
,0,sizeof(*dev
));
873 if (pci_enable_device(pci_dev
)) {
878 dev
->nr
= saa7134_devcount
;
879 sprintf(dev
->name
,"saa%x[%d]",pci_dev
->device
,dev
->nr
);
882 if (pci_pci_problems
) {
883 if (pci_pci_problems
& PCIPCI_TRITON
)
884 printk(KERN_INFO
"%s: quirk: PCIPCI_TRITON\n", dev
->name
);
885 if (pci_pci_problems
& PCIPCI_NATOMA
)
886 printk(KERN_INFO
"%s: quirk: PCIPCI_NATOMA\n", dev
->name
);
887 if (pci_pci_problems
& PCIPCI_VIAETBF
)
888 printk(KERN_INFO
"%s: quirk: PCIPCI_VIAETBF\n", dev
->name
);
889 if (pci_pci_problems
& PCIPCI_VSFX
)
890 printk(KERN_INFO
"%s: quirk: PCIPCI_VSFX\n",dev
->name
);
891 #ifdef PCIPCI_ALIMAGIK
892 if (pci_pci_problems
& PCIPCI_ALIMAGIK
) {
893 printk(KERN_INFO
"%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
899 if (UNSET
!= latency
) {
900 printk(KERN_INFO
"%s: setting pci latency timer to %d\n",
902 pci_write_config_byte(pci_dev
, PCI_LATENCY_TIMER
, latency
);
906 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
907 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
908 printk(KERN_INFO
"%s: found at %s, rev: %d, irq: %d, "
909 "latency: %d, mmio: 0x%lx\n", dev
->name
,
910 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
911 dev
->pci_lat
,pci_resource_start(pci_dev
,0));
912 pci_set_master(pci_dev
);
913 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
914 printk("%s: Oops: no 32bit PCI DMA ???\n",dev
->name
);
920 dev
->board
= pci_id
->driver_data
;
921 if (card
[dev
->nr
] >= 0 &&
922 card
[dev
->nr
] < saa7134_bcount
)
923 dev
->board
= card
[dev
->nr
];
924 if (SAA7134_BOARD_NOAUTO
== dev
->board
) {
925 must_configure_manually();
926 dev
->board
= SAA7134_BOARD_UNKNOWN
;
928 dev
->tuner_type
= saa7134_boards
[dev
->board
].tuner_type
;
929 dev
->tda9887_conf
= saa7134_boards
[dev
->board
].tda9887_conf
;
930 if (UNSET
!= tuner
[dev
->nr
])
931 dev
->tuner_type
= tuner
[dev
->nr
];
932 printk(KERN_INFO
"%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
933 dev
->name
,pci_dev
->subsystem_vendor
,
934 pci_dev
->subsystem_device
,saa7134_boards
[dev
->board
].name
,
935 dev
->board
, card
[dev
->nr
] == dev
->board
?
936 "insmod option" : "autodetected");
939 if (!request_mem_region(pci_resource_start(pci_dev
,0),
940 pci_resource_len(pci_dev
,0),
943 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%lx\n",
944 dev
->name
,pci_resource_start(pci_dev
,0));
947 dev
->lmmio
= ioremap(pci_resource_start(pci_dev
,0), 0x1000);
948 dev
->bmmio
= (__u8 __iomem
*)dev
->lmmio
;
949 if (NULL
== dev
->lmmio
) {
951 printk(KERN_ERR
"%s: can't ioremap() MMIO memory\n",
956 /* initialize hardware #1 */
958 SAA7134_IRQ2_INTE_DEC3
|
959 SAA7134_IRQ2_INTE_DEC2
|
960 SAA7134_IRQ2_INTE_DEC1
|
961 SAA7134_IRQ2_INTE_DEC0
|
962 SAA7134_IRQ2_INTE_PE
|
963 SAA7134_IRQ2_INTE_AR
;
964 saa7134_board_init1(dev
);
965 saa7134_hwinit1(dev
);
968 err
= request_irq(pci_dev
->irq
, saa7134_irq
,
969 SA_SHIRQ
| SA_INTERRUPT
, dev
->name
, dev
);
971 printk(KERN_ERR
"%s: can't get IRQ %d\n",
972 dev
->name
,pci_dev
->irq
);
976 /* wait a bit, register i2c bus */
978 saa7134_i2c_register(dev
);
980 /* initialize hardware #2 */
981 saa7134_board_init2(dev
);
982 saa7134_hwinit2(dev
);
984 /* load i2c helpers */
985 if (TUNER_ABSENT
!= dev
->tuner_type
)
986 request_module("tuner");
987 if (dev
->tda9887_conf
)
988 request_module("tda9887");
989 if (card_is_empress(dev
)) {
990 request_module("saa6752hs");
991 request_module_depend("saa7134-empress",&need_empress
);
993 if (card_is_dvb(dev
))
994 request_module_depend("saa7134-dvb",&need_dvb
);
996 v4l2_prio_init(&dev
->prio
);
998 /* register v4l devices */
999 dev
->video_dev
= vdev_init(dev
,&saa7134_video_template
,"video");
1000 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1003 printk(KERN_INFO
"%s: can't register video device\n",
1007 printk(KERN_INFO
"%s: registered device video%d [v4l2]\n",
1008 dev
->name
,dev
->video_dev
->minor
& 0x1f);
1010 dev
->vbi_dev
= vdev_init(dev
,&saa7134_vbi_template
,"vbi");
1011 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1015 printk(KERN_INFO
"%s: registered device vbi%d\n",
1016 dev
->name
,dev
->vbi_dev
->minor
& 0x1f);
1018 if (card_has_radio(dev
)) {
1019 dev
->radio_dev
= vdev_init(dev
,&saa7134_radio_template
,"radio");
1020 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1024 printk(KERN_INFO
"%s: registered device radio%d\n",
1025 dev
->name
,dev
->radio_dev
->minor
& 0x1f);
1028 /* register oss devices */
1029 switch (dev
->pci
->device
) {
1030 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
1031 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1032 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1034 err
= dev
->oss
.minor_dsp
=
1035 register_sound_dsp(&saa7134_dsp_fops
,
1040 printk(KERN_INFO
"%s: registered device dsp%d\n",
1041 dev
->name
,dev
->oss
.minor_dsp
>> 4);
1043 err
= dev
->oss
.minor_mixer
=
1044 register_sound_mixer(&saa7134_mixer_fops
,
1048 printk(KERN_INFO
"%s: registered device mixer%d\n",
1049 dev
->name
,dev
->oss
.minor_mixer
>> 4);
1054 /* everything worked */
1055 pci_set_drvdata(pci_dev
,dev
);
1058 down(&devlist_lock
);
1059 list_for_each(item
,&mops_list
) {
1060 mops
= list_entry(item
, struct saa7134_mpeg_ops
, next
);
1061 mpeg_ops_attach(mops
, dev
);
1063 list_add_tail(&dev
->devlist
,&saa7134_devlist
);
1066 /* check for signal */
1067 saa7134_irq_video_intl(dev
);
1071 switch (dev
->pci
->device
) {
1072 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
1073 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1074 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1076 unregister_sound_dsp(dev
->oss
.minor_dsp
);
1080 saa7134_unregister_video(dev
);
1081 saa7134_i2c_unregister(dev
);
1082 free_irq(pci_dev
->irq
, dev
);
1084 saa7134_hwfini(dev
);
1085 iounmap(dev
->lmmio
);
1087 release_mem_region(pci_resource_start(pci_dev
,0),
1088 pci_resource_len(pci_dev
,0));
1094 static void __devexit
saa7134_finidev(struct pci_dev
*pci_dev
)
1096 struct saa7134_dev
*dev
= pci_get_drvdata(pci_dev
);
1097 struct list_head
*item
;
1098 struct saa7134_mpeg_ops
*mops
;
1102 u32 report
= saa_readl(SAA7134_IRQ_REPORT
);
1103 u32 status
= saa_readl(SAA7134_IRQ_STATUS
);
1104 print_irqstatus(dev
,42,report
,status
);
1107 /* disable peripheral devices */
1108 saa_writeb(SAA7134_SPECIAL_MODE
,0);
1110 /* shutdown hardware */
1111 saa_writel(SAA7134_IRQ1
,0);
1112 saa_writel(SAA7134_IRQ2
,0);
1113 saa_writel(SAA7134_MAIN_CTRL
,0);
1115 /* shutdown subsystems */
1116 saa7134_hwfini(dev
);
1119 down(&devlist_lock
);
1120 list_del(&dev
->devlist
);
1121 list_for_each(item
,&mops_list
) {
1122 mops
= list_entry(item
, struct saa7134_mpeg_ops
, next
);
1123 mpeg_ops_detach(mops
, dev
);
1128 saa7134_i2c_unregister(dev
);
1129 switch (dev
->pci
->device
) {
1130 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
1131 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1132 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1134 unregister_sound_mixer(dev
->oss
.minor_mixer
);
1135 unregister_sound_dsp(dev
->oss
.minor_dsp
);
1139 saa7134_unregister_video(dev
);
1141 /* release ressources */
1142 free_irq(pci_dev
->irq
, dev
);
1143 iounmap(dev
->lmmio
);
1144 release_mem_region(pci_resource_start(pci_dev
,0),
1145 pci_resource_len(pci_dev
,0));
1147 #if 0 /* causes some trouble when reinserting the driver ... */
1148 pci_disable_device(pci_dev
);
1150 pci_set_drvdata(pci_dev
, NULL
);
1156 /* ----------------------------------------------------------- */
1158 int saa7134_ts_register(struct saa7134_mpeg_ops
*ops
)
1160 struct list_head
*item
;
1161 struct saa7134_dev
*dev
;
1163 down(&devlist_lock
);
1164 list_for_each(item
,&saa7134_devlist
) {
1165 dev
= list_entry(item
, struct saa7134_dev
, devlist
);
1166 mpeg_ops_attach(ops
, dev
);
1168 list_add_tail(&ops
->next
,&mops_list
);
1173 void saa7134_ts_unregister(struct saa7134_mpeg_ops
*ops
)
1175 struct list_head
*item
;
1176 struct saa7134_dev
*dev
;
1178 down(&devlist_lock
);
1179 list_del(&ops
->next
);
1180 list_for_each(item
,&saa7134_devlist
) {
1181 dev
= list_entry(item
, struct saa7134_dev
, devlist
);
1182 mpeg_ops_detach(ops
, dev
);
1187 EXPORT_SYMBOL(saa7134_ts_register
);
1188 EXPORT_SYMBOL(saa7134_ts_unregister
);
1190 /* ----------------------------------------------------------- */
1192 static struct pci_driver saa7134_pci_driver
= {
1194 .id_table
= saa7134_pci_tbl
,
1195 .probe
= saa7134_initdev
,
1196 .remove
= __devexit_p(saa7134_finidev
),
1199 static int saa7134_init(void)
1201 INIT_LIST_HEAD(&saa7134_devlist
);
1202 printk(KERN_INFO
"saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1203 (SAA7134_VERSION_CODE
>> 16) & 0xff,
1204 (SAA7134_VERSION_CODE
>> 8) & 0xff,
1205 SAA7134_VERSION_CODE
& 0xff);
1207 printk(KERN_INFO
"saa7130/34: snapshot date %04d-%02d-%02d\n",
1208 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
1210 return pci_module_init(&saa7134_pci_driver
);
1213 static void saa7134_fini(void)
1215 #ifdef CONFIG_MODULES
1216 if (pending_registered
)
1217 unregister_module_notifier(&pending_notifier
);
1219 pci_unregister_driver(&saa7134_pci_driver
);
1222 module_init(saa7134_init
);
1223 module_exit(saa7134_fini
);
1225 /* ----------------------------------------------------------- */
1227 EXPORT_SYMBOL(saa7134_print_ioctl
);
1228 EXPORT_SYMBOL(saa7134_i2c_call_clients
);
1229 EXPORT_SYMBOL(saa7134_devlist
);
1230 EXPORT_SYMBOL(saa7134_boards
);
1232 /* ----------------------------------------------------------- */