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