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
);
169 static void flush_request_submodules(struct saa7134_dev
*dev
)
171 flush_work_sync(&dev
->request_module_wk
);
175 #define request_submodules(dev)
176 #define flush_request_submodules(dev)
177 #endif /* CONFIG_MODULES */
179 /* ------------------------------------------------------------------ */
181 /* nr of (saa7134-)pages for the given buffer size */
182 static int saa7134_buffer_pages(int size
)
184 size
= PAGE_ALIGN(size
);
185 size
+= PAGE_SIZE
; /* for non-page-aligned buffers */
190 /* calc max # of buffers from size (must not exceed the 4MB virtual
191 * address space per DMA channel) */
192 int saa7134_buffer_count(unsigned int size
, unsigned int count
)
194 unsigned int maxcount
;
196 maxcount
= 1024 / saa7134_buffer_pages(size
);
197 if (count
> maxcount
)
202 int saa7134_buffer_startpage(struct saa7134_buf
*buf
)
204 return saa7134_buffer_pages(buf
->vb
.bsize
) * buf
->vb
.i
;
207 unsigned long saa7134_buffer_base(struct saa7134_buf
*buf
)
210 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
212 base
= saa7134_buffer_startpage(buf
) * 4096;
213 base
+= dma
->sglist
[0].offset
;
217 /* ------------------------------------------------------------------ */
219 int saa7134_pgtable_alloc(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
222 dma_addr_t dma_addr
= 0;
224 cpu
= pci_alloc_consistent(pci
, SAA7134_PGTABLE_SIZE
, &dma_addr
);
227 pt
->size
= SAA7134_PGTABLE_SIZE
;
233 int saa7134_pgtable_build(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
,
234 struct scatterlist
*list
, unsigned int length
,
235 unsigned int startpage
)
240 BUG_ON(NULL
== pt
|| NULL
== pt
->cpu
);
242 ptr
= pt
->cpu
+ startpage
;
243 for (i
= 0; i
< length
; i
++, list
++)
244 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr
++)
245 *ptr
= cpu_to_le32(sg_dma_address(list
) - list
->offset
);
249 void saa7134_pgtable_free(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
253 pci_free_consistent(pci
, pt
->size
, pt
->cpu
, pt
->dma
);
257 /* ------------------------------------------------------------------ */
259 void saa7134_dma_free(struct videobuf_queue
*q
,struct saa7134_buf
*buf
)
261 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
262 BUG_ON(in_interrupt());
264 videobuf_waiton(q
, &buf
->vb
, 0, 0);
265 videobuf_dma_unmap(q
->dev
, dma
);
266 videobuf_dma_free(dma
);
267 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
270 /* ------------------------------------------------------------------ */
272 int saa7134_buffer_queue(struct saa7134_dev
*dev
,
273 struct saa7134_dmaqueue
*q
,
274 struct saa7134_buf
*buf
)
276 struct saa7134_buf
*next
= NULL
;
278 assert_spin_locked(&dev
->slock
);
279 dprintk("buffer_queue %p\n",buf
);
280 if (NULL
== q
->curr
) {
283 buf
->activate(dev
,buf
,NULL
);
284 } else if (list_empty(&q
->queue
)) {
285 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
286 buf
->vb
.state
= VIDEOBUF_QUEUED
;
288 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
291 buf
->activate(dev
,buf
,next
);
294 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
295 buf
->vb
.state
= VIDEOBUF_QUEUED
;
300 void saa7134_buffer_finish(struct saa7134_dev
*dev
,
301 struct saa7134_dmaqueue
*q
,
304 assert_spin_locked(&dev
->slock
);
305 dprintk("buffer_finish %p\n",q
->curr
);
307 /* finish current buffer */
308 q
->curr
->vb
.state
= state
;
309 do_gettimeofday(&q
->curr
->vb
.ts
);
310 wake_up(&q
->curr
->vb
.done
);
314 void saa7134_buffer_next(struct saa7134_dev
*dev
,
315 struct saa7134_dmaqueue
*q
)
317 struct saa7134_buf
*buf
,*next
= NULL
;
319 assert_spin_locked(&dev
->slock
);
320 BUG_ON(NULL
!= q
->curr
);
322 if (!list_empty(&q
->queue
)) {
323 /* activate next one from queue */
324 buf
= list_entry(q
->queue
.next
,struct saa7134_buf
,vb
.queue
);
325 dprintk("buffer_next %p [prev=%p/next=%p]\n",
326 buf
,q
->queue
.prev
,q
->queue
.next
);
327 list_del(&buf
->vb
.queue
);
328 if (!list_empty(&q
->queue
))
329 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
332 buf
->activate(dev
,buf
,next
);
333 dprintk("buffer_next #2 prev=%p/next=%p\n",
334 q
->queue
.prev
,q
->queue
.next
);
336 /* nothing to do -- just stop DMA */
337 dprintk("buffer_next %p\n",NULL
);
338 saa7134_set_dmabits(dev
);
339 del_timer(&q
->timeout
);
341 if (card_has_mpeg(dev
))
343 saa7134_ts_stop(dev
);
347 void saa7134_buffer_timeout(unsigned long data
)
349 struct saa7134_dmaqueue
*q
= (struct saa7134_dmaqueue
*)data
;
350 struct saa7134_dev
*dev
= q
->dev
;
353 spin_lock_irqsave(&dev
->slock
,flags
);
355 /* try to reset the hardware (SWRST) */
356 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
357 saa_writeb(SAA7134_REGION_ENABLE
, 0x80);
358 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
360 /* flag current buffer as failed,
361 try to start over with the next one. */
363 dprintk("timeout on %p\n",q
->curr
);
364 saa7134_buffer_finish(dev
,q
,VIDEOBUF_ERROR
);
366 saa7134_buffer_next(dev
,q
);
367 spin_unlock_irqrestore(&dev
->slock
,flags
);
370 /* ------------------------------------------------------------------ */
372 int saa7134_set_dmabits(struct saa7134_dev
*dev
)
374 u32 split
, task
=0, ctrl
=0, irq
=0;
375 enum v4l2_field cap
= V4L2_FIELD_ANY
;
376 enum v4l2_field ov
= V4L2_FIELD_ANY
;
378 assert_spin_locked(&dev
->slock
);
383 /* video capture -- dma 0 + video task A */
384 if (dev
->video_q
.curr
) {
386 ctrl
|= SAA7134_MAIN_CTRL_TE0
;
387 irq
|= SAA7134_IRQ1_INTE_RA0_1
|
388 SAA7134_IRQ1_INTE_RA0_0
;
389 cap
= dev
->video_q
.curr
->vb
.field
;
392 /* video capture -- dma 1+2 (planar modes) */
393 if (dev
->video_q
.curr
&&
394 dev
->video_q
.curr
->fmt
->planar
) {
395 ctrl
|= SAA7134_MAIN_CTRL_TE4
|
396 SAA7134_MAIN_CTRL_TE5
;
399 /* screen overlay -- dma 0 + video task B */
402 ctrl
|= SAA7134_MAIN_CTRL_TE1
;
406 /* vbi capture -- dma 0 + vbi task A+B */
407 if (dev
->vbi_q
.curr
) {
409 ctrl
|= SAA7134_MAIN_CTRL_TE2
|
410 SAA7134_MAIN_CTRL_TE3
;
411 irq
|= SAA7134_IRQ1_INTE_RA0_7
|
412 SAA7134_IRQ1_INTE_RA0_6
|
413 SAA7134_IRQ1_INTE_RA0_5
|
414 SAA7134_IRQ1_INTE_RA0_4
;
417 /* audio capture -- dma 3 */
418 if (dev
->dmasound
.dma_running
) {
419 ctrl
|= SAA7134_MAIN_CTRL_TE6
;
420 irq
|= SAA7134_IRQ1_INTE_RA3_1
|
421 SAA7134_IRQ1_INTE_RA3_0
;
424 /* TS capture -- dma 5 */
425 if (dev
->ts_q
.curr
) {
426 ctrl
|= SAA7134_MAIN_CTRL_TE5
;
427 irq
|= SAA7134_IRQ1_INTE_RA2_1
|
428 SAA7134_IRQ1_INTE_RA2_0
;
431 /* set task conditions + field handling */
432 if (V4L2_FIELD_HAS_BOTH(cap
) || V4L2_FIELD_HAS_BOTH(ov
) || cap
== ov
) {
433 /* default config -- use full frames */
434 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
435 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
436 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x02);
437 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x02);
440 /* split fields between tasks */
441 if (V4L2_FIELD_TOP
== cap
) {
442 /* odd A, even B, repeat */
443 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
444 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0e);
446 /* odd B, even A, repeat */
447 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0e);
448 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
450 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x01);
451 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x01);
456 saa_writeb(SAA7134_REGION_ENABLE
, task
);
457 saa_writel(SAA7134_IRQ1
, irq
);
458 saa_andorl(SAA7134_MAIN_CTRL
,
459 SAA7134_MAIN_CTRL_TE0
|
460 SAA7134_MAIN_CTRL_TE1
|
461 SAA7134_MAIN_CTRL_TE2
|
462 SAA7134_MAIN_CTRL_TE3
|
463 SAA7134_MAIN_CTRL_TE4
|
464 SAA7134_MAIN_CTRL_TE5
|
465 SAA7134_MAIN_CTRL_TE6
,
467 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
468 task
, ctrl
, irq
, split
? "no" : "yes");
473 /* ------------------------------------------------------------------ */
474 /* IRQ handler + helpers */
476 static char *irqbits
[] = {
477 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
478 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
479 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
480 "GPIO16", "GPIO18", "GPIO22", "GPIO23"
482 #define IRQBITS ARRAY_SIZE(irqbits)
484 static void print_irqstatus(struct saa7134_dev
*dev
, int loop
,
485 unsigned long report
, unsigned long status
)
489 printk(KERN_DEBUG
"%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
490 dev
->name
,loop
,jiffies
,report
,status
);
491 for (i
= 0; i
< IRQBITS
; i
++) {
492 if (!(report
& (1 << i
)))
494 printk(" %s",irqbits
[i
]);
496 if (report
& SAA7134_IRQ_REPORT_DONE_RA0
) {
497 printk(" | RA0=%s,%s,%s,%ld",
498 (status
& 0x40) ? "vbi" : "video",
499 (status
& 0x20) ? "b" : "a",
500 (status
& 0x10) ? "odd" : "even",
506 static irqreturn_t
saa7134_irq(int irq
, void *dev_id
)
508 struct saa7134_dev
*dev
= (struct saa7134_dev
*) dev_id
;
509 unsigned long report
,status
;
510 int loop
, handled
= 0;
515 for (loop
= 0; loop
< 10; loop
++) {
516 report
= saa_readl(SAA7134_IRQ_REPORT
);
517 status
= saa_readl(SAA7134_IRQ_STATUS
);
519 /* If dmasound support is active and we get a sound report,
520 * mask out the report and let the saa7134-alsa module deal
522 if ((report
& SAA7134_IRQ_REPORT_DONE_RA3
) &&
523 (dev
->dmasound
.priv_data
!= NULL
) )
526 printk(KERN_DEBUG
"%s/irq: preserving DMA sound interrupt\n",
528 report
&= ~SAA7134_IRQ_REPORT_DONE_RA3
;
533 printk(KERN_DEBUG
"%s/irq: no (more) work\n",
539 saa_writel(SAA7134_IRQ_REPORT
,report
);
541 print_irqstatus(dev
,loop
,report
,status
);
544 if ((report
& SAA7134_IRQ_REPORT_RDCAP
) ||
545 (report
& SAA7134_IRQ_REPORT_INTL
))
546 saa7134_irq_video_signalchange(dev
);
549 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
550 (status
& 0x60) == 0)
551 saa7134_irq_video_done(dev
,status
);
553 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
554 (status
& 0x40) == 0x40)
555 saa7134_irq_vbi_done(dev
,status
);
557 if ((report
& SAA7134_IRQ_REPORT_DONE_RA2
) &&
559 saa7134_irq_ts_done(dev
,status
);
561 if (report
& SAA7134_IRQ_REPORT_GPIO16
) {
562 switch (dev
->has_remote
) {
563 case SAA7134_REMOTE_GPIO
:
566 if (dev
->remote
->mask_keydown
& 0x10000) {
567 saa7134_input_irq(dev
);
571 case SAA7134_REMOTE_I2C
:
572 break; /* FIXME: invoke I2C get_key() */
574 default: /* GPIO16 not used by IR remote */
579 if (report
& SAA7134_IRQ_REPORT_GPIO18
) {
580 switch (dev
->has_remote
) {
581 case SAA7134_REMOTE_GPIO
:
584 if ((dev
->remote
->mask_keydown
& 0x40000) ||
585 (dev
->remote
->mask_keyup
& 0x40000)) {
586 saa7134_input_irq(dev
);
590 case SAA7134_REMOTE_I2C
:
591 break; /* FIXME: invoke I2C get_key() */
593 default: /* GPIO18 not used by IR remote */
600 print_irqstatus(dev
,loop
,report
,status
);
601 if (report
& SAA7134_IRQ_REPORT_PE
) {
602 /* disable all parity error */
603 printk(KERN_WARNING
"%s/irq: looping -- "
604 "clearing PE (parity error!) enable bit\n",dev
->name
);
605 saa_clearl(SAA7134_IRQ2
,SAA7134_IRQ2_INTE_PE
);
606 } else if (report
& SAA7134_IRQ_REPORT_GPIO16
) {
607 /* disable gpio16 IRQ */
608 printk(KERN_WARNING
"%s/irq: looping -- "
609 "clearing GPIO16 enable bit\n",dev
->name
);
610 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO16_P
);
611 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO16_N
);
612 } else if (report
& SAA7134_IRQ_REPORT_GPIO18
) {
613 /* disable gpio18 IRQs */
614 printk(KERN_WARNING
"%s/irq: looping -- "
615 "clearing GPIO18 enable bit\n",dev
->name
);
616 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO18_P
);
617 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO18_N
);
619 /* disable all irqs */
620 printk(KERN_WARNING
"%s/irq: looping -- "
621 "clearing all enable bits\n",dev
->name
);
622 saa_writel(SAA7134_IRQ1
,0);
623 saa_writel(SAA7134_IRQ2
,0);
628 return IRQ_RETVAL(handled
);
631 /* ------------------------------------------------------------------ */
633 /* early init (no i2c, no irq) */
635 static int saa7134_hw_enable1(struct saa7134_dev
*dev
)
637 /* RAM FIFO config */
638 saa_writel(SAA7134_FIFO_SIZE
, 0x08070503);
639 saa_writel(SAA7134_THRESHOULD
, 0x02020202);
641 /* enable audio + video processing */
642 saa_writel(SAA7134_MAIN_CTRL
,
643 SAA7134_MAIN_CTRL_VPLLE
|
644 SAA7134_MAIN_CTRL_APLLE
|
645 SAA7134_MAIN_CTRL_EXOSC
|
646 SAA7134_MAIN_CTRL_EVFE1
|
647 SAA7134_MAIN_CTRL_EVFE2
|
648 SAA7134_MAIN_CTRL_ESFE
|
649 SAA7134_MAIN_CTRL_EBDAC
);
652 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
653 * OSS initialization writes to registers via the audio DSP; these
654 * writes will fail unless the audio clock has been started. At worst,
655 * audio will not work.
658 /* enable peripheral devices */
659 saa_writeb(SAA7134_SPECIAL_MODE
, 0x01);
661 /* set vertical line numbering start (vbi needs this) */
662 saa_writeb(SAA7134_SOURCE_TIMING2
, 0x20);
667 static int saa7134_hwinit1(struct saa7134_dev
*dev
)
669 dprintk("hwinit1\n");
671 saa_writel(SAA7134_IRQ1
, 0);
672 saa_writel(SAA7134_IRQ2
, 0);
674 /* Clear any stale IRQ reports */
675 saa_writel(SAA7134_IRQ_REPORT
, saa_readl(SAA7134_IRQ_REPORT
));
677 mutex_init(&dev
->lock
);
678 spin_lock_init(&dev
->slock
);
680 saa7134_track_gpio(dev
,"pre-init");
681 saa7134_video_init1(dev
);
682 saa7134_vbi_init1(dev
);
683 if (card_has_mpeg(dev
))
684 saa7134_ts_init1(dev
);
685 saa7134_input_init1(dev
);
687 saa7134_hw_enable1(dev
);
692 /* late init (with i2c + irq) */
693 static int saa7134_hw_enable2(struct saa7134_dev
*dev
)
696 unsigned int irq2_mask
;
700 SAA7134_IRQ2_INTE_DEC3
|
701 SAA7134_IRQ2_INTE_DEC2
|
702 SAA7134_IRQ2_INTE_DEC1
|
703 SAA7134_IRQ2_INTE_DEC0
|
704 SAA7134_IRQ2_INTE_PE
|
705 SAA7134_IRQ2_INTE_AR
;
707 if (dev
->has_remote
== SAA7134_REMOTE_GPIO
&& dev
->remote
) {
708 if (dev
->remote
->mask_keydown
& 0x10000)
709 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO16_N
;
710 else { /* Allow enabling both IRQ edge triggers */
711 if (dev
->remote
->mask_keydown
& 0x40000)
712 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO18_P
;
713 if (dev
->remote
->mask_keyup
& 0x40000)
714 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO18_N
;
718 if (dev
->has_remote
== SAA7134_REMOTE_I2C
) {
719 request_module("ir-kbd-i2c");
722 saa_writel(SAA7134_IRQ1
, 0);
723 saa_writel(SAA7134_IRQ2
, irq2_mask
);
728 static int saa7134_hwinit2(struct saa7134_dev
*dev
)
731 dprintk("hwinit2\n");
733 saa7134_video_init2(dev
);
734 saa7134_tvaudio_init2(dev
);
736 saa7134_hw_enable2(dev
);
743 static int saa7134_hwfini(struct saa7134_dev
*dev
)
747 if (card_has_mpeg(dev
))
748 saa7134_ts_fini(dev
);
749 saa7134_input_fini(dev
);
750 saa7134_vbi_fini(dev
);
751 saa7134_tvaudio_fini(dev
);
755 static void __devinit
must_configure_manually(void)
761 "saa7134: Congratulations! Your TV card vendor saved a few\n"
762 "saa7134: cents for a eeprom, thus your pci board has no\n"
763 "saa7134: subsystem ID and I can't identify it automatically\n"
765 "saa7134: I feel better now. Ok, here are the good news:\n"
766 "saa7134: You can use the card=<nr> insmod option to specify\n"
767 "saa7134: which board do you have. The list:\n");
768 for (i
= 0; i
< saa7134_bcount
; i
++) {
769 printk(KERN_WARNING
"saa7134: card=%d -> %-40.40s",
770 i
,saa7134_boards
[i
].name
);
771 for (p
= 0; saa7134_pci_tbl
[p
].driver_data
; p
++) {
772 if (saa7134_pci_tbl
[p
].driver_data
!= i
)
775 saa7134_pci_tbl
[p
].subvendor
,
776 saa7134_pci_tbl
[p
].subdevice
);
782 static struct video_device
*vdev_init(struct saa7134_dev
*dev
,
783 struct video_device
*template,
786 struct video_device
*vfd
;
788 vfd
= video_device_alloc();
792 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
793 vfd
->release
= video_device_release
;
794 vfd
->debug
= video_debug
;
795 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)",
796 dev
->name
, type
, saa7134_boards
[dev
->board
].name
);
797 video_set_drvdata(vfd
, dev
);
801 static void saa7134_unregister_video(struct saa7134_dev
*dev
)
803 if (dev
->video_dev
) {
804 if (video_is_registered(dev
->video_dev
))
805 video_unregister_device(dev
->video_dev
);
807 video_device_release(dev
->video_dev
);
808 dev
->video_dev
= NULL
;
811 if (video_is_registered(dev
->vbi_dev
))
812 video_unregister_device(dev
->vbi_dev
);
814 video_device_release(dev
->vbi_dev
);
817 if (dev
->radio_dev
) {
818 if (video_is_registered(dev
->radio_dev
))
819 video_unregister_device(dev
->radio_dev
);
821 video_device_release(dev
->radio_dev
);
822 dev
->radio_dev
= NULL
;
826 static void mpeg_ops_attach(struct saa7134_mpeg_ops
*ops
,
827 struct saa7134_dev
*dev
)
831 if (NULL
!= dev
->mops
)
833 if (saa7134_boards
[dev
->board
].mpeg
!= ops
->type
)
835 err
= ops
->init(dev
);
841 static void mpeg_ops_detach(struct saa7134_mpeg_ops
*ops
,
842 struct saa7134_dev
*dev
)
844 if (NULL
== dev
->mops
)
846 if (dev
->mops
!= ops
)
848 dev
->mops
->fini(dev
);
852 static int __devinit
saa7134_initdev(struct pci_dev
*pci_dev
,
853 const struct pci_device_id
*pci_id
)
855 struct saa7134_dev
*dev
;
856 struct saa7134_mpeg_ops
*mops
;
859 if (saa7134_devcount
== SAA7134_MAXBOARDS
)
862 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
866 err
= v4l2_device_register(&pci_dev
->dev
, &dev
->v4l2_dev
);
872 if (pci_enable_device(pci_dev
)) {
877 dev
->nr
= saa7134_devcount
;
878 sprintf(dev
->name
,"saa%x[%d]",pci_dev
->device
,dev
->nr
);
881 if (pci_pci_problems
) {
882 if (pci_pci_problems
& PCIPCI_TRITON
)
883 printk(KERN_INFO
"%s: quirk: PCIPCI_TRITON\n", dev
->name
);
884 if (pci_pci_problems
& PCIPCI_NATOMA
)
885 printk(KERN_INFO
"%s: quirk: PCIPCI_NATOMA\n", dev
->name
);
886 if (pci_pci_problems
& PCIPCI_VIAETBF
)
887 printk(KERN_INFO
"%s: quirk: PCIPCI_VIAETBF\n", dev
->name
);
888 if (pci_pci_problems
& PCIPCI_VSFX
)
889 printk(KERN_INFO
"%s: quirk: PCIPCI_VSFX\n",dev
->name
);
890 #ifdef PCIPCI_ALIMAGIK
891 if (pci_pci_problems
& PCIPCI_ALIMAGIK
) {
892 printk(KERN_INFO
"%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
897 if (pci_pci_problems
& (PCIPCI_FAIL
|PCIAGP_FAIL
)) {
898 printk(KERN_INFO
"%s: quirk: this driver and your "
899 "chipset may not work together"
900 " in overlay mode.\n",dev
->name
);
901 if (!saa7134_no_overlay
) {
902 printk(KERN_INFO
"%s: quirk: overlay "
903 "mode will be disabled.\n",
905 saa7134_no_overlay
= 1;
907 printk(KERN_INFO
"%s: quirk: overlay "
908 "mode will be forced. Use this"
909 " option at your own risk.\n",
914 if (UNSET
!= latency
) {
915 printk(KERN_INFO
"%s: setting pci latency timer to %d\n",
917 pci_write_config_byte(pci_dev
, PCI_LATENCY_TIMER
, latency
);
921 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
922 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
923 printk(KERN_INFO
"%s: found at %s, rev: %d, irq: %d, "
924 "latency: %d, mmio: 0x%llx\n", dev
->name
,
925 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
926 dev
->pci_lat
,(unsigned long long)pci_resource_start(pci_dev
,0));
927 pci_set_master(pci_dev
);
928 if (!pci_dma_supported(pci_dev
, DMA_BIT_MASK(32))) {
929 printk("%s: Oops: no 32bit PCI DMA ???\n",dev
->name
);
935 dev
->board
= pci_id
->driver_data
;
936 if (card
[dev
->nr
] >= 0 &&
937 card
[dev
->nr
] < saa7134_bcount
)
938 dev
->board
= card
[dev
->nr
];
939 if (SAA7134_BOARD_NOAUTO
== dev
->board
) {
940 must_configure_manually();
941 dev
->board
= SAA7134_BOARD_UNKNOWN
;
943 dev
->autodetected
= card
[dev
->nr
] != dev
->board
;
944 dev
->tuner_type
= saa7134_boards
[dev
->board
].tuner_type
;
945 dev
->tuner_addr
= saa7134_boards
[dev
->board
].tuner_addr
;
946 dev
->radio_type
= saa7134_boards
[dev
->board
].radio_type
;
947 dev
->radio_addr
= saa7134_boards
[dev
->board
].radio_addr
;
948 dev
->tda9887_conf
= saa7134_boards
[dev
->board
].tda9887_conf
;
949 if (UNSET
!= tuner
[dev
->nr
])
950 dev
->tuner_type
= tuner
[dev
->nr
];
951 printk(KERN_INFO
"%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
952 dev
->name
,pci_dev
->subsystem_vendor
,
953 pci_dev
->subsystem_device
,saa7134_boards
[dev
->board
].name
,
954 dev
->board
, dev
->autodetected
?
955 "autodetected" : "insmod option");
958 if (!request_mem_region(pci_resource_start(pci_dev
,0),
959 pci_resource_len(pci_dev
,0),
962 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%llx\n",
963 dev
->name
,(unsigned long long)pci_resource_start(pci_dev
,0));
966 dev
->lmmio
= ioremap(pci_resource_start(pci_dev
, 0),
967 pci_resource_len(pci_dev
, 0));
968 dev
->bmmio
= (__u8 __iomem
*)dev
->lmmio
;
969 if (NULL
== dev
->lmmio
) {
971 printk(KERN_ERR
"%s: can't ioremap() MMIO memory\n",
976 /* initialize hardware #1 */
977 saa7134_board_init1(dev
);
978 saa7134_hwinit1(dev
);
981 err
= request_irq(pci_dev
->irq
, saa7134_irq
,
982 IRQF_SHARED
| IRQF_DISABLED
, dev
->name
, dev
);
984 printk(KERN_ERR
"%s: can't get IRQ %d\n",
985 dev
->name
,pci_dev
->irq
);
989 /* wait a bit, register i2c bus */
991 saa7134_i2c_register(dev
);
992 saa7134_board_init2(dev
);
994 saa7134_hwinit2(dev
);
996 /* load i2c helpers */
997 if (card_is_empress(dev
)) {
998 struct v4l2_subdev
*sd
=
999 v4l2_i2c_new_subdev(&dev
->v4l2_dev
, &dev
->i2c_adap
,
1001 saa7134_boards
[dev
->board
].empress_addr
, NULL
);
1004 sd
->grp_id
= GRP_EMPRESS
;
1007 if (saa7134_boards
[dev
->board
].rds_addr
) {
1008 struct v4l2_subdev
*sd
;
1010 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1011 &dev
->i2c_adap
, "saa6588",
1012 0, I2C_ADDRS(saa7134_boards
[dev
->board
].rds_addr
));
1014 printk(KERN_INFO
"%s: found RDS decoder\n", dev
->name
);
1019 v4l2_prio_init(&dev
->prio
);
1021 mutex_lock(&saa7134_devlist_lock
);
1022 list_for_each_entry(mops
, &mops_list
, next
)
1023 mpeg_ops_attach(mops
, dev
);
1024 list_add_tail(&dev
->devlist
, &saa7134_devlist
);
1025 mutex_unlock(&saa7134_devlist_lock
);
1027 /* check for signal */
1028 saa7134_irq_video_signalchange(dev
);
1030 if (TUNER_ABSENT
!= dev
->tuner_type
)
1031 saa_call_all(dev
, core
, s_power
, 0);
1033 /* register v4l devices */
1034 if (saa7134_no_overlay
> 0)
1035 printk(KERN_INFO
"%s: Overlay support disabled.\n", dev
->name
);
1037 dev
->video_dev
= vdev_init(dev
,&saa7134_video_template
,"video");
1038 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1041 printk(KERN_INFO
"%s: can't register video device\n",
1045 printk(KERN_INFO
"%s: registered device %s [v4l2]\n",
1046 dev
->name
, video_device_node_name(dev
->video_dev
));
1048 dev
->vbi_dev
= vdev_init(dev
, &saa7134_video_template
, "vbi");
1050 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1054 printk(KERN_INFO
"%s: registered device %s\n",
1055 dev
->name
, video_device_node_name(dev
->vbi_dev
));
1057 if (card_has_radio(dev
)) {
1058 dev
->radio_dev
= vdev_init(dev
,&saa7134_radio_template
,"radio");
1059 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1063 printk(KERN_INFO
"%s: registered device %s\n",
1064 dev
->name
, video_device_node_name(dev
->radio_dev
));
1067 /* everything worked */
1070 if (saa7134_dmasound_init
&& !dev
->dmasound
.priv_data
)
1071 saa7134_dmasound_init(dev
);
1073 request_submodules(dev
);
1077 saa7134_unregister_video(dev
);
1078 saa7134_i2c_unregister(dev
);
1079 free_irq(pci_dev
->irq
, dev
);
1081 saa7134_hwfini(dev
);
1082 iounmap(dev
->lmmio
);
1084 release_mem_region(pci_resource_start(pci_dev
,0),
1085 pci_resource_len(pci_dev
,0));
1087 v4l2_device_unregister(&dev
->v4l2_dev
);
1093 static void __devexit
saa7134_finidev(struct pci_dev
*pci_dev
)
1095 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1096 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1097 struct saa7134_mpeg_ops
*mops
;
1099 flush_request_submodules(dev
);
1101 /* Release DMA sound modules if present */
1102 if (saa7134_dmasound_exit
&& dev
->dmasound
.priv_data
) {
1103 saa7134_dmasound_exit(dev
);
1108 u32 report
= saa_readl(SAA7134_IRQ_REPORT
);
1109 u32 status
= saa_readl(SAA7134_IRQ_STATUS
);
1110 print_irqstatus(dev
,42,report
,status
);
1113 /* disable peripheral devices */
1114 saa_writeb(SAA7134_SPECIAL_MODE
,0);
1116 /* shutdown hardware */
1117 saa_writel(SAA7134_IRQ1
,0);
1118 saa_writel(SAA7134_IRQ2
,0);
1119 saa_writel(SAA7134_MAIN_CTRL
,0);
1121 /* shutdown subsystems */
1122 saa7134_hwfini(dev
);
1125 mutex_lock(&saa7134_devlist_lock
);
1126 list_del(&dev
->devlist
);
1127 list_for_each_entry(mops
, &mops_list
, next
)
1128 mpeg_ops_detach(mops
, dev
);
1129 mutex_unlock(&saa7134_devlist_lock
);
1132 saa7134_i2c_unregister(dev
);
1133 saa7134_unregister_video(dev
);
1136 /* the DMA sound modules should be unloaded before reaching
1137 this, but just in case they are still present... */
1138 if (dev
->dmasound
.priv_data
!= NULL
) {
1139 free_irq(pci_dev
->irq
, &dev
->dmasound
);
1140 dev
->dmasound
.priv_data
= NULL
;
1144 /* release resources */
1145 free_irq(pci_dev
->irq
, dev
);
1146 iounmap(dev
->lmmio
);
1147 release_mem_region(pci_resource_start(pci_dev
,0),
1148 pci_resource_len(pci_dev
,0));
1151 v4l2_device_unregister(&dev
->v4l2_dev
);
1159 /* resends a current buffer in queue after resume */
1160 static int saa7134_buffer_requeue(struct saa7134_dev
*dev
,
1161 struct saa7134_dmaqueue
*q
)
1163 struct saa7134_buf
*buf
, *next
;
1165 assert_spin_locked(&dev
->slock
);
1169 dprintk("buffer_requeue\n");
1174 dprintk("buffer_requeue : resending active buffers \n");
1176 if (!list_empty(&q
->queue
))
1177 next
= list_entry(q
->queue
.next
, struct saa7134_buf
,
1179 buf
->activate(dev
, buf
, next
);
1184 static int saa7134_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1186 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1187 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1189 /* disable overlay - apps should enable it explicitly on resume*/
1192 /* Disable interrupts, DMA, and rest of the chip*/
1193 saa_writel(SAA7134_IRQ1
, 0);
1194 saa_writel(SAA7134_IRQ2
, 0);
1195 saa_writel(SAA7134_MAIN_CTRL
, 0);
1198 synchronize_irq(pci_dev
->irq
);
1200 /* ACK interrupts once more, just in case,
1201 since the IRQ handler won't ack them anymore*/
1203 saa_writel(SAA7134_IRQ_REPORT
, saa_readl(SAA7134_IRQ_REPORT
));
1205 /* Disable timeout timers - if we have active buffers, we will
1206 fill them on resume*/
1208 del_timer(&dev
->video_q
.timeout
);
1209 del_timer(&dev
->vbi_q
.timeout
);
1210 del_timer(&dev
->ts_q
.timeout
);
1213 saa7134_ir_stop(dev
);
1215 pci_save_state(pci_dev
);
1216 pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
));
1221 static int saa7134_resume(struct pci_dev
*pci_dev
)
1223 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1224 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1225 unsigned long flags
;
1227 pci_set_power_state(pci_dev
, PCI_D0
);
1228 pci_restore_state(pci_dev
);
1230 /* Do things that are done in saa7134_initdev ,
1231 except of initializing memory structures.*/
1233 saa7134_board_init1(dev
);
1235 /* saa7134_hwinit1 */
1236 if (saa7134_boards
[dev
->board
].video_out
)
1237 saa7134_videoport_init(dev
);
1238 if (card_has_mpeg(dev
))
1239 saa7134_ts_init_hw(dev
);
1241 saa7134_ir_start(dev
);
1242 saa7134_hw_enable1(dev
);
1246 saa7134_board_init2(dev
);
1249 saa7134_set_tvnorm_hw(dev
);
1250 saa7134_tvaudio_setmute(dev
);
1251 saa7134_tvaudio_setvolume(dev
, dev
->ctl_volume
);
1252 saa7134_tvaudio_init(dev
);
1253 saa7134_tvaudio_do_scan(dev
);
1254 saa7134_enable_i2s(dev
);
1255 saa7134_hw_enable2(dev
);
1257 saa7134_irq_video_signalchange(dev
);
1259 /*resume unfinished buffer(s)*/
1260 spin_lock_irqsave(&dev
->slock
, flags
);
1261 saa7134_buffer_requeue(dev
, &dev
->video_q
);
1262 saa7134_buffer_requeue(dev
, &dev
->vbi_q
);
1263 saa7134_buffer_requeue(dev
, &dev
->ts_q
);
1265 /* FIXME: Disable DMA audio sound - temporary till proper support
1268 dev
->dmasound
.dma_running
= 0;
1273 saa7134_set_dmabits(dev
);
1274 spin_unlock_irqrestore(&dev
->slock
, flags
);
1280 /* ----------------------------------------------------------- */
1282 int saa7134_ts_register(struct saa7134_mpeg_ops
*ops
)
1284 struct saa7134_dev
*dev
;
1286 mutex_lock(&saa7134_devlist_lock
);
1287 list_for_each_entry(dev
, &saa7134_devlist
, devlist
)
1288 mpeg_ops_attach(ops
, dev
);
1289 list_add_tail(&ops
->next
,&mops_list
);
1290 mutex_unlock(&saa7134_devlist_lock
);
1294 void saa7134_ts_unregister(struct saa7134_mpeg_ops
*ops
)
1296 struct saa7134_dev
*dev
;
1298 mutex_lock(&saa7134_devlist_lock
);
1299 list_del(&ops
->next
);
1300 list_for_each_entry(dev
, &saa7134_devlist
, devlist
)
1301 mpeg_ops_detach(ops
, dev
);
1302 mutex_unlock(&saa7134_devlist_lock
);
1305 EXPORT_SYMBOL(saa7134_ts_register
);
1306 EXPORT_SYMBOL(saa7134_ts_unregister
);
1308 /* ----------------------------------------------------------- */
1310 static struct pci_driver saa7134_pci_driver
= {
1312 .id_table
= saa7134_pci_tbl
,
1313 .probe
= saa7134_initdev
,
1314 .remove
= __devexit_p(saa7134_finidev
),
1316 .suspend
= saa7134_suspend
,
1317 .resume
= saa7134_resume
1321 static int __init
saa7134_init(void)
1323 INIT_LIST_HEAD(&saa7134_devlist
);
1324 printk(KERN_INFO
"saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1325 (SAA7134_VERSION_CODE
>> 16) & 0xff,
1326 (SAA7134_VERSION_CODE
>> 8) & 0xff,
1327 SAA7134_VERSION_CODE
& 0xff);
1329 printk(KERN_INFO
"saa7130/34: snapshot date %04d-%02d-%02d\n",
1330 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
1332 return pci_register_driver(&saa7134_pci_driver
);
1335 static void __exit
saa7134_fini(void)
1337 pci_unregister_driver(&saa7134_pci_driver
);
1340 module_init(saa7134_init
);
1341 module_exit(saa7134_fini
);
1343 /* ----------------------------------------------------------- */
1345 EXPORT_SYMBOL(saa7134_set_gpio
);
1346 EXPORT_SYMBOL(saa7134_boards
);
1348 /* ----------------- for the DMA sound modules --------------- */
1350 EXPORT_SYMBOL(saa7134_dmasound_init
);
1351 EXPORT_SYMBOL(saa7134_dmasound_exit
);
1352 EXPORT_SYMBOL(saa7134_pgtable_free
);
1353 EXPORT_SYMBOL(saa7134_pgtable_build
);
1354 EXPORT_SYMBOL(saa7134_pgtable_alloc
);
1355 EXPORT_SYMBOL(saa7134_set_dmabits
);
1357 /* ----------------------------------------------------------- */