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;
46 module_param(irq_debug, int, 0644);
47 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
49 static unsigned int core_debug;
50 module_param(core_debug, int, 0644);
51 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
53 static unsigned int gpio_tracking;
54 module_param(gpio_tracking, int, 0644);
55 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
57 static unsigned int alsa = 1;
58 module_param(alsa, int, 0644);
59 MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
61 static unsigned int latency = UNSET;
62 module_param(latency, int, 0444);
63 MODULE_PARM_DESC(latency,"pci latency timer");
65 int saa7134_no_overlay=-1;
66 module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
67 MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
68 " [some VIA/SIS chipsets are known to have problem with overlay]");
70 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
71 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
72 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
73 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
74 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77 module_param_array(video_nr, int, NULL, 0444);
78 module_param_array(vbi_nr, int, NULL, 0444);
79 module_param_array(radio_nr, int, NULL, 0444);
80 module_param_array(tuner, int, NULL, 0444);
81 module_param_array(card, int, NULL, 0444);
83 MODULE_PARM_DESC(video_nr, "video device number");
84 MODULE_PARM_DESC(vbi_nr, "vbi device number");
85 MODULE_PARM_DESC(radio_nr, "radio device number");
86 MODULE_PARM_DESC(tuner, "tuner type");
87 MODULE_PARM_DESC(card, "card type");
89 DEFINE_MUTEX(saa7134_devlist_lock);
90 EXPORT_SYMBOL(saa7134_devlist_lock);
91 LIST_HEAD(saa7134_devlist);
92 EXPORT_SYMBOL(saa7134_devlist);
93 static LIST_HEAD(mops_list);
94 static unsigned int saa7134_devcount;
96 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
97 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
99 #define dprintk(fmt, arg...) if (core_debug) \
100 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
102 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
104 unsigned long mode,status;
108 /* rising SAA7134_GPIO_GPRESCAN reads the status */
109 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
110 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
111 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
112 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
114 "%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
115 dev->name, mode, (~mode) & status, mode & status, msg);
118 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
124 case 0: /* static value */
125 case 1: dprintk("setting GPIO%d to static %d\n", bit_no, value);
126 /* turn sync mode off if necessary */
127 if (index & 0x00c00000)
128 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
133 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
134 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
136 case 3: /* tristate */
137 dprintk("setting GPIO%d to tristate\n", bit_no);
138 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
143 /* ------------------------------------------------------------------ */
146 /* ----------------------------------------------------------- */
147 /* delayed request_module */
149 #if defined(CONFIG_MODULES) && defined(MODULE)
151 static void request_module_async(struct work_struct *work){
152 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
153 if (card_is_empress(dev))
154 request_module("saa7134-empress");
155 if (card_is_dvb(dev))
156 request_module("saa7134-dvb");
158 if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
159 request_module("saa7134-alsa");
163 static void request_submodules(struct saa7134_dev *dev)
165 INIT_WORK(&dev->request_module_wk, request_module_async);
166 schedule_work(&dev->request_module_wk);
170 #define request_submodules(dev)
171 #endif /* CONFIG_MODULES */
173 /* ------------------------------------------------------------------ */
175 /* nr of (saa7134-)pages for the given buffer size */
176 static int saa7134_buffer_pages(int size)
178 size = PAGE_ALIGN(size);
179 size += PAGE_SIZE; /* for non-page-aligned buffers */
184 /* calc max # of buffers from size (must not exceed the 4MB virtual
185 * address space per DMA channel) */
186 int saa7134_buffer_count(unsigned int size, unsigned int count)
188 unsigned int maxcount;
190 maxcount = 1024 / saa7134_buffer_pages(size);
191 if (count > maxcount)
196 int saa7134_buffer_startpage(struct saa7134_buf *buf)
198 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
201 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
204 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
206 base = saa7134_buffer_startpage(buf) * 4096;
207 base += dma->sglist[0].offset;
211 /* ------------------------------------------------------------------ */
213 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
216 dma_addr_t dma_addr = 0;
218 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
221 pt->size = SAA7134_PGTABLE_SIZE;
227 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
228 struct scatterlist *list, unsigned int length,
229 unsigned int startpage)
234 BUG_ON(NULL == pt || NULL == pt->cpu);
236 ptr = pt->cpu + startpage;
237 for (i = 0; i < length; i++, list++)
238 for (p = 0; p * 4096 < list->length; p++, ptr++)
239 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
243 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
247 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
251 /* ------------------------------------------------------------------ */
253 void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
255 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
256 BUG_ON(in_interrupt());
258 videobuf_waiton(&buf->vb,0,0);
259 videobuf_dma_unmap(q, dma);
260 videobuf_dma_free(dma);
261 buf->vb.state = VIDEOBUF_NEEDS_INIT;
264 /* ------------------------------------------------------------------ */
266 int saa7134_buffer_queue(struct saa7134_dev *dev,
267 struct saa7134_dmaqueue *q,
268 struct saa7134_buf *buf)
270 struct saa7134_buf *next = NULL;
272 assert_spin_locked(&dev->slock);
273 dprintk("buffer_queue %p\n",buf);
274 if (NULL == q->curr) {
277 buf->activate(dev,buf,NULL);
278 } else if (list_empty(&q->queue)) {
279 list_add_tail(&buf->vb.queue,&q->queue);
280 buf->vb.state = VIDEOBUF_QUEUED;
282 next = list_entry(q->queue.next,struct saa7134_buf,
285 buf->activate(dev,buf,next);
288 list_add_tail(&buf->vb.queue,&q->queue);
289 buf->vb.state = VIDEOBUF_QUEUED;
294 void saa7134_buffer_finish(struct saa7134_dev *dev,
295 struct saa7134_dmaqueue *q,
298 assert_spin_locked(&dev->slock);
299 dprintk("buffer_finish %p\n",q->curr);
301 /* finish current buffer */
302 q->curr->vb.state = state;
303 do_gettimeofday(&q->curr->vb.ts);
304 wake_up(&q->curr->vb.done);
308 void saa7134_buffer_next(struct saa7134_dev *dev,
309 struct saa7134_dmaqueue *q)
311 struct saa7134_buf *buf,*next = NULL;
313 assert_spin_locked(&dev->slock);
314 BUG_ON(NULL != q->curr);
316 if (!list_empty(&q->queue)) {
317 /* activate next one from queue */
318 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
319 dprintk("buffer_next %p [prev=%p/next=%p]\n",
320 buf,q->queue.prev,q->queue.next);
321 list_del(&buf->vb.queue);
322 if (!list_empty(&q->queue))
323 next = list_entry(q->queue.next,struct saa7134_buf,
326 buf->activate(dev,buf,next);
327 dprintk("buffer_next #2 prev=%p/next=%p\n",
328 q->queue.prev,q->queue.next);
330 /* nothing to do -- just stop DMA */
331 dprintk("buffer_next %p\n",NULL);
332 saa7134_set_dmabits(dev);
333 del_timer(&q->timeout);
337 void saa7134_buffer_timeout(unsigned long data)
339 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
340 struct saa7134_dev *dev = q->dev;
343 spin_lock_irqsave(&dev->slock,flags);
345 /* try to reset the hardware (SWRST) */
346 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
347 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
348 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
350 /* flag current buffer as failed,
351 try to start over with the next one. */
353 dprintk("timeout on %p\n",q->curr);
354 saa7134_buffer_finish(dev,q,VIDEOBUF_ERROR);
356 saa7134_buffer_next(dev,q);
357 spin_unlock_irqrestore(&dev->slock,flags);
360 /* ------------------------------------------------------------------ */
362 int saa7134_set_dmabits(struct saa7134_dev *dev)
364 u32 split, task=0, ctrl=0, irq=0;
365 enum v4l2_field cap = V4L2_FIELD_ANY;
366 enum v4l2_field ov = V4L2_FIELD_ANY;
368 assert_spin_locked(&dev->slock);
373 /* video capture -- dma 0 + video task A */
374 if (dev->video_q.curr) {
376 ctrl |= SAA7134_MAIN_CTRL_TE0;
377 irq |= SAA7134_IRQ1_INTE_RA0_1 |
378 SAA7134_IRQ1_INTE_RA0_0;
379 cap = dev->video_q.curr->vb.field;
382 /* video capture -- dma 1+2 (planar modes) */
383 if (dev->video_q.curr &&
384 dev->video_q.curr->fmt->planar) {
385 ctrl |= SAA7134_MAIN_CTRL_TE4 |
386 SAA7134_MAIN_CTRL_TE5;
389 /* screen overlay -- dma 0 + video task B */
392 ctrl |= SAA7134_MAIN_CTRL_TE1;
396 /* vbi capture -- dma 0 + vbi task A+B */
397 if (dev->vbi_q.curr) {
399 ctrl |= SAA7134_MAIN_CTRL_TE2 |
400 SAA7134_MAIN_CTRL_TE3;
401 irq |= SAA7134_IRQ1_INTE_RA0_7 |
402 SAA7134_IRQ1_INTE_RA0_6 |
403 SAA7134_IRQ1_INTE_RA0_5 |
404 SAA7134_IRQ1_INTE_RA0_4;
407 /* audio capture -- dma 3 */
408 if (dev->dmasound.dma_running) {
409 ctrl |= SAA7134_MAIN_CTRL_TE6;
410 irq |= SAA7134_IRQ1_INTE_RA3_1 |
411 SAA7134_IRQ1_INTE_RA3_0;
414 /* TS capture -- dma 5 */
415 if (dev->ts_q.curr) {
416 ctrl |= SAA7134_MAIN_CTRL_TE5;
417 irq |= SAA7134_IRQ1_INTE_RA2_1 |
418 SAA7134_IRQ1_INTE_RA2_0;
421 /* set task conditions + field handling */
422 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
423 /* default config -- use full frames */
424 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
425 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
426 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
427 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
430 /* split fields between tasks */
431 if (V4L2_FIELD_TOP == cap) {
432 /* odd A, even B, repeat */
433 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
434 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
436 /* odd B, even A, repeat */
437 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
438 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
440 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
441 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
446 saa_writeb(SAA7134_REGION_ENABLE, task);
447 saa_writel(SAA7134_IRQ1, irq);
448 saa_andorl(SAA7134_MAIN_CTRL,
449 SAA7134_MAIN_CTRL_TE0 |
450 SAA7134_MAIN_CTRL_TE1 |
451 SAA7134_MAIN_CTRL_TE2 |
452 SAA7134_MAIN_CTRL_TE3 |
453 SAA7134_MAIN_CTRL_TE4 |
454 SAA7134_MAIN_CTRL_TE5 |
455 SAA7134_MAIN_CTRL_TE6,
457 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
458 task, ctrl, irq, split ? "no" : "yes");
463 /* ------------------------------------------------------------------ */
464 /* IRQ handler + helpers */
466 static char *irqbits[] = {
467 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
468 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
469 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
470 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
472 #define IRQBITS ARRAY_SIZE(irqbits)
474 static void print_irqstatus(struct saa7134_dev *dev, int loop,
475 unsigned long report, unsigned long status)
479 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
480 dev->name,loop,jiffies,report,status);
481 for (i = 0; i < IRQBITS; i++) {
482 if (!(report & (1 << i)))
484 printk(" %s",irqbits[i]);
486 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
487 printk(" | RA0=%s,%s,%s,%ld",
488 (status & 0x40) ? "vbi" : "video",
489 (status & 0x20) ? "b" : "a",
490 (status & 0x10) ? "odd" : "even",
496 static irqreturn_t saa7134_irq(int irq, void *dev_id)
498 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
499 unsigned long report,status;
500 int loop, handled = 0;
505 for (loop = 0; loop < 10; loop++) {
506 report = saa_readl(SAA7134_IRQ_REPORT);
507 status = saa_readl(SAA7134_IRQ_STATUS);
509 /* If dmasound support is active and we get a sound report,
510 * mask out the report and let the saa7134-alsa module deal
512 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
513 (dev->dmasound.priv_data != NULL) )
516 printk(KERN_DEBUG "%s/irq: preserving DMA sound interrupt\n",
518 report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
523 printk(KERN_DEBUG "%s/irq: no (more) work\n",
529 saa_writel(SAA7134_IRQ_REPORT,report);
531 print_irqstatus(dev,loop,report,status);
534 if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
535 (report & SAA7134_IRQ_REPORT_INTL))
536 saa7134_irq_video_signalchange(dev);
539 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
540 (status & 0x60) == 0)
541 saa7134_irq_video_done(dev,status);
543 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
544 (status & 0x40) == 0x40)
545 saa7134_irq_vbi_done(dev,status);
547 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
549 saa7134_irq_ts_done(dev,status);
551 if (report & SAA7134_IRQ_REPORT_GPIO16) {
552 switch (dev->has_remote) {
553 case SAA7134_REMOTE_GPIO:
556 if (dev->remote->mask_keydown & 0x10000) {
557 saa7134_input_irq(dev);
561 case SAA7134_REMOTE_I2C:
562 break; /* FIXME: invoke I2C get_key() */
564 default: /* GPIO16 not used by IR remote */
569 if (report & SAA7134_IRQ_REPORT_GPIO18) {
570 switch (dev->has_remote) {
571 case SAA7134_REMOTE_GPIO:
574 if ((dev->remote->mask_keydown & 0x40000) ||
575 (dev->remote->mask_keyup & 0x40000)) {
576 saa7134_input_irq(dev);
580 case SAA7134_REMOTE_I2C:
581 break; /* FIXME: invoke I2C get_key() */
583 default: /* GPIO18 not used by IR remote */
590 print_irqstatus(dev,loop,report,status);
591 if (report & SAA7134_IRQ_REPORT_PE) {
592 /* disable all parity error */
593 printk(KERN_WARNING "%s/irq: looping -- "
594 "clearing PE (parity error!) enable bit\n",dev->name);
595 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
596 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
597 /* disable gpio16 IRQ */
598 printk(KERN_WARNING "%s/irq: looping -- "
599 "clearing GPIO16 enable bit\n",dev->name);
600 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
601 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
602 /* disable gpio18 IRQs */
603 printk(KERN_WARNING "%s/irq: looping -- "
604 "clearing GPIO18 enable bit\n",dev->name);
605 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
607 /* disable all irqs */
608 printk(KERN_WARNING "%s/irq: looping -- "
609 "clearing all enable bits\n",dev->name);
610 saa_writel(SAA7134_IRQ1,0);
611 saa_writel(SAA7134_IRQ2,0);
616 return IRQ_RETVAL(handled);
619 /* ------------------------------------------------------------------ */
621 /* early init (no i2c, no irq) */
623 static int saa7134_hw_enable1(struct saa7134_dev *dev)
625 /* RAM FIFO config */
626 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
627 saa_writel(SAA7134_THRESHOULD, 0x02020202);
629 /* enable audio + video processing */
630 saa_writel(SAA7134_MAIN_CTRL,
631 SAA7134_MAIN_CTRL_VPLLE |
632 SAA7134_MAIN_CTRL_APLLE |
633 SAA7134_MAIN_CTRL_EXOSC |
634 SAA7134_MAIN_CTRL_EVFE1 |
635 SAA7134_MAIN_CTRL_EVFE2 |
636 SAA7134_MAIN_CTRL_ESFE |
637 SAA7134_MAIN_CTRL_EBDAC);
640 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
641 * OSS initialization writes to registers via the audio DSP; these
642 * writes will fail unless the audio clock has been started. At worst,
643 * audio will not work.
646 /* enable peripheral devices */
647 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
649 /* set vertical line numbering start (vbi needs this) */
650 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
655 static int saa7134_hwinit1(struct saa7134_dev *dev)
657 dprintk("hwinit1\n");
659 saa_writel(SAA7134_IRQ1, 0);
660 saa_writel(SAA7134_IRQ2, 0);
662 /* Clear any stale IRQ reports */
663 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
665 mutex_init(&dev->lock);
666 spin_lock_init(&dev->slock);
668 saa7134_track_gpio(dev,"pre-init");
669 saa7134_video_init1(dev);
670 saa7134_vbi_init1(dev);
671 if (card_has_mpeg(dev))
672 saa7134_ts_init1(dev);
673 saa7134_input_init1(dev);
675 saa7134_hw_enable1(dev);
680 /* late init (with i2c + irq) */
681 static int saa7134_hw_enable2(struct saa7134_dev *dev)
684 unsigned int irq2_mask;
688 SAA7134_IRQ2_INTE_DEC3 |
689 SAA7134_IRQ2_INTE_DEC2 |
690 SAA7134_IRQ2_INTE_DEC1 |
691 SAA7134_IRQ2_INTE_DEC0 |
692 SAA7134_IRQ2_INTE_PE |
693 SAA7134_IRQ2_INTE_AR;
695 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
696 if (dev->remote->mask_keydown & 0x10000)
697 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
698 else if (dev->remote->mask_keydown & 0x40000)
699 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
700 else if (dev->remote->mask_keyup & 0x40000)
701 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
704 if (dev->has_remote == SAA7134_REMOTE_I2C) {
705 request_module("ir-kbd-i2c");
708 saa_writel(SAA7134_IRQ1, 0);
709 saa_writel(SAA7134_IRQ2, irq2_mask);
714 static int saa7134_hwinit2(struct saa7134_dev *dev)
717 dprintk("hwinit2\n");
719 saa7134_video_init2(dev);
720 saa7134_tvaudio_init2(dev);
722 saa7134_hw_enable2(dev);
729 static int saa7134_hwfini(struct saa7134_dev *dev)
733 if (card_has_mpeg(dev))
734 saa7134_ts_fini(dev);
735 saa7134_input_fini(dev);
736 saa7134_vbi_fini(dev);
737 saa7134_tvaudio_fini(dev);
741 static void __devinit must_configure_manually(void)
747 "saa7134: Congratulations! Your TV card vendor saved a few\n"
748 "saa7134: cents for a eeprom, thus your pci board has no\n"
749 "saa7134: subsystem ID and I can't identify it automatically\n"
751 "saa7134: I feel better now. Ok, here are the good news:\n"
752 "saa7134: You can use the card=<nr> insmod option to specify\n"
753 "saa7134: which board do you have. The list:\n");
754 for (i = 0; i < saa7134_bcount; i++) {
755 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
756 i,saa7134_boards[i].name);
757 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
758 if (saa7134_pci_tbl[p].driver_data != i)
761 saa7134_pci_tbl[p].subvendor,
762 saa7134_pci_tbl[p].subdevice);
768 static struct video_device *vdev_init(struct saa7134_dev *dev,
769 struct video_device *template,
772 struct video_device *vfd;
774 vfd = video_device_alloc();
779 vfd->v4l2_dev = &dev->v4l2_dev;
780 vfd->release = video_device_release;
781 vfd->debug = video_debug;
782 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
783 dev->name, type, saa7134_boards[dev->board].name);
787 static void saa7134_unregister_video(struct saa7134_dev *dev)
789 if (dev->video_dev) {
790 if (-1 != dev->video_dev->minor)
791 video_unregister_device(dev->video_dev);
793 video_device_release(dev->video_dev);
794 dev->video_dev = NULL;
797 if (-1 != dev->vbi_dev->minor)
798 video_unregister_device(dev->vbi_dev);
800 video_device_release(dev->vbi_dev);
803 if (dev->radio_dev) {
804 if (-1 != dev->radio_dev->minor)
805 video_unregister_device(dev->radio_dev);
807 video_device_release(dev->radio_dev);
808 dev->radio_dev = NULL;
812 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
813 struct saa7134_dev *dev)
817 if (NULL != dev->mops)
819 if (saa7134_boards[dev->board].mpeg != ops->type)
821 err = ops->init(dev);
827 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
828 struct saa7134_dev *dev)
830 if (NULL == dev->mops)
832 if (dev->mops != ops)
834 dev->mops->fini(dev);
838 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
839 const struct pci_device_id *pci_id)
841 struct saa7134_dev *dev;
842 struct saa7134_mpeg_ops *mops;
845 if (saa7134_devcount == SAA7134_MAXBOARDS)
848 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
852 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
858 if (pci_enable_device(pci_dev)) {
863 dev->nr = saa7134_devcount;
864 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
867 if (pci_pci_problems) {
868 if (pci_pci_problems & PCIPCI_TRITON)
869 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
870 if (pci_pci_problems & PCIPCI_NATOMA)
871 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
872 if (pci_pci_problems & PCIPCI_VIAETBF)
873 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
874 if (pci_pci_problems & PCIPCI_VSFX)
875 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
876 #ifdef PCIPCI_ALIMAGIK
877 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
878 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
883 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
884 printk(KERN_INFO "%s: quirk: this driver and your "
885 "chipset may not work together"
886 " in overlay mode.\n",dev->name);
887 if (!saa7134_no_overlay) {
888 printk(KERN_INFO "%s: quirk: overlay "
889 "mode will be disabled.\n",
891 saa7134_no_overlay = 1;
893 printk(KERN_INFO "%s: quirk: overlay "
894 "mode will be forced. Use this"
895 " option at your own risk.\n",
900 if (UNSET != latency) {
901 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
903 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
907 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
908 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
909 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
910 "latency: %d, mmio: 0x%llx\n", dev->name,
911 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
912 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
913 pci_set_master(pci_dev);
914 if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
915 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
921 dev->board = pci_id->driver_data;
922 if (card[dev->nr] >= 0 &&
923 card[dev->nr] < saa7134_bcount)
924 dev->board = card[dev->nr];
925 if (SAA7134_BOARD_NOAUTO == dev->board) {
926 must_configure_manually();
927 dev->board = SAA7134_BOARD_UNKNOWN;
929 dev->autodetected = card[dev->nr] != dev->board;
930 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
931 dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
932 dev->radio_type = saa7134_boards[dev->board].radio_type;
933 dev->radio_addr = saa7134_boards[dev->board].radio_addr;
934 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
935 if (UNSET != tuner[dev->nr])
936 dev->tuner_type = tuner[dev->nr];
937 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
938 dev->name,pci_dev->subsystem_vendor,
939 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
940 dev->board, dev->autodetected ?
941 "autodetected" : "insmod option");
944 if (!request_mem_region(pci_resource_start(pci_dev,0),
945 pci_resource_len(pci_dev,0),
948 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
949 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
952 dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
953 pci_resource_len(pci_dev, 0));
954 dev->bmmio = (__u8 __iomem *)dev->lmmio;
955 if (NULL == dev->lmmio) {
957 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
962 /* initialize hardware #1 */
963 saa7134_board_init1(dev);
964 saa7134_hwinit1(dev);
967 err = request_irq(pci_dev->irq, saa7134_irq,
968 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
970 printk(KERN_ERR "%s: can't get IRQ %d\n",
971 dev->name,pci_dev->irq);
975 /* wait a bit, register i2c bus */
977 saa7134_i2c_register(dev);
978 saa7134_board_init2(dev);
980 saa7134_hwinit2(dev);
982 /* load i2c helpers */
983 if (card_is_empress(dev)) {
984 struct v4l2_subdev *sd =
985 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
986 "saa6752hs", "saa6752hs",
987 saa7134_boards[dev->board].empress_addr);
990 sd->grp_id = GRP_EMPRESS;
993 if (saa7134_boards[dev->board].rds_addr) {
994 struct v4l2_subdev *sd;
996 sd = v4l2_i2c_new_probed_subdev_addr(&dev->v4l2_dev,
997 &dev->i2c_adap, "saa6588", "saa6588",
998 saa7134_boards[dev->board].rds_addr);
1000 printk(KERN_INFO "%s: found RDS decoder\n", dev->name);
1003 request_submodules(dev);
1005 v4l2_prio_init(&dev->prio);
1007 mutex_lock(&saa7134_devlist_lock);
1008 list_for_each_entry(mops, &mops_list, next)
1009 mpeg_ops_attach(mops, dev);
1010 list_add_tail(&dev->devlist, &saa7134_devlist);
1011 mutex_unlock(&saa7134_devlist_lock);
1013 /* check for signal */
1014 saa7134_irq_video_signalchange(dev);
1016 if (TUNER_ABSENT != dev->tuner_type)
1017 saa_call_all(dev, tuner, s_standby);
1019 /* register v4l devices */
1020 if (saa7134_no_overlay > 0)
1021 printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);
1023 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1024 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1027 printk(KERN_INFO "%s: can't register video device\n",
1031 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1032 dev->name, dev->video_dev->num);
1034 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1036 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1040 printk(KERN_INFO "%s: registered device vbi%d\n",
1041 dev->name, dev->vbi_dev->num);
1043 if (card_has_radio(dev)) {
1044 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1045 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1049 printk(KERN_INFO "%s: registered device radio%d\n",
1050 dev->name, dev->radio_dev->num);
1053 /* everything worked */
1056 if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1057 saa7134_dmasound_init(dev);
1062 saa7134_unregister_video(dev);
1063 saa7134_i2c_unregister(dev);
1064 free_irq(pci_dev->irq, dev);
1066 saa7134_hwfini(dev);
1067 iounmap(dev->lmmio);
1069 release_mem_region(pci_resource_start(pci_dev,0),
1070 pci_resource_len(pci_dev,0));
1072 v4l2_device_unregister(&dev->v4l2_dev);
1078 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1080 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1081 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1082 struct saa7134_mpeg_ops *mops;
1084 /* Release DMA sound modules if present */
1085 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1086 saa7134_dmasound_exit(dev);
1091 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1092 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1093 print_irqstatus(dev,42,report,status);
1096 /* disable peripheral devices */
1097 saa_writeb(SAA7134_SPECIAL_MODE,0);
1099 /* shutdown hardware */
1100 saa_writel(SAA7134_IRQ1,0);
1101 saa_writel(SAA7134_IRQ2,0);
1102 saa_writel(SAA7134_MAIN_CTRL,0);
1104 /* shutdown subsystems */
1105 saa7134_hwfini(dev);
1108 mutex_lock(&saa7134_devlist_lock);
1109 list_del(&dev->devlist);
1110 list_for_each_entry(mops, &mops_list, next)
1111 mpeg_ops_detach(mops, dev);
1112 mutex_unlock(&saa7134_devlist_lock);
1115 saa7134_i2c_unregister(dev);
1116 saa7134_unregister_video(dev);
1119 /* the DMA sound modules should be unloaded before reaching
1120 this, but just in case they are still present... */
1121 if (dev->dmasound.priv_data != NULL) {
1122 free_irq(pci_dev->irq, &dev->dmasound);
1123 dev->dmasound.priv_data = NULL;
1127 /* release resources */
1128 free_irq(pci_dev->irq, dev);
1129 iounmap(dev->lmmio);
1130 release_mem_region(pci_resource_start(pci_dev,0),
1131 pci_resource_len(pci_dev,0));
1134 v4l2_device_unregister(&dev->v4l2_dev);
1142 /* resends a current buffer in queue after resume */
1143 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1144 struct saa7134_dmaqueue *q)
1146 struct saa7134_buf *buf, *next;
1148 assert_spin_locked(&dev->slock);
1152 dprintk("buffer_requeue\n");
1157 dprintk("buffer_requeue : resending active buffers \n");
1159 if (!list_empty(&q->queue))
1160 next = list_entry(q->queue.next, struct saa7134_buf,
1162 buf->activate(dev, buf, next);
1167 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1169 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1170 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1172 /* disable overlay - apps should enable it explicitly on resume*/
1175 /* Disable interrupts, DMA, and rest of the chip*/
1176 saa_writel(SAA7134_IRQ1, 0);
1177 saa_writel(SAA7134_IRQ2, 0);
1178 saa_writel(SAA7134_MAIN_CTRL, 0);
1181 synchronize_irq(pci_dev->irq);
1183 /* ACK interrupts once more, just in case,
1184 since the IRQ handler won't ack them anymore*/
1186 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1188 /* Disable timeout timers - if we have active buffers, we will
1189 fill them on resume*/
1191 del_timer(&dev->video_q.timeout);
1192 del_timer(&dev->vbi_q.timeout);
1193 del_timer(&dev->ts_q.timeout);
1196 saa7134_ir_stop(dev);
1198 pci_save_state(pci_dev);
1199 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1204 static int saa7134_resume(struct pci_dev *pci_dev)
1206 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1207 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1208 unsigned long flags;
1210 pci_set_power_state(pci_dev, PCI_D0);
1211 pci_restore_state(pci_dev);
1213 /* Do things that are done in saa7134_initdev ,
1214 except of initializing memory structures.*/
1216 saa7134_board_init1(dev);
1218 /* saa7134_hwinit1 */
1219 if (saa7134_boards[dev->board].video_out)
1220 saa7134_videoport_init(dev);
1221 if (card_has_mpeg(dev))
1222 saa7134_ts_init_hw(dev);
1224 saa7134_ir_start(dev, dev->remote);
1225 saa7134_hw_enable1(dev);
1229 saa7134_board_init2(dev);
1232 saa7134_set_tvnorm_hw(dev);
1233 saa7134_tvaudio_setmute(dev);
1234 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1235 saa7134_tvaudio_init(dev);
1236 saa7134_tvaudio_do_scan(dev);
1237 saa7134_enable_i2s(dev);
1238 saa7134_hw_enable2(dev);
1240 saa7134_irq_video_signalchange(dev);
1242 /*resume unfinished buffer(s)*/
1243 spin_lock_irqsave(&dev->slock, flags);
1244 saa7134_buffer_requeue(dev, &dev->video_q);
1245 saa7134_buffer_requeue(dev, &dev->vbi_q);
1246 saa7134_buffer_requeue(dev, &dev->ts_q);
1248 /* FIXME: Disable DMA audio sound - temporary till proper support
1251 dev->dmasound.dma_running = 0;
1256 saa7134_set_dmabits(dev);
1257 spin_unlock_irqrestore(&dev->slock, flags);
1263 /* ----------------------------------------------------------- */
1265 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1267 struct saa7134_dev *dev;
1269 mutex_lock(&saa7134_devlist_lock);
1270 list_for_each_entry(dev, &saa7134_devlist, devlist)
1271 mpeg_ops_attach(ops, dev);
1272 list_add_tail(&ops->next,&mops_list);
1273 mutex_unlock(&saa7134_devlist_lock);
1277 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1279 struct saa7134_dev *dev;
1281 mutex_lock(&saa7134_devlist_lock);
1282 list_del(&ops->next);
1283 list_for_each_entry(dev, &saa7134_devlist, devlist)
1284 mpeg_ops_detach(ops, dev);
1285 mutex_unlock(&saa7134_devlist_lock);
1288 EXPORT_SYMBOL(saa7134_ts_register);
1289 EXPORT_SYMBOL(saa7134_ts_unregister);
1291 /* ----------------------------------------------------------- */
1293 static struct pci_driver saa7134_pci_driver = {
1295 .id_table = saa7134_pci_tbl,
1296 .probe = saa7134_initdev,
1297 .remove = __devexit_p(saa7134_finidev),
1299 .suspend = saa7134_suspend,
1300 .resume = saa7134_resume
1304 static int saa7134_init(void)
1306 INIT_LIST_HEAD(&saa7134_devlist);
1307 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1308 (SAA7134_VERSION_CODE >> 16) & 0xff,
1309 (SAA7134_VERSION_CODE >> 8) & 0xff,
1310 SAA7134_VERSION_CODE & 0xff);
1312 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1313 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1315 return pci_register_driver(&saa7134_pci_driver);
1318 static void saa7134_fini(void)
1320 pci_unregister_driver(&saa7134_pci_driver);
1323 module_init(saa7134_init);
1324 module_exit(saa7134_fini);
1326 /* ----------------------------------------------------------- */
1328 EXPORT_SYMBOL(saa7134_set_gpio);
1329 EXPORT_SYMBOL(saa7134_boards);
1331 /* ----------------- for the DMA sound modules --------------- */
1333 EXPORT_SYMBOL(saa7134_dmasound_init);
1334 EXPORT_SYMBOL(saa7134_dmasound_exit);
1335 EXPORT_SYMBOL(saa7134_pgtable_free);
1336 EXPORT_SYMBOL(saa7134_pgtable_build);
1337 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1338 EXPORT_SYMBOL(saa7134_set_dmabits);
1340 /* ----------------------------------------------------------- */