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");
42 MODULE_VERSION(SAA7134_VERSION
);
45 /* ------------------------------------------------------------------ */
47 static unsigned int irq_debug
;
48 module_param(irq_debug
, int, 0644);
49 MODULE_PARM_DESC(irq_debug
,"enable debug messages [IRQ handler]");
51 static unsigned int core_debug
;
52 module_param(core_debug
, int, 0644);
53 MODULE_PARM_DESC(core_debug
,"enable debug messages [core]");
55 static unsigned int gpio_tracking
;
56 module_param(gpio_tracking
, int, 0644);
57 MODULE_PARM_DESC(gpio_tracking
,"enable debug messages [gpio]");
59 static unsigned int alsa
= 1;
60 module_param(alsa
, int, 0644);
61 MODULE_PARM_DESC(alsa
,"enable/disable ALSA DMA sound [dmasound]");
63 static unsigned int latency
= UNSET
;
64 module_param(latency
, int, 0444);
65 MODULE_PARM_DESC(latency
,"pci latency timer");
67 int saa7134_no_overlay
=-1;
68 module_param_named(no_overlay
, saa7134_no_overlay
, int, 0444);
69 MODULE_PARM_DESC(no_overlay
,"allow override overlay default (0 disables, 1 enables)"
70 " [some VIA/SIS chipsets are known to have problem with overlay]");
72 static unsigned int video_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
73 static unsigned int vbi_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
74 static unsigned int radio_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
75 static unsigned int tuner
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
76 static unsigned int card
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
79 module_param_array(video_nr
, int, NULL
, 0444);
80 module_param_array(vbi_nr
, int, NULL
, 0444);
81 module_param_array(radio_nr
, int, NULL
, 0444);
82 module_param_array(tuner
, int, NULL
, 0444);
83 module_param_array(card
, int, NULL
, 0444);
85 MODULE_PARM_DESC(video_nr
, "video device number");
86 MODULE_PARM_DESC(vbi_nr
, "vbi device number");
87 MODULE_PARM_DESC(radio_nr
, "radio device number");
88 MODULE_PARM_DESC(tuner
, "tuner type");
89 MODULE_PARM_DESC(card
, "card type");
91 DEFINE_MUTEX(saa7134_devlist_lock
);
92 EXPORT_SYMBOL(saa7134_devlist_lock
);
93 LIST_HEAD(saa7134_devlist
);
94 EXPORT_SYMBOL(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 if (dev
->pci
->device
!= PCI_DEVICE_ID_PHILIPS_SAA7130
)
161 request_module("saa7134-alsa");
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
);
171 static void flush_request_submodules(struct saa7134_dev
*dev
)
173 flush_work_sync(&dev
->request_module_wk
);
177 #define request_submodules(dev)
178 #define flush_request_submodules(dev)
179 #endif /* CONFIG_MODULES */
181 /* ------------------------------------------------------------------ */
183 /* nr of (saa7134-)pages for the given buffer size */
184 static int saa7134_buffer_pages(int size
)
186 size
= PAGE_ALIGN(size
);
187 size
+= PAGE_SIZE
; /* for non-page-aligned buffers */
192 /* calc max # of buffers from size (must not exceed the 4MB virtual
193 * address space per DMA channel) */
194 int saa7134_buffer_count(unsigned int size
, unsigned int count
)
196 unsigned int maxcount
;
198 maxcount
= 1024 / saa7134_buffer_pages(size
);
199 if (count
> maxcount
)
204 int saa7134_buffer_startpage(struct saa7134_buf
*buf
)
206 return saa7134_buffer_pages(buf
->vb
.bsize
) * buf
->vb
.i
;
209 unsigned long saa7134_buffer_base(struct saa7134_buf
*buf
)
212 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
214 base
= saa7134_buffer_startpage(buf
) * 4096;
215 base
+= dma
->sglist
[0].offset
;
219 /* ------------------------------------------------------------------ */
221 int saa7134_pgtable_alloc(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
224 dma_addr_t dma_addr
= 0;
226 cpu
= pci_alloc_consistent(pci
, SAA7134_PGTABLE_SIZE
, &dma_addr
);
229 pt
->size
= SAA7134_PGTABLE_SIZE
;
235 int saa7134_pgtable_build(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
,
236 struct scatterlist
*list
, unsigned int length
,
237 unsigned int startpage
)
242 BUG_ON(NULL
== pt
|| NULL
== pt
->cpu
);
244 ptr
= pt
->cpu
+ startpage
;
245 for (i
= 0; i
< length
; i
++, list
++)
246 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr
++)
247 *ptr
= cpu_to_le32(sg_dma_address(list
) - list
->offset
);
251 void saa7134_pgtable_free(struct pci_dev
*pci
, struct saa7134_pgtable
*pt
)
255 pci_free_consistent(pci
, pt
->size
, pt
->cpu
, pt
->dma
);
259 /* ------------------------------------------------------------------ */
261 void saa7134_dma_free(struct videobuf_queue
*q
,struct saa7134_buf
*buf
)
263 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
264 BUG_ON(in_interrupt());
266 videobuf_waiton(q
, &buf
->vb
, 0, 0);
267 videobuf_dma_unmap(q
->dev
, dma
);
268 videobuf_dma_free(dma
);
269 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
272 /* ------------------------------------------------------------------ */
274 int saa7134_buffer_queue(struct saa7134_dev
*dev
,
275 struct saa7134_dmaqueue
*q
,
276 struct saa7134_buf
*buf
)
278 struct saa7134_buf
*next
= NULL
;
280 assert_spin_locked(&dev
->slock
);
281 dprintk("buffer_queue %p\n",buf
);
282 if (NULL
== q
->curr
) {
285 buf
->activate(dev
,buf
,NULL
);
286 } else if (list_empty(&q
->queue
)) {
287 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
288 buf
->vb
.state
= VIDEOBUF_QUEUED
;
290 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
293 buf
->activate(dev
,buf
,next
);
296 list_add_tail(&buf
->vb
.queue
,&q
->queue
);
297 buf
->vb
.state
= VIDEOBUF_QUEUED
;
302 void saa7134_buffer_finish(struct saa7134_dev
*dev
,
303 struct saa7134_dmaqueue
*q
,
306 assert_spin_locked(&dev
->slock
);
307 dprintk("buffer_finish %p\n",q
->curr
);
309 /* finish current buffer */
310 q
->curr
->vb
.state
= state
;
311 do_gettimeofday(&q
->curr
->vb
.ts
);
312 wake_up(&q
->curr
->vb
.done
);
316 void saa7134_buffer_next(struct saa7134_dev
*dev
,
317 struct saa7134_dmaqueue
*q
)
319 struct saa7134_buf
*buf
,*next
= NULL
;
321 assert_spin_locked(&dev
->slock
);
322 BUG_ON(NULL
!= q
->curr
);
324 if (!list_empty(&q
->queue
)) {
325 /* activate next one from queue */
326 buf
= list_entry(q
->queue
.next
,struct saa7134_buf
,vb
.queue
);
327 dprintk("buffer_next %p [prev=%p/next=%p]\n",
328 buf
,q
->queue
.prev
,q
->queue
.next
);
329 list_del(&buf
->vb
.queue
);
330 if (!list_empty(&q
->queue
))
331 next
= list_entry(q
->queue
.next
,struct saa7134_buf
,
334 buf
->activate(dev
,buf
,next
);
335 dprintk("buffer_next #2 prev=%p/next=%p\n",
336 q
->queue
.prev
,q
->queue
.next
);
338 /* nothing to do -- just stop DMA */
339 dprintk("buffer_next %p\n",NULL
);
340 saa7134_set_dmabits(dev
);
341 del_timer(&q
->timeout
);
343 if (card_has_mpeg(dev
))
345 saa7134_ts_stop(dev
);
349 void saa7134_buffer_timeout(unsigned long data
)
351 struct saa7134_dmaqueue
*q
= (struct saa7134_dmaqueue
*)data
;
352 struct saa7134_dev
*dev
= q
->dev
;
355 spin_lock_irqsave(&dev
->slock
,flags
);
357 /* try to reset the hardware (SWRST) */
358 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
359 saa_writeb(SAA7134_REGION_ENABLE
, 0x80);
360 saa_writeb(SAA7134_REGION_ENABLE
, 0x00);
362 /* flag current buffer as failed,
363 try to start over with the next one. */
365 dprintk("timeout on %p\n",q
->curr
);
366 saa7134_buffer_finish(dev
,q
,VIDEOBUF_ERROR
);
368 saa7134_buffer_next(dev
,q
);
369 spin_unlock_irqrestore(&dev
->slock
,flags
);
372 /* ------------------------------------------------------------------ */
374 int saa7134_set_dmabits(struct saa7134_dev
*dev
)
376 u32 split
, task
=0, ctrl
=0, irq
=0;
377 enum v4l2_field cap
= V4L2_FIELD_ANY
;
378 enum v4l2_field ov
= V4L2_FIELD_ANY
;
380 assert_spin_locked(&dev
->slock
);
385 /* video capture -- dma 0 + video task A */
386 if (dev
->video_q
.curr
) {
388 ctrl
|= SAA7134_MAIN_CTRL_TE0
;
389 irq
|= SAA7134_IRQ1_INTE_RA0_1
|
390 SAA7134_IRQ1_INTE_RA0_0
;
391 cap
= dev
->video_q
.curr
->vb
.field
;
394 /* video capture -- dma 1+2 (planar modes) */
395 if (dev
->video_q
.curr
&&
396 dev
->video_q
.curr
->fmt
->planar
) {
397 ctrl
|= SAA7134_MAIN_CTRL_TE4
|
398 SAA7134_MAIN_CTRL_TE5
;
401 /* screen overlay -- dma 0 + video task B */
404 ctrl
|= SAA7134_MAIN_CTRL_TE1
;
408 /* vbi capture -- dma 0 + vbi task A+B */
409 if (dev
->vbi_q
.curr
) {
411 ctrl
|= SAA7134_MAIN_CTRL_TE2
|
412 SAA7134_MAIN_CTRL_TE3
;
413 irq
|= SAA7134_IRQ1_INTE_RA0_7
|
414 SAA7134_IRQ1_INTE_RA0_6
|
415 SAA7134_IRQ1_INTE_RA0_5
|
416 SAA7134_IRQ1_INTE_RA0_4
;
419 /* audio capture -- dma 3 */
420 if (dev
->dmasound
.dma_running
) {
421 ctrl
|= SAA7134_MAIN_CTRL_TE6
;
422 irq
|= SAA7134_IRQ1_INTE_RA3_1
|
423 SAA7134_IRQ1_INTE_RA3_0
;
426 /* TS capture -- dma 5 */
427 if (dev
->ts_q
.curr
) {
428 ctrl
|= SAA7134_MAIN_CTRL_TE5
;
429 irq
|= SAA7134_IRQ1_INTE_RA2_1
|
430 SAA7134_IRQ1_INTE_RA2_0
;
433 /* set task conditions + field handling */
434 if (V4L2_FIELD_HAS_BOTH(cap
) || V4L2_FIELD_HAS_BOTH(ov
) || cap
== ov
) {
435 /* default config -- use full frames */
436 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
437 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
438 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x02);
439 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x02);
442 /* split fields between tasks */
443 if (V4L2_FIELD_TOP
== cap
) {
444 /* odd A, even B, repeat */
445 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0d);
446 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0e);
448 /* odd B, even A, repeat */
449 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A
), 0x0e);
450 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B
), 0x0d);
452 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A
), 0x01);
453 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B
), 0x01);
458 saa_writeb(SAA7134_REGION_ENABLE
, task
);
459 saa_writel(SAA7134_IRQ1
, irq
);
460 saa_andorl(SAA7134_MAIN_CTRL
,
461 SAA7134_MAIN_CTRL_TE0
|
462 SAA7134_MAIN_CTRL_TE1
|
463 SAA7134_MAIN_CTRL_TE2
|
464 SAA7134_MAIN_CTRL_TE3
|
465 SAA7134_MAIN_CTRL_TE4
|
466 SAA7134_MAIN_CTRL_TE5
|
467 SAA7134_MAIN_CTRL_TE6
,
469 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
470 task
, ctrl
, irq
, split
? "no" : "yes");
475 /* ------------------------------------------------------------------ */
476 /* IRQ handler + helpers */
478 static char *irqbits
[] = {
479 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
480 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
481 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
482 "GPIO16", "GPIO18", "GPIO22", "GPIO23"
484 #define IRQBITS ARRAY_SIZE(irqbits)
486 static void print_irqstatus(struct saa7134_dev
*dev
, int loop
,
487 unsigned long report
, unsigned long status
)
491 printk(KERN_DEBUG
"%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
492 dev
->name
,loop
,jiffies
,report
,status
);
493 for (i
= 0; i
< IRQBITS
; i
++) {
494 if (!(report
& (1 << i
)))
496 printk(" %s",irqbits
[i
]);
498 if (report
& SAA7134_IRQ_REPORT_DONE_RA0
) {
499 printk(" | RA0=%s,%s,%s,%ld",
500 (status
& 0x40) ? "vbi" : "video",
501 (status
& 0x20) ? "b" : "a",
502 (status
& 0x10) ? "odd" : "even",
508 static irqreturn_t
saa7134_irq(int irq
, void *dev_id
)
510 struct saa7134_dev
*dev
= (struct saa7134_dev
*) dev_id
;
511 unsigned long report
,status
;
512 int loop
, handled
= 0;
517 for (loop
= 0; loop
< 10; loop
++) {
518 report
= saa_readl(SAA7134_IRQ_REPORT
);
519 status
= saa_readl(SAA7134_IRQ_STATUS
);
521 /* If dmasound support is active and we get a sound report,
522 * mask out the report and let the saa7134-alsa module deal
524 if ((report
& SAA7134_IRQ_REPORT_DONE_RA3
) &&
525 (dev
->dmasound
.priv_data
!= NULL
) )
528 printk(KERN_DEBUG
"%s/irq: preserving DMA sound interrupt\n",
530 report
&= ~SAA7134_IRQ_REPORT_DONE_RA3
;
535 printk(KERN_DEBUG
"%s/irq: no (more) work\n",
541 saa_writel(SAA7134_IRQ_REPORT
,report
);
543 print_irqstatus(dev
,loop
,report
,status
);
546 if ((report
& SAA7134_IRQ_REPORT_RDCAP
) ||
547 (report
& SAA7134_IRQ_REPORT_INTL
))
548 saa7134_irq_video_signalchange(dev
);
551 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
552 (status
& 0x60) == 0)
553 saa7134_irq_video_done(dev
,status
);
555 if ((report
& SAA7134_IRQ_REPORT_DONE_RA0
) &&
556 (status
& 0x40) == 0x40)
557 saa7134_irq_vbi_done(dev
,status
);
559 if ((report
& SAA7134_IRQ_REPORT_DONE_RA2
) &&
561 saa7134_irq_ts_done(dev
,status
);
563 if (report
& SAA7134_IRQ_REPORT_GPIO16
) {
564 switch (dev
->has_remote
) {
565 case SAA7134_REMOTE_GPIO
:
568 if (dev
->remote
->mask_keydown
& 0x10000) {
569 saa7134_input_irq(dev
);
573 case SAA7134_REMOTE_I2C
:
574 break; /* FIXME: invoke I2C get_key() */
576 default: /* GPIO16 not used by IR remote */
581 if (report
& SAA7134_IRQ_REPORT_GPIO18
) {
582 switch (dev
->has_remote
) {
583 case SAA7134_REMOTE_GPIO
:
586 if ((dev
->remote
->mask_keydown
& 0x40000) ||
587 (dev
->remote
->mask_keyup
& 0x40000)) {
588 saa7134_input_irq(dev
);
592 case SAA7134_REMOTE_I2C
:
593 break; /* FIXME: invoke I2C get_key() */
595 default: /* GPIO18 not used by IR remote */
602 print_irqstatus(dev
,loop
,report
,status
);
603 if (report
& SAA7134_IRQ_REPORT_PE
) {
604 /* disable all parity error */
605 printk(KERN_WARNING
"%s/irq: looping -- "
606 "clearing PE (parity error!) enable bit\n",dev
->name
);
607 saa_clearl(SAA7134_IRQ2
,SAA7134_IRQ2_INTE_PE
);
608 } else if (report
& SAA7134_IRQ_REPORT_GPIO16
) {
609 /* disable gpio16 IRQ */
610 printk(KERN_WARNING
"%s/irq: looping -- "
611 "clearing GPIO16 enable bit\n",dev
->name
);
612 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO16_P
);
613 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO16_N
);
614 } else if (report
& SAA7134_IRQ_REPORT_GPIO18
) {
615 /* disable gpio18 IRQs */
616 printk(KERN_WARNING
"%s/irq: looping -- "
617 "clearing GPIO18 enable bit\n",dev
->name
);
618 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO18_P
);
619 saa_clearl(SAA7134_IRQ2
, SAA7134_IRQ2_INTE_GPIO18_N
);
621 /* disable all irqs */
622 printk(KERN_WARNING
"%s/irq: looping -- "
623 "clearing all enable bits\n",dev
->name
);
624 saa_writel(SAA7134_IRQ1
,0);
625 saa_writel(SAA7134_IRQ2
,0);
630 return IRQ_RETVAL(handled
);
633 /* ------------------------------------------------------------------ */
635 /* early init (no i2c, no irq) */
637 static int saa7134_hw_enable1(struct saa7134_dev
*dev
)
639 /* RAM FIFO config */
640 saa_writel(SAA7134_FIFO_SIZE
, 0x08070503);
641 saa_writel(SAA7134_THRESHOULD
, 0x02020202);
643 /* enable audio + video processing */
644 saa_writel(SAA7134_MAIN_CTRL
,
645 SAA7134_MAIN_CTRL_VPLLE
|
646 SAA7134_MAIN_CTRL_APLLE
|
647 SAA7134_MAIN_CTRL_EXOSC
|
648 SAA7134_MAIN_CTRL_EVFE1
|
649 SAA7134_MAIN_CTRL_EVFE2
|
650 SAA7134_MAIN_CTRL_ESFE
|
651 SAA7134_MAIN_CTRL_EBDAC
);
654 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
655 * OSS initialization writes to registers via the audio DSP; these
656 * writes will fail unless the audio clock has been started. At worst,
657 * audio will not work.
660 /* enable peripheral devices */
661 saa_writeb(SAA7134_SPECIAL_MODE
, 0x01);
663 /* set vertical line numbering start (vbi needs this) */
664 saa_writeb(SAA7134_SOURCE_TIMING2
, 0x20);
669 static int saa7134_hwinit1(struct saa7134_dev
*dev
)
671 dprintk("hwinit1\n");
673 saa_writel(SAA7134_IRQ1
, 0);
674 saa_writel(SAA7134_IRQ2
, 0);
676 /* Clear any stale IRQ reports */
677 saa_writel(SAA7134_IRQ_REPORT
, saa_readl(SAA7134_IRQ_REPORT
));
679 mutex_init(&dev
->lock
);
680 spin_lock_init(&dev
->slock
);
682 saa7134_track_gpio(dev
,"pre-init");
683 saa7134_video_init1(dev
);
684 saa7134_vbi_init1(dev
);
685 if (card_has_mpeg(dev
))
686 saa7134_ts_init1(dev
);
687 saa7134_input_init1(dev
);
689 saa7134_hw_enable1(dev
);
694 /* late init (with i2c + irq) */
695 static int saa7134_hw_enable2(struct saa7134_dev
*dev
)
698 unsigned int irq2_mask
;
702 SAA7134_IRQ2_INTE_DEC3
|
703 SAA7134_IRQ2_INTE_DEC2
|
704 SAA7134_IRQ2_INTE_DEC1
|
705 SAA7134_IRQ2_INTE_DEC0
|
706 SAA7134_IRQ2_INTE_PE
|
707 SAA7134_IRQ2_INTE_AR
;
709 if (dev
->has_remote
== SAA7134_REMOTE_GPIO
&& dev
->remote
) {
710 if (dev
->remote
->mask_keydown
& 0x10000)
711 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO16_N
;
712 else { /* Allow enabling both IRQ edge triggers */
713 if (dev
->remote
->mask_keydown
& 0x40000)
714 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO18_P
;
715 if (dev
->remote
->mask_keyup
& 0x40000)
716 irq2_mask
|= SAA7134_IRQ2_INTE_GPIO18_N
;
720 if (dev
->has_remote
== SAA7134_REMOTE_I2C
) {
721 request_module("ir-kbd-i2c");
724 saa_writel(SAA7134_IRQ1
, 0);
725 saa_writel(SAA7134_IRQ2
, irq2_mask
);
730 static int saa7134_hwinit2(struct saa7134_dev
*dev
)
733 dprintk("hwinit2\n");
735 saa7134_video_init2(dev
);
736 saa7134_tvaudio_init2(dev
);
738 saa7134_hw_enable2(dev
);
745 static int saa7134_hwfini(struct saa7134_dev
*dev
)
749 if (card_has_mpeg(dev
))
750 saa7134_ts_fini(dev
);
751 saa7134_input_fini(dev
);
752 saa7134_vbi_fini(dev
);
753 saa7134_tvaudio_fini(dev
);
757 static void __devinit
must_configure_manually(int has_eeprom
)
764 "saa7134: Congratulations! Your TV card vendor saved a few\n"
765 "saa7134: cents for a eeprom, thus your pci board has no\n"
766 "saa7134: subsystem ID and I can't identify it automatically\n"
768 "saa7134: I feel better now. Ok, here are the good news:\n"
769 "saa7134: You can use the card=<nr> insmod option to specify\n"
770 "saa7134: which board do you have. The list:\n");
773 "saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
774 "saa7134: insmod option to specify which board do you have, but this is\n"
775 "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
776 "saa7134: for support at linux-media@vger.kernel.org.\n"
777 "saa7134: The supported cards are:\n");
779 for (i
= 0; i
< saa7134_bcount
; i
++) {
780 printk(KERN_WARNING
"saa7134: card=%d -> %-40.40s",
781 i
,saa7134_boards
[i
].name
);
782 for (p
= 0; saa7134_pci_tbl
[p
].driver_data
; p
++) {
783 if (saa7134_pci_tbl
[p
].driver_data
!= i
)
786 saa7134_pci_tbl
[p
].subvendor
,
787 saa7134_pci_tbl
[p
].subdevice
);
793 static struct video_device
*vdev_init(struct saa7134_dev
*dev
,
794 struct video_device
*template,
797 struct video_device
*vfd
;
799 vfd
= video_device_alloc();
803 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
804 vfd
->release
= video_device_release
;
805 vfd
->debug
= video_debug
;
806 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)",
807 dev
->name
, type
, saa7134_boards
[dev
->board
].name
);
808 video_set_drvdata(vfd
, dev
);
812 static void saa7134_unregister_video(struct saa7134_dev
*dev
)
814 if (dev
->video_dev
) {
815 if (video_is_registered(dev
->video_dev
))
816 video_unregister_device(dev
->video_dev
);
818 video_device_release(dev
->video_dev
);
819 dev
->video_dev
= NULL
;
822 if (video_is_registered(dev
->vbi_dev
))
823 video_unregister_device(dev
->vbi_dev
);
825 video_device_release(dev
->vbi_dev
);
828 if (dev
->radio_dev
) {
829 if (video_is_registered(dev
->radio_dev
))
830 video_unregister_device(dev
->radio_dev
);
832 video_device_release(dev
->radio_dev
);
833 dev
->radio_dev
= NULL
;
837 static void mpeg_ops_attach(struct saa7134_mpeg_ops
*ops
,
838 struct saa7134_dev
*dev
)
842 if (NULL
!= dev
->mops
)
844 if (saa7134_boards
[dev
->board
].mpeg
!= ops
->type
)
846 err
= ops
->init(dev
);
852 static void mpeg_ops_detach(struct saa7134_mpeg_ops
*ops
,
853 struct saa7134_dev
*dev
)
855 if (NULL
== dev
->mops
)
857 if (dev
->mops
!= ops
)
859 dev
->mops
->fini(dev
);
863 static int __devinit
saa7134_initdev(struct pci_dev
*pci_dev
,
864 const struct pci_device_id
*pci_id
)
866 struct saa7134_dev
*dev
;
867 struct saa7134_mpeg_ops
*mops
;
870 if (saa7134_devcount
== SAA7134_MAXBOARDS
)
873 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
877 err
= v4l2_device_register(&pci_dev
->dev
, &dev
->v4l2_dev
);
883 if (pci_enable_device(pci_dev
)) {
888 dev
->nr
= saa7134_devcount
;
889 sprintf(dev
->name
,"saa%x[%d]",pci_dev
->device
,dev
->nr
);
892 if (pci_pci_problems
) {
893 if (pci_pci_problems
& PCIPCI_TRITON
)
894 printk(KERN_INFO
"%s: quirk: PCIPCI_TRITON\n", dev
->name
);
895 if (pci_pci_problems
& PCIPCI_NATOMA
)
896 printk(KERN_INFO
"%s: quirk: PCIPCI_NATOMA\n", dev
->name
);
897 if (pci_pci_problems
& PCIPCI_VIAETBF
)
898 printk(KERN_INFO
"%s: quirk: PCIPCI_VIAETBF\n", dev
->name
);
899 if (pci_pci_problems
& PCIPCI_VSFX
)
900 printk(KERN_INFO
"%s: quirk: PCIPCI_VSFX\n",dev
->name
);
901 #ifdef PCIPCI_ALIMAGIK
902 if (pci_pci_problems
& PCIPCI_ALIMAGIK
) {
903 printk(KERN_INFO
"%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
908 if (pci_pci_problems
& (PCIPCI_FAIL
|PCIAGP_FAIL
)) {
909 printk(KERN_INFO
"%s: quirk: this driver and your "
910 "chipset may not work together"
911 " in overlay mode.\n",dev
->name
);
912 if (!saa7134_no_overlay
) {
913 printk(KERN_INFO
"%s: quirk: overlay "
914 "mode will be disabled.\n",
916 saa7134_no_overlay
= 1;
918 printk(KERN_INFO
"%s: quirk: overlay "
919 "mode will be forced. Use this"
920 " option at your own risk.\n",
925 if (UNSET
!= latency
) {
926 printk(KERN_INFO
"%s: setting pci latency timer to %d\n",
928 pci_write_config_byte(pci_dev
, PCI_LATENCY_TIMER
, latency
);
932 dev
->pci_rev
= pci_dev
->revision
;
933 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
934 printk(KERN_INFO
"%s: found at %s, rev: %d, irq: %d, "
935 "latency: %d, mmio: 0x%llx\n", dev
->name
,
936 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
937 dev
->pci_lat
,(unsigned long long)pci_resource_start(pci_dev
,0));
938 pci_set_master(pci_dev
);
939 if (!pci_dma_supported(pci_dev
, DMA_BIT_MASK(32))) {
940 printk("%s: Oops: no 32bit PCI DMA ???\n",dev
->name
);
946 dev
->board
= pci_id
->driver_data
;
947 if (card
[dev
->nr
] >= 0 &&
948 card
[dev
->nr
] < saa7134_bcount
)
949 dev
->board
= card
[dev
->nr
];
950 if (SAA7134_BOARD_UNKNOWN
== dev
->board
)
951 must_configure_manually(0);
952 else if (SAA7134_BOARD_NOAUTO
== dev
->board
) {
953 must_configure_manually(1);
954 dev
->board
= SAA7134_BOARD_UNKNOWN
;
956 dev
->autodetected
= card
[dev
->nr
] != dev
->board
;
957 dev
->tuner_type
= saa7134_boards
[dev
->board
].tuner_type
;
958 dev
->tuner_addr
= saa7134_boards
[dev
->board
].tuner_addr
;
959 dev
->radio_type
= saa7134_boards
[dev
->board
].radio_type
;
960 dev
->radio_addr
= saa7134_boards
[dev
->board
].radio_addr
;
961 dev
->tda9887_conf
= saa7134_boards
[dev
->board
].tda9887_conf
;
962 if (UNSET
!= tuner
[dev
->nr
])
963 dev
->tuner_type
= tuner
[dev
->nr
];
964 printk(KERN_INFO
"%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
965 dev
->name
,pci_dev
->subsystem_vendor
,
966 pci_dev
->subsystem_device
,saa7134_boards
[dev
->board
].name
,
967 dev
->board
, dev
->autodetected
?
968 "autodetected" : "insmod option");
971 if (!request_mem_region(pci_resource_start(pci_dev
,0),
972 pci_resource_len(pci_dev
,0),
975 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%llx\n",
976 dev
->name
,(unsigned long long)pci_resource_start(pci_dev
,0));
979 dev
->lmmio
= ioremap(pci_resource_start(pci_dev
, 0),
980 pci_resource_len(pci_dev
, 0));
981 dev
->bmmio
= (__u8 __iomem
*)dev
->lmmio
;
982 if (NULL
== dev
->lmmio
) {
984 printk(KERN_ERR
"%s: can't ioremap() MMIO memory\n",
989 /* initialize hardware #1 */
990 saa7134_board_init1(dev
);
991 saa7134_hwinit1(dev
);
994 err
= request_irq(pci_dev
->irq
, saa7134_irq
,
995 IRQF_SHARED
| IRQF_DISABLED
, dev
->name
, dev
);
997 printk(KERN_ERR
"%s: can't get IRQ %d\n",
998 dev
->name
,pci_dev
->irq
);
1002 /* wait a bit, register i2c bus */
1004 saa7134_i2c_register(dev
);
1005 saa7134_board_init2(dev
);
1007 saa7134_hwinit2(dev
);
1009 /* load i2c helpers */
1010 if (card_is_empress(dev
)) {
1011 struct v4l2_subdev
*sd
=
1012 v4l2_i2c_new_subdev(&dev
->v4l2_dev
, &dev
->i2c_adap
,
1014 saa7134_boards
[dev
->board
].empress_addr
, NULL
);
1017 sd
->grp_id
= GRP_EMPRESS
;
1020 if (saa7134_boards
[dev
->board
].rds_addr
) {
1021 struct v4l2_subdev
*sd
;
1023 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1024 &dev
->i2c_adap
, "saa6588",
1025 0, I2C_ADDRS(saa7134_boards
[dev
->board
].rds_addr
));
1027 printk(KERN_INFO
"%s: found RDS decoder\n", dev
->name
);
1032 v4l2_prio_init(&dev
->prio
);
1034 mutex_lock(&saa7134_devlist_lock
);
1035 list_for_each_entry(mops
, &mops_list
, next
)
1036 mpeg_ops_attach(mops
, dev
);
1037 list_add_tail(&dev
->devlist
, &saa7134_devlist
);
1038 mutex_unlock(&saa7134_devlist_lock
);
1040 /* check for signal */
1041 saa7134_irq_video_signalchange(dev
);
1043 if (TUNER_ABSENT
!= dev
->tuner_type
)
1044 saa_call_all(dev
, core
, s_power
, 0);
1046 /* register v4l devices */
1047 if (saa7134_no_overlay
> 0)
1048 printk(KERN_INFO
"%s: Overlay support disabled.\n", dev
->name
);
1050 dev
->video_dev
= vdev_init(dev
,&saa7134_video_template
,"video");
1051 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1054 printk(KERN_INFO
"%s: can't register video device\n",
1058 printk(KERN_INFO
"%s: registered device %s [v4l2]\n",
1059 dev
->name
, video_device_node_name(dev
->video_dev
));
1061 dev
->vbi_dev
= vdev_init(dev
, &saa7134_video_template
, "vbi");
1063 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1067 printk(KERN_INFO
"%s: registered device %s\n",
1068 dev
->name
, video_device_node_name(dev
->vbi_dev
));
1070 if (card_has_radio(dev
)) {
1071 dev
->radio_dev
= vdev_init(dev
,&saa7134_radio_template
,"radio");
1072 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1076 printk(KERN_INFO
"%s: registered device %s\n",
1077 dev
->name
, video_device_node_name(dev
->radio_dev
));
1080 /* everything worked */
1083 if (saa7134_dmasound_init
&& !dev
->dmasound
.priv_data
)
1084 saa7134_dmasound_init(dev
);
1086 request_submodules(dev
);
1090 saa7134_unregister_video(dev
);
1091 saa7134_i2c_unregister(dev
);
1092 free_irq(pci_dev
->irq
, dev
);
1094 saa7134_hwfini(dev
);
1095 iounmap(dev
->lmmio
);
1097 release_mem_region(pci_resource_start(pci_dev
,0),
1098 pci_resource_len(pci_dev
,0));
1100 v4l2_device_unregister(&dev
->v4l2_dev
);
1106 static void __devexit
saa7134_finidev(struct pci_dev
*pci_dev
)
1108 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1109 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1110 struct saa7134_mpeg_ops
*mops
;
1112 flush_request_submodules(dev
);
1114 /* Release DMA sound modules if present */
1115 if (saa7134_dmasound_exit
&& dev
->dmasound
.priv_data
) {
1116 saa7134_dmasound_exit(dev
);
1121 u32 report
= saa_readl(SAA7134_IRQ_REPORT
);
1122 u32 status
= saa_readl(SAA7134_IRQ_STATUS
);
1123 print_irqstatus(dev
,42,report
,status
);
1126 /* disable peripheral devices */
1127 saa_writeb(SAA7134_SPECIAL_MODE
,0);
1129 /* shutdown hardware */
1130 saa_writel(SAA7134_IRQ1
,0);
1131 saa_writel(SAA7134_IRQ2
,0);
1132 saa_writel(SAA7134_MAIN_CTRL
,0);
1134 /* shutdown subsystems */
1135 saa7134_hwfini(dev
);
1138 mutex_lock(&saa7134_devlist_lock
);
1139 list_del(&dev
->devlist
);
1140 list_for_each_entry(mops
, &mops_list
, next
)
1141 mpeg_ops_detach(mops
, dev
);
1142 mutex_unlock(&saa7134_devlist_lock
);
1145 saa7134_i2c_unregister(dev
);
1146 saa7134_unregister_video(dev
);
1149 /* the DMA sound modules should be unloaded before reaching
1150 this, but just in case they are still present... */
1151 if (dev
->dmasound
.priv_data
!= NULL
) {
1152 free_irq(pci_dev
->irq
, &dev
->dmasound
);
1153 dev
->dmasound
.priv_data
= NULL
;
1157 /* release resources */
1158 free_irq(pci_dev
->irq
, dev
);
1159 iounmap(dev
->lmmio
);
1160 release_mem_region(pci_resource_start(pci_dev
,0),
1161 pci_resource_len(pci_dev
,0));
1164 v4l2_device_unregister(&dev
->v4l2_dev
);
1172 /* resends a current buffer in queue after resume */
1173 static int saa7134_buffer_requeue(struct saa7134_dev
*dev
,
1174 struct saa7134_dmaqueue
*q
)
1176 struct saa7134_buf
*buf
, *next
;
1178 assert_spin_locked(&dev
->slock
);
1182 dprintk("buffer_requeue\n");
1187 dprintk("buffer_requeue : resending active buffers \n");
1189 if (!list_empty(&q
->queue
))
1190 next
= list_entry(q
->queue
.next
, struct saa7134_buf
,
1192 buf
->activate(dev
, buf
, next
);
1197 static int saa7134_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1199 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1200 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1202 /* disable overlay - apps should enable it explicitly on resume*/
1205 /* Disable interrupts, DMA, and rest of the chip*/
1206 saa_writel(SAA7134_IRQ1
, 0);
1207 saa_writel(SAA7134_IRQ2
, 0);
1208 saa_writel(SAA7134_MAIN_CTRL
, 0);
1211 synchronize_irq(pci_dev
->irq
);
1213 /* ACK interrupts once more, just in case,
1214 since the IRQ handler won't ack them anymore*/
1216 saa_writel(SAA7134_IRQ_REPORT
, saa_readl(SAA7134_IRQ_REPORT
));
1218 /* Disable timeout timers - if we have active buffers, we will
1219 fill them on resume*/
1221 del_timer(&dev
->video_q
.timeout
);
1222 del_timer(&dev
->vbi_q
.timeout
);
1223 del_timer(&dev
->ts_q
.timeout
);
1226 saa7134_ir_stop(dev
);
1228 pci_save_state(pci_dev
);
1229 pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
));
1234 static int saa7134_resume(struct pci_dev
*pci_dev
)
1236 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pci_dev
);
1237 struct saa7134_dev
*dev
= container_of(v4l2_dev
, struct saa7134_dev
, v4l2_dev
);
1238 unsigned long flags
;
1240 pci_set_power_state(pci_dev
, PCI_D0
);
1241 pci_restore_state(pci_dev
);
1243 /* Do things that are done in saa7134_initdev ,
1244 except of initializing memory structures.*/
1246 saa7134_board_init1(dev
);
1248 /* saa7134_hwinit1 */
1249 if (saa7134_boards
[dev
->board
].video_out
)
1250 saa7134_videoport_init(dev
);
1251 if (card_has_mpeg(dev
))
1252 saa7134_ts_init_hw(dev
);
1254 saa7134_ir_start(dev
);
1255 saa7134_hw_enable1(dev
);
1259 saa7134_board_init2(dev
);
1262 saa7134_set_tvnorm_hw(dev
);
1263 saa7134_tvaudio_setmute(dev
);
1264 saa7134_tvaudio_setvolume(dev
, dev
->ctl_volume
);
1265 saa7134_tvaudio_init(dev
);
1266 saa7134_tvaudio_do_scan(dev
);
1267 saa7134_enable_i2s(dev
);
1268 saa7134_hw_enable2(dev
);
1270 saa7134_irq_video_signalchange(dev
);
1272 /*resume unfinished buffer(s)*/
1273 spin_lock_irqsave(&dev
->slock
, flags
);
1274 saa7134_buffer_requeue(dev
, &dev
->video_q
);
1275 saa7134_buffer_requeue(dev
, &dev
->vbi_q
);
1276 saa7134_buffer_requeue(dev
, &dev
->ts_q
);
1278 /* FIXME: Disable DMA audio sound - temporary till proper support
1281 dev
->dmasound
.dma_running
= 0;
1286 saa7134_set_dmabits(dev
);
1287 spin_unlock_irqrestore(&dev
->slock
, flags
);
1293 /* ----------------------------------------------------------- */
1295 int saa7134_ts_register(struct saa7134_mpeg_ops
*ops
)
1297 struct saa7134_dev
*dev
;
1299 mutex_lock(&saa7134_devlist_lock
);
1300 list_for_each_entry(dev
, &saa7134_devlist
, devlist
)
1301 mpeg_ops_attach(ops
, dev
);
1302 list_add_tail(&ops
->next
,&mops_list
);
1303 mutex_unlock(&saa7134_devlist_lock
);
1307 void saa7134_ts_unregister(struct saa7134_mpeg_ops
*ops
)
1309 struct saa7134_dev
*dev
;
1311 mutex_lock(&saa7134_devlist_lock
);
1312 list_del(&ops
->next
);
1313 list_for_each_entry(dev
, &saa7134_devlist
, devlist
)
1314 mpeg_ops_detach(ops
, dev
);
1315 mutex_unlock(&saa7134_devlist_lock
);
1318 EXPORT_SYMBOL(saa7134_ts_register
);
1319 EXPORT_SYMBOL(saa7134_ts_unregister
);
1321 /* ----------------------------------------------------------- */
1323 static struct pci_driver saa7134_pci_driver
= {
1325 .id_table
= saa7134_pci_tbl
,
1326 .probe
= saa7134_initdev
,
1327 .remove
= __devexit_p(saa7134_finidev
),
1329 .suspend
= saa7134_suspend
,
1330 .resume
= saa7134_resume
1334 static int __init
saa7134_init(void)
1336 INIT_LIST_HEAD(&saa7134_devlist
);
1337 printk(KERN_INFO
"saa7130/34: v4l2 driver version %s loaded\n",
1339 return pci_register_driver(&saa7134_pci_driver
);
1342 static void __exit
saa7134_fini(void)
1344 pci_unregister_driver(&saa7134_pci_driver
);
1347 module_init(saa7134_init
);
1348 module_exit(saa7134_fini
);
1350 /* ----------------------------------------------------------- */
1352 EXPORT_SYMBOL(saa7134_set_gpio
);
1353 EXPORT_SYMBOL(saa7134_boards
);
1355 /* ----------------- for the DMA sound modules --------------- */
1357 EXPORT_SYMBOL(saa7134_dmasound_init
);
1358 EXPORT_SYMBOL(saa7134_dmasound_exit
);
1359 EXPORT_SYMBOL(saa7134_pgtable_free
);
1360 EXPORT_SYMBOL(saa7134_pgtable_build
);
1361 EXPORT_SYMBOL(saa7134_pgtable_alloc
);
1362 EXPORT_SYMBOL(saa7134_set_dmabits
);
1364 /* ----------------------------------------------------------- */