3 * device driver for philips saa7134 based TV cards
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/config.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/kmod.h>
31 #include <linux/sound.h>
32 #include <linux/interrupt.h>
33 #include <linux/delay.h>
35 #include "saa7134-reg.h"
38 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 /* ------------------------------------------------------------------ */
44 static unsigned int irq_debug = 0;
45 module_param(irq_debug, int, 0644);
46 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
48 static unsigned int core_debug = 0;
49 module_param(core_debug, int, 0644);
50 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
52 static unsigned int gpio_tracking = 0;
53 module_param(gpio_tracking, int, 0644);
54 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
56 static unsigned int oss = 0;
57 module_param(oss, int, 0444);
58 MODULE_PARM_DESC(oss,"register oss devices (default: no)");
60 static unsigned int latency = UNSET;
61 module_param(latency, int, 0444);
62 MODULE_PARM_DESC(latency,"pci latency timer");
64 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
65 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
66 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
67 static unsigned int dsp_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
68 static unsigned int mixer_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
69 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
70 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
72 module_param_array(video_nr, int, NULL, 0444);
73 module_param_array(vbi_nr, int, NULL, 0444);
74 module_param_array(radio_nr, int, NULL, 0444);
75 module_param_array(dsp_nr, int, NULL, 0444);
76 module_param_array(mixer_nr, int, NULL, 0444);
77 module_param_array(tuner, int, NULL, 0444);
78 module_param_array(card, int, NULL, 0444);
80 MODULE_PARM_DESC(video_nr, "video device number");
81 MODULE_PARM_DESC(vbi_nr, "vbi device number");
82 MODULE_PARM_DESC(radio_nr, "radio device number");
83 MODULE_PARM_DESC(dsp_nr, "oss dsp device number");
84 MODULE_PARM_DESC(mixer_nr, "oss mixer device number");
85 MODULE_PARM_DESC(tuner, "tuner type");
86 MODULE_PARM_DESC(card, "card type");
88 static DECLARE_MUTEX(devlist_lock);
89 LIST_HEAD(saa7134_devlist);
90 static LIST_HEAD(mops_list);
91 static unsigned int saa7134_devcount;
93 #define dprintk(fmt, arg...) if (core_debug) \
94 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
96 /* ------------------------------------------------------------------ */
97 /* debug help functions */
99 static const char *v4l1_ioctls[] = {
100 "0", "GCAP", "GCHAN", "SCHAN", "GTUNER", "STUNER", "GPICT", "SPICT",
101 "CCAPTURE", "GWIN", "SWIN", "GFBUF", "SFBUF", "KEY", "GFREQ",
102 "SFREQ", "GAUDIO", "SAUDIO", "SYNC", "MCAPTURE", "GMBUF", "GUNIT",
103 "GCAPTURE", "SCAPTURE", "SPLAYMODE", "SWRITEMODE", "GPLAYINFO",
104 "SMICROCODE", "GVBIFMT", "SVBIFMT" };
105 #define V4L1_IOCTLS ARRAY_SIZE(v4l1_ioctls)
107 static const char *v4l2_ioctls[] = {
108 "QUERYCAP", "1", "ENUM_PIXFMT", "ENUM_FBUFFMT", "G_FMT", "S_FMT",
109 "G_COMP", "S_COMP", "REQBUFS", "QUERYBUF", "G_FBUF", "S_FBUF",
110 "G_WIN", "S_WIN", "PREVIEW", "QBUF", "16", "DQBUF", "STREAMON",
111 "STREAMOFF", "G_PERF", "G_PARM", "S_PARM", "G_STD", "S_STD",
112 "ENUMSTD", "ENUMINPUT", "G_CTRL", "S_CTRL", "G_TUNER", "S_TUNER",
113 "G_FREQ", "S_FREQ", "G_AUDIO", "S_AUDIO", "35", "QUERYCTRL",
114 "QUERYMENU", "G_INPUT", "S_INPUT", "ENUMCVT", "41", "42", "43",
115 "44", "45", "G_OUTPUT", "S_OUTPUT", "ENUMOUTPUT", "G_AUDOUT",
116 "S_AUDOUT", "ENUMFX", "G_EFFECT", "S_EFFECT", "G_MODULATOR",
119 #define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
121 static const char *osspcm_ioctls[] = {
122 "RESET", "SYNC", "SPEED", "STEREO", "GETBLKSIZE", "SETFMT",
123 "CHANNELS", "?", "POST", "SUBDIVIDE", "SETFRAGMENT", "GETFMTS",
124 "GETOSPACE", "GETISPACE", "NONBLOCK", "GETCAPS", "GET/SETTRIGGER",
125 "GETIPTR", "GETOPTR", "MAPINBUF", "MAPOUTBUF", "SETSYNCRO",
126 "SETDUPLEX", "GETODELAY"
128 #define OSSPCM_IOCTLS ARRAY_SIZE(v4l2_ioctls)
130 void saa7134_print_ioctl(char *name, unsigned int cmd)
134 switch (_IOC_DIR(cmd)) {
135 case _IOC_NONE: dir = "--"; break;
136 case _IOC_READ: dir = "r-"; break;
137 case _IOC_WRITE: dir = "-w"; break;
138 case _IOC_READ | _IOC_WRITE: dir = "rw"; break;
139 default: dir = "??"; break;
141 switch (_IOC_TYPE(cmd)) {
143 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l1, %s, VIDIOC%s)\n",
144 name, cmd, dir, (_IOC_NR(cmd) < V4L1_IOCTLS) ?
145 v4l1_ioctls[_IOC_NR(cmd)] : "???");
148 printk(KERN_DEBUG "%s: ioctl 0x%08x (v4l2, %s, VIDIOC_%s)\n",
149 name, cmd, dir, (_IOC_NR(cmd) < V4L2_IOCTLS) ?
150 v4l2_ioctls[_IOC_NR(cmd)] : "???");
153 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss dsp, %s, SNDCTL_DSP_%s)\n",
154 name, cmd, dir, (_IOC_NR(cmd) < OSSPCM_IOCTLS) ?
155 osspcm_ioctls[_IOC_NR(cmd)] : "???");
158 printk(KERN_DEBUG "%s: ioctl 0x%08x (oss mixer, %s, #%d)\n",
159 name, cmd, dir, _IOC_NR(cmd));
162 printk(KERN_DEBUG "%s: ioctl 0x%08x (???, %s, #%d)\n",
163 name, cmd, dir, _IOC_NR(cmd));
167 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
169 unsigned long mode,status;
173 /* rising SAA7134_GPIO_GPRESCAN reads the status */
174 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
175 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
176 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
177 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
179 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
180 dev->name, mode, (~mode) & status, mode & status, msg);
183 /* ------------------------------------------------------------------ */
186 /* ----------------------------------------------------------- */
187 /* delayed request_module */
189 #ifdef CONFIG_MODULES
191 static int need_empress;
194 static int pending_call(struct notifier_block *self, unsigned long state,
197 if (module != THIS_MODULE || state != MODULE_STATE_LIVE)
201 request_module("saa7134-empress");
203 request_module("saa7134-dvb");
207 static int pending_registered;
208 static struct notifier_block pending_notifier = {
209 .notifier_call = pending_call,
212 static void request_module_depend(char *name, int *flag)
214 switch (THIS_MODULE->state) {
215 case MODULE_STATE_COMING:
216 if (!pending_registered) {
217 register_module_notifier(&pending_notifier);
218 pending_registered = 1;
222 case MODULE_STATE_LIVE:
223 request_module(name);
233 #define request_module_depend(name,flag)
235 #endif /* CONFIG_MODULES */
237 /* ------------------------------------------------------------------ */
239 /* nr of (saa7134-)pages for the given buffer size */
240 static int saa7134_buffer_pages(int size)
242 size = PAGE_ALIGN(size);
243 size += PAGE_SIZE; /* for non-page-aligned buffers */
248 /* calc max # of buffers from size (must not exceed the 4MB virtual
249 * address space per DMA channel) */
250 int saa7134_buffer_count(unsigned int size, unsigned int count)
252 unsigned int maxcount;
254 maxcount = 1024 / saa7134_buffer_pages(size);
255 if (count > maxcount)
260 int saa7134_buffer_startpage(struct saa7134_buf *buf)
262 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
265 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
269 base = saa7134_buffer_startpage(buf) * 4096;
270 base += buf->vb.dma.sglist[0].offset;
274 /* ------------------------------------------------------------------ */
276 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
281 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
284 pt->size = SAA7134_PGTABLE_SIZE;
290 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
291 struct scatterlist *list, unsigned int length,
292 unsigned int startpage)
297 BUG_ON(NULL == pt || NULL == pt->cpu);
299 ptr = pt->cpu + startpage;
300 for (i = 0; i < length; i++, list++)
301 for (p = 0; p * 4096 < list->length; p++, ptr++)
302 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
306 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
310 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
314 /* ------------------------------------------------------------------ */
316 void saa7134_dma_free(struct saa7134_dev *dev,struct saa7134_buf *buf)
321 videobuf_waiton(&buf->vb,0,0);
322 videobuf_dma_pci_unmap(dev->pci, &buf->vb.dma);
323 videobuf_dma_free(&buf->vb.dma);
324 buf->vb.state = STATE_NEEDS_INIT;
327 /* ------------------------------------------------------------------ */
329 int saa7134_buffer_queue(struct saa7134_dev *dev,
330 struct saa7134_dmaqueue *q,
331 struct saa7134_buf *buf)
333 struct saa7134_buf *next = NULL;
335 assert_spin_locked(&dev->slock);
336 dprintk("buffer_queue %p\n",buf);
337 if (NULL == q->curr) {
340 buf->activate(dev,buf,NULL);
341 } else if (list_empty(&q->queue)) {
342 list_add_tail(&buf->vb.queue,&q->queue);
343 buf->vb.state = STATE_QUEUED;
345 next = list_entry(q->queue.next,struct saa7134_buf,
348 buf->activate(dev,buf,next);
351 list_add_tail(&buf->vb.queue,&q->queue);
352 buf->vb.state = STATE_QUEUED;
357 void saa7134_buffer_finish(struct saa7134_dev *dev,
358 struct saa7134_dmaqueue *q,
361 assert_spin_locked(&dev->slock);
362 dprintk("buffer_finish %p\n",q->curr);
364 /* finish current buffer */
365 q->curr->vb.state = state;
366 do_gettimeofday(&q->curr->vb.ts);
367 wake_up(&q->curr->vb.done);
371 void saa7134_buffer_next(struct saa7134_dev *dev,
372 struct saa7134_dmaqueue *q)
374 struct saa7134_buf *buf,*next = NULL;
376 assert_spin_locked(&dev->slock);
377 BUG_ON(NULL != q->curr);
379 if (!list_empty(&q->queue)) {
380 /* activate next one from queue */
381 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
382 dprintk("buffer_next %p [prev=%p/next=%p]\n",
383 buf,q->queue.prev,q->queue.next);
384 list_del(&buf->vb.queue);
385 if (!list_empty(&q->queue))
386 next = list_entry(q->queue.next,struct saa7134_buf,
389 buf->activate(dev,buf,next);
390 dprintk("buffer_next #2 prev=%p/next=%p\n",
391 q->queue.prev,q->queue.next);
393 /* nothing to do -- just stop DMA */
394 dprintk("buffer_next %p\n",NULL);
395 saa7134_set_dmabits(dev);
396 del_timer(&q->timeout);
400 void saa7134_buffer_timeout(unsigned long data)
402 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
403 struct saa7134_dev *dev = q->dev;
406 spin_lock_irqsave(&dev->slock,flags);
408 /* try to reset the hardware (SWRST) */
409 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
410 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
411 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
413 /* flag current buffer as failed,
414 try to start over with the next one. */
416 dprintk("timeout on %p\n",q->curr);
417 saa7134_buffer_finish(dev,q,STATE_ERROR);
419 saa7134_buffer_next(dev,q);
420 spin_unlock_irqrestore(&dev->slock,flags);
423 /* ------------------------------------------------------------------ */
425 int saa7134_set_dmabits(struct saa7134_dev *dev)
427 u32 split, task=0, ctrl=0, irq=0;
428 enum v4l2_field cap = V4L2_FIELD_ANY;
429 enum v4l2_field ov = V4L2_FIELD_ANY;
431 assert_spin_locked(&dev->slock);
433 /* video capture -- dma 0 + video task A */
434 if (dev->video_q.curr) {
436 ctrl |= SAA7134_MAIN_CTRL_TE0;
437 irq |= SAA7134_IRQ1_INTE_RA0_1 |
438 SAA7134_IRQ1_INTE_RA0_0;
439 cap = dev->video_q.curr->vb.field;
442 /* video capture -- dma 1+2 (planar modes) */
443 if (dev->video_q.curr &&
444 dev->video_q.curr->fmt->planar) {
445 ctrl |= SAA7134_MAIN_CTRL_TE4 |
446 SAA7134_MAIN_CTRL_TE5;
449 /* screen overlay -- dma 0 + video task B */
452 ctrl |= SAA7134_MAIN_CTRL_TE1;
456 /* vbi capture -- dma 0 + vbi task A+B */
457 if (dev->vbi_q.curr) {
459 ctrl |= SAA7134_MAIN_CTRL_TE2 |
460 SAA7134_MAIN_CTRL_TE3;
461 irq |= SAA7134_IRQ1_INTE_RA0_7 |
462 SAA7134_IRQ1_INTE_RA0_6 |
463 SAA7134_IRQ1_INTE_RA0_5 |
464 SAA7134_IRQ1_INTE_RA0_4;
467 /* audio capture -- dma 3 */
468 if (dev->oss.dma_running) {
469 ctrl |= SAA7134_MAIN_CTRL_TE6;
470 irq |= SAA7134_IRQ1_INTE_RA3_1 |
471 SAA7134_IRQ1_INTE_RA3_0;
474 /* TS capture -- dma 5 */
475 if (dev->ts_q.curr) {
476 ctrl |= SAA7134_MAIN_CTRL_TE5;
477 irq |= SAA7134_IRQ1_INTE_RA2_3 |
478 SAA7134_IRQ1_INTE_RA2_2 |
479 SAA7134_IRQ1_INTE_RA2_1 |
480 SAA7134_IRQ1_INTE_RA2_0;
483 /* set task conditions + field handling */
484 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
485 /* default config -- use full frames */
486 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
487 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
488 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
489 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
492 /* split fields between tasks */
493 if (V4L2_FIELD_TOP == cap) {
494 /* odd A, even B, repeat */
495 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
496 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
498 /* odd B, even A, repeat */
499 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
500 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
502 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
503 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
508 saa_writeb(SAA7134_REGION_ENABLE, task);
509 saa_writel(SAA7134_IRQ1, irq);
510 saa_andorl(SAA7134_MAIN_CTRL,
511 SAA7134_MAIN_CTRL_TE0 |
512 SAA7134_MAIN_CTRL_TE1 |
513 SAA7134_MAIN_CTRL_TE2 |
514 SAA7134_MAIN_CTRL_TE3 |
515 SAA7134_MAIN_CTRL_TE4 |
516 SAA7134_MAIN_CTRL_TE5 |
517 SAA7134_MAIN_CTRL_TE6,
519 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
520 task, ctrl, irq, split ? "no" : "yes");
525 /* ------------------------------------------------------------------ */
526 /* IRQ handler + helpers */
528 static char *irqbits[] = {
529 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
530 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
531 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
532 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
534 #define IRQBITS ARRAY_SIZE(irqbits)
536 static void print_irqstatus(struct saa7134_dev *dev, int loop,
537 unsigned long report, unsigned long status)
541 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
542 dev->name,loop,jiffies,report,status);
543 for (i = 0; i < IRQBITS; i++) {
544 if (!(report & (1 << i)))
546 printk(" %s",irqbits[i]);
548 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
549 printk(" | RA0=%s,%s,%s,%ld",
550 (status & 0x40) ? "vbi" : "video",
551 (status & 0x20) ? "b" : "a",
552 (status & 0x10) ? "odd" : "even",
558 static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
560 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
561 unsigned long report,status;
562 int loop, handled = 0;
564 for (loop = 0; loop < 10; loop++) {
565 report = saa_readl(SAA7134_IRQ_REPORT);
566 status = saa_readl(SAA7134_IRQ_STATUS);
569 printk(KERN_DEBUG "%s/irq: no (more) work\n",
574 saa_writel(SAA7134_IRQ_REPORT,report);
576 print_irqstatus(dev,loop,report,status);
579 if (report & SAA7134_IRQ_REPORT_RDCAP /* _INTL */)
580 saa7134_irq_video_intl(dev);
582 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
583 (status & 0x60) == 0)
584 saa7134_irq_video_done(dev,status);
586 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
587 (status & 0x40) == 0x40)
588 saa7134_irq_vbi_done(dev,status);
590 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
592 saa7134_irq_ts_done(dev,status);
594 if ((report & SAA7134_IRQ_REPORT_DONE_RA3))
595 saa7134_irq_oss_done(dev,status);
597 if ((report & (SAA7134_IRQ_REPORT_GPIO16 |
598 SAA7134_IRQ_REPORT_GPIO18)) &&
600 saa7134_input_irq(dev);
604 print_irqstatus(dev,loop,report,status);
605 if (report & SAA7134_IRQ_REPORT_PE) {
606 /* disable all parity error */
607 printk(KERN_WARNING "%s/irq: looping -- "
608 "clearing PE (parity error!) enable bit\n",dev->name);
609 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
610 } else if (report & (SAA7134_IRQ_REPORT_GPIO16 |
611 SAA7134_IRQ_REPORT_GPIO18)) {
612 /* disable gpio IRQs */
613 printk(KERN_WARNING "%s/irq: looping -- "
614 "clearing GPIO enable bits\n",dev->name);
615 saa_clearl(SAA7134_IRQ2, (SAA7134_IRQ2_INTE_GPIO16 |
616 SAA7134_IRQ2_INTE_GPIO18));
618 /* disable all irqs */
619 printk(KERN_WARNING "%s/irq: looping -- "
620 "clearing all enable bits\n",dev->name);
621 saa_writel(SAA7134_IRQ1,0);
622 saa_writel(SAA7134_IRQ2,0);
627 return IRQ_RETVAL(handled);
630 /* ------------------------------------------------------------------ */
632 /* early init (no i2c, no irq) */
633 static int saa7134_hwinit1(struct saa7134_dev *dev)
635 dprintk("hwinit1\n");
637 saa_writel(SAA7134_IRQ1, 0);
638 saa_writel(SAA7134_IRQ2, 0);
639 init_MUTEX(&dev->lock);
640 spin_lock_init(&dev->slock);
642 saa7134_track_gpio(dev,"pre-init");
643 saa7134_video_init1(dev);
644 saa7134_vbi_init1(dev);
645 if (card_has_mpeg(dev))
646 saa7134_ts_init1(dev);
647 saa7134_input_init1(dev);
649 switch (dev->pci->device) {
650 case PCI_DEVICE_ID_PHILIPS_SAA7134:
651 case PCI_DEVICE_ID_PHILIPS_SAA7133:
652 case PCI_DEVICE_ID_PHILIPS_SAA7135:
653 saa7134_oss_init1(dev);
657 /* RAM FIFO config */
658 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
659 saa_writel(SAA7134_THRESHOULD,0x02020202);
661 /* enable audio + video processing */
662 saa_writel(SAA7134_MAIN_CTRL,
663 SAA7134_MAIN_CTRL_VPLLE |
664 SAA7134_MAIN_CTRL_APLLE |
665 SAA7134_MAIN_CTRL_EXOSC |
666 SAA7134_MAIN_CTRL_EVFE1 |
667 SAA7134_MAIN_CTRL_EVFE2 |
668 SAA7134_MAIN_CTRL_ESFE |
669 SAA7134_MAIN_CTRL_EBDAC);
671 /* enable peripheral devices */
672 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
674 /* set vertical line numbering start (vbi needs this) */
675 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
680 /* late init (with i2c + irq) */
681 static int saa7134_hwinit2(struct saa7134_dev *dev)
683 unsigned int irq2_mask;
684 dprintk("hwinit2\n");
686 saa7134_video_init2(dev);
687 saa7134_tvaudio_init2(dev);
691 SAA7134_IRQ2_INTE_DEC3 |
692 SAA7134_IRQ2_INTE_DEC2 |
693 SAA7134_IRQ2_INTE_DEC1 |
694 SAA7134_IRQ2_INTE_DEC0 |
695 SAA7134_IRQ2_INTE_PE |
696 SAA7134_IRQ2_INTE_AR;
699 irq2_mask |= (SAA7134_IRQ2_INTE_GPIO18 |
700 SAA7134_IRQ2_INTE_GPIO18A |
701 SAA7134_IRQ2_INTE_GPIO16 );
703 saa_writel(SAA7134_IRQ1, 0);
704 saa_writel(SAA7134_IRQ2, irq2_mask);
710 static int saa7134_hwfini(struct saa7134_dev *dev)
714 switch (dev->pci->device) {
715 case PCI_DEVICE_ID_PHILIPS_SAA7134:
716 case PCI_DEVICE_ID_PHILIPS_SAA7133:
717 case PCI_DEVICE_ID_PHILIPS_SAA7135:
718 saa7134_oss_fini(dev);
721 if (card_has_mpeg(dev))
722 saa7134_ts_fini(dev);
723 saa7134_input_fini(dev);
724 saa7134_vbi_fini(dev);
725 saa7134_video_fini(dev);
726 saa7134_tvaudio_fini(dev);
730 static void __devinit must_configure_manually(void)
736 "saa7134: Congratulations! Your TV card vendor saved a few\n"
737 "saa7134: cents for a eeprom, thus your pci board has no\n"
738 "saa7134: subsystem ID and I can't identify it automatically\n"
740 "saa7134: I feel better now. Ok, here are the good news:\n"
741 "saa7134: You can use the card=<nr> insmod option to specify\n"
742 "saa7134: which board do you have. The list:\n");
743 for (i = 0; i < saa7134_bcount; i++) {
744 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
745 i,saa7134_boards[i].name);
746 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
747 if (saa7134_pci_tbl[p].driver_data != i)
750 saa7134_pci_tbl[p].subvendor,
751 saa7134_pci_tbl[p].subdevice);
757 static struct video_device *vdev_init(struct saa7134_dev *dev,
758 struct video_device *template,
761 struct video_device *vfd;
763 vfd = video_device_alloc();
768 vfd->dev = &dev->pci->dev;
769 vfd->release = video_device_release;
770 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
771 dev->name, type, saa7134_boards[dev->board].name);
775 static void saa7134_unregister_video(struct saa7134_dev *dev)
777 if (dev->video_dev) {
778 if (-1 != dev->video_dev->minor)
779 video_unregister_device(dev->video_dev);
781 video_device_release(dev->video_dev);
782 dev->video_dev = NULL;
785 if (-1 != dev->vbi_dev->minor)
786 video_unregister_device(dev->vbi_dev);
788 video_device_release(dev->vbi_dev);
791 if (dev->radio_dev) {
792 if (-1 != dev->radio_dev->minor)
793 video_unregister_device(dev->radio_dev);
795 video_device_release(dev->radio_dev);
796 dev->radio_dev = NULL;
800 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
801 struct saa7134_dev *dev)
805 if (NULL != dev->mops)
807 if (saa7134_boards[dev->board].mpeg != ops->type)
809 err = ops->init(dev);
815 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
816 struct saa7134_dev *dev)
818 if (NULL == dev->mops)
820 if (dev->mops != ops)
822 dev->mops->fini(dev);
826 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
827 const struct pci_device_id *pci_id)
829 struct saa7134_dev *dev;
830 struct list_head *item;
831 struct saa7134_mpeg_ops *mops;
834 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
837 memset(dev,0,sizeof(*dev));
841 if (pci_enable_device(pci_dev)) {
846 dev->nr = saa7134_devcount;
847 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
850 if (pci_pci_problems) {
851 if (pci_pci_problems & PCIPCI_TRITON)
852 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
853 if (pci_pci_problems & PCIPCI_NATOMA)
854 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
855 if (pci_pci_problems & PCIPCI_VIAETBF)
856 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
857 if (pci_pci_problems & PCIPCI_VSFX)
858 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
859 #ifdef PCIPCI_ALIMAGIK
860 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
861 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
867 if (UNSET != latency) {
868 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
870 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
874 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
875 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
876 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
877 "latency: %d, mmio: 0x%lx\n", dev->name,
878 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
879 dev->pci_lat,pci_resource_start(pci_dev,0));
880 pci_set_master(pci_dev);
881 if (!pci_dma_supported(pci_dev,0xffffffff)) {
882 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
888 dev->board = pci_id->driver_data;
889 if (card[dev->nr] >= 0 &&
890 card[dev->nr] < saa7134_bcount)
891 dev->board = card[dev->nr];
892 if (SAA7134_BOARD_NOAUTO == dev->board) {
893 must_configure_manually();
894 dev->board = SAA7134_BOARD_UNKNOWN;
896 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
897 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
898 if (UNSET != tuner[dev->nr])
899 dev->tuner_type = tuner[dev->nr];
900 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
901 dev->name,pci_dev->subsystem_vendor,
902 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
903 dev->board, card[dev->nr] == dev->board ?
904 "insmod option" : "autodetected");
907 if (!request_mem_region(pci_resource_start(pci_dev,0),
908 pci_resource_len(pci_dev,0),
911 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
912 dev->name,pci_resource_start(pci_dev,0));
915 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
916 dev->bmmio = (__u8 __iomem *)dev->lmmio;
917 if (NULL == dev->lmmio) {
919 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
924 /* initialize hardware #1 */
925 saa7134_board_init1(dev);
926 saa7134_hwinit1(dev);
929 err = request_irq(pci_dev->irq, saa7134_irq,
930 SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
932 printk(KERN_ERR "%s: can't get IRQ %d\n",
933 dev->name,pci_dev->irq);
937 /* wait a bit, register i2c bus */
939 saa7134_i2c_register(dev);
941 /* initialize hardware #2 */
942 saa7134_board_init2(dev);
943 saa7134_hwinit2(dev);
945 /* load i2c helpers */
946 if (TUNER_ABSENT != dev->tuner_type)
947 request_module("tuner");
948 if (dev->tda9887_conf)
949 request_module("tda9887");
950 if (card_is_empress(dev)) {
951 request_module("saa6752hs");
952 request_module_depend("saa7134-empress",&need_empress);
955 if (card_is_dvb(dev))
956 request_module_depend("saa7134-dvb",&need_dvb);
958 v4l2_prio_init(&dev->prio);
960 /* register v4l devices */
961 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
962 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
965 printk(KERN_INFO "%s: can't register video device\n",
969 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
970 dev->name,dev->video_dev->minor & 0x1f);
972 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
973 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
977 printk(KERN_INFO "%s: registered device vbi%d\n",
978 dev->name,dev->vbi_dev->minor & 0x1f);
980 if (card_has_radio(dev)) {
981 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
982 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
986 printk(KERN_INFO "%s: registered device radio%d\n",
987 dev->name,dev->radio_dev->minor & 0x1f);
990 /* register oss devices */
991 switch (dev->pci->device) {
992 case PCI_DEVICE_ID_PHILIPS_SAA7134:
993 case PCI_DEVICE_ID_PHILIPS_SAA7133:
994 case PCI_DEVICE_ID_PHILIPS_SAA7135:
996 err = dev->oss.minor_dsp =
997 register_sound_dsp(&saa7134_dsp_fops,
1002 printk(KERN_INFO "%s: registered device dsp%d\n",
1003 dev->name,dev->oss.minor_dsp >> 4);
1005 err = dev->oss.minor_mixer =
1006 register_sound_mixer(&saa7134_mixer_fops,
1010 printk(KERN_INFO "%s: registered device mixer%d\n",
1011 dev->name,dev->oss.minor_mixer >> 4);
1016 /* everything worked */
1017 pci_set_drvdata(pci_dev,dev);
1020 down(&devlist_lock);
1021 list_for_each(item,&mops_list) {
1022 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1023 mpeg_ops_attach(mops, dev);
1025 list_add_tail(&dev->devlist,&saa7134_devlist);
1028 /* check for signal */
1029 saa7134_irq_video_intl(dev);
1033 switch (dev->pci->device) {
1034 case PCI_DEVICE_ID_PHILIPS_SAA7134:
1035 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1036 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1038 unregister_sound_dsp(dev->oss.minor_dsp);
1042 saa7134_unregister_video(dev);
1043 saa7134_i2c_unregister(dev);
1044 free_irq(pci_dev->irq, dev);
1046 saa7134_hwfini(dev);
1047 iounmap(dev->lmmio);
1049 release_mem_region(pci_resource_start(pci_dev,0),
1050 pci_resource_len(pci_dev,0));
1056 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1058 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1059 struct list_head *item;
1060 struct saa7134_mpeg_ops *mops;
1064 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1065 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1066 print_irqstatus(dev,42,report,status);
1069 /* disable peripheral devices */
1070 saa_writeb(SAA7134_SPECIAL_MODE,0);
1072 /* shutdown hardware */
1073 saa_writel(SAA7134_IRQ1,0);
1074 saa_writel(SAA7134_IRQ2,0);
1075 saa_writel(SAA7134_MAIN_CTRL,0);
1077 /* shutdown subsystems */
1078 saa7134_hwfini(dev);
1081 down(&devlist_lock);
1082 list_del(&dev->devlist);
1083 list_for_each(item,&mops_list) {
1084 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1085 mpeg_ops_detach(mops, dev);
1090 saa7134_i2c_unregister(dev);
1091 switch (dev->pci->device) {
1092 case PCI_DEVICE_ID_PHILIPS_SAA7134:
1093 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1094 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1096 unregister_sound_mixer(dev->oss.minor_mixer);
1097 unregister_sound_dsp(dev->oss.minor_dsp);
1101 saa7134_unregister_video(dev);
1103 /* release ressources */
1104 free_irq(pci_dev->irq, dev);
1105 iounmap(dev->lmmio);
1106 release_mem_region(pci_resource_start(pci_dev,0),
1107 pci_resource_len(pci_dev,0));
1109 pci_set_drvdata(pci_dev, NULL);
1115 /* ----------------------------------------------------------- */
1117 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1119 struct list_head *item;
1120 struct saa7134_dev *dev;
1122 down(&devlist_lock);
1123 list_for_each(item,&saa7134_devlist) {
1124 dev = list_entry(item, struct saa7134_dev, devlist);
1125 mpeg_ops_attach(ops, dev);
1127 list_add_tail(&ops->next,&mops_list);
1132 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1134 struct list_head *item;
1135 struct saa7134_dev *dev;
1137 down(&devlist_lock);
1138 list_del(&ops->next);
1139 list_for_each(item,&saa7134_devlist) {
1140 dev = list_entry(item, struct saa7134_dev, devlist);
1141 mpeg_ops_detach(ops, dev);
1146 EXPORT_SYMBOL(saa7134_ts_register);
1147 EXPORT_SYMBOL(saa7134_ts_unregister);
1149 /* ----------------------------------------------------------- */
1151 static struct pci_driver saa7134_pci_driver = {
1153 .id_table = saa7134_pci_tbl,
1154 .probe = saa7134_initdev,
1155 .remove = __devexit_p(saa7134_finidev),
1158 static int saa7134_init(void)
1160 INIT_LIST_HEAD(&saa7134_devlist);
1161 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1162 (SAA7134_VERSION_CODE >> 16) & 0xff,
1163 (SAA7134_VERSION_CODE >> 8) & 0xff,
1164 SAA7134_VERSION_CODE & 0xff);
1166 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1167 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1169 return pci_module_init(&saa7134_pci_driver);
1172 static void saa7134_fini(void)
1174 #ifdef CONFIG_MODULES
1175 if (pending_registered)
1176 unregister_module_notifier(&pending_notifier);
1178 pci_unregister_driver(&saa7134_pci_driver);
1181 module_init(saa7134_init);
1182 module_exit(saa7134_fini);
1184 /* ----------------------------------------------------------- */
1186 EXPORT_SYMBOL(saa7134_print_ioctl);
1187 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1188 EXPORT_SYMBOL(saa7134_devlist);
1189 EXPORT_SYMBOL(saa7134_boards);
1191 /* ----------------------------------------------------------- */