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/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/kmod.h>
29 #include <linux/sound.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/mutex.h>
33 #include <linux/dma-mapping.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 alsa = 0;
58 module_param(alsa, int, 0644);
59 MODULE_PARM_DESC(alsa,"enable ALSA DMA sound [dmasound]");
61 static unsigned int oss = 0;
62 module_param(oss, int, 0644);
63 MODULE_PARM_DESC(oss,"enable OSS DMA sound [dmasound]");
65 static unsigned int latency = UNSET;
66 module_param(latency, int, 0444);
67 MODULE_PARM_DESC(latency,"pci latency timer");
69 int saa7134_no_overlay=-1;
70 module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
71 MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
72 " [some VIA/SIS chipsets are known to have problem with overlay]");
74 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
75 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
76 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
78 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
81 module_param_array(video_nr, int, NULL, 0444);
82 module_param_array(vbi_nr, int, NULL, 0444);
83 module_param_array(radio_nr, int, NULL, 0444);
84 module_param_array(tuner, int, NULL, 0444);
85 module_param_array(card, int, NULL, 0444);
87 MODULE_PARM_DESC(video_nr, "video device number");
88 MODULE_PARM_DESC(vbi_nr, "vbi device number");
89 MODULE_PARM_DESC(radio_nr, "radio device number");
90 MODULE_PARM_DESC(tuner, "tuner type");
91 MODULE_PARM_DESC(card, "card type");
93 static DEFINE_MUTEX(devlist_lock);
94 LIST_HEAD(saa7134_devlist);
95 static LIST_HEAD(mops_list);
96 static unsigned int saa7134_devcount;
98 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
99 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
101 #define dprintk(fmt, arg...) if (core_debug) \
102 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
104 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
106 unsigned long mode,status;
110 /* rising SAA7134_GPIO_GPRESCAN reads the status */
111 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
112 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
113 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
114 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
116 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
117 dev->name, mode, (~mode) & status, mode & status, msg);
120 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
126 case 0: /* static value */
127 case 1: dprintk("setting GPIO%d to static %d\n", bit_no, value);
128 /* turn sync mode off if necessary */
129 if (index & 0x00c00000)
130 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
135 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
136 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
138 case 3: /* tristate */
139 dprintk("setting GPIO%d to tristate\n", bit_no);
140 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
145 int saa7134_tuner_callback(void *ptr, int command, int arg)
148 struct saa7134_dev *dev = ptr;
150 switch (dev->tuner_type) {
151 case TUNER_PHILIPS_TDA8290:
153 case 0: /* switch LNA gain through GPIO 22*/
154 saa7134_set_gpio(dev, 22, arg) ;
156 case 1: /* vsync output at GPIO22. 50 / 60Hz */
157 dprintk("setting GPIO22 to vsync %d\n", arg);
158 saa_andorb(SAA7134_VIDEO_PORT_CTRL3, 0x80, 0x80);
159 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x03);
164 saa_writeb(SAA7134_VGATE_START, sync_control);
165 saa_writeb(SAA7134_VGATE_STOP, sync_control + 1);
166 saa_andorb(SAA7134_MISC_VGATE_MSB, 0x03, 0x00);
178 /* ------------------------------------------------------------------ */
181 /* ----------------------------------------------------------- */
182 /* delayed request_module */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
187 static void request_module_async(struct work_struct *work){
188 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
189 if (card_is_empress(dev))
190 request_module("saa7134-empress");
191 if (card_is_dvb(dev))
192 request_module("saa7134-dvb");
194 request_module("saa7134-alsa");
196 request_module("saa7134-oss");
199 static void request_submodules(struct saa7134_dev *dev)
201 INIT_WORK(&dev->request_module_wk, request_module_async);
202 schedule_work(&dev->request_module_wk);
206 #define request_submodules(dev)
207 #endif /* CONFIG_MODULES */
209 /* ------------------------------------------------------------------ */
211 /* nr of (saa7134-)pages for the given buffer size */
212 static int saa7134_buffer_pages(int size)
214 size = PAGE_ALIGN(size);
215 size += PAGE_SIZE; /* for non-page-aligned buffers */
220 /* calc max # of buffers from size (must not exceed the 4MB virtual
221 * address space per DMA channel) */
222 int saa7134_buffer_count(unsigned int size, unsigned int count)
224 unsigned int maxcount;
226 maxcount = 1024 / saa7134_buffer_pages(size);
227 if (count > maxcount)
232 int saa7134_buffer_startpage(struct saa7134_buf *buf)
234 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
237 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
240 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
242 base = saa7134_buffer_startpage(buf) * 4096;
243 base += dma->sglist[0].offset;
247 /* ------------------------------------------------------------------ */
249 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
254 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
257 pt->size = SAA7134_PGTABLE_SIZE;
263 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
264 struct scatterlist *list, unsigned int length,
265 unsigned int startpage)
270 BUG_ON(NULL == pt || NULL == pt->cpu);
272 ptr = pt->cpu + startpage;
273 for (i = 0; i < length; i++, list++)
274 for (p = 0; p * 4096 < list->length; p++, ptr++)
275 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
279 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
283 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
287 /* ------------------------------------------------------------------ */
289 void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
291 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
292 BUG_ON(in_interrupt());
294 videobuf_waiton(&buf->vb,0,0);
295 videobuf_dma_unmap(q, dma);
296 videobuf_dma_free(dma);
297 buf->vb.state = STATE_NEEDS_INIT;
300 /* ------------------------------------------------------------------ */
302 int saa7134_buffer_queue(struct saa7134_dev *dev,
303 struct saa7134_dmaqueue *q,
304 struct saa7134_buf *buf)
306 struct saa7134_buf *next = NULL;
308 assert_spin_locked(&dev->slock);
309 dprintk("buffer_queue %p\n",buf);
310 if (NULL == q->curr) {
313 buf->activate(dev,buf,NULL);
314 } else if (list_empty(&q->queue)) {
315 list_add_tail(&buf->vb.queue,&q->queue);
316 buf->vb.state = STATE_QUEUED;
318 next = list_entry(q->queue.next,struct saa7134_buf,
321 buf->activate(dev,buf,next);
324 list_add_tail(&buf->vb.queue,&q->queue);
325 buf->vb.state = STATE_QUEUED;
330 void saa7134_buffer_finish(struct saa7134_dev *dev,
331 struct saa7134_dmaqueue *q,
334 assert_spin_locked(&dev->slock);
335 dprintk("buffer_finish %p\n",q->curr);
337 /* finish current buffer */
338 q->curr->vb.state = state;
339 do_gettimeofday(&q->curr->vb.ts);
340 wake_up(&q->curr->vb.done);
344 void saa7134_buffer_next(struct saa7134_dev *dev,
345 struct saa7134_dmaqueue *q)
347 struct saa7134_buf *buf,*next = NULL;
349 assert_spin_locked(&dev->slock);
350 BUG_ON(NULL != q->curr);
352 if (!list_empty(&q->queue)) {
353 /* activate next one from queue */
354 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
355 dprintk("buffer_next %p [prev=%p/next=%p]\n",
356 buf,q->queue.prev,q->queue.next);
357 list_del(&buf->vb.queue);
358 if (!list_empty(&q->queue))
359 next = list_entry(q->queue.next,struct saa7134_buf,
362 buf->activate(dev,buf,next);
363 dprintk("buffer_next #2 prev=%p/next=%p\n",
364 q->queue.prev,q->queue.next);
366 /* nothing to do -- just stop DMA */
367 dprintk("buffer_next %p\n",NULL);
368 saa7134_set_dmabits(dev);
369 del_timer(&q->timeout);
373 void saa7134_buffer_timeout(unsigned long data)
375 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
376 struct saa7134_dev *dev = q->dev;
379 spin_lock_irqsave(&dev->slock,flags);
381 /* try to reset the hardware (SWRST) */
382 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
383 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
384 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
386 /* flag current buffer as failed,
387 try to start over with the next one. */
389 dprintk("timeout on %p\n",q->curr);
390 saa7134_buffer_finish(dev,q,STATE_ERROR);
392 saa7134_buffer_next(dev,q);
393 spin_unlock_irqrestore(&dev->slock,flags);
396 /* resends a current buffer in queue after resume */
398 int saa7134_buffer_requeue(struct saa7134_dev *dev,
399 struct saa7134_dmaqueue *q)
401 struct saa7134_buf *buf , *next;
404 spin_lock_irqsave(&dev->slock, flags);
409 dprintk("buffer_requeue\n");
412 spin_unlock_irqrestore(&dev->slock, flags);
416 dprintk("buffer_requeue : resending active buffers \n");
418 if (!list_empty(&q->queue))
419 next = list_entry(q->queue.next, struct saa7134_buf,
422 buf->activate(dev, buf, next);
423 spin_unlock_irqrestore(&dev->slock, flags);
428 /* ------------------------------------------------------------------ */
430 int saa7134_set_dmabits(struct saa7134_dev *dev)
432 u32 split, task=0, ctrl=0, irq=0;
433 enum v4l2_field cap = V4L2_FIELD_ANY;
434 enum v4l2_field ov = V4L2_FIELD_ANY;
436 assert_spin_locked(&dev->slock);
438 /* video capture -- dma 0 + video task A */
439 if (dev->video_q.curr) {
441 ctrl |= SAA7134_MAIN_CTRL_TE0;
442 irq |= SAA7134_IRQ1_INTE_RA0_1 |
443 SAA7134_IRQ1_INTE_RA0_0;
444 cap = dev->video_q.curr->vb.field;
447 /* video capture -- dma 1+2 (planar modes) */
448 if (dev->video_q.curr &&
449 dev->video_q.curr->fmt->planar) {
450 ctrl |= SAA7134_MAIN_CTRL_TE4 |
451 SAA7134_MAIN_CTRL_TE5;
454 /* screen overlay -- dma 0 + video task B */
457 ctrl |= SAA7134_MAIN_CTRL_TE1;
461 /* vbi capture -- dma 0 + vbi task A+B */
462 if (dev->vbi_q.curr) {
464 ctrl |= SAA7134_MAIN_CTRL_TE2 |
465 SAA7134_MAIN_CTRL_TE3;
466 irq |= SAA7134_IRQ1_INTE_RA0_7 |
467 SAA7134_IRQ1_INTE_RA0_6 |
468 SAA7134_IRQ1_INTE_RA0_5 |
469 SAA7134_IRQ1_INTE_RA0_4;
472 /* audio capture -- dma 3 */
473 if (dev->dmasound.dma_running) {
474 ctrl |= SAA7134_MAIN_CTRL_TE6;
475 irq |= SAA7134_IRQ1_INTE_RA3_1 |
476 SAA7134_IRQ1_INTE_RA3_0;
479 /* TS capture -- dma 5 */
480 if (dev->ts_q.curr) {
481 ctrl |= SAA7134_MAIN_CTRL_TE5;
482 irq |= SAA7134_IRQ1_INTE_RA2_3 |
483 SAA7134_IRQ1_INTE_RA2_2 |
484 SAA7134_IRQ1_INTE_RA2_1 |
485 SAA7134_IRQ1_INTE_RA2_0;
488 /* set task conditions + field handling */
489 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
490 /* default config -- use full frames */
491 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
492 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
493 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
494 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
497 /* split fields between tasks */
498 if (V4L2_FIELD_TOP == cap) {
499 /* odd A, even B, repeat */
500 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
501 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
503 /* odd B, even A, repeat */
504 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
505 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
507 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
508 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
513 saa_writeb(SAA7134_REGION_ENABLE, task);
514 saa_writel(SAA7134_IRQ1, irq);
515 saa_andorl(SAA7134_MAIN_CTRL,
516 SAA7134_MAIN_CTRL_TE0 |
517 SAA7134_MAIN_CTRL_TE1 |
518 SAA7134_MAIN_CTRL_TE2 |
519 SAA7134_MAIN_CTRL_TE3 |
520 SAA7134_MAIN_CTRL_TE4 |
521 SAA7134_MAIN_CTRL_TE5 |
522 SAA7134_MAIN_CTRL_TE6,
524 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
525 task, ctrl, irq, split ? "no" : "yes");
530 /* ------------------------------------------------------------------ */
531 /* IRQ handler + helpers */
533 static char *irqbits[] = {
534 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
535 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
536 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
537 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
539 #define IRQBITS ARRAY_SIZE(irqbits)
541 static void print_irqstatus(struct saa7134_dev *dev, int loop,
542 unsigned long report, unsigned long status)
546 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
547 dev->name,loop,jiffies,report,status);
548 for (i = 0; i < IRQBITS; i++) {
549 if (!(report & (1 << i)))
551 printk(" %s",irqbits[i]);
553 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
554 printk(" | RA0=%s,%s,%s,%ld",
555 (status & 0x40) ? "vbi" : "video",
556 (status & 0x20) ? "b" : "a",
557 (status & 0x10) ? "odd" : "even",
563 static irqreturn_t saa7134_irq(int irq, void *dev_id)
565 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
566 unsigned long report,status;
567 int loop, handled = 0;
569 for (loop = 0; loop < 10; loop++) {
570 report = saa_readl(SAA7134_IRQ_REPORT);
571 status = saa_readl(SAA7134_IRQ_STATUS);
574 printk(KERN_DEBUG "%s/irq: no (more) work\n",
579 /* If dmasound support is active and we get a sound report, exit
580 and let the saa7134-alsa/oss module deal with it */
582 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
583 (dev->dmasound.priv_data != NULL) )
586 printk(KERN_DEBUG "%s/irq: ignoring interrupt for DMA sound\n",
592 saa_writel(SAA7134_IRQ_REPORT,report);
594 print_irqstatus(dev,loop,report,status);
597 if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
598 (report & SAA7134_IRQ_REPORT_INTL))
599 saa7134_irq_video_signalchange(dev);
602 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
603 (status & 0x60) == 0)
604 saa7134_irq_video_done(dev,status);
606 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
607 (status & 0x40) == 0x40)
608 saa7134_irq_vbi_done(dev,status);
610 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
612 saa7134_irq_ts_done(dev,status);
614 if (report & SAA7134_IRQ_REPORT_GPIO16) {
615 switch (dev->has_remote) {
616 case SAA7134_REMOTE_GPIO:
619 if (dev->remote->mask_keydown & 0x10000) {
620 saa7134_input_irq(dev);
624 case SAA7134_REMOTE_I2C:
625 break; /* FIXME: invoke I2C get_key() */
627 default: /* GPIO16 not used by IR remote */
632 if (report & SAA7134_IRQ_REPORT_GPIO18) {
633 switch (dev->has_remote) {
634 case SAA7134_REMOTE_GPIO:
637 if ((dev->remote->mask_keydown & 0x40000) ||
638 (dev->remote->mask_keyup & 0x40000)) {
639 saa7134_input_irq(dev);
643 case SAA7134_REMOTE_I2C:
644 break; /* FIXME: invoke I2C get_key() */
646 default: /* GPIO18 not used by IR remote */
653 print_irqstatus(dev,loop,report,status);
654 if (report & SAA7134_IRQ_REPORT_PE) {
655 /* disable all parity error */
656 printk(KERN_WARNING "%s/irq: looping -- "
657 "clearing PE (parity error!) enable bit\n",dev->name);
658 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
659 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
660 /* disable gpio16 IRQ */
661 printk(KERN_WARNING "%s/irq: looping -- "
662 "clearing GPIO16 enable bit\n",dev->name);
663 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
664 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
665 /* disable gpio18 IRQs */
666 printk(KERN_WARNING "%s/irq: looping -- "
667 "clearing GPIO18 enable bit\n",dev->name);
668 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
670 /* disable all irqs */
671 printk(KERN_WARNING "%s/irq: looping -- "
672 "clearing all enable bits\n",dev->name);
673 saa_writel(SAA7134_IRQ1,0);
674 saa_writel(SAA7134_IRQ2,0);
679 return IRQ_RETVAL(handled);
682 /* ------------------------------------------------------------------ */
684 /* early init (no i2c, no irq) */
686 static int saa7134_hw_enable1(struct saa7134_dev *dev)
688 /* RAM FIFO config */
689 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
690 saa_writel(SAA7134_THRESHOULD, 0x02020202);
692 /* enable audio + video processing */
693 saa_writel(SAA7134_MAIN_CTRL,
694 SAA7134_MAIN_CTRL_VPLLE |
695 SAA7134_MAIN_CTRL_APLLE |
696 SAA7134_MAIN_CTRL_EXOSC |
697 SAA7134_MAIN_CTRL_EVFE1 |
698 SAA7134_MAIN_CTRL_EVFE2 |
699 SAA7134_MAIN_CTRL_ESFE |
700 SAA7134_MAIN_CTRL_EBDAC);
703 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
704 * OSS initialization writes to registers via the audio DSP; these
705 * writes will fail unless the audio clock has been started. At worst,
706 * audio will not work.
709 /* enable peripheral devices */
710 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
712 /* set vertical line numbering start (vbi needs this) */
713 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
718 static int saa7134_hwinit1(struct saa7134_dev *dev)
720 dprintk("hwinit1\n");
722 saa_writel(SAA7134_IRQ1, 0);
723 saa_writel(SAA7134_IRQ2, 0);
724 mutex_init(&dev->lock);
725 spin_lock_init(&dev->slock);
727 saa7134_track_gpio(dev,"pre-init");
728 saa7134_video_init1(dev);
729 saa7134_vbi_init1(dev);
730 if (card_has_mpeg(dev))
731 saa7134_ts_init1(dev);
732 saa7134_input_init1(dev);
734 saa7134_hw_enable1(dev);
739 /* late init (with i2c + irq) */
740 static int saa7134_hw_enable2(struct saa7134_dev *dev)
743 unsigned int irq2_mask;
747 SAA7134_IRQ2_INTE_DEC3 |
748 SAA7134_IRQ2_INTE_DEC2 |
749 SAA7134_IRQ2_INTE_DEC1 |
750 SAA7134_IRQ2_INTE_DEC0 |
751 SAA7134_IRQ2_INTE_PE |
752 SAA7134_IRQ2_INTE_AR;
754 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
755 if (dev->remote->mask_keydown & 0x10000)
756 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
757 else if (dev->remote->mask_keydown & 0x40000)
758 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
759 else if (dev->remote->mask_keyup & 0x40000)
760 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
763 saa_writel(SAA7134_IRQ1, 0);
764 saa_writel(SAA7134_IRQ2, irq2_mask);
769 static int saa7134_hwinit2(struct saa7134_dev *dev)
772 dprintk("hwinit2\n");
774 saa7134_video_init2(dev);
775 saa7134_tvaudio_init2(dev);
777 saa7134_hw_enable2(dev);
784 static int saa7134_hwfini(struct saa7134_dev *dev)
788 if (card_has_mpeg(dev))
789 saa7134_ts_fini(dev);
790 saa7134_input_fini(dev);
791 saa7134_vbi_fini(dev);
792 saa7134_tvaudio_fini(dev);
796 static void __devinit must_configure_manually(void)
802 "saa7134: Congratulations! Your TV card vendor saved a few\n"
803 "saa7134: cents for a eeprom, thus your pci board has no\n"
804 "saa7134: subsystem ID and I can't identify it automatically\n"
806 "saa7134: I feel better now. Ok, here are the good news:\n"
807 "saa7134: You can use the card=<nr> insmod option to specify\n"
808 "saa7134: which board do you have. The list:\n");
809 for (i = 0; i < saa7134_bcount; i++) {
810 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
811 i,saa7134_boards[i].name);
812 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
813 if (saa7134_pci_tbl[p].driver_data != i)
816 saa7134_pci_tbl[p].subvendor,
817 saa7134_pci_tbl[p].subdevice);
823 static struct video_device *vdev_init(struct saa7134_dev *dev,
824 struct video_device *template,
827 struct video_device *vfd;
829 vfd = video_device_alloc();
834 vfd->dev = &dev->pci->dev;
835 vfd->release = video_device_release;
836 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
837 dev->name, type, saa7134_boards[dev->board].name);
841 static void saa7134_unregister_video(struct saa7134_dev *dev)
843 if (dev->video_dev) {
844 if (-1 != dev->video_dev->minor)
845 video_unregister_device(dev->video_dev);
847 video_device_release(dev->video_dev);
848 dev->video_dev = NULL;
851 if (-1 != dev->vbi_dev->minor)
852 video_unregister_device(dev->vbi_dev);
854 video_device_release(dev->vbi_dev);
857 if (dev->radio_dev) {
858 if (-1 != dev->radio_dev->minor)
859 video_unregister_device(dev->radio_dev);
861 video_device_release(dev->radio_dev);
862 dev->radio_dev = NULL;
866 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
867 struct saa7134_dev *dev)
871 if (NULL != dev->mops)
873 if (saa7134_boards[dev->board].mpeg != ops->type)
875 err = ops->init(dev);
881 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
882 struct saa7134_dev *dev)
884 if (NULL == dev->mops)
886 if (dev->mops != ops)
888 dev->mops->fini(dev);
892 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
893 const struct pci_device_id *pci_id)
895 struct saa7134_dev *dev;
896 struct list_head *item;
897 struct saa7134_mpeg_ops *mops;
900 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
906 if (pci_enable_device(pci_dev)) {
911 dev->nr = saa7134_devcount;
912 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
915 if (pci_pci_problems) {
916 if (pci_pci_problems & PCIPCI_TRITON)
917 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
918 if (pci_pci_problems & PCIPCI_NATOMA)
919 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
920 if (pci_pci_problems & PCIPCI_VIAETBF)
921 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
922 if (pci_pci_problems & PCIPCI_VSFX)
923 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
924 #ifdef PCIPCI_ALIMAGIK
925 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
926 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
931 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
932 printk(KERN_INFO "%s: quirk: this driver and your "
933 "chipset may not work together"
934 " in overlay mode.\n",dev->name);
935 if (!saa7134_no_overlay) {
936 printk(KERN_INFO "%s: quirk: overlay "
937 "mode will be disabled.\n",
939 saa7134_no_overlay = 1;
941 printk(KERN_INFO "%s: quirk: overlay "
942 "mode will be forced. Use this"
943 " option at your own risk.\n",
948 if (UNSET != latency) {
949 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
951 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
955 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
956 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
957 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
958 "latency: %d, mmio: 0x%llx\n", dev->name,
959 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
960 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
961 pci_set_master(pci_dev);
962 if (!pci_dma_supported(pci_dev, DMA_32BIT_MASK)) {
963 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
969 dev->board = pci_id->driver_data;
970 if (card[dev->nr] >= 0 &&
971 card[dev->nr] < saa7134_bcount)
972 dev->board = card[dev->nr];
973 if (SAA7134_BOARD_NOAUTO == dev->board) {
974 must_configure_manually();
975 dev->board = SAA7134_BOARD_UNKNOWN;
977 dev->autodetected = card[dev->nr] != dev->board;
978 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
979 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
980 if (UNSET != tuner[dev->nr])
981 dev->tuner_type = tuner[dev->nr];
982 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
983 dev->name,pci_dev->subsystem_vendor,
984 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
985 dev->board, dev->autodetected ?
986 "autodetected" : "insmod option");
989 if (!request_mem_region(pci_resource_start(pci_dev,0),
990 pci_resource_len(pci_dev,0),
993 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
994 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
997 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
998 dev->bmmio = (__u8 __iomem *)dev->lmmio;
999 if (NULL == dev->lmmio) {
1001 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
1006 /* initialize hardware #1 */
1007 saa7134_board_init1(dev);
1008 saa7134_hwinit1(dev);
1011 err = request_irq(pci_dev->irq, saa7134_irq,
1012 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
1014 printk(KERN_ERR "%s: can't get IRQ %d\n",
1015 dev->name,pci_dev->irq);
1019 /* wait a bit, register i2c bus */
1021 saa7134_i2c_register(dev);
1023 /* initialize hardware #2 */
1024 saa7134_board_init2(dev);
1025 saa7134_hwinit2(dev);
1027 /* load i2c helpers */
1028 if (TUNER_ABSENT != dev->tuner_type)
1029 request_module("tuner");
1030 if (card_is_empress(dev)) {
1031 request_module("saa6752hs");
1034 request_submodules(dev);
1036 v4l2_prio_init(&dev->prio);
1038 /* register v4l devices */
1039 if (saa7134_no_overlay <= 0) {
1040 saa7134_video_template.type |= VID_TYPE_OVERLAY;
1042 printk("%s: Overlay support disabled.\n",dev->name);
1044 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1045 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1048 printk(KERN_INFO "%s: can't register video device\n",
1052 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1053 dev->name,dev->video_dev->minor & 0x1f);
1055 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
1056 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1060 printk(KERN_INFO "%s: registered device vbi%d\n",
1061 dev->name,dev->vbi_dev->minor & 0x1f);
1063 if (card_has_radio(dev)) {
1064 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1065 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1069 printk(KERN_INFO "%s: registered device radio%d\n",
1070 dev->name,dev->radio_dev->minor & 0x1f);
1073 /* everything worked */
1074 pci_set_drvdata(pci_dev,dev);
1077 mutex_lock(&devlist_lock);
1078 list_for_each(item,&mops_list) {
1079 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1080 mpeg_ops_attach(mops, dev);
1082 list_add_tail(&dev->devlist,&saa7134_devlist);
1083 mutex_unlock(&devlist_lock);
1085 /* check for signal */
1086 saa7134_irq_video_signalchange(dev);
1088 if (saa7134_dmasound_init && !dev->dmasound.priv_data) {
1089 saa7134_dmasound_init(dev);
1092 if (TUNER_ABSENT != dev->tuner_type)
1093 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1098 saa7134_unregister_video(dev);
1099 saa7134_i2c_unregister(dev);
1100 free_irq(pci_dev->irq, dev);
1102 saa7134_hwfini(dev);
1103 iounmap(dev->lmmio);
1105 release_mem_region(pci_resource_start(pci_dev,0),
1106 pci_resource_len(pci_dev,0));
1112 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1114 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1115 struct list_head *item;
1116 struct saa7134_mpeg_ops *mops;
1118 /* Release DMA sound modules if present */
1119 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1120 saa7134_dmasound_exit(dev);
1125 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1126 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1127 print_irqstatus(dev,42,report,status);
1130 /* disable peripheral devices */
1131 saa_writeb(SAA7134_SPECIAL_MODE,0);
1133 /* shutdown hardware */
1134 saa_writel(SAA7134_IRQ1,0);
1135 saa_writel(SAA7134_IRQ2,0);
1136 saa_writel(SAA7134_MAIN_CTRL,0);
1138 /* shutdown subsystems */
1139 saa7134_hwfini(dev);
1142 mutex_lock(&devlist_lock);
1143 list_del(&dev->devlist);
1144 list_for_each(item,&mops_list) {
1145 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1146 mpeg_ops_detach(mops, dev);
1148 mutex_unlock(&devlist_lock);
1151 saa7134_i2c_unregister(dev);
1152 saa7134_unregister_video(dev);
1155 /* the DMA sound modules should be unloaded before reaching
1156 this, but just in case they are still present... */
1157 if (dev->dmasound.priv_data != NULL) {
1158 free_irq(pci_dev->irq, &dev->dmasound);
1159 dev->dmasound.priv_data = NULL;
1163 /* release resources */
1164 free_irq(pci_dev->irq, dev);
1165 iounmap(dev->lmmio);
1166 release_mem_region(pci_resource_start(pci_dev,0),
1167 pci_resource_len(pci_dev,0));
1169 pci_set_drvdata(pci_dev, NULL);
1175 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1178 /* Disable card's IRQs to prevent it from resuming computer */
1180 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1182 saa_writel(SAA7134_IRQ1, 0);
1183 saa_writel(SAA7134_IRQ2, 0);
1186 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1187 pci_save_state(pci_dev);
1192 static int saa7134_resume(struct pci_dev *pci_dev)
1195 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1197 pci_restore_state(pci_dev);
1198 pci_set_power_state(pci_dev, PCI_D0);
1200 /* Do things that are done in saa7134_initdev ,
1201 except of initializing memory structures.*/
1203 saa7134_board_init1(dev);
1205 if (saa7134_boards[dev->board].video_out)
1206 saa7134_videoport_init(dev);
1208 if (card_has_mpeg(dev))
1209 saa7134_ts_init_hw(dev);
1211 saa7134_hw_enable1(dev);
1213 saa7134_set_decoder(dev);
1215 saa7134_i2c_call_clients(dev, VIDIOC_S_STD, &dev->tvnorm->id);
1217 saa7134_board_init2(dev);
1218 saa7134_hw_enable2(dev);
1220 dev->force_mute_update = 1;
1221 saa7134_tvaudio_setmute(dev);
1222 dev->force_mute_update = 0;
1223 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1224 saa7134_enable_i2s(dev);
1226 /*recapture unfinished buffer(s)*/
1227 saa7134_buffer_requeue(dev, &dev->video_q);
1228 saa7134_buffer_requeue(dev, &dev->vbi_q);
1229 saa7134_buffer_requeue(dev, &dev->ts_q);
1234 /* ----------------------------------------------------------- */
1236 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1238 struct list_head *item;
1239 struct saa7134_dev *dev;
1241 mutex_lock(&devlist_lock);
1242 list_for_each(item,&saa7134_devlist) {
1243 dev = list_entry(item, struct saa7134_dev, devlist);
1244 mpeg_ops_attach(ops, dev);
1246 list_add_tail(&ops->next,&mops_list);
1247 mutex_unlock(&devlist_lock);
1251 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1253 struct list_head *item;
1254 struct saa7134_dev *dev;
1256 mutex_lock(&devlist_lock);
1257 list_del(&ops->next);
1258 list_for_each(item,&saa7134_devlist) {
1259 dev = list_entry(item, struct saa7134_dev, devlist);
1260 mpeg_ops_detach(ops, dev);
1262 mutex_unlock(&devlist_lock);
1265 EXPORT_SYMBOL(saa7134_ts_register);
1266 EXPORT_SYMBOL(saa7134_ts_unregister);
1268 /* ----------------------------------------------------------- */
1270 static struct pci_driver saa7134_pci_driver = {
1272 .id_table = saa7134_pci_tbl,
1273 .probe = saa7134_initdev,
1274 .remove = __devexit_p(saa7134_finidev),
1275 .suspend = saa7134_suspend,
1276 .resume = saa7134_resume
1279 static int saa7134_init(void)
1281 INIT_LIST_HEAD(&saa7134_devlist);
1282 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1283 (SAA7134_VERSION_CODE >> 16) & 0xff,
1284 (SAA7134_VERSION_CODE >> 8) & 0xff,
1285 SAA7134_VERSION_CODE & 0xff);
1287 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1288 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1290 return pci_register_driver(&saa7134_pci_driver);
1293 static void saa7134_fini(void)
1295 pci_unregister_driver(&saa7134_pci_driver);
1298 module_init(saa7134_init);
1299 module_exit(saa7134_fini);
1301 /* ----------------------------------------------------------- */
1303 EXPORT_SYMBOL(saa7134_set_gpio);
1304 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1305 EXPORT_SYMBOL(saa7134_devlist);
1306 EXPORT_SYMBOL(saa7134_boards);
1308 /* ----------------- for the DMA sound modules --------------- */
1310 EXPORT_SYMBOL(saa7134_dmasound_init);
1311 EXPORT_SYMBOL(saa7134_dmasound_exit);
1312 EXPORT_SYMBOL(saa7134_pgtable_free);
1313 EXPORT_SYMBOL(saa7134_pgtable_build);
1314 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1315 EXPORT_SYMBOL(saa7134_set_dmabits);
1317 /* ----------------------------------------------------------- */