Revert "[PATCH] paravirt: Add startup infrastructure for paravirtualization"
[pv_ops_mirror.git] / drivers / media / video / saa7134 / saa7134-core.c
blob25f84701a8e8f8a473ff617cb7dc898f265f2302
1 /*
3 * device driver for philips saa7134 based TV cards
4 * driver core
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/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/kmod.h>
30 #include <linux/sound.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/mutex.h>
34 #include <linux/dma-mapping.h>
36 #include "saa7134-reg.h"
37 #include "saa7134.h"
39 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
40 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
41 MODULE_LICENSE("GPL");
43 /* ------------------------------------------------------------------ */
45 static unsigned int irq_debug = 0;
46 module_param(irq_debug, int, 0644);
47 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
49 static unsigned int core_debug = 0;
50 module_param(core_debug, int, 0644);
51 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
53 static unsigned int gpio_tracking = 0;
54 module_param(gpio_tracking, int, 0644);
55 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
57 static unsigned int alsa = 0;
58 module_param(alsa, int, 0644);
59 MODULE_PARM_DESC(alsa,"enable ALSA DMA sound [dmasound]");
61 static unsigned int oss = 0;
62 module_param(oss, int, 0644);
63 MODULE_PARM_DESC(oss,"enable OSS DMA sound [dmasound]");
65 static unsigned int latency = UNSET;
66 module_param(latency, int, 0444);
67 MODULE_PARM_DESC(latency,"pci latency timer");
69 int saa7134_no_overlay=-1;
70 module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
71 MODULE_PARM_DESC(no_overlay,"allow override overlay default (0 disables, 1 enables)"
72 " [some VIA/SIS chipsets are known to have problem with overlay]");
74 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
75 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
76 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
77 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
78 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
81 module_param_array(video_nr, int, NULL, 0444);
82 module_param_array(vbi_nr, int, NULL, 0444);
83 module_param_array(radio_nr, int, NULL, 0444);
84 module_param_array(tuner, int, NULL, 0444);
85 module_param_array(card, int, NULL, 0444);
87 MODULE_PARM_DESC(video_nr, "video device number");
88 MODULE_PARM_DESC(vbi_nr, "vbi device number");
89 MODULE_PARM_DESC(radio_nr, "radio device number");
90 MODULE_PARM_DESC(tuner, "tuner type");
91 MODULE_PARM_DESC(card, "card type");
93 static DEFINE_MUTEX(devlist_lock);
94 LIST_HEAD(saa7134_devlist);
95 static LIST_HEAD(mops_list);
96 static unsigned int saa7134_devcount;
98 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
99 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
101 #define dprintk(fmt, arg...) if (core_debug) \
102 printk(KERN_DEBUG "%s/core: " fmt, dev->name , ## arg)
104 void saa7134_track_gpio(struct saa7134_dev *dev, char *msg)
106 unsigned long mode,status;
108 if (!gpio_tracking)
109 return;
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;
115 printk(KERN_DEBUG
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)
122 u32 index, bitval;
124 index = 1 << bit_no;
125 switch (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);
131 if (value)
132 bitval = index;
133 else
134 bitval = 0;
135 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
136 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
137 break;
138 case 3: /* tristate */
139 dprintk("setting GPIO%d to tristate\n", bit_no);
140 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
141 break;
145 int saa7134_tuner_callback(void *ptr, int command, int arg)
147 u8 sync_control;
148 struct saa7134_dev *dev = ptr;
150 switch (dev->tuner_type) {
151 case TUNER_PHILIPS_TDA8290:
152 switch (command) {
153 case 0: /* switch LNA gain through GPIO 22*/
154 saa7134_set_gpio(dev, 22, arg) ;
155 break;
156 case 1: /* vsync output at GPIO22. 50 / 60Hz */
157 dprintk("setting GPIO22 to vsync %d\n", arg);
158 saa_andorb(SAA7134_VIDEO_PORT_CTRL3, 0x80, 0x80);
159 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x03);
160 if (arg == 1)
161 sync_control = 11;
162 else
163 sync_control = 17;
164 saa_writeb(SAA7134_VGATE_START, sync_control);
165 saa_writeb(SAA7134_VGATE_STOP, sync_control + 1);
166 saa_andorb(SAA7134_MISC_VGATE_MSB, 0x03, 0x00);
167 break;
168 default:
169 return -EINVAL;
171 break;
172 default:
173 return -ENODEV;
175 return 0;
178 /* ------------------------------------------------------------------ */
181 /* ----------------------------------------------------------- */
182 /* delayed request_module */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
187 static void request_module_async(struct work_struct *work){
188 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
189 if (card_is_empress(dev))
190 request_module("saa7134-empress");
191 if (card_is_dvb(dev))
192 request_module("saa7134-dvb");
193 if (alsa)
194 request_module("saa7134-alsa");
195 if (oss)
196 request_module("saa7134-oss");
199 static void request_submodules(struct saa7134_dev *dev)
201 INIT_WORK(&dev->request_module_wk, request_module_async);
202 schedule_work(&dev->request_module_wk);
205 #else
206 #define request_submodules(dev)
207 #endif /* CONFIG_MODULES */
209 /* ------------------------------------------------------------------ */
211 /* nr of (saa7134-)pages for the given buffer size */
212 static int saa7134_buffer_pages(int size)
214 size = PAGE_ALIGN(size);
215 size += PAGE_SIZE; /* for non-page-aligned buffers */
216 size /= 4096;
217 return size;
220 /* calc max # of buffers from size (must not exceed the 4MB virtual
221 * address space per DMA channel) */
222 int saa7134_buffer_count(unsigned int size, unsigned int count)
224 unsigned int maxcount;
226 maxcount = 1024 / saa7134_buffer_pages(size);
227 if (count > maxcount)
228 count = maxcount;
229 return count;
232 int saa7134_buffer_startpage(struct saa7134_buf *buf)
234 return saa7134_buffer_pages(buf->vb.bsize) * buf->vb.i;
237 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
239 unsigned long base;
241 base = saa7134_buffer_startpage(buf) * 4096;
242 base += buf->vb.dma.sglist[0].offset;
243 return base;
246 /* ------------------------------------------------------------------ */
248 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
250 __le32 *cpu;
251 dma_addr_t dma_addr;
253 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
254 if (NULL == cpu)
255 return -ENOMEM;
256 pt->size = SAA7134_PGTABLE_SIZE;
257 pt->cpu = cpu;
258 pt->dma = dma_addr;
259 return 0;
262 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
263 struct scatterlist *list, unsigned int length,
264 unsigned int startpage)
266 __le32 *ptr;
267 unsigned int i,p;
269 BUG_ON(NULL == pt || NULL == pt->cpu);
271 ptr = pt->cpu + startpage;
272 for (i = 0; i < length; i++, list++)
273 for (p = 0; p * 4096 < list->length; p++, ptr++)
274 *ptr = cpu_to_le32(sg_dma_address(list) - list->offset);
275 return 0;
278 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
280 if (NULL == pt->cpu)
281 return;
282 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
283 pt->cpu = NULL;
286 /* ------------------------------------------------------------------ */
288 void saa7134_dma_free(struct videobuf_queue *q,struct saa7134_buf *buf)
290 BUG_ON(in_interrupt());
292 videobuf_waiton(&buf->vb,0,0);
293 videobuf_dma_unmap(q, &buf->vb.dma);
294 videobuf_dma_free(&buf->vb.dma);
295 buf->vb.state = STATE_NEEDS_INIT;
298 /* ------------------------------------------------------------------ */
300 int saa7134_buffer_queue(struct saa7134_dev *dev,
301 struct saa7134_dmaqueue *q,
302 struct saa7134_buf *buf)
304 struct saa7134_buf *next = NULL;
306 assert_spin_locked(&dev->slock);
307 dprintk("buffer_queue %p\n",buf);
308 if (NULL == q->curr) {
309 if (!q->need_two) {
310 q->curr = buf;
311 buf->activate(dev,buf,NULL);
312 } else if (list_empty(&q->queue)) {
313 list_add_tail(&buf->vb.queue,&q->queue);
314 buf->vb.state = STATE_QUEUED;
315 } else {
316 next = list_entry(q->queue.next,struct saa7134_buf,
317 vb.queue);
318 q->curr = buf;
319 buf->activate(dev,buf,next);
321 } else {
322 list_add_tail(&buf->vb.queue,&q->queue);
323 buf->vb.state = STATE_QUEUED;
325 return 0;
328 void saa7134_buffer_finish(struct saa7134_dev *dev,
329 struct saa7134_dmaqueue *q,
330 unsigned int state)
332 assert_spin_locked(&dev->slock);
333 dprintk("buffer_finish %p\n",q->curr);
335 /* finish current buffer */
336 q->curr->vb.state = state;
337 do_gettimeofday(&q->curr->vb.ts);
338 wake_up(&q->curr->vb.done);
339 q->curr = NULL;
342 void saa7134_buffer_next(struct saa7134_dev *dev,
343 struct saa7134_dmaqueue *q)
345 struct saa7134_buf *buf,*next = NULL;
347 assert_spin_locked(&dev->slock);
348 BUG_ON(NULL != q->curr);
350 if (!list_empty(&q->queue)) {
351 /* activate next one from queue */
352 buf = list_entry(q->queue.next,struct saa7134_buf,vb.queue);
353 dprintk("buffer_next %p [prev=%p/next=%p]\n",
354 buf,q->queue.prev,q->queue.next);
355 list_del(&buf->vb.queue);
356 if (!list_empty(&q->queue))
357 next = list_entry(q->queue.next,struct saa7134_buf,
358 vb.queue);
359 q->curr = buf;
360 buf->activate(dev,buf,next);
361 dprintk("buffer_next #2 prev=%p/next=%p\n",
362 q->queue.prev,q->queue.next);
363 } else {
364 /* nothing to do -- just stop DMA */
365 dprintk("buffer_next %p\n",NULL);
366 saa7134_set_dmabits(dev);
367 del_timer(&q->timeout);
371 void saa7134_buffer_timeout(unsigned long data)
373 struct saa7134_dmaqueue *q = (struct saa7134_dmaqueue*)data;
374 struct saa7134_dev *dev = q->dev;
375 unsigned long flags;
377 spin_lock_irqsave(&dev->slock,flags);
379 /* try to reset the hardware (SWRST) */
380 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
381 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
382 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
384 /* flag current buffer as failed,
385 try to start over with the next one. */
386 if (q->curr) {
387 dprintk("timeout on %p\n",q->curr);
388 saa7134_buffer_finish(dev,q,STATE_ERROR);
390 saa7134_buffer_next(dev,q);
391 spin_unlock_irqrestore(&dev->slock,flags);
394 /* ------------------------------------------------------------------ */
396 int saa7134_set_dmabits(struct saa7134_dev *dev)
398 u32 split, task=0, ctrl=0, irq=0;
399 enum v4l2_field cap = V4L2_FIELD_ANY;
400 enum v4l2_field ov = V4L2_FIELD_ANY;
402 assert_spin_locked(&dev->slock);
404 /* video capture -- dma 0 + video task A */
405 if (dev->video_q.curr) {
406 task |= 0x01;
407 ctrl |= SAA7134_MAIN_CTRL_TE0;
408 irq |= SAA7134_IRQ1_INTE_RA0_1 |
409 SAA7134_IRQ1_INTE_RA0_0;
410 cap = dev->video_q.curr->vb.field;
413 /* video capture -- dma 1+2 (planar modes) */
414 if (dev->video_q.curr &&
415 dev->video_q.curr->fmt->planar) {
416 ctrl |= SAA7134_MAIN_CTRL_TE4 |
417 SAA7134_MAIN_CTRL_TE5;
420 /* screen overlay -- dma 0 + video task B */
421 if (dev->ovenable) {
422 task |= 0x10;
423 ctrl |= SAA7134_MAIN_CTRL_TE1;
424 ov = dev->ovfield;
427 /* vbi capture -- dma 0 + vbi task A+B */
428 if (dev->vbi_q.curr) {
429 task |= 0x22;
430 ctrl |= SAA7134_MAIN_CTRL_TE2 |
431 SAA7134_MAIN_CTRL_TE3;
432 irq |= SAA7134_IRQ1_INTE_RA0_7 |
433 SAA7134_IRQ1_INTE_RA0_6 |
434 SAA7134_IRQ1_INTE_RA0_5 |
435 SAA7134_IRQ1_INTE_RA0_4;
438 /* audio capture -- dma 3 */
439 if (dev->dmasound.dma_running) {
440 ctrl |= SAA7134_MAIN_CTRL_TE6;
441 irq |= SAA7134_IRQ1_INTE_RA3_1 |
442 SAA7134_IRQ1_INTE_RA3_0;
445 /* TS capture -- dma 5 */
446 if (dev->ts_q.curr) {
447 ctrl |= SAA7134_MAIN_CTRL_TE5;
448 irq |= SAA7134_IRQ1_INTE_RA2_3 |
449 SAA7134_IRQ1_INTE_RA2_2 |
450 SAA7134_IRQ1_INTE_RA2_1 |
451 SAA7134_IRQ1_INTE_RA2_0;
454 /* set task conditions + field handling */
455 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
456 /* default config -- use full frames */
457 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
458 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
459 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
460 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
461 split = 0;
462 } else {
463 /* split fields between tasks */
464 if (V4L2_FIELD_TOP == cap) {
465 /* odd A, even B, repeat */
466 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
467 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
468 } else {
469 /* odd B, even A, repeat */
470 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
471 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
473 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
474 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
475 split = 1;
478 /* irqs */
479 saa_writeb(SAA7134_REGION_ENABLE, task);
480 saa_writel(SAA7134_IRQ1, irq);
481 saa_andorl(SAA7134_MAIN_CTRL,
482 SAA7134_MAIN_CTRL_TE0 |
483 SAA7134_MAIN_CTRL_TE1 |
484 SAA7134_MAIN_CTRL_TE2 |
485 SAA7134_MAIN_CTRL_TE3 |
486 SAA7134_MAIN_CTRL_TE4 |
487 SAA7134_MAIN_CTRL_TE5 |
488 SAA7134_MAIN_CTRL_TE6,
489 ctrl);
490 dprintk("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
491 task, ctrl, irq, split ? "no" : "yes");
493 return 0;
496 /* ------------------------------------------------------------------ */
497 /* IRQ handler + helpers */
499 static char *irqbits[] = {
500 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
501 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
502 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
503 "GPIO16?", "GPIO18", "GPIO22", "GPIO23"
505 #define IRQBITS ARRAY_SIZE(irqbits)
507 static void print_irqstatus(struct saa7134_dev *dev, int loop,
508 unsigned long report, unsigned long status)
510 unsigned int i;
512 printk(KERN_DEBUG "%s/irq[%d,%ld]: r=0x%lx s=0x%02lx",
513 dev->name,loop,jiffies,report,status);
514 for (i = 0; i < IRQBITS; i++) {
515 if (!(report & (1 << i)))
516 continue;
517 printk(" %s",irqbits[i]);
519 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
520 printk(" | RA0=%s,%s,%s,%ld",
521 (status & 0x40) ? "vbi" : "video",
522 (status & 0x20) ? "b" : "a",
523 (status & 0x10) ? "odd" : "even",
524 (status & 0x0f));
526 printk("\n");
529 static irqreturn_t saa7134_irq(int irq, void *dev_id)
531 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
532 unsigned long report,status;
533 int loop, handled = 0;
535 for (loop = 0; loop < 10; loop++) {
536 report = saa_readl(SAA7134_IRQ_REPORT);
537 status = saa_readl(SAA7134_IRQ_STATUS);
538 if (0 == report) {
539 if (irq_debug > 1)
540 printk(KERN_DEBUG "%s/irq: no (more) work\n",
541 dev->name);
542 goto out;
545 /* If dmasound support is active and we get a sound report, exit
546 and let the saa7134-alsa/oss module deal with it */
548 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
549 (dev->dmasound.priv_data != NULL) )
551 if (irq_debug > 1)
552 printk(KERN_DEBUG "%s/irq: ignoring interrupt for DMA sound\n",
553 dev->name);
554 goto out;
557 handled = 1;
558 saa_writel(SAA7134_IRQ_REPORT,report);
559 if (irq_debug)
560 print_irqstatus(dev,loop,report,status);
563 if (report & SAA7134_IRQ_REPORT_RDCAP /* _INTL */)
564 saa7134_irq_video_intl(dev);
566 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
567 (status & 0x60) == 0)
568 saa7134_irq_video_done(dev,status);
570 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
571 (status & 0x40) == 0x40)
572 saa7134_irq_vbi_done(dev,status);
574 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
575 card_has_mpeg(dev))
576 saa7134_irq_ts_done(dev,status);
578 if (report & SAA7134_IRQ_REPORT_GPIO16) {
579 switch (dev->has_remote) {
580 case SAA7134_REMOTE_GPIO:
581 if (!dev->remote)
582 break;
583 if (dev->remote->mask_keydown & 0x10000) {
584 saa7134_input_irq(dev);
586 break;
588 case SAA7134_REMOTE_I2C:
589 break; /* FIXME: invoke I2C get_key() */
591 default: /* GPIO16 not used by IR remote */
592 break;
596 if (report & SAA7134_IRQ_REPORT_GPIO18) {
597 switch (dev->has_remote) {
598 case SAA7134_REMOTE_GPIO:
599 if (!dev->remote)
600 break;
601 if ((dev->remote->mask_keydown & 0x40000) ||
602 (dev->remote->mask_keyup & 0x40000)) {
603 saa7134_input_irq(dev);
605 break;
607 case SAA7134_REMOTE_I2C:
608 break; /* FIXME: invoke I2C get_key() */
610 default: /* GPIO18 not used by IR remote */
611 break;
616 if (10 == loop) {
617 print_irqstatus(dev,loop,report,status);
618 if (report & SAA7134_IRQ_REPORT_PE) {
619 /* disable all parity error */
620 printk(KERN_WARNING "%s/irq: looping -- "
621 "clearing PE (parity error!) enable bit\n",dev->name);
622 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
623 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
624 /* disable gpio16 IRQ */
625 printk(KERN_WARNING "%s/irq: looping -- "
626 "clearing GPIO16 enable bit\n",dev->name);
627 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16);
628 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
629 /* disable gpio18 IRQs */
630 printk(KERN_WARNING "%s/irq: looping -- "
631 "clearing GPIO18 enable bit\n",dev->name);
632 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18);
633 } else {
634 /* disable all irqs */
635 printk(KERN_WARNING "%s/irq: looping -- "
636 "clearing all enable bits\n",dev->name);
637 saa_writel(SAA7134_IRQ1,0);
638 saa_writel(SAA7134_IRQ2,0);
642 out:
643 return IRQ_RETVAL(handled);
646 /* ------------------------------------------------------------------ */
648 /* early init (no i2c, no irq) */
649 static int saa7134_hwinit1(struct saa7134_dev *dev)
651 dprintk("hwinit1\n");
653 saa_writel(SAA7134_IRQ1, 0);
654 saa_writel(SAA7134_IRQ2, 0);
655 mutex_init(&dev->lock);
656 spin_lock_init(&dev->slock);
658 saa7134_track_gpio(dev,"pre-init");
659 saa7134_video_init1(dev);
660 saa7134_vbi_init1(dev);
661 if (card_has_mpeg(dev))
662 saa7134_ts_init1(dev);
663 saa7134_input_init1(dev);
665 /* RAM FIFO config */
666 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
667 saa_writel(SAA7134_THRESHOULD,0x02020202);
669 /* enable audio + video processing */
670 saa_writel(SAA7134_MAIN_CTRL,
671 SAA7134_MAIN_CTRL_VPLLE |
672 SAA7134_MAIN_CTRL_APLLE |
673 SAA7134_MAIN_CTRL_EXOSC |
674 SAA7134_MAIN_CTRL_EVFE1 |
675 SAA7134_MAIN_CTRL_EVFE2 |
676 SAA7134_MAIN_CTRL_ESFE |
677 SAA7134_MAIN_CTRL_EBDAC);
680 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
681 * OSS initialization writes to registers via the audio DSP; these
682 * writes will fail unless the audio clock has been started. At worst,
683 * audio will not work.
686 /* enable peripheral devices */
687 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
689 /* set vertical line numbering start (vbi needs this) */
690 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
692 return 0;
695 /* late init (with i2c + irq) */
696 static int saa7134_hwinit2(struct saa7134_dev *dev)
698 unsigned int irq2_mask;
699 dprintk("hwinit2\n");
701 saa7134_video_init2(dev);
702 saa7134_tvaudio_init2(dev);
704 /* enable IRQ's */
705 irq2_mask =
706 SAA7134_IRQ2_INTE_DEC3 |
707 SAA7134_IRQ2_INTE_DEC2 |
708 SAA7134_IRQ2_INTE_DEC1 |
709 SAA7134_IRQ2_INTE_DEC0 |
710 SAA7134_IRQ2_INTE_PE |
711 SAA7134_IRQ2_INTE_AR;
713 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
714 if (dev->remote->mask_keydown & 0x10000)
715 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16;
716 else if (dev->remote->mask_keydown & 0x40000)
717 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18;
718 else if (dev->remote->mask_keyup & 0x40000)
719 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18A;
722 saa_writel(SAA7134_IRQ1, 0);
723 saa_writel(SAA7134_IRQ2, irq2_mask);
725 return 0;
728 /* shutdown */
729 static int saa7134_hwfini(struct saa7134_dev *dev)
731 dprintk("hwfini\n");
733 if (card_has_mpeg(dev))
734 saa7134_ts_fini(dev);
735 saa7134_input_fini(dev);
736 saa7134_vbi_fini(dev);
737 saa7134_tvaudio_fini(dev);
738 return 0;
741 static void __devinit must_configure_manually(void)
743 unsigned int i,p;
745 printk(KERN_WARNING
746 "saa7134: <rant>\n"
747 "saa7134: Congratulations! Your TV card vendor saved a few\n"
748 "saa7134: cents for a eeprom, thus your pci board has no\n"
749 "saa7134: subsystem ID and I can't identify it automatically\n"
750 "saa7134: </rant>\n"
751 "saa7134: I feel better now. Ok, here are the good news:\n"
752 "saa7134: You can use the card=<nr> insmod option to specify\n"
753 "saa7134: which board do you have. The list:\n");
754 for (i = 0; i < saa7134_bcount; i++) {
755 printk(KERN_WARNING "saa7134: card=%d -> %-40.40s",
756 i,saa7134_boards[i].name);
757 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
758 if (saa7134_pci_tbl[p].driver_data != i)
759 continue;
760 printk(" %04x:%04x",
761 saa7134_pci_tbl[p].subvendor,
762 saa7134_pci_tbl[p].subdevice);
764 printk("\n");
768 static struct video_device *vdev_init(struct saa7134_dev *dev,
769 struct video_device *template,
770 char *type)
772 struct video_device *vfd;
774 vfd = video_device_alloc();
775 if (NULL == vfd)
776 return NULL;
777 *vfd = *template;
778 vfd->minor = -1;
779 vfd->dev = &dev->pci->dev;
780 vfd->release = video_device_release;
781 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
782 dev->name, type, saa7134_boards[dev->board].name);
783 return vfd;
786 static void saa7134_unregister_video(struct saa7134_dev *dev)
788 if (dev->video_dev) {
789 if (-1 != dev->video_dev->minor)
790 video_unregister_device(dev->video_dev);
791 else
792 video_device_release(dev->video_dev);
793 dev->video_dev = NULL;
795 if (dev->vbi_dev) {
796 if (-1 != dev->vbi_dev->minor)
797 video_unregister_device(dev->vbi_dev);
798 else
799 video_device_release(dev->vbi_dev);
800 dev->vbi_dev = NULL;
802 if (dev->radio_dev) {
803 if (-1 != dev->radio_dev->minor)
804 video_unregister_device(dev->radio_dev);
805 else
806 video_device_release(dev->radio_dev);
807 dev->radio_dev = NULL;
811 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
812 struct saa7134_dev *dev)
814 int err;
816 if (NULL != dev->mops)
817 return;
818 if (saa7134_boards[dev->board].mpeg != ops->type)
819 return;
820 err = ops->init(dev);
821 if (0 != err)
822 return;
823 dev->mops = ops;
826 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
827 struct saa7134_dev *dev)
829 if (NULL == dev->mops)
830 return;
831 if (dev->mops != ops)
832 return;
833 dev->mops->fini(dev);
834 dev->mops = NULL;
837 static int __devinit saa7134_initdev(struct pci_dev *pci_dev,
838 const struct pci_device_id *pci_id)
840 struct saa7134_dev *dev;
841 struct list_head *item;
842 struct saa7134_mpeg_ops *mops;
843 int err;
845 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
846 if (NULL == dev)
847 return -ENOMEM;
849 /* pci init */
850 dev->pci = pci_dev;
851 if (pci_enable_device(pci_dev)) {
852 err = -EIO;
853 goto fail1;
856 dev->nr = saa7134_devcount;
857 sprintf(dev->name,"saa%x[%d]",pci_dev->device,dev->nr);
859 /* pci quirks */
860 if (pci_pci_problems) {
861 if (pci_pci_problems & PCIPCI_TRITON)
862 printk(KERN_INFO "%s: quirk: PCIPCI_TRITON\n", dev->name);
863 if (pci_pci_problems & PCIPCI_NATOMA)
864 printk(KERN_INFO "%s: quirk: PCIPCI_NATOMA\n", dev->name);
865 if (pci_pci_problems & PCIPCI_VIAETBF)
866 printk(KERN_INFO "%s: quirk: PCIPCI_VIAETBF\n", dev->name);
867 if (pci_pci_problems & PCIPCI_VSFX)
868 printk(KERN_INFO "%s: quirk: PCIPCI_VSFX\n",dev->name);
869 #ifdef PCIPCI_ALIMAGIK
870 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
871 printk(KERN_INFO "%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
872 dev->name);
873 latency = 0x0A;
875 #endif
876 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
877 printk(KERN_INFO "%s: quirk: this driver and your "
878 "chipset may not work together"
879 " in overlay mode.\n",dev->name);
880 if (!saa7134_no_overlay) {
881 printk(KERN_INFO "%s: quirk: overlay "
882 "mode will be disabled.\n",
883 dev->name);
884 saa7134_no_overlay = 1;
885 } else {
886 printk(KERN_INFO "%s: quirk: overlay "
887 "mode will be forced. Use this"
888 " option at your own risk.\n",
889 dev->name);
893 if (UNSET != latency) {
894 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
895 dev->name,latency);
896 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
899 /* print pci info */
900 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
901 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
902 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
903 "latency: %d, mmio: 0x%llx\n", dev->name,
904 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
905 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
906 pci_set_master(pci_dev);
907 if (!pci_dma_supported(pci_dev, DMA_32BIT_MASK)) {
908 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
909 err = -EIO;
910 goto fail1;
913 /* board config */
914 dev->board = pci_id->driver_data;
915 if (card[dev->nr] >= 0 &&
916 card[dev->nr] < saa7134_bcount)
917 dev->board = card[dev->nr];
918 if (SAA7134_BOARD_NOAUTO == dev->board) {
919 must_configure_manually();
920 dev->board = SAA7134_BOARD_UNKNOWN;
922 dev->autodetected = card[dev->nr] != dev->board;
923 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
924 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
925 if (UNSET != tuner[dev->nr])
926 dev->tuner_type = tuner[dev->nr];
927 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
928 dev->name,pci_dev->subsystem_vendor,
929 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
930 dev->board, dev->autodetected ?
931 "autodetected" : "insmod option");
933 /* get mmio */
934 if (!request_mem_region(pci_resource_start(pci_dev,0),
935 pci_resource_len(pci_dev,0),
936 dev->name)) {
937 err = -EBUSY;
938 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%llx\n",
939 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
940 goto fail1;
942 dev->lmmio = ioremap(pci_resource_start(pci_dev,0), 0x1000);
943 dev->bmmio = (__u8 __iomem *)dev->lmmio;
944 if (NULL == dev->lmmio) {
945 err = -EIO;
946 printk(KERN_ERR "%s: can't ioremap() MMIO memory\n",
947 dev->name);
948 goto fail2;
951 /* initialize hardware #1 */
952 saa7134_board_init1(dev);
953 saa7134_hwinit1(dev);
955 /* get irq */
956 err = request_irq(pci_dev->irq, saa7134_irq,
957 IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
958 if (err < 0) {
959 printk(KERN_ERR "%s: can't get IRQ %d\n",
960 dev->name,pci_dev->irq);
961 goto fail3;
964 /* wait a bit, register i2c bus */
965 msleep(100);
966 saa7134_i2c_register(dev);
968 /* initialize hardware #2 */
969 saa7134_board_init2(dev);
970 saa7134_hwinit2(dev);
972 /* load i2c helpers */
973 if (TUNER_ABSENT != dev->tuner_type)
974 request_module("tuner");
975 if (card_is_empress(dev)) {
976 request_module("saa6752hs");
979 request_submodules(dev);
981 v4l2_prio_init(&dev->prio);
983 /* register v4l devices */
984 if (saa7134_no_overlay <= 0) {
985 saa7134_video_template.type |= VID_TYPE_OVERLAY;
986 } else {
987 printk("%s: Overlay support disabled.\n",dev->name);
989 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
990 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
991 video_nr[dev->nr]);
992 if (err < 0) {
993 printk(KERN_INFO "%s: can't register video device\n",
994 dev->name);
995 goto fail4;
997 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
998 dev->name,dev->video_dev->minor & 0x1f);
1000 dev->vbi_dev = vdev_init(dev,&saa7134_vbi_template,"vbi");
1001 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1002 vbi_nr[dev->nr]);
1003 if (err < 0)
1004 goto fail4;
1005 printk(KERN_INFO "%s: registered device vbi%d\n",
1006 dev->name,dev->vbi_dev->minor & 0x1f);
1008 if (card_has_radio(dev)) {
1009 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1010 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1011 radio_nr[dev->nr]);
1012 if (err < 0)
1013 goto fail4;
1014 printk(KERN_INFO "%s: registered device radio%d\n",
1015 dev->name,dev->radio_dev->minor & 0x1f);
1018 /* everything worked */
1019 pci_set_drvdata(pci_dev,dev);
1020 saa7134_devcount++;
1022 mutex_lock(&devlist_lock);
1023 list_for_each(item,&mops_list) {
1024 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1025 mpeg_ops_attach(mops, dev);
1027 list_add_tail(&dev->devlist,&saa7134_devlist);
1028 mutex_unlock(&devlist_lock);
1030 /* check for signal */
1031 saa7134_irq_video_intl(dev);
1033 if (saa7134_dmasound_init && !dev->dmasound.priv_data) {
1034 saa7134_dmasound_init(dev);
1037 if (TUNER_ABSENT != dev->tuner_type)
1038 saa7134_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
1040 return 0;
1042 fail4:
1043 saa7134_unregister_video(dev);
1044 saa7134_i2c_unregister(dev);
1045 free_irq(pci_dev->irq, dev);
1046 fail3:
1047 saa7134_hwfini(dev);
1048 iounmap(dev->lmmio);
1049 fail2:
1050 release_mem_region(pci_resource_start(pci_dev,0),
1051 pci_resource_len(pci_dev,0));
1052 fail1:
1053 kfree(dev);
1054 return err;
1057 static void __devexit saa7134_finidev(struct pci_dev *pci_dev)
1059 struct saa7134_dev *dev = pci_get_drvdata(pci_dev);
1060 struct list_head *item;
1061 struct saa7134_mpeg_ops *mops;
1063 /* Release DMA sound modules if present */
1064 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1065 saa7134_dmasound_exit(dev);
1068 /* debugging ... */
1069 if (irq_debug) {
1070 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1071 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1072 print_irqstatus(dev,42,report,status);
1075 /* disable peripheral devices */
1076 saa_writeb(SAA7134_SPECIAL_MODE,0);
1078 /* shutdown hardware */
1079 saa_writel(SAA7134_IRQ1,0);
1080 saa_writel(SAA7134_IRQ2,0);
1081 saa_writel(SAA7134_MAIN_CTRL,0);
1083 /* shutdown subsystems */
1084 saa7134_hwfini(dev);
1086 /* unregister */
1087 mutex_lock(&devlist_lock);
1088 list_del(&dev->devlist);
1089 list_for_each(item,&mops_list) {
1090 mops = list_entry(item, struct saa7134_mpeg_ops, next);
1091 mpeg_ops_detach(mops, dev);
1093 mutex_unlock(&devlist_lock);
1094 saa7134_devcount--;
1096 saa7134_i2c_unregister(dev);
1097 saa7134_unregister_video(dev);
1100 /* the DMA sound modules should be unloaded before reaching
1101 this, but just in case they are still present... */
1102 if (dev->dmasound.priv_data != NULL) {
1103 free_irq(pci_dev->irq, &dev->dmasound);
1104 dev->dmasound.priv_data = NULL;
1108 /* release resources */
1109 free_irq(pci_dev->irq, dev);
1110 iounmap(dev->lmmio);
1111 release_mem_region(pci_resource_start(pci_dev,0),
1112 pci_resource_len(pci_dev,0));
1114 pci_set_drvdata(pci_dev, NULL);
1116 /* free memory */
1117 kfree(dev);
1120 /* ----------------------------------------------------------- */
1122 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1124 struct list_head *item;
1125 struct saa7134_dev *dev;
1127 mutex_lock(&devlist_lock);
1128 list_for_each(item,&saa7134_devlist) {
1129 dev = list_entry(item, struct saa7134_dev, devlist);
1130 mpeg_ops_attach(ops, dev);
1132 list_add_tail(&ops->next,&mops_list);
1133 mutex_unlock(&devlist_lock);
1134 return 0;
1137 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1139 struct list_head *item;
1140 struct saa7134_dev *dev;
1142 mutex_lock(&devlist_lock);
1143 list_del(&ops->next);
1144 list_for_each(item,&saa7134_devlist) {
1145 dev = list_entry(item, struct saa7134_dev, devlist);
1146 mpeg_ops_detach(ops, dev);
1148 mutex_unlock(&devlist_lock);
1151 EXPORT_SYMBOL(saa7134_ts_register);
1152 EXPORT_SYMBOL(saa7134_ts_unregister);
1154 /* ----------------------------------------------------------- */
1156 static struct pci_driver saa7134_pci_driver = {
1157 .name = "saa7134",
1158 .id_table = saa7134_pci_tbl,
1159 .probe = saa7134_initdev,
1160 .remove = __devexit_p(saa7134_finidev),
1163 static int saa7134_init(void)
1165 INIT_LIST_HEAD(&saa7134_devlist);
1166 printk(KERN_INFO "saa7130/34: v4l2 driver version %d.%d.%d loaded\n",
1167 (SAA7134_VERSION_CODE >> 16) & 0xff,
1168 (SAA7134_VERSION_CODE >> 8) & 0xff,
1169 SAA7134_VERSION_CODE & 0xff);
1170 #ifdef SNAPSHOT
1171 printk(KERN_INFO "saa7130/34: snapshot date %04d-%02d-%02d\n",
1172 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
1173 #endif
1174 return pci_register_driver(&saa7134_pci_driver);
1177 static void saa7134_fini(void)
1179 pci_unregister_driver(&saa7134_pci_driver);
1182 module_init(saa7134_init);
1183 module_exit(saa7134_fini);
1185 /* ----------------------------------------------------------- */
1187 EXPORT_SYMBOL(saa7134_set_gpio);
1188 EXPORT_SYMBOL(saa7134_i2c_call_clients);
1189 EXPORT_SYMBOL(saa7134_devlist);
1190 EXPORT_SYMBOL(saa7134_boards);
1192 /* ----------------- for the DMA sound modules --------------- */
1194 EXPORT_SYMBOL(saa7134_dmasound_init);
1195 EXPORT_SYMBOL(saa7134_dmasound_exit);
1196 EXPORT_SYMBOL(saa7134_pgtable_free);
1197 EXPORT_SYMBOL(saa7134_pgtable_build);
1198 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1199 EXPORT_SYMBOL(saa7134_set_dmabits);
1201 /* ----------------------------------------------------------- */
1203 * Local variables:
1204 * c-basic-offset: 8
1205 * End: