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;
58 module_param(alsa, int, 0644);
59 MODULE_PARM_DESC(alsa,"enable ALSA DMA sound [dmasound]");
61 static unsigned int oss;
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 /* ------------------------------------------------------------------ */
148 /* ----------------------------------------------------------- */
149 /* delayed request_module */
151 #if defined(CONFIG_MODULES) && defined(MODULE)
153 static void request_module_async(struct work_struct *work){
154 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
155 if (card_is_empress(dev))
156 request_module("saa7134-empress");
157 if (card_is_dvb(dev))
158 request_module("saa7134-dvb");
160 request_module("saa7134-alsa");
162 request_module("saa7134-oss");
165 static void request_submodules(struct saa7134_dev *dev)
167 INIT_WORK(&dev->request_module_wk, request_module_async);
168 schedule_work(&dev->request_module_wk);
172 #define request_submodules(dev)
173 #endif /* CONFIG_MODULES */
175 /* ------------------------------------------------------------------ */
177 /* nr of (saa7134-)pages for the given buffer size */
178 static int saa7134_buffer_pages(int size)
180 size = PAGE_ALIGN(size);
181 size += PAGE_SIZE; /* for non-page-aligned buffers */
186 /* calc max # of buffers from size (must not exceed the 4MB virtual
187 * address space per DMA channel) */
188 int saa7134_buffer_count(unsigned int size, unsigned int count)
190 unsigned int maxcount;
192 maxcount = 1024 / saa7134_buffer_pages(size);
193 if (count > maxcount)
198 int saa7134_buffer_startpage(struct saa7134_buf *buf)
200 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
203 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
206 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
208 base = saa7134_buffer_startpage(buf) * 4096;
209 base += dma->sglist[0].offset;
213 /* ------------------------------------------------------------------ */
215 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
218 dma_addr_t dma_addr = 0;
220 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
223 pt->size = SAA7134_PGTABLE_SIZE;
229 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
230 struct scatterlist *list, unsigned int length,
231 unsigned int startpage)
236 BUG_ON(NULL == pt || NULL == pt->cpu);
238 ptr = pt->cpu + startpage;
239 for (i = 0; i < length; i++, list++)
240 for (p = 0; p * 4096 < list->length; p++, ptr++)
241 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
245 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
249 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
253 /* ------------------------------------------------------------------ */
255 void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
257 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
258 BUG_ON(in_interrupt());
260 videobuf_waiton(&buf->vb,0,0);
261 videobuf_dma_unmap(q, dma);
262 videobuf_dma_free(dma);
263 buf->vb.state = VIDEOBUF_NEEDS_INIT;
266 /* ------------------------------------------------------------------ */
268 int saa7134_buffer_queue(struct saa7134_dev *dev,
269 struct saa7134_dmaqueue *q,
270 struct saa7134_buf *buf)
272 struct saa7134_buf *next = NULL;
274 assert_spin_locked(&dev->slock);
275 dprintk("buffer_queue %p\n",buf);
276 if (NULL == q->curr) {
279 buf->activate(dev,buf,NULL);
280 } else if (list_empty(&q->queue)) {
281 list_add_tail(&buf->vb.queue,&q->queue);
282 buf->vb.state = VIDEOBUF_QUEUED;
284 next = list_entry(q->queue.next,struct saa7134_buf,
287 buf->activate(dev,buf,next);
290 list_add_tail(&buf->vb.queue,&q->queue);
291 buf->vb.state = VIDEOBUF_QUEUED;
296 void saa7134_buffer_finish(struct saa7134_dev *dev,
297 struct saa7134_dmaqueue *q,
300 assert_spin_locked(&dev->slock);
301 dprintk("buffer_finish %p\n",q->curr);
303 /* finish current buffer */
304 q->curr->vb.state = state;
305 do_gettimeofday(&q->curr->vb.ts);
306 wake_up(&q->curr->vb.done);
310 void saa7134_buffer_next(struct saa7134_dev *dev,
311 struct saa7134_dmaqueue *q)
313 struct saa7134_buf *buf,*next = NULL;
315 assert_spin_locked(&dev->slock);
316 BUG_ON(NULL != q->curr);
318 if (!list_empty(&q->queue)) {
319 /* activate next one from queue */
320 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
321 dprintk("buffer_next %p [prev=%p/next=%p]\n",
322 buf,q->queue.prev,q->queue.next);
323 list_del(&buf->vb.queue);
324 if (!list_empty(&q->queue))
325 next = list_entry(q->queue.next,struct saa7134_buf,
328 buf->activate(dev,buf,next);
329 dprintk("buffer_next #2 prev=%p/next=%p\n",
330 q->queue.prev,q->queue.next);
332 /* nothing to do -- just stop DMA */
333 dprintk("buffer_next %p\n",NULL);
334 saa7134_set_dmabits(dev);
335 del_timer(&q->timeout);
339 void saa7134_buffer_timeout(unsigned long data)
341 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
342 struct saa7134_dev *dev = q->dev;
345 spin_lock_irqsave(&dev->slock,flags);
347 /* try to reset the hardware (SWRST) */
348 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
349 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
350 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
352 /* flag current buffer as failed,
353 try to start over with the next one. */
355 dprintk("timeout on %p\n",q->curr);
356 saa7134_buffer_finish(dev,q,VIDEOBUF_ERROR);
358 saa7134_buffer_next(dev,q);
359 spin_unlock_irqrestore(&dev->slock,flags);
362 /* ------------------------------------------------------------------ */
364 int saa7134_set_dmabits(struct saa7134_dev *dev)
366 u32 split, task=0, ctrl=0, irq=0;
367 enum v4l2_field cap = V4L2_FIELD_ANY;
368 enum v4l2_field ov = V4L2_FIELD_ANY;
370 assert_spin_locked(&dev->slock);
375 /* video capture -- dma 0 + video task A */
376 if (dev->video_q.curr) {
378 ctrl |= SAA7134_MAIN_CTRL_TE0;
379 irq |= SAA7134_IRQ1_INTE_RA0_1 |
380 SAA7134_IRQ1_INTE_RA0_0;
381 cap = dev->video_q.curr->vb.field;
384 /* video capture -- dma 1+2 (planar modes) */
385 if (dev->video_q.curr &&
386 dev->video_q.curr->fmt->planar) {
387 ctrl |= SAA7134_MAIN_CTRL_TE4 |
388 SAA7134_MAIN_CTRL_TE5;
391 /* screen overlay -- dma 0 + video task B */
394 ctrl |= SAA7134_MAIN_CTRL_TE1;
398 /* vbi capture -- dma 0 + vbi task A+B */
399 if (dev->vbi_q.curr) {
401 ctrl |= SAA7134_MAIN_CTRL_TE2 |
402 SAA7134_MAIN_CTRL_TE3;
403 irq |= SAA7134_IRQ1_INTE_RA0_7 |
404 SAA7134_IRQ1_INTE_RA0_6 |
405 SAA7134_IRQ1_INTE_RA0_5 |
406 SAA7134_IRQ1_INTE_RA0_4;
409 /* audio capture -- dma 3 */
410 if (dev->dmasound.dma_running) {
411 ctrl |= SAA7134_MAIN_CTRL_TE6;
412 irq |= SAA7134_IRQ1_INTE_RA3_1 |
413 SAA7134_IRQ1_INTE_RA3_0;
416 /* TS capture -- dma 5 */
417 if (dev->ts_q.curr) {
418 ctrl |= SAA7134_MAIN_CTRL_TE5;
419 irq |= SAA7134_IRQ1_INTE_RA2_1 |
420 SAA7134_IRQ1_INTE_RA2_0;
423 /* set task conditions + field handling */
424 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
425 /* default config -- use full frames */
426 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
427 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
428 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
429 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
432 /* split fields between tasks */
433 if (V4L2_FIELD_TOP == cap) {
434 /* odd A, even B, repeat */
435 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
436 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
438 /* odd B, even A, repeat */
439 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
440 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
442 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
443 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
448 saa_writeb(SAA7134_REGION_ENABLE, task);
449 saa_writel(SAA7134_IRQ1, irq);
450 saa_andorl(SAA7134_MAIN_CTRL,
451 SAA7134_MAIN_CTRL_TE0 |
452 SAA7134_MAIN_CTRL_TE1 |
453 SAA7134_MAIN_CTRL_TE2 |
454 SAA7134_MAIN_CTRL_TE3 |
455 SAA7134_MAIN_CTRL_TE4 |
456 SAA7134_MAIN_CTRL_TE5 |
457 SAA7134_MAIN_CTRL_TE6,
459 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
460 task, ctrl, irq, split ? "no" : "yes");
465 /* ------------------------------------------------------------------ */
466 /* IRQ handler + helpers */
468 static char *irqbits[] = {
469 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
470 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
471 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
472 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
474 #define IRQBITS ARRAY_SIZE(irqbits)
476 static void print_irqstatus(struct saa7134_dev *dev, int loop,
477 unsigned long report, unsigned long status)
481 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
482 dev->name,loop,jiffies,report,status);
483 for (i = 0; i < IRQBITS; i++) {
484 if (!(report & (1 << i)))
486 printk(" %s",irqbits[i]);
488 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
489 printk(" | RA0=%s,%s,%s,%ld",
490 (status & 0x40) ? "vbi" : "video",
491 (status & 0x20) ? "b" : "a",
492 (status & 0x10) ? "odd" : "even",
498 static irqreturn_t saa7134_irq(int irq, void *dev_id)
500 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
501 unsigned long report,status;
502 int loop, handled = 0;
507 for (loop = 0; loop < 10; loop++) {
508 report = saa_readl(SAA7134_IRQ_REPORT);
509 status = saa_readl(SAA7134_IRQ_STATUS);
511 /* If dmasound support is active and we get a sound report,
512 * mask out the report and let the saa7134-alsa module deal
514 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
515 (dev->dmasound.priv_data != NULL) )
518 printk(KERN_DEBUG "%s/irq: preserving DMA sound interrupt\n",
520 report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
525 printk(KERN_DEBUG "%s/irq: no (more) work\n",
531 saa_writel(SAA7134_IRQ_REPORT,report);
533 print_irqstatus(dev,loop,report,status);
536 if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
537 (report & SAA7134_IRQ_REPORT_INTL))
538 saa7134_irq_video_signalchange(dev);
541 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
542 (status & 0x60) == 0)
543 saa7134_irq_video_done(dev,status);
545 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
546 (status & 0x40) == 0x40)
547 saa7134_irq_vbi_done(dev,status);
549 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
551 saa7134_irq_ts_done(dev,status);
553 if (report & SAA7134_IRQ_REPORT_GPIO16) {
554 switch (dev->has_remote) {
555 case SAA7134_REMOTE_GPIO:
558 if (dev->remote->mask_keydown & 0x10000) {
559 saa7134_input_irq(dev);
563 case SAA7134_REMOTE_I2C:
564 break; /* FIXME: invoke I2C get_key() */
566 default: /* GPIO16 not used by IR remote */
571 if (report & SAA7134_IRQ_REPORT_GPIO18) {
572 switch (dev->has_remote) {
573 case SAA7134_REMOTE_GPIO:
576 if ((dev->remote->mask_keydown & 0x40000) ||
577 (dev->remote->mask_keyup & 0x40000)) {
578 saa7134_input_irq(dev);
582 case SAA7134_REMOTE_I2C:
583 break; /* FIXME: invoke I2C get_key() */
585 default: /* GPIO18 not used by IR remote */
592 print_irqstatus(dev,loop,report,status);
593 if (report & SAA7134_IRQ_REPORT_PE) {
594 /* disable all parity error */
595 printk(KERN_WARNING "%s/irq: looping -- "
596 "clearing PE (parity error!) enable bit\n",dev->name);
597 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
598 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
599 /* disable gpio16 IRQ */
600 printk(KERN_WARNING "%s/irq: looping -- "
601 "clearing GPIO16 enable bit\n",dev->name);
602 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
603 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
604 /* disable gpio18 IRQs */
605 printk(KERN_WARNING "%s/irq: looping -- "
606 "clearing GPIO18 enable bit\n",dev->name);
607 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
609 /* disable all irqs */
610 printk(KERN_WARNING "%s/irq: looping -- "
611 "clearing all enable bits\n",dev->name);
612 saa_writel(SAA7134_IRQ1,0);
613 saa_writel(SAA7134_IRQ2,0);
618 return IRQ_RETVAL(handled);
621 /* ------------------------------------------------------------------ */
623 /* early init (no i2c, no irq) */
625 static int saa7134_hw_enable1(struct saa7134_dev *dev)
627 /* RAM FIFO config */
628 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
629 saa_writel(SAA7134_THRESHOULD, 0x02020202);
631 /* enable audio + video processing */
632 saa_writel(SAA7134_MAIN_CTRL,
633 SAA7134_MAIN_CTRL_VPLLE |
634 SAA7134_MAIN_CTRL_APLLE |
635 SAA7134_MAIN_CTRL_EXOSC |
636 SAA7134_MAIN_CTRL_EVFE1 |
637 SAA7134_MAIN_CTRL_EVFE2 |
638 SAA7134_MAIN_CTRL_ESFE |
639 SAA7134_MAIN_CTRL_EBDAC);
642 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
643 * OSS initialization writes to registers via the audio DSP; these
644 * writes will fail unless the audio clock has been started. At worst,
645 * audio will not work.
648 /* enable peripheral devices */
649 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
651 /* set vertical line numbering start (vbi needs this) */
652 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
657 static int saa7134_hwinit1(struct saa7134_dev *dev)
659 dprintk("hwinit1\n");
661 saa_writel(SAA7134_IRQ1, 0);
662 saa_writel(SAA7134_IRQ2, 0);
664 /* Clear any stale IRQ reports */
665 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
667 mutex_init(&dev->lock);
668 spin_lock_init(&dev->slock);
670 saa7134_track_gpio(dev,"pre-init");
671 saa7134_video_init1(dev);
672 saa7134_vbi_init1(dev);
673 if (card_has_mpeg(dev))
674 saa7134_ts_init1(dev);
675 saa7134_input_init1(dev);
677 saa7134_hw_enable1(dev);
682 /* late init (with i2c + irq) */
683 static int saa7134_hw_enable2(struct saa7134_dev *dev)
686 unsigned int irq2_mask;
690 SAA7134_IRQ2_INTE_DEC3 |
691 SAA7134_IRQ2_INTE_DEC2 |
692 SAA7134_IRQ2_INTE_DEC1 |
693 SAA7134_IRQ2_INTE_DEC0 |
694 SAA7134_IRQ2_INTE_PE |
695 SAA7134_IRQ2_INTE_AR;
697 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
698 if (dev->remote->mask_keydown & 0x10000)
699 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
700 else if (dev->remote->mask_keydown & 0x40000)
701 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
702 else if (dev->remote->mask_keyup & 0x40000)
703 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
706 if (dev->has_remote == SAA7134_REMOTE_I2C) {
707 request_module("ir-kbd-i2c");
710 saa_writel(SAA7134_IRQ1, 0);
711 saa_writel(SAA7134_IRQ2, irq2_mask);
716 static int saa7134_hwinit2(struct saa7134_dev *dev)
719 dprintk("hwinit2\n");
721 saa7134_video_init2(dev);
722 saa7134_tvaudio_init2(dev);
724 saa7134_hw_enable2(dev);
731 static int saa7134_hwfini(struct saa7134_dev *dev)
735 if (card_has_mpeg(dev))
736 saa7134_ts_fini(dev);
737 saa7134_input_fini(dev);
738 saa7134_vbi_fini(dev);
739 saa7134_tvaudio_fini(dev);
743 static void __devinit must_configure_manually(void)
749 "saa7134: Congratulations! Your TV card vendor saved a few\n"
750 "saa7134: cents for a eeprom, thus your pci board has no\n"
751 "saa7134: subsystem ID and I can't identify it automatically\n"
753 "saa7134: I feel better now. Ok, here are the good news:\n"
754 "saa7134: You can use the card=<nr> insmod option to specify\n"
755 "saa7134: which board do you have. The list:\n");
756 for (i = 0; i < saa7134_bcount; i++) {
757 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
758 i,saa7134_boards[i].name);
759 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
760 if (saa7134_pci_tbl[p].driver_data != i)
763 saa7134_pci_tbl[p].subvendor,
764 saa7134_pci_tbl[p].subdevice);
770 static struct video_device *vdev_init(struct saa7134_dev *dev,
771 struct video_device *template,
774 struct video_device *vfd;
776 vfd = video_device_alloc();
781 vfd->parent = &dev->pci->dev;
782 vfd->release = video_device_release;
783 vfd->debug = video_debug;
784 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
785 dev->name, type, saa7134_boards[dev->board].name);
789 static void saa7134_unregister_video(struct saa7134_dev *dev)
791 if (dev->video_dev) {
792 if (-1 != dev->video_dev->minor)
793 video_unregister_device(dev->video_dev);
795 video_device_release(dev->video_dev);
796 dev->video_dev = NULL;
799 if (-1 != dev->vbi_dev->minor)
800 video_unregister_device(dev->vbi_dev);
802 video_device_release(dev->vbi_dev);
805 if (dev->radio_dev) {
806 if (-1 != dev->radio_dev->minor)
807 video_unregister_device(dev->radio_dev);
809 video_device_release(dev->radio_dev);
810 dev->radio_dev = NULL;
814 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
815 struct saa7134_dev *dev)
819 if (NULL != dev->mops)
821 if (saa7134_boards[dev->board].mpeg != ops->type)
823 err = ops->init(dev);
829 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
830 struct saa7134_dev *dev)
832 if (NULL == dev->mops)
834 if (dev->mops != ops)
836 dev->mops->fini(dev);
840 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
841 const struct pci_device_id *pci_id)
843 struct saa7134_dev *dev;
844 struct saa7134_mpeg_ops *mops;
847 if (saa7134_devcount == SAA7134_MAXBOARDS)
850 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
856 if (pci_enable_device(pci_dev)) {
861 dev->nr = saa7134_devcount;
862 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
865 if (pci_pci_problems) {
866 if (pci_pci_problems & PCIPCI_TRITON)
867 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
868 if (pci_pci_problems & PCIPCI_NATOMA)
869 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
870 if (pci_pci_problems & PCIPCI_VIAETBF)
871 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
872 if (pci_pci_problems & PCIPCI_VSFX)
873 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
874 #ifdef PCIPCI_ALIMAGIK
875 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
876 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
881 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
882 printk(KERN_INFO "%s: quirk: this driver and your "
883 "chipset may not work together"
884 " in overlay mode.\n",dev->name);
885 if (!saa7134_no_overlay) {
886 printk(KERN_INFO "%s: quirk: overlay "
887 "mode will be disabled.\n",
889 saa7134_no_overlay = 1;
891 printk(KERN_INFO "%s: quirk: overlay "
892 "mode will be forced. Use this"
893 " option at your own risk.\n",
898 if (UNSET != latency) {
899 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
901 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
905 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
906 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
907 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
908 "latency: %d, mmio: 0x%llx\n", dev->name,
909 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
910 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
911 pci_set_master(pci_dev);
912 if (!pci_dma_supported(pci_dev, DMA_32BIT_MASK)) {
913 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
919 dev->board = pci_id->driver_data;
920 if (card[dev->nr] >= 0 &&
921 card[dev->nr] < saa7134_bcount)
922 dev->board = card[dev->nr];
923 if (SAA7134_BOARD_NOAUTO == dev->board) {
924 must_configure_manually();
925 dev->board = SAA7134_BOARD_UNKNOWN;
927 dev->autodetected = card[dev->nr] != dev->board;
928 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
929 dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
930 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
931 if (UNSET != tuner[dev->nr])
932 dev->tuner_type = tuner[dev->nr];
933 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
934 dev->name,pci_dev->subsystem_vendor,
935 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
936 dev->board, dev->autodetected ?
937 "autodetected" : "insmod option");
940 if (!request_mem_region(pci_resource_start(pci_dev,0),
941 pci_resource_len(pci_dev,0),
944 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
945 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
948 dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
949 pci_resource_len(pci_dev, 0));
950 dev->bmmio = (__u8 __iomem *)dev->lmmio;
951 if (NULL == dev->lmmio) {
953 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
958 /* initialize hardware #1 */
959 saa7134_board_init1(dev);
960 saa7134_hwinit1(dev);
963 err = request_irq(pci_dev->irq, saa7134_irq,
964 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
966 printk(KERN_ERR "%s: can't get IRQ %d\n",
967 dev->name,pci_dev->irq);
971 /* wait a bit, register i2c bus */
973 saa7134_i2c_register(dev);
975 /* initialize hardware #2 */
976 if (TUNER_ABSENT != dev->tuner_type)
977 request_module("tuner");
978 saa7134_board_init2(dev);
980 saa7134_hwinit2(dev);
982 /* load i2c helpers */
983 if (card_is_empress(dev)) {
984 request_module("saa6752hs");
987 request_submodules(dev);
989 v4l2_prio_init(&dev->prio);
991 /* register v4l devices */
992 if (saa7134_no_overlay > 0)
993 printk(KERN_INFO "%s: Overlay support disabled.\n", dev->name);
995 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
996 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
999 printk(KERN_INFO "%s: can't register video device\n",
1003 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
1004 dev->name, dev->video_dev->num);
1006 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1008 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1012 printk(KERN_INFO "%s: registered device vbi%d\n",
1013 dev->name, dev->vbi_dev->num);
1015 if (card_has_radio(dev)) {
1016 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1017 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1021 printk(KERN_INFO "%s: registered device radio%d\n",
1022 dev->name, dev->radio_dev->num);
1025 /* everything worked */
1026 pci_set_drvdata(pci_dev,dev);
1029 mutex_lock(&devlist_lock);
1030 list_for_each_entry(mops, &mops_list, next)
1031 mpeg_ops_attach(mops, dev);
1032 list_add_tail(&dev->devlist,&saa7134_devlist);
1033 mutex_unlock(&devlist_lock);
1035 /* check for signal */
1036 saa7134_irq_video_signalchange(dev);
1038 if (saa7134_dmasound_init && !dev->dmasound.priv_data) {
1039 saa7134_dmasound_init(dev);
1042 if (TUNER_ABSENT != dev->tuner_type)
1043 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1048 saa7134_unregister_video(dev);
1049 saa7134_i2c_unregister(dev);
1050 free_irq(pci_dev->irq, dev);
1052 saa7134_hwfini(dev);
1053 iounmap(dev->lmmio);
1055 release_mem_region(pci_resource_start(pci_dev,0),
1056 pci_resource_len(pci_dev,0));
1062 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1064 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1065 struct saa7134_mpeg_ops *mops;
1067 /* Release DMA sound modules if present */
1068 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1069 saa7134_dmasound_exit(dev);
1074 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1075 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1076 print_irqstatus(dev,42,report,status);
1079 /* disable peripheral devices */
1080 saa_writeb(SAA7134_SPECIAL_MODE,0);
1082 /* shutdown hardware */
1083 saa_writel(SAA7134_IRQ1,0);
1084 saa_writel(SAA7134_IRQ2,0);
1085 saa_writel(SAA7134_MAIN_CTRL,0);
1087 /* shutdown subsystems */
1088 saa7134_hwfini(dev);
1091 mutex_lock(&devlist_lock);
1092 list_del(&dev->devlist);
1093 list_for_each_entry(mops, &mops_list, next)
1094 mpeg_ops_detach(mops, dev);
1095 mutex_unlock(&devlist_lock);
1098 saa7134_i2c_unregister(dev);
1099 saa7134_unregister_video(dev);
1102 /* the DMA sound modules should be unloaded before reaching
1103 this, but just in case they are still present... */
1104 if (dev->dmasound.priv_data != NULL) {
1105 free_irq(pci_dev->irq, &dev->dmasound);
1106 dev->dmasound.priv_data = NULL;
1110 /* release resources */
1111 free_irq(pci_dev->irq, dev);
1112 iounmap(dev->lmmio);
1113 release_mem_region(pci_resource_start(pci_dev,0),
1114 pci_resource_len(pci_dev,0));
1116 pci_set_drvdata(pci_dev, NULL);
1124 /* resends a current buffer in queue after resume */
1125 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1126 struct saa7134_dmaqueue *q)
1128 struct saa7134_buf *buf, *next;
1130 assert_spin_locked(&dev->slock);
1134 dprintk("buffer_requeue\n");
1139 dprintk("buffer_requeue : resending active buffers \n");
1141 if (!list_empty(&q->queue))
1142 next = list_entry(q->queue.next, struct saa7134_buf,
1144 buf->activate(dev, buf, next);
1149 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1152 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1154 /* disable overlay - apps should enable it explicitly on resume*/
1157 /* Disable interrupts, DMA, and rest of the chip*/
1158 saa_writel(SAA7134_IRQ1, 0);
1159 saa_writel(SAA7134_IRQ2, 0);
1160 saa_writel(SAA7134_MAIN_CTRL, 0);
1163 synchronize_irq(pci_dev->irq);
1165 /* ACK interrupts once more, just in case,
1166 since the IRQ handler won't ack them anymore*/
1168 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1170 /* Disable timeout timers - if we have active buffers, we will
1171 fill them on resume*/
1173 del_timer(&dev->video_q.timeout);
1174 del_timer(&dev->vbi_q.timeout);
1175 del_timer(&dev->ts_q.timeout);
1178 saa7134_ir_stop(dev);
1180 pci_save_state(pci_dev);
1181 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1186 static int saa7134_resume(struct pci_dev *pci_dev)
1188 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1189 unsigned long flags;
1191 pci_set_power_state(pci_dev, PCI_D0);
1192 pci_restore_state(pci_dev);
1194 /* Do things that are done in saa7134_initdev ,
1195 except of initializing memory structures.*/
1197 saa7134_board_init1(dev);
1199 /* saa7134_hwinit1 */
1200 if (saa7134_boards[dev->board].video_out)
1201 saa7134_videoport_init(dev);
1202 if (card_has_mpeg(dev))
1203 saa7134_ts_init_hw(dev);
1205 saa7134_ir_start(dev, dev->remote);
1206 saa7134_hw_enable1(dev);
1210 saa7134_board_init2(dev);
1213 saa7134_set_tvnorm_hw(dev);
1214 saa7134_tvaudio_setmute(dev);
1215 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1216 saa7134_tvaudio_init(dev);
1217 saa7134_tvaudio_do_scan(dev);
1218 saa7134_enable_i2s(dev);
1219 saa7134_hw_enable2(dev);
1221 saa7134_irq_video_signalchange(dev);
1223 /*resume unfinished buffer(s)*/
1224 spin_lock_irqsave(&dev->slock, flags);
1225 saa7134_buffer_requeue(dev, &dev->video_q);
1226 saa7134_buffer_requeue(dev, &dev->vbi_q);
1227 saa7134_buffer_requeue(dev, &dev->ts_q);
1229 /* FIXME: Disable DMA audio sound - temporary till proper support
1232 dev->dmasound.dma_running = 0;
1237 saa7134_set_dmabits(dev);
1238 spin_unlock_irqrestore(&dev->slock, flags);
1244 /* ----------------------------------------------------------- */
1246 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1248 struct saa7134_dev *dev;
1250 mutex_lock(&devlist_lock);
1251 list_for_each_entry(dev, &saa7134_devlist, devlist)
1252 mpeg_ops_attach(ops, dev);
1253 list_add_tail(&ops->next,&mops_list);
1254 mutex_unlock(&devlist_lock);
1258 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1260 struct saa7134_dev *dev;
1262 mutex_lock(&devlist_lock);
1263 list_del(&ops->next);
1264 list_for_each_entry(dev, &saa7134_devlist, devlist)
1265 mpeg_ops_detach(ops, dev);
1266 mutex_unlock(&devlist_lock);
1269 EXPORT_SYMBOL(saa7134_ts_register);
1270 EXPORT_SYMBOL(saa7134_ts_unregister);
1272 /* ----------------------------------------------------------- */
1274 static struct pci_driver saa7134_pci_driver = {
1276 .id_table = saa7134_pci_tbl,
1277 .probe = saa7134_initdev,
1278 .remove = __devexit_p(saa7134_finidev),
1280 .suspend = saa7134_suspend,
1281 .resume = saa7134_resume
1285 static int saa7134_init(void)
1287 INIT_LIST_HEAD(&saa7134_devlist);
1288 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1289 (SAA7134_VERSION_CODE >> 16) & 0xff,
1290 (SAA7134_VERSION_CODE >> 8) & 0xff,
1291 SAA7134_VERSION_CODE & 0xff);
1293 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1294 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1296 return pci_register_driver(&saa7134_pci_driver);
1299 static void saa7134_fini(void)
1301 pci_unregister_driver(&saa7134_pci_driver);
1304 module_init(saa7134_init);
1305 module_exit(saa7134_fini);
1307 /* ----------------------------------------------------------- */
1309 EXPORT_SYMBOL(saa7134_set_gpio);
1310 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1311 EXPORT_SYMBOL(saa7134_devlist);
1312 EXPORT_SYMBOL(saa7134_boards);
1314 /* ----------------- for the DMA sound modules --------------- */
1316 EXPORT_SYMBOL(saa7134_dmasound_init);
1317 EXPORT_SYMBOL(saa7134_dmasound_exit);
1318 EXPORT_SYMBOL(saa7134_pgtable_free);
1319 EXPORT_SYMBOL(saa7134_pgtable_build);
1320 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1321 EXPORT_SYMBOL(saa7134_set_dmabits);
1323 /* ----------------------------------------------------------- */