Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / saa7134 / saa7134-core.c
blob9e76de2411ae6c77ac8b5c20097b05c9e12f7532
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.
19 #include "saa7134.h"
20 #include "saa7134-reg.h"
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/kmod.h>
28 #include <linux/sound.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/mutex.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/pm.h>
35 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION(SAA7134_VERSION);
41 /* ------------------------------------------------------------------ */
43 static unsigned int irq_debug;
44 module_param(irq_debug, int, 0644);
45 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
47 static unsigned int core_debug;
48 module_param(core_debug, int, 0644);
49 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
51 static unsigned int gpio_tracking;
52 module_param(gpio_tracking, int, 0644);
53 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
55 static unsigned int alsa = 1;
56 module_param(alsa, int, 0644);
57 MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
59 static unsigned int latency = UNSET;
60 module_param(latency, int, 0444);
61 MODULE_PARM_DESC(latency,"pci latency timer");
63 int saa7134_no_overlay=-1;
64 module_param_named(no_overlay, saa7134_no_overlay, int, 0444);
65 MODULE_PARM_DESC(no_overlay, "allow override overlay default (0 disables, 1 enables) [some VIA/SIS chipsets are known to have problem with overlay]");
67 bool saa7134_userptr;
68 module_param(saa7134_userptr, bool, 0644);
69 MODULE_PARM_DESC(saa7134_userptr, "enable page-aligned userptr support");
71 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
72 static unsigned int vbi_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
73 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
74 static unsigned int tuner[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
75 static unsigned int card[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
78 module_param_array(video_nr, int, NULL, 0444);
79 module_param_array(vbi_nr, int, NULL, 0444);
80 module_param_array(radio_nr, int, NULL, 0444);
81 module_param_array(tuner, int, NULL, 0444);
82 module_param_array(card, int, NULL, 0444);
84 MODULE_PARM_DESC(video_nr, "video device number");
85 MODULE_PARM_DESC(vbi_nr, "vbi device number");
86 MODULE_PARM_DESC(radio_nr, "radio device number");
87 MODULE_PARM_DESC(tuner, "tuner type");
88 MODULE_PARM_DESC(card, "card type");
90 DEFINE_MUTEX(saa7134_devlist_lock);
91 EXPORT_SYMBOL(saa7134_devlist_lock);
92 LIST_HEAD(saa7134_devlist);
93 EXPORT_SYMBOL(saa7134_devlist);
94 static LIST_HEAD(mops_list);
95 static unsigned int saa7134_devcount;
97 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
98 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
100 #define core_dbg(fmt, arg...) do { \
101 if (core_debug) \
102 printk(KERN_DEBUG pr_fmt("core: " fmt), ## arg); \
103 } while (0)
105 #define irq_dbg(level, fmt, arg...) do {\
106 if (irq_debug > level) \
107 printk(KERN_DEBUG pr_fmt("irq: " fmt), ## arg); \
108 } while (0)
110 void saa7134_track_gpio(struct saa7134_dev *dev, const char *msg)
112 unsigned long mode,status;
114 if (!gpio_tracking)
115 return;
116 /* rising SAA7134_GPIO_GPRESCAN reads the status */
117 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
118 saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
119 mode = saa_readl(SAA7134_GPIO_GPMODE0 >> 2) & 0xfffffff;
120 status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
121 core_dbg("%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
122 dev->name, mode, (~mode) & status, mode & status, msg);
125 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
127 u32 index, bitval;
129 index = 1 << bit_no;
130 switch (value) {
131 case 0: /* static value */
132 case 1:
133 core_dbg("setting GPIO%d to static %d\n", bit_no, value);
134 /* turn sync mode off if necessary */
135 if (index & 0x00c00000)
136 saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
137 if (value)
138 bitval = index;
139 else
140 bitval = 0;
141 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
142 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
143 break;
144 case 3: /* tristate */
145 core_dbg("setting GPIO%d to tristate\n", bit_no);
146 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
147 break;
151 /* ------------------------------------------------------------------ */
154 /* ----------------------------------------------------------- */
155 /* delayed request_module */
157 #if defined(CONFIG_MODULES) && defined(MODULE)
159 static void request_module_async(struct work_struct *work){
160 struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
161 if (card_is_empress(dev))
162 request_module("saa7134-empress");
163 if (card_is_dvb(dev))
164 request_module("saa7134-dvb");
165 if (card_is_go7007(dev))
166 request_module("saa7134-go7007");
167 if (alsa) {
168 if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
169 request_module("saa7134-alsa");
173 static void request_submodules(struct saa7134_dev *dev)
175 INIT_WORK(&dev->request_module_wk, request_module_async);
176 schedule_work(&dev->request_module_wk);
179 static void flush_request_submodules(struct saa7134_dev *dev)
181 flush_work(&dev->request_module_wk);
184 #else
185 #define request_submodules(dev)
186 #define flush_request_submodules(dev)
187 #endif /* CONFIG_MODULES */
189 /* ------------------------------------------------------------------ */
191 /* nr of (saa7134-)pages for the given buffer size */
192 static int saa7134_buffer_pages(int size)
194 size = PAGE_ALIGN(size);
195 size += PAGE_SIZE; /* for non-page-aligned buffers */
196 size /= 4096;
197 return size;
200 /* calc max # of buffers from size (must not exceed the 4MB virtual
201 * address space per DMA channel) */
202 int saa7134_buffer_count(unsigned int size, unsigned int count)
204 unsigned int maxcount;
206 maxcount = 1024 / saa7134_buffer_pages(size);
207 if (count > maxcount)
208 count = maxcount;
209 return count;
212 int saa7134_buffer_startpage(struct saa7134_buf *buf)
214 return saa7134_buffer_pages(vb2_plane_size(&buf->vb2.vb2_buf, 0))
215 * buf->vb2.vb2_buf.index;
218 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
220 unsigned long base;
221 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2.vb2_buf, 0);
223 base = saa7134_buffer_startpage(buf) * 4096;
224 base += dma->sgl[0].offset;
225 return base;
228 /* ------------------------------------------------------------------ */
230 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
232 __le32 *cpu;
233 dma_addr_t dma_addr = 0;
235 cpu = pci_alloc_consistent(pci, SAA7134_PGTABLE_SIZE, &dma_addr);
236 if (NULL == cpu)
237 return -ENOMEM;
238 pt->size = SAA7134_PGTABLE_SIZE;
239 pt->cpu = cpu;
240 pt->dma = dma_addr;
241 return 0;
244 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
245 struct scatterlist *list, unsigned int length,
246 unsigned int startpage)
248 __le32 *ptr;
249 unsigned int i, p;
251 BUG_ON(NULL == pt || NULL == pt->cpu);
253 ptr = pt->cpu + startpage;
254 for (i = 0; i < length; i++, list = sg_next(list)) {
255 for (p = 0; p * 4096 < list->length; p++, ptr++)
256 *ptr = cpu_to_le32(sg_dma_address(list) +
257 list->offset + p * 4096);
259 return 0;
262 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
264 if (NULL == pt->cpu)
265 return;
266 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
267 pt->cpu = NULL;
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;
277 unsigned long flags;
279 spin_lock_irqsave(&dev->slock, flags);
280 core_dbg("buffer_queue %p\n", buf);
281 if (NULL == q->curr) {
282 if (!q->need_two) {
283 q->curr = buf;
284 buf->activate(dev, buf, NULL);
285 } else if (list_empty(&q->queue)) {
286 list_add_tail(&buf->entry, &q->queue);
287 } else {
288 next = list_entry(q->queue.next, struct saa7134_buf,
289 entry);
290 q->curr = buf;
291 buf->activate(dev, buf, next);
293 } else {
294 list_add_tail(&buf->entry, &q->queue);
296 spin_unlock_irqrestore(&dev->slock, flags);
297 return 0;
300 void saa7134_buffer_finish(struct saa7134_dev *dev,
301 struct saa7134_dmaqueue *q,
302 unsigned int state)
304 core_dbg("buffer_finish %p\n", q->curr);
306 /* finish current buffer */
307 q->curr->vb2.vb2_buf.timestamp = ktime_get_ns();
308 q->curr->vb2.sequence = q->seq_nr++;
309 vb2_buffer_done(&q->curr->vb2.vb2_buf, state);
310 q->curr = NULL;
313 void saa7134_buffer_next(struct saa7134_dev *dev,
314 struct saa7134_dmaqueue *q)
316 struct saa7134_buf *buf,*next = NULL;
318 assert_spin_locked(&dev->slock);
319 BUG_ON(NULL != q->curr);
321 if (!list_empty(&q->queue)) {
322 /* activate next one from queue */
323 buf = list_entry(q->queue.next, struct saa7134_buf, entry);
324 core_dbg("buffer_next %p [prev=%p/next=%p]\n",
325 buf, q->queue.prev, q->queue.next);
326 list_del(&buf->entry);
327 if (!list_empty(&q->queue))
328 next = list_entry(q->queue.next, struct saa7134_buf, entry);
329 q->curr = buf;
330 buf->activate(dev, buf, next);
331 core_dbg("buffer_next #2 prev=%p/next=%p\n",
332 q->queue.prev, q->queue.next);
333 } else {
334 /* nothing to do -- just stop DMA */
335 core_dbg("buffer_next %p\n", NULL);
336 saa7134_set_dmabits(dev);
337 del_timer(&q->timeout);
341 void saa7134_buffer_timeout(struct timer_list *t)
343 struct saa7134_dmaqueue *q = from_timer(q, t, timeout);
344 struct saa7134_dev *dev = q->dev;
345 unsigned long flags;
347 spin_lock_irqsave(&dev->slock, flags);
349 /* try to reset the hardware (SWRST) */
350 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
351 saa_writeb(SAA7134_REGION_ENABLE, 0x80);
352 saa_writeb(SAA7134_REGION_ENABLE, 0x00);
354 /* flag current buffer as failed,
355 try to start over with the next one. */
356 if (q->curr) {
357 core_dbg("timeout on %p\n", q->curr);
358 saa7134_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
360 saa7134_buffer_next(dev, q);
361 spin_unlock_irqrestore(&dev->slock, flags);
364 void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q)
366 unsigned long flags;
367 struct list_head *pos, *n;
368 struct saa7134_buf *tmp;
370 spin_lock_irqsave(&dev->slock, flags);
371 if (!list_empty(&q->queue)) {
372 list_for_each_safe(pos, n, &q->queue) {
373 tmp = list_entry(pos, struct saa7134_buf, entry);
374 vb2_buffer_done(&tmp->vb2.vb2_buf,
375 VB2_BUF_STATE_ERROR);
376 list_del(pos);
377 tmp = NULL;
380 spin_unlock_irqrestore(&dev->slock, flags);
381 saa7134_buffer_timeout(&q->timeout); /* also calls del_timer(&q->timeout) */
383 EXPORT_SYMBOL_GPL(saa7134_stop_streaming);
385 /* ------------------------------------------------------------------ */
387 int saa7134_set_dmabits(struct saa7134_dev *dev)
389 u32 split, task=0, ctrl=0, irq=0;
390 enum v4l2_field cap = V4L2_FIELD_ANY;
391 enum v4l2_field ov = V4L2_FIELD_ANY;
393 assert_spin_locked(&dev->slock);
395 if (dev->insuspend)
396 return 0;
398 /* video capture -- dma 0 + video task A */
399 if (dev->video_q.curr) {
400 task |= 0x01;
401 ctrl |= SAA7134_MAIN_CTRL_TE0;
402 irq |= SAA7134_IRQ1_INTE_RA0_1 |
403 SAA7134_IRQ1_INTE_RA0_0;
404 cap = dev->field;
407 /* video capture -- dma 1+2 (planar modes) */
408 if (dev->video_q.curr && dev->fmt->planar) {
409 ctrl |= SAA7134_MAIN_CTRL_TE4 |
410 SAA7134_MAIN_CTRL_TE5;
413 /* screen overlay -- dma 0 + video task B */
414 if (dev->ovenable) {
415 task |= 0x10;
416 ctrl |= SAA7134_MAIN_CTRL_TE1;
417 ov = dev->ovfield;
420 /* vbi capture -- dma 0 + vbi task A+B */
421 if (dev->vbi_q.curr) {
422 task |= 0x22;
423 ctrl |= SAA7134_MAIN_CTRL_TE2 |
424 SAA7134_MAIN_CTRL_TE3;
425 irq |= SAA7134_IRQ1_INTE_RA0_7 |
426 SAA7134_IRQ1_INTE_RA0_6 |
427 SAA7134_IRQ1_INTE_RA0_5 |
428 SAA7134_IRQ1_INTE_RA0_4;
431 /* audio capture -- dma 3 */
432 if (dev->dmasound.dma_running) {
433 ctrl |= SAA7134_MAIN_CTRL_TE6;
434 irq |= SAA7134_IRQ1_INTE_RA3_1 |
435 SAA7134_IRQ1_INTE_RA3_0;
438 /* TS capture -- dma 5 */
439 if (dev->ts_q.curr) {
440 ctrl |= SAA7134_MAIN_CTRL_TE5;
441 irq |= SAA7134_IRQ1_INTE_RA2_1 |
442 SAA7134_IRQ1_INTE_RA2_0;
445 /* set task conditions + field handling */
446 if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
447 /* default config -- use full frames */
448 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
449 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
450 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x02);
451 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x02);
452 split = 0;
453 } else {
454 /* split fields between tasks */
455 if (V4L2_FIELD_TOP == cap) {
456 /* odd A, even B, repeat */
457 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
458 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
459 } else {
460 /* odd B, even A, repeat */
461 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
462 saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
464 saa_writeb(SAA7134_FIELD_HANDLING(TASK_A), 0x01);
465 saa_writeb(SAA7134_FIELD_HANDLING(TASK_B), 0x01);
466 split = 1;
469 /* irqs */
470 saa_writeb(SAA7134_REGION_ENABLE, task);
471 saa_writel(SAA7134_IRQ1, irq);
472 saa_andorl(SAA7134_MAIN_CTRL,
473 SAA7134_MAIN_CTRL_TE0 |
474 SAA7134_MAIN_CTRL_TE1 |
475 SAA7134_MAIN_CTRL_TE2 |
476 SAA7134_MAIN_CTRL_TE3 |
477 SAA7134_MAIN_CTRL_TE4 |
478 SAA7134_MAIN_CTRL_TE5 |
479 SAA7134_MAIN_CTRL_TE6,
480 ctrl);
481 core_dbg("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
482 task, ctrl, irq, split ? "no" : "yes");
484 return 0;
487 /* ------------------------------------------------------------------ */
488 /* IRQ handler + helpers */
490 static char *irqbits[] = {
491 "DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
492 "AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
493 "TRIG_ERR", "CONF_ERR", "LOAD_ERR",
494 "GPIO16", "GPIO18", "GPIO22", "GPIO23"
496 #define IRQBITS ARRAY_SIZE(irqbits)
498 static void print_irqstatus(struct saa7134_dev *dev, int loop,
499 unsigned long report, unsigned long status)
501 unsigned int i;
503 irq_dbg(1, "[%d,%ld]: r=0x%lx s=0x%02lx",
504 loop, jiffies, report, status);
505 for (i = 0; i < IRQBITS; i++) {
506 if (!(report & (1 << i)))
507 continue;
508 pr_cont(" %s", irqbits[i]);
510 if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
511 pr_cont(" | RA0=%s,%s,%s,%ld",
512 (status & 0x40) ? "vbi" : "video",
513 (status & 0x20) ? "b" : "a",
514 (status & 0x10) ? "odd" : "even",
515 (status & 0x0f));
517 pr_cont("\n");
520 static irqreturn_t saa7134_irq(int irq, void *dev_id)
522 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
523 unsigned long report,status;
524 int loop, handled = 0;
526 if (dev->insuspend)
527 goto out;
529 for (loop = 0; loop < 10; loop++) {
530 report = saa_readl(SAA7134_IRQ_REPORT);
531 status = saa_readl(SAA7134_IRQ_STATUS);
533 /* If dmasound support is active and we get a sound report,
534 * mask out the report and let the saa7134-alsa module deal
535 * with it */
536 if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
537 (dev->dmasound.priv_data != NULL) )
539 irq_dbg(2, "preserving DMA sound interrupt\n");
540 report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
543 if (0 == report) {
544 irq_dbg(2, "no (more) work\n");
545 goto out;
548 handled = 1;
549 saa_writel(SAA7134_IRQ_REPORT,report);
550 if (irq_debug)
551 print_irqstatus(dev,loop,report,status);
554 if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
555 (report & SAA7134_IRQ_REPORT_INTL))
556 saa7134_irq_video_signalchange(dev);
559 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
560 (status & 0x60) == 0)
561 saa7134_irq_video_done(dev,status);
563 if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
564 (status & 0x40) == 0x40)
565 saa7134_irq_vbi_done(dev,status);
567 if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
568 card_has_mpeg(dev)) {
569 if (dev->mops->irq_ts_done != NULL)
570 dev->mops->irq_ts_done(dev, status);
571 else
572 saa7134_irq_ts_done(dev, status);
575 if (report & SAA7134_IRQ_REPORT_GPIO16) {
576 switch (dev->has_remote) {
577 case SAA7134_REMOTE_GPIO:
578 if (!dev->remote)
579 break;
580 if (dev->remote->mask_keydown & 0x10000) {
581 saa7134_input_irq(dev);
583 break;
585 case SAA7134_REMOTE_I2C:
586 break; /* FIXME: invoke I2C get_key() */
588 default: /* GPIO16 not used by IR remote */
589 break;
593 if (report & SAA7134_IRQ_REPORT_GPIO18) {
594 switch (dev->has_remote) {
595 case SAA7134_REMOTE_GPIO:
596 if (!dev->remote)
597 break;
598 if ((dev->remote->mask_keydown & 0x40000) ||
599 (dev->remote->mask_keyup & 0x40000)) {
600 saa7134_input_irq(dev);
602 break;
604 case SAA7134_REMOTE_I2C:
605 break; /* FIXME: invoke I2C get_key() */
607 default: /* GPIO18 not used by IR remote */
608 break;
613 if (10 == loop) {
614 print_irqstatus(dev,loop,report,status);
615 if (report & SAA7134_IRQ_REPORT_PE) {
616 /* disable all parity error */
617 pr_warn("%s/irq: looping -- clearing PE (parity error!) enable bit\n",
618 dev->name);
619 saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
620 } else if (report & SAA7134_IRQ_REPORT_GPIO16) {
621 /* disable gpio16 IRQ */
622 pr_warn("%s/irq: looping -- clearing GPIO16 enable bit\n",
623 dev->name);
624 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_P);
625 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_N);
626 } else if (report & SAA7134_IRQ_REPORT_GPIO18) {
627 /* disable gpio18 IRQs */
628 pr_warn("%s/irq: looping -- clearing GPIO18 enable bit\n",
629 dev->name);
630 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
631 saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_N);
632 } else {
633 /* disable all irqs */
634 pr_warn("%s/irq: looping -- clearing all enable bits\n",
635 dev->name);
636 saa_writel(SAA7134_IRQ1,0);
637 saa_writel(SAA7134_IRQ2,0);
641 out:
642 return IRQ_RETVAL(handled);
645 /* ------------------------------------------------------------------ */
647 /* early init (no i2c, no irq) */
649 static int saa7134_hw_enable1(struct saa7134_dev *dev)
651 /* RAM FIFO config */
652 saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
653 saa_writel(SAA7134_THRESHOULD, 0x02020202);
655 /* enable audio + video processing */
656 saa_writel(SAA7134_MAIN_CTRL,
657 SAA7134_MAIN_CTRL_VPLLE |
658 SAA7134_MAIN_CTRL_APLLE |
659 SAA7134_MAIN_CTRL_EXOSC |
660 SAA7134_MAIN_CTRL_EVFE1 |
661 SAA7134_MAIN_CTRL_EVFE2 |
662 SAA7134_MAIN_CTRL_ESFE |
663 SAA7134_MAIN_CTRL_EBDAC);
666 * Initialize OSS _after_ enabling audio clock PLL and audio processing.
667 * OSS initialization writes to registers via the audio DSP; these
668 * writes will fail unless the audio clock has been started. At worst,
669 * audio will not work.
672 /* enable peripheral devices */
673 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
675 /* set vertical line numbering start (vbi needs this) */
676 saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
678 return 0;
681 static int saa7134_hwinit1(struct saa7134_dev *dev)
683 core_dbg("hwinit1\n");
685 saa_writel(SAA7134_IRQ1, 0);
686 saa_writel(SAA7134_IRQ2, 0);
688 /* Clear any stale IRQ reports */
689 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
691 mutex_init(&dev->lock);
692 spin_lock_init(&dev->slock);
694 saa7134_track_gpio(dev,"pre-init");
695 saa7134_video_init1(dev);
696 saa7134_vbi_init1(dev);
697 if (card_has_mpeg(dev))
698 saa7134_ts_init1(dev);
699 saa7134_input_init1(dev);
701 saa7134_hw_enable1(dev);
703 return 0;
706 /* late init (with i2c + irq) */
707 static int saa7134_hw_enable2(struct saa7134_dev *dev)
710 unsigned int irq2_mask;
712 /* enable IRQ's */
713 irq2_mask =
714 SAA7134_IRQ2_INTE_DEC3 |
715 SAA7134_IRQ2_INTE_DEC2 |
716 SAA7134_IRQ2_INTE_DEC1 |
717 SAA7134_IRQ2_INTE_DEC0 |
718 SAA7134_IRQ2_INTE_PE |
719 SAA7134_IRQ2_INTE_AR;
721 if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
722 if (dev->remote->mask_keydown & 0x10000)
723 irq2_mask |= SAA7134_IRQ2_INTE_GPIO16_N;
724 else { /* Allow enabling both IRQ edge triggers */
725 if (dev->remote->mask_keydown & 0x40000)
726 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_P;
727 if (dev->remote->mask_keyup & 0x40000)
728 irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_N;
732 if (dev->has_remote == SAA7134_REMOTE_I2C) {
733 request_module("ir-kbd-i2c");
736 saa_writel(SAA7134_IRQ1, 0);
737 saa_writel(SAA7134_IRQ2, irq2_mask);
739 return 0;
742 static int saa7134_hwinit2(struct saa7134_dev *dev)
745 core_dbg("hwinit2\n");
747 saa7134_video_init2(dev);
748 saa7134_tvaudio_init2(dev);
750 saa7134_hw_enable2(dev);
752 return 0;
756 /* shutdown */
757 static int saa7134_hwfini(struct saa7134_dev *dev)
759 core_dbg("hwfini\n");
761 if (card_has_mpeg(dev))
762 saa7134_ts_fini(dev);
763 saa7134_input_fini(dev);
764 saa7134_vbi_fini(dev);
765 saa7134_tvaudio_fini(dev);
766 saa7134_video_fini(dev);
767 return 0;
770 static void must_configure_manually(int has_eeprom)
772 unsigned int i,p;
774 if (!has_eeprom)
775 pr_warn("saa7134: <rant>\n"
776 "saa7134: Congratulations! Your TV card vendor saved a few\n"
777 "saa7134: cents for a eeprom, thus your pci board has no\n"
778 "saa7134: subsystem ID and I can't identify it automatically\n"
779 "saa7134: </rant>\n"
780 "saa7134: I feel better now. Ok, here are the good news:\n"
781 "saa7134: You can use the card=<nr> insmod option to specify\n"
782 "saa7134: which board do you have. The list:\n");
783 else
784 pr_warn("saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
785 "saa7134: insmod option to specify which board do you have, but this is\n"
786 "saa7134: somewhat risky, as might damage your card. It is better to ask\n"
787 "saa7134: for support at linux-media@vger.kernel.org.\n"
788 "saa7134: The supported cards are:\n");
790 for (i = 0; i < saa7134_bcount; i++) {
791 pr_warn("saa7134: card=%d -> %-40.40s",
792 i,saa7134_boards[i].name);
793 for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
794 if (saa7134_pci_tbl[p].driver_data != i)
795 continue;
796 pr_cont(" %04x:%04x",
797 saa7134_pci_tbl[p].subvendor,
798 saa7134_pci_tbl[p].subdevice);
800 pr_cont("\n");
804 static void saa7134_unregister_media_device(struct saa7134_dev *dev)
807 #ifdef CONFIG_MEDIA_CONTROLLER
808 if (!dev->media_dev)
809 return;
810 media_device_unregister(dev->media_dev);
811 media_device_cleanup(dev->media_dev);
812 kfree(dev->media_dev);
813 dev->media_dev = NULL;
814 #endif
817 static void saa7134_media_release(struct saa7134_dev *dev)
819 #ifdef CONFIG_MEDIA_CONTROLLER
820 int i;
822 for (i = 0; i < SAA7134_INPUT_MAX + 1; i++)
823 media_device_unregister_entity(&dev->input_ent[i]);
824 #endif
827 #if defined(CONFIG_MEDIA_CONTROLLER)
828 static void saa7134_create_entities(struct saa7134_dev *dev)
830 int ret, i;
831 struct media_entity *entity;
832 struct media_entity *decoder = NULL;
834 /* Check if it is using an external analog TV demod */
835 media_device_for_each_entity(entity, dev->media_dev) {
836 if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
837 decoder = entity;
838 break;
843 * saa713x is not using an external ATV demod.
844 * Register the internal one
846 if (!decoder) {
847 dev->demod.name = "saa713x";
848 dev->demod_pad[DEMOD_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
849 dev->demod_pad[DEMOD_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
850 dev->demod_pad[DEMOD_PAD_VBI_OUT].flags = MEDIA_PAD_FL_SOURCE;
851 dev->demod.function = MEDIA_ENT_F_ATV_DECODER;
853 ret = media_entity_pads_init(&dev->demod, DEMOD_NUM_PADS,
854 dev->demod_pad);
855 if (ret < 0)
856 pr_err("failed to initialize demod pad!\n");
858 ret = media_device_register_entity(dev->media_dev, &dev->demod);
859 if (ret < 0)
860 pr_err("failed to register demod entity!\n");
862 dev->decoder = &dev->demod;
863 } else {
864 dev->decoder = decoder;
867 /* Initialize Video, VBI and Radio pads */
868 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
869 ret = media_entity_pads_init(&dev->video_dev->entity, 1,
870 &dev->video_pad);
871 if (ret < 0)
872 pr_err("failed to initialize video media entity!\n");
874 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
875 ret = media_entity_pads_init(&dev->vbi_dev->entity, 1,
876 &dev->vbi_pad);
877 if (ret < 0)
878 pr_err("failed to initialize vbi media entity!\n");
880 /* Create entities for each input connector */
881 for (i = 0; i < SAA7134_INPUT_MAX; i++) {
882 struct media_entity *ent = &dev->input_ent[i];
883 struct saa7134_input *in = &card_in(dev, i);
885 if (in->type == SAA7134_NO_INPUT)
886 break;
888 /* This input uses the S-Video connector */
889 if (in->type == SAA7134_INPUT_COMPOSITE_OVER_SVIDEO)
890 continue;
892 ent->name = saa7134_input_name[in->type];
893 ent->flags = MEDIA_ENT_FL_CONNECTOR;
894 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
896 switch (in->type) {
897 case SAA7134_INPUT_COMPOSITE:
898 case SAA7134_INPUT_COMPOSITE0:
899 case SAA7134_INPUT_COMPOSITE1:
900 case SAA7134_INPUT_COMPOSITE2:
901 case SAA7134_INPUT_COMPOSITE3:
902 case SAA7134_INPUT_COMPOSITE4:
903 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
904 break;
905 case SAA7134_INPUT_SVIDEO:
906 case SAA7134_INPUT_SVIDEO0:
907 case SAA7134_INPUT_SVIDEO1:
908 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
909 break;
910 default:
912 * SAA7134_INPUT_TV and SAA7134_INPUT_TV_MONO.
914 * Please notice that neither SAA7134_INPUT_MUTE or
915 * SAA7134_INPUT_RADIO are defined at
916 * saa7134_board.input.
918 ent->function = MEDIA_ENT_F_CONN_RF;
919 break;
922 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
923 if (ret < 0)
924 pr_err("failed to initialize input pad[%d]!\n", i);
926 ret = media_device_register_entity(dev->media_dev, ent);
927 if (ret < 0)
928 pr_err("failed to register input entity %d!\n", i);
931 /* Create input for Radio RF connector */
932 if (card_has_radio(dev)) {
933 struct saa7134_input *in = &saa7134_boards[dev->board].radio;
934 struct media_entity *ent = &dev->input_ent[i];
936 ent->name = saa7134_input_name[in->type];
937 ent->flags = MEDIA_ENT_FL_CONNECTOR;
938 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
939 ent->function = MEDIA_ENT_F_CONN_RF;
941 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
942 if (ret < 0)
943 pr_err("failed to initialize input pad[%d]!\n", i);
945 ret = media_device_register_entity(dev->media_dev, ent);
946 if (ret < 0)
947 pr_err("failed to register input entity %d!\n", i);
950 #endif
952 static struct video_device *vdev_init(struct saa7134_dev *dev,
953 struct video_device *template,
954 char *type)
956 struct video_device *vfd;
958 vfd = video_device_alloc();
959 if (NULL == vfd)
960 return NULL;
961 *vfd = *template;
962 vfd->v4l2_dev = &dev->v4l2_dev;
963 vfd->release = video_device_release;
964 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
965 dev->name, type, saa7134_boards[dev->board].name);
966 video_set_drvdata(vfd, dev);
967 return vfd;
970 static void saa7134_unregister_video(struct saa7134_dev *dev)
972 saa7134_media_release(dev);
974 if (dev->video_dev) {
975 if (video_is_registered(dev->video_dev))
976 video_unregister_device(dev->video_dev);
977 else
978 video_device_release(dev->video_dev);
979 dev->video_dev = NULL;
981 if (dev->vbi_dev) {
982 if (video_is_registered(dev->vbi_dev))
983 video_unregister_device(dev->vbi_dev);
984 else
985 video_device_release(dev->vbi_dev);
986 dev->vbi_dev = NULL;
988 if (dev->radio_dev) {
989 if (video_is_registered(dev->radio_dev))
990 video_unregister_device(dev->radio_dev);
991 else
992 video_device_release(dev->radio_dev);
993 dev->radio_dev = NULL;
997 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
998 struct saa7134_dev *dev)
1000 int err;
1002 if (NULL != dev->mops)
1003 return;
1004 if (saa7134_boards[dev->board].mpeg != ops->type)
1005 return;
1006 err = ops->init(dev);
1007 if (0 != err)
1008 return;
1009 dev->mops = ops;
1012 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
1013 struct saa7134_dev *dev)
1015 if (NULL == dev->mops)
1016 return;
1017 if (dev->mops != ops)
1018 return;
1019 dev->mops->fini(dev);
1020 dev->mops = NULL;
1023 static int saa7134_initdev(struct pci_dev *pci_dev,
1024 const struct pci_device_id *pci_id)
1026 struct saa7134_dev *dev;
1027 struct saa7134_mpeg_ops *mops;
1028 int err;
1030 if (saa7134_devcount == SAA7134_MAXBOARDS)
1031 return -ENOMEM;
1033 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1034 if (NULL == dev)
1035 return -ENOMEM;
1037 dev->nr = saa7134_devcount;
1038 sprintf(dev->name, "saa%x[%d]", pci_dev->device, dev->nr);
1040 #ifdef CONFIG_MEDIA_CONTROLLER
1041 dev->media_dev = kzalloc(sizeof(*dev->media_dev), GFP_KERNEL);
1042 if (!dev->media_dev) {
1043 err = -ENOMEM;
1044 goto fail0;
1046 media_device_pci_init(dev->media_dev, pci_dev, dev->name);
1047 dev->v4l2_dev.mdev = dev->media_dev;
1048 #endif
1050 err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
1051 if (err)
1052 goto fail0;
1054 /* pci init */
1055 dev->pci = pci_dev;
1056 if (pci_enable_device(pci_dev)) {
1057 err = -EIO;
1058 goto fail1;
1061 /* pci quirks */
1062 if (pci_pci_problems) {
1063 if (pci_pci_problems & PCIPCI_TRITON)
1064 pr_info("%s: quirk: PCIPCI_TRITON\n", dev->name);
1065 if (pci_pci_problems & PCIPCI_NATOMA)
1066 pr_info("%s: quirk: PCIPCI_NATOMA\n", dev->name);
1067 if (pci_pci_problems & PCIPCI_VIAETBF)
1068 pr_info("%s: quirk: PCIPCI_VIAETBF\n", dev->name);
1069 if (pci_pci_problems & PCIPCI_VSFX)
1070 pr_info("%s: quirk: PCIPCI_VSFX\n", dev->name);
1071 #ifdef PCIPCI_ALIMAGIK
1072 if (pci_pci_problems & PCIPCI_ALIMAGIK) {
1073 pr_info("%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
1074 dev->name);
1075 latency = 0x0A;
1077 #endif
1078 if (pci_pci_problems & (PCIPCI_FAIL|PCIAGP_FAIL)) {
1079 pr_info("%s: quirk: this driver and your chipset may not work together in overlay mode.\n",
1080 dev->name);
1081 if (!saa7134_no_overlay) {
1082 pr_info("%s: quirk: overlay mode will be disabled.\n",
1083 dev->name);
1084 saa7134_no_overlay = 1;
1085 } else {
1086 pr_info("%s: quirk: overlay mode will be forced. Use this option at your own risk.\n",
1087 dev->name);
1091 if (UNSET != latency) {
1092 pr_info("%s: setting pci latency timer to %d\n",
1093 dev->name,latency);
1094 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
1097 /* print pci info */
1098 dev->pci_rev = pci_dev->revision;
1099 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1100 pr_info("%s: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1101 dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1102 dev->pci_lat,
1103 (unsigned long long)pci_resource_start(pci_dev, 0));
1104 pci_set_master(pci_dev);
1105 err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
1106 if (err) {
1107 pr_warn("%s: Oops: no 32bit PCI DMA ???\n", dev->name);
1108 goto fail1;
1111 /* board config */
1112 dev->board = pci_id->driver_data;
1113 if ((unsigned)card[dev->nr] < saa7134_bcount)
1114 dev->board = card[dev->nr];
1115 if (SAA7134_BOARD_UNKNOWN == dev->board)
1116 must_configure_manually(0);
1117 else if (SAA7134_BOARD_NOAUTO == dev->board) {
1118 must_configure_manually(1);
1119 dev->board = SAA7134_BOARD_UNKNOWN;
1121 dev->autodetected = card[dev->nr] != dev->board;
1122 dev->tuner_type = saa7134_boards[dev->board].tuner_type;
1123 dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
1124 dev->radio_type = saa7134_boards[dev->board].radio_type;
1125 dev->radio_addr = saa7134_boards[dev->board].radio_addr;
1126 dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
1127 if (UNSET != tuner[dev->nr])
1128 dev->tuner_type = tuner[dev->nr];
1129 pr_info("%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
1130 dev->name,pci_dev->subsystem_vendor,
1131 pci_dev->subsystem_device,saa7134_boards[dev->board].name,
1132 dev->board, dev->autodetected ?
1133 "autodetected" : "insmod option");
1135 /* get mmio */
1136 if (!request_mem_region(pci_resource_start(pci_dev,0),
1137 pci_resource_len(pci_dev,0),
1138 dev->name)) {
1139 err = -EBUSY;
1140 pr_err("%s: can't get MMIO memory @ 0x%llx\n",
1141 dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
1142 goto fail1;
1144 dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
1145 pci_resource_len(pci_dev, 0));
1146 dev->bmmio = (__u8 __iomem *)dev->lmmio;
1147 if (NULL == dev->lmmio) {
1148 err = -EIO;
1149 pr_err("%s: can't ioremap() MMIO memory\n",
1150 dev->name);
1151 goto fail2;
1154 /* initialize hardware #1 */
1155 saa7134_board_init1(dev);
1156 saa7134_hwinit1(dev);
1158 /* get irq */
1159 err = request_irq(pci_dev->irq, saa7134_irq,
1160 IRQF_SHARED, dev->name, dev);
1161 if (err < 0) {
1162 pr_err("%s: can't get IRQ %d\n",
1163 dev->name,pci_dev->irq);
1164 goto fail3;
1167 /* wait a bit, register i2c bus */
1168 msleep(100);
1169 saa7134_i2c_register(dev);
1170 saa7134_board_init2(dev);
1172 saa7134_hwinit2(dev);
1174 /* load i2c helpers */
1175 if (card_is_empress(dev)) {
1176 dev->empress_sd =
1177 v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1178 "saa6752hs",
1179 saa7134_boards[dev->board].empress_addr, NULL);
1181 if (dev->empress_sd)
1182 dev->empress_sd->grp_id = GRP_EMPRESS;
1185 if (saa7134_boards[dev->board].rds_addr) {
1186 struct v4l2_subdev *sd;
1188 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1189 &dev->i2c_adap, "saa6588",
1190 0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1191 if (sd) {
1192 pr_info("%s: found RDS decoder\n", dev->name);
1193 dev->has_rds = 1;
1197 mutex_lock(&saa7134_devlist_lock);
1198 list_for_each_entry(mops, &mops_list, next)
1199 mpeg_ops_attach(mops, dev);
1200 list_add_tail(&dev->devlist, &saa7134_devlist);
1201 mutex_unlock(&saa7134_devlist_lock);
1203 /* check for signal */
1204 saa7134_irq_video_signalchange(dev);
1206 if (TUNER_ABSENT != dev->tuner_type)
1207 saa_call_all(dev, core, s_power, 0);
1209 /* register v4l devices */
1210 if (saa7134_no_overlay > 0)
1211 pr_info("%s: Overlay support disabled.\n", dev->name);
1213 dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1214 dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1215 dev->video_dev->lock = &dev->lock;
1216 dev->video_dev->queue = &dev->video_vbq;
1217 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1218 video_nr[dev->nr]);
1219 if (err < 0) {
1220 pr_info("%s: can't register video device\n",
1221 dev->name);
1222 goto fail4;
1224 pr_info("%s: registered device %s [v4l2]\n",
1225 dev->name, video_device_node_name(dev->video_dev));
1227 dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1228 dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1229 dev->vbi_dev->lock = &dev->lock;
1230 dev->vbi_dev->queue = &dev->vbi_vbq;
1232 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1233 vbi_nr[dev->nr]);
1234 if (err < 0)
1235 goto fail4;
1236 pr_info("%s: registered device %s\n",
1237 dev->name, video_device_node_name(dev->vbi_dev));
1239 if (card_has_radio(dev)) {
1240 dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1241 dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1242 dev->radio_dev->lock = &dev->lock;
1243 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1244 radio_nr[dev->nr]);
1245 if (err < 0)
1246 goto fail4;
1247 pr_info("%s: registered device %s\n",
1248 dev->name, video_device_node_name(dev->radio_dev));
1251 #ifdef CONFIG_MEDIA_CONTROLLER
1252 saa7134_create_entities(dev);
1254 err = v4l2_mc_create_media_graph(dev->media_dev);
1255 if (err) {
1256 pr_err("failed to create media graph\n");
1257 goto fail4;
1259 #endif
1260 /* everything worked */
1261 saa7134_devcount++;
1263 if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1264 saa7134_dmasound_init(dev);
1266 request_submodules(dev);
1269 * Do it at the end, to reduce dynamic configuration changes during
1270 * the device init. Yet, as request_modules() can be async, the
1271 * topology will likely change after load the saa7134 subdrivers.
1273 #ifdef CONFIG_MEDIA_CONTROLLER
1274 err = media_device_register(dev->media_dev);
1275 if (err)
1276 goto fail4;
1277 #endif
1279 return 0;
1281 fail4:
1282 saa7134_unregister_video(dev);
1283 saa7134_i2c_unregister(dev);
1284 free_irq(pci_dev->irq, dev);
1285 fail3:
1286 saa7134_hwfini(dev);
1287 iounmap(dev->lmmio);
1288 fail2:
1289 release_mem_region(pci_resource_start(pci_dev,0),
1290 pci_resource_len(pci_dev,0));
1291 fail1:
1292 v4l2_device_unregister(&dev->v4l2_dev);
1293 fail0:
1294 #ifdef CONFIG_MEDIA_CONTROLLER
1295 kfree(dev->media_dev);
1296 #endif
1297 kfree(dev);
1298 return err;
1301 static void saa7134_finidev(struct pci_dev *pci_dev)
1303 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1304 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1305 struct saa7134_mpeg_ops *mops;
1307 flush_request_submodules(dev);
1309 /* Release DMA sound modules if present */
1310 if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1311 saa7134_dmasound_exit(dev);
1314 /* debugging ... */
1315 if (irq_debug) {
1316 u32 report = saa_readl(SAA7134_IRQ_REPORT);
1317 u32 status = saa_readl(SAA7134_IRQ_STATUS);
1318 print_irqstatus(dev,42,report,status);
1321 /* disable peripheral devices */
1322 saa_writeb(SAA7134_SPECIAL_MODE,0);
1324 /* shutdown hardware */
1325 saa_writel(SAA7134_IRQ1,0);
1326 saa_writel(SAA7134_IRQ2,0);
1327 saa_writel(SAA7134_MAIN_CTRL,0);
1329 /* shutdown subsystems */
1330 saa7134_hwfini(dev);
1332 /* unregister */
1333 mutex_lock(&saa7134_devlist_lock);
1334 list_del(&dev->devlist);
1335 list_for_each_entry(mops, &mops_list, next)
1336 mpeg_ops_detach(mops, dev);
1337 mutex_unlock(&saa7134_devlist_lock);
1338 saa7134_devcount--;
1340 saa7134_i2c_unregister(dev);
1341 saa7134_unregister_video(dev);
1344 /* the DMA sound modules should be unloaded before reaching
1345 this, but just in case they are still present... */
1346 if (dev->dmasound.priv_data != NULL) {
1347 free_irq(pci_dev->irq, &dev->dmasound);
1348 dev->dmasound.priv_data = NULL;
1352 /* release resources */
1353 free_irq(pci_dev->irq, dev);
1354 iounmap(dev->lmmio);
1355 release_mem_region(pci_resource_start(pci_dev,0),
1356 pci_resource_len(pci_dev,0));
1358 v4l2_device_unregister(&dev->v4l2_dev);
1360 saa7134_unregister_media_device(dev);
1362 /* free memory */
1363 kfree(dev);
1366 #ifdef CONFIG_PM
1368 /* resends a current buffer in queue after resume */
1369 static int saa7134_buffer_requeue(struct saa7134_dev *dev,
1370 struct saa7134_dmaqueue *q)
1372 struct saa7134_buf *buf, *next;
1374 assert_spin_locked(&dev->slock);
1376 buf = q->curr;
1377 next = buf;
1378 core_dbg("buffer_requeue\n");
1380 if (!buf)
1381 return 0;
1383 core_dbg("buffer_requeue : resending active buffer\n");
1385 if (!list_empty(&q->queue))
1386 next = list_entry(q->queue.next, struct saa7134_buf,
1387 entry);
1388 buf->activate(dev, buf, next);
1390 return 0;
1393 static int saa7134_suspend(struct pci_dev *pci_dev , pm_message_t state)
1395 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1396 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1398 /* disable overlay - apps should enable it explicitly on resume*/
1399 dev->ovenable = 0;
1401 /* Disable interrupts, DMA, and rest of the chip*/
1402 saa_writel(SAA7134_IRQ1, 0);
1403 saa_writel(SAA7134_IRQ2, 0);
1404 saa_writel(SAA7134_MAIN_CTRL, 0);
1406 dev->insuspend = 1;
1407 synchronize_irq(pci_dev->irq);
1409 /* ACK interrupts once more, just in case,
1410 since the IRQ handler won't ack them anymore*/
1412 saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1414 /* Disable timeout timers - if we have active buffers, we will
1415 fill them on resume*/
1417 del_timer(&dev->video_q.timeout);
1418 del_timer(&dev->vbi_q.timeout);
1419 del_timer(&dev->ts_q.timeout);
1421 if (dev->remote)
1422 saa7134_ir_stop(dev);
1424 pci_save_state(pci_dev);
1425 pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
1427 return 0;
1430 static int saa7134_resume(struct pci_dev *pci_dev)
1432 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1433 struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1434 unsigned long flags;
1436 pci_set_power_state(pci_dev, PCI_D0);
1437 pci_restore_state(pci_dev);
1439 /* Do things that are done in saa7134_initdev ,
1440 except of initializing memory structures.*/
1442 saa7134_board_init1(dev);
1444 /* saa7134_hwinit1 */
1445 if (saa7134_boards[dev->board].video_out)
1446 saa7134_videoport_init(dev);
1447 if (card_has_mpeg(dev))
1448 saa7134_ts_init_hw(dev);
1449 if (dev->remote)
1450 saa7134_ir_start(dev);
1451 saa7134_hw_enable1(dev);
1453 msleep(100);
1455 saa7134_board_init2(dev);
1457 /*saa7134_hwinit2*/
1458 saa7134_set_tvnorm_hw(dev);
1459 saa7134_tvaudio_setmute(dev);
1460 saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1461 saa7134_tvaudio_init(dev);
1462 saa7134_enable_i2s(dev);
1463 saa7134_hw_enable2(dev);
1465 saa7134_irq_video_signalchange(dev);
1467 /*resume unfinished buffer(s)*/
1468 spin_lock_irqsave(&dev->slock, flags);
1469 saa7134_buffer_requeue(dev, &dev->video_q);
1470 saa7134_buffer_requeue(dev, &dev->vbi_q);
1471 saa7134_buffer_requeue(dev, &dev->ts_q);
1473 /* FIXME: Disable DMA audio sound - temporary till proper support
1474 is implemented*/
1476 dev->dmasound.dma_running = 0;
1478 /* start DMA now*/
1479 dev->insuspend = 0;
1480 smp_wmb();
1481 saa7134_set_dmabits(dev);
1482 spin_unlock_irqrestore(&dev->slock, flags);
1484 return 0;
1486 #endif
1488 /* ----------------------------------------------------------- */
1490 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1492 struct saa7134_dev *dev;
1494 mutex_lock(&saa7134_devlist_lock);
1495 list_for_each_entry(dev, &saa7134_devlist, devlist)
1496 mpeg_ops_attach(ops, dev);
1497 list_add_tail(&ops->next,&mops_list);
1498 mutex_unlock(&saa7134_devlist_lock);
1499 return 0;
1502 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1504 struct saa7134_dev *dev;
1506 mutex_lock(&saa7134_devlist_lock);
1507 list_del(&ops->next);
1508 list_for_each_entry(dev, &saa7134_devlist, devlist)
1509 mpeg_ops_detach(ops, dev);
1510 mutex_unlock(&saa7134_devlist_lock);
1513 EXPORT_SYMBOL(saa7134_ts_register);
1514 EXPORT_SYMBOL(saa7134_ts_unregister);
1516 /* ----------------------------------------------------------- */
1518 static struct pci_driver saa7134_pci_driver = {
1519 .name = "saa7134",
1520 .id_table = saa7134_pci_tbl,
1521 .probe = saa7134_initdev,
1522 .remove = saa7134_finidev,
1523 #ifdef CONFIG_PM
1524 .suspend = saa7134_suspend,
1525 .resume = saa7134_resume
1526 #endif
1529 static int __init saa7134_init(void)
1531 INIT_LIST_HEAD(&saa7134_devlist);
1532 pr_info("saa7130/34: v4l2 driver version %s loaded\n",
1533 SAA7134_VERSION);
1534 return pci_register_driver(&saa7134_pci_driver);
1537 static void __exit saa7134_fini(void)
1539 pci_unregister_driver(&saa7134_pci_driver);
1542 module_init(saa7134_init);
1543 module_exit(saa7134_fini);
1545 /* ----------------------------------------------------------- */
1547 EXPORT_SYMBOL(saa7134_set_gpio);
1548 EXPORT_SYMBOL(saa7134_boards);
1550 /* ----------------- for the DMA sound modules --------------- */
1552 EXPORT_SYMBOL(saa7134_dmasound_init);
1553 EXPORT_SYMBOL(saa7134_dmasound_exit);
1554 EXPORT_SYMBOL(saa7134_pgtable_free);
1555 EXPORT_SYMBOL(saa7134_pgtable_build);
1556 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1557 EXPORT_SYMBOL(saa7134_set_dmabits);